Go to the documentation of this file.
125 ost->enc_ctx->pix_fmt) {
139 config->pix_fmt ==
ost->enc_ctx->pix_fmt)) {
141 "frames context (format %s) with %s encoder.\n",
143 ost->enc_ctx->codec->name);
145 if (!
ost->enc_ctx->hw_frames_ctx)
157 "(type %s) with %s encoder.\n", dev->
name,
160 if (!
ost->enc_ctx->hw_device_ctx)
170 const char *cname =
ost->enc_ctx->codec->name;
171 uint8_t *encoder_string;
172 int encoder_string_len;
178 encoder_string =
av_mallocz(encoder_string_len);
185 av_strlcpy(encoder_string,
"Lavc ", encoder_string_len);
186 av_strlcat(encoder_string, cname, encoder_string_len);
202 if (
ost->enc_timebase.num)
204 "-enc_time_base not supported for subtitles, ignoring\n");
215 "Demuxing timebase not available - cannot use it for encoding\n");
219 switch (
ost->enc_timebase.num) {
223 default:
tb =
ost->enc_timebase;
break;
231 fr =
ost->frame_rate;
236 if (!fr.
num && !
ost->max_frame_rate.num) {
240 "about the input framerate is available. Falling "
241 "back to a default value of 25fps. Use the -r option "
242 "if you want a different framerate.\n");
245 if (
ost->max_frame_rate.num &&
248 fr =
ost->max_frame_rate;
267 "Please consider specifying a lower framerate, a different muxer or "
268 "setting vsync/fps_mode to vfr\n");
275 if (!(
tb.num > 0 &&
tb.den > 0))
277 if (!(
tb.num > 0 &&
tb.den > 0))
330 if (
ost->bits_per_raw_sample)
341 ost->frame_aspect_ratio.num ?
343 frame->sample_aspect_ratio;
347 if (
ost->bits_per_raw_sample)
362 ||
ost->top_field_first >= 0
365 int top_field_first =
367 ost->top_field_first >= 0 ?
368 ost->top_field_first :
382 if (!enc_ctx->
width) {
383 enc_ctx->
width =
ost->ist->par->width;
419 "Encoding hardware device setup failed: %s\n",
av_err2str(
ret));
426 "incorrect parameters such as bit_rate, rate, width or height.\n");
432 if (
ost->sq_idx_encode >= 0) {
438 if (
ost->enc_ctx->frame_size) {
441 ost->sq_idx_encode,
ost->enc_ctx->frame_size);
448 if (
ost->enc_ctx->bit_rate &&
ost->enc_ctx->bit_rate < 1000 &&
451 " It takes bits/s as argument, not kbits/s\n");
456 "Error initializing the output stream codec context.\n");
460 if (
ost->enc_ctx->nb_coded_side_data) {
463 for (
i = 0;
i <
ost->enc_ctx->nb_coded_side_data;
i++) {
470 memcpy(dst_data, sd_src->
data, sd_src->
size);
522 int subtitle_out_max_size = 1024 * 1024;
523 int subtitle_out_size, nb,
i,
ret;
552 for (
i = 0;
i < nb;
i++) {
575 ost->frames_encoded++;
578 if (subtitle_out_size < 0) {
580 return subtitle_out_size;
615 int64_t ptsi = INT64_MAX;
674 return -10.0 * log10(
d);
684 int64_t frame_number;
685 double ti1,
bitrate, avg_bitrate;
686 double psnr_val = -1;
712 fprintf(
vstats_file,
"frame= %5"PRId64
" q= %2.1f ", frame_number,
715 fprintf(
vstats_file,
"out= %2d st= %2d frame= %5"PRId64
" q= %2.1f ",
ost->file_index,
ost->
index, frame_number,
730 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
743 const char *action =
frame ?
"encode" :
"flush";
747 if (
ost->enc_stats_pre.io)
749 ost->frames_encoded);
751 ost->frames_encoded++;
752 ost->samples_encoded +=
frame->nb_samples;
756 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
762 if (
frame->sample_aspect_ratio.num && !
ost->frame_aspect_ratio.num)
794 }
else if (
ret < 0) {
805 if (
ost->enc_stats_post.io)
811 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
812 "duration:%s duration_time:%s\n",
821 "Subtitle heartbeat logic failed in %s! (%s)\n",
844 if (
ost->sq_idx_encode < 0)
870 }
else if (
ret < 0) {
895 "Audio channel count changed and encoder does not support parameter changes\n");
943 if (float_pts !=
llrint(float_pts))
944 float_pts +=
FFSIGN(float_pts) * 1.0 / (1<<17);
948 frame->time_base = enc->time_base;
957 enc ? enc->time_base.num : -1,
958 enc ? enc->time_base.den : -1);
970 int64_t *nb_frames, int64_t *nb_frames_prev)
973 double delta0,
delta, sync_ipts;
1007 switch (
ost->vsync_method) {
1019 }
else if (
delta < -1.1)
1021 else if (
delta > 1.1) {
1024 *nb_frames_prev =
llrintf(delta0 - 0.6);
1026 frame->duration = 1;
1031 else if (
delta > 0.6)
1064 goto force_keyframe;
1065 }
else if (kf->
pexpr) {
1071 "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1085 goto force_keyframe;
1089 goto force_keyframe;
1093 goto force_keyframe;
1109 int64_t nb_frames, nb_frames_prev,
i;
1123 &nb_frames, &nb_frames_prev);
1125 if (nb_frames_prev == 0 &&
ost->last_dropped) {
1126 ost->nb_frames_drop++;
1128 "*** dropping frame %"PRId64
" at ts %"PRId64
"\n",
1131 if (nb_frames > (nb_frames_prev &&
ost->last_dropped) + (nb_frames > nb_frames_prev)) {
1134 ost->nb_frames_drop++;
1137 ost->nb_frames_dup += nb_frames - (nb_frames_prev &&
ost->last_dropped) - (nb_frames > nb_frames_prev);
1144 ost->last_dropped = nb_frames == nb_frames_prev &&
frame;
1148 for (
i = 0;
i < nb_frames;
i++) {
1151 if (i < nb_frames_prev && e->last_frame->buf[0]) {
1168 if (
ost->top_field_first >= 0) {
1210 if (
ost->sq_idx_encode >= 0)
1219 if (!enc || !e->
opened ||
static void error(const char *err)
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture, int dup_idx)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
static AVCodecContext * dec_ctx
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
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.
enum AVColorSpace colorspace
YUV colorspace type.
void close_output_stream(OutputStream *ost)
int64_t frames_prev_hist[3]
int enc_open(OutputStream *ost, AVFrame *frame)
int sample_rate
samples per second
@ KF_FORCE_SOURCE_NO_DROP
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
float frame_drop_threshold
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int64_t start_time
start time in microseconds == AV_TIME_BASE units
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int capabilities
Codec capabilities.
int depth
Number of bits in the component.
enum AVFieldOrder field_order
Field order.
static int do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
AVRational avg_frame_rate
Average framerate.
uint8_t * subtitle_header
Header containing style information for text subtitles.
#define AV_LOG_VERBOSE
Detailed information.
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure clockwise rotation by the specified angle (in de...
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
int nb_channels
Number of channels in this layout.
static int set_encoder_id(OutputFile *of, OutputStream *ost)
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
static double psnr(double d)
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
const struct AVCodec * codec
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
AVChannelLayout ch_layout
Audio channel layout.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int flags
AV_CODEC_FLAG_*.
int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
void update_benchmark(const char *fmt,...)
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Read a frame from the queue.
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
@ AV_CODEC_ID_DVB_SUBTITLE
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
AVRational frame_rate_filter
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
EncStatsComponent * components
const AVRational * supported_framerates
array of supported framerates, or NULL if any, array is terminated by {0,0}
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
int global_quality
Global quality for codecs which cannot change it per frame.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#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_assert0(cond)
assert() equivalent, that is always enabled.
int enc_frame(OutputStream *ost, AVFrame *frame)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void enc_stats_write(OutputStream *ost, EncStats *es, const AVFrame *frame, const AVPacket *pkt, uint64_t frame_num)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int64_t pts
Same as packet pts, in AV_TIME_BASE.
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
double expr_const_values[FKF_NB]
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Rational number (pair of numerator and denominator).
static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
enum AVPacketSideDataType type
@ AV_PICTURE_TYPE_I
Intra.
OutputStream * ost_iter(OutputStream *prev)
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
void avio_w8(AVIOContext *s, int b)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, int frame_samples)
Set a constant output audio frame size, in samples.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost, AVFrame *frame)
char * stats_out
pass1 encoding statistics output buffer
enum AVPictureType pict_type
Picture type of the frame.
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
OutputFile ** output_files
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int64_t start_time
enum AVSampleFormat sample_fmt
audio sample format
int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
Submit a frame for the stream with index stream_idx.
#define AV_NOPTS_VALUE
Undefined timestamp value.
int check_avoptions(AVDictionary *m)
static int submit_encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
@ AV_PICTURE_TYPE_NONE
Undefined.
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
uint32_t end_display_time
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
@ AV_FIELD_TT
Top coded_first, top displayed first.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
#define AV_LOG_INFO
Standard information.
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
int av_find_nearest_q_idx(AVRational q, const AVRational *q_list)
Find the value in a list of rationals nearest a given reference rational.
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
This struct describes a set or pool of "hardware" frames (i.e.
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
int nb_side_data
The number of elements in the AVStream.side_data array.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
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.
main external API structure.
int index
stream index in AVFormatContext
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
const AVCodecHWConfig * avcodec_get_hw_config(const AVCodec *codec, int index)
Retrieve supported hardware configurations for a codec.
void enc_free(Encoder **penc)
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
enum AVMediaType codec_type
A reference to a data buffer.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
This structure stores compressed data.
int64_t vsync_frame_number
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
@ AV_FIELD_TB
Top coded first, bottom displayed first.
uint8_t * av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
int width
picture width / height.
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
float dts_error_threshold
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
const AVOutputFormat * format
static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame, double duration, int64_t *nb_frames, int64_t *nb_frames_prev)
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
@ AV_FIELD_BT
Bottom coded first, top displayed first.
int of_stream_init(OutputFile *of, OutputStream *ost)
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
uint32_t start_display_time
AVRational time_base
Time base of the packet's timestamps.
int enc_alloc(Encoder **penc, const AVCodec *codec)
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.