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