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 #include "libavformat/avformat.h"
40 
41 struct Encoder {
43 
44  // packet for receiving encoded output
46 
47  // combined size of all the packets received from the encoder
48  uint64_t data_size;
49 
50  // number of packets received from the encoder
51  uint64_t packets_encoded;
52 
53  int opened;
54 };
55 
56 void enc_free(Encoder **penc)
57 {
58  Encoder *enc = *penc;
59 
60  if (!enc)
61  return;
62 
63  av_frame_free(&enc->sq_frame);
64 
65  av_packet_free(&enc->pkt);
66 
67  av_freep(penc);
68 }
69 
70 int enc_alloc(Encoder **penc, const AVCodec *codec)
71 {
72  Encoder *enc;
73 
74  *penc = NULL;
75 
76  enc = av_mallocz(sizeof(*enc));
77  if (!enc)
78  return AVERROR(ENOMEM);
79 
80  enc->pkt = av_packet_alloc();
81  if (!enc->pkt)
82  goto fail;
83 
84  *penc = enc;
85 
86  return 0;
87 fail:
88  enc_free(&enc);
89  return AVERROR(ENOMEM);
90 }
91 
93 {
94  const AVCodecHWConfig *config;
95  HWDevice *dev = NULL;
96  int i;
97 
98  if (frames_ref &&
99  ((AVHWFramesContext*)frames_ref->data)->format ==
100  ost->enc_ctx->pix_fmt) {
101  // Matching format, will try to use hw_frames_ctx.
102  } else {
103  frames_ref = NULL;
104  }
105 
106  for (i = 0;; i++) {
107  config = avcodec_get_hw_config(ost->enc_ctx->codec, i);
108  if (!config)
109  break;
110 
111  if (frames_ref &&
113  (config->pix_fmt == AV_PIX_FMT_NONE ||
114  config->pix_fmt == ost->enc_ctx->pix_fmt)) {
115  av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
116  "frames context (format %s) with %s encoder.\n",
117  av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
118  ost->enc_ctx->codec->name);
119  ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
120  if (!ost->enc_ctx->hw_frames_ctx)
121  return AVERROR(ENOMEM);
122  return 0;
123  }
124 
125  if (!dev &&
127  dev = hw_device_get_by_type(config->device_type);
128  }
129 
130  if (dev) {
131  av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
132  "(type %s) with %s encoder.\n", dev->name,
133  av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name);
134  ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
135  if (!ost->enc_ctx->hw_device_ctx)
136  return AVERROR(ENOMEM);
137  } else {
138  // No device required, or no device available.
139  }
140  return 0;
141 }
142 
144 {
145  const char *cname = ost->enc_ctx->codec->name;
146  uint8_t *encoder_string;
147  int encoder_string_len;
148 
149  if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
150  return 0;
151 
152  encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
153  encoder_string = av_mallocz(encoder_string_len);
154  if (!encoder_string)
155  return AVERROR(ENOMEM);
156 
157  if (!of->bitexact && !ost->bitexact)
158  av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
159  else
160  av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
161  av_strlcat(encoder_string, cname, encoder_string_len);
162  av_dict_set(&ost->st->metadata, "encoder", encoder_string,
164 
165  return 0;
166 }
167 
169 {
170  InputStream *ist = ost->ist;
171  Encoder *e = ost->enc;
172  AVCodecContext *enc_ctx = ost->enc_ctx;
174  const AVCodec *enc = enc_ctx->codec;
175  OutputFile *of = output_files[ost->file_index];
176  FrameData *fd;
177  int ret;
178 
179  if (e->opened)
180  return 0;
181 
182  // frame is always non-NULL for audio and video
184 
185  if (frame) {
187  fd = (FrameData*)frame->opaque_ref->data;
188  }
189 
190  ret = set_encoder_id(output_files[ost->file_index], ost);
191  if (ret < 0)
192  return ret;
193 
194  if (ist) {
195  dec_ctx = ist->dec_ctx;
196  }
197 
198  // the timebase is chosen by filtering code
199  if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
200  enc_ctx->time_base = frame->time_base;
201  enc_ctx->framerate = fd->frame_rate_filter;
203  }
204 
205  switch (enc_ctx->codec_type) {
206  case AVMEDIA_TYPE_AUDIO:
207  enc_ctx->sample_fmt = frame->format;
208  enc_ctx->sample_rate = frame->sample_rate;
210  if (ret < 0)
211  return ret;
212 
213  if (ost->bits_per_raw_sample)
214  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
215  else
217  av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
218  break;
219 
220  case AVMEDIA_TYPE_VIDEO: {
221  enc_ctx->width = frame->width;
222  enc_ctx->height = frame->height;
224  ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
225  av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
227 
228  enc_ctx->pix_fmt = frame->format;
229 
230  if (ost->bits_per_raw_sample)
231  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
232  else
234  av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
235 
236  enc_ctx->color_range = frame->color_range;
238  enc_ctx->color_trc = frame->color_trc;
239  enc_ctx->colorspace = frame->colorspace;
241 
244 #if FFMPEG_OPT_TOP
245  || ost->top_field_first >= 0
246 #endif
247  ) {
248  int top_field_first =
249 #if FFMPEG_OPT_TOP
250  ost->top_field_first >= 0 ?
251  ost->top_field_first :
252 #endif
254 
255  if (enc->id == AV_CODEC_ID_MJPEG)
256  enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
257  else
258  enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
259  } else
261 
262  break;
263  }
265  if (ost->enc_timebase.num)
267  "-enc_time_base not supported for subtitles, ignoring\n");
268  enc_ctx->time_base = AV_TIME_BASE_Q;
269 
270  if (!enc_ctx->width) {
271  enc_ctx->width = ost->ist->par->width;
272  enc_ctx->height = ost->ist->par->height;
273  }
274  if (dec_ctx && dec_ctx->subtitle_header) {
275  /* ASS code assumes this buffer is null terminated so add extra byte. */
277  if (!enc_ctx->subtitle_header)
278  return AVERROR(ENOMEM);
279  memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
282  }
283 
284  break;
285  default:
286  av_assert0(0);
287  break;
288  }
289 
290  if (ost->bitexact)
291  enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
292 
293  if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
294  av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
295 
297  ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
298  if (ret < 0)
299  return ret;
300  }
301 
302  av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
303 
305  if (ret < 0) {
307  "Encoding hardware device setup failed: %s\n", av_err2str(ret));
308  return ret;
309  }
310 
311  if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
312  if (ret != AVERROR_EXPERIMENTAL)
313  av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
314  "incorrect parameters such as bit_rate, rate, width or height.\n");
315  return ret;
316  }
317 
318  e->opened = 1;
319 
320  if (ost->sq_idx_encode >= 0) {
321  e->sq_frame = av_frame_alloc();
322  if (!e->sq_frame)
323  return AVERROR(ENOMEM);
324  }
325 
326  if (ost->enc_ctx->frame_size) {
327  av_assert0(ost->sq_idx_encode >= 0);
329  ost->sq_idx_encode, ost->enc_ctx->frame_size);
330  }
331 
332  ret = check_avoptions(ost->encoder_opts);
333  if (ret < 0)
334  return ret;
335 
336  if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
337  ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
338  av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
339  " It takes bits/s as argument, not kbits/s\n");
340 
341  ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
342  if (ret < 0) {
344  "Error initializing the output stream codec context.\n");
345  return ret;
346  }
347 
348  /*
349  * Add global input side data. For now this is naive, and copies it
350  * from the input stream's global side data. All side data should
351  * really be funneled over AVFrame and libavfilter, then added back to
352  * packet side data, and then potentially using the first packet for
353  * global side data.
354  */
355  if (ist) {
356  int i;
357  for (i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
358  AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
359  if (sd_src->type != AV_PKT_DATA_CPB_PROPERTIES) {
360  AVPacketSideData *sd_dst = av_packet_side_data_new(&ost->par_in->coded_side_data,
361  &ost->par_in->nb_coded_side_data,
362  sd_src->type, sd_src->size, 0);
363  if (!sd_dst)
364  return AVERROR(ENOMEM);
365  memcpy(sd_dst->data, sd_src->data, sd_src->size);
366  if (ist->autorotate && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
367  av_display_rotation_set((int32_t *)sd_dst->data, 0);
368  }
369  }
370  }
371 
372  // copy timebase while removing common factors
373  if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
374  ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
375 
376  ret = of_stream_init(of, ost);
377  if (ret < 0)
378  return ret;
379 
380  return 0;
381 }
382 
384 {
385  OutputFile *of = output_files[ost->file_index];
386 
387  if (of->recording_time != INT64_MAX &&
388  av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
390  return 0;
391  }
392  return 1;
393 }
394 
396 {
397  Encoder *e = ost->enc;
398  int subtitle_out_max_size = 1024 * 1024;
399  int subtitle_out_size, nb, i, ret;
400  AVCodecContext *enc;
401  AVPacket *pkt = e->pkt;
402  int64_t pts;
403 
404  if (sub->pts == AV_NOPTS_VALUE) {
405  av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
406  return exit_on_error ? AVERROR(EINVAL) : 0;
407  }
408  if (ost->finished ||
409  (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
410  return 0;
411 
412  enc = ost->enc_ctx;
413 
414  /* Note: DVB subtitle need one packet to draw them and one other
415  packet to clear them */
416  /* XXX: signal it in the codec context ? */
418  nb = 2;
419  else if (enc->codec_id == AV_CODEC_ID_ASS)
420  nb = FFMAX(sub->num_rects, 1);
421  else
422  nb = 1;
423 
424  /* shift timestamp to honor -ss and make check_recording_time() work with -t */
425  pts = sub->pts;
426  if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
427  pts -= output_files[ost->file_index]->start_time;
428  for (i = 0; i < nb; i++) {
429  AVSubtitle local_sub = *sub;
430 
432  return 0;
433 
434  ret = av_new_packet(pkt, subtitle_out_max_size);
435  if (ret < 0)
436  return AVERROR(ENOMEM);
437 
438  local_sub.pts = pts;
439  // start_display_time is required to be 0
440  local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
441  local_sub.end_display_time -= sub->start_display_time;
442  local_sub.start_display_time = 0;
443 
444  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
445  local_sub.num_rects = 0;
446  else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
447  local_sub.num_rects = 1;
448  local_sub.rects += i;
449  }
450 
451  ost->frames_encoded++;
452 
453  subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
454  if (subtitle_out_size < 0) {
455  av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
456  return subtitle_out_size;
457  }
458 
459  av_shrink_packet(pkt, subtitle_out_size);
461  pkt->pts = sub->pts;
463  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
464  /* XXX: the pts correction is handled here. Maybe handling
465  it in the codec would be better */
466  if (i == 0)
467  pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
468  else
469  pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
470  }
471  pkt->dts = pkt->pts;
472 
473  ret = of_output_packet(of, ost, pkt);
474  if (ret < 0)
475  return ret;
476  }
477 
478  return 0;
479 }
480 
482  const AVFrame *frame, const AVPacket *pkt,
483  uint64_t frame_num)
484 {
485  Encoder *e = ost->enc;
486  AVIOContext *io = es->io;
488  int64_t pts = frame ? frame->pts : pkt->pts;
489 
490  AVRational tbi = (AVRational){ 0, 1};
491  int64_t ptsi = INT64_MAX;
492 
493  const FrameData *fd;
494 
495  if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
496  fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
497  tbi = fd->dec.tb;
498  ptsi = fd->dec.pts;
499  }
500 
501  for (size_t i = 0; i < es->nb_components; i++) {
502  const EncStatsComponent *c = &es->components[i];
503 
504  switch (c->type) {
505  case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
506  case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file_index); continue;
507  case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
508  case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
509  case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
510  case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
511  case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
512  case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
513  case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
514  INFINITY : ptsi * av_q2d(tbi)); continue;
515  case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
516  case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
517  }
518 
519  if (frame) {
520  switch (c->type) {
521  case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, ost->samples_encoded); continue;
522  case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
523  default: av_assert0(0);
524  }
525  } else {
526  switch (c->type) {
527  case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
528  case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
529  case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
530  case ENC_STATS_BITRATE: {
531  double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
532  avio_printf(io, "%g", 8.0 * pkt->size / duration);
533  continue;
534  }
535  case ENC_STATS_AVG_BITRATE: {
536  double duration = pkt->dts * av_q2d(tb);
537  avio_printf(io, "%g", duration > 0 ? 8.0 * e->data_size / duration : -1.);
538  continue;
539  }
540  default: av_assert0(0);
541  }
542  }
543  }
544  avio_w8(io, '\n');
545  avio_flush(io);
546 }
547 
548 static inline double psnr(double d)
549 {
550  return -10.0 * log10(d);
551 }
552 
553 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
554 {
555  Encoder *e = ost->enc;
557  NULL);
558  AVCodecContext *enc = ost->enc_ctx;
559  enum AVPictureType pict_type;
560  int64_t frame_number;
561  double ti1, bitrate, avg_bitrate;
562  double psnr_val = -1;
563 
564  ost->quality = sd ? AV_RL32(sd) : -1;
565  pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
566 
567  if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
568  // FIXME the scaling assumes 8bit
569  double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
570  if (error >= 0 && error <= 1)
571  psnr_val = psnr(error);
572  }
573 
574  if (!write_vstats)
575  return 0;
576 
577  /* this is executed just the first time update_video_stats is called */
578  if (!vstats_file) {
579  vstats_file = fopen(vstats_filename, "w");
580  if (!vstats_file) {
581  perror("fopen");
582  return AVERROR(errno);
583  }
584  }
585 
586  frame_number = e->packets_encoded;
587  if (vstats_version <= 1) {
588  fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
589  ost->quality / (float)FF_QP2LAMBDA);
590  } else {
591  fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
592  ost->quality / (float)FF_QP2LAMBDA);
593  }
594 
595  if (psnr_val >= 0)
596  fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
597 
598  fprintf(vstats_file,"f_size= %6d ", pkt->size);
599  /* compute pts value */
600  ti1 = pkt->dts * av_q2d(pkt->time_base);
601  if (ti1 < 0.01)
602  ti1 = 0.01;
603 
604  bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
605  avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
606  fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
607  (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
608  fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
609 
610  return 0;
611 }
612 
614 {
615  Encoder *e = ost->enc;
616  AVCodecContext *enc = ost->enc_ctx;
617  AVPacket *pkt = e->pkt;
618  const char *type_desc = av_get_media_type_string(enc->codec_type);
619  const char *action = frame ? "encode" : "flush";
620  int ret;
621 
622  if (frame) {
623  if (ost->enc_stats_pre.io)
624  enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
625  ost->frames_encoded);
626 
627  ost->frames_encoded++;
628  ost->samples_encoded += frame->nb_samples;
629 
630  if (debug_ts) {
631  av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
632  "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
633  type_desc,
635  enc->time_base.num, enc->time_base.den);
636  }
637 
638  if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
640  }
641 
643 
644  ret = avcodec_send_frame(enc, frame);
645  if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
646  av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
647  type_desc);
648  return ret;
649  }
650 
651  while (1) {
653 
655  update_benchmark("%s_%s %d.%d", action, type_desc,
656  ost->file_index, ost->index);
657 
658  pkt->time_base = enc->time_base;
659 
660  /* if two pass, output log on success and EOF */
661  if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
662  fprintf(ost->logfile, "%s", enc->stats_out);
663 
664  if (ret == AVERROR(EAGAIN)) {
665  av_assert0(frame); // should never happen during flushing
666  return 0;
667  } else if (ret == AVERROR_EOF) {
668  ret = of_output_packet(of, ost, NULL);
669  return ret < 0 ? ret : AVERROR_EOF;
670  } else if (ret < 0) {
671  av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
672  return ret;
673  }
674 
675  if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
677  if (ret < 0)
678  return ret;
679  }
680 
681  if (ost->enc_stats_post.io)
682  enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
683  e->packets_encoded);
684 
685  if (debug_ts) {
686  av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
687  "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
688  "duration:%s duration_time:%s\n",
689  type_desc,
693  }
694 
697  "Subtitle heartbeat logic failed in %s! (%s)\n",
698  __func__, av_err2str(ret));
699  return ret;
700  }
701 
702  e->data_size += pkt->size;
703 
704  e->packets_encoded++;
705 
706  ret = of_output_packet(of, ost, pkt);
707  if (ret < 0)
708  return ret;
709  }
710 
711  av_assert0(0);
712 }
713 
715  AVFrame *frame)
716 {
717  Encoder *e = ost->enc;
718  int ret;
719 
720  if (ost->sq_idx_encode < 0)
721  return encode_frame(of, ost, frame);
722 
723  if (frame) {
725  if (ret < 0)
726  return ret;
727  frame = e->sq_frame;
728  }
729 
730  ret = sq_send(of->sq_encode, ost->sq_idx_encode,
731  SQFRAME(frame));
732  if (ret < 0) {
733  if (frame)
735  if (ret != AVERROR_EOF)
736  return ret;
737  }
738 
739  while (1) {
740  AVFrame *enc_frame = e->sq_frame;
741 
742  ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
743  SQFRAME(enc_frame));
744  if (ret == AVERROR_EOF) {
745  enc_frame = NULL;
746  } else if (ret < 0) {
747  return (ret == AVERROR(EAGAIN)) ? 0 : ret;
748  }
749 
750  ret = encode_frame(of, ost, enc_frame);
751  if (enc_frame)
753  if (ret < 0) {
754  if (ret == AVERROR_EOF)
756  return ret;
757  }
758  }
759 }
760 
762  AVFrame *frame)
763 {
764  AVCodecContext *enc = ost->enc_ctx;
765  int ret;
766 
770  "Audio channel count changed and encoder does not support parameter changes\n");
771  return 0;
772  }
773 
775  return 0;
776 
778  return (ret < 0 && ret != AVERROR_EOF) ? ret : 0;
779 }
780 
781 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
782  AVRational tb, const AVFrame *in_picture)
783 {
784  double pts_time;
785 
786  if (kf->ref_pts == AV_NOPTS_VALUE)
787  kf->ref_pts = in_picture->pts;
788 
789  pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
790  if (kf->index < kf->nb_pts &&
791  av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
792  kf->index++;
793  goto force_keyframe;
794  } else if (kf->pexpr) {
795  double res;
796  kf->expr_const_values[FKF_T] = pts_time;
797  res = av_expr_eval(kf->pexpr,
798  kf->expr_const_values, NULL);
799  av_log(logctx, AV_LOG_TRACE,
800  "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
806  res);
807 
808  kf->expr_const_values[FKF_N] += 1;
809 
810  if (res) {
814  goto force_keyframe;
815  }
816  } else if (kf->type == KF_FORCE_SOURCE && (in_picture->flags & AV_FRAME_FLAG_KEY)) {
817  goto force_keyframe;
818  }
819 
820  return AV_PICTURE_TYPE_NONE;
821 
822 force_keyframe:
823  av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
824  return AV_PICTURE_TYPE_I;
825 }
826 
827 /* May modify/reset frame */
828 static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *in_picture)
829 {
830  int ret;
831  AVCodecContext *enc = ost->enc_ctx;
832 
833  if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
834  return 0;
835 
836  in_picture->quality = enc->global_quality;
837  in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture);
838 
839 #if FFMPEG_OPT_TOP
840  if (ost->top_field_first >= 0) {
841  in_picture->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
842  in_picture->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
843  }
844 #endif
845 
846  ret = submit_encode_frame(of, ost, in_picture);
847  return (ret == AVERROR_EOF) ? 0 : ret;
848 }
849 
851 {
852  OutputFile *of = output_files[ost->file_index];
853  int ret;
854 
855  ret = enc_open(ost, frame);
856  if (ret < 0)
857  return ret;
858 
859  return ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ?
860  do_video_out(of, ost, frame) : do_audio_out(of, ost, frame);
861 }
862 
863 int enc_flush(void)
864 {
865  int ret;
866 
867  for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
868  OutputFile *of = output_files[ost->file_index];
869  if (ost->sq_idx_encode >= 0)
870  sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
871  }
872 
873  for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
874  Encoder *e = ost->enc;
875  AVCodecContext *enc = ost->enc_ctx;
876  OutputFile *of = output_files[ost->file_index];
877 
878  if (!enc || !e->opened ||
880  continue;
881 
883  if (ret != AVERROR_EOF)
884  return ret;
885  }
886 
887  return 0;
888 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVSubtitle
Definition: avcodec.h:2269
KeyframeForceCtx::pts
int64_t * pts
Definition: ffmpeg.h:502
avcodec_encode_subtitle
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: encode.c:190
AVFrame::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: frame.h:660
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
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodec
AVCodec.
Definition: codec.h:187
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
AVFrame::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:656
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
INFINITY
#define INFINITY
Definition: mathematics.h:118
FKF_PREV_FORCED_T
@ FKF_PREV_FORCED_T
Definition: ffmpeg.h:439
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:556
Encoder::opened
int opened
Definition: ffmpeg_enc.c:53
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:2274
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:1029
FrameData
Definition: ffmpeg.h:636
close_output_stream
void close_output_stream(OutputStream *ost)
Definition: ffmpeg.c:487
ENC_STATS_PTS
@ ENC_STATS_PTS
Definition: ffmpeg.h:455
ENC_STATS_FRAME_NUM_IN
@ ENC_STATS_FRAME_NUM_IN
Definition: ffmpeg.h:452
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
FKF_PREV_FORCED_N
@ FKF_PREV_FORCED_N
Definition: ffmpeg.h:438
AV_PKT_DATA_QUALITY_STATS
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
Definition: packet.h:133
forced_kf_apply
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture)
Definition: ffmpeg_enc.c:781
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
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ENC_STATS_DTS
@ ENC_STATS_DTS
Definition: ffmpeg.h:459
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:200
AVPictureType
AVPictureType
Definition: avutil.h:277
KeyframeForceCtx::nb_pts
int nb_pts
Definition: ffmpeg.h:503
rational.h
InputStream::dec_ctx
AVCodecContext * dec_ctx
Definition: ffmpeg.h:344
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:863
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2273
ENC_STATS_AVG_BITRATE
@ ENC_STATS_AVG_BITRATE
Definition: ffmpeg.h:465
AVFrame::color_primaries
enum AVColorPrimaries color_primaries
Definition: frame.h:658
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::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:667
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:1022
AVFrame::width
int width
Definition: frame.h:412
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:342
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
FKF_T
@ FKF_T
Definition: ffmpeg.h:440
AVPacket::data
uint8_t * data
Definition: packet.h:491
ENC_STATS_LITERAL
@ ENC_STATS_LITERAL
Definition: ffmpeg.h:448
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:1061
do_audio_out
static int do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:761
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:930
KeyframeForceCtx::type
int type
Definition: ffmpeg.h:497
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1776
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:509
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:322
HWDevice
Definition: ffmpeg.h:83
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:317
set_encoder_id
static int set_encoder_id(OutputFile *of, OutputStream *ost)
Definition: ffmpeg_enc.c:143
AV_CODEC_FLAG_INTERLACED_ME
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:347
psnr
static double psnr(double d)
Definition: ffmpeg_enc.c:548
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:454
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:204
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
AVFrame::opaque_ref
AVBufferRef * opaque_ref
Frame owner's private data.
Definition: frame.h:768
InputStream
Definition: ffmpeg.h:324
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1803
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:81
AVPacketSideData::size
size_t size
Definition: packet.h:344
AVFrame::chroma_location
enum AVChromaLocation chroma_location
Definition: frame.h:669
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:201
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:572
vstats_version
int vstats_version
Definition: ffmpeg_opt.c:89
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:450
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:527
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
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
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:521
KeyframeForceCtx::ref_pts
int64_t ref_pts
Definition: ffmpeg.h:499
enc_subtitle
int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
Definition: ffmpeg_enc.c:395
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:466
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:802
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
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:771
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:203
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:326
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
do_video_out
static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *in_picture)
Definition: ffmpeg_enc.c:828
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:1015
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
ENC_STATS_PTS_IN
@ ENC_STATS_PTS_IN
Definition: ffmpeg.h:457
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
EncStats::components
EncStatsComponent * components
Definition: ffmpeg.h:476
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:507
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:449
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:42
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:464
Encoder::data_size
uint64_t data_size
Definition: ffmpeg_enc.c:48
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:850
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1517
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:343
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:481
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:2275
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
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:228
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
KeyframeForceCtx::expr_const_values
double expr_const_values[FKF_NB]
Definition: ffmpeg.h:507
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:270
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
encode_frame
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
Definition: ffmpeg_enc.c:613
Encoder::pkt
AVPacket * pkt
Definition: ffmpeg_enc.c:45
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:880
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1039
InputStream::st
AVStream * st
Definition: ffmpeg.h:330
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
enc_open
int enc_open(OutputStream *ost, const AVFrame *frame)
Definition: ffmpeg_enc.c:168
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
ENC_STATS_PTS_TIME
@ ENC_STATS_PTS_TIME
Definition: ffmpeg.h:456
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:345
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:1777
EncStats
Definition: ffmpeg.h:475
vstats_file
FILE * vstats_file
Definition: ffmpeg.c:107
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:921
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:128
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:458
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
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:563
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1326
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:492
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
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:567
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
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:462
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:714
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:467
HWDevice::device_ref
AVBufferRef * device_ref
Definition: ffmpeg.h:86
hw_device_get_by_type
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:30
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:278
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:919
AVSubtitle::end_display_time
uint32_t end_display_time
Definition: avcodec.h:2272
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:490
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_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:41
OutputFile::sq_encode
SyncQueue * sq_encode
Definition: ffmpeg.h:626
KF_FORCE_SOURCE
@ KF_FORCE_SOURCE
Definition: ffmpeg.h:490
ENC_STATS_FRAME_NUM
@ ENC_STATS_FRAME_NUM
Definition: ffmpeg.h:451
KeyframeForceCtx
Definition: ffmpeg.h:496
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
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
AV_PKT_DATA_CPB_PROPERTIES
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition: packet.h:146
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
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
OutputFile::bitexact
int bitexact
Definition: ffmpeg.h:632
display.h
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:202
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:82
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1046
ENC_STATS_STREAM_IDX
@ ENC_STATS_STREAM_IDX
Definition: ffmpeg.h:450
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:223
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:518
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
ENC_STATS_SAMPLE_NUM
@ ENC_STATS_SAMPLE_NUM
Definition: ffmpeg.h:461
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
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
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
hw_device_setup_for_encode
static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
Definition: ffmpeg_enc.c:92
avformat.h
dict.h
av_packet_side_data_new
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition: avpacket.c:700
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:447
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.
AVFrame::hw_frames_ctx
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:752
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:441
AVFrame::height
int height
Definition: frame.h:412
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:847
HWDevice::name
const char * name
Definition: ffmpeg.h:84
AVRational::den
int den
Denominator.
Definition: rational.h:60
KeyframeForceCtx::index
int index
Definition: ffmpeg.h:504
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:383
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:859
enc_free
void enc_free(Encoder **penc)
Definition: ffmpeg_enc.c:56
AV_CODEC_CAP_PARAM_CHANGE
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition: codec.h:118
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:647
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:449
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:338
avcodec_parameters_from_context
int avcodec_parameters_from_context(struct AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:137
KeyframeForceCtx::pexpr
AVExpr * pexpr
Definition: ffmpeg.h:506
FKF_N_FORCED
@ FKF_N_FORCED
Definition: ffmpeg.h:437
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
AVPacket
This structure stores compressed data.
Definition: packet.h:468
EncStatsComponent
Definition: ffmpeg.h:468
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:85
EncStats::nb_components
int nb_components
Definition: ffmpeg.h:477
d
d
Definition: ffmpeg_filter.c:368
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
int32_t
int32_t
Definition: audioconvert.c:56
ENC_STATS_PKT_SIZE
@ ENC_STATS_PKT_SIZE
Definition: ffmpeg.h:463
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
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
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:436
ENC_STATS_DTS_TIME
@ ENC_STATS_DTS_TIME
Definition: ffmpeg.h:460
OutputFile::recording_time
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:628
of_stream_init
int of_stream_init(OutputFile *of, OutputStream *ost)
Definition: ffmpeg_mux.c:671
EncStats::io
AVIOContext * io
Definition: ffmpeg.h:479
update_video_stats
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
Definition: ffmpeg_enc.c:553
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:822
AVSubtitle::start_display_time
uint32_t start_display_time
Definition: avcodec.h:2271
ENC_STATS_TIMEBASE
@ ENC_STATS_TIMEBASE
Definition: ffmpeg.h:453
AVPacket::time_base
AVRational time_base
Time base of the packet's timestamps.
Definition: packet.h:535
enc_alloc
int enc_alloc(Encoder **penc, const AVCodec *codec)
Definition: ffmpeg_enc.c:70
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:51
InputStream::autorotate
int autorotate
Definition: ffmpeg.h:358