96 .class_name =
"Encoder",
98 .parent_log_context_offset = offsetof(
EncoderPriv, log_parent),
105 char *stats_in =
NULL;
126 Scheduler *sch,
unsigned sch_idx,
void *log_parent)
173 for (
int i = 0;;
i++) {
181 config->pix_fmt == enc_ctx->
pix_fmt)) {
183 "frames context (format %s) with %s encoder.\n",
199 "(type %s) with %s encoder.\n", dev->
name,
253 if (extra_encoder_opts) {
254 threads_manual |= !!
av_dict_get(*extra_encoder_opts,
"threads",
NULL, 0);
280 if (
ost->enc->codec_tag)
295 frame->sample_rate > 0 &&
296 frame->ch_layout.nb_channels > 0);
306 if (
ost->bits_per_raw_sample)
320 ost->frame_aspect_ratio.num ?
322 frame->sample_aspect_ratio;
326 if (
ost->bits_per_raw_sample)
348 "Requested chroma sample location '%s' does not match the "
349 "frame tagged sample location '%s'; result may be incorrect.\n",
370 if (!enc_ctx->
width) {
371 enc_ctx->
width =
ost->ist->par->width;
403 "Encoding hardware device setup failed: %s\n",
av_err2str(ret));
410 "incorrect parameters such as bit_rate, rate, width or height.\n");
419 " It takes bits/s as argument, not kbits/s\n");
467 int subtitle_out_max_size = 1024 * 1024;
468 int subtitle_out_size, nb,
i, ret;
495 for (
i = 0;
i < nb;
i++) {
521 if (subtitle_out_size < 0) {
523 return subtitle_out_size;
603 "K" :
"N", 1);
continue;
624static inline double psnr(
double d)
626 return -10.0 * log10(d);
638 double ti1,
bitrate, avg_bitrate;
639 double psnr_val = -1;
668 fprintf(
vstats_file,
"frame= %5"PRId64
" q= %2.1f ", frame_number,
671 fprintf(
vstats_file,
"out= %2d st= %2d frame= %5"PRId64
" q= %2.1f ",
672 ost->file->index,
ost->index, frame_number,
687 fprintf(
vstats_file,
"s_size= %8.0fKiB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
701 const char *action =
frame ?
"encode" :
"flush";
712 if (
ost->enc_stats_pre.io)
721 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
727 if (
frame->sample_aspect_ratio.num && !
ost->frame_aspect_ratio.num)
758 }
else if (ret < 0) {
801 if (
ost->enc_stats_post.io)
807 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
808 "duration:%s duration_time:%s\n",
842 }
else if (kf->
pexpr) {
848 "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
904 "Audio channel count changed and encoder does not support parameter changes\n");
917 ost->enc->enc_ctx->codec->name);
926 memset(et, 0,
sizeof(*et));
931 memset(et, 0,
sizeof(*et));
987 " Trying to restart it instead\n");
1016 int ret = 0, input_status = 0;
1036 while (!input_status) {
1038 if (input_status < 0) {
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
void avio_w8(AVIOContext *s, int b)
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Convenience header that includes libavutil's core.
#define i(width, name, range_min, range_max)
int check_avoptions(AVDictionary *m)
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
AVCodecParameters * avcodec_parameters_alloc(void)
void avcodec_parameters_free(AVCodecParameters **ppar)
@ AV_FIELD_TT
Top coded_first, top displayed first.
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
@ AV_FIELD_BT
Bottom coded first, top displayed first.
@ AV_FIELD_TB
Top coded first, bottom displayed first.
#define atomic_store(object, desired)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
simple arithmetic expression evaluator
const FrameData * frame_data_c(AVFrame *frame)
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
FrameData * packet_data(AVPacket *pkt)
void update_benchmark(const char *fmt,...)
int of_stream_init(OutputFile *of, OutputStream *ost, const AVCodecContext *enc_ctx)
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
static void enc_thread_uninit(EncoderThread *et)
static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx, AVBufferRef *frames_ref)
static int enc_realloc(Encoder *enc, const AVCodec *codec)
static int enc_thread_init(EncoderThread *et)
int enc_loopback(Encoder *enc)
static double psnr(double d)
static const AVClass enc_class
static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
int enc_alloc(Encoder **penc, const AVCodec *codec, Scheduler *sch, unsigned sch_idx, void *log_parent)
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, const AVFrame *frame)
static int enc_reopen(void *opaque, const AVFrame *frame, AVDictionary **extra_encoder_opts)
int encoder_thread(void *arg)
int enc_open(void *opaque, const AVFrame *frame)
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
static int flush_encoder(OutputStream *ost, EncoderThread *et)
static int apply_enc_options(Encoder *e, AVDictionary **opts)
static const char * enc_item_name(void *obj)
static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub, AVPacket *pkt)
static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
void enc_free(Encoder **penc)
static EncoderPriv * ep_from_enc(Encoder *enc)
static void enc_thread_set_name(const OutputStream *ost)
static int reinit_encoder(OutputStream *ost, EncoderThread *et)
void enc_stats_write(OutputStream *ost, EncStats *es, const AVFrame *frame, const AVPacket *pkt, uint64_t frame_num)
int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
Called by encoder tasks to obtain frames for encoding.
int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
Called by encoder tasks to send encoded packets downstream.
static int clone_side_data(AVFrameSideData ***dst, int *nb_dst, AVFrameSideData *const *src, int nb_src, unsigned int flags)
Wrapper calling av_frame_side_data_clone() in a loop for all source entries.
reference-counted frame API
int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **options)
Try to reconfigure the encoder with the provided dictionary.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_ENCODER_RECONF
Encoder can be reconfigured by passing new initialization parameters.
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define AV_CODEC_FLAG_FRAME_DURATION
Signal to the encoder that the values of AVFrame.duration are valid and should be used (typically for...
#define AV_CODEC_FLAG2_FIXED_FRAME_SIZE
Force audio encoders to use a fixed frame size.
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
const AVCodecHWConfig * avcodec_get_hw_config(const AVCodec *codec, int index)
Retrieve supported hardware configurations for a codec.
#define AV_CODEC_FLAG_PSNR
error[?
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
#define AV_CODEC_FLAG_COPY_OPAQUE
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
@ AV_CODEC_ID_DVB_SUBTITLE
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_FRAME_SIDE_DATA_FLAG_UNIQUE
Remove existing entries before adding new ones.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_NONE
Undefined.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
#define LIBAVUTIL_VERSION_INT
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
static int ff_thread_setname(const char *name)
Memory handling functions.
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
const char * av_chroma_location_name(enum AVChromaLocation location)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVCHROMA_LOC_UNSPECIFIED
Utilities for rational number calculation.
A reference to a data buffer.
uint8_t * data
The data buffer.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int subtitle_header_size
Header containing style information for text subtitles.
int width
picture width / height.
char * stats_out
pass1 encoding statistics output buffer
AVChannelLayout ch_layout
Audio channel layout.
int flags2
AV_CODEC_FLAG2_*.
enum AVSampleFormat sample_fmt
audio sample format
int global_quality
Global quality for codecs which cannot change it per frame.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
enum AVMediaType codec_type
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
enum AVFieldOrder field_order
Field order.
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
enum AVColorSpace colorspace
YUV colorspace type.
int sample_rate
samples per second
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
uint8_t * subtitle_header
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
AVFrameSideData ** decoded_side_data
Array containing static side data, such as HDR10 CLL / MDCV structures.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
int capabilities
Codec capabilities.
This structure describes decoded (raw) audio or video data.
This struct describes a set or pool of "hardware" frames (i.e.
This structure stores compressed data.
Rational number (pair of numerator and denominator).
uint32_t start_display_time
uint32_t end_display_time
int64_t pts
Same as packet pts, in AV_TIME_BASE.
uint8_t * subtitle_header
EncStatsComponent * components
AVDictionary * encoder_opts
struct FrameData::@304126211346234154321045014345346376220164157123 dec
AVRational frame_rate_filter
int64_t wallclock[LATENCY_PROBE_NB]
AVCodecParameters * par_enc
AVFrameSideData ** side_data
AVDictionary * reinit_opts
double expr_const_values[FKF_NB]
int64_t start_time
start time in microseconds == AV_TIME_BASE units
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
static void error(const char *err)
static AVDictionary * opts
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static void copy(const float *p1, float *p2, const int length)
static const uint8_t quality[]