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