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/mem.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/rational.h"
36 #include "libavutil/time.h"
37 #include "libavutil/timestamp.h"
38 
39 #include "libavcodec/avcodec.h"
40 
41 typedef struct EncoderPriv {
43 
44  void *log_parent;
45  char log_name[32];
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;
55 
57  unsigned sch_idx;
58 } EncoderPriv;
59 
61 {
62  return (EncoderPriv*)enc;
63 }
64 
65 // data that is local to the decoder thread and not visible outside of it
66 typedef struct EncoderThread {
70 
71 void enc_free(Encoder **penc)
72 {
73  Encoder *enc = *penc;
74 
75  if (!enc)
76  return;
77 
78  if (enc->enc_ctx)
79  av_freep(&enc->enc_ctx->stats_in);
81 
82  av_freep(penc);
83 }
84 
85 static const char *enc_item_name(void *obj)
86 {
87  const EncoderPriv *ep = obj;
88 
89  return ep->log_name;
90 }
91 
92 static const AVClass enc_class = {
93  .class_name = "Encoder",
94  .version = LIBAVUTIL_VERSION_INT,
95  .parent_log_context_offset = offsetof(EncoderPriv, log_parent),
96  .item_name = enc_item_name,
97 };
98 
99 int enc_alloc(Encoder **penc, const AVCodec *codec,
100  Scheduler *sch, unsigned sch_idx, void *log_parent)
101 {
102  EncoderPriv *ep;
103  int ret = 0;
104 
105  *penc = NULL;
106 
107  ep = av_mallocz(sizeof(*ep));
108  if (!ep)
109  return AVERROR(ENOMEM);
110 
111  ep->e.class = &enc_class;
112  ep->log_parent = log_parent;
113 
114  ep->sch = sch;
115  ep->sch_idx = sch_idx;
116 
117  snprintf(ep->log_name, sizeof(ep->log_name), "enc:%s", codec->name);
118 
119  ep->e.enc_ctx = avcodec_alloc_context3(codec);
120  if (!ep->e.enc_ctx) {
121  ret = AVERROR(ENOMEM);
122  goto fail;
123  }
124 
125  *penc = &ep->e;
126 
127  return 0;
128 fail:
129  enc_free((Encoder**)&ep);
130  return ret;
131 }
132 
134  AVBufferRef *frames_ref)
135 {
136  const AVCodecHWConfig *config;
137  HWDevice *dev = NULL;
138 
139  if (frames_ref &&
140  ((AVHWFramesContext*)frames_ref->data)->format ==
141  enc_ctx->pix_fmt) {
142  // Matching format, will try to use hw_frames_ctx.
143  } else {
144  frames_ref = NULL;
145  }
146 
147  for (int i = 0;; i++) {
148  config = avcodec_get_hw_config(enc_ctx->codec, i);
149  if (!config)
150  break;
151 
152  if (frames_ref &&
154  (config->pix_fmt == AV_PIX_FMT_NONE ||
155  config->pix_fmt == enc_ctx->pix_fmt)) {
156  av_log(e, AV_LOG_VERBOSE, "Using input "
157  "frames context (format %s) with %s encoder.\n",
158  av_get_pix_fmt_name(enc_ctx->pix_fmt),
159  enc_ctx->codec->name);
160  enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
161  if (!enc_ctx->hw_frames_ctx)
162  return AVERROR(ENOMEM);
163  return 0;
164  }
165 
166  if (!dev &&
168  dev = hw_device_get_by_type(config->device_type);
169  }
170 
171  if (dev) {
172  av_log(e, AV_LOG_VERBOSE, "Using device %s "
173  "(type %s) with %s encoder.\n", dev->name,
174  av_hwdevice_get_type_name(dev->type), enc_ctx->codec->name);
175  enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
176  if (!enc_ctx->hw_device_ctx)
177  return AVERROR(ENOMEM);
178  } else {
179  // No device required, or no device available.
180  }
181  return 0;
182 }
183 
184 int enc_open(void *opaque, const AVFrame *frame)
185 {
186  OutputStream *ost = opaque;
187  InputStream *ist = ost->ist;
188  Encoder *e = ost->enc;
189  EncoderPriv *ep = ep_from_enc(e);
190  AVCodecContext *enc_ctx = e->enc_ctx;
191  Decoder *dec = NULL;
192  const AVCodec *enc = enc_ctx->codec;
193  OutputFile *of = ost->file;
194  FrameData *fd;
195  int frame_samples = 0;
196  int ret;
197 
198  if (ep->opened)
199  return 0;
200 
201  // frame is always non-NULL for audio and video
203 
204  if (frame) {
205  av_assert0(frame->opaque_ref);
206  fd = (FrameData*)frame->opaque_ref->data;
207 
210  if (ret < 0)
211  return ret;
212  }
213 
214  if (ist)
215  dec = ist->decoder;
216 
217  // the timebase is chosen by filtering code
218  if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
219  enc_ctx->time_base = frame->time_base;
220  enc_ctx->framerate = fd->frame_rate_filter;
221  }
222 
223  switch (enc_ctx->codec_type) {
224  case AVMEDIA_TYPE_AUDIO:
225  av_assert0(frame->format != AV_SAMPLE_FMT_NONE &&
226  frame->sample_rate > 0 &&
227  frame->ch_layout.nb_channels > 0);
228  enc_ctx->sample_fmt = frame->format;
229  enc_ctx->sample_rate = frame->sample_rate;
230  if (!enc_ctx->frame_size && (!(enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) ||
232  enc_ctx->frame_size = frame->nb_samples;
233  ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
234  if (ret < 0)
235  return ret;
236 
237  if (ost->bits_per_raw_sample)
238  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
239  else
241  av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
242  break;
243 
244  case AVMEDIA_TYPE_VIDEO: {
245  av_assert0(frame->format != AV_PIX_FMT_NONE &&
246  frame->width > 0 &&
247  frame->height > 0);
248  enc_ctx->width = frame->width;
249  enc_ctx->height = frame->height;
250  enc_ctx->sample_aspect_ratio =
251  ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
252  av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
253  frame->sample_aspect_ratio;
254 
255  enc_ctx->pix_fmt = frame->format;
256 
257  if (ost->bits_per_raw_sample)
258  enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
259  else
261  av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
262 
263  /**
264  * The video color properties should always be in sync with the user-
265  * requested values, since we forward them to the filter graph.
266  */
267  enc_ctx->color_range = frame->color_range;
268  enc_ctx->color_primaries = frame->color_primaries;
269  enc_ctx->color_trc = frame->color_trc;
270  enc_ctx->colorspace = frame->colorspace;
271  enc_ctx->alpha_mode = frame->alpha_mode;
272 
273  /* Video properties which are not part of filter graph negotiation */
275  enc_ctx->chroma_sample_location = frame->chroma_location;
276  } else if (enc_ctx->chroma_sample_location != frame->chroma_location &&
277  frame->chroma_location != AVCHROMA_LOC_UNSPECIFIED) {
279  "Requested chroma sample location '%s' does not match the "
280  "frame tagged sample location '%s'; result may be incorrect.\n",
282  av_chroma_location_name(frame->chroma_location));
283  }
284 
287 #if FFMPEG_OPT_TOP
288  || ost->top_field_first >= 0
289 #endif
290  ) {
291  int top_field_first =
292 #if FFMPEG_OPT_TOP
293  ost->top_field_first >= 0 ?
294  ost->top_field_first :
295 #endif
297 
298  if (enc->id == AV_CODEC_ID_MJPEG)
299  enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
300  else
301  enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
302  } else
304 
305  break;
306  }
308  enc_ctx->time_base = AV_TIME_BASE_Q;
309 
310  if (!enc_ctx->width) {
311  enc_ctx->width = ost->ist->par->width;
312  enc_ctx->height = ost->ist->par->height;
313  }
314 
315  av_assert0(dec);
316  if (dec->subtitle_header) {
317  /* ASS code assumes this buffer is null terminated so add extra byte. */
318  enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
319  if (!enc_ctx->subtitle_header)
320  return AVERROR(ENOMEM);
321  memcpy(enc_ctx->subtitle_header, dec->subtitle_header,
322  dec->subtitle_header_size);
324  }
325 
326  break;
327  default:
328  av_assert0(0);
329  break;
330  }
331 
332  if (ost->bitexact)
333  enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
334 
336  enc_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
337 
339 
340  ret = hw_device_setup_for_encode(e, enc_ctx, frame ? frame->hw_frames_ctx : NULL);
341  if (ret < 0) {
342  av_log(e, AV_LOG_ERROR,
343  "Encoding hardware device setup failed: %s\n", av_err2str(ret));
344  return ret;
345  }
346 
347  if ((ret = avcodec_open2(enc_ctx, enc, NULL)) < 0) {
348  if (ret != AVERROR_EXPERIMENTAL)
349  av_log(e, AV_LOG_ERROR, "Error while opening encoder - maybe "
350  "incorrect parameters such as bit_rate, rate, width or height.\n");
351  return ret;
352  }
353 
354  ep->opened = 1;
355 
356  if (enc_ctx->frame_size)
357  frame_samples = enc_ctx->frame_size;
358 
359  if (enc_ctx->bit_rate && enc_ctx->bit_rate < 1000 &&
360  enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
361  av_log(e, AV_LOG_WARNING, "The bitrate parameter is set too low."
362  " It takes bits/s as argument, not kbits/s\n");
363 
364  ret = of_stream_init(of, ost, enc_ctx);
365  if (ret < 0)
366  return ret;
367 
368  return frame_samples;
369 }
370 
372 {
373  OutputFile *of = ost->file;
374 
375  if (of->recording_time != INT64_MAX &&
376  av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
377  return 0;
378  }
379  return 1;
380 }
381 
382 static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub,
383  AVPacket *pkt)
384 {
385  Encoder *e = ost->enc;
386  EncoderPriv *ep = ep_from_enc(e);
387  int subtitle_out_max_size = 1024 * 1024;
388  int subtitle_out_size, nb, i, ret;
389  AVCodecContext *enc;
390  int64_t pts;
391 
392  if (sub->pts == AV_NOPTS_VALUE) {
393  av_log(e, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
394  return exit_on_error ? AVERROR(EINVAL) : 0;
395  }
396  if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
397  return 0;
398 
399  enc = e->enc_ctx;
400 
401  /* Note: DVB subtitle need one packet to draw them and one other
402  packet to clear them */
403  /* XXX: signal it in the codec context ? */
405  nb = 2;
406  else if (enc->codec_id == AV_CODEC_ID_ASS)
407  nb = FFMAX(sub->num_rects, 1);
408  else
409  nb = 1;
410 
411  /* shift timestamp to honor -ss and make check_recording_time() work with -t */
412  pts = sub->pts;
413  if (of->start_time != AV_NOPTS_VALUE)
414  pts -= of->start_time;
415  for (i = 0; i < nb; i++) {
416  AVSubtitle local_sub = *sub;
417 
419  return AVERROR_EOF;
420 
421  ret = av_new_packet(pkt, subtitle_out_max_size);
422  if (ret < 0)
423  return AVERROR(ENOMEM);
424 
425  local_sub.pts = pts;
426  // start_display_time is required to be 0
427  local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
428  local_sub.end_display_time -= sub->start_display_time;
429  local_sub.start_display_time = 0;
430 
431  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
432  local_sub.num_rects = 0;
433  else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
434  local_sub.num_rects = 1;
435  local_sub.rects += i;
436  }
437 
438  e->frames_encoded++;
439 
440  subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
441  if (subtitle_out_size < 0) {
442  av_log(e, AV_LOG_FATAL, "Subtitle encoding failed\n");
443  return subtitle_out_size;
444  }
445 
446  av_shrink_packet(pkt, subtitle_out_size);
448  pkt->pts = sub->pts;
450  if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
451  /* XXX: the pts correction is handled here. Maybe handling
452  it in the codec would be better */
453  if (i == 0)
454  pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
455  else
456  pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
457  }
458  pkt->dts = pkt->pts;
459 
460  ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
461  if (ret < 0) {
463  return ret;
464  }
465  }
466 
467  return 0;
468 }
469 
471  const AVFrame *frame, const AVPacket *pkt,
472  uint64_t frame_num)
473 {
474  Encoder *e = ost->enc;
475  EncoderPriv *ep = ep_from_enc(e);
476  AVIOContext *io = es->io;
477  AVRational tb = frame ? frame->time_base : pkt->time_base;
478  int64_t pts = frame ? frame->pts : pkt->pts;
479 
480  AVRational tbi = (AVRational){ 0, 1};
481  int64_t ptsi = INT64_MAX;
482 
483  const FrameData *fd = NULL;
484 
485  if (frame ? frame->opaque_ref : pkt->opaque_ref) {
486  fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
487  tbi = fd->dec.tb;
488  ptsi = fd->dec.pts;
489  }
490 
491  pthread_mutex_lock(&es->lock);
492 
493  for (size_t i = 0; i < es->nb_components; i++) {
494  const EncStatsComponent *c = &es->components[i];
495 
496  switch (c->type) {
497  case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
498  case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file->index); continue;
499  case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
500  case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
501  case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
502  case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
503  case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
504  case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
505  case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
506  INFINITY : ptsi * av_q2d(tbi)); continue;
507  case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
508  case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
509  }
510 
511  if (frame) {
512  switch (c->type) {
513  case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, e->samples_encoded); continue;
514  case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
515  default: av_assert0(0);
516  }
517  } else {
518  switch (c->type) {
519  case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
520  case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
521  case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
523  "K" : "N", 1); continue;
524  case ENC_STATS_BITRATE: {
525  double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
526  avio_printf(io, "%g", 8.0 * pkt->size / duration);
527  continue;
528  }
529  case ENC_STATS_AVG_BITRATE: {
530  double duration = pkt->dts * av_q2d(tb);
531  avio_printf(io, "%g", duration > 0 ? 8.0 * ep->data_size / duration : -1.);
532  continue;
533  }
534  default: av_assert0(0);
535  }
536  }
537  }
538  avio_w8(io, '\n');
539  avio_flush(io);
540 
542 }
543 
544 static inline double psnr(double d)
545 {
546  return -10.0 * log10(d);
547 }
548 
549 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
550 {
551  Encoder *e = ost->enc;
552  EncoderPriv *ep = ep_from_enc(e);
554  NULL);
555  AVCodecContext *enc = e->enc_ctx;
556  enum AVPictureType pict_type;
557  int64_t frame_number;
558  double ti1, bitrate, avg_bitrate;
559  double psnr_val = -1;
560  int quality;
561 
562  quality = sd ? AV_RL32(sd) : -1;
563  pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
564 
565  atomic_store(&ost->quality, quality);
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 = ep->packets_encoded;
587  if (vstats_version <= 1) {
588  fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
589  quality / (float)FF_QP2LAMBDA);
590  } else {
591  fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ",
592  ost->file->index, ost->index, frame_number,
593  quality / (float)FF_QP2LAMBDA);
594  }
595 
596  if (psnr_val >= 0)
597  fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
598 
599  fprintf(vstats_file,"f_size= %6d ", pkt->size);
600  /* compute pts value */
601  ti1 = pkt->dts * av_q2d(pkt->time_base);
602  if (ti1 < 0.01)
603  ti1 = 0.01;
604 
605  bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
606  avg_bitrate = (double)(ep->data_size * 8) / ti1 / 1000.0;
607  fprintf(vstats_file, "s_size= %8.0fKiB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
608  (double)ep->data_size / 1024, ti1, bitrate, avg_bitrate);
609  fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
610 
611  return 0;
612 }
613 
615  AVPacket *pkt)
616 {
617  Encoder *e = ost->enc;
618  EncoderPriv *ep = ep_from_enc(e);
619  AVCodecContext *enc = e->enc_ctx;
620  const char *type_desc = av_get_media_type_string(enc->codec_type);
621  const char *action = frame ? "encode" : "flush";
622  int ret;
623 
624  if (frame) {
625  FrameData *fd = frame_data(frame);
626 
627  if (!fd)
628  return AVERROR(ENOMEM);
629 
631 
632  if (ost->enc_stats_pre.io)
633  enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
634  e->frames_encoded);
635 
636  e->frames_encoded++;
637  e->samples_encoded += frame->nb_samples;
638 
639  if (debug_ts) {
640  av_log(e, AV_LOG_INFO, "encoder <- type:%s "
641  "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
642  type_desc,
643  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
644  enc->time_base.num, enc->time_base.den);
645  }
646 
647  if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
648  enc->sample_aspect_ratio = frame->sample_aspect_ratio;
649  }
650 
652 
653  ret = avcodec_send_frame(enc, frame);
654  if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
655  av_log(e, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
656  type_desc);
657  return ret;
658  }
659 
660  while (1) {
661  FrameData *fd;
662 
664 
666  update_benchmark("%s_%s %d.%d", action, type_desc,
667  of->index, ost->index);
668 
669  pkt->time_base = enc->time_base;
670 
671  /* if two pass, output log on success and EOF */
672  if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
673  fprintf(ost->logfile, "%s", enc->stats_out);
674 
675  if (ret == AVERROR(EAGAIN)) {
676  av_assert0(frame); // should never happen during flushing
677  return 0;
678  } else if (ret < 0) {
679  if (ret != AVERROR_EOF)
680  av_log(e, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
681  return ret;
682  }
683 
684  fd = packet_data(pkt);
685  if (!fd)
686  return AVERROR(ENOMEM);
688 
689  // attach stream parameters to first packet if requested
691  if (ep->attach_par && !ep->packets_encoded) {
693  if (!fd->par_enc)
694  return AVERROR(ENOMEM);
695 
697  if (ret < 0)
698  return ret;
699  }
700 
702 
703  if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
705  if (ret < 0)
706  return ret;
707  }
708 
709  if (ost->enc_stats_post.io)
710  enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
711  ep->packets_encoded);
712 
713  if (debug_ts) {
714  av_log(e, AV_LOG_INFO, "encoder -> type:%s "
715  "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
716  "duration:%s duration_time:%s\n",
717  type_desc,
721  }
722 
723  ep->data_size += pkt->size;
724 
725  ep->packets_encoded++;
726 
727  ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
728  if (ret < 0) {
730  return ret;
731  }
732  }
733 
734  av_unreachable("encode_frame() loop should return");
735 }
736 
737 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
738  const AVFrame *frame)
739 {
740  double pts_time;
741 
742  if (kf->ref_pts == AV_NOPTS_VALUE)
743  kf->ref_pts = frame->pts;
744 
745  pts_time = (frame->pts - kf->ref_pts) * av_q2d(frame->time_base);
746  if (kf->index < kf->nb_pts &&
747  av_compare_ts(frame->pts, frame->time_base, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
748  kf->index++;
749  goto force_keyframe;
750  } else if (kf->pexpr) {
751  double res;
752  kf->expr_const_values[FKF_T] = pts_time;
753  res = av_expr_eval(kf->pexpr,
754  kf->expr_const_values, NULL);
755  av_log(logctx, AV_LOG_TRACE,
756  "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
762  res);
763 
764  kf->expr_const_values[FKF_N] += 1;
765 
766  if (res) {
770  goto force_keyframe;
771  }
772  } else if (kf->type == KF_FORCE_SOURCE && (frame->flags & AV_FRAME_FLAG_KEY)) {
773  goto force_keyframe;
774  } else if (kf->type == KF_FORCE_SCD_METADATA &&
775  av_dict_get(frame->metadata, "lavfi.scd.time", NULL, 0)) {
776  goto force_keyframe;
777  }
778 
779  return AV_PICTURE_TYPE_NONE;
780 
781 force_keyframe:
782  av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
783  return AV_PICTURE_TYPE_I;
784 }
785 
787 {
788  Encoder *e = ost->enc;
789  OutputFile *of = ost->file;
790  enum AVMediaType type = ost->type;
791 
792  if (type == AVMEDIA_TYPE_SUBTITLE) {
793  const AVSubtitle *subtitle = frame && frame->buf[0] ?
794  (AVSubtitle*)frame->buf[0]->data : NULL;
795 
796  // no flushing for subtitles
797  return subtitle && subtitle->num_rects ?
798  do_subtitle_out(of, ost, subtitle, pkt) : 0;
799  }
800 
801  if (frame) {
802  if (!check_recording_time(ost, frame->pts, frame->time_base))
803  return AVERROR_EOF;
804 
805  if (type == AVMEDIA_TYPE_VIDEO) {
806  frame->quality = e->enc_ctx->global_quality;
807  frame->pict_type = forced_kf_apply(e, &ost->kf, frame);
808 
809 #if FFMPEG_OPT_TOP
810  if (ost->top_field_first >= 0) {
812  frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
813  }
814 #endif
815  } else {
817  e->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
818  av_log(e, AV_LOG_ERROR,
819  "Audio channel count changed and encoder does not support parameter changes\n");
820  return 0;
821  }
822  }
823  }
824 
825  return encode_frame(of, ost, frame, pkt);
826 }
827 
829 {
830  char name[16];
831  snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
832  ost->enc->enc_ctx->codec->name);
834 }
835 
837 {
838  av_packet_free(&et->pkt);
839  av_frame_free(&et->frame);
840 
841  memset(et, 0, sizeof(*et));
842 }
843 
845 {
846  memset(et, 0, sizeof(*et));
847 
848  et->frame = av_frame_alloc();
849  if (!et->frame)
850  goto fail;
851 
852  et->pkt = av_packet_alloc();
853  if (!et->pkt)
854  goto fail;
855 
856  return 0;
857 
858 fail:
859  enc_thread_uninit(et);
860  return AVERROR(ENOMEM);
861 }
862 
863 int encoder_thread(void *arg)
864 {
865  OutputStream *ost = arg;
866  Encoder *e = ost->enc;
867  EncoderPriv *ep = ep_from_enc(e);
868  EncoderThread et;
869  int ret = 0, input_status = 0;
870  int name_set = 0;
871 
872  ret = enc_thread_init(&et);
873  if (ret < 0)
874  goto finish;
875 
876  /* Open the subtitle encoders immediately. AVFrame-based encoders
877  * are opened through a callback from the scheduler once they get
878  * their first frame
879  *
880  * N.B.: because the callback is called from a different thread,
881  * enc_ctx MUST NOT be accessed before sch_enc_receive() returns
882  * for the first time for audio/video. */
883  if (ost->type != AVMEDIA_TYPE_VIDEO && ost->type != AVMEDIA_TYPE_AUDIO) {
884  ret = enc_open(ost, NULL);
885  if (ret < 0)
886  goto finish;
887  }
888 
889  while (!input_status) {
890  input_status = sch_enc_receive(ep->sch, ep->sch_idx, et.frame);
891  if (input_status < 0) {
892  if (input_status == AVERROR_EOF) {
893  av_log(e, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
894  if (ep->opened)
895  break;
896 
897  av_log(e, AV_LOG_ERROR, "Could not open encoder before EOF\n");
898  ret = AVERROR(EINVAL);
899  } else {
900  av_log(e, AV_LOG_ERROR, "Error receiving a frame for encoding: %s\n",
901  av_err2str(ret));
902  ret = input_status;
903  }
904  goto finish;
905  }
906 
907  if (!name_set) {
909  name_set = 1;
910  }
911 
912  ret = frame_encode(ost, et.frame, et.pkt);
913 
914  av_packet_unref(et.pkt);
915  av_frame_unref(et.frame);
916 
917  if (ret < 0) {
918  if (ret == AVERROR_EOF)
919  av_log(e, AV_LOG_VERBOSE, "Encoder returned EOF, finishing\n");
920  else
921  av_log(e, AV_LOG_ERROR, "Error encoding a frame: %s\n",
922  av_err2str(ret));
923  break;
924  }
925  }
926 
927  // flush the encoder
928  if (ret == 0 || ret == AVERROR_EOF) {
929  ret = frame_encode(ost, NULL, et.pkt);
930  if (ret < 0 && ret != AVERROR_EOF)
931  av_log(e, AV_LOG_ERROR, "Error flushing encoder: %s\n",
932  av_err2str(ret));
933  }
934 
935  // EOF is normal thread termination
936  if (ret == AVERROR_EOF)
937  ret = 0;
938 
939 finish:
940  enc_thread_uninit(&et);
941 
942  return ret;
943 }
944 
946 {
947  EncoderPriv *ep = ep_from_enc(enc);
948  ep->attach_par = 1;
949  return ep->sch_idx;
950 }
frame_samples
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
Definition: sync_queue.c:131
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AVSubtitle
Definition: avcodec.h:2100
KeyframeForceCtx::pts
int64_t * pts
Definition: ffmpeg.h:622
Decoder::subtitle_header
const uint8_t * subtitle_header
Definition: ffmpeg.h:474
avcodec_encode_subtitle
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: encode.c:203
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1068
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:433
FrameData::par_enc
AVCodecParameters * par_enc
Definition: ffmpeg.h:741
AVCodec
AVCodec.
Definition: codec.h:172
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:203
av_gettime_relative
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
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:560
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
AVCodecContext::alpha_mode
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition: avcodec.h:1950
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:552
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:2105
FrameData::nb_side_data
int nb_side_data
Definition: ffmpeg.h:744
Encoder::samples_encoded
uint64_t samples_encoded
Definition: ffmpeg.h:639
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:671
KF_FORCE_SCD_METADATA
@ KF_FORCE_SCD_METADATA
Definition: ffmpeg.h:613
FrameData
Definition: ffmpeg.h:722
AVCodecContext::decoded_side_data
AVFrameSideData ** decoded_side_data
Array containing static side data, such as HDR10 CLL / MDCV structures.
Definition: avcodec.h:1942
ENC_STATS_PTS
@ ENC_STATS_PTS
Definition: ffmpeg.h:576
ENC_STATS_FRAME_NUM_IN
@ ENC_STATS_FRAME_NUM_IN
Definition: ffmpeg.h:573
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1040
FKF_PREV_FORCED_N
@ FKF_PREV_FORCED_N
Definition: ffmpeg.h:559
AV_PKT_DATA_QUALITY_STATS
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
Definition: packet.h:129
clone_side_data
static int clone_side_data(AVFrameSideData ***dst, int *nb_dst, AVFrameSideData *const *src, int nb_src, unsigned int flags)
Wrapper calling av_frame_side_data_clone() in a loop for all source entries.
Definition: ffmpeg_utils.h:50
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:3456
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:580
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:213
AVPictureType
AVPictureType
Definition: avutil.h:276
KeyframeForceCtx::nb_pts
int nb_pts
Definition: ffmpeg.h:623
rational.h
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:263
int64_t
long long int64_t
Definition: coverity.c:34
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2104
ENC_STATS_AVG_BITRATE
@ ENC_STATS_AVG_BITRATE
Definition: ffmpeg.h:586
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
OutputFile::start_time
int64_t start_time
start time in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:716
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:459
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:664
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:191
FKF_T
@ FKF_T
Definition: ffmpeg.h:561
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:311
AVPacket::data
uint8_t * data
Definition: packet.h:595
ENC_STATS_LITERAL
@ ENC_STATS_LITERAL
Definition: ffmpeg.h:569
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:694
pthread_mutex_lock
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:119
KeyframeForceCtx::type
int type
Definition: ffmpeg.h:617
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Definition: avcodec.h:1757
forced_kf_apply
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, const AVFrame *frame)
Definition: ffmpeg_enc.c:737
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:613
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:306
HWDevice
Definition: ffmpeg.h:110
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
LATENCY_PROBE_ENC_POST
@ LATENCY_PROBE_ENC_POST
Definition: ffmpeg.h:106
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
AV_CODEC_FLAG_INTERLACED_ME
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:331
psnr
static double psnr(double d)
Definition: ffmpeg_enc.c:544
ost
static AVStream * ost
Definition: vaapi_transcode.c:42
tf_sess_config.config
config
Definition: tf_sess_config.py:33
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:650
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
av_chroma_location_name
const char * av_chroma_location_name(enum AVChromaLocation location)
Definition: pixdesc.c:3877
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
encode_frame
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition: ffmpeg_enc.c:614
ENC_STATS_TIMEBASE_IN
@ ENC_STATS_TIMEBASE_IN
Definition: ffmpeg.h:575
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:217
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:687
FrameData::frame_num
uint64_t frame_num
Definition: ffmpeg.h:729
InputStream
Definition: ffmpeg.h:483
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:563
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:70
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:214
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:594
AV_CODEC_FLAG_COPY_OPAQUE
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition: avcodec.h:279
finish
static void finish(void)
Definition: movenc.c:374
EncoderPriv::log_name
char log_name[32]
Definition: ffmpeg_enc.c:45
vstats_version
int vstats_version
Definition: ffmpeg_opt.c:79
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:452
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:631
FrameData::dec
struct FrameData::@6 dec
AV_CODEC_FLAG_FRAME_DURATION
#define AV_CODEC_FLAG_FRAME_DURATION
Signal to the encoder that the values of AVFrame.duration are valid and should be used (typically for...
Definition: avcodec.h:286
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1055
fail
#define fail()
Definition: checkasm.h:225
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: packet.c:113
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:500
KeyframeForceCtx::ref_pts
int64_t ref_pts
Definition: ffmpeg.h:619
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:551
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:644
FrameData::tb
AVRational tb
Definition: ffmpeg.h:732
EncoderPriv::packets_encoded
uint64_t packets_encoded
Definition: ffmpeg_enc.c:51
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:216
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:310
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:573
enc_thread_uninit
static void enc_thread_uninit(EncoderThread *et)
Definition: ffmpeg_enc.c:836
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:657
FrameData::frame_rate_filter
AVRational frame_rate_filter
Definition: ffmpeg.h:735
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AV_FRAME_SIDE_DATA_FLAG_UNIQUE
#define AV_FRAME_SIDE_DATA_FLAG_UNIQUE
Remove existing entries before adding new ones.
Definition: frame.h:1080
encoder_thread
int encoder_thread(void *arg)
Definition: ffmpeg_enc.c:863
ENC_STATS_PTS_IN
@ ENC_STATS_PTS_IN
Definition: ffmpeg.h:578
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:674
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:60
EncStats::components
EncStatsComponent * components
Definition: ffmpeg.h:598
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
enc_class
static const AVClass enc_class
Definition: ffmpeg_enc.c:92
intreadwrite.h
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1338
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: packet.c:98
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1235
vstats_filename
char * vstats_filename
Definition: ffmpeg_opt.c:54
EncoderPriv::attach_par
int attach_par
Definition: ffmpeg_enc.c:54
pthread_mutex_unlock
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:126
Encoder::class
const AVClass * class
Definition: ffmpeg.h:633
AVCodecContext::nb_decoded_side_data
int nb_decoded_side_data
Definition: avcodec.h:1943
bitrate
int64_t bitrate
Definition: av1_levels.c:47
ENC_STATS_FILE_IDX
@ ENC_STATS_FILE_IDX
Definition: ffmpeg.h:570
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
enc_item_name
static const char * enc_item_name(void *obj)
Definition: ffmpeg_enc.c:85
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:144
ENC_STATS_BITRATE
@ ENC_STATS_BITRATE
Definition: ffmpeg.h:585
Encoder::frames_encoded
uint64_t frames_encoded
Definition: ffmpeg.h:638
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
LATENCY_PROBE_ENC_PRE
@ LATENCY_PROBE_ENC_PRE
Definition: ffmpeg.h:105
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1571
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
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:470
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:824
EncoderPriv::sch_idx
unsigned sch_idx
Definition: ffmpeg_enc.c:57
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
KF_FORCE_SOURCE
@ KF_FORCE_SOURCE
Definition: ffmpeg.h:608
AVSubtitle::pts
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:2106
Encoder::enc_ctx
AVCodecContext * enc_ctx
Definition: ffmpeg.h:635
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:120
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:527
sch_enc_receive
int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
Called by encoder tasks to obtain frames for encoding.
Definition: ffmpeg_sched.c:2436
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:453
arg
const char * arg
Definition: jacosubdec.c:65
KeyframeForceCtx::expr_const_values
double expr_const_values[FKF_NB]
Definition: ffmpeg.h:627
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:228
ENC_STATS_KEYFRAME
@ ENC_STATS_KEYFRAME
Definition: ffmpeg.h:587
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
ep_from_enc
static EncoderPriv * ep_from_enc(Encoder *enc)
Definition: ffmpeg_enc.c:60
NULL
#define NULL
Definition: coverity.c:32
frame_encode
static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition: ffmpeg_enc.c:786
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:681
AVCodec::type
enum AVMediaType type
Definition: codec.h:185
Decoder
Definition: ffmpeg.h:469
ENC_STATS_PTS_TIME
@ ENC_STATS_PTS_TIME
Definition: ffmpeg.h:577
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:164
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:116
EncStats::lock
pthread_mutex_t lock
Definition: ffmpeg.h:603
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:493
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
AVCodecContext::subtitle_header_size
int subtitle_header_size
Header containing style information for text subtitles.
Definition: avcodec.h:1756
EncStats
Definition: ffmpeg.h:597
vstats_file
FILE * vstats_file
Definition: ffmpeg.c:92
FrameData::wallclock
int64_t wallclock[LATENCY_PROBE_NB]
Definition: ffmpeg.h:739
AV_CODEC_CAP_VARIABLE_FRAME_SIZE
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:113
double
double
Definition: af_crystalizer.c:132
of_stream_init
int of_stream_init(OutputFile *of, OutputStream *ost, const AVCodecContext *enc_ctx)
Definition: ffmpeg_mux.c:611
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:144
time.h
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:184
OutputFile::index
int index
Definition: ffmpeg.h:708
EncoderThread::pkt
AVPacket * pkt
Definition: ffmpeg_enc.c:68
ENC_STATS_PTS_TIME_IN
@ ENC_STATS_PTS_TIME_IN
Definition: ffmpeg.h:579
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
sch_enc_send
int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
Called by encoder tasks to send encoded packets downstream.
Definition: ffmpeg_sched.c:2480
enc_loopback
int enc_loopback(Encoder *enc)
Definition: ffmpeg_enc.c:945
Scheduler
Definition: ffmpeg_sched.c:273
enc_thread_init
static int enc_thread_init(EncoderThread *et)
Definition: ffmpeg_enc.c:844
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:547
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1330
eval.h
FrameData::side_data
AVFrameSideData ** side_data
Definition: ffmpeg.h:743
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:507
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:83
AVMediaType
AVMediaType
Definition: avutil.h:198
AVPacket::size
int size
Definition: packet.h:596
FFMPEG_OPT_TOP
#define FFMPEG_OPT_TOP
Definition: ffmpeg.h:58
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
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:122
EncoderPriv::sch
Scheduler * sch
Definition: ffmpeg_enc.c:56
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1047
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
ENC_STATS_NB_SAMPLES
@ ENC_STATS_NB_SAMPLES
Definition: ffmpeg.h:583
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
hw_device_setup_for_encode
static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx, AVBufferRef *frames_ref)
Definition: ffmpeg_enc.c:133
HWDevice::device_ref
AVBufferRef * device_ref
Definition: ffmpeg.h:113
hw_device_get_by_type
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:28
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:797
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:277
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:477
AVSubtitle::end_display_time
uint32_t end_display_time
Definition: avcodec.h:2103
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:594
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:206
EncoderPriv::data_size
uint64_t data_size
Definition: ffmpeg_enc.c:48
FrameData::pts
int64_t pts
Definition: ffmpeg.h:731
EncoderPriv::opened
int opened
Definition: ffmpeg_enc.c:53
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:601
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
Encoder
Definition: ffmpeg.h:632
AVCodecParameters::avcodec_parameters_alloc
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: codec_par.c:57
ENC_STATS_FRAME_NUM
@ ENC_STATS_FRAME_NUM
Definition: ffmpeg.h:572
KeyframeForceCtx
Definition: ffmpeg.h:616
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
AVCodec::id
enum AVCodecID id
Definition: codec.h:186
enc_thread_set_name
static void enc_thread_set_name(const OutputStream *ost)
Definition: ffmpeg_enc.c:828
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
log.h
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:588
FrameData::bits_per_raw_sample
int bits_per_raw_sample
Definition: ffmpeg.h:737
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: packet.c:252
display.h
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:215
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:71
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:496
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1493
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:688
ENC_STATS_STREAM_IDX
@ ENC_STATS_STREAM_IDX
Definition: ffmpeg.h:571
AVCodecContext::height
int height
Definition: avcodec.h:604
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:519
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:643
ENC_STATS_SAMPLE_NUM
@ ENC_STATS_SAMPLE_NUM
Definition: ffmpeg.h:582
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:682
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1471
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:118
InputStream::decoder
Decoder * decoder
Definition: ffmpeg.h:500
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:204
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
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:265
EncoderThread::frame
AVFrame * frame
Definition: ffmpeg_enc.c:67
EncoderPriv
Definition: ffmpeg_enc.c:41
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecParameters::avcodec_parameters_free
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: codec_par.c:67
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:443
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:752
HWDevice::name
const char * name
Definition: ffmpeg.h:111
enc_open
int enc_open(void *opaque, const AVFrame *frame)
Definition: ffmpeg_enc.c:184
AVRational::den
int den
Denominator.
Definition: rational.h:60
KeyframeForceCtx::index
int index
Definition: ffmpeg.h:624
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
EncoderPriv::e
Encoder e
Definition: ffmpeg_enc.c:42
do_subtitle_out
static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub, AVPacket *pkt)
Definition: ffmpeg_enc.c:382
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
Decoder::subtitle_header_size
int subtitle_header_size
Definition: ffmpeg.h:475
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:298
check_recording_time
static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
Definition: ffmpeg_enc.c:371
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:850
enc_free
void enc_free(Encoder **penc)
Definition: ffmpeg_enc.c:71
AV_CODEC_CAP_PARAM_CHANGE
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition: codec.h:103
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:450
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:451
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
avutil.h
mem.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:322
enc_alloc
int enc_alloc(Encoder **penc, const AVCodec *codec, Scheduler *sch, unsigned sch_idx, void *log_parent)
Definition: ffmpeg_enc.c:99
AVCodecParameters::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:138
KeyframeForceCtx::pexpr
AVExpr * pexpr
Definition: ffmpeg.h:626
FKF_N_FORCED
@ FKF_N_FORCED
Definition: ffmpeg.h:558
AVPacket
This structure stores compressed data.
Definition: packet.h:572
EncStatsComponent
Definition: ffmpeg.h:590
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
HWDevice::type
enum AVHWDeviceType type
Definition: ffmpeg.h:112
EncStats::nb_components
int nb_components
Definition: ffmpeg.h:599
packet_data
FrameData * packet_data(AVPacket *pkt)
Definition: ffmpeg.c:489
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:604
AV_CODEC_FLAG2_FIXED_FRAME_SIZE
#define AV_CODEC_FLAG2_FIXED_FRAME_SIZE
Force audio encoders to use a fixed frame size.
Definition: avcodec.h:359
ENC_STATS_PKT_SIZE
@ ENC_STATS_PKT_SIZE
Definition: ffmpeg.h:584
timestamp.h
OutputStream
Definition: mux.c:53
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
EncoderPriv::log_parent
void * log_parent
Definition: ffmpeg_enc.c:44
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:330
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
avstring.h
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:226
FKF_N
@ FKF_N
Definition: ffmpeg.h:557
ENC_STATS_DTS_TIME
@ ENC_STATS_DTS_TIME
Definition: ffmpeg.h:581
OutputFile::recording_time
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:715
AV_PKT_FLAG_TRUSTED
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
Definition: packet.h:664
snprintf
#define snprintf
Definition: snprintf.h:34
EncStats::io
AVIOContext * io
Definition: ffmpeg.h:601
update_video_stats
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
Definition: ffmpeg_enc.c:549
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:628
AVSubtitle::start_display_time
uint32_t start_display_time
Definition: avcodec.h:2102
duration
static int64_t duration
Definition: ffplay.c:329
ENC_STATS_TIMEBASE
@ ENC_STATS_TIMEBASE
Definition: ffmpeg.h:574
AVPacket::time_base
AVRational time_base
Time base of the packet's timestamps.
Definition: packet.h:639
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:3376
EncoderThread
Definition: ffmpeg_enc.c:66
OutputFile
Definition: ffmpeg.h:705
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:216