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