FFmpeg
ffmpeg_mux_init.c
Go to the documentation of this file.
1 /*
2  * Muxer/output file setup.
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <string.h>
22 
23 #include "cmdutils.h"
24 #include "ffmpeg.h"
25 #include "ffmpeg_mux.h"
26 #include "ffmpeg_sched.h"
27 #include "fopen_utf8.h"
28 
29 #include "libavformat/avformat.h"
30 #include "libavformat/avio.h"
31 
32 #include "libavcodec/avcodec.h"
33 
34 #include "libavfilter/avfilter.h"
35 
36 #include "libavutil/avassert.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/avutil.h"
39 #include "libavutil/bprint.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/display.h"
42 #include "libavutil/getenv_utf8.h"
43 #include "libavutil/iamf.h"
44 #include "libavutil/intreadwrite.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/pixdesc.h"
50 
51 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
52 
53 static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
54  const char *opt_name, int flag)
55 {
56  const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0);
57 
58  if (e) {
59  const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0);
60  int val = 0;
61  if (!o)
62  return 0;
63  av_opt_eval_flags(ctx, o, e->value, &val);
64  return !!(val & flag);
65  }
66  return 0;
67 }
68 
70  OutputStream *ost, const AVCodec **enc)
71 {
72  enum AVMediaType type = ost->type;
73  const char *codec_name = NULL;
74 
75  *enc = NULL;
76 
77  opt_match_per_stream_str(ost, &o->codec_names, s, ost->st, &codec_name);
78 
79  if (type != AVMEDIA_TYPE_VIDEO &&
82  if (codec_name && strcmp(codec_name, "copy")) {
83  const char *type_str = av_get_media_type_string(type);
85  "Encoder '%s' specified, but only '-codec copy' supported "
86  "for %s streams\n", codec_name, type_str);
87  return AVERROR(ENOSYS);
88  }
89  return 0;
90  }
91 
92  if (!codec_name) {
93  ost->par_in->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->type);
94  *enc = avcodec_find_encoder(ost->par_in->codec_id);
95  if (!*enc) {
96  av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
97  "Default encoder for format %s (codec %s) is "
98  "probably disabled. Please choose an encoder manually.\n",
99  s->oformat->name, avcodec_get_name(ost->par_in->codec_id));
101  }
102  } else if (strcmp(codec_name, "copy")) {
103  int ret = find_codec(ost, codec_name, ost->type, 1, enc);
104  if (ret < 0)
105  return ret;
106  ost->par_in->codec_id = (*enc)->id;
107  }
108 
109  return 0;
110 }
111 
112 static char *get_line(AVIOContext *s, AVBPrint *bprint)
113 {
114  char c;
115 
116  while ((c = avio_r8(s)) && c != '\n')
117  av_bprint_chars(bprint, c, 1);
118 
119  if (!av_bprint_is_complete(bprint))
120  return NULL;
121 
122  return bprint->str;
123 }
124 
125 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
126 {
127  int i, ret = -1;
128  char filename[1000];
129  char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR");
130  char *env_home = getenv_utf8("HOME");
131  const char *base[3] = { env_avconv_datadir,
132  env_home,
133  AVCONV_DATADIR,
134  };
135 
136  for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
137  if (!base[i])
138  continue;
139  if (codec_name) {
140  snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
141  i != 1 ? "" : "/.avconv", codec_name, preset_name);
142  ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
143  }
144  if (ret < 0) {
145  snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
146  i != 1 ? "" : "/.avconv", preset_name);
147  ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
148  }
149  }
150  freeenv_utf8(env_home);
151  freeenv_utf8(env_avconv_datadir);
152  return ret;
153 }
154 
155 typedef struct EncStatsFile {
156  char *path;
158 } EncStatsFile;
159 
162 
163 static int enc_stats_get_file(AVIOContext **io, const char *path)
164 {
165  EncStatsFile *esf;
166  int ret;
167 
168  for (int i = 0; i < nb_enc_stats_files; i++)
169  if (!strcmp(path, enc_stats_files[i].path)) {
170  *io = enc_stats_files[i].io;
171  return 0;
172  }
173 
175  if (ret < 0)
176  return ret;
177 
179 
180  ret = avio_open2(&esf->io, path, AVIO_FLAG_WRITE, &int_cb, NULL);
181  if (ret < 0) {
182  av_log(NULL, AV_LOG_ERROR, "Error opening stats file '%s': %s\n",
183  path, av_err2str(ret));
184  return ret;
185  }
186 
187  esf->path = av_strdup(path);
188  if (!esf->path)
189  return AVERROR(ENOMEM);
190 
191  *io = esf->io;
192 
193  return 0;
194 }
195 
197 {
198  for (int i = 0; i < nb_enc_stats_files; i++) {
199  av_freep(&enc_stats_files[i].path);
201  }
203  nb_enc_stats_files = 0;
204 }
205 
206 static int unescape(char **pdst, size_t *dst_len,
207  const char **pstr, char delim)
208 {
209  const char *str = *pstr;
210  char *dst;
211  size_t len, idx;
212 
213  *pdst = NULL;
214 
215  len = strlen(str);
216  if (!len)
217  return 0;
218 
219  dst = av_malloc(len + 1);
220  if (!dst)
221  return AVERROR(ENOMEM);
222 
223  for (idx = 0; *str; idx++, str++) {
224  if (str[0] == '\\' && str[1])
225  str++;
226  else if (*str == delim)
227  break;
228 
229  dst[idx] = *str;
230  }
231  if (!idx) {
232  av_freep(&dst);
233  return 0;
234  }
235 
236  dst[idx] = 0;
237 
238  *pdst = dst;
239  *dst_len = idx;
240  *pstr = str;
241 
242  return 0;
243 }
244 
245 static int enc_stats_init(OutputStream *ost, EncStats *es, int pre,
246  const char *path, const char *fmt_spec)
247 {
248  static const struct {
249  enum EncStatsType type;
250  const char *str;
251  unsigned pre_only:1;
252  unsigned post_only:1;
253  unsigned need_input_data:1;
254  } fmt_specs[] = {
255  { ENC_STATS_FILE_IDX, "fidx" },
256  { ENC_STATS_STREAM_IDX, "sidx" },
257  { ENC_STATS_FRAME_NUM, "n" },
258  { ENC_STATS_FRAME_NUM_IN, "ni", 0, 0, 1 },
259  { ENC_STATS_TIMEBASE, "tb" },
260  { ENC_STATS_TIMEBASE_IN, "tbi", 0, 0, 1 },
261  { ENC_STATS_PTS, "pts" },
262  { ENC_STATS_PTS_TIME, "t" },
263  { ENC_STATS_PTS_IN, "ptsi", 0, 0, 1 },
264  { ENC_STATS_PTS_TIME_IN, "ti", 0, 0, 1 },
265  { ENC_STATS_DTS, "dts", 0, 1 },
266  { ENC_STATS_DTS_TIME, "dt", 0, 1 },
267  { ENC_STATS_SAMPLE_NUM, "sn", 1 },
268  { ENC_STATS_NB_SAMPLES, "samp", 1 },
269  { ENC_STATS_PKT_SIZE, "size", 0, 1 },
270  { ENC_STATS_BITRATE, "br", 0, 1 },
271  { ENC_STATS_AVG_BITRATE, "abr", 0, 1 },
272  { ENC_STATS_KEYFRAME, "key", 0, 1 },
273  };
274  const char *next = fmt_spec;
275 
276  int ret;
277 
278  while (*next) {
280  char *val;
281  size_t val_len;
282 
283  // get the sequence up until next opening brace
284  ret = unescape(&val, &val_len, &next, '{');
285  if (ret < 0)
286  return ret;
287 
288  if (val) {
290  if (ret < 0) {
291  av_freep(&val);
292  return ret;
293  }
294 
295  c = &es->components[es->nb_components - 1];
296  c->type = ENC_STATS_LITERAL;
297  c->str = val;
298  c->str_len = val_len;
299  }
300 
301  if (!*next)
302  break;
303  next++;
304 
305  // get the part inside braces
306  ret = unescape(&val, &val_len, &next, '}');
307  if (ret < 0)
308  return ret;
309 
310  if (!val) {
312  "Empty formatting directive in: %s\n", fmt_spec);
313  return AVERROR(EINVAL);
314  }
315 
316  if (!*next) {
318  "Missing closing brace in: %s\n", fmt_spec);
319  ret = AVERROR(EINVAL);
320  goto fail;
321  }
322  next++;
323 
325  if (ret < 0)
326  goto fail;
327 
328  c = &es->components[es->nb_components - 1];
329 
330  for (size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) {
331  if (!strcmp(val, fmt_specs[i].str)) {
332  if ((pre && fmt_specs[i].post_only) || (!pre && fmt_specs[i].pre_only)) {
334  "Format directive '%s' may only be used %s-encoding\n",
335  val, pre ? "post" : "pre");
336  ret = AVERROR(EINVAL);
337  goto fail;
338  }
339 
340  c->type = fmt_specs[i].type;
341 
342  if (fmt_specs[i].need_input_data && !ost->ist) {
344  "Format directive '%s' is unavailable, because "
345  "this output stream has no associated input stream\n",
346  val);
347  }
348 
349  break;
350  }
351  }
352 
353  if (!c->type) {
354  av_log(NULL, AV_LOG_ERROR, "Invalid format directive: %s\n", val);
355  ret = AVERROR(EINVAL);
356  goto fail;
357  }
358 
359 fail:
360  av_freep(&val);
361  if (ret < 0)
362  return ret;
363  }
364 
365  ret = pthread_mutex_init(&es->lock, NULL);
366  if (ret)
367  return AVERROR(ret);
368  es->lock_initialized = 1;
369 
370  ret = enc_stats_get_file(&es->io, path);
371  if (ret < 0)
372  return ret;
373 
374  return 0;
375 }
376 
377 static const char *output_stream_item_name(void *obj)
378 {
379  const MuxStream *ms = obj;
380 
381  return ms->log_name;
382 }
383 
384 static const AVClass output_stream_class = {
385  .class_name = "OutputStream",
386  .version = LIBAVUTIL_VERSION_INT,
387  .item_name = output_stream_item_name,
388  .category = AV_CLASS_CATEGORY_MUXER,
389 };
390 
392 {
393  const char *type_str = av_get_media_type_string(type);
394  MuxStream *ms;
395 
396  ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams);
397  if (!ms)
398  return NULL;
399 
400  ms->ost.file = &mux->of;
401  ms->ost.index = mux->of.nb_streams - 1;
402  ms->ost.type = type;
403 
405 
406  ms->sch_idx = -1;
407  ms->sch_idx_enc = -1;
408 
409  snprintf(ms->log_name, sizeof(ms->log_name), "%cost#%d:%d",
410  type_str ? *type_str : '?', mux->of.index, ms->ost.index);
411 
412  return ms;
413 }
414 
416  OutputStream *ost, char **dst)
417 {
418  const char *filters = NULL;
419 #if FFMPEG_OPT_FILTER_SCRIPT
420  const char *filters_script = NULL;
421 
422  opt_match_per_stream_str(ost, &o->filter_scripts, oc, ost->st, &filters_script);
423 #endif
425 
426  if (!ost->enc) {
427  if (
429  filters_script ||
430 #endif
431  filters) {
433  "%s '%s' was specified, but codec copy was selected. "
434  "Filtering and streamcopy cannot be used together.\n",
436  filters ? "Filtergraph" : "Filtergraph script",
437  filters ? filters : filters_script
438 #else
439  "Filtergraph", filters
440 #endif
441  );
442  return AVERROR(ENOSYS);
443  }
444  return 0;
445  }
446 
447  if (!ost->ist) {
448  if (
450  filters_script ||
451 #endif
452  filters) {
454  "%s '%s' was specified for a stream fed from a complex "
455  "filtergraph. Simple and complex filtering cannot be used "
456  "together for the same stream.\n",
458  filters ? "Filtergraph" : "Filtergraph script",
459  filters ? filters : filters_script
460 #else
461  "Filtergraph", filters
462 #endif
463  );
464  return AVERROR(EINVAL);
465  }
466  return 0;
467  }
468 
469 #if FFMPEG_OPT_FILTER_SCRIPT
470  if (filters_script && filters) {
471  av_log(ost, AV_LOG_ERROR, "Both -filter and -filter_script set\n");
472  return AVERROR(EINVAL);
473  }
474 
475  if (filters_script)
476  *dst = file_read(filters_script);
477  else
478 #endif
479  if (filters)
480  *dst = av_strdup(filters);
481  else
482  *dst = av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull");
483  return *dst ? 0 : AVERROR(ENOMEM);
484 }
485 
486 static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str)
487 {
488  const char *p = str;
489  for (int i = 0;; i++) {
490  dest[i] = atoi(p);
491  if (i == 63)
492  break;
493  p = strchr(p, ',');
494  if (!p) {
495  av_log(logctx, AV_LOG_FATAL,
496  "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
497  return AVERROR(EINVAL);
498  }
499  p++;
500  }
501 
502  return 0;
503 }
504 
505 static int fmt_in_list(const int *formats, int format)
506 {
507  for (; *formats != -1; formats++)
508  if (*formats == format)
509  return 1;
510  return 0;
511 }
512 
513 static enum AVPixelFormat
515 {
516  const enum AVPixelFormat *p;
518  //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
519  int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
520  enum AVPixelFormat best= AV_PIX_FMT_NONE;
521  int ret;
522 
524  0, (const void **) &p, NULL);
525  if (ret < 0)
526  return AV_PIX_FMT_NONE;
527 
528  for (; *p != AV_PIX_FMT_NONE; p++) {
529  best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
530  if (*p == target)
531  break;
532  }
533  if (*p == AV_PIX_FMT_NONE) {
534  if (target != AV_PIX_FMT_NONE)
536  "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
537  av_get_pix_fmt_name(target),
538  avctx->codec->name,
539  av_get_pix_fmt_name(best));
540  return best;
541  }
542  return target;
543 }
544 
545 static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name)
546 {
547  const enum AVPixelFormat *fmts;
548  enum AVPixelFormat fmt;
549  int ret;
550 
551  fmt = av_get_pix_fmt(name);
552  if (fmt == AV_PIX_FMT_NONE) {
553  av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", name);
554  return AV_PIX_FMT_NONE;
555  }
556 
558  0, (const void **) &fmts, NULL);
559  if (ret < 0)
560  return AV_PIX_FMT_NONE;
561 
562  /* when the user specified-format is an alias for an endianness-specific
563  * one (e.g. rgb48 -> rgb48be/le), it gets translated into the native
564  * endianness by av_get_pix_fmt();
565  * the following code handles the case when the native endianness is not
566  * supported by the encoder, but the other one is */
567  if (fmts && !fmt_in_list(fmts, fmt)) {
568  const char *name_canonical = av_get_pix_fmt_name(fmt);
569  int len = strlen(name_canonical);
570 
571  if (strcmp(name, name_canonical) &&
572  (!strcmp(name_canonical + len - 2, "le") ||
573  !strcmp(name_canonical + len - 2, "be"))) {
574  char name_other[64];
575  enum AVPixelFormat fmt_other;
576 
577  snprintf(name_other, sizeof(name_other), "%s%ce",
578  name, name_canonical[len - 2] == 'l' ? 'b' : 'l');
579  fmt_other = av_get_pix_fmt(name_other);
580  if (fmt_other != AV_PIX_FMT_NONE && fmt_in_list(fmts, fmt_other)) {
581  av_log(ost, AV_LOG_VERBOSE, "Mapping pixel format %s->%s\n",
582  name, name_other);
583  fmt = fmt_other;
584  }
585  }
586  }
587 
588  if (fmts && !fmt_in_list(fmts, fmt))
589  fmt = choose_pixel_fmt(ost->enc_ctx, fmt);
590 
591  return fmt;
592 }
593 
594 static int new_stream_video(Muxer *mux, const OptionsContext *o,
595  OutputStream *ost, int *keep_pix_fmt,
596  enum VideoSyncMethod *vsync_method)
597 {
598  MuxStream *ms = ms_from_ost(ost);
599  AVFormatContext *oc = mux->fc;
600  AVStream *st;
601  const char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
602  int ret = 0;
603 
604  st = ost->st;
605 
606  opt_match_per_stream_str(ost, &o->frame_rates, oc, st, &frame_rate);
607  if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
608  av_log(ost, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
609  return AVERROR(EINVAL);
610  }
611 
612  opt_match_per_stream_str(ost, &o->max_frame_rates, oc, st, &max_frame_rate);
613  if (max_frame_rate && av_parse_video_rate(&ost->max_frame_rate, max_frame_rate) < 0) {
614  av_log(ost, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
615  return AVERROR(EINVAL);
616  }
617 
618  if (frame_rate && max_frame_rate) {
619  av_log(ost, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
620  return AVERROR(EINVAL);
621  }
622 
623  opt_match_per_stream_str(ost, &o->frame_aspect_ratios, oc, st, &frame_aspect_ratio);
624  if (frame_aspect_ratio) {
625  AVRational q;
626  if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
627  q.num <= 0 || q.den <= 0) {
628  av_log(ost, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
629  return AVERROR(EINVAL);
630  }
631  ost->frame_aspect_ratio = q;
632  }
633 
634  if (ost->enc_ctx) {
635  AVCodecContext *video_enc = ost->enc_ctx;
636  const char *p = NULL, *fps_mode = NULL;
637  const char *frame_size = NULL;
638  const char *frame_pix_fmt = NULL;
639  const char *intra_matrix = NULL, *inter_matrix = NULL;
640  const char *chroma_intra_matrix = NULL;
641  int do_pass = 0;
642  int i;
643 
645  if (frame_size) {
646  ret = av_parse_video_size(&video_enc->width, &video_enc->height, frame_size);
647  if (ret < 0) {
648  av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
649  return AVERROR(EINVAL);
650  }
651  }
652 
653  opt_match_per_stream_str(ost, &o->frame_pix_fmts, oc, st, &frame_pix_fmt);
654  if (frame_pix_fmt && *frame_pix_fmt == '+') {
655  *keep_pix_fmt = 1;
656  if (!*++frame_pix_fmt)
657  frame_pix_fmt = NULL;
658  }
659  if (frame_pix_fmt) {
660  video_enc->pix_fmt = pix_fmt_parse(ost, frame_pix_fmt);
661  if (video_enc->pix_fmt == AV_PIX_FMT_NONE)
662  return AVERROR(EINVAL);
663  }
664 
665  opt_match_per_stream_str(ost, &o->intra_matrices, oc, st, &intra_matrix);
666  if (intra_matrix) {
667  if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64)))
668  return AVERROR(ENOMEM);
669 
670  ret = parse_matrix_coeffs(ost, video_enc->intra_matrix, intra_matrix);
671  if (ret < 0)
672  return ret;
673  }
674  opt_match_per_stream_str(ost, &o->chroma_intra_matrices, oc, st, &chroma_intra_matrix);
675  if (chroma_intra_matrix) {
676  if (!(video_enc->chroma_intra_matrix = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64)))
677  return AVERROR(ENOMEM);
678  ret = parse_matrix_coeffs(ost, video_enc->chroma_intra_matrix, chroma_intra_matrix);
679  if (ret < 0)
680  return ret;
681  }
682  opt_match_per_stream_str(ost, &o->inter_matrices, oc, st, &inter_matrix);
683  if (inter_matrix) {
684  if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64)))
685  return AVERROR(ENOMEM);
686  ret = parse_matrix_coeffs(ost, video_enc->inter_matrix, inter_matrix);
687  if (ret < 0)
688  return ret;
689  }
690 
691  opt_match_per_stream_str(ost, &o->rc_overrides, oc, st, &p);
692  for (i = 0; p; i++) {
693  int start, end, q;
694  int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
695  if (e != 3) {
696  av_log(ost, AV_LOG_FATAL, "error parsing rc_override\n");
697  return AVERROR(EINVAL);
698  }
699  video_enc->rc_override =
700  av_realloc_array(video_enc->rc_override,
701  i + 1, sizeof(RcOverride));
702  if (!video_enc->rc_override) {
703  av_log(ost, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
704  return AVERROR(ENOMEM);
705  }
706  video_enc->rc_override[i].start_frame = start;
707  video_enc->rc_override[i].end_frame = end;
708  if (q > 0) {
709  video_enc->rc_override[i].qscale = q;
710  video_enc->rc_override[i].quality_factor = 1.0;
711  }
712  else {
713  video_enc->rc_override[i].qscale = 0;
714  video_enc->rc_override[i].quality_factor = -q/100.0;
715  }
716  p = strchr(p, '/');
717  if (p) p++;
718  }
719  video_enc->rc_override_count = i;
720 
721  /* two pass mode */
722  opt_match_per_stream_int(ost, &o->pass, oc, st, &do_pass);
723  if (do_pass) {
724  if (do_pass & 1)
725  video_enc->flags |= AV_CODEC_FLAG_PASS1;
726  if (do_pass & 2)
727  video_enc->flags |= AV_CODEC_FLAG_PASS2;
728  }
729 
730  opt_match_per_stream_str(ost, &o->passlogfiles, oc, st, &ost->logfile_prefix);
731  if (ost->logfile_prefix &&
732  !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
733  return AVERROR(ENOMEM);
734 
735  if (do_pass) {
736  int ost_idx = -1;
737  char logfilename[1024];
738  FILE *f;
739 
740  /* compute this stream's global index */
741  for (int idx = 0; idx <= ost->file->index; idx++)
742  ost_idx += output_files[idx]->nb_streams;
743 
744  snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
745  ost->logfile_prefix ? ost->logfile_prefix :
746  DEFAULT_PASS_LOGFILENAME_PREFIX,
747  ost_idx);
748  if (!strcmp(ost->enc_ctx->codec->name, "libx264") || !strcmp(ost->enc_ctx->codec->name, "libvvenc")) {
749  if (av_opt_is_set_to_default_by_name(ost->enc_ctx, "stats",
751  av_opt_set(ost->enc_ctx, "stats", logfilename,
753  } else {
754  if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
755  char *logbuffer = file_read(logfilename);
756 
757  if (!logbuffer) {
758  av_log(ost, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
759  logfilename);
760  return AVERROR(EIO);
761  }
762  video_enc->stats_in = logbuffer;
763  }
764  if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
765  f = fopen_utf8(logfilename, "wb");
766  if (!f) {
768  "Cannot write log file '%s' for pass-1 encoding: %s\n",
769  logfilename, strerror(errno));
770  return AVERROR(errno);
771  }
772  ost->logfile = f;
773  }
774  }
775  }
776 
777  opt_match_per_stream_int(ost, &o->force_fps, oc, st, &ost->force_fps);
778 
779 #if FFMPEG_OPT_TOP
780  ost->top_field_first = -1;
781  opt_match_per_stream_int(ost, &o->top_field_first, oc, st, &ost->top_field_first);
782  if (ost->top_field_first >= 0)
783  av_log(ost, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
784 #endif
785 
786 #if FFMPEG_OPT_VSYNC
787  *vsync_method = video_sync_method;
788 #else
789  *vsync_method = VSYNC_AUTO;
790 #endif
791  opt_match_per_stream_str(ost, &o->fps_mode, oc, st, &fps_mode);
792  if (fps_mode) {
793  ret = parse_and_set_vsync(fps_mode, vsync_method, ost->file->index, ost->index, 0);
794  if (ret < 0)
795  return ret;
796  }
797 
798  if ((ost->frame_rate.num || ost->max_frame_rate.num) &&
799  !(*vsync_method == VSYNC_AUTO ||
800  *vsync_method == VSYNC_CFR || *vsync_method == VSYNC_VSCFR)) {
801  av_log(ost, AV_LOG_FATAL, "One of -r/-fpsmax was specified "
802  "together a non-CFR -vsync/-fps_mode. This is contradictory.\n");
803  return AVERROR(EINVAL);
804  }
805 
806  if (*vsync_method == VSYNC_AUTO) {
807  if (ost->frame_rate.num || ost->max_frame_rate.num) {
808  *vsync_method = VSYNC_CFR;
809  } else if (!strcmp(oc->oformat->name, "avi")) {
810  *vsync_method = VSYNC_VFR;
811  } else {
812  *vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
813  ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
815  }
816 
817  if (ost->ist && *vsync_method == VSYNC_CFR) {
818  const InputFile *ifile = ost->ist->file;
819 
820  if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0)
821  *vsync_method = VSYNC_VSCFR;
822  }
823 
824  if (*vsync_method == VSYNC_CFR && copy_ts) {
825  *vsync_method = VSYNC_VSCFR;
826  }
827  }
828 #if FFMPEG_OPT_VSYNC_DROP
829  if (*vsync_method == VSYNC_DROP)
830  ms->ts_drop = 1;
831 #endif
832  }
833 
834  return 0;
835 }
836 
837 static int new_stream_audio(Muxer *mux, const OptionsContext *o,
838  OutputStream *ost)
839 {
840  MuxStream *ms = ms_from_ost(ost);
841  AVFormatContext *oc = mux->fc;
842  AVStream *st = ost->st;
843 
844  if (ost->enc_ctx) {
845  AVCodecContext *audio_enc = ost->enc_ctx;
846  int channels = 0;
847  const char *layout = NULL;
848  const char *sample_fmt = NULL;
849 
851  if (channels) {
853  audio_enc->ch_layout.nb_channels = channels;
854  }
855 
857  if (layout && av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
858  av_log(ost, AV_LOG_FATAL, "Unknown channel layout: %s\n", layout);
859  return AVERROR(EINVAL);
860  }
861 
862  opt_match_per_stream_str(ost, &o->sample_fmts, oc, st, &sample_fmt);
863  if (sample_fmt &&
864  (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
865  av_log(ost, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
866  return AVERROR(EINVAL);
867  }
868 
869  opt_match_per_stream_int(ost, &o->audio_sample_rate, oc, st, &audio_enc->sample_rate);
870  opt_match_per_stream_str(ost, &o->apad, oc, st, &ms->apad);
871  }
872 
873  return 0;
874 }
875 
876 static int new_stream_subtitle(Muxer *mux, const OptionsContext *o,
877  OutputStream *ost)
878 {
879  AVStream *st;
880 
881  st = ost->st;
882 
883  if (ost->enc_ctx) {
884  AVCodecContext *subtitle_enc = ost->enc_ctx;
885 
886  AVCodecDescriptor const *input_descriptor =
887  avcodec_descriptor_get(ost->ist->par->codec_id);
888  AVCodecDescriptor const *output_descriptor =
889  avcodec_descriptor_get(subtitle_enc->codec_id);
890  int input_props = 0, output_props = 0;
891 
892  const char *frame_size = NULL;
893 
895  if (frame_size) {
896  int ret = av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size);
897  if (ret < 0) {
898  av_log(ost, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
899  return ret;
900  }
901  }
902  if (input_descriptor)
903  input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
904  if (output_descriptor)
905  output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
906  if (input_props && output_props && input_props != output_props) {
908  "Subtitle encoding currently only possible from text to text "
909  "or bitmap to bitmap\n");
910  return AVERROR(EINVAL);
911  }
912  }
913 
914  return 0;
915 }
916 
917 static int
918 ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter,
919  const OptionsContext *o, char *filters,
920  AVRational enc_tb, enum VideoSyncMethod vsync_method,
921  int keep_pix_fmt, int autoscale, int threads_manual,
922  const ViewSpecifier *vs)
923 {
924  OutputStream *ost = &ms->ost;
925  AVCodecContext *enc_ctx = ost->enc_ctx;
926  char name[16];
927  int ret;
928 
930  .enc = enc_ctx->codec,
931  .name = name,
932  .format = (ost->type == AVMEDIA_TYPE_VIDEO) ?
933  enc_ctx->pix_fmt : enc_ctx->sample_fmt,
934  .width = enc_ctx->width,
935  .height = enc_ctx->height,
936  .color_space = enc_ctx->colorspace,
937  .color_range = enc_ctx->color_range,
938  .vsync_method = vsync_method,
939  .sample_rate = enc_ctx->sample_rate,
940  .ch_layout = enc_ctx->ch_layout,
941  .sws_opts = o->g->sws_dict,
942  .swr_opts = o->g->swr_opts,
943  .output_tb = enc_tb,
944  .trim_start_us = mux->of.start_time,
945  .trim_duration_us = mux->of.recording_time,
946  .ts_offset = mux->of.start_time == AV_NOPTS_VALUE ?
947  0 : mux->of.start_time,
948  .vs = vs,
949 
950  .flags = OFILTER_FLAG_DISABLE_CONVERT * !!keep_pix_fmt |
951  OFILTER_FLAG_AUTOSCALE * !!autoscale |
953  };
954 
955  snprintf(name, sizeof(name), "#%d:%d", mux->of.index, ost->index);
956 
957  if (ost->type == AVMEDIA_TYPE_VIDEO) {
958  if (!keep_pix_fmt) {
961  (const void **) &opts.formats, NULL);
962  if (ret < 0)
963  return ret;
964  }
965  if (!ost->force_fps) {
968  (const void **) &opts.frame_rates, NULL);
969  if (ret < 0)
970  return ret;
971  }
974  (const void **) &opts.color_spaces, NULL);
975  if (ret < 0)
976  return ret;
979  (const void **) &opts.color_ranges, NULL);
980  if (ret < 0)
981  return ret;
982  } else {
985  (const void **) &opts.formats, NULL);
986  if (ret < 0)
987  return ret;
990  (const void **) &opts.sample_rates, NULL);
991  if (ret < 0)
992  return ret;
995  (const void **) &opts.ch_layouts, NULL);
996  if (ret < 0)
997  return ret;
998  }
999 
1000  if (threads_manual) {
1001  ret = av_opt_get(enc_ctx, "threads", 0, (uint8_t**)&opts.nb_threads);
1002  if (ret < 0)
1003  return ret;
1004  }
1005 
1006  if (ofilter) {
1007  ost->filter = ofilter;
1008  ret = ofilter_bind_ost(ofilter, ost, ms->sch_idx_enc, &opts);
1009  } else {
1011  mux->sch, ms->sch_idx_enc, &opts);
1012  }
1013  av_freep(&opts.nb_threads);
1014  if (ret < 0)
1015  return ret;
1016 
1017  ret = sch_connect(mux->sch, SCH_ENC(ms->sch_idx_enc),
1018  SCH_MSTREAM(mux->sch_idx, ms->sch_idx));
1019  if (ret < 0)
1020  return ret;
1021 
1022  return ret;
1023 }
1024 
1025 static int streamcopy_init(const Muxer *mux, OutputStream *ost, AVDictionary **encoder_opts)
1026 {
1027  MuxStream *ms = ms_from_ost(ost);
1028 
1029  const InputStream *ist = ost->ist;
1030  const InputFile *ifile = ist->file;
1031 
1032  AVCodecParameters *par = ost->par_in;
1033  uint32_t codec_tag = par->codec_tag;
1034 
1035  AVCodecContext *codec_ctx = NULL;
1036 
1037  AVRational fr = ost->frame_rate;
1038 
1039  int ret = 0;
1040 
1041  codec_ctx = avcodec_alloc_context3(NULL);
1042  if (!codec_ctx)
1043  return AVERROR(ENOMEM);
1044 
1045  ret = avcodec_parameters_to_context(codec_ctx, ist->par);
1046  if (ret >= 0)
1047  ret = av_opt_set_dict(codec_ctx, encoder_opts);
1048  if (ret < 0) {
1050  "Error setting up codec context options.\n");
1051  goto fail;
1052  }
1053 
1054  ret = avcodec_parameters_from_context(par, codec_ctx);
1055  if (ret < 0) {
1057  "Error getting reference codec parameters.\n");
1058  goto fail;
1059  }
1060 
1061  if (!codec_tag) {
1062  const struct AVCodecTag * const *ct = mux->fc->oformat->codec_tag;
1063  unsigned int codec_tag_tmp;
1064  if (!ct || av_codec_get_id (ct, par->codec_tag) == par->codec_id ||
1065  !av_codec_get_tag2(ct, par->codec_id, &codec_tag_tmp))
1066  codec_tag = par->codec_tag;
1067  }
1068 
1069  par->codec_tag = codec_tag;
1070 
1071  if (!fr.num)
1072  fr = ist->framerate;
1073 
1074  if (fr.num)
1075  ost->st->avg_frame_rate = fr;
1076  else
1077  ost->st->avg_frame_rate = ist->st->avg_frame_rate;
1078 
1079  // copy timebase while removing common factors
1080  if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
1081  if (fr.num)
1082  ost->st->time_base = av_inv_q(fr);
1083  else
1084  ost->st->time_base = av_add_q(ist->st->time_base, (AVRational){0, 1});
1085  }
1086 
1087  if (!ms->copy_prior_start) {
1088  ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ?
1089  0 : mux->of.start_time;
1090  if (copy_ts && ifile->start_time != AV_NOPTS_VALUE) {
1091  ms->ts_copy_start = FFMAX(ms->ts_copy_start,
1092  ifile->start_time + ifile->ts_offset);
1093  }
1094  }
1095 
1096  for (int i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
1097  const AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
1098  AVPacketSideData *sd_dst;
1099 
1102  sd_src->type, sd_src->size, 0);
1103  if (!sd_dst) {
1104  ret = AVERROR(ENOMEM);
1105  goto fail;
1106  }
1107  memcpy(sd_dst->data, sd_src->data, sd_src->size);
1108  }
1109 
1110  switch (par->codec_type) {
1111  case AVMEDIA_TYPE_AUDIO:
1112  if ((par->block_align == 1 || par->block_align == 1152 || par->block_align == 576) &&
1113  par->codec_id == AV_CODEC_ID_MP3)
1114  par->block_align = 0;
1115  if (par->codec_id == AV_CODEC_ID_AC3)
1116  par->block_align = 0;
1117  break;
1118  case AVMEDIA_TYPE_VIDEO: {
1119  AVRational sar;
1120  if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
1121  sar =
1122  av_mul_q(ost->frame_aspect_ratio,
1123  (AVRational){ par->height, par->width });
1124  av_log(ost, AV_LOG_WARNING, "Overriding aspect ratio "
1125  "with stream copy may produce invalid files\n");
1126  }
1127  else if (ist->st->sample_aspect_ratio.num)
1128  sar = ist->st->sample_aspect_ratio;
1129  else
1130  sar = par->sample_aspect_ratio;
1131  ost->st->sample_aspect_ratio = par->sample_aspect_ratio = sar;
1132  ost->st->r_frame_rate = ist->st->r_frame_rate;
1133  break;
1134  }
1135  }
1136 
1137 fail:
1138  avcodec_free_context(&codec_ctx);
1139  return ret;
1140 }
1141 
1142 static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
1143  InputStream *ist, OutputFilter *ofilter, const ViewSpecifier *vs,
1144  OutputStream **post)
1145 {
1146  AVFormatContext *oc = mux->fc;
1147  MuxStream *ms;
1148  OutputStream *ost;
1149  const AVCodec *enc;
1150  AVStream *st;
1151  AVDictionary *encoder_opts = NULL;
1152  int ret = 0, keep_pix_fmt = 0, autoscale = 1;
1153  int threads_manual = 0;
1154  AVRational enc_tb = { 0, 0 };
1155  enum VideoSyncMethod vsync_method = VSYNC_AUTO;
1156  const char *bsfs = NULL, *time_base = NULL, *codec_tag = NULL;
1157  char *filters = NULL, *next;
1158  double qscale = -1;
1159 
1160  st = avformat_new_stream(oc, NULL);
1161  if (!st)
1162  return AVERROR(ENOMEM);
1163 
1164  ms = mux_stream_alloc(mux, type);
1165  if (!ms)
1166  return AVERROR(ENOMEM);
1167 
1168  // only streams with sources (i.e. not attachments)
1169  // are handled by the scheduler
1170  if (ist || ofilter) {
1172  if (ret < 0)
1173  return ret;
1174 
1175  ret = sch_add_mux_stream(mux->sch, mux->sch_idx);
1176  if (ret < 0)
1177  return ret;
1178 
1179  av_assert0(ret == mux->nb_sch_stream_idx - 1);
1180  mux->sch_stream_idx[ret] = ms->ost.index;
1181  ms->sch_idx = ret;
1182  }
1183 
1184  ost = &ms->ost;
1185 
1186  if (o->streamid) {
1187  AVDictionaryEntry *e;
1188  char idx[16], *p;
1189  snprintf(idx, sizeof(idx), "%d", ost->index);
1190 
1191  e = av_dict_get(o->streamid, idx, NULL, 0);
1192  if (e) {
1193  st->id = strtol(e->value, &p, 0);
1194  if (!e->value[0] || *p) {
1195  av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value);
1196  return AVERROR(EINVAL);
1197  }
1198  }
1199  }
1200 
1201  ost->par_in = avcodec_parameters_alloc();
1202  if (!ost->par_in)
1203  return AVERROR(ENOMEM);
1204 
1206 
1207  ost->st = st;
1208  ost->ist = ist;
1209  ost->kf.ref_pts = AV_NOPTS_VALUE;
1210  ost->par_in->codec_type = type;
1211  st->codecpar->codec_type = type;
1212 
1213  ret = choose_encoder(o, oc, ost, &enc);
1214  if (ret < 0) {
1215  av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n");
1216  return ret;
1217  }
1218 
1219  if (enc) {
1220  ost->enc_ctx = avcodec_alloc_context3(enc);
1221  if (!ost->enc_ctx)
1222  return AVERROR(ENOMEM);
1223 
1225  ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open);
1226  if (ret < 0)
1227  return ret;
1228  ms->sch_idx_enc = ret;
1229 
1230  ret = enc_alloc(&ost->enc, enc, mux->sch, ms->sch_idx_enc);
1231  if (ret < 0)
1232  return ret;
1233 
1234  av_strlcat(ms->log_name, "/", sizeof(ms->log_name));
1235  av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name));
1236  } else {
1237  if (ofilter) {
1239  "Streamcopy requested for output stream fed "
1240  "from a complex filtergraph. Filtering and streamcopy "
1241  "cannot be used together.\n");
1242  return AVERROR(EINVAL);
1243  }
1244 
1245  av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name));
1246  }
1247 
1248  av_log(ost, AV_LOG_VERBOSE, "Created %s stream from ",
1250  if (ist)
1251  av_log(ost, AV_LOG_VERBOSE, "input stream %d:%d",
1252  ist->file->index, ist->index);
1253  else if (ofilter)
1254  av_log(ost, AV_LOG_VERBOSE, "complex filtergraph %d:[%s]\n",
1255  ofilter->graph->index, ofilter->name);
1256  else if (type == AVMEDIA_TYPE_ATTACHMENT)
1257  av_log(ost, AV_LOG_VERBOSE, "attached file");
1258  else av_assert0(0);
1259  av_log(ost, AV_LOG_VERBOSE, "\n");
1260 
1261  ms->pkt = av_packet_alloc();
1262  if (!ms->pkt)
1263  return AVERROR(ENOMEM);
1264 
1265  if (ost->enc_ctx) {
1266  AVIOContext *s = NULL;
1267  char *buf = NULL, *arg = NULL;
1268  const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
1269  const char *enc_time_base = NULL, *preset = NULL;
1270 
1271  ret = filter_codec_opts(o->g->codec_opts, ost->enc_ctx->codec_id,
1272  oc, st, ost->enc_ctx->codec, &encoder_opts,
1273  &mux->enc_opts_used);
1274  if (ret < 0)
1275  goto fail;
1276 
1277  opt_match_per_stream_str(ost, &o->presets, oc, st, &preset);
1278  opt_match_per_stream_int(ost, &o->autoscale, oc, st, &autoscale);
1279  if (preset && (!(ret = get_preset_file_2(preset, ost->enc_ctx->codec->name, &s)))) {
1280  AVBPrint bprint;
1282  do {
1283  av_bprint_clear(&bprint);
1284  buf = get_line(s, &bprint);
1285  if (!buf) {
1286  ret = AVERROR(ENOMEM);
1287  break;
1288  }
1289 
1290  if (!buf[0] || buf[0] == '#')
1291  continue;
1292  if (!(arg = strchr(buf, '='))) {
1293  av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
1294  ret = AVERROR(EINVAL);
1295  break;
1296  }
1297  *arg++ = 0;
1298  av_dict_set(&encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
1299  } while (!s->eof_reached);
1300  av_bprint_finalize(&bprint, NULL);
1301  avio_closep(&s);
1302  }
1303  if (ret) {
1305  "Preset %s specified, but could not be opened.\n", preset);
1306  goto fail;
1307  }
1308 
1309  opt_match_per_stream_str(ost, &o->enc_stats_pre, oc, st, &enc_stats_pre);
1310  if (enc_stats_pre &&
1312  const char *format = "{fidx} {sidx} {n} {t}";
1313 
1315 
1316  ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format);
1317  if (ret < 0)
1318  goto fail;
1319  }
1320 
1321  opt_match_per_stream_str(ost, &o->enc_stats_post, oc, st, &enc_stats_post);
1322  if (enc_stats_post &&
1324  const char *format = "{fidx} {sidx} {n} {t}";
1325 
1327 
1328  ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format);
1329  if (ret < 0)
1330  goto fail;
1331  }
1332 
1333  opt_match_per_stream_str(ost, &o->mux_stats, oc, st, &mux_stats);
1334  if (mux_stats &&
1336  const char *format = "{fidx} {sidx} {n} {t}";
1337 
1339 
1340  ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format);
1341  if (ret < 0)
1342  goto fail;
1343  }
1344 
1345  opt_match_per_stream_str(ost, &o->enc_time_bases, oc, st, &enc_time_base);
1346  if (enc_time_base && type == AVMEDIA_TYPE_SUBTITLE)
1348  "-enc_time_base not supported for subtitles, ignoring\n");
1349  else if (enc_time_base) {
1350  AVRational q;
1351 
1352  if (!strcmp(enc_time_base, "demux")) {
1353  q = (AVRational){ ENC_TIME_BASE_DEMUX, 0 };
1354  } else if (!strcmp(enc_time_base, "filter")) {
1355  q = (AVRational){ ENC_TIME_BASE_FILTER, 0 };
1356  } else {
1357  ret = av_parse_ratio(&q, enc_time_base, INT_MAX, 0, NULL);
1358  if (ret < 0 || q.den <= 0
1360  || q.num < 0
1361 #endif
1362  ) {
1363  av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", enc_time_base);
1364  ret = ret < 0 ? ret : AVERROR(EINVAL);
1365  goto fail;
1366  }
1367 #if FFMPEG_OPT_ENC_TIME_BASE_NUM
1368  if (q.num < 0)
1369  av_log(ost, AV_LOG_WARNING, "-enc_time_base -1 is deprecated,"
1370  " use -enc_time_base demux\n");
1371 #endif
1372  }
1373 
1374  enc_tb = q;
1375  }
1376 
1377  threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0);
1378 
1379  ret = av_opt_set_dict2(ost->enc_ctx, &encoder_opts, AV_OPT_SEARCH_CHILDREN);
1380  if (ret < 0) {
1381  av_log(ost, AV_LOG_ERROR, "Error applying encoder options: %s\n",
1382  av_err2str(ret));
1383  goto fail;
1384  }
1385 
1386  ret = check_avoptions(encoder_opts);
1387  if (ret < 0)
1388  goto fail;
1389 
1390  // default to automatic thread count
1391  if (!threads_manual)
1392  ost->enc_ctx->thread_count = 0;
1393  } else {
1395  NULL, &encoder_opts,
1396  &mux->enc_opts_used);
1397  if (ret < 0)
1398  goto fail;
1399  }
1400 
1401 
1402  if (o->bitexact) {
1403  ost->bitexact = 1;
1404  } else if (ost->enc_ctx) {
1405  ost->bitexact = !!(ost->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT);
1406  }
1407 
1408  opt_match_per_stream_str(ost, &o->time_bases, oc, st, &time_base);
1409  if (time_base) {
1410  AVRational q;
1411  if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1412  q.num <= 0 || q.den <= 0) {
1413  av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
1414  ret = AVERROR(EINVAL);
1415  goto fail;
1416  }
1417  st->time_base = q;
1418  }
1419 
1420  ms->max_frames = INT64_MAX;
1422  for (int i = 0; i < o->max_frames.nb_opt; i++) {
1423  char *p = o->max_frames.opt[i].specifier;
1424  if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1425  av_log(ost, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1426  break;
1427  }
1428  }
1429 
1430  ms->copy_prior_start = -1;
1432  opt_match_per_stream_str(ost, &o->bitstream_filters, oc, st, &bsfs);
1433  if (bsfs && *bsfs) {
1434  ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx);
1435  if (ret < 0) {
1436  av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1437  goto fail;
1438  }
1439  }
1440 
1441  opt_match_per_stream_str(ost, &o->codec_tags, oc, st, &codec_tag);
1442  if (codec_tag) {
1443  uint32_t tag = strtol(codec_tag, &next, 0);
1444  if (*next) {
1445  uint8_t buf[4] = { 0 };
1446  memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
1447  tag = AV_RL32(buf);
1448  }
1449  ost->st->codecpar->codec_tag = tag;
1450  ost->par_in->codec_tag = tag;
1451  if (ost->enc_ctx)
1452  ost->enc_ctx->codec_tag = tag;
1453  }
1454 
1455  opt_match_per_stream_dbl(ost, &o->qscale, oc, st, &qscale);
1456  if (ost->enc_ctx && qscale >= 0) {
1457  ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1458  ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1459  }
1460 
1461  if (ms->sch_idx >= 0) {
1462  int max_muxing_queue_size = 128;
1463  int muxing_queue_data_threshold = 50 * 1024 * 1024;
1464 
1466  &max_muxing_queue_size);
1468  oc, st, &muxing_queue_data_threshold);
1469 
1470  sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx,
1471  max_muxing_queue_size, muxing_queue_data_threshold);
1472  }
1473 
1475  &ost->bits_per_raw_sample);
1476 
1478  oc, st, &ost->fix_sub_duration_heartbeat);
1479 
1480  if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx)
1481  ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1482 
1484  oc, st, &ms->copy_initial_nonkeyframes);
1485  switch (type) {
1486  case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost, &keep_pix_fmt, &vsync_method); break;
1487  case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break;
1488  case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break;
1489  }
1490  if (ret < 0)
1491  goto fail;
1492 
1494  ret = ost_get_filters(o, oc, ost, &filters);
1495  if (ret < 0)
1496  goto fail;
1497  }
1498 
1499  if (ost->enc &&
1501  ret = ost_bind_filter(mux, ms, ofilter, o, filters, enc_tb, vsync_method,
1502  keep_pix_fmt, autoscale, threads_manual, vs);
1503  if (ret < 0)
1504  goto fail;
1505  } else if (ost->ist) {
1506  int sched_idx = ist_output_add(ost->ist, ost);
1507  if (sched_idx < 0) {
1509  "Error binding an input stream\n");
1510  ret = sched_idx;
1511  goto fail;
1512  }
1513  ms->sch_idx_src = sched_idx;
1514 
1515  if (ost->enc) {
1516  ret = sch_connect(mux->sch, SCH_DEC_OUT(sched_idx, 0),
1517  SCH_ENC(ms->sch_idx_enc));
1518  if (ret < 0)
1519  goto fail;
1520 
1521  ret = sch_connect(mux->sch, SCH_ENC(ms->sch_idx_enc),
1522  SCH_MSTREAM(mux->sch_idx, ms->sch_idx));
1523  if (ret < 0)
1524  goto fail;
1525  } else {
1526  ret = sch_connect(mux->sch, SCH_DSTREAM(ost->ist->file->index, sched_idx),
1527  SCH_MSTREAM(ost->file->index, ms->sch_idx));
1528  if (ret < 0)
1529  goto fail;
1530  }
1531  }
1532 
1533  if (ost->ist && !ost->enc) {
1534  ret = streamcopy_init(mux, ost, &encoder_opts);
1535  if (ret < 0)
1536  goto fail;
1537  }
1538 
1539  // copy estimated duration as a hint to the muxer
1540  if (ost->ist && ost->ist->st->duration > 0) {
1541  ms->stream_duration = ist->st->duration;
1542  ms->stream_duration_tb = ist->st->time_base;
1543  }
1544 
1545  if (post)
1546  *post = ost;
1547 
1548  ret = 0;
1549 
1550 fail:
1551  av_dict_free(&encoder_opts);
1552 
1553  return ret;
1554 }
1555 
1556 static int map_auto_video(Muxer *mux, const OptionsContext *o)
1557 {
1558  AVFormatContext *oc = mux->fc;
1559  InputStream *best_ist = NULL;
1560  int best_score = 0;
1561  int qcr;
1562 
1563  /* video: highest resolution */
1565  return 0;
1566 
1567  qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
1568  for (int j = 0; j < nb_input_files; j++) {
1569  InputFile *ifile = input_files[j];
1570  InputStream *file_best_ist = NULL;
1571  int file_best_score = 0;
1572  for (int i = 0; i < ifile->nb_streams; i++) {
1573  InputStream *ist = ifile->streams[i];
1574  int score;
1575 
1576  if (ist->user_set_discard == AVDISCARD_ALL ||
1578  continue;
1579 
1580  score = ist->st->codecpar->width * ist->st->codecpar->height
1581  + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1582  + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1583  if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1584  score = 1;
1585 
1586  if (score > file_best_score) {
1587  if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1588  continue;
1589  file_best_score = score;
1590  file_best_ist = ist;
1591  }
1592  }
1593  if (file_best_ist) {
1594  if((qcr == MKTAG('A', 'P', 'I', 'C')) ||
1595  !(file_best_ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1596  file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1597  if (file_best_score > best_score) {
1598  best_score = file_best_score;
1599  best_ist = file_best_ist;
1600  }
1601  }
1602  }
1603  if (best_ist)
1604  return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL, NULL, NULL);
1605 
1606  return 0;
1607 }
1608 
1609 static int map_auto_audio(Muxer *mux, const OptionsContext *o)
1610 {
1611  AVFormatContext *oc = mux->fc;
1612  InputStream *best_ist = NULL;
1613  int best_score = 0;
1614 
1615  /* audio: most channels */
1617  return 0;
1618 
1619  for (int j = 0; j < nb_input_files; j++) {
1620  InputFile *ifile = input_files[j];
1621  InputStream *file_best_ist = NULL;
1622  int file_best_score = 0;
1623  for (int i = 0; i < ifile->nb_streams; i++) {
1624  InputStream *ist = ifile->streams[i];
1625  int score;
1626 
1627  if (ist->user_set_discard == AVDISCARD_ALL ||
1629  continue;
1630 
1631  score = ist->st->codecpar->ch_layout.nb_channels
1632  + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1633  + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
1634  if (score > file_best_score) {
1635  file_best_score = score;
1636  file_best_ist = ist;
1637  }
1638  }
1639  if (file_best_ist) {
1640  file_best_score -= 5000000*!!(file_best_ist->st->disposition & AV_DISPOSITION_DEFAULT);
1641  if (file_best_score > best_score) {
1642  best_score = file_best_score;
1643  best_ist = file_best_ist;
1644  }
1645  }
1646  }
1647  if (best_ist)
1648  return ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL, NULL, NULL);
1649 
1650  return 0;
1651 }
1652 
1653 static int map_auto_subtitle(Muxer *mux, const OptionsContext *o)
1654 {
1655  AVFormatContext *oc = mux->fc;
1656  const char *subtitle_codec_name = NULL;
1657 
1658  /* subtitles: pick first */
1661  return 0;
1662 
1663  for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist))
1664  if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1665  AVCodecDescriptor const *input_descriptor =
1666  avcodec_descriptor_get(ist->st->codecpar->codec_id);
1667  AVCodecDescriptor const *output_descriptor = NULL;
1668  AVCodec const *output_codec =
1670  int input_props = 0, output_props = 0;
1671  if (ist->user_set_discard == AVDISCARD_ALL)
1672  continue;
1673  if (output_codec)
1674  output_descriptor = avcodec_descriptor_get(output_codec->id);
1675  if (input_descriptor)
1676  input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1677  if (output_descriptor)
1678  output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1679  if (subtitle_codec_name ||
1680  input_props & output_props ||
1681  // Map dvb teletext which has neither property to any output subtitle encoder
1682  input_descriptor && output_descriptor &&
1683  (!input_descriptor->props ||
1684  !output_descriptor->props)) {
1685  return ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL, NULL, NULL);
1686  }
1687  }
1688 
1689  return 0;
1690 }
1691 
1692 static int map_auto_data(Muxer *mux, const OptionsContext *o)
1693 {
1694  AVFormatContext *oc = mux->fc;
1695  /* Data only if codec id match */
1697 
1698  if (codec_id == AV_CODEC_ID_NONE)
1699  return 0;
1700 
1701  for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
1702  if (ist->user_set_discard == AVDISCARD_ALL)
1703  continue;
1704  if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1705  ist->st->codecpar->codec_id == codec_id) {
1706  int ret = ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL, NULL, NULL);
1707  if (ret < 0)
1708  return ret;
1709  }
1710  }
1711 
1712  return 0;
1713 }
1714 
1715 static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map)
1716 {
1717  InputStream *ist;
1718  int ret;
1719 
1720  if (map->disabled)
1721  return 0;
1722 
1723  if (map->linklabel) {
1724  FilterGraph *fg;
1725  OutputFilter *ofilter = NULL;
1726  int j, k;
1727 
1728  for (j = 0; j < nb_filtergraphs; j++) {
1729  fg = filtergraphs[j];
1730  for (k = 0; k < fg->nb_outputs; k++) {
1731  const char *linklabel = fg->outputs[k]->linklabel;
1732  if (linklabel && !strcmp(linklabel, map->linklabel)) {
1733  ofilter = fg->outputs[k];
1734  goto loop_end;
1735  }
1736  }
1737  }
1738 loop_end:
1739  if (!ofilter) {
1740  av_log(mux, AV_LOG_FATAL, "Output with label '%s' does not exist "
1741  "in any defined filter graph, or was already used elsewhere.\n", map->linklabel);
1742  return AVERROR(EINVAL);
1743  }
1744 
1745  av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly "
1746  "mapped complex filtergraph %d, output [%s]\n", fg->index, map->linklabel);
1747 
1748  ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL);
1749  if (ret < 0)
1750  return ret;
1751  } else {
1752  const ViewSpecifier *vs = map->vs.type == VIEW_SPECIFIER_TYPE_NONE ?
1753  NULL : &map->vs;
1754 
1755  ist = input_files[map->file_index]->streams[map->stream_index];
1756  if (ist->user_set_discard == AVDISCARD_ALL) {
1757  av_log(mux, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n",
1758  map->file_index, map->stream_index);
1759  return AVERROR(EINVAL);
1760  }
1762  return 0;
1764  return 0;
1766  return 0;
1767  if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1768  return 0;
1769 
1770  if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
1773  "Cannot map stream #%d:%d - unsupported type.\n",
1774  map->file_index, map->stream_index);
1775  if (!ignore_unknown_streams) {
1776  av_log(mux, AV_LOG_FATAL,
1777  "If you want unsupported types ignored instead "
1778  "of failing, please use the -ignore_unknown option\n"
1779  "If you want them copied, please use -copy_unknown\n");
1780  return AVERROR(EINVAL);
1781  }
1782  return 0;
1783  }
1784 
1785  if (vs && ist->st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
1786  av_log(mux, AV_LOG_ERROR,
1787  "View specifier given for mapping a %s input stream\n",
1789  return AVERROR(EINVAL);
1790  }
1791 
1792  ret = ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL, vs, NULL);
1793  if (ret < 0)
1794  return ret;
1795  }
1796 
1797  return 0;
1798 }
1799 
1800 static int of_add_attachments(Muxer *mux, const OptionsContext *o)
1801 {
1802  OutputStream *ost;
1803  int err;
1804 
1805  for (int i = 0; i < o->nb_attachments; i++) {
1806  AVIOContext *pb;
1807  uint8_t *attachment;
1808  char *attachment_filename;
1809  const char *p;
1810  int64_t len;
1811 
1812  if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
1813  av_log(mux, AV_LOG_FATAL, "Could not open attachment file %s.\n",
1814  o->attachments[i]);
1815  return err;
1816  }
1817  if ((len = avio_size(pb)) <= 0) {
1818  av_log(mux, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
1819  o->attachments[i]);
1820  err = len ? len : AVERROR_INVALIDDATA;
1821  goto read_fail;
1822  }
1823  if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
1824  av_log(mux, AV_LOG_FATAL, "Attachment %s too large.\n",
1825  o->attachments[i]);
1826  err = AVERROR(ERANGE);
1827  goto read_fail;
1828  }
1829 
1830  attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
1831  if (!attachment) {
1832  err = AVERROR(ENOMEM);
1833  goto read_fail;
1834  }
1835 
1836  err = avio_read(pb, attachment, len);
1837  if (err < 0)
1838  av_log(mux, AV_LOG_FATAL, "Error reading attachment file %s: %s\n",
1839  o->attachments[i], av_err2str(err));
1840  else if (err != len) {
1841  av_log(mux, AV_LOG_FATAL, "Could not read all %"PRId64" bytes for "
1842  "attachment file %s\n", len, o->attachments[i]);
1843  err = AVERROR(EIO);
1844  }
1845 
1846 read_fail:
1847  avio_closep(&pb);
1848  if (err < 0)
1849  return err;
1850 
1851  memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1852 
1853  av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n",
1854  o->attachments[i]);
1855 
1856  attachment_filename = av_strdup(o->attachments[i]);
1857  if (!attachment_filename) {
1858  av_free(attachment);
1859  return AVERROR(ENOMEM);
1860  }
1861 
1862  err = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL, NULL, &ost);
1863  if (err < 0) {
1864  av_free(attachment_filename);
1865  av_freep(&attachment);
1866  return err;
1867  }
1868 
1869  ost->attachment_filename = attachment_filename;
1870  ost->par_in->extradata = attachment;
1871  ost->par_in->extradata_size = len;
1872 
1873  p = strrchr(o->attachments[i], '/');
1874  av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
1875  }
1876 
1877  return 0;
1878 }
1879 
1880 static int create_streams(Muxer *mux, const OptionsContext *o)
1881 {
1882  static int (* const map_func[])(Muxer *mux, const OptionsContext *o) = {
1887  };
1888 
1889  AVFormatContext *oc = mux->fc;
1890 
1891  int auto_disable =
1892  o->video_disable * (1 << AVMEDIA_TYPE_VIDEO) |
1893  o->audio_disable * (1 << AVMEDIA_TYPE_AUDIO) |
1895  o->data_disable * (1 << AVMEDIA_TYPE_DATA);
1896 
1897  int ret;
1898 
1899  /* create streams for all unlabeled output pads */
1900  for (int i = 0; i < nb_filtergraphs; i++) {
1901  FilterGraph *fg = filtergraphs[i];
1902  for (int j = 0; j < fg->nb_outputs; j++) {
1903  OutputFilter *ofilter = fg->outputs[j];
1904 
1905  if (ofilter->linklabel || ofilter->bound)
1906  continue;
1907 
1908  auto_disable |= 1 << ofilter->type;
1909 
1910  av_log(mux, AV_LOG_VERBOSE, "Creating output stream from unlabeled "
1911  "output of complex filtergraph %d.", fg->index);
1912  if (!o->nb_stream_maps)
1913  av_log(mux, AV_LOG_VERBOSE, " This overrides automatic %s mapping.",
1914  av_get_media_type_string(ofilter->type));
1915  av_log(mux, AV_LOG_VERBOSE, "\n");
1916 
1917  ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL, NULL);
1918  if (ret < 0)
1919  return ret;
1920  }
1921  }
1922 
1923  if (!o->nb_stream_maps) {
1924  av_log(mux, AV_LOG_VERBOSE, "No explicit maps, mapping streams automatically...\n");
1925 
1926  /* pick the "best" stream of each type */
1927  for (int i = 0; i < FF_ARRAY_ELEMS(map_func); i++) {
1928  if (!map_func[i] || auto_disable & (1 << i))
1929  continue;
1930  ret = map_func[i](mux, o);
1931  if (ret < 0)
1932  return ret;
1933  }
1934  } else {
1935  av_log(mux, AV_LOG_VERBOSE, "Adding streams from explicit maps...\n");
1936 
1937  for (int i = 0; i < o->nb_stream_maps; i++) {
1938  ret = map_manual(mux, o, &o->stream_maps[i]);
1939  if (ret < 0)
1940  return ret;
1941  }
1942  }
1943 
1944  ret = of_add_attachments(mux, o);
1945  if (ret < 0)
1946  return ret;
1947 
1948  // setup fix_sub_duration_heartbeat mappings
1949  for (unsigned i = 0; i < oc->nb_streams; i++) {
1950  MuxStream *src = ms_from_ost(mux->of.streams[i]);
1951 
1952  if (!src->ost.fix_sub_duration_heartbeat)
1953  continue;
1954 
1955  for (unsigned j = 0; j < oc->nb_streams; j++) {
1956  MuxStream *dst = ms_from_ost(mux->of.streams[j]);
1957 
1958  if (src == dst || dst->ost.type != AVMEDIA_TYPE_SUBTITLE ||
1959  !dst->ost.enc || !dst->ost.ist || !dst->ost.ist->fix_sub_duration)
1960  continue;
1961 
1962  ret = sch_mux_sub_heartbeat_add(mux->sch, mux->sch_idx, src->sch_idx,
1963  dst->sch_idx_src);
1964 
1965  }
1966  }
1967 
1968  // handle -apad
1969  if (o->shortest) {
1970  int have_video = 0;
1971 
1972  for (unsigned i = 0; i < mux->of.nb_streams; i++)
1973  if (mux->of.streams[i]->type == AVMEDIA_TYPE_VIDEO) {
1974  have_video = 1;
1975  break;
1976  }
1977 
1978  for (unsigned i = 0; have_video && i < mux->of.nb_streams; i++) {
1979  MuxStream *ms = ms_from_ost(mux->of.streams[i]);
1980  OutputFilter *ofilter = ms->ost.filter;
1981 
1982  if (ms->ost.type != AVMEDIA_TYPE_AUDIO || !ms->apad || !ofilter)
1983  continue;
1984 
1985  ofilter->apad = av_strdup(ms->apad);
1986  if (!ofilter->apad)
1987  return AVERROR(ENOMEM);
1988  }
1989  }
1990  for (unsigned i = 0; i < mux->of.nb_streams; i++) {
1991  MuxStream *ms = ms_from_ost(mux->of.streams[i]);
1992  ms->apad = NULL;
1993  }
1994 
1995  if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
1996  av_dump_format(oc, nb_output_files - 1, oc->url, 1);
1997  av_log(mux, AV_LOG_ERROR, "Output file does not contain any stream\n");
1998  return AVERROR(EINVAL);
1999  }
2000 
2001  return 0;
2002 }
2003 
2005  int64_t buf_size_us, int shortest)
2006 {
2007  OutputFile *of = &mux->of;
2008  int nb_av_enc = 0, nb_audio_fs = 0, nb_interleaved = 0;
2009  int limit_frames = 0, limit_frames_av_enc = 0;
2010 
2011 #define IS_AV_ENC(ost, type) \
2012  (ost->enc_ctx && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
2013 #define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT)
2014 
2015  for (int i = 0; i < oc->nb_streams; i++) {
2016  OutputStream *ost = of->streams[i];
2017  MuxStream *ms = ms_from_ost(ost);
2018  enum AVMediaType type = ost->type;
2019 
2020  ms->sq_idx_mux = -1;
2021 
2022  nb_interleaved += IS_INTERLEAVED(type);
2023  nb_av_enc += IS_AV_ENC(ost, type);
2024  nb_audio_fs += (ost->enc_ctx && type == AVMEDIA_TYPE_AUDIO &&
2025  !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
2026 
2027  limit_frames |= ms->max_frames < INT64_MAX;
2028  limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type);
2029  }
2030 
2031  if (!((nb_interleaved > 1 && shortest) ||
2032  (nb_interleaved > 0 && limit_frames) ||
2033  nb_audio_fs))
2034  return 0;
2035 
2036  /* we use a sync queue before encoding when:
2037  * - 'shortest' is in effect and we have two or more encoded audio/video
2038  * streams
2039  * - at least one encoded audio/video stream is frame-limited, since
2040  * that has similar semantics to 'shortest'
2041  * - at least one audio encoder requires constant frame sizes
2042  *
2043  * Note that encoding sync queues are handled in the scheduler, because
2044  * different encoders run in different threads and need external
2045  * synchronization, while muxer sync queues can be handled inside the muxer
2046  */
2047  if ((shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) {
2048  int sq_idx, ret;
2049 
2050  sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux);
2051  if (sq_idx < 0)
2052  return sq_idx;
2053 
2054  for (int i = 0; i < oc->nb_streams; i++) {
2055  OutputStream *ost = of->streams[i];
2056  MuxStream *ms = ms_from_ost(ost);
2057  enum AVMediaType type = ost->type;
2058 
2059  if (!IS_AV_ENC(ost, type))
2060  continue;
2061 
2062  ret = sch_sq_add_enc(mux->sch, sq_idx, ms->sch_idx_enc,
2063  shortest || ms->max_frames < INT64_MAX,
2064  ms->max_frames);
2065  if (ret < 0)
2066  return ret;
2067  }
2068  }
2069 
2070  /* if there are any additional interleaved streams, then ALL the streams
2071  * are also synchronized before sending them to the muxer */
2072  if (nb_interleaved > nb_av_enc) {
2073  mux->sq_mux = sq_alloc(SYNC_QUEUE_PACKETS, buf_size_us, mux);
2074  if (!mux->sq_mux)
2075  return AVERROR(ENOMEM);
2076 
2077  mux->sq_pkt = av_packet_alloc();
2078  if (!mux->sq_pkt)
2079  return AVERROR(ENOMEM);
2080 
2081  for (int i = 0; i < oc->nb_streams; i++) {
2082  OutputStream *ost = of->streams[i];
2083  MuxStream *ms = ms_from_ost(ost);
2084  enum AVMediaType type = ost->type;
2085 
2086  if (!IS_INTERLEAVED(type))
2087  continue;
2088 
2089  ms->sq_idx_mux = sq_add_stream(mux->sq_mux,
2090  shortest || ms->max_frames < INT64_MAX);
2091  if (ms->sq_idx_mux < 0)
2092  return ms->sq_idx_mux;
2093 
2094  if (ms->max_frames != INT64_MAX)
2095  sq_limit_frames(mux->sq_mux, ms->sq_idx_mux, ms->max_frames);
2096  }
2097  }
2098 
2099 #undef IS_AV_ENC
2100 #undef IS_INTERLEAVED
2101 
2102  return 0;
2103 }
2104 
2105 static int of_parse_iamf_audio_element_layers(Muxer *mux, AVStreamGroup *stg, char *ptr)
2106 {
2107  AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element;
2108  AVDictionary *dict = NULL;
2109  const char *token;
2110  int ret = 0;
2111 
2112  audio_element->demixing_info =
2114  audio_element->recon_gain_info =
2116 
2117  if (!audio_element->demixing_info ||
2118  !audio_element->recon_gain_info)
2119  return AVERROR(ENOMEM);
2120 
2121  /* process manually set layers and parameters */
2122  token = av_strtok(NULL, ",", &ptr);
2123  while (token) {
2124  const AVDictionaryEntry *e;
2125  int demixing = 0, recon_gain = 0;
2126  int layer = 0;
2127 
2128  if (ptr)
2129  ptr += strspn(ptr, " \n\t\r");
2130  if (av_strstart(token, "layer=", &token))
2131  layer = 1;
2132  else if (av_strstart(token, "demixing=", &token))
2133  demixing = 1;
2134  else if (av_strstart(token, "recon_gain=", &token))
2135  recon_gain = 1;
2136 
2137  av_dict_free(&dict);
2138  ret = av_dict_parse_string(&dict, token, "=", ":", 0);
2139  if (ret < 0) {
2140  av_log(mux, AV_LOG_ERROR, "Error parsing audio element specification %s\n", token);
2141  goto fail;
2142  }
2143 
2144  if (layer) {
2145  AVIAMFLayer *audio_layer = av_iamf_audio_element_add_layer(audio_element);
2146  if (!audio_layer) {
2147  av_log(mux, AV_LOG_ERROR, "Error adding layer to stream group %d\n", stg->index);
2148  ret = AVERROR(ENOMEM);
2149  goto fail;
2150  }
2151  av_opt_set_dict(audio_layer, &dict);
2152  } else if (demixing || recon_gain) {
2153  AVIAMFParamDefinition *param = demixing ? audio_element->demixing_info
2154  : audio_element->recon_gain_info;
2155  void *subblock = av_iamf_param_definition_get_subblock(param, 0);
2156 
2157  av_opt_set_dict(param, &dict);
2158  av_opt_set_dict(subblock, &dict);
2159  }
2160 
2161  // make sure that no entries are left in the dict
2162  e = NULL;
2163  if (e = av_dict_iterate(dict, e)) {
2164  av_log(mux, AV_LOG_FATAL, "Unknown layer key %s.\n", e->key);
2165  ret = AVERROR(EINVAL);
2166  goto fail;
2167  }
2168  token = av_strtok(NULL, ",", &ptr);
2169  }
2170 
2171 fail:
2172  av_dict_free(&dict);
2173  if (!ret && !audio_element->nb_layers) {
2174  av_log(mux, AV_LOG_ERROR, "No layer in audio element specification\n");
2175  ret = AVERROR(EINVAL);
2176  }
2177 
2178  return ret;
2179 }
2180 
2181 static int of_parse_iamf_submixes(Muxer *mux, AVStreamGroup *stg, char *ptr)
2182 {
2183  AVFormatContext *oc = mux->fc;
2185  AVDictionary *dict = NULL;
2186  const char *token;
2187  char *submix_str = NULL;
2188  int ret = 0;
2189 
2190  /* process manually set submixes */
2191  token = av_strtok(NULL, ",", &ptr);
2192  while (token) {
2193  AVIAMFSubmix *submix = NULL;
2194  const char *subtoken;
2195  char *subptr = NULL;
2196 
2197  if (ptr)
2198  ptr += strspn(ptr, " \n\t\r");
2199  if (!av_strstart(token, "submix=", &token)) {
2200  av_log(mux, AV_LOG_ERROR, "No submix in mix presentation specification \"%s\"\n", token);
2201  goto fail;
2202  }
2203 
2204  submix_str = av_strdup(token);
2205  if (!submix_str)
2206  goto fail;
2207 
2209  if (!submix) {
2210  av_log(mux, AV_LOG_ERROR, "Error adding submix to stream group %d\n", stg->index);
2211  ret = AVERROR(ENOMEM);
2212  goto fail;
2213  }
2214  submix->output_mix_config =
2216  if (!submix->output_mix_config) {
2217  ret = AVERROR(ENOMEM);
2218  goto fail;
2219  }
2220 
2221  subptr = NULL;
2222  subtoken = av_strtok(submix_str, "|", &subptr);
2223  while (subtoken) {
2224  const AVDictionaryEntry *e;
2225  int element = 0, layout = 0;
2226 
2227  if (subptr)
2228  subptr += strspn(subptr, " \n\t\r");
2229  if (av_strstart(subtoken, "element=", &subtoken))
2230  element = 1;
2231  else if (av_strstart(subtoken, "layout=", &subtoken))
2232  layout = 1;
2233 
2234  av_dict_free(&dict);
2235  ret = av_dict_parse_string(&dict, subtoken, "=", ":", 0);
2236  if (ret < 0) {
2237  av_log(mux, AV_LOG_ERROR, "Error parsing submix specification \"%s\"\n", subtoken);
2238  goto fail;
2239  }
2240 
2241  if (element) {
2242  AVIAMFSubmixElement *submix_element;
2243  char *endptr = NULL;
2244  int64_t idx = -1;
2245 
2246  if (e = av_dict_get(dict, "stg", NULL, 0))
2247  idx = strtoll(e->value, &endptr, 0);
2248  if (!endptr || *endptr || idx < 0 || idx >= oc->nb_stream_groups - 1 ||
2250  av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in "
2251  "submix element specification \"%s\"\n", subtoken);
2252  ret = AVERROR(EINVAL);
2253  goto fail;
2254  }
2255  submix_element = av_iamf_submix_add_element(submix);
2256  if (!submix_element) {
2257  av_log(mux, AV_LOG_ERROR, "Error adding element to submix\n");
2258  ret = AVERROR(ENOMEM);
2259  goto fail;
2260  }
2261 
2262  submix_element->audio_element_id = oc->stream_groups[idx]->id;
2263 
2264  submix_element->element_mix_config =
2266  if (!submix_element->element_mix_config)
2267  ret = AVERROR(ENOMEM);
2268  av_dict_set(&dict, "stg", NULL, 0);
2269  av_opt_set_dict2(submix_element, &dict, AV_OPT_SEARCH_CHILDREN);
2270  } else if (layout) {
2271  AVIAMFSubmixLayout *submix_layout = av_iamf_submix_add_layout(submix);
2272  if (!submix_layout) {
2273  av_log(mux, AV_LOG_ERROR, "Error adding layout to submix\n");
2274  ret = AVERROR(ENOMEM);
2275  goto fail;
2276  }
2277  av_opt_set_dict(submix_layout, &dict);
2278  } else
2279  av_opt_set_dict2(submix, &dict, AV_OPT_SEARCH_CHILDREN);
2280 
2281  if (ret < 0) {
2282  goto fail;
2283  }
2284 
2285  // make sure that no entries are left in the dict
2286  e = NULL;
2287  while (e = av_dict_iterate(dict, e)) {
2288  av_log(mux, AV_LOG_FATAL, "Unknown submix key %s.\n", e->key);
2289  ret = AVERROR(EINVAL);
2290  goto fail;
2291  }
2292  subtoken = av_strtok(NULL, "|", &subptr);
2293  }
2294  av_freep(&submix_str);
2295 
2296  if (!submix->nb_elements) {
2297  av_log(mux, AV_LOG_ERROR, "No audio elements in submix specification \"%s\"\n", token);
2298  ret = AVERROR(EINVAL);
2299  }
2300  token = av_strtok(NULL, ",", &ptr);
2301  }
2302 
2303 fail:
2304  av_dict_free(&dict);
2305  av_free(submix_str);
2306 
2307  return ret;
2308 }
2309 
2310 static int of_serialize_options(Muxer *mux, void *obj, AVBPrint *bp)
2311 {
2312  char *ptr;
2313  int ret;
2314 
2316  &ptr, '=', ':');
2317  if (ret < 0) {
2318  av_log(mux, AV_LOG_ERROR, "Failed to serialize group\n");
2319  return ret;
2320  }
2321 
2322  av_bprintf(bp, "%s", ptr);
2323  ret = strlen(ptr);
2324  av_free(ptr);
2325 
2326  return ret;
2327 }
2328 
2329 #define SERIALIZE(parent, child) do { \
2330  ret = of_serialize_options(mux, parent->child, bp); \
2331  if (ret < 0) \
2332  return ret; \
2333 } while (0)
2334 
2335 #define SERIALIZE_LOOP_SUBBLOCK(obj) do { \
2336  for (int k = 0; k < obj->nb_subblocks; k++) { \
2337  ret = of_serialize_options(mux, \
2338  av_iamf_param_definition_get_subblock(obj, k), bp); \
2339  if (ret < 0) \
2340  return ret; \
2341  } \
2342 } while (0)
2343 
2344 #define SERIALIZE_LOOP(parent, child, suffix, separator) do { \
2345  for (int j = 0; j < parent->nb_## child ## suffix; j++) { \
2346  av_bprintf(bp, separator#child "="); \
2347  SERIALIZE(parent, child ## suffix[j]); \
2348  } \
2349 } while (0)
2350 
2352 {
2353  AVFormatContext *oc = mux->fc;
2354 
2355  for (unsigned i = 0; i < oc->nb_stream_groups; i++)
2356  if (oc->stream_groups[i]->id == id)
2357  return oc->stream_groups[i]->index;
2358 
2359  return AVERROR(EINVAL);
2360 }
2361 
2362 static int of_map_group(Muxer *mux, AVDictionary **dict, AVBPrint *bp, const char *map)
2363 {
2364  AVStreamGroup *stg;
2365  int ret, file_idx, stream_idx;
2366  char *ptr;
2367 
2368  file_idx = strtol(map, &ptr, 0);
2369  if (file_idx >= nb_input_files || file_idx < 0 || map == ptr) {
2370  av_log(mux, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx);
2371  return AVERROR(EINVAL);
2372  }
2373 
2374  stream_idx = strtol(*ptr == '=' ? ptr + 1 : ptr, &ptr, 0);
2375  if (*ptr || stream_idx >= input_files[file_idx]->ctx->nb_stream_groups || stream_idx < 0) {
2376  av_log(mux, AV_LOG_ERROR, "Invalid input stream group index: %d.\n", stream_idx);
2377  return AVERROR(EINVAL);
2378  }
2379 
2380  stg = input_files[file_idx]->ctx->stream_groups[stream_idx];
2381  ret = of_serialize_options(mux, stg, bp);
2382  if (ret < 0)
2383  return ret;
2384 
2385  ret = av_dict_parse_string(dict, bp->str, "=", ":", 0);
2386  if (ret < 0)
2387  av_log(mux, AV_LOG_ERROR, "Error parsing mapped group specification %s\n", ptr);
2388  av_dict_set_int(dict, "type", stg->type, 0);
2389 
2390  av_bprint_clear(bp);
2391  switch(stg->type) {
2393  AVIAMFAudioElement *audio_element = stg->params.iamf_audio_element;
2394 
2395  if (audio_element->demixing_info) {
2396  AVIAMFParamDefinition *demixing_info = audio_element->demixing_info;
2397  av_bprintf(bp, ",demixing=");
2398  SERIALIZE(audio_element, demixing_info);
2399  if (ret && demixing_info->nb_subblocks)
2400  av_bprintf(bp, ":");
2401  SERIALIZE_LOOP_SUBBLOCK(demixing_info);
2402  }
2403  if (audio_element->recon_gain_info) {
2404  AVIAMFParamDefinition *recon_gain_info = audio_element->recon_gain_info;
2405  av_bprintf(bp, ",recon_gain=");
2406  SERIALIZE(audio_element, recon_gain_info);
2407  if (ret && recon_gain_info->nb_subblocks)
2408  av_bprintf(bp, ":");
2409  SERIALIZE_LOOP_SUBBLOCK(recon_gain_info);
2410  }
2411  SERIALIZE_LOOP(audio_element, layer, s, ",");
2412  break;
2413  }
2416 
2417  for (int i = 0; i < mix->nb_submixes; i++) {
2418  AVIAMFSubmix *submix = mix->submixes[i];
2419  AVIAMFParamDefinition *output_mix_config = submix->output_mix_config;
2420 
2421  av_bprintf(bp, ",submix=");
2422  SERIALIZE(mix, submixes[i]);
2423  if (ret && output_mix_config->nb_subblocks)
2424  av_bprintf(bp, ":");
2425  SERIALIZE_LOOP_SUBBLOCK(output_mix_config);
2426  for (int j = 0; j < submix->nb_elements; j++) {
2427  AVIAMFSubmixElement *element = submix->elements[j];
2428  AVIAMFParamDefinition *element_mix_config = element->element_mix_config;
2430 
2431  if (id < 0) {
2432  av_log(mux, AV_LOG_ERROR, "Invalid or missing stream group index in"
2433  "submix element");
2434  return id;
2435  }
2436 
2437  av_bprintf(bp, "|element=");
2438  SERIALIZE(submix, elements[j]);
2439  if (ret && element_mix_config->nb_subblocks)
2440  av_bprintf(bp, ":");
2441  SERIALIZE_LOOP_SUBBLOCK(element_mix_config);
2442  if (ret)
2443  av_bprintf(bp, ":");
2444  av_bprintf(bp, "stg=%"PRId64, id);
2445  }
2446  SERIALIZE_LOOP(submix, layout, s, "|");
2447  }
2448  break;
2449  }
2450  default:
2451  av_log(mux, AV_LOG_ERROR, "Unsupported mapped group type %d.\n", stg->type);
2452  ret = AVERROR(EINVAL);
2453  break;
2454  }
2455  return 0;
2456 }
2457 
2458 static int of_parse_group_token(Muxer *mux, const char *token, char *ptr)
2459 {
2460  AVFormatContext *oc = mux->fc;
2461  AVStreamGroup *stg;
2462  AVDictionary *dict = NULL, *tmp = NULL;
2463  char *mapped_string = NULL;
2464  const AVDictionaryEntry *e;
2465  const AVOption opts[] = {
2466  { "type", "Set group type", offsetof(AVStreamGroup, type), AV_OPT_TYPE_INT,
2467  { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "type" },
2468  { "iamf_audio_element", NULL, 0, AV_OPT_TYPE_CONST,
2469  { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT }, .unit = "type" },
2470  { "iamf_mix_presentation", NULL, 0, AV_OPT_TYPE_CONST,
2471  { .i64 = AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION }, .unit = "type" },
2472  { NULL },
2473  };
2474  const AVClass class = {
2475  .class_name = "StreamGroupType",
2476  .item_name = av_default_item_name,
2477  .option = opts,
2478  .version = LIBAVUTIL_VERSION_INT,
2479  };
2480  const AVClass *pclass = &class;
2481  int type, ret;
2482 
2483  ret = av_dict_parse_string(&dict, token, "=", ":", AV_DICT_MULTIKEY);
2484  if (ret < 0) {
2485  av_log(mux, AV_LOG_ERROR, "Error parsing group specification %s\n", token);
2486  return ret;
2487  }
2488 
2489  av_dict_copy(&tmp, dict, 0);
2490  e = av_dict_get(dict, "map", NULL, 0);
2491  if (e) {
2492  AVBPrint bp;
2493 
2494  if (ptr) {
2495  av_log(mux, AV_LOG_ERROR, "Unexpected extra parameters when mapping a"
2496  " stream group\n");
2497  ret = AVERROR(EINVAL);
2498  goto end;
2499  }
2500 
2502  ret = of_map_group(mux, &tmp, &bp, e->value);
2503  if (ret < 0) {
2504  av_bprint_finalize(&bp, NULL);
2505  goto end;
2506  }
2507 
2508  av_bprint_finalize(&bp, &mapped_string);
2509  ptr = mapped_string;
2510  }
2511 
2512  // "type" is not a user settable AVOption in AVStreamGroup, so handle it here
2513  e = av_dict_get(tmp, "type", NULL, 0);
2514  if (!e) {
2515  av_log(mux, AV_LOG_ERROR, "No type specified for Stream Group in \"%s\"\n", token);
2516  ret = AVERROR(EINVAL);
2517  goto end;
2518  }
2519 
2520  ret = av_opt_eval_int(&pclass, opts, e->value, &type);
2522  ret = AVERROR(EINVAL);
2523  if (ret < 0) {
2524  av_log(mux, AV_LOG_ERROR, "Invalid group type \"%s\"\n", e->value);
2525  goto end;
2526  }
2527 
2528  stg = avformat_stream_group_create(oc, type, &tmp);
2529  if (!stg) {
2530  ret = AVERROR(ENOMEM);
2531  goto end;
2532  }
2533 
2534  e = NULL;
2535  while (e = av_dict_get(dict, "st", e, 0)) {
2536  char *endptr;
2537  int64_t idx = strtoll(e->value, &endptr, 0);
2538  if (*endptr || idx < 0 || idx >= oc->nb_streams) {
2539  av_log(mux, AV_LOG_ERROR, "Invalid stream index %"PRId64"\n", idx);
2540  ret = AVERROR(EINVAL);
2541  goto end;
2542  }
2543  ret = avformat_stream_group_add_stream(stg, oc->streams[idx]);
2544  if (ret < 0)
2545  goto end;
2546  }
2547  while (e = av_dict_get(dict, "stg", e, 0)) {
2548  char *endptr;
2549  int64_t idx = strtoll(e->value, &endptr, 0);
2550  if (*endptr || idx < 0 || idx >= oc->nb_stream_groups - 1) {
2551  av_log(mux, AV_LOG_ERROR, "Invalid stream group index %"PRId64"\n", idx);
2552  ret = AVERROR(EINVAL);
2553  goto end;
2554  }
2555  for (unsigned i = 0; i < oc->stream_groups[idx]->nb_streams; i++) {
2557  if (ret < 0)
2558  goto end;
2559  }
2560  }
2561 
2562  switch(type) {
2564  ret = of_parse_iamf_audio_element_layers(mux, stg, ptr);
2565  break;
2567  ret = of_parse_iamf_submixes(mux, stg, ptr);
2568  break;
2569  default:
2570  av_log(mux, AV_LOG_FATAL, "Unknown group type %d.\n", type);
2571  ret = AVERROR(EINVAL);
2572  break;
2573  }
2574 
2575  if (ret < 0)
2576  goto end;
2577 
2578  // make sure that nothing but "st" and "stg" entries are left in the dict
2579  e = NULL;
2580  av_dict_set(&tmp, "map", NULL, 0);
2581  av_dict_set(&tmp, "type", NULL, 0);
2582  while (e = av_dict_iterate(tmp, e)) {
2583  if (!strcmp(e->key, "st") || !strcmp(e->key, "stg"))
2584  continue;
2585 
2586  av_log(mux, AV_LOG_FATAL, "Unknown group key %s.\n", e->key);
2587  ret = AVERROR(EINVAL);
2588  goto end;
2589  }
2590 
2591  ret = 0;
2592 end:
2593  av_free(mapped_string);
2594  av_dict_free(&dict);
2595  av_dict_free(&tmp);
2596 
2597  return ret;
2598 }
2599 
2600 static int of_add_groups(Muxer *mux, const OptionsContext *o)
2601 {
2602  /* process manually set groups */
2603  for (int i = 0; i < o->stream_groups.nb_opt; i++) {
2604  const char *token;
2605  char *str, *ptr = NULL;
2606  int ret = 0;
2607 
2608  str = av_strdup(o->stream_groups.opt[i].u.str);
2609  if (!str)
2610  return ret;
2611 
2612  token = av_strtok(str, ",", &ptr);
2613  if (token) {
2614  if (ptr)
2615  ptr += strspn(ptr, " \n\t\r");
2616  ret = of_parse_group_token(mux, token, ptr);
2617  }
2618 
2619  av_free(str);
2620  if (ret < 0)
2621  return ret;
2622  }
2623 
2624  return 0;
2625 }
2626 
2627 static int of_add_programs(Muxer *mux, const OptionsContext *o)
2628 {
2629  AVFormatContext *oc = mux->fc;
2630  /* process manually set programs */
2631  for (int i = 0; i < o->program.nb_opt; i++) {
2632  AVDictionary *dict = NULL;
2633  const AVDictionaryEntry *e;
2634  AVProgram *program;
2635  int ret, progid = i + 1;
2636 
2637  ret = av_dict_parse_string(&dict, o->program.opt[i].u.str, "=", ":",
2639  if (ret < 0) {
2640  av_log(mux, AV_LOG_ERROR, "Error parsing program specification %s\n",
2641  o->program.opt[i].u.str);
2642  return ret;
2643  }
2644 
2645  e = av_dict_get(dict, "program_num", NULL, 0);
2646  if (e) {
2647  progid = strtol(e->value, NULL, 0);
2648  av_dict_set(&dict, e->key, NULL, 0);
2649  }
2650 
2651  program = av_new_program(oc, progid);
2652  if (!program) {
2653  ret = AVERROR(ENOMEM);
2654  goto fail;
2655  }
2656 
2657  e = av_dict_get(dict, "title", NULL, 0);
2658  if (e) {
2659  av_dict_set(&program->metadata, e->key, e->value, 0);
2660  av_dict_set(&dict, e->key, NULL, 0);
2661  }
2662 
2663  e = NULL;
2664  while (e = av_dict_get(dict, "st", e, 0)) {
2665  int st_num = strtol(e->value, NULL, 0);
2666  av_program_add_stream_index(oc, progid, st_num);
2667  }
2668 
2669  // make sure that nothing but "st" entries are left in the dict
2670  e = NULL;
2671  while (e = av_dict_iterate(dict, e)) {
2672  if (!strcmp(e->key, "st"))
2673  continue;
2674 
2675  av_log(mux, AV_LOG_FATAL, "Unknown program key %s.\n", e->key);
2676  ret = AVERROR(EINVAL);
2677  goto fail;
2678  }
2679 
2680 fail:
2681  av_dict_free(&dict);
2682  if (ret < 0)
2683  return ret;
2684  }
2685 
2686  return 0;
2687 }
2688 
2689 /**
2690  * Parse a metadata specifier passed as 'arg' parameter.
2691  * @param arg metadata string to parse
2692  * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
2693  * @param index for type c/p, chapter/program index is written here
2694  * @param stream_spec for type s, the stream specifier is written here
2695  */
2696 static int parse_meta_type(void *logctx, const char *arg,
2697  char *type, int *index, const char **stream_spec)
2698 {
2699  if (*arg) {
2700  *type = *arg;
2701  switch (*arg) {
2702  case 'g':
2703  break;
2704  case 's':
2705  if (*(++arg) && *arg != ':') {
2706  av_log(logctx, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
2707  return AVERROR(EINVAL);
2708  }
2709  *stream_spec = *arg == ':' ? arg + 1 : "";
2710  break;
2711  case 'c':
2712  case 'p':
2713  if (*(++arg) == ':')
2714  *index = strtol(++arg, NULL, 0);
2715  break;
2716  default:
2717  av_log(logctx, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
2718  return AVERROR(EINVAL);
2719  }
2720  } else
2721  *type = 'g';
2722 
2723  return 0;
2724 }
2725 
2727  const OptionsContext *o)
2728 {
2729  for (int i = 0; i < o->metadata.nb_opt; i++) {
2730  AVDictionary **m;
2731  char type, *val;
2732  const char *stream_spec;
2733  int index = 0, ret = 0;
2734 
2735  val = strchr(o->metadata.opt[i].u.str, '=');
2736  if (!val) {
2737  av_log(of, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
2738  o->metadata.opt[i].u.str);
2739  return AVERROR(EINVAL);
2740  }
2741  *val++ = 0;
2742 
2743  ret = parse_meta_type(of, o->metadata.opt[i].specifier, &type, &index, &stream_spec);
2744  if (ret < 0)
2745  return ret;
2746 
2747  if (type == 's') {
2748  for (int j = 0; j < oc->nb_streams; j++) {
2749  if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
2750  av_dict_set(&oc->streams[j]->metadata, o->metadata.opt[i].u.str, *val ? val : NULL, 0);
2751  } else if (ret < 0)
2752  return ret;
2753  }
2754  } else {
2755  switch (type) {
2756  case 'g':
2757  m = &oc->metadata;
2758  break;
2759  case 'c':
2760  if (index < 0 || index >= oc->nb_chapters) {
2761  av_log(of, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
2762  return AVERROR(EINVAL);
2763  }
2764  m = &oc->chapters[index]->metadata;
2765  break;
2766  case 'p':
2767  if (index < 0 || index >= oc->nb_programs) {
2768  av_log(of, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index);
2769  return AVERROR(EINVAL);
2770  }
2771  m = &oc->programs[index]->metadata;
2772  break;
2773  default:
2774  av_log(of, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata.opt[i].specifier);
2775  return AVERROR(EINVAL);
2776  }
2777  av_dict_set(m, o->metadata.opt[i].u.str, *val ? val : NULL, 0);
2778  }
2779  }
2780 
2781  return 0;
2782 }
2783 
2784 static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os,
2785  int copy_metadata)
2786 {
2787  AVFormatContext *is = ifile->ctx;
2788  AVChapter **tmp;
2789 
2790  tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters));
2791  if (!tmp)
2792  return AVERROR(ENOMEM);
2793  os->chapters = tmp;
2794 
2795  for (int i = 0; i < is->nb_chapters; i++) {
2796  AVChapter *in_ch = is->chapters[i], *out_ch;
2797  int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
2798  int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset,
2799  AV_TIME_BASE_Q, in_ch->time_base);
2800  int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
2802 
2803 
2804  if (in_ch->end < ts_off)
2805  continue;
2806  if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2807  break;
2808 
2809  out_ch = av_mallocz(sizeof(AVChapter));
2810  if (!out_ch)
2811  return AVERROR(ENOMEM);
2812 
2813  out_ch->id = in_ch->id;
2814  out_ch->time_base = in_ch->time_base;
2815  out_ch->start = FFMAX(0, in_ch->start - ts_off);
2816  out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2817 
2818  if (copy_metadata)
2819  av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2820 
2821  os->chapters[os->nb_chapters++] = out_ch;
2822  }
2823  return 0;
2824 }
2825 
2826 static int copy_metadata(Muxer *mux, AVFormatContext *ic,
2827  const char *outspec, const char *inspec,
2828  int *metadata_global_manual, int *metadata_streams_manual,
2829  int *metadata_chapters_manual)
2830 {
2831  AVFormatContext *oc = mux->fc;
2832  AVDictionary **meta_in = NULL;
2833  AVDictionary **meta_out = NULL;
2834  int i, ret = 0;
2835  char type_in, type_out;
2836  const char *istream_spec = NULL, *ostream_spec = NULL;
2837  int idx_in = 0, idx_out = 0;
2838 
2839  ret = parse_meta_type(mux, inspec, &type_in, &idx_in, &istream_spec);
2840  if (ret >= 0)
2841  ret = parse_meta_type(mux, outspec, &type_out, &idx_out, &ostream_spec);
2842  if (ret < 0)
2843  return ret;
2844 
2845  if (type_in == 'g' || type_out == 'g' || (!*outspec && !ic))
2846  *metadata_global_manual = 1;
2847  if (type_in == 's' || type_out == 's' || (!*outspec && !ic))
2848  *metadata_streams_manual = 1;
2849  if (type_in == 'c' || type_out == 'c' || (!*outspec && !ic))
2850  *metadata_chapters_manual = 1;
2851 
2852  /* ic is NULL when just disabling automatic mappings */
2853  if (!ic)
2854  return 0;
2855 
2856 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
2857  if ((index) < 0 || (index) >= (nb_elems)) {\
2858  av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
2859  (desc), (index));\
2860  return AVERROR(EINVAL);\
2861  }
2862 
2863 #define SET_DICT(type, meta, context, index)\
2864  switch (type) {\
2865  case 'g':\
2866  meta = &context->metadata;\
2867  break;\
2868  case 'c':\
2869  METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
2870  meta = &context->chapters[index]->metadata;\
2871  break;\
2872  case 'p':\
2873  METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
2874  meta = &context->programs[index]->metadata;\
2875  break;\
2876  case 's':\
2877  break; /* handled separately below */ \
2878  default: av_assert0(0);\
2879  }\
2880 
2881  SET_DICT(type_in, meta_in, ic, idx_in);
2882  SET_DICT(type_out, meta_out, oc, idx_out);
2883 
2884  /* for input streams choose first matching stream */
2885  if (type_in == 's') {
2886  for (i = 0; i < ic->nb_streams; i++) {
2887  if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
2888  meta_in = &ic->streams[i]->metadata;
2889  break;
2890  } else if (ret < 0)
2891  return ret;
2892  }
2893  if (!meta_in) {
2894  av_log(mux, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
2895  return AVERROR(EINVAL);
2896  }
2897  }
2898 
2899  if (type_out == 's') {
2900  for (i = 0; i < oc->nb_streams; i++) {
2901  if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
2902  meta_out = &oc->streams[i]->metadata;
2903  av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
2904  } else if (ret < 0)
2905  return ret;
2906  }
2907  } else
2908  av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
2909 
2910  return 0;
2911 }
2912 
2913 static int copy_meta(Muxer *mux, const OptionsContext *o)
2914 {
2915  OutputFile *of = &mux->of;
2916  AVFormatContext *oc = mux->fc;
2917  int chapters_input_file = o->chapters_input_file;
2918  int metadata_global_manual = 0;
2919  int metadata_streams_manual = 0;
2920  int metadata_chapters_manual = 0;
2921  int ret;
2922 
2923  /* copy metadata */
2924  for (int i = 0; i < o->metadata_map.nb_opt; i++) {
2925  char *p;
2926  int in_file_index = strtol(o->metadata_map.opt[i].u.str, &p, 0);
2927 
2928  if (in_file_index >= nb_input_files) {
2929  av_log(mux, AV_LOG_FATAL, "Invalid input file index %d while "
2930  "processing metadata maps\n", in_file_index);
2931  return AVERROR(EINVAL);
2932  }
2933  ret = copy_metadata(mux,
2934  in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL,
2935  o->metadata_map.opt[i].specifier, *p ? p + 1 : p,
2936  &metadata_global_manual, &metadata_streams_manual,
2937  &metadata_chapters_manual);
2938  if (ret < 0)
2939  return ret;
2940  }
2941 
2942  /* copy chapters */
2943  if (chapters_input_file >= nb_input_files) {
2944  if (chapters_input_file == INT_MAX) {
2945  /* copy chapters from the first input file that has them*/
2946  chapters_input_file = -1;
2947  for (int i = 0; i < nb_input_files; i++)
2948  if (input_files[i]->ctx->nb_chapters) {
2949  chapters_input_file = i;
2950  break;
2951  }
2952  } else {
2953  av_log(mux, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
2954  chapters_input_file);
2955  return AVERROR(EINVAL);
2956  }
2957  }
2958  if (chapters_input_file >= 0)
2959  copy_chapters(input_files[chapters_input_file], of, oc,
2960  !metadata_chapters_manual);
2961 
2962  /* copy global metadata by default */
2963  if (!metadata_global_manual && nb_input_files){
2966  if (of->recording_time != INT64_MAX)
2967  av_dict_set(&oc->metadata, "duration", NULL, 0);
2968  av_dict_set(&oc->metadata, "creation_time", NULL, 0);
2969  av_dict_set(&oc->metadata, "company_name", NULL, 0);
2970  av_dict_set(&oc->metadata, "product_name", NULL, 0);
2971  av_dict_set(&oc->metadata, "product_version", NULL, 0);
2972  }
2973  if (!metadata_streams_manual)
2974  for (int i = 0; i < of->nb_streams; i++) {
2975  OutputStream *ost = of->streams[i];
2976 
2977  if (!ost->ist) /* this is true e.g. for attached files */
2978  continue;
2980  if (ost->enc_ctx) {
2981  av_dict_set(&ost->st->metadata, "encoder", NULL, 0);
2982  }
2983  }
2984 
2985  return 0;
2986 }
2987 
2988 static int set_dispositions(Muxer *mux, const OptionsContext *o)
2989 {
2990  OutputFile *of = &mux->of;
2991  AVFormatContext *ctx = mux->fc;
2992 
2993  // indexed by type+1, because AVMEDIA_TYPE_UNKNOWN=-1
2994  int nb_streams[AVMEDIA_TYPE_NB + 1] = { 0 };
2995  int have_default[AVMEDIA_TYPE_NB + 1] = { 0 };
2996  int have_manual = 0;
2997  int ret = 0;
2998 
2999  const char **dispositions;
3000 
3001  dispositions = av_calloc(ctx->nb_streams, sizeof(*dispositions));
3002  if (!dispositions)
3003  return AVERROR(ENOMEM);
3004 
3005  // first, copy the input dispositions
3006  for (int i = 0; i < ctx->nb_streams; i++) {
3007  OutputStream *ost = of->streams[i];
3008 
3009  nb_streams[ost->type + 1]++;
3010 
3011  opt_match_per_stream_str(ost, &o->disposition, ctx, ost->st, &dispositions[i]);
3012 
3013  have_manual |= !!dispositions[i];
3014 
3015  if (ost->ist) {
3016  ost->st->disposition = ost->ist->st->disposition;
3017 
3019  have_default[ost->type + 1] = 1;
3020  }
3021  }
3022 
3023  if (have_manual) {
3024  // process manually set dispositions - they override the above copy
3025  for (int i = 0; i < ctx->nb_streams; i++) {
3026  OutputStream *ost = of->streams[i];
3027  const char *disp = dispositions[i];
3028 
3029  if (!disp)
3030  continue;
3031 
3032  ret = av_opt_set(ost->st, "disposition", disp, 0);
3033  if (ret < 0)
3034  goto finish;
3035  }
3036  } else {
3037  // For each media type with more than one stream, find a suitable stream to
3038  // mark as default, unless one is already marked default.
3039  // "Suitable" means the first of that type, skipping attached pictures.
3040  for (int i = 0; i < ctx->nb_streams; i++) {
3041  OutputStream *ost = of->streams[i];
3042  enum AVMediaType type = ost->type;
3043 
3044  if (nb_streams[type + 1] < 2 || have_default[type + 1] ||
3046  continue;
3047 
3049  have_default[type + 1] = 1;
3050  }
3051  }
3052 
3053 finish:
3054  av_freep(&dispositions);
3055 
3056  return ret;
3057 }
3058 
3059 const char *const forced_keyframes_const_names[] = {
3060  "n",
3061  "n_forced",
3062  "prev_forced_n",
3063  "prev_forced_t",
3064  "t",
3065  NULL
3066 };
3067 
3068 static int compare_int64(const void *a, const void *b)
3069 {
3070  return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
3071 }
3072 
3074  const Muxer *mux, const char *spec)
3075 {
3076  const char *p;
3077  int n = 1, i, ret, size, index = 0;
3078  int64_t t, *pts;
3079 
3080  for (p = spec; *p; p++)
3081  if (*p == ',')
3082  n++;
3083  size = n;
3084  pts = av_malloc_array(size, sizeof(*pts));
3085  if (!pts)
3086  return AVERROR(ENOMEM);
3087 
3088  p = spec;
3089  for (i = 0; i < n; i++) {
3090  char *next = strchr(p, ',');
3091 
3092  if (next)
3093  *next++ = 0;
3094 
3095  if (strstr(p, "chapters") == p) {
3096  AVChapter * const *ch = mux->fc->chapters;
3097  unsigned int nb_ch = mux->fc->nb_chapters;
3098  int j;
3099 
3100  if (nb_ch > INT_MAX - size) {
3101  ret = AVERROR(ERANGE);
3102  goto fail;
3103  }
3104  size += nb_ch - 1;
3105  pts = av_realloc_f(pts, size, sizeof(*pts));
3106  if (!pts)
3107  return AVERROR(ENOMEM);
3108 
3109  if (p[8]) {
3110  ret = av_parse_time(&t, p + 8, 1);
3111  if (ret < 0) {
3113  "Invalid chapter time offset: %s\n", p + 8);
3114  goto fail;
3115  }
3116  } else
3117  t = 0;
3118 
3119  for (j = 0; j < nb_ch; j++) {
3120  const AVChapter *c = ch[j];
3121  av_assert1(index < size);
3122  pts[index++] = av_rescale_q(c->start, c->time_base,
3123  AV_TIME_BASE_Q) + t;
3124  }
3125 
3126  } else {
3127  av_assert1(index < size);
3128  ret = av_parse_time(&t, p, 1);
3129  if (ret < 0) {
3130  av_log(log, AV_LOG_ERROR, "Invalid keyframe time: %s\n", p);
3131  goto fail;
3132  }
3133 
3134  pts[index++] = t;
3135  }
3136 
3137  p = next;
3138  }
3139 
3140  av_assert0(index == size);
3141  qsort(pts, size, sizeof(*pts), compare_int64);
3142  kf->nb_pts = size;
3143  kf->pts = pts;
3144 
3145  return 0;
3146 fail:
3147  av_freep(&pts);
3148  return ret;
3149 }
3150 
3152 {
3153  for (int i = 0; i < mux->of.nb_streams; i++) {
3154  OutputStream *ost = mux->of.streams[i];
3155  const char *forced_keyframes = NULL;
3156 
3158  mux->fc, ost->st, &forced_keyframes);
3159 
3160  if (!(ost->type == AVMEDIA_TYPE_VIDEO &&
3161  ost->enc_ctx && forced_keyframes))
3162  continue;
3163 
3164  if (!strncmp(forced_keyframes, "expr:", 5)) {
3165  int ret = av_expr_parse(&ost->kf.pexpr, forced_keyframes + 5,
3167  if (ret < 0) {
3169  "Invalid force_key_frames expression '%s'\n", forced_keyframes + 5);
3170  return ret;
3171  }
3172  ost->kf.expr_const_values[FKF_N] = 0;
3173  ost->kf.expr_const_values[FKF_N_FORCED] = 0;
3174  ost->kf.expr_const_values[FKF_PREV_FORCED_N] = NAN;
3175  ost->kf.expr_const_values[FKF_PREV_FORCED_T] = NAN;
3176 
3177  // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
3178  // parse it only for static kf timings
3179  } else if (!strcmp(forced_keyframes, "source")) {
3180  ost->kf.type = KF_FORCE_SOURCE;
3181 #if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
3182  } else if (!strcmp(forced_keyframes, "source_no_drop")) {
3183  av_log(ost, AV_LOG_WARNING, "The 'source_no_drop' value for "
3184  "-force_key_frames is deprecated, use just 'source'\n");
3185  ost->kf.type = KF_FORCE_SOURCE;
3186 #endif
3187  } else {
3188  int ret = parse_forced_key_frames(ost, &ost->kf, mux, forced_keyframes);
3189  if (ret < 0)
3190  return ret;
3191  }
3192  }
3193 
3194  return 0;
3195 }
3196 
3197 static const char *output_file_item_name(void *obj)
3198 {
3199  const Muxer *mux = obj;
3200 
3201  return mux->log_name;
3202 }
3203 
3204 static const AVClass output_file_class = {
3205  .class_name = "OutputFile",
3206  .version = LIBAVUTIL_VERSION_INT,
3207  .item_name = output_file_item_name,
3208  .category = AV_CLASS_CATEGORY_MUXER,
3209 };
3210 
3211 static Muxer *mux_alloc(void)
3212 {
3213  Muxer *mux = allocate_array_elem(&output_files, sizeof(*mux), &nb_output_files);
3214 
3215  if (!mux)
3216  return NULL;
3217 
3218  mux->of.class = &output_file_class;
3219  mux->of.index = nb_output_files - 1;
3220 
3221  snprintf(mux->log_name, sizeof(mux->log_name), "out#%d", mux->of.index);
3222 
3223  return mux;
3224 }
3225 
3226 int of_open(const OptionsContext *o, const char *filename, Scheduler *sch)
3227 {
3228  Muxer *mux;
3229  AVFormatContext *oc;
3230  int err;
3231  OutputFile *of;
3232 
3233  int64_t recording_time = o->recording_time;
3234  int64_t stop_time = o->stop_time;
3235 
3236  mux = mux_alloc();
3237  if (!mux)
3238  return AVERROR(ENOMEM);
3239 
3240  of = &mux->of;
3241 
3242  if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
3243  stop_time = INT64_MAX;
3244  av_log(mux, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
3245  }
3246 
3247  if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
3249  if (stop_time <= start_time) {
3250  av_log(mux, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
3251  return AVERROR(EINVAL);
3252  } else {
3253  recording_time = stop_time - start_time;
3254  }
3255  }
3256 
3257  of->recording_time = recording_time;
3258  of->start_time = o->start_time;
3259 
3260  mux->limit_filesize = o->limit_filesize;
3261  av_dict_copy(&mux->opts, o->g->format_opts, 0);
3262 
3263  if (!strcmp(filename, "-"))
3264  filename = "pipe:";
3265 
3266  err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
3267  if (!oc) {
3268  av_log(mux, AV_LOG_FATAL, "Error initializing the muxer for %s: %s\n",
3269  filename, av_err2str(err));
3270  return err;
3271  }
3272  mux->fc = oc;
3273 
3274  av_strlcat(mux->log_name, "/", sizeof(mux->log_name));
3275  av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name));
3276 
3277 
3278  if (recording_time != INT64_MAX)
3279  oc->duration = recording_time;
3280 
3281  oc->interrupt_callback = int_cb;
3282 
3283  if (o->bitexact) {
3284  oc->flags |= AVFMT_FLAG_BITEXACT;
3285  of->bitexact = 1;
3286  } else {
3287  of->bitexact = check_opt_bitexact(oc, mux->opts, "fflags",
3289  }
3290 
3291  err = sch_add_mux(sch, muxer_thread, mux_check_init, mux,
3292  !strcmp(oc->oformat->name, "rtp"), o->thread_queue_size);
3293  if (err < 0)
3294  return err;
3295  mux->sch = sch;
3296  mux->sch_idx = err;
3297 
3298  /* create all output streams for this file */
3299  err = create_streams(mux, o);
3300  if (err < 0)
3301  return err;
3302 
3303  /* check if all codec options have been used */
3304  err = check_avoptions_used(o->g->codec_opts, mux->enc_opts_used, mux, 0);
3305  av_dict_free(&mux->enc_opts_used);
3306  if (err < 0)
3307  return err;
3308 
3309  /* check filename in case of an image number is expected */
3311  av_log(mux, AV_LOG_FATAL,
3312  "Output filename '%s' does not contain a numeric pattern like "
3313  "'%%d', which is required by output format '%s'.\n",
3314  oc->url, oc->oformat->name);
3315  return AVERROR(EINVAL);
3316  }
3317 
3318  if (!(oc->oformat->flags & AVFMT_NOFILE)) {
3319  /* test if it already exists to avoid losing precious files */
3320  err = assert_file_overwrite(filename);
3321  if (err < 0)
3322  return err;
3323 
3324  /* open the file */
3325  if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
3326  &oc->interrupt_callback,
3327  &mux->opts)) < 0) {
3328  av_log(mux, AV_LOG_FATAL, "Error opening output %s: %s\n",
3329  filename, av_err2str(err));
3330  return err;
3331  }
3332  } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename)) {
3333  err = assert_file_overwrite(filename);
3334  if (err < 0)
3335  return err;
3336  }
3337 
3338  if (o->mux_preload) {
3339  av_dict_set_int(&mux->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
3340  }
3341  oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
3342 
3343  /* copy metadata and chapters from input files */
3344  err = copy_meta(mux, o);
3345  if (err < 0)
3346  return err;
3347 
3348  err = of_add_groups(mux, o);
3349  if (err < 0)
3350  return err;
3351 
3352  err = of_add_programs(mux, o);
3353  if (err < 0)
3354  return err;
3355 
3356  err = of_add_metadata(of, oc, o);
3357  if (err < 0)
3358  return err;
3359 
3360  err = set_dispositions(mux, o);
3361  if (err < 0) {
3362  av_log(mux, AV_LOG_FATAL, "Error setting output stream dispositions\n");
3363  return err;
3364  }
3365 
3366  // parse forced keyframe specifications;
3367  // must be done after chapters are created
3368  err = process_forced_keyframes(mux, o);
3369  if (err < 0) {
3370  av_log(mux, AV_LOG_FATAL, "Error processing forced keyframes\n");
3371  return err;
3372  }
3373 
3375  o->shortest);
3376  if (err < 0) {
3377  av_log(mux, AV_LOG_FATAL, "Error setting up output sync queues\n");
3378  return err;
3379  }
3380 
3381  of->url = filename;
3382 
3383  /* initialize streamcopy streams. */
3384  for (int i = 0; i < of->nb_streams; i++) {
3385  OutputStream *ost = of->streams[i];
3386 
3387  if (!ost->enc) {
3388  err = of_stream_init(of, ost);
3389  if (err < 0)
3390  return err;
3391  }
3392  }
3393 
3394  return 0;
3395 }
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:605
choose_encoder
static int choose_encoder(const OptionsContext *o, AVFormatContext *s, OutputStream *ost, const AVCodec **enc)
Definition: ffmpeg_mux_init.c:69
formats
formats
Definition: signature.h:47
KeyframeForceCtx::pts
int64_t * pts
Definition: ffmpeg.h:568
MuxStream::ost
OutputStream ost
Definition: ffmpeg_mux.h:37
iamf.h
fopen_utf8
static FILE * fopen_utf8(const char *path, const char *mode)
Definition: fopen_utf8.h:66
map_manual
static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map)
Definition: ffmpeg_mux_init.c:1715
SYNC_QUEUE_PACKETS
@ SYNC_QUEUE_PACKETS
Definition: sync_queue.h:29
AVCodec
AVCodec.
Definition: codec.h:187
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AVIAMFSubmix::elements
AVIAMFSubmixElement ** elements
Array of submix elements.
Definition: iamf.h:561
av_codec_get_id
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
MuxStream::copy_initial_nonkeyframes
int copy_initial_nonkeyframes
Definition: ffmpeg_mux.h:75
AVStreamGroup::params
union AVStreamGroup::@363 params
Group type-specific parameters.
MuxStream::sch_idx_enc
int sch_idx_enc
Definition: ffmpeg_mux.h:50
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVIAMFAudioElement::nb_layers
unsigned int nb_layers
Number of layers, or channel groups, in the Audio Element.
Definition: iamf.h:368
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
InputFile::start_time
int64_t start_time
Definition: ffmpeg.h:488
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
Muxer::fc
AVFormatContext * fc
Definition: ffmpeg_mux.h:91
AVFormatContext::stream_groups
AVStreamGroup ** stream_groups
A list of all stream groups in the file.
Definition: avformat.h:1374
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
OptionsContext::stop_time
int64_t stop_time
Definition: ffmpeg.h:189
ost_get_filters
static int ost_get_filters(const OptionsContext *o, AVFormatContext *oc, OutputStream *ost, char **dst)
Definition: ffmpeg_mux_init.c:415
AVStreamGroup::id
int64_t id
Group type-specific group ID.
Definition: avformat.h:1140
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:443
program
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C program
Definition: undefined.txt:6
OutputFilter::graph
struct FilterGraph * graph
Definition: ffmpeg.h:355
FKF_PREV_FORCED_T
@ FKF_PREV_FORCED_T
Definition: ffmpeg.h:501
OptionsContext::force_fps
SpecifierOptList force_fps
Definition: ffmpeg.h:213
OptionsContext::forced_key_frames
SpecifierOptList forced_key_frames
Definition: ffmpeg.h:211
VSYNC_VFR
@ VSYNC_VFR
Definition: ffmpeg.h:69
mix
static int mix(int c0, int c1)
Definition: 4xm.c:716
ms_from_ost
static MuxStream * ms_from_ost(OutputStream *ost)
Definition: ffmpeg_mux.h:116
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVChapter::metadata
AVDictionary * metadata
Definition: avformat.h:1250
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
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
nb_input_files
int nb_input_files
Definition: ffmpeg.c:105
opt.h
of_serialize_options
static int of_serialize_options(Muxer *mux, void *obj, AVBPrint *bp)
Definition: ffmpeg_mux_init.c:2310
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:691
MuxStream::sch_idx
int sch_idx
Definition: ffmpeg_mux.h:49
AVSTREAM_EVENT_FLAG_NEW_PACKETS
#define AVSTREAM_EVENT_FLAG_NEW_PACKETS
Definition: avformat.h:904
check_avoptions
int check_avoptions(AVDictionary *m)
Definition: cmdutils.c:1527
ENC_STATS_PTS
@ ENC_STATS_PTS
Definition: ffmpeg.h:517
OutputFilter::apad
char * apad
Definition: ffmpeg.h:363
Muxer::sch_stream_idx
int * sch_stream_idx
Definition: ffmpeg_mux.h:97
ENC_STATS_FRAME_NUM_IN
@ ENC_STATS_FRAME_NUM_IN
Definition: ffmpeg.h:514
elements
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:565
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
FKF_PREV_FORCED_N
@ FKF_PREV_FORCED_N
Definition: ffmpeg.h:500
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
is
The official guide to swscale for confused that is
Definition: swscale.txt:28
AVFormatContext::nb_chapters
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1387
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
Muxer::nb_sch_stream_idx
int nb_sch_stream_idx
Definition: ffmpeg_mux.h:98
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
enc_open
int enc_open(void *opaque, const AVFrame *frame)
Definition: ffmpeg_enc.c:165
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
av_iamf_param_definition_alloc
AVIAMFParamDefinition * av_iamf_param_definition_alloc(enum AVIAMFParamDefinitionType type, unsigned int nb_subblocks, size_t *out_size)
Allocates memory for AVIAMFParamDefinition, plus an array of.
Definition: iamf.c:159
AV_DISPOSITION_ATTACHED_PIC
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:674
ENC_STATS_DTS
@ ENC_STATS_DTS
Definition: ffmpeg.h:521
sq_limit_frames
void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
Limit the number of output frames for stream with index stream_idx to max_frames.
Definition: sync_queue.c:649
pthread_mutex_init
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
AV_CODEC_CONFIG_SAMPLE_RATE
@ AV_CODEC_CONFIG_SAMPLE_RATE
int, terminated by 0
Definition: avcodec.h:2707
of_parse_iamf_audio_element_layers
static int of_parse_iamf_audio_element_layers(Muxer *mux, AVStreamGroup *stg, char *ptr)
Definition: ffmpeg_mux_init.c:2105
KeyframeForceCtx::nb_pts
int nb_pts
Definition: ffmpeg.h:569
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:224
SCH_DSTREAM
#define SCH_DSTREAM(file, stream)
Definition: ffmpeg_sched.h:111
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
int64_t
long long int64_t
Definition: coverity.c:34
InputStream::user_set_discard
int user_set_discard
Definition: ffmpeg.h:441
FFMPEG_OPT_ENC_TIME_BASE_NUM
#define FFMPEG_OPT_ENC_TIME_BASE_NUM
Definition: ffmpeg.h:56
OptionsContext::bits_per_raw_sample
SpecifierOptList bits_per_raw_sample
Definition: ffmpeg.h:250
ENC_STATS_AVG_BITRATE
@ ENC_STATS_AVG_BITRATE
Definition: ffmpeg.h:527
AVCodecContext::intra_matrix
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:980
OptionsContext::audio_ch_layouts
SpecifierOptList audio_ch_layouts
Definition: ffmpeg.h:153
OptionsContext::qscale
SpecifierOptList qscale
Definition: ffmpeg.h:210
ist_iter
InputStream * ist_iter(InputStream *prev)
Definition: ffmpeg.c:376
parse_and_set_vsync
int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global)
Definition: ffmpeg_opt.c:285
normalize.log
log
Definition: normalize.py:21
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition: avformat.h:621
OptionsContext::nb_attachments
int nb_attachments
Definition: ffmpeg.h:184
OutputFile::start_time
int64_t start_time
start time in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:667
avcodec_find_encoder
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: allcodecs.c:1002
InputFile::index
int index
Definition: ffmpeg.h:477
OptionsContext::presets
SpecifierOptList presets
Definition: ffmpeg.h:226
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
pixdesc.h
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1355
OptionsContext::mux_max_delay
float mux_max_delay
Definition: ffmpeg.h:192
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:390
subtitle_codec_name
static const char * subtitle_codec_name
Definition: ffplay.c:340
ENC_STATS_LITERAL
@ ENC_STATS_LITERAL
Definition: ffmpeg.h:510
OptionsContext::subtitle_disable
int subtitle_disable
Definition: ffmpeg.h:199
AV_CODEC_CONFIG_COLOR_RANGE
@ AV_CODEC_CONFIG_COLOR_RANGE
AVColorRange, terminated by AVCOL_RANGE_UNSPECIFIED.
Definition: avcodec.h:2710
OptionsContext::passlogfiles
SpecifierOptList passlogfiles
Definition: ffmpeg.h:238
AVOption
AVOption.
Definition: opt.h:429
OutputStream::index
int index
Definition: ffmpeg.h:595
b
#define b
Definition: input.c:41
FilterGraph::index
int index
Definition: ffmpeg.h:373
choose_pixel_fmt
static enum AVPixelFormat choose_pixel_fmt(const AVCodecContext *avctx, enum AVPixelFormat target)
Definition: ffmpeg_mux_init.c:514
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:837
AVChapter::start
int64_t start
Definition: avformat.h:1249
Muxer::of
OutputFile of
Definition: ffmpeg_mux.h:86
RcOverride::qscale
int qscale
Definition: avcodec.h:207
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
base
uint8_t base
Definition: vp3data.h:128
freeenv_utf8
static void freeenv_utf8(char *var)
Definition: getenv_utf8.h:72
OptionGroup::swr_opts
AVDictionary * swr_opts
Definition: cmdutils.h:346
AVFormatContext::programs
AVProgram ** programs
Definition: avformat.h:1488
AVIAMFParamDefinition
Parameters as defined in section 3.6.1 of IAMF.
Definition: iamf.h:193
OptionsContext::bitexact
int bitexact
Definition: ffmpeg.h:195
ViewSpecifier
Definition: ffmpeg.h:128
get_stream_group_index_from_id
static int64_t get_stream_group_index_from_id(Muxer *mux, int64_t id)
Definition: ffmpeg_mux_init.c:2351
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
video_disable
static int video_disable
Definition: ffplay.c:315
AVDictionary
Definition: dict.c:34
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:316
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AV_CODEC_CONFIG_PIX_FORMAT
@ AV_CODEC_CONFIG_PIX_FORMAT
AVPixelFormat, terminated by AV_PIX_FMT_NONE.
Definition: avcodec.h:2705
MuxStream::copy_prior_start
int copy_prior_start
Definition: ffmpeg_mux.h:76
OptionsContext::format
const char * format
Definition: ffmpeg.h:150
parse_forced_key_frames
static int parse_forced_key_frames(void *log, KeyframeForceCtx *kf, const Muxer *mux, const char *spec)
Definition: ffmpeg_mux_init.c:3073
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
enc_stats_init
static int enc_stats_init(OutputStream *ost, EncStats *es, int pre, const char *path, const char *fmt_spec)
Definition: ffmpeg_mux_init.c:245
ost_add
static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, InputStream *ist, OutputFilter *ofilter, const ViewSpecifier *vs, OutputStream **post)
Definition: ffmpeg_mux_init.c:1142
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
ost
static AVStream * ost
Definition: vaapi_transcode.c:42
parse_matrix_coeffs
static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str)
Definition: ffmpeg_mux_init.c:486
OptionsContext::frame_pix_fmts
SpecifierOptList frame_pix_fmts
Definition: ffmpeg.h:159
set_dispositions
static int set_dispositions(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:2988
av_opt_serialize
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
Definition: opt.c:2734
MuxStream::ts_copy_start
int64_t ts_copy_start
Definition: ffmpeg_mux.h:60
OutputStream::file
struct OutputFile * file
Definition: ffmpeg.h:593
AVOutputFormat::subtitle_codec
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:522
MuxStream::stream_duration_tb
AVRational stream_duration_tb
Definition: ffmpeg_mux.h:67
ENC_STATS_TIMEBASE_IN
@ ENC_STATS_TIMEBASE_IN
Definition: ffmpeg.h:516
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:338
get_preset_file_2
static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
Definition: ffmpeg_mux_init.c:125
ost_bind_filter
static int ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter, const OptionsContext *o, char *filters, AVRational enc_tb, enum VideoSyncMethod vsync_method, int keep_pix_fmt, int autoscale, int threads_manual, const ViewSpecifier *vs)
Definition: ffmpeg_mux_init.c:918
OutputFile::nb_streams
int nb_streams
Definition: ffmpeg.h:664
av_filename_number_test
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:114
av_expr_parse
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:710
Muxer
Definition: ffmpeg_mux.h:85
InputStream
Definition: ffmpeg.h:432
OptionsContext::chapters_input_file
int chapters_input_file
Definition: ffmpeg.h:186
AVPacketSideData::size
size_t size
Definition: packet.h:392
OutputFilterOptions
Definition: ffmpeg.h:300
EncStatsFile
Definition: ffmpeg_mux_init.c:155
AVFormatContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1560
map_auto_subtitle
static int map_auto_subtitle(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1653
OptionsContext::max_frame_rates
SpecifierOptList max_frame_rates
Definition: ffmpeg.h:157
AV_OPT_SERIALIZE_SEARCH_CHILDREN
#define AV_OPT_SERIALIZE_SEARCH_CHILDREN
Serialize options in possible children of the given object.
Definition: opt.h:1120
finish
static void finish(void)
Definition: movenc.c:374
fopen_utf8.h
av_iamf_mix_presentation_add_submix
AVIAMFSubmix * av_iamf_mix_presentation_add_submix(AVIAMFMixPresentation *mix_presentation)
Allocate a submix and add it to a given AVIAMFMixPresentation.
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
of_parse_group_token
static int of_parse_group_token(Muxer *mux, const char *token, char *ptr)
Definition: ffmpeg_mux_init.c:2458
OptionsContext::g
OptionGroup * g
Definition: ffmpeg.h:144
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
mux_alloc
static Muxer * mux_alloc(void)
Definition: ffmpeg_mux_init.c:3211
opt_match_per_stream_int
void opt_match_per_stream_int(void *logctx, const SpecifierOptList *sol, AVFormatContext *fc, AVStream *st, int *out)
OptionsContext::enc_stats_pre_fmt
SpecifierOptList enc_stats_pre_fmt
Definition: ffmpeg.h:254
fail
#define fail()
Definition: checkasm.h:188
OptionsContext::mux_stats_fmt
SpecifierOptList mux_stats_fmt
Definition: ffmpeg.h:256
AVIAMFSubmixLayout
Submix layout as defined in section 3.7.6 of IAMF.
Definition: iamf.h:510
sch_add_mux_stream
int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx)
Add a muxed stream for a previously added muxer.
Definition: ffmpeg_sched.c:656
copy_meta
static int copy_meta(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:2913
AV_BPRINT_SIZE_AUTOMATIC
#define AV_BPRINT_SIZE_AUTOMATIC
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
AVChapter
Definition: avformat.h:1246
val
static double val(void *priv, double ch)
Definition: aeval.c:77
SCH_ENC
#define SCH_ENC(encoder)
Definition: ffmpeg_sched.h:123
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
OptionsContext
Definition: ffmpeg.h:143
av_opt_set
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:748
av_new_program
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition: avformat.c:339
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:441
Muxer::sq_pkt
AVPacket * sq_pkt
Definition: ffmpeg_mux.h:111
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
av_codec_get_tag2
int av_codec_get_tag2(const struct AVCodecTag *const *tags, enum AVCodecID id, unsigned int *tag)
Get the codec tag for the given codec id.
AV_OPT_SERIALIZE_SKIP_DEFAULTS
#define AV_OPT_SERIALIZE_SKIP_DEFAULTS
Serialize options that are not set to default values only.
Definition: opt.h:1118
enc_stats_files
static EncStatsFile * enc_stats_files
Definition: ffmpeg_mux_init.c:160
new_stream_video
static int new_stream_video(Muxer *mux, const OptionsContext *o, OutputStream *ost, int *keep_pix_fmt, enum VideoSyncMethod *vsync_method)
Definition: ffmpeg_mux_init.c:594
AVRational::num
int num
Numerator.
Definition: rational.h:59
OutputFilter::bound
int bound
Definition: ffmpeg.h:360
InputFile
Definition: ffmpeg.h:474
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN
@ AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN
Subblocks are of struct type AVIAMFReconGain.
Definition: iamf.h:181
OptionsContext::recording_time
int64_t recording_time
Definition: ffmpeg.h:188
OptionsContext::nb_stream_maps
int nb_stream_maps
Definition: ffmpeg.h:182
OptionsContext::audio_disable
int audio_disable
Definition: ffmpeg.h:198
check_stream_specifier
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the given stream matches a stream specifier.
Definition: cmdutils.c:1334
preset
preset
Definition: vf_curves.c:47
avassert.h
RcOverride::quality_factor
float quality_factor
Definition: avcodec.h:208
MuxStream::log_name
char log_name[32]
Definition: ffmpeg_mux.h:40
opt_match_per_stream_int64
void opt_match_per_stream_int64(void *logctx, const SpecifierOptList *sol, AVFormatContext *fc, AVStream *st, int64_t *out)
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVFormatContext::metadata
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1522
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
OptionGroup::codec_opts
AVDictionary * codec_opts
Definition: cmdutils.h:343
av_dump_format
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate,...
Definition: dump.c:824
AVCodecTag
Definition: internal.h:42
ENC_STATS_PTS_IN
@ ENC_STATS_PTS_IN
Definition: ffmpeg.h:519
OptionsContext::metadata
SpecifierOptList metadata
Definition: ffmpeg.h:205
SpecifierOptList::nb_opt
int nb_opt
Definition: cmdutils.h:181
OptionsContext::filters
SpecifierOptList filters
Definition: ffmpeg.h:229
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
avformat_query_codec
int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
Test if the given container can store a codec.
Definition: mux_utils.c:33
init_simple_filtergraph
int init_simple_filtergraph(InputStream *ist, OutputStream *ost, char *graph_desc, Scheduler *sch, unsigned sch_idx_enc, const OutputFilterOptions *opts)
Definition: ffmpeg_filter.c:1185
av_iamf_submix_add_layout
AVIAMFSubmixLayout * av_iamf_submix_add_layout(AVIAMFSubmix *submix)
Allocate a submix layout and add it to a given AVIAMFSubmix.
AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
@ AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
Definition: avformat.h:1113
output_stream_class
static const AVClass output_stream_class
Definition: ffmpeg_mux_init.c:384
VSYNC_VSCFR
@ VSYNC_VSCFR
Definition: ffmpeg.h:70
EncStats::components
EncStatsComponent * components
Definition: ffmpeg.h:539
of_parse_iamf_submixes
static int of_parse_iamf_submixes(Muxer *mux, AVStreamGroup *stg, char *ptr)
Definition: ffmpeg_mux_init.c:2181
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
assert_file_overwrite
int assert_file_overwrite(const char *filename)
Definition: ffmpeg_opt.c:735
AVChapter::end
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1249
SpecifierOpt::specifier
char * specifier
Definition: cmdutils.h:165
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
mux_stream_alloc
static MuxStream * mux_stream_alloc(Muxer *mux, enum AVMediaType type)
Definition: ffmpeg_mux_init.c:391
AV_CODEC_CONFIG_SAMPLE_FORMAT
@ AV_CODEC_CONFIG_SAMPLE_FORMAT
AVSampleFormat, terminated by AV_SAMPLE_FMT_NONE.
Definition: avcodec.h:2708
intreadwrite.h
sch_add_mux
int sch_add_mux(Scheduler *sch, SchThreadFunc func, int(*init)(void *), void *arg, int sdp_auto, unsigned thread_queue_size)
Add a muxer to the scheduler.
Definition: ffmpeg_sched.c:632
OptionsContext::intra_matrices
SpecifierOptList intra_matrices
Definition: ffmpeg.h:219
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:1352
MuxStream::pkt
AVPacket * pkt
Definition: ffmpeg_mux.h:45
OptionsContext::stream_groups
SpecifierOptList stream_groups
Definition: ffmpeg.h:246
FilterGraph::outputs
OutputFilter ** outputs
Definition: ffmpeg.h:377
enc_stats_get_file
static int enc_stats_get_file(AVIOContext **io, const char *path)
Definition: ffmpeg_mux_init.c:163
of_add_groups
static int of_add_groups(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:2600
InputStream::framerate
AVRational framerate
Definition: ffmpeg.h:453
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1438
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:144
output_file_class
static const AVClass output_file_class
Definition: ffmpeg_mux_init.c:3204
AVFormatContext::nb_programs
unsigned int nb_programs
Definition: avformat.h:1487
RcOverride
Definition: avcodec.h:204
AVFormatContext::chapters
AVChapter ** chapters
Definition: avformat.h:1388
ENC_STATS_FILE_IDX
@ ENC_STATS_FILE_IDX
Definition: ffmpeg.h:511
AVDictionaryEntry::key
char * key
Definition: dict.h:90
frame_size
int frame_size
Definition: mxfenc.c:2424
OptionsContext::limit_filesize
int64_t limit_filesize
Definition: ffmpeg.h:190
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
av_iamf_param_definition_get_subblock
static av_always_inline void * av_iamf_param_definition_get_subblock(const AVIAMFParamDefinition *par, unsigned int idx)
Get the subblock at the specified.
Definition: iamf.h:260
VIEW_SPECIFIER_TYPE_NONE
@ VIEW_SPECIFIER_TYPE_NONE
Definition: ffmpeg.h:117
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:116
OptionsContext::autoscale
SpecifierOptList autoscale
Definition: ffmpeg.h:249
OutputFilter::linklabel
uint8_t * linklabel
Definition: ffmpeg.h:361
av_strtok
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:178
get_line
static char * get_line(AVIOContext *s, AVBPrint *bprint)
Definition: ffmpeg_mux_init.c:112
filters
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
Definition: af_crystalizer.c:55
ENC_STATS_BITRATE
@ ENC_STATS_BITRATE
Definition: ffmpeg.h:526
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVMEDIA_TYPE_NB
@ AVMEDIA_TYPE_NB
Definition: avutil.h:206
output_stream_item_name
static const char * output_stream_item_name(void *obj)
Definition: ffmpeg_mux_init.c:377
of_add_metadata
static int of_add_metadata(OutputFile *of, AVFormatContext *oc, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:2726
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:618
OptionsContext::sample_fmts
SpecifierOptList sample_fmts
Definition: ffmpeg.h:209
AVStreamGroup::index
unsigned int index
Group index in AVFormatContext.
Definition: avformat.h:1132
AVPacketSideData::data
uint8_t * data
Definition: packet.h:391
ignore_unknown_streams
int ignore_unknown_streams
Definition: ffmpeg_opt.c:85
ctx
AVFormatContext * ctx
Definition: movenc.c:49
RcOverride::start_frame
int start_frame
Definition: avcodec.h:205
channels
channels
Definition: aptx.h:31
OFILTER_FLAG_AUTOSCALE
@ OFILTER_FLAG_AUTOSCALE
Definition: ffmpeg.h:297
nb_streams
static int nb_streams
Definition: ffprobe.c:384
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:556
encoder_thread
int encoder_thread(void *arg)
Definition: ffmpeg_enc.c:841
OptionsContext::shortest
int shortest
Definition: ffmpeg.h:194
AVOutputFormat::codec_tag
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:535
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:394
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:86
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
NAN
#define NAN
Definition: mathematics.h:115
MuxStream::max_frames
int64_t max_frames
Definition: ffmpeg_mux.h:55
AVFMT_NEEDNUMBER
#define AVFMT_NEEDNUMBER
Needs 'd' in filename.
Definition: avformat.h:469
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
Muxer::limit_filesize
int64_t limit_filesize
Definition: ffmpeg_mux.h:106
arg
const char * arg
Definition: jacosubdec.c:67
AVCodecDescriptor::props
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: codec_desc.h:54
if
if(ret)
Definition: filter_design.txt:179
sq_add_stream
int sq_add_stream(SyncQueue *sq, int limiting)
Add a new stream to the sync queue.
Definition: sync_queue.c:620
OptionsContext::start_time
int64_t start_time
Definition: ffmpeg.h:147
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:221
AVFormatContext
Format I/O context.
Definition: avformat.h:1287
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:32
ENC_STATS_KEYFRAME
@ ENC_STATS_KEYFRAME
Definition: ffmpeg.h:528
OptionGroup::format_opts
AVDictionary * format_opts
Definition: cmdutils.h:344
opts
AVDictionary * opts
Definition: movenc.c:51
new_stream_subtitle
static int new_stream_subtitle(Muxer *mux, const OptionsContext *o, OutputStream *ost)
Definition: ffmpeg_mux_init.c:876
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
OutputFilter::name
uint8_t * name
Definition: ffmpeg.h:356
parse_meta_type
static int parse_meta_type(void *logctx, const char *arg, char *type, int *index, const char **stream_spec)
Parse a metadata specifier passed as 'arg' parameter.
Definition: ffmpeg_mux_init.c:2696
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:787
NULL
#define NULL
Definition: coverity.c:32
av_program_add_stream_index
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition: avformat.c:370
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:701
InputStream::st
AVStream * st
Definition: ffmpeg.h:440
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
AV_CODEC_CONFIG_FRAME_RATE
@ AV_CODEC_CONFIG_FRAME_RATE
AVRational, terminated by {0, 0}.
Definition: avcodec.h:2706
MuxStream::sch_idx_src
int sch_idx_src
Definition: ffmpeg_mux.h:51
OptionsContext::audio_channels
SpecifierOptList audio_channels
Definition: ffmpeg.h:154
map_auto_video
static int map_auto_video(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1556
nb_enc_stats_files
static int nb_enc_stats_files
Definition: ffmpeg_mux_init.c:161
sch_add_enc
int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx, int(*open_cb)(void *opaque, const AVFrame *frame))
Definition: ffmpeg_sched.c:791
ENC_STATS_PTS_TIME
@ ENC_STATS_PTS_TIME
Definition: ffmpeg.h:518
OptionsContext::fix_sub_duration_heartbeat
SpecifierOptList fix_sub_duration_heartbeat
Definition: ffmpeg.h:235
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
OFILTER_FLAG_AUDIO_24BIT
@ OFILTER_FLAG_AUDIO_24BIT
Definition: ffmpeg.h:296
EncStats::lock
pthread_mutex_t lock
Definition: ffmpeg.h:544
OptionsContext::copy_prior_start
SpecifierOptList copy_prior_start
Definition: ffmpeg.h:228
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:393
filter_codec_opts
int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, const AVCodec *codec, AVDictionary **dst, AVDictionary **opts_used)
Filter out options for given codec.
Definition: cmdutils.c:1348
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
streamcopy_init
static int streamcopy_init(const Muxer *mux, OutputStream *ost, AVDictionary **encoder_opts)
Definition: ffmpeg_mux_init.c:1025
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1329
AV_CODEC_PROP_BITMAP_SUB
#define AV_CODEC_PROP_BITMAP_SUB
Subtitle codec is bitmap based Decoded AVSubtitle data can be read from the AVSubtitleRect->pict fiel...
Definition: codec_desc.h:103
parseutils.h
AVIAMFLayer
A layer defining a Channel Layout in the Audio Element.
Definition: iamf.h:294
EncStats
Definition: ffmpeg.h:538
OptionsContext::program
SpecifierOptList program
Definition: ffmpeg.h:245
EncStatsFile::io
AVIOContext * io
Definition: ffmpeg_mux_init.c:157
getenv_utf8
static char * getenv_utf8(const char *varname)
Definition: getenv_utf8.h:67
copy_unknown_streams
int copy_unknown_streams
Definition: ffmpeg_opt.c:86
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:828
OptionsContext::frame_aspect_ratios
SpecifierOptList frame_aspect_ratios
Definition: ffmpeg.h:214
MuxStream::bsf_ctx
AVBSFContext * bsf_ctx
Definition: ffmpeg_mux.h:42
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:128
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:589
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
av_parse_ratio
int av_parse_ratio(AVRational *q, const char *str, int max, int log_offset, void *log_ctx)
Parse str and store the parsed ratio in q.
Definition: parseutils.c:45
OptionsContext::max_frames
SpecifierOptList max_frames
Definition: ffmpeg.h:206
Muxer::log_name
char log_name[32]
Definition: ffmpeg_mux.h:89
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
OutputFile::index
int index
Definition: ffmpeg.h:659
OutputFile::class
const AVClass * class
Definition: ffmpeg.h:657
FFMPEG_OPT_FILTER_SCRIPT
#define FFMPEG_OPT_FILTER_SCRIPT
Definition: ffmpeg.h:61
ENC_STATS_PTS_TIME_IN
@ ENC_STATS_PTS_TIME_IN
Definition: ffmpeg.h:520
FilterGraph::nb_outputs
int nb_outputs
Definition: ffmpeg.h:378
copy_metadata
static int copy_metadata(Muxer *mux, AVFormatContext *ic, const char *outspec, const char *inspec, int *metadata_global_manual, int *metadata_streams_manual, int *metadata_chapters_manual)
Definition: ffmpeg_mux_init.c:2826
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:352
index
int index
Definition: gxfenc.c:90
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
find_codec
int find_codec(void *logctx, const char *name, enum AVMediaType type, int encoder, const AVCodec **codec)
Definition: ffmpeg_opt.c:702
InputStream::par
AVCodecParameters * par
Codec parameters - to be used by the decoding/streamcopy code.
Definition: ffmpeg.h:448
input_files
InputFile ** input_files
Definition: ffmpeg.c:104
OutputFile::streams
OutputStream ** streams
Definition: ffmpeg.h:663
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
Scheduler
Definition: ffmpeg_sched.c:275
AVIAMFSubmixElement::audio_element_id
unsigned int audio_element_id
The id of the Audio Element this submix element references.
Definition: iamf.h:452
avformat_stream_group_add_stream
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
Add an already allocated stream to a stream group.
Definition: options.c:525
FilterGraph
Definition: ffmpeg.h:371
AVIAMFSubmix
Submix layout as defined in section 3.7 of IAMF.
Definition: iamf.h:552
file_read
char * file_read(const char *filename)
Definition: cmdutils.c:1493
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1343
ENC_TIME_BASE_DEMUX
@ ENC_TIME_BASE_DEMUX
Definition: ffmpeg.h:77
of_add_programs
static int of_add_programs(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:2627
AVOutputFormat::flags
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:529
avcodec_get_supported_config
int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec, enum AVCodecConfig config, unsigned flags, const void **out, int *out_num)
Retrieve a list of all supported values for a given configuration type.
Definition: avcodec.c:796
AV_CODEC_CONFIG_CHANNEL_LAYOUT
@ AV_CODEC_CONFIG_CHANNEL_LAYOUT
AVChannelLayout, terminated by {0}.
Definition: avcodec.h:2709
VideoSyncMethod
VideoSyncMethod
Definition: ffmpeg.h:65
av_get_exact_bits_per_sample
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:457
av_opt_find
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition: opt.c:1957
OptionsContext::apad
SpecifierOptList apad
Definition: ffmpeg.h:242
OptionsContext::enc_stats_post_fmt
SpecifierOptList enc_stats_post_fmt
Definition: ffmpeg.h:255
OutputStream::filter
OutputFilter * filter
Definition: ffmpeg.h:632
f
f
Definition: af_crystalizer.c:122
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVCodecContext::rc_override
RcOverride * rc_override
Definition: avcodec.h:1295
OptionsContext::thread_queue_size
int thread_queue_size
Definition: ffmpeg.h:168
AVMediaType
AVMediaType
Definition: avutil.h:199
Muxer::sq_mux
SyncQueue * sq_mux
Definition: ffmpeg_mux.h:110
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
MuxStream::apad
const char * apad
Definition: ffmpeg_mux.h:82
OptionsContext::enc_stats_pre
SpecifierOptList enc_stats_pre
Definition: ffmpeg.h:251
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
output_files
OutputFile ** output_files
Definition: ffmpeg.c:107
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
start_time
static int64_t start_time
Definition: ffplay.c:326
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1403
EncStatsType
EncStatsType
Definition: ffmpeg.h:509
Muxer::sch
Scheduler * sch
Definition: ffmpeg_mux.h:93
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1063
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
StreamMap
Definition: ffmpeg.h:134
ENC_STATS_NB_SAMPLES
@ ENC_STATS_NB_SAMPLES
Definition: ffmpeg.h:524
size
int size
Definition: twinvq_data.h:10344
copy_ts
int copy_ts
Definition: ffmpeg_opt.c:67
avio.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVStreamGroup::iamf_audio_element
struct AVIAMFAudioElement * iamf_audio_element
Definition: avformat.h:1154
AVStream::event_flags
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:891
OptionsContext::streamid
AVDictionary * streamid
Definition: ffmpeg.h:203
OptionsContext::pass
SpecifierOptList pass
Definition: ffmpeg.h:237
OutputStream::type
enum AVMediaType type
Definition: ffmpeg.h:590
OutputFile::url
const char * url
Definition: ffmpeg.h:661
setup_sync_queues
static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_us, int shortest)
Definition: ffmpeg_mux_init.c:2004
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
AVCodecContext::chroma_intra_matrix
uint16_t * chroma_intra_matrix
custom intra quantization matrix
Definition: avcodec.h:996
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
Muxer::opts
AVDictionary * opts
Definition: ffmpeg_mux.h:100
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:826
OptionsContext::disposition
SpecifierOptList disposition
Definition: ffmpeg.h:244
AVFMT_NOSTREAMS
#define AVFMT_NOSTREAMS
Format does not require any streams.
Definition: avformat.h:484
allocate_array_elem
void * allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
Atomically add a new element to an array of pointers, i.e.
Definition: cmdutils.c:1465
of_enc_stats_close
void of_enc_stats_close(void)
Definition: ffmpeg_mux_init.c:196
MuxStream
Definition: ffmpeg_mux.h:36
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:314
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
getenv_utf8.h
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
av_iamf_submix_add_element
AVIAMFSubmixElement * av_iamf_submix_add_element(AVIAMFSubmix *submix)
Allocate a submix element and add it to a given AVIAMFSubmix.
AVIAMFAudioElement
Information on how to combine one or more audio streams, as defined in section 3.6 of IAMF.
Definition: iamf.h:356
SpecifierOptList::opt
SpecifierOpt * opt
Definition: cmdutils.h:180
opt_match_per_stream_dbl
void opt_match_per_stream_dbl(void *logctx, const SpecifierOptList *sol, AVFormatContext *fc, AVStream *st, double *out)
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
@ AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
Definition: avformat.h:1112
AVStreamGroup::streams
AVStream ** streams
A list of streams in the group.
Definition: avformat.h:1188
av_parse_video_size
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:150
Muxer::sch_idx
unsigned sch_idx
Definition: ffmpeg_mux.h:94
ENC_STATS_FRAME_NUM
@ ENC_STATS_FRAME_NUM
Definition: ffmpeg.h:513
KeyframeForceCtx
Definition: ffmpeg.h:562
OutputFilter::type
enum AVMediaType type
Definition: ffmpeg.h:365
AVStreamGroup::iamf_mix_presentation
struct AVIAMFMixPresentation * iamf_mix_presentation
Definition: avformat.h:1155
OptionsContext::chroma_intra_matrices
SpecifierOptList chroma_intra_matrices
Definition: ffmpeg.h:221
mux_check_init
int mux_check_init(void *arg)
Definition: ffmpeg_mux.c:555
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
layout
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 layout
Definition: filter_design.txt:18
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:56
flag
#define flag(name)
Definition: cbs_av1.c:474
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:409
av_parse_video_rate
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:181
of_open
int of_open(const OptionsContext *o, const char *filename, Scheduler *sch)
Definition: ffmpeg_mux_init.c:3226
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:307
bprint.h
map_auto_data
static int map_auto_data(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1692
AV_STREAM_GROUP_PARAMS_NONE
@ AV_STREAM_GROUP_PARAMS_NONE
Definition: avformat.h:1111
log.h
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:478
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
RcOverride::end_frame
int end_frame
Definition: avcodec.h:206
OptionsContext::frame_rates
SpecifierOptList frame_rates
Definition: ffmpeg.h:156
AVChapter::id
int64_t id
unique ID to identify the chapter
Definition: avformat.h:1247
OptionsContext::codec_names
SpecifierOptList codec_names
Definition: ffmpeg.h:152
MuxStream::stats
EncStats stats
Definition: ffmpeg_mux.h:47
OptionsContext::stream_maps
StreamMap * stream_maps
Definition: ffmpeg.h:181
av_opt_set_dict2
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
Definition: opt.c:1928
ofilter_bind_ost
int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost, unsigned sched_idx_enc, const OutputFilterOptions *opts)
Definition: ffmpeg_filter.c:786
pix_fmt_parse
static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name)
Definition: ffmpeg_mux_init.c:545
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_get_sample_fmt
enum AVSampleFormat av_get_sample_fmt(const char *name)
Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE on error.
Definition: samplefmt.c:58
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
OptionsContext::fps_mode
SpecifierOptList fps_mode
Definition: ffmpeg.h:212
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
VSYNC_CFR
@ VSYNC_CFR
Definition: ffmpeg.h:68
OptionsContext::shortest_buf_duration
float shortest_buf_duration
Definition: ffmpeg.h:193
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AVProgram::metadata
AVDictionary * metadata
Definition: avformat.h:1217
OutputFile::bitexact
int bitexact
Definition: ffmpeg.h:669
display.h
SCH_DEC_OUT
#define SCH_DEC_OUT(decoder, out_idx)
Definition: ffmpeg_sched.h:120
AVIAMFMixPresentation
Information on how to render and mix one or more AVIAMFAudioElement to generate the final audio outpu...
Definition: iamf.h:609
OptionsContext::mux_stats
SpecifierOptList mux_stats
Definition: ffmpeg.h:253
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
OptionsContext::muxing_queue_data_threshold
SpecifierOptList muxing_queue_data_threshold
Definition: ffmpeg.h:240
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
InputFile::ctx
AVFormatContext * ctx
Definition: ffmpeg.h:479
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1432
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:256
AVProgram
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1211
OptionsContext::enc_stats_post
SpecifierOptList enc_stats_post
Definition: ffmpeg.h:252
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
len
int len
Definition: vorbis_enc_data.h:426
ENC_STATS_STREAM_IDX
@ ENC_STATS_STREAM_IDX
Definition: ffmpeg.h:512
filtergraphs
FilterGraph ** filtergraphs
Definition: ffmpeg.c:110
int_cb
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:307
AVCodecContext::height
int height
Definition: avcodec.h:624
fmt_in_list
static int fmt_in_list(const int *formats, int format)
Definition: ffmpeg_mux_init.c:505
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:81
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
ENC_STATS_SAMPLE_NUM
@ ENC_STATS_SAMPLE_NUM
Definition: ffmpeg.h:523
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
nb_output_files
int nb_output_files
Definition: ffmpeg.c:108
AVIAMFParamDefinition::nb_subblocks
unsigned int nb_subblocks
Number of subblocks in the array.
Definition: iamf.h:208
sch_connect
int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst)
Definition: ffmpeg_sched.c:927
avcodec.h
OptionGroup::sws_dict
AVDictionary * sws_dict
Definition: cmdutils.h:345
av_opt_eval_flags
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:817
tag
uint32_t tag
Definition: movenc.c:1879
OptionsContext::metadata_map
SpecifierOptList metadata_map
Definition: ffmpeg.h:225
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:760
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1455
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
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:71
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1306
av_strlcat
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition: avstring.c:95
VSYNC_DROP
@ VSYNC_DROP
Definition: ffmpeg.h:72
output_file_item_name
static const char * output_file_item_name(void *obj)
Definition: ffmpeg_mux_init.c:3197
av_opt_eval_int
int av_opt_eval_int(void *obj, const AVOption *o, const char *val, int *int_out)
AV_CODEC_PROP_TEXT_SUB
#define AV_CODEC_PROP_TEXT_SUB
Subtitle codec is text based.
Definition: codec_desc.h:108
InputFile::streams
InputStream ** streams
Definition: ffmpeg.h:493
avformat.h
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
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: packet.c:706
check_avoptions_used
int check_avoptions_used(const AVDictionary *opts, const AVDictionary *opts_used, void *logctx, int decode)
Definition: ffmpeg.c:477
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
id
enum AVCodecID id
Definition: dts2pts.c:367
av_guess_codec
enum AVCodecID av_guess_codec(const AVOutputFormat *fmt, const char *short_name, const char *filename, const char *mime_type, enum AVMediaType type)
Guess the codec ID based upon muxer and filename.
Definition: format.c:115
SCH_MSTREAM
#define SCH_MSTREAM(file, stream)
Definition: ffmpeg_sched.h:114
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2897
OptionsContext::max_muxing_queue_size
SpecifierOptList max_muxing_queue_size
Definition: ffmpeg.h:239
AVStreamGroup
Definition: avformat.h:1121
of_add_attachments
static int of_add_attachments(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1800
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:451
OptionsContext::inter_matrices
SpecifierOptList inter_matrices
Definition: ffmpeg.h:220
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:754
AV_CLASS_CATEGORY_MUXER
@ AV_CLASS_CATEGORY_MUXER
Definition: log.h:32
av_find_best_pix_fmt_of_2
enum AVPixelFormat av_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
Compute what kind of losses will occur when converting from one specific pixel format to another.
Definition: pixdesc.c:3244
AVStreamGroup::nb_streams
unsigned int nb_streams
Number of elements in AVStreamGroup.streams.
Definition: avformat.h:1175
OptionsContext::audio_sample_rate
SpecifierOptList audio_sample_rate
Definition: ffmpeg.h:155
OptionsContext::mux_preload
float mux_preload
Definition: ffmpeg.h:191
ist_output_add
int ist_output_add(InputStream *ist, OutputStream *ost)
Definition: ffmpeg_demux.c:987
AVRational::den
int den
Denominator.
Definition: rational.h:60
InputStream::file
struct InputFile * file
Definition: ffmpeg.h:436
SpecifierOpt::str
uint8_t * str
Definition: cmdutils.h:170
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
av_dict_parse_string
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:200
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
OptionsContext::frame_sizes
SpecifierOptList frame_sizes
Definition: ffmpeg.h:158
OptionsContext::video_disable
int video_disable
Definition: ffmpeg.h:197
AVOutputFormat::video_codec
enum AVCodecID video_codec
default video codec
Definition: avformat.h:521
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:914
AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN
@ AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN
Subblocks are of struct type AVIAMFMixGain.
Definition: iamf.h:173
EncStats::lock_initialized
int lock_initialized
Definition: ffmpeg.h:545
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1422
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
video_sync_method
enum VideoSyncMethod video_sync_method
Definition: ffmpeg_opt.c:60
GROW_ARRAY
#define GROW_ARRAY(array, nb_elems)
Definition: cmdutils.h:532
InputFile::ts_offset
int64_t ts_offset
Definition: ffmpeg.h:486
av_iamf_audio_element_add_layer
AVIAMFLayer * av_iamf_audio_element_add_layer(AVIAMFAudioElement *audio_element)
Allocate a layer and add it to a given AVIAMFAudioElement.
VSYNC_AUTO
@ VSYNC_AUTO
Definition: ffmpeg.h:66
OutputFilter
Definition: ffmpeg.h:352
map_auto_audio
static int map_auto_audio(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1609
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:167
OptionsContext::codec_tags
SpecifierOptList codec_tags
Definition: ffmpeg.h:208
AVIAMFSubmix::nb_elements
unsigned int nb_elements
Number of elements in the submix.
Definition: iamf.h:568
OptionsContext::filter_scripts
SpecifierOptList filter_scripts
Definition: ffmpeg.h:231
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
desc
const char * desc
Definition: libsvtav1.c:79
OptionsContext::time_bases
SpecifierOptList time_bases
Definition: ffmpeg.h:247
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVERROR_ENCODER_NOT_FOUND
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
Definition: error.h:56
av_bsf_list_parse_str
int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
Parse string describing list of bitstream filters and create single AVBSFContext describing the whole...
Definition: bsf.c:526
unescape
static int unescape(char **pdst, size_t *dst_len, const char **pstr, char delim)
Definition: ffmpeg_mux_init.c:206
avutil.h
AVIAMFAudioElement::demixing_info
AVIAMFParamDefinition * demixing_info
Demixing information used to reconstruct a scalable channel audio representation.
Definition: iamf.h:376
sch_sq_add_enc
int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx, int limiting, uint64_t max_frames)
Definition: ffmpeg_sched.c:896
mem.h
AVIAMFSubmix::output_mix_config
AVIAMFParamDefinition * output_mix_config
Information required for post-processing the mixed audio signal to generate the audio signal for play...
Definition: iamf.h:591
MuxStream::sq_idx_mux
int sq_idx_mux
Definition: ffmpeg_mux.h:53
AVStreamGroup::type
enum AVStreamGroupParamsType type
Group type.
Definition: avformat.h:1148
SpecifierOpt::u
union SpecifierOpt::@0 u
avio_open2
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: avio.c:491
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
ffmpeg_mux.h
OptionsContext::rc_overrides
SpecifierOptList rc_overrides
Definition: ffmpeg.h:218
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
new_stream_audio
static int new_stream_audio(Muxer *mux, const OptionsContext *o, OutputStream *ost)
Definition: ffmpeg_mux_init.c:837
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
sch_mux_sub_heartbeat_add
int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, unsigned dec_idx)
Definition: ffmpeg_sched.c:1216
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
avformat_stream_group_create
AVStreamGroup * avformat_stream_group_create(AVFormatContext *s, enum AVStreamGroupParamsType type, AVDictionary **options)
Add a new empty stream group to a media file.
Definition: options.c:445
InputStream::index
int index
Definition: ffmpeg.h:438
of_map_group
static int of_map_group(Muxer *mux, AVDictionary **dict, AVBPrint *bp, const char *map)
Definition: ffmpeg_mux_init.c:2362
AVFormatContext::nb_stream_groups
unsigned int nb_stream_groups
Number of elements in AVFormatContext.stream_groups.
Definition: avformat.h:1362
ffmpeg_sched.h
EncStatsFile::path
char * path
Definition: ffmpeg_mux_init.c:156
compare_int64
static int compare_int64(const void *a, const void *b)
Definition: ffmpeg_mux_init.c:3068
OptionsContext::copy_initial_nonkeyframes
SpecifierOptList copy_initial_nonkeyframes
Definition: ffmpeg.h:227
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FKF_N_FORCED
@ FKF_N_FORCED
Definition: ffmpeg.h:499
AVDictionaryEntry
Definition: dict.h:89
OptionsContext::attachments
const char ** attachments
Definition: ffmpeg.h:183
ENC_TIME_BASE_FILTER
@ ENC_TIME_BASE_FILTER
Definition: ffmpeg.h:78
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
audio_disable
static int audio_disable
Definition: ffplay.c:314
EncStatsComponent
Definition: ffmpeg.h:531
avio_closep
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: avio.c:649
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
OFILTER_FLAG_DISABLE_CONVERT
@ OFILTER_FLAG_DISABLE_CONVERT
Definition: ffmpeg.h:294
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:237
AVCodecContext::inter_matrix
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:989
cmdutils.h
Muxer::enc_opts_used
AVDictionary * enc_opts_used
Definition: ffmpeg_mux.h:103
AVCodecContext::rc_override_count
int rc_override_count
ratecontrol override, see RcOverride
Definition: avcodec.h:1294
InputFile::input_ts_offset
int64_t input_ts_offset
Definition: ffmpeg.h:480
EncStats::nb_components
int nb_components
Definition: ffmpeg.h:540
OptionsContext::data_disable
int data_disable
Definition: ffmpeg.h:200
nb_filtergraphs
int nb_filtergraphs
Definition: ffmpeg.c:111
AVIAMFSubmixElement::element_mix_config
AVIAMFParamDefinition * element_mix_config
Information required required for applying any processing to the referenced and rendered Audio Elemen...
Definition: iamf.h:461
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
muxer_thread
int muxer_thread(void *arg)
Definition: ffmpeg_mux.c:407
enc_alloc
int enc_alloc(Encoder **penc, const AVCodec *codec, Scheduler *sch, unsigned sch_idx)
Definition: ffmpeg_enc.c:71
AVIAMFSubmixElement
Submix element as defined in section 3.7 of IAMF.
Definition: iamf.h:446
ENC_STATS_PKT_SIZE
@ ENC_STATS_PKT_SIZE
Definition: ffmpeg.h:525
OutputStream
Definition: mux.c:53
check_opt_bitexact
static int check_opt_bitexact(void *ctx, const AVDictionary *opts, const char *opt_name, int flag)
Definition: ffmpeg_mux_init.c:53
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:1160
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
forced_keyframes_const_names
const char *const forced_keyframes_const_names[]
Definition: ffmpeg_mux_init.c:3059
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
process_forced_keyframes
static int process_forced_keyframes(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:3151
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
avcodec_descriptor_get
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3741
AVIAMFAudioElement::recon_gain_info
AVIAMFParamDefinition * recon_gain_info
Recon gain information used to reconstruct a scalable channel audio representation.
Definition: iamf.h:383
sq_alloc
SyncQueue * sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
Allocate a sync queue of the given type.
Definition: sync_queue.c:675
av_opt_set_dict
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1952
AVDictionaryEntry::value
char * value
Definition: dict.h:91
avstring.h
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
opt_match_per_type_str
const char * opt_match_per_type_str(const SpecifierOptList *sol, char mediatype)
Definition: ffmpeg_opt.c:155
opt_match_per_stream_str
void opt_match_per_stream_str(void *logctx, const SpecifierOptList *sol, AVFormatContext *fc, AVStream *st, const char **out)
sch_mux_stream_buffering
void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, size_t data_threshold, int max_packets)
Configure limits on packet buffering performed before the muxer task is started.
Definition: ffmpeg_sched.c:1175
InputFile::nb_streams
int nb_streams
Definition: ffmpeg.h:494
FKF_N
@ FKF_N
Definition: ffmpeg.h:498
avformat_alloc_output_context2
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:94
ENC_STATS_DTS_TIME
@ ENC_STATS_DTS_TIME
Definition: ffmpeg.h:522
AVChapter::time_base
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1248
OutputFile::recording_time
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
Definition: ffmpeg.h:666
of_stream_init
int of_stream_init(OutputFile *of, OutputStream *ost)
Definition: ffmpeg_mux.c:611
VSYNC_PASSTHROUGH
@ VSYNC_PASSTHROUGH
Definition: ffmpeg.h:67
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
snprintf
#define snprintf
Definition: snprintf.h:34
EncStats::io
AVIOContext * io
Definition: ffmpeg.h:542
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:44
OptionsContext::bitstream_filters
SpecifierOptList bitstream_filters
Definition: ffmpeg.h:207
src
#define src
Definition: vp8dsp.c:248
copy_chapters
static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os, int copy_metadata)
Definition: ffmpeg_mux_init.c:2784
MuxStream::last_mux_dts
int64_t last_mux_dts
Definition: ffmpeg_mux.h:64
OptionsContext::enc_time_bases
SpecifierOptList enc_time_bases
Definition: ffmpeg.h:248
av_opt_is_set_to_default_by_name
int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
Check if given option is set to its default value.
Definition: opt.c:2679
ENC_STATS_TIMEBASE
@ ENC_STATS_TIMEBASE
Definition: ffmpeg.h:515
create_streams
static int create_streams(Muxer *mux, const OptionsContext *o)
Definition: ffmpeg_mux_init.c:1880
MuxStream::stream_duration
int64_t stream_duration
Definition: ffmpeg_mux.h:66
OutputStream::class
const AVClass * class
Definition: ffmpeg.h:588
OptionsContext::top_field_first
SpecifierOptList top_field_first
Definition: ffmpeg.h:223
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:2885
AV_IAMF_PARAMETER_DEFINITION_DEMIXING
@ AV_IAMF_PARAMETER_DEFINITION_DEMIXING
Subblocks are of struct type AVIAMFDemixingInfo.
Definition: iamf.h:177
OutputFile
Definition: ffmpeg.h:656
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:310
sch_add_sq_enc
int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx)
Add an pre-encoding sync queue to the scheduler.
Definition: ffmpeg_sched.c:871
AV_CODEC_CONFIG_COLOR_SPACE
@ AV_CODEC_CONFIG_COLOR_SPACE
AVColorSpace, terminated by AVCOL_SPC_UNSPECIFIED.
Definition: avcodec.h:2711