FFmpeg
ffmpeg_enc.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <math.h>
20 #include <stdint.h>
21 
22 #include "ffmpeg.h"
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/avutil.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/display.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/frame.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/log.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/rational.h"
35 #include "libavutil/timestamp.h"
36 
37 #include "libavcodec/avcodec.h"
38 
39 // FIXME private header, used for mid_pred()
40 #include "libavcodec/mathops.h"
41 
42 #include "libavformat/avformat.h"
43 
44 struct Encoder {
45  /* predicted pts of the next frame to be encoded */
46  int64_t next_pts;
47 
49  /* number of frames emitted by the video-encoding sync code */
51  /* history of nb_frames_prev, i.e. the number of times the
52  * previous frame was duplicated by vsync code in recent
53  * do_video_out() calls */
54  int64_t frames_prev_hist[3];
55 
57 
58  // packet for receiving encoded output
60 
61  // combined size of all the packets received from the encoder
62  uint64_t data_size;
63 
64  // number of packets received from the encoder
65  uint64_t packets_encoded;
66 
67  uint64_t dup_warning;
68 
69  int opened;
70 };
71 
72 void enc_free(Encoder **penc)
73 {
74  Encoder *enc = *penc;
75 
76  if (!enc)
77  return;
78 
80  av_frame_free(&enc->sq_frame);
81 
82  av_packet_free(&enc->pkt);
83 
84  av_freep(penc);
85 }
86 
87 int enc_alloc(Encoder **penc, const AVCodec *codec)
88 {
89  Encoder *enc;
90 
91  *penc = NULL;
92 
93  enc = av_mallocz(sizeof(*enc));
94  if (!enc)
95  return AVERROR(ENOMEM);
96 
97  if (codec->type == AVMEDIA_TYPE_VIDEO) {
98  enc->last_frame = av_frame_alloc();
99  if (!enc->last_frame)
100  goto fail;
101  }
102 
103  enc->pkt = av_packet_alloc();
104  if (!enc->pkt)
105  goto fail;
106 
107  enc->dup_warning = 1000;
108 
109  *penc = enc;
110 
111  return 0;
112 fail:
113  enc_free(&enc);
114  return AVERROR(ENOMEM);
115 }
116 
118 {
119  const AVCodecHWConfig *config;
120  HWDevice *dev = NULL;
121  int i;
122 
123  if (frames_ref &&
124  ((AVHWFramesContext*)frames_ref->data)->format ==
125  ost->enc_ctx->pix_fmt) {
126  // Matching format, will try to use hw_frames_ctx.
127  } else {
128  frames_ref = NULL;
129  }
130 
131  for (i = 0;; i++) {
132  config = avcodec_get_hw_config(ost->enc_ctx->codec, i);
133  if (!config)
134  break;
135 
136  if (frames_ref &&
138  (config->pix_fmt == AV_PIX_FMT_NONE ||
139  config->pix_fmt == ost->enc_ctx->pix_fmt)) {
140  av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
141  "frames context (format %s) with %s encoder.\n",
142  av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
143  ost->enc_ctx->codec->name);
144  ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
145  if (!ost->enc_ctx->hw_frames_ctx)
146  return AVERROR(ENOMEM);
147  return 0;
148  }
149 
150  if (!dev &&
152  dev = hw_device_get_by_type(config->device_type);
153  }
154 
155  if (dev) {
156  av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
157  "(type %s) with %s encoder.\n", dev->name,
158  av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name);
159  ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
160  if (!ost->enc_ctx->hw_device_ctx)
161  return AVERROR(ENOMEM);
162  } else {
163  // No device required, or no device available.
164  }
165  return 0;
166 }
167 
169 {
170  const char *cname = ost->enc_ctx->codec->name;
171  uint8_t *encoder_string;
172  int encoder_string_len;
173 
174  if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
175  return 0;
176 
177  encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
178  encoder_string = av_mallocz(encoder_string_len);
179  if (!encoder_string)
180  return AVERROR(ENOMEM);
181 
182  if (!of->bitexact && !ost->bitexact)
183  av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
184  else
185  av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
186  av_strlcat(encoder_string, cname, encoder_string_len);
187  av_dict_set(&ost->st->metadata, "encoder", encoder_string,
189 
190  return 0;
191 }
192 
194 {
195  const OutputFile *of = output_files[ost->file_index];
196  AVCodecContext *enc = ost->enc_ctx;
197  AVRational tb = (AVRational){ 0, 0 };
198  AVRational fr;
199  FrameData *fd;
200 
201  if (ost->type == AVMEDIA_TYPE_SUBTITLE) {
202  if (ost->enc_timebase.num)
204  "-enc_time_base not supported for subtitles, ignoring\n");
205  enc->time_base = AV_TIME_BASE_Q;
206  return 0;
207  }
208 
209  fd = frame_data(frame);
210 
211  // apply -enc_time_base
212  if (ost->enc_timebase.num == ENC_TIME_BASE_DEMUX &&
213  (fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0)) {
215  "Demuxing timebase not available - cannot use it for encoding\n");
216  return AVERROR(EINVAL);
217  }
218 
219  switch (ost->enc_timebase.num) {
220  case 0: break;
221  case ENC_TIME_BASE_DEMUX: tb = fd->dec.tb; break;
222  case ENC_TIME_BASE_FILTER: tb = frame->time_base; break;
223  default: tb = ost->enc_timebase; break;
224  }
225 
226  if (ost->type == AVMEDIA_TYPE_AUDIO) {
227  enc->time_base = tb.num ? tb : (AVRational){ 1, frame->sample_rate };
228  return 0;
229  }
230 
231  fr = ost->frame_rate;
232  if (!fr.num)
233  fr = fd->frame_rate_filter;
234 
235  if (ost->is_cfr) {
236  if (!fr.num && !ost->max_frame_rate.num) {
237  fr = (AVRational){25, 1};
239  "No information "
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");
243  }
244 
245  if (ost->max_frame_rate.num &&
246  (av_q2d(fr) > av_q2d(ost->max_frame_rate) ||
247  !fr.den))
248  fr = ost->max_frame_rate;
249  }
250 
251  if (fr.num > 0) {
252  if (enc->codec->supported_framerates && !ost->force_fps) {
253  int idx = av_find_nearest_q_idx(fr, enc->codec->supported_framerates);
254  fr = enc->codec->supported_framerates[idx];
255  }
256  // reduce frame rate for mpeg4 to be within the spec limits
257  if (enc->codec_id == AV_CODEC_ID_MPEG4) {
258  av_reduce(&fr.num, &fr.den,
259  fr.num, fr.den, 65535);
260  }
261  }
262 
263  if (av_q2d(fr) > 1e3 && ost->vsync_method != VSYNC_PASSTHROUGH &&
264  (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
265  (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
266  av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
267  "Please consider specifying a lower framerate, a different muxer or "
268  "setting vsync/fps_mode to vfr\n");
269  }
270 
271  enc->framerate = fr;
272 
273  ost->st->avg_frame_rate = fr;
274 
275  if (!(tb.num > 0 && tb.den > 0))
276  tb = av_inv_q(fr);
277  if (!(tb.num > 0 && tb.den > 0))
278  tb = frame->time_base;
279 
280  enc->time_base = tb;
281 
282  return 0;
283 }
284 
286 {
287  InputStream *ist = ost->ist;
288  Encoder *e = ost->enc;
289  AVCodecContext *enc_ctx = ost->enc_ctx;
291  const AVCodec *enc = enc_ctx->codec;
292  OutputFile *of = output_files[ost->file_index];
293  FrameData *fd;
294  int ret;
295 
296  if (e->opened)
297  return 0;
298 
299  // frame is always non-NULL for audio and video
301 
302  if (frame) {
303  fd = frame_data(frame);
304  if (!fd)
305  return AVERROR(ENOMEM);
306  }
307 
308  ret = set_encoder_id(output_files[ost->file_index], ost);
309  if (ret < 0)
310  return ret;
311 
312  if (ist) {
313  dec_ctx = ist->dec_ctx;
314  }
315 
317  if (ret < 0) {
318  av_log(ost, AV_LOG_ERROR, "Could not choose a time base for encoding\n");
319  return AVERROR(EINVAL);
320  }
321 
322  switch (enc_ctx->codec_type) {
323  case AVMEDIA_TYPE_AUDIO:
324  enc_ctx->sample_fmt = frame->format;
325  enc_ctx->sample_rate = frame->sample_rate;
326  ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
327  if (ret < 0)
328  return ret;
329 
330  if (ost->bits_per_raw_sample)
331  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
332  else
334  av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
335  break;
336 
337  case AVMEDIA_TYPE_VIDEO: {
338  enc_ctx->width = frame->width;
339  enc_ctx->height = frame->height;
341  ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
342  av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
343  frame->sample_aspect_ratio;
344 
345  enc_ctx->pix_fmt = frame->format;
346 
347  if (ost->bits_per_raw_sample)
348  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
349  else
351  av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
352 
353  enc_ctx->color_range = frame->color_range;
354  enc_ctx->color_primaries = frame->color_primaries;
355  enc_ctx->color_trc = frame->color_trc;
356  enc_ctx->colorspace = frame->colorspace;
357  enc_ctx->chroma_sample_location = frame->chroma_location;
358 
361 #if FFMPEG_OPT_TOP
362  || ost->top_field_first >= 0
363 #endif
364  ) {
365  int top_field_first =
366 #if FFMPEG_OPT_TOP
367  ost->top_field_first >= 0 ?
368  ost->top_field_first :
369 #endif
371 
372  if (enc->id == AV_CODEC_ID_MJPEG)
373  enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
374  else
375  enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
376  } else
378 
379  break;
380  }
382  if (!enc_ctx->width) {
383  enc_ctx->width = ost->ist->par->width;
384  enc_ctx->height = ost->ist->par->height;
385  }
386  if (dec_ctx && dec_ctx->subtitle_header) {
387  /* ASS code assumes this buffer is null terminated so add extra byte. */
389  if (!enc_ctx->subtitle_header)
390  return AVERROR(ENOMEM);
391  memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
394  }
395 
396  break;
397  default:
398  av_assert0(0);
399  break;
400  }
401 
402  if (ost->bitexact)
403  enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
404 
405  if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
406  av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
407 
409  ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
410  if (ret < 0)
411  return ret;
412  }
413 
414  av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
415 
416  ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL);
417  if (ret < 0) {
419  "Encoding hardware device setup failed: %s\n", av_err2str(ret));
420  return ret;
421  }
422 
423  if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
424  if (ret != AVERROR_EXPERIMENTAL)
425  av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
426  "incorrect parameters such as bit_rate, rate, width or height.\n");
427  return ret;
428  }
429 
430  e->opened = 1;
431 
432  if (ost->sq_idx_encode >= 0) {
433  e->sq_frame = av_frame_alloc();
434  if (!e->sq_frame)
435  return AVERROR(ENOMEM);
436  }
437 
438  if (ost->enc_ctx->frame_size) {
439  av_assert0(ost->sq_idx_encode >= 0);
441  ost->sq_idx_encode, ost->enc_ctx->frame_size);
442  }
443 
444  ret = check_avoptions(ost->encoder_opts);
445  if (ret < 0)
446  return ret;
447 
448  if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
449  ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
450  av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
451  " It takes bits/s as argument, not kbits/s\n");
452 
453  ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
454  if (ret < 0) {
456  "Error initializing the output stream codec context.\n");
457  return ret;
458  }
459 
460  if (ost->enc_ctx->nb_coded_side_data) {
461  int i;
462 
463  for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
464  const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
465  uint8_t *dst_data;
466 
467  dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
468  if (!dst_data)
469  return AVERROR(ENOMEM);
470  memcpy(dst_data, sd_src->data, sd_src->size);
471  }
472  }
473 
474  /*
475  * Add global input side data. For now this is naive, and copies it
476  * from the input stream's global side data. All side data should
477  * really be funneled over AVFrame and libavfilter, then added back to
478  * packet side data, and then potentially using the first packet for
479  * global side data.
480  */
481  if (ist) {
482  int i;
483  for (i = 0; i < ist->st->nb_side_data; i++) {
484  AVPacketSideData *sd = &ist->st->side_data[i];
485  if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
486  uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
487  if (!dst)
488  return AVERROR(ENOMEM);
489  memcpy(dst, sd->data, sd->size);
490  if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
491  av_display_rotation_set((int32_t *)dst, 0);
492  }
493  }
494  }
495 
496  // copy timebase while removing common factors
497  if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
498  ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
499 
500  ret = of_stream_init(of, ost);
501  if (ret < 0)
502  return ret;
503 
504  return 0;
505 }
506 
508 {
509  OutputFile *of = output_files[ost->file_index];
510 
511  if (of->recording_time != INT64_MAX &&
512  av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
514  return 0;
515  }
516  return 1;
517 }
518 
520 {
521  Encoder *e = ost->enc;
522  int subtitle_out_max_size = 1024 * 1024;
523  int subtitle_out_size, nb, i, ret;
524  AVCodecContext *enc;
525  AVPacket *pkt = e->pkt;
526  int64_t pts;
527 
528  if (sub->pts == AV_NOPTS_VALUE) {
529  av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
530  return exit_on_error ? AVERROR(EINVAL) : 0;
531  }
532  if (ost->finished ||
533  (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
534  return 0;
535 
536  enc = ost->enc_ctx;
537 
538  /* Note: DVB subtitle need one packet to draw them and one other
539  packet to clear them */
540  /* XXX: signal it in the codec context ? */
542  nb = 2;
543  else if (enc->codec_id == AV_CODEC_ID_ASS)
544  nb = FFMAX(sub->num_rects, 1);
545  else
546  nb = 1;
547 
548  /* shift timestamp to honor -ss and make check_recording_time() work with -t */
549  pts = sub->pts;
550  if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
551  pts -= output_files[ost->file_index]->start_time;
552  for (i = 0; i < nb; i++) {
553  AVSubtitle local_sub = *sub;
554 
556  return 0;
557 
558  ret = av_new_packet(pkt, subtitle_out_max_size);
559  if (ret < 0)
560  return AVERROR(ENOMEM);
561 
562  local_sub.pts = pts;
563  // start_display_time is required to be 0
564  local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
565  local_sub.end_display_time -= sub->start_display_time;
566  local_sub.start_display_time = 0;
567 
568  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
569  local_sub.num_rects = 0;
570  else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
571  local_sub.num_rects = 1;
572  local_sub.rects += i;
573  }
574 
575  ost->frames_encoded++;
576 
577  subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
578  if (subtitle_out_size < 0) {
579  av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
580  return subtitle_out_size;
581  }
582 
583  av_shrink_packet(pkt, subtitle_out_size);
585  pkt->pts = sub->pts;
587  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
588  /* XXX: the pts correction is handled here. Maybe handling
589  it in the codec would be better */
590  if (i == 0)
591  pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
592  else
593  pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
594  }
595  pkt->dts = pkt->pts;
596 
597  ret = of_output_packet(of, ost, pkt);
598  if (ret < 0)
599  return ret;
600  }
601 
602  return 0;
603 }
604 
606  const AVFrame *frame, const AVPacket *pkt,
607  uint64_t frame_num)
608 {
609  Encoder *e = ost->enc;
610  AVIOContext *io = es->io;
611  AVRational tb = frame ? frame->time_base : pkt->time_base;
612  int64_t pts = frame ? frame->pts : pkt->pts;
613 
614  AVRational tbi = (AVRational){ 0, 1};
615  int64_t ptsi = INT64_MAX;
616 
617  const FrameData *fd;
618 
619  if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
620  fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
621  tbi = fd->dec.tb;
622  ptsi = fd->dec.pts;
623  }
624 
625  for (size_t i = 0; i < es->nb_components; i++) {
626  const EncStatsComponent *c = &es->components[i];
627 
628  switch (c->type) {
629  case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
630  case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file_index); continue;
631  case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
632  case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
633  case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
634  case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
635  case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
636  case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
637  case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
638  INFINITY : ptsi * av_q2d(tbi)); continue;
639  case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
640  case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
641  }
642 
643  if (frame) {
644  switch (c->type) {
645  case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, ost->samples_encoded); continue;
646  case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
647  default: av_assert0(0);
648  }
649  } else {
650  switch (c->type) {
651  case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
652  case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
653  case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
654  case ENC_STATS_BITRATE: {
655  double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
656  avio_printf(io, "%g", 8.0 * pkt->size / duration);
657  continue;
658  }
659  case ENC_STATS_AVG_BITRATE: {
660  double duration = pkt->dts * av_q2d(tb);
661  avio_printf(io, "%g", duration > 0 ? 8.0 * e->data_size / duration : -1.);
662  continue;
663  }
664  default: av_assert0(0);
665  }
666  }
667  }
668  avio_w8(io, '\n');
669  avio_flush(io);
670 }
671 
672 static inline double psnr(double d)
673 {
674  return -10.0 * log10(d);
675 }
676 
677 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
678 {
679  Encoder *e = ost->enc;
681  NULL);
682  AVCodecContext *enc = ost->enc_ctx;
683  enum AVPictureType pict_type;
684  int64_t frame_number;
685  double ti1, bitrate, avg_bitrate;
686  double psnr_val = -1;
687 
688  ost->quality = sd ? AV_RL32(sd) : -1;
689  pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
690 
691  if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
692  // FIXME the scaling assumes 8bit
693  double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
694  if (error >= 0 && error <= 1)
695  psnr_val = psnr(error);
696  }
697 
698  if (!write_vstats)
699  return 0;
700 
701  /* this is executed just the first time update_video_stats is called */
702  if (!vstats_file) {
703  vstats_file = fopen(vstats_filename, "w");
704  if (!vstats_file) {
705  perror("fopen");
706  return AVERROR(errno);
707  }
708  }
709 
710  frame_number = e->packets_encoded;
711  if (vstats_version <= 1) {
712  fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
713  ost->quality / (float)FF_QP2LAMBDA);
714  } else {
715  fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
716  ost->quality / (float)FF_QP2LAMBDA);
717  }
718 
719  if (psnr_val >= 0)
720  fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
721 
722  fprintf(vstats_file,"f_size= %6d ", pkt->size);
723  /* compute pts value */
724  ti1 = pkt->dts * av_q2d(pkt->time_base);
725  if (ti1 < 0.01)
726  ti1 = 0.01;
727 
728  bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
729  avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
730  fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
731  (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
732  fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
733 
734  return 0;
735 }
736 
738 {
739  Encoder *e = ost->enc;
740  AVCodecContext *enc = ost->enc_ctx;
741  AVPacket *pkt = e->pkt;
742  const char *type_desc = av_get_media_type_string(enc->codec_type);
743  const char *action = frame ? "encode" : "flush";
744  int ret;
745 
746  if (frame) {
747  if (ost->enc_stats_pre.io)
748  enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
749  ost->frames_encoded);
750 
751  ost->frames_encoded++;
752  ost->samples_encoded += frame->nb_samples;
753 
754  if (debug_ts) {
755  av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
756  "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
757  type_desc,
758  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
759  enc->time_base.num, enc->time_base.den);
760  }
761 
762  if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
763  enc->sample_aspect_ratio = frame->sample_aspect_ratio;
764  }
765 
767 
768  ret = avcodec_send_frame(enc, frame);
769  if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
770  av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
771  type_desc);
772  return ret;
773  }
774 
775  while (1) {
777 
779  update_benchmark("%s_%s %d.%d", action, type_desc,
780  ost->file_index, ost->index);
781 
782  pkt->time_base = enc->time_base;
783 
784  /* if two pass, output log on success and EOF */
785  if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
786  fprintf(ost->logfile, "%s", enc->stats_out);
787 
788  if (ret == AVERROR(EAGAIN)) {
789  av_assert0(frame); // should never happen during flushing
790  return 0;
791  } else if (ret == AVERROR_EOF) {
792  ret = of_output_packet(of, ost, NULL);
793  return ret < 0 ? ret : AVERROR_EOF;
794  } else if (ret < 0) {
795  av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
796  return ret;
797  }
798 
799  if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
801  if (ret < 0)
802  return ret;
803  }
804 
805  if (ost->enc_stats_post.io)
806  enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
807  e->packets_encoded);
808 
809  if (debug_ts) {
810  av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
811  "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
812  "duration:%s duration_time:%s\n",
813  type_desc,
817  }
818 
821  "Subtitle heartbeat logic failed in %s! (%s)\n",
822  __func__, av_err2str(ret));
823  return ret;
824  }
825 
826  e->data_size += pkt->size;
827 
828  e->packets_encoded++;
829 
830  ret = of_output_packet(of, ost, pkt);
831  if (ret < 0)
832  return ret;
833  }
834 
835  av_assert0(0);
836 }
837 
839  AVFrame *frame)
840 {
841  Encoder *e = ost->enc;
842  int ret;
843 
844  if (ost->sq_idx_encode < 0)
845  return encode_frame(of, ost, frame);
846 
847  if (frame) {
849  if (ret < 0)
850  return ret;
851  frame = e->sq_frame;
852  }
853 
854  ret = sq_send(of->sq_encode, ost->sq_idx_encode,
855  SQFRAME(frame));
856  if (ret < 0) {
857  if (frame)
859  if (ret != AVERROR_EOF)
860  return ret;
861  }
862 
863  while (1) {
864  AVFrame *enc_frame = e->sq_frame;
865 
866  ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
867  SQFRAME(enc_frame));
868  if (ret == AVERROR_EOF) {
869  enc_frame = NULL;
870  } else if (ret < 0) {
871  return (ret == AVERROR(EAGAIN)) ? 0 : ret;
872  }
873 
874  ret = encode_frame(of, ost, enc_frame);
875  if (enc_frame)
877  if (ret < 0) {
878  if (ret == AVERROR_EOF)
880  return ret;
881  }
882  }
883 }
884 
886  AVFrame *frame)
887 {
888  Encoder *e = ost->enc;
889  AVCodecContext *enc = ost->enc_ctx;
890  int ret;
891 
893  enc->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
895  "Audio channel count changed and encoder does not support parameter changes\n");
896  return 0;
897  }
898 
899  if (frame->pts == AV_NOPTS_VALUE)
900  frame->pts = e->next_pts;
901  else {
902  int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
903  frame->pts =
904  av_rescale_q(frame->pts, frame->time_base, enc->time_base) -
906  }
907  frame->time_base = enc->time_base;
908  frame->duration = av_rescale_q(frame->nb_samples, (AVRational){1, frame->sample_rate},
909  enc->time_base);
910 
911  if (!check_recording_time(ost, frame->pts, frame->time_base))
912  return 0;
913 
914  e->next_pts = frame->pts + frame->nb_samples;
915 
917  return (ret < 0 && ret != AVERROR_EOF) ? ret : 0;
918 }
919 
921  AVFrame *frame)
922 {
923  double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
924  const int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ?
925  0 : of->start_time;
926 
927  AVCodecContext *const enc = ost->enc_ctx;
928 
929  AVRational tb = enc->time_base;
930  AVRational filter_tb = frame->time_base;
931  const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
932 
933  if (frame->pts == AV_NOPTS_VALUE)
934  goto early_exit;
935 
936  tb.den <<= extra_bits;
937  float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
939  float_pts /= 1 << extra_bits;
940  // when float_pts is not exactly an integer,
941  // avoid exact midpoints to reduce the chance of rounding differences, this
942  // can be removed in case the fps code is changed to work with integers
943  if (float_pts != llrint(float_pts))
944  float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
945 
946  frame->pts = av_rescale_q(frame->pts, filter_tb, enc->time_base) -
947  av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
948  frame->time_base = enc->time_base;
949 
950 early_exit:
951 
952  if (debug_ts) {
953  av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
954  frame ? av_ts2str(frame->pts) : "NULL",
955  (enc && frame) ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
956  float_pts,
957  enc ? enc->time_base.num : -1,
958  enc ? enc->time_base.den : -1);
959  }
960 
961  return float_pts;
962 }
963 
964 /* Convert frame timestamps to the encoder timebase and decide how many times
965  * should this (and possibly previous) frame be repeated in order to conform to
966  * desired target framerate (if any).
967  */
969  AVFrame *frame, double duration,
970  int64_t *nb_frames, int64_t *nb_frames_prev)
971 {
972  Encoder *e = ost->enc;
973  double delta0, delta, sync_ipts;
974 
975  if (!frame) {
976  *nb_frames_prev = *nb_frames = mid_pred(e->frames_prev_hist[0],
977  e->frames_prev_hist[1],
978  e->frames_prev_hist[2]);
979  goto finish;
980  }
981 
982  sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, frame);
983  /* delta0 is the "drift" between the input frame and
984  * where it would fall in the output. */
985  delta0 = sync_ipts - e->next_pts;
986  delta = delta0 + duration;
987 
988  // tracks the number of times the PREVIOUS frame should be duplicated,
989  // mostly for variable framerate (VFR)
990  *nb_frames_prev = 0;
991  /* by default, we output a single frame */
992  *nb_frames = 1;
993 
994  if (delta0 < 0 &&
995  delta > 0 &&
996  ost->vsync_method != VSYNC_PASSTHROUGH &&
997  ost->vsync_method != VSYNC_DROP) {
998  if (delta0 < -0.6) {
999  av_log(ost, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
1000  } else
1001  av_log(ost, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
1002  sync_ipts = e->next_pts;
1003  duration += delta0;
1004  delta0 = 0;
1005  }
1006 
1007  switch (ost->vsync_method) {
1008  case VSYNC_VSCFR:
1009  if (e->vsync_frame_number == 0 && delta0 >= 0.5) {
1010  av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
1011  delta = duration;
1012  delta0 = 0;
1013  e->next_pts = llrint(sync_ipts);
1014  }
1015  case VSYNC_CFR:
1016  // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
1017  if (frame_drop_threshold && delta < frame_drop_threshold && e->vsync_frame_number) {
1018  *nb_frames = 0;
1019  } else if (delta < -1.1)
1020  *nb_frames = 0;
1021  else if (delta > 1.1) {
1022  *nb_frames = llrintf(delta);
1023  if (delta0 > 1.1)
1024  *nb_frames_prev = llrintf(delta0 - 0.6);
1025  }
1026  frame->duration = 1;
1027  break;
1028  case VSYNC_VFR:
1029  if (delta <= -0.6)
1030  *nb_frames = 0;
1031  else if (delta > 0.6)
1032  e->next_pts = llrint(sync_ipts);
1033  frame->duration = duration;
1034  break;
1035  case VSYNC_DROP:
1036  case VSYNC_PASSTHROUGH:
1037  frame->duration = duration;
1038  e->next_pts = llrint(sync_ipts);
1039  break;
1040  default:
1041  av_assert0(0);
1042  }
1043 
1044 finish:
1045  memmove(e->frames_prev_hist + 1,
1046  e->frames_prev_hist,
1047  sizeof(e->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(e->frames_prev_hist) - 1));
1048  e->frames_prev_hist[0] = *nb_frames_prev;
1049 }
1050 
1051 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
1052  AVRational tb, const AVFrame *in_picture,
1053  int dup_idx)
1054 {
1055  double pts_time;
1056 
1057  if (kf->ref_pts == AV_NOPTS_VALUE)
1058  kf->ref_pts = in_picture->pts;
1059 
1060  pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
1061  if (kf->index < kf->nb_pts &&
1062  av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
1063  kf->index++;
1064  goto force_keyframe;
1065  } else if (kf->pexpr) {
1066  double res;
1067  kf->expr_const_values[FKF_T] = pts_time;
1068  res = av_expr_eval(kf->pexpr,
1069  kf->expr_const_values, NULL);
1070  av_log(logctx, AV_LOG_TRACE,
1071  "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1072  kf->expr_const_values[FKF_N],
1075  kf->expr_const_values[FKF_T],
1077  res);
1078 
1079  kf->expr_const_values[FKF_N] += 1;
1080 
1081  if (res) {
1084  kf->expr_const_values[FKF_N_FORCED] += 1;
1085  goto force_keyframe;
1086  }
1087  } else if (kf->type == KF_FORCE_SOURCE &&
1088  (in_picture->flags & AV_FRAME_FLAG_KEY) && !dup_idx) {
1089  goto force_keyframe;
1090  } else if (kf->type == KF_FORCE_SOURCE_NO_DROP && !dup_idx) {
1091  kf->dropped_keyframe = 0;
1092  if ((in_picture->flags & AV_FRAME_FLAG_KEY) || kf->dropped_keyframe)
1093  goto force_keyframe;
1094  }
1095 
1096  return AV_PICTURE_TYPE_NONE;
1097 
1098 force_keyframe:
1099  av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1100  return AV_PICTURE_TYPE_I;
1101 }
1102 
1103 /* May modify/reset frame */
1105 {
1106  int ret;
1107  Encoder *e = ost->enc;
1108  AVCodecContext *enc = ost->enc_ctx;
1109  int64_t nb_frames, nb_frames_prev, i;
1110  double duration = 0;
1111 
1112  if (frame) {
1113  FrameData *fd = frame_data(frame);
1114 
1115  duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
1116 
1117  if (duration <= 0 &&
1118  fd->frame_rate_filter.num > 0 && fd->frame_rate_filter.den > 0)
1119  duration = 1 / (av_q2d(fd->frame_rate_filter) * av_q2d(enc->time_base));
1120  }
1121 
1123  &nb_frames, &nb_frames_prev);
1124 
1125  if (nb_frames_prev == 0 && ost->last_dropped) {
1126  ost->nb_frames_drop++;
1128  "*** dropping frame %"PRId64" at ts %"PRId64"\n",
1130  }
1131  if (nb_frames > (nb_frames_prev && ost->last_dropped) + (nb_frames > nb_frames_prev)) {
1132  if (nb_frames > dts_error_threshold * 30) {
1133  av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", nb_frames - 1);
1134  ost->nb_frames_drop++;
1135  return 0;
1136  }
1137  ost->nb_frames_dup += nb_frames - (nb_frames_prev && ost->last_dropped) - (nb_frames > nb_frames_prev);
1138  av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", nb_frames - 1);
1139  if (ost->nb_frames_dup > e->dup_warning) {
1140  av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", e->dup_warning);
1141  e->dup_warning *= 10;
1142  }
1143  }
1144  ost->last_dropped = nb_frames == nb_frames_prev && frame;
1145  ost->kf.dropped_keyframe = ost->last_dropped && frame && (frame->flags & AV_FRAME_FLAG_KEY);
1146 
1147  /* duplicates frame if needed */
1148  for (i = 0; i < nb_frames; i++) {
1149  AVFrame *in_picture;
1150 
1151  if (i < nb_frames_prev && e->last_frame->buf[0]) {
1152  in_picture = e->last_frame;
1153  } else
1154  in_picture = frame;
1155 
1156  if (!in_picture)
1157  return 0;
1158 
1159  in_picture->pts = e->next_pts;
1160 
1161  if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
1162  return 0;
1163 
1164  in_picture->quality = enc->global_quality;
1165  in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture, i);
1166 
1167 #if FFMPEG_OPT_TOP
1168  if (ost->top_field_first >= 0) {
1169  in_picture->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
1170  in_picture->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
1171  }
1172 #endif
1173 
1174  ret = submit_encode_frame(of, ost, in_picture);
1175  if (ret == AVERROR_EOF)
1176  break;
1177  else if (ret < 0)
1178  return ret;
1179 
1180  e->next_pts++;
1181  e->vsync_frame_number++;
1182  }
1183 
1185  if (frame)
1187 
1188  return 0;
1189 }
1190 
1192 {
1193  OutputFile *of = output_files[ost->file_index];
1194  int ret;
1195 
1196  ret = enc_open(ost, frame);
1197  if (ret < 0)
1198  return ret;
1199 
1200  return ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ?
1201  do_video_out(of, ost, frame) : do_audio_out(of, ost, frame);
1202 }
1203 
1204 int enc_flush(void)
1205 {
1206  int ret;
1207 
1208  for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1209  OutputFile *of = output_files[ost->file_index];
1210  if (ost->sq_idx_encode >= 0)
1211  sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
1212  }
1213 
1214  for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1215  Encoder *e = ost->enc;
1216  AVCodecContext *enc = ost->enc_ctx;
1217  OutputFile *of = output_files[ost->file_index];
1218 
1219  if (!enc || !e->opened ||
1221  continue;
1222 
1223  ret = submit_encode_frame(of, ost, NULL);
1224  if (ret != AVERROR_EOF)
1225  return ret;
1226  }
1227 
1228  return 0;
1229 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVSubtitle
Definition: avcodec.h:2263
KeyframeForceCtx::pts
int64_t * pts
Definition: ffmpeg.h:496
avcodec_encode_subtitle
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: encode.c:189
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodec
AVCodec.
Definition: codec.h:187
forced_kf_apply
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture, int dup_idx)
Definition: ffmpeg_enc.c:1051
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR_EXPERIMENTAL
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
Definition: error.h:74
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:40
KeyframeForceCtx::dropped_keyframe
int dropped_keyframe
Definition: ffmpeg.h:503
extra_bits
#define extra_bits(eb)
Definition: intrax8.c:125
av_clip
#define av_clip
Definition: common.h:96
INFINITY
#define INFINITY
Definition: mathematics.h:118
FKF_PREV_FORCED_T
@ FKF_PREV_FORCED_T
Definition: ffmpeg.h:435
dec_ctx
static AVCodecContext * dec_ctx
Definition: decode_filter_audio.c:46
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:555
VSYNC_VFR
@ VSYNC_VFR
Definition: ffmpeg.h:66
Encoder::opened
int opened
Definition: ffmpeg_enc.c:69
AVERROR
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
AVSubtitle::rects
AVSubtitleRect ** rects
Definition: avcodec.h:2268
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:43
av_compare_ts
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.
Definition: mathematics.c:147
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1025
FrameData
Definition: ffmpeg.h:636
close_output_stream
void close_output_stream(OutputStream *ost)
Definition: ffmpeg.c:487
Encoder::frames_prev_hist
int64_t frames_prev_hist[3]
Definition: ffmpeg_enc.c:54
ENC_STATS_PTS
@ ENC_STATS_PTS
Definition: ffmpeg.h:451
ENC_STATS_FRAME_NUM_IN
@ ENC_STATS_FRAME_NUM_IN
Definition: ffmpeg.h:448
enc_open
int enc_open(OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:285
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1060
KF_FORCE_SOURCE_NO_DROP
@ KF_FORCE_SOURCE_NO_DROP
Definition: ffmpeg.h:487
FKF_PREV_FORCED_N
@ FKF_PREV_FORCED_N
Definition: ffmpeg.h:434
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2964
AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
Definition: codec.h:322
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:483
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ENC_STATS_DTS
@ ENC_STATS_DTS
Definition: ffmpeg.h:455
enc_choose_timebase
static int enc_choose_timebase(OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:193
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:100
AVPictureType
AVPictureType
Definition: avutil.h:277
KeyframeForceCtx::nb_pts
int nb_pts
Definition: ffmpeg.h:497
frame_drop_threshold
float frame_drop_threshold
Definition: ffmpeg_opt.c:73
rational.h
InputStream::dec_ctx
AVCodecContext * dec_ctx
Definition: ffmpeg.h:340
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
enc_flush
int enc_flush(void)
Definition: ffmpeg_enc.c:1204
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2267
ENC_STATS_AVG_BITRATE
@ ENC_STATS_AVG_BITRATE
Definition: ffmpeg.h:461
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
OutputFile::start_time
int64_t start_time
start time in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:629
SQFRAME
#define SQFRAME(frame)
Definition: sync_queue.h:38
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1018
AVPacketSideData
Definition: packet.h:315
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
FKF_T
@ FKF_T
Definition: ffmpeg.h:436
AVPacket::data
uint8_t * data
Definition: packet.h:374
ENC_STATS_LITERAL
@ ENC_STATS_LITERAL
Definition: ffmpeg.h:444
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
AVCodecContext::field_order
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1057
do_audio_out
static int do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:885
Encoder::next_pts
int64_t next_pts
Definition: ffmpeg_enc.c:46
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:928
KeyframeForceCtx::type
int type
Definition: ffmpeg.h:491
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1770
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
av_display_rotation_set
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...
Definition: display.c:51
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AV_CODEC_FLAG_PSNR
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:318
HWDevice
Definition: ffmpeg.h:82
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
set_encoder_id
static int set_encoder_id(OutputFile *of, OutputStream *ost)
Definition: ffmpeg_enc.c:168
AV_CODEC_FLAG_INTERLACED_ME
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:343
psnr
static double psnr(double d)
Definition: ffmpeg_enc.c:672
ost
static AVStream * ost
Definition: vaapi_transcode.c:42
tf_sess_config.config
config
Definition: tf_sess_config.py:33
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
ENC_STATS_TIMEBASE_IN
@ ENC_STATS_TIMEBASE_IN
Definition: ffmpeg.h:450
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:309
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:641
FrameData::frame_num
uint64_t frame_num
Definition: ffmpeg.h:639
InputStream
Definition: ffmpeg.h:320
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1797
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:81
AVPacketSideData::size
size_t size
Definition: packet.h:317
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:572
finish
static void finish(void)
Definition: movenc.c:342
vstats_version
int vstats_version
Definition: ffmpeg_opt.c:89
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:446
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:410
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2101
fail
#define fail()
Definition: checkasm.h:138
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:113
FFSIGN
#define FFSIGN(a)
Definition: common.h:66
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:517
KeyframeForceCtx::ref_pts
int64_t ref_pts
Definition: ffmpeg.h:493
enc_subtitle
int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
Definition: ffmpeg_enc.c:519
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:466
sq_receive
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Read a frame from the queue.
Definition: sync_queue.c:608
pts
static int64_t pts
Definition: transcode_aac.c:643
AV_PKT_DATA_DISPLAYMATRIX
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: packet.h:109
FrameData::tb
AVRational tb
Definition: ffmpeg.h:642
trigger_fix_sub_duration_heartbeat
int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
Definition: ffmpeg.c:774
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:322
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:551
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1011
FrameData::frame_rate_filter
AVRational frame_rate_filter
Definition: ffmpeg.h:645
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ENC_STATS_PTS_IN
@ ENC_STATS_PTS_IN
Definition: ffmpeg.h:453
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
duration
int64_t duration
Definition: movenc.c:64
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
VSYNC_VSCFR
@ VSYNC_VSCFR
Definition: ffmpeg.h:67
EncStats::components
EncStatsComponent * components
Definition: ffmpeg.h:472
AVCodec::supported_framerates
const AVRational * supported_framerates
array of supported framerates, or NULL if any, array is terminated by {0,0}
Definition: codec.h:208
llrintf
#define llrintf(x)
Definition: libm.h:399
intreadwrite.h
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:503
vstats_filename
char * vstats_filename
Definition: ffmpeg_opt.c:65
bitrate
int64_t bitrate
Definition: av1_levels.c:47
ENC_STATS_FILE_IDX
@ ENC_STATS_FILE_IDX
Definition: ffmpeg.h:445
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
Encoder::sq_frame
AVFrame * sq_frame
Definition: ffmpeg_enc.c:56
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
ENC_STATS_BITRATE
@ ENC_STATS_BITRATE
Definition: ffmpeg.h:460
Encoder::data_size
uint64_t data_size
Definition: ffmpeg_enc.c:62
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
enc_frame
int enc_frame(OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:1191
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1513
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVPacketSideData::data
uint8_t * data
Definition: packet.h:316
enc_stats_write
void enc_stats_write(OutputStream *ost, EncStats *es, const AVFrame *frame, const AVPacket *pkt, uint64_t frame_num)
Definition: ffmpeg_enc.c:605
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:766
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVSubtitle::pts
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:2269
av_hwdevice_get_type_name
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
Definition: hwcontext.c:93
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:509
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:447
KeyframeForceCtx::expr_const_values
double expr_const_values[FKF_NB]
Definition: ffmpeg.h:501
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:270
encode_frame
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:737
Encoder::pkt
AVPacket * pkt
Definition: ffmpeg_enc.c:59
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:878
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1035
InputStream::st
AVStream * st
Definition: ffmpeg.h:326
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
ENC_STATS_PTS_TIME
@ ENC_STATS_PTS_TIME
Definition: ffmpeg.h:452
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
do_video_out
static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:1104
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:318
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:1771
EncStats
Definition: ffmpeg.h:471
mathops.h
vstats_file
FILE * vstats_file
Definition: ffmpeg.c:107
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:919
ost_iter
OutputStream * ost_iter(OutputStream *prev)
Definition: ffmpeg.c:397
double
double
Definition: af_crystalizer.c:131
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:126
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:226
ENC_STATS_PTS_TIME_IN
@ ENC_STATS_PTS_TIME_IN
Definition: ffmpeg.h:454
c
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
Definition: undefined.txt:32
sq_frame_samples
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, int frame_samples)
Set a constant output audio frame size, in samples.
Definition: sync_queue.c:661
ENC_TIME_BASE_DEMUX
@ ENC_TIME_BASE_DEMUX
Definition: ffmpeg.h:72
AVOutputFormat::flags
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:528
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:559
adjust_frame_pts_to_encoder_tb
static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:920
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1322
eval.h
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
AVPacket::size
int size
Definition: packet.h:375
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:361
FFMPEG_OPT_TOP
#define FFMPEG_OPT_TOP
Definition: ffmpeg.h:60
output_files
OutputFile ** output_files
Definition: ffmpeg.c:126
FrameData::dec
struct FrameData::@3 dec
AVFrame::quality
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:485
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
Encoder::dup_warning
uint64_t dup_warning
Definition: ffmpeg_enc.c:67
start_time
static int64_t start_time
Definition: ffplay.c:328
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1076
sq_send
int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
Submit a frame for the stream with index stream_idx.
Definition: sync_queue.c:343
ENC_STATS_NB_SAMPLES
@ ENC_STATS_NB_SAMPLES
Definition: ffmpeg.h:458
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
check_avoptions
int check_avoptions(AVDictionary *m)
Definition: ffmpeg.c:455
submit_encode_frame
static int submit_encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:838
HWDevice::device_ref
AVBufferRef * device_ref
Definition: ffmpeg.h:85
hw_device_get_by_type
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:30
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:278
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:429
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:917
AVSubtitle::end_display_time
uint32_t end_display_time
Definition: avcodec.h:2266
frame.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:248
FrameData::pts
int64_t pts
Definition: ffmpeg.h:641
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: codec_par.h:41
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
Encoder
Definition: ffmpeg_enc.c:44
OutputFile::sq_encode
SyncQueue * sq_encode
Definition: ffmpeg.h:626
KF_FORCE_SOURCE
@ KF_FORCE_SOURCE
Definition: ffmpeg.h:486
ENC_STATS_FRAME_NUM
@ ENC_STATS_FRAME_NUM
Definition: ffmpeg.h:447
KeyframeForceCtx
Definition: ffmpeg.h:490
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
AVStream::side_data
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:957
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
av_find_nearest_q_idx
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.
Definition: rational.c:142
av_get_picture_type_char
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:40
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
of_output_packet
int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:349
log.h
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:244
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
FrameData::bits_per_raw_sample
int bits_per_raw_sample
Definition: ffmpeg.h:647
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:252
VSYNC_CFR
@ VSYNC_CFR
Definition: ffmpeg.h:65
OutputFile::bitexact
int bitexact
Definition: ffmpeg.h:632
display.h
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:82
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:649
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
tb
#define tb
Definition: regdef.h:68
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AV_PKT_DATA_CPB_PROPERTIES
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition: packet.h:146
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1042
ENC_STATS_STREAM_IDX
@ ENC_STATS_STREAM_IDX
Definition: ffmpeg.h:446
AVCodecContext::height
int height
Definition: avcodec.h:617
avcodec_send_frame
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:517
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:654
ENC_STATS_SAMPLE_NUM
@ ENC_STATS_SAMPLE_NUM
Definition: ffmpeg.h:457
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:636
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
mid_pred
#define mid_pred
Definition: mathops.h:98
ret
ret
Definition: filter_design.txt:187
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
frame
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
Definition: filter_design.txt:264
av_strlcat
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,...
Definition: avstring.c:95
VSYNC_DROP
@ VSYNC_DROP
Definition: ffmpeg.h:68
hw_device_setup_for_encode
static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
Definition: ffmpeg_enc.c:117
AVStream::nb_side_data
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:961
avformat.h
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
av_get_media_type_string
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.
Definition: utils.c:28
AVCodecContext
main external API structure.
Definition: avcodec.h:437
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:845
HWDevice::name
const char * name
Definition: ffmpeg.h:83
Encoder::last_frame
AVFrame * last_frame
Definition: ffmpeg_enc.c:48
AVRational::den
int den
Denominator.
Definition: rational.h:60
KeyframeForceCtx::index
int index
Definition: ffmpeg.h:498
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
check_recording_time
static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
Definition: ffmpeg_enc.c:507
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
avcodec_get_hw_config
const AVCodecHWConfig * avcodec_get_hw_config(const AVCodec *codec, int index)
Retrieve supported hardware configurations for a codec.
Definition: utils.c:856
enc_free
void enc_free(Encoder **penc)
Definition: ffmpeg_enc.c:72
AV_CODEC_CAP_PARAM_CHANGE
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition: codec.h:118
VSYNC_AUTO
@ VSYNC_AUTO
Definition: ffmpeg.h:63
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:643
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:445
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avutil.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:334
llrint
#define llrint(x)
Definition: libm.h:394
KeyframeForceCtx::pexpr
AVExpr * pexpr
Definition: ffmpeg.h:500
FKF_N_FORCED
@ FKF_N_FORCED
Definition: ffmpeg.h:433
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
ENC_TIME_BASE_FILTER
@ ENC_TIME_BASE_FILTER
Definition: ffmpeg.h:73
AVPacket
This structure stores compressed data.
Definition: packet.h:351
Encoder::vsync_frame_number
int64_t vsync_frame_number
Definition: ffmpeg_enc.c:50
EncStatsComponent
Definition: ffmpeg.h:464
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
HWDevice::type
enum AVHWDeviceType type
Definition: ffmpeg.h:84
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: codec_par.h:43
EncStats::nb_components
int nb_components
Definition: ffmpeg.h:473
av_stream_new_side_data
uint8_t * av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
Definition: avformat.c:193
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: codec_par.h:42
d
d
Definition: ffmpeg_filter.c:331
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:617
int32_t
int32_t
Definition: audioconvert.c:56
AV_PKT_DATA_QUALITY_STATS
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
Definition: packet.h:133
ENC_STATS_PKT_SIZE
@ ENC_STATS_PKT_SIZE
Definition: ffmpeg.h:459
timestamp.h
OutputStream
Definition: mux.c:53
av_strlcpy
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.
Definition: avstring.c:85
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
dts_error_threshold
float dts_error_threshold
Definition: ffmpeg_opt.c:70
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
AVCodecHWConfig
Definition: codec.h:341
OutputFile::format
const AVOutputFormat * format
Definition: ffmpeg.h:620
video_sync_process
static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame, double duration, int64_t *nb_frames, int64_t *nb_frames_prev)
Definition: ffmpeg_enc.c:968
avstring.h
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
FKF_N
@ FKF_N
Definition: ffmpeg.h:432
ENC_STATS_DTS_TIME
@ ENC_STATS_DTS_TIME
Definition: ffmpeg.h:456
OutputFile::recording_time
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:628
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: codec_par.h:44
of_stream_init
int of_stream_init(OutputFile *of, OutputStream *ost)
Definition: ffmpeg_mux.c:671
VSYNC_PASSTHROUGH
@ VSYNC_PASSTHROUGH
Definition: ffmpeg.h:64
EncStats::io
AVIOContext * io
Definition: ffmpeg.h:475
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
update_video_stats
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
Definition: ffmpeg_enc.c:677
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:818
AVSubtitle::start_display_time
uint32_t start_display_time
Definition: avcodec.h:2265
ENC_STATS_TIMEBASE
@ ENC_STATS_TIMEBASE
Definition: ffmpeg.h:449
AVPacket::time_base
AVRational time_base
Time base of the packet's timestamps.
Definition: packet.h:418
enc_alloc
int enc_alloc(Encoder **penc, const AVCodec *codec)
Definition: ffmpeg_enc.c:87
av_get_pix_fmt_name
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.
Definition: pixdesc.c:2884
OutputFile
Definition: ffmpeg.h:615
Encoder::packets_encoded
uint64_t packets_encoded
Definition: ffmpeg_enc.c:65
InputStream::autorotate
int autorotate
Definition: ffmpeg.h:354