FFmpeg
ffmpeg_opt.c
Go to the documentation of this file.
1 
2 /*
3  * ffmpeg option parsing
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 
24 #include "ffmpeg.h"
25 #include "cmdutils.h"
26 
27 #include "libavformat/avformat.h"
28 
29 #include "libavcodec/avcodec.h"
30 #include "libavcodec/bsf.h"
31 
32 #include "libavfilter/avfilter.h"
33 
34 #include "libavutil/avassert.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/avutil.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/fifo.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/pixfmt.h"
45 
46 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
47 
48 #define SPECIFIER_OPT_FMT_str "%s"
49 #define SPECIFIER_OPT_FMT_i "%i"
50 #define SPECIFIER_OPT_FMT_i64 "%"PRId64
51 #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
52 #define SPECIFIER_OPT_FMT_f "%f"
53 #define SPECIFIER_OPT_FMT_dbl "%lf"
54 
55 static const char *const opt_name_codec_names[] = {"c", "codec", "acodec", "vcodec", "scodec", "dcodec", NULL};
56 static const char *const opt_name_audio_channels[] = {"ac", NULL};
57 static const char *const opt_name_audio_sample_rate[] = {"ar", NULL};
58 static const char *const opt_name_frame_rates[] = {"r", NULL};
59 static const char *const opt_name_max_frame_rates[] = {"fpsmax", NULL};
60 static const char *const opt_name_frame_sizes[] = {"s", NULL};
61 static const char *const opt_name_frame_pix_fmts[] = {"pix_fmt", NULL};
62 static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
63 static const char *const opt_name_hwaccels[] = {"hwaccel", NULL};
64 static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
65 static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
66 static const char *const opt_name_autorotate[] = {"autorotate", NULL};
67 static const char *const opt_name_autoscale[] = {"autoscale", NULL};
68 static const char *const opt_name_max_frames[] = {"frames", "aframes", "vframes", "dframes", NULL};
69 static const char *const opt_name_bitstream_filters[] = {"bsf", "absf", "vbsf", NULL};
70 static const char *const opt_name_codec_tags[] = {"tag", "atag", "vtag", "stag", NULL};
71 static const char *const opt_name_sample_fmts[] = {"sample_fmt", NULL};
72 static const char *const opt_name_qscale[] = {"q", "qscale", NULL};
73 static const char *const opt_name_forced_key_frames[] = {"forced_key_frames", NULL};
74 static const char *const opt_name_force_fps[] = {"force_fps", NULL};
75 static const char *const opt_name_frame_aspect_ratios[] = {"aspect", NULL};
76 static const char *const opt_name_rc_overrides[] = {"rc_override", NULL};
77 static const char *const opt_name_intra_matrices[] = {"intra_matrix", NULL};
78 static const char *const opt_name_inter_matrices[] = {"inter_matrix", NULL};
79 static const char *const opt_name_chroma_intra_matrices[] = {"chroma_intra_matrix", NULL};
80 static const char *const opt_name_top_field_first[] = {"top", NULL};
81 static const char *const opt_name_presets[] = {"pre", "apre", "vpre", "spre", NULL};
82 static const char *const opt_name_copy_initial_nonkeyframes[] = {"copyinkfr", NULL};
83 static const char *const opt_name_copy_prior_start[] = {"copypriorss", NULL};
84 static const char *const opt_name_filters[] = {"filter", "af", "vf", NULL};
85 static const char *const opt_name_filter_scripts[] = {"filter_script", NULL};
86 static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL};
87 static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL};
88 static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL};
89 static const char *const opt_name_pass[] = {"pass", NULL};
90 static const char *const opt_name_passlogfiles[] = {"passlogfile", NULL};
91 static const char *const opt_name_max_muxing_queue_size[] = {"max_muxing_queue_size", NULL};
92 static const char *const opt_name_muxing_queue_data_threshold[] = {"muxing_queue_data_threshold", NULL};
93 static const char *const opt_name_guess_layout_max[] = {"guess_layout_max", NULL};
94 static const char *const opt_name_apad[] = {"apad", NULL};
95 static const char *const opt_name_discard[] = {"discard", NULL};
96 static const char *const opt_name_disposition[] = {"disposition", NULL};
97 static const char *const opt_name_time_bases[] = {"time_base", NULL};
98 static const char *const opt_name_enc_time_bases[] = {"enc_time_base", NULL};
99 
100 #define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
101 {\
102  char namestr[128] = "";\
103  const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
104  for (i = 0; opt_name_##name[i]; i++)\
105  av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[i], opt_name_##name[i+1] ? (opt_name_##name[i+2] ? ", " : " or ") : "");\
106  av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
107  namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
108 }
109 
110 #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
111 {\
112  int i, ret, matches = 0;\
113  SpecifierOpt *so;\
114  for (i = 0; i < o->nb_ ## name; i++) {\
115  char *spec = o->name[i].specifier;\
116  if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
117  outvar = o->name[i].u.type;\
118  so = &o->name[i];\
119  matches++;\
120  } else if (ret < 0)\
121  exit_program(1);\
122  }\
123  if (matches > 1)\
124  WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
125 }
126 
127 #define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
128 {\
129  int i;\
130  for (i = 0; i < o->nb_ ## name; i++) {\
131  char *spec = o->name[i].specifier;\
132  if (!strcmp(spec, mediatype))\
133  outvar = o->name[i].u.type;\
134  }\
135 }
136 
137 const HWAccel hwaccels[] = {
138 #if CONFIG_VIDEOTOOLBOX
140 #endif
141  { 0 },
142 };
144 
147 
150 float dts_error_threshold = 3600*30;
151 
152 int audio_volume = 256;
157 int do_benchmark = 0;
159 int do_hex_dump = 0;
160 int do_pkt_dump = 0;
161 int copy_ts = 0;
163 int copy_tb = -1;
164 int debug_ts = 0;
167 int print_stats = -1;
168 int qp_hist = 0;
171 float max_error_rate = 2.0/3;
176 int64_t stats_period = 500000;
177 
178 
179 static int intra_only = 0;
180 static int file_overwrite = 0;
181 static int no_file_overwrite = 0;
182 static int do_psnr = 0;
183 static int input_sync;
185 static int ignore_unknown_streams = 0;
186 static int copy_unknown_streams = 0;
187 static int recast_media = 0;
188 static int find_stream_info = 1;
189 
191 {
192  const OptionDef *po = options;
193  int i;
194 
195  /* all OPT_SPEC and OPT_STRING can be freed in generic way */
196  while (po->name) {
197  void *dst = (uint8_t*)o + po->u.off;
198 
199  if (po->flags & OPT_SPEC) {
200  SpecifierOpt **so = dst;
201  int i, *count = (int*)(so + 1);
202  for (i = 0; i < *count; i++) {
203  av_freep(&(*so)[i].specifier);
204  if (po->flags & OPT_STRING)
205  av_freep(&(*so)[i].u.str);
206  }
207  av_freep(so);
208  *count = 0;
209  } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
210  av_freep(dst);
211  po++;
212  }
213 
214  for (i = 0; i < o->nb_stream_maps; i++)
216  av_freep(&o->stream_maps);
218  av_freep(&o->streamid_map);
219  av_freep(&o->attachments);
220 }
221 
223 {
224  memset(o, 0, sizeof(*o));
225 
226  o->stop_time = INT64_MAX;
227  o->mux_max_delay = 0.7;
230  o->recording_time = INT64_MAX;
231  o->limit_filesize = UINT64_MAX;
232  o->chapters_input_file = INT_MAX;
233  o->accurate_seek = 1;
234  o->thread_queue_size = -1;
235 }
236 
237 static int show_hwaccels(void *optctx, const char *opt, const char *arg)
238 {
240 
241  printf("Hardware acceleration methods:\n");
242  while ((type = av_hwdevice_iterate_types(type)) !=
245  printf("\n");
246  return 0;
247 }
248 
249 /* return a copy of the input with the stream specifiers removed from the keys */
251 {
252  AVDictionaryEntry *e = NULL;
253  AVDictionary *ret = NULL;
254 
255  while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) {
256  char *p = strchr(e->key, ':');
257 
258  if (p)
259  *p = 0;
260  av_dict_set(&ret, e->key, e->value, 0);
261  if (p)
262  *p = ':';
263  }
264  return ret;
265 }
266 
267 static int opt_filter_threads(void *optctx, const char *opt, const char *arg)
268 {
271  return 0;
272 }
273 
274 static int opt_abort_on(void *optctx, const char *opt, const char *arg)
275 {
276  static const AVOption opts[] = {
277  { "abort_on" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
278  { "empty_output" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = ABORT_ON_FLAG_EMPTY_OUTPUT }, .unit = "flags" },
279  { "empty_output_stream", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM }, .unit = "flags" },
280  { NULL },
281  };
282  static const AVClass class = {
283  .class_name = "",
284  .item_name = av_default_item_name,
285  .option = opts,
286  .version = LIBAVUTIL_VERSION_INT,
287  };
288  const AVClass *pclass = &class;
289 
290  return av_opt_eval_flags(&pclass, &opts[0], arg, &abort_on_flags);
291 }
292 
293 static int opt_stats_period(void *optctx, const char *opt, const char *arg)
294 {
295  int64_t user_stats_period = parse_time_or_die(opt, arg, 1);
296 
297  if (user_stats_period <= 0) {
298  av_log(NULL, AV_LOG_ERROR, "stats_period %s must be positive.\n", arg);
299  return AVERROR(EINVAL);
300  }
301 
302  stats_period = user_stats_period;
303  av_log(NULL, AV_LOG_INFO, "ffmpeg stats and -progress period set to %s.\n", arg);
304 
305  return 0;
306 }
307 
308 static int opt_sameq(void *optctx, const char *opt, const char *arg)
309 {
310  av_log(NULL, AV_LOG_ERROR, "Option '%s' was removed. "
311  "If you are looking for an option to preserve the quality (which is not "
312  "what -%s was for), use -qscale 0 or an equivalent quality factor option.\n",
313  opt, opt);
314  return AVERROR(EINVAL);
315 }
316 
317 static int opt_video_channel(void *optctx, const char *opt, const char *arg)
318 {
319  av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
320  return opt_default(optctx, "channel", arg);
321 }
322 
323 static int opt_video_standard(void *optctx, const char *opt, const char *arg)
324 {
325  av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
326  return opt_default(optctx, "standard", arg);
327 }
328 
329 static int opt_audio_codec(void *optctx, const char *opt, const char *arg)
330 {
331  OptionsContext *o = optctx;
332  return parse_option(o, "codec:a", arg, options);
333 }
334 
335 static int opt_video_codec(void *optctx, const char *opt, const char *arg)
336 {
337  OptionsContext *o = optctx;
338  return parse_option(o, "codec:v", arg, options);
339 }
340 
341 static int opt_subtitle_codec(void *optctx, const char *opt, const char *arg)
342 {
343  OptionsContext *o = optctx;
344  return parse_option(o, "codec:s", arg, options);
345 }
346 
347 static int opt_data_codec(void *optctx, const char *opt, const char *arg)
348 {
349  OptionsContext *o = optctx;
350  return parse_option(o, "codec:d", arg, options);
351 }
352 
353 static int opt_map(void *optctx, const char *opt, const char *arg)
354 {
355  OptionsContext *o = optctx;
356  StreamMap *m = NULL;
357  int i, negative = 0, file_idx, disabled = 0;
358  int sync_file_idx = -1, sync_stream_idx = 0;
359  char *p, *sync;
360  char *map;
361  char *allow_unused;
362 
363  if (*arg == '-') {
364  negative = 1;
365  arg++;
366  }
367  map = av_strdup(arg);
368  if (!map)
369  return AVERROR(ENOMEM);
370 
371  /* parse sync stream first, just pick first matching stream */
372  if (sync = strchr(map, ',')) {
373  *sync = 0;
374  sync_file_idx = strtol(sync + 1, &sync, 0);
375  if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
376  av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
377  exit_program(1);
378  }
379  if (*sync)
380  sync++;
381  for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
382  if (check_stream_specifier(input_files[sync_file_idx]->ctx,
383  input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
384  sync_stream_idx = i;
385  break;
386  }
387  if (i == input_files[sync_file_idx]->nb_streams) {
388  av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
389  "match any streams.\n", arg);
390  exit_program(1);
391  }
392  if (input_streams[input_files[sync_file_idx]->ist_index + sync_stream_idx]->user_set_discard == AVDISCARD_ALL) {
393  av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s matches a disabled input "
394  "stream.\n", arg);
395  exit_program(1);
396  }
397  }
398 
399 
400  if (map[0] == '[') {
401  /* this mapping refers to lavfi output */
402  const char *c = map + 1;
404  m = &o->stream_maps[o->nb_stream_maps - 1];
405  m->linklabel = av_get_token(&c, "]");
406  if (!m->linklabel) {
407  av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
408  exit_program(1);
409  }
410  } else {
411  if (allow_unused = strchr(map, '?'))
412  *allow_unused = 0;
413  file_idx = strtol(map, &p, 0);
414  if (file_idx >= nb_input_files || file_idx < 0) {
415  av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
416  exit_program(1);
417  }
418  if (negative)
419  /* disable some already defined maps */
420  for (i = 0; i < o->nb_stream_maps; i++) {
421  m = &o->stream_maps[i];
422  if (file_idx == m->file_index &&
425  *p == ':' ? p + 1 : p) > 0)
426  m->disabled = 1;
427  }
428  else
429  for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
430  if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
431  *p == ':' ? p + 1 : p) <= 0)
432  continue;
433  if (input_streams[input_files[file_idx]->ist_index + i]->user_set_discard == AVDISCARD_ALL) {
434  disabled = 1;
435  continue;
436  }
438  m = &o->stream_maps[o->nb_stream_maps - 1];
439 
440  m->file_index = file_idx;
441  m->stream_index = i;
442 
443  if (sync_file_idx >= 0) {
444  m->sync_file_index = sync_file_idx;
445  m->sync_stream_index = sync_stream_idx;
446  } else {
447  m->sync_file_index = file_idx;
448  m->sync_stream_index = i;
449  }
450  }
451  }
452 
453  if (!m) {
454  if (allow_unused) {
455  av_log(NULL, AV_LOG_VERBOSE, "Stream map '%s' matches no streams; ignoring.\n", arg);
456  } else if (disabled) {
457  av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches disabled streams.\n"
458  "To ignore this, add a trailing '?' to the map.\n", arg);
459  exit_program(1);
460  } else {
461  av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n"
462  "To ignore this, add a trailing '?' to the map.\n", arg);
463  exit_program(1);
464  }
465  }
466 
467  av_freep(&map);
468  return 0;
469 }
470 
471 static int opt_attach(void *optctx, const char *opt, const char *arg)
472 {
473  OptionsContext *o = optctx;
475  o->attachments[o->nb_attachments - 1] = arg;
476  return 0;
477 }
478 
479 static int opt_map_channel(void *optctx, const char *opt, const char *arg)
480 {
481  OptionsContext *o = optctx;
482  int n;
483  AVStream *st;
484  AudioChannelMap *m;
485  char *allow_unused;
486  char *mapchan;
487  mapchan = av_strdup(arg);
488  if (!mapchan)
489  return AVERROR(ENOMEM);
490 
493 
494  /* muted channel syntax */
495  n = sscanf(arg, "%d:%d.%d", &m->channel_idx, &m->ofile_idx, &m->ostream_idx);
496  if ((n == 1 || n == 3) && m->channel_idx == -1) {
497  m->file_idx = m->stream_idx = -1;
498  if (n == 1)
499  m->ofile_idx = m->ostream_idx = -1;
500  av_free(mapchan);
501  return 0;
502  }
503 
504  /* normal syntax */
505  n = sscanf(arg, "%d.%d.%d:%d.%d",
506  &m->file_idx, &m->stream_idx, &m->channel_idx,
507  &m->ofile_idx, &m->ostream_idx);
508 
509  if (n != 3 && n != 5) {
510  av_log(NULL, AV_LOG_FATAL, "Syntax error, mapchan usage: "
511  "[file.stream.channel|-1][:syncfile:syncstream]\n");
512  exit_program(1);
513  }
514 
515  if (n != 5) // only file.stream.channel specified
516  m->ofile_idx = m->ostream_idx = -1;
517 
518  /* check input */
519  if (m->file_idx < 0 || m->file_idx >= nb_input_files) {
520  av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file index: %d\n",
521  m->file_idx);
522  exit_program(1);
523  }
524  if (m->stream_idx < 0 ||
526  av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n",
527  m->file_idx, m->stream_idx);
528  exit_program(1);
529  }
530  st = input_files[m->file_idx]->ctx->streams[m->stream_idx];
531  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
532  av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n",
533  m->file_idx, m->stream_idx);
534  exit_program(1);
535  }
536  /* allow trailing ? to map_channel */
537  if (allow_unused = strchr(mapchan, '?'))
538  *allow_unused = 0;
539  if (m->channel_idx < 0 || m->channel_idx >= st->codecpar->channels ||
541  if (allow_unused) {
542  av_log(NULL, AV_LOG_VERBOSE, "mapchan: invalid audio channel #%d.%d.%d\n",
543  m->file_idx, m->stream_idx, m->channel_idx);
544  } else {
545  av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n"
546  "To ignore this, add a trailing '?' to the map_channel.\n",
547  m->file_idx, m->stream_idx, m->channel_idx);
548  exit_program(1);
549  }
550 
551  }
552  av_free(mapchan);
553  return 0;
554 }
555 
556 static int opt_sdp_file(void *optctx, const char *opt, const char *arg)
557 {
560  return 0;
561 }
562 
563 #if CONFIG_VAAPI
564 static int opt_vaapi_device(void *optctx, const char *opt, const char *arg)
565 {
566  const char *prefix = "vaapi:";
567  char *tmp;
568  int err;
569  tmp = av_asprintf("%s%s", prefix, arg);
570  if (!tmp)
571  return AVERROR(ENOMEM);
573  av_free(tmp);
574  return err;
575 }
576 #endif
577 
578 #if CONFIG_QSV
579 static int opt_qsv_device(void *optctx, const char *opt, const char *arg)
580 {
581  const char *prefix = "qsv=__qsv_device:hw_any,child_device=";
582  int err;
583  char *tmp = av_asprintf("%s%s", prefix, arg);
584 
585  if (!tmp)
586  return AVERROR(ENOMEM);
587 
589  av_free(tmp);
590 
591  return err;
592 }
593 #endif
594 
595 static int opt_init_hw_device(void *optctx, const char *opt, const char *arg)
596 {
597  if (!strcmp(arg, "list")) {
599  printf("Supported hardware device types:\n");
600  while ((type = av_hwdevice_iterate_types(type)) !=
603  printf("\n");
604  exit_program(0);
605  } else {
607  }
608 }
609 
610 static int opt_filter_hw_device(void *optctx, const char *opt, const char *arg)
611 {
612  if (filter_hw_device) {
613  av_log(NULL, AV_LOG_ERROR, "Only one filter device can be used.\n");
614  return AVERROR(EINVAL);
615  }
617  if (!filter_hw_device) {
618  av_log(NULL, AV_LOG_ERROR, "Invalid filter device %s.\n", arg);
619  return AVERROR(EINVAL);
620  }
621  return 0;
622 }
623 
624 /**
625  * Parse a metadata specifier passed as 'arg' parameter.
626  * @param arg metadata string to parse
627  * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
628  * @param index for type c/p, chapter/program index is written here
629  * @param stream_spec for type s, the stream specifier is written here
630  */
631 static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)
632 {
633  if (*arg) {
634  *type = *arg;
635  switch (*arg) {
636  case 'g':
637  break;
638  case 's':
639  if (*(++arg) && *arg != ':') {
640  av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
641  exit_program(1);
642  }
643  *stream_spec = *arg == ':' ? arg + 1 : "";
644  break;
645  case 'c':
646  case 'p':
647  if (*(++arg) == ':')
648  *index = strtol(++arg, NULL, 0);
649  break;
650  default:
651  av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
652  exit_program(1);
653  }
654  } else
655  *type = 'g';
656 }
657 
658 static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
659 {
660  AVDictionary **meta_in = NULL;
661  AVDictionary **meta_out = NULL;
662  int i, ret = 0;
663  char type_in, type_out;
664  const char *istream_spec = NULL, *ostream_spec = NULL;
665  int idx_in = 0, idx_out = 0;
666 
667  parse_meta_type(inspec, &type_in, &idx_in, &istream_spec);
668  parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
669 
670  if (!ic) {
671  if (type_out == 'g' || !*outspec)
672  o->metadata_global_manual = 1;
673  if (type_out == 's' || !*outspec)
675  if (type_out == 'c' || !*outspec)
677  return 0;
678  }
679 
680  if (type_in == 'g' || type_out == 'g')
681  o->metadata_global_manual = 1;
682  if (type_in == 's' || type_out == 's')
684  if (type_in == 'c' || type_out == 'c')
686 
687  /* ic is NULL when just disabling automatic mappings */
688  if (!ic)
689  return 0;
690 
691 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
692  if ((index) < 0 || (index) >= (nb_elems)) {\
693  av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
694  (desc), (index));\
695  exit_program(1);\
696  }
697 
698 #define SET_DICT(type, meta, context, index)\
699  switch (type) {\
700  case 'g':\
701  meta = &context->metadata;\
702  break;\
703  case 'c':\
704  METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
705  meta = &context->chapters[index]->metadata;\
706  break;\
707  case 'p':\
708  METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
709  meta = &context->programs[index]->metadata;\
710  break;\
711  case 's':\
712  break; /* handled separately below */ \
713  default: av_assert0(0);\
714  }\
715 
716  SET_DICT(type_in, meta_in, ic, idx_in);
717  SET_DICT(type_out, meta_out, oc, idx_out);
718 
719  /* for input streams choose first matching stream */
720  if (type_in == 's') {
721  for (i = 0; i < ic->nb_streams; i++) {
722  if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
723  meta_in = &ic->streams[i]->metadata;
724  break;
725  } else if (ret < 0)
726  exit_program(1);
727  }
728  if (!meta_in) {
729  av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
730  exit_program(1);
731  }
732  }
733 
734  if (type_out == 's') {
735  for (i = 0; i < oc->nb_streams; i++) {
736  if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
737  meta_out = &oc->streams[i]->metadata;
738  av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
739  } else if (ret < 0)
740  exit_program(1);
741  }
742  } else
743  av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
744 
745  return 0;
746 }
747 
748 static int opt_recording_timestamp(void *optctx, const char *opt, const char *arg)
749 {
750  OptionsContext *o = optctx;
751  char buf[128];
752  int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
753  struct tm time = *gmtime((time_t*)&recording_timestamp);
754  if (!strftime(buf, sizeof(buf), "creation_time=%Y-%m-%dT%H:%M:%S%z", &time))
755  return -1;
756  parse_option(o, "metadata", buf, options);
757 
758  av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
759  "tag instead.\n", opt);
760  return 0;
761 }
762 
763 static const AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
764 {
765  const AVCodecDescriptor *desc;
766  const char *codec_string = encoder ? "encoder" : "decoder";
767  const AVCodec *codec;
768 
769  codec = encoder ?
772 
773  if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
774  codec = encoder ? avcodec_find_encoder(desc->id) :
776  if (codec)
777  av_log(NULL, AV_LOG_VERBOSE, "Matched %s '%s' for codec '%s'.\n",
778  codec_string, codec->name, desc->name);
779  }
780 
781  if (!codec) {
782  av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
783  exit_program(1);
784  }
785  if (codec->type != type && !recast_media) {
786  av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
787  exit_program(1);
788  }
789  return codec;
790 }
791 
793 {
794  char *codec_name = NULL;
795 
796  MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
797  if (codec_name) {
798  const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
799  st->codecpar->codec_id = codec->id;
800  if (recast_media && st->codecpar->codec_type != codec->type)
801  st->codecpar->codec_type = codec->type;
802  return codec;
803  } else
805 }
806 
807 /* Add all the streams from the given input file to the global
808  * list of input streams. */
810 {
811  int i, ret;
812 
813  for (i = 0; i < ic->nb_streams; i++) {
814  AVStream *st = ic->streams[i];
815  AVCodecParameters *par = st->codecpar;
816  InputStream *ist = av_mallocz(sizeof(*ist));
817  char *framerate = NULL, *hwaccel_device = NULL;
818  const char *hwaccel = NULL;
819  char *hwaccel_output_format = NULL;
820  char *codec_tag = NULL;
821  char *next;
822  char *discard_str = NULL;
823  const AVClass *cc = avcodec_get_class();
824  const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
826 
827  if (!ist)
828  exit_program(1);
829 
832 
833  ist->st = st;
834  ist->file_index = nb_input_files;
835  ist->discard = 1;
836  st->discard = AVDISCARD_ALL;
837  ist->nb_samples = 0;
838  ist->first_dts = AV_NOPTS_VALUE;
839  ist->min_pts = INT64_MAX;
840  ist->max_pts = INT64_MIN;
841 
842  ist->ts_scale = 1.0;
843  MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
844 
845  ist->autorotate = 1;
846  MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
847 
848  MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
849  if (codec_tag) {
850  uint32_t tag = strtol(codec_tag, &next, 0);
851  if (*next)
852  tag = AV_RL32(codec_tag);
853  st->codecpar->codec_tag = tag;
854  }
855 
856  ist->dec = choose_decoder(o, ic, st);
857  ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
858 
859  ist->reinit_filters = -1;
860  MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
861 
862  MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
863  ist->user_set_discard = AVDISCARD_NONE;
864 
865  if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
866  (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
867  (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
868  (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
869  ist->user_set_discard = AVDISCARD_ALL;
870 
871  if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) {
872  av_log(NULL, AV_LOG_ERROR, "Error parsing discard %s.\n",
873  discard_str);
874  exit_program(1);
875  }
876 
877  ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
878 
879  ist->dec_ctx = avcodec_alloc_context3(ist->dec);
880  if (!ist->dec_ctx) {
881  av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n");
882  exit_program(1);
883  }
884 
885  ret = avcodec_parameters_to_context(ist->dec_ctx, par);
886  if (ret < 0) {
887  av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
888  exit_program(1);
889  }
890 
891  ist->pkt = av_packet_alloc();
892  if (!ist->pkt)
893  exit_program(1);
894 
895  if (o->bitexact)
896  ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
897 
898  switch (par->codec_type) {
899  case AVMEDIA_TYPE_VIDEO:
900  if(!ist->dec)
901  ist->dec = avcodec_find_decoder(par->codec_id);
902 
903  // avformat_find_stream_info() doesn't set this for us anymore.
904  ist->dec_ctx->framerate = st->avg_frame_rate;
905 
906  MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
907  if (framerate && av_parse_video_rate(&ist->framerate,
908  framerate) < 0) {
909  av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
910  framerate);
911  exit_program(1);
912  }
913 
914  ist->top_field_first = -1;
915  MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
916 
917  MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
918  MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
919  hwaccel_output_format, ic, st);
920 
921  if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
923  "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
924  "with old commandlines. This behaviour is DEPRECATED and will be removed "
925  "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
926  ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
927  } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
929  "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
930  "with old commandlines. This behaviour is DEPRECATED and will be removed "
931  "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
932  ist->hwaccel_output_format = AV_PIX_FMT_QSV;
933  } else if (hwaccel_output_format) {
934  ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
935  if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
936  av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
937  "format: %s", hwaccel_output_format);
938  }
939  } else {
940  ist->hwaccel_output_format = AV_PIX_FMT_NONE;
941  }
942 
943  if (hwaccel) {
944  // The NVDEC hwaccels use a CUDA device, so remap the name here.
945  if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
946  hwaccel = "cuda";
947 
948  if (!strcmp(hwaccel, "none"))
949  ist->hwaccel_id = HWACCEL_NONE;
950  else if (!strcmp(hwaccel, "auto"))
951  ist->hwaccel_id = HWACCEL_AUTO;
952  else {
953  enum AVHWDeviceType type;
954  int i;
955  for (i = 0; hwaccels[i].name; i++) {
956  if (!strcmp(hwaccels[i].name, hwaccel)) {
957  ist->hwaccel_id = hwaccels[i].id;
958  break;
959  }
960  }
961 
962  if (!ist->hwaccel_id) {
964  if (type != AV_HWDEVICE_TYPE_NONE) {
965  ist->hwaccel_id = HWACCEL_GENERIC;
966  ist->hwaccel_device_type = type;
967  }
968  }
969 
970  if (!ist->hwaccel_id) {
971  av_log(NULL, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
972  hwaccel);
973  av_log(NULL, AV_LOG_FATAL, "Supported hwaccels: ");
975  while ((type = av_hwdevice_iterate_types(type)) !=
977  av_log(NULL, AV_LOG_FATAL, "%s ",
979  av_log(NULL, AV_LOG_FATAL, "\n");
980  exit_program(1);
981  }
982  }
983  }
984 
985  MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
986  if (hwaccel_device) {
987  ist->hwaccel_device = av_strdup(hwaccel_device);
988  if (!ist->hwaccel_device)
989  exit_program(1);
990  }
991 
992  ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
993 
994  break;
995  case AVMEDIA_TYPE_AUDIO:
996  ist->guess_layout_max = INT_MAX;
997  MATCH_PER_STREAM_OPT(guess_layout_max, i, ist->guess_layout_max, ic, st);
999  break;
1000  case AVMEDIA_TYPE_DATA:
1001  case AVMEDIA_TYPE_SUBTITLE: {
1002  char *canvas_size = NULL;
1003  if(!ist->dec)
1004  ist->dec = avcodec_find_decoder(par->codec_id);
1005  MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
1006  MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
1007  if (canvas_size &&
1008  av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height, canvas_size) < 0) {
1009  av_log(NULL, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
1010  exit_program(1);
1011  }
1012  break;
1013  }
1015  case AVMEDIA_TYPE_UNKNOWN:
1016  break;
1017  default:
1018  abort();
1019  }
1020 
1021  ret = avcodec_parameters_from_context(par, ist->dec_ctx);
1022  if (ret < 0) {
1023  av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1024  exit_program(1);
1025  }
1026  }
1027 }
1028 
1029 static void assert_file_overwrite(const char *filename)
1030 {
1031  const char *proto_name = avio_find_protocol_name(filename);
1032 
1034  fprintf(stderr, "Error, both -y and -n supplied. Exiting.\n");
1035  exit_program(1);
1036  }
1037 
1038  if (!file_overwrite) {
1039  if (proto_name && !strcmp(proto_name, "file") && avio_check(filename, 0) == 0) {
1041  fprintf(stderr,"File '%s' already exists. Overwrite? [y/N] ", filename);
1042  fflush(stderr);
1043  term_exit();
1044  signal(SIGINT, SIG_DFL);
1045  if (!read_yesno()) {
1046  av_log(NULL, AV_LOG_FATAL, "Not overwriting - exiting\n");
1047  exit_program(1);
1048  }
1049  term_init();
1050  }
1051  else {
1052  av_log(NULL, AV_LOG_FATAL, "File '%s' already exists. Exiting.\n", filename);
1053  exit_program(1);
1054  }
1055  }
1056  }
1057 
1058  if (proto_name && !strcmp(proto_name, "file")) {
1059  for (int i = 0; i < nb_input_files; i++) {
1060  InputFile *file = input_files[i];
1061  if (file->ctx->iformat->flags & AVFMT_NOFILE)
1062  continue;
1063  if (!strcmp(filename, file->ctx->url)) {
1064  av_log(NULL, AV_LOG_FATAL, "Output %s same as Input #%d - exiting\n", filename, i);
1065  av_log(NULL, AV_LOG_WARNING, "FFmpeg cannot edit existing files in-place.\n");
1066  exit_program(1);
1067  }
1068  }
1069  }
1070 }
1071 
1072 static void dump_attachment(AVStream *st, const char *filename)
1073 {
1074  int ret;
1075  AVIOContext *out = NULL;
1076  AVDictionaryEntry *e;
1077 
1078  if (!st->codecpar->extradata_size) {
1079  av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
1080  nb_input_files - 1, st->index);
1081  return;
1082  }
1083  if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
1084  filename = e->value;
1085  if (!*filename) {
1086  av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
1087  "in stream #%d:%d.\n", nb_input_files - 1, st->index);
1088  exit_program(1);
1089  }
1090 
1091  assert_file_overwrite(filename);
1092 
1093  if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
1094  av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
1095  filename);
1096  exit_program(1);
1097  }
1098 
1100  avio_flush(out);
1101  avio_close(out);
1102 }
1103 
1104 static int open_input_file(OptionsContext *o, const char *filename)
1105 {
1106  InputFile *f;
1107  AVFormatContext *ic;
1108  const AVInputFormat *file_iformat = NULL;
1109  int err, i, ret;
1110  int64_t timestamp;
1111  AVDictionary *unused_opts = NULL;
1112  AVDictionaryEntry *e = NULL;
1113  char * video_codec_name = NULL;
1114  char * audio_codec_name = NULL;
1115  char *subtitle_codec_name = NULL;
1116  char * data_codec_name = NULL;
1117  int scan_all_pmts_set = 0;
1118 
1119  if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) {
1120  o->stop_time = INT64_MAX;
1121  av_log(NULL, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
1122  }
1123 
1124  if (o->stop_time != INT64_MAX && o->recording_time == INT64_MAX) {
1125  int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time;
1126  if (o->stop_time <= start_time) {
1127  av_log(NULL, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
1128  exit_program(1);
1129  } else {
1131  }
1132  }
1133 
1134  if (o->format) {
1135  if (!(file_iformat = av_find_input_format(o->format))) {
1136  av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
1137  exit_program(1);
1138  }
1139  }
1140 
1141  if (!strcmp(filename, "-"))
1142  filename = "pipe:";
1143 
1144  stdin_interaction &= strncmp(filename, "pipe:", 5) &&
1145  strcmp(filename, "/dev/stdin");
1146 
1147  /* get default parameters from command line */
1148  ic = avformat_alloc_context();
1149  if (!ic) {
1150  print_error(filename, AVERROR(ENOMEM));
1151  exit_program(1);
1152  }
1153  if (o->nb_audio_sample_rate) {
1154  av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
1155  }
1156  if (o->nb_audio_channels) {
1157  const AVClass *priv_class;
1158  /* because we set audio_channels based on both the "ac" and
1159  * "channel_layout" options, we need to check that the specified
1160  * demuxer actually has the "channels" option before setting it */
1161  if (file_iformat && (priv_class = file_iformat->priv_class) &&
1162  av_opt_find(&priv_class, "channels", NULL, 0,
1164  av_dict_set_int(&o->g->format_opts, "channels", o->audio_channels[o->nb_audio_channels - 1].u.i, 0);
1165  }
1166  }
1167  if (o->nb_frame_rates) {
1168  const AVClass *priv_class;
1169  /* set the format-level framerate option;
1170  * this is important for video grabbers, e.g. x11 */
1171  if (file_iformat && (priv_class = file_iformat->priv_class) &&
1172  av_opt_find(&priv_class, "framerate", NULL, 0,
1174  av_dict_set(&o->g->format_opts, "framerate",
1175  o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
1176  }
1177  }
1178  if (o->nb_frame_sizes) {
1179  av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
1180  }
1181  if (o->nb_frame_pix_fmts)
1182  av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
1183 
1184  MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
1185  MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
1186  MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
1187  MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
1188 
1189  if (video_codec_name)
1191  if (audio_codec_name)
1193  if (subtitle_codec_name)
1195  if (data_codec_name)
1196  ic->data_codec = find_codec_or_die(data_codec_name , AVMEDIA_TYPE_DATA , 0);
1197 
1201  ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1202 
1203  ic->flags |= AVFMT_FLAG_NONBLOCK;
1204  if (o->bitexact)
1205  ic->flags |= AVFMT_FLAG_BITEXACT;
1206  ic->interrupt_callback = int_cb;
1207 
1208  if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1209  av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
1210  scan_all_pmts_set = 1;
1211  }
1212  /* open the input file with generic avformat function */
1213  err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
1214  if (err < 0) {
1215  print_error(filename, err);
1216  if (err == AVERROR_PROTOCOL_NOT_FOUND)
1217  av_log(NULL, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
1218  exit_program(1);
1219  }
1220  if (scan_all_pmts_set)
1221  av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1224 
1225  /* apply forced codec ids */
1226  for (i = 0; i < ic->nb_streams; i++)
1227  choose_decoder(o, ic, ic->streams[i]);
1228 
1229  if (find_stream_info) {
1231  int orig_nb_streams = ic->nb_streams;
1232 
1233  /* If not enough info to get the stream parameters, we decode the
1234  first frames to get it. (used in mpeg case for example) */
1236 
1237  for (i = 0; i < orig_nb_streams; i++)
1238  av_dict_free(&opts[i]);
1239  av_freep(&opts);
1240 
1241  if (ret < 0) {
1242  av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
1243  if (ic->nb_streams == 0) {
1244  avformat_close_input(&ic);
1245  exit_program(1);
1246  }
1247  }
1248  }
1249 
1251  av_log(NULL, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss for %s\n", filename);
1253  }
1254 
1255  if (o->start_time_eof != AV_NOPTS_VALUE) {
1256  if (o->start_time_eof >= 0) {
1257  av_log(NULL, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
1258  exit_program(1);
1259  }
1260  if (ic->duration > 0) {
1261  o->start_time = o->start_time_eof + ic->duration;
1262  if (o->start_time < 0) {
1263  av_log(NULL, AV_LOG_WARNING, "-sseof value seeks to before start of file %s; ignored\n", filename);
1265  }
1266  } else
1267  av_log(NULL, AV_LOG_WARNING, "Cannot use -sseof, duration of %s not known\n", filename);
1268  }
1269  timestamp = (o->start_time == AV_NOPTS_VALUE) ? 0 : o->start_time;
1270  /* add the stream start time */
1271  if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
1272  timestamp += ic->start_time;
1273 
1274  /* if seeking requested, we execute it */
1275  if (o->start_time != AV_NOPTS_VALUE) {
1276  int64_t seek_timestamp = timestamp;
1277 
1278  if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1279  int dts_heuristic = 0;
1280  for (i=0; i<ic->nb_streams; i++) {
1281  const AVCodecParameters *par = ic->streams[i]->codecpar;
1282  if (par->video_delay) {
1283  dts_heuristic = 1;
1284  break;
1285  }
1286  }
1287  if (dts_heuristic) {
1288  seek_timestamp -= 3*AV_TIME_BASE / 23;
1289  }
1290  }
1291  ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1292  if (ret < 0) {
1293  av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
1294  filename, (double)timestamp / AV_TIME_BASE);
1295  }
1296  }
1297 
1298  /* update the current parameters so that they match the one of the input stream */
1299  add_input_streams(o, ic);
1300 
1301  /* dump the file content */
1302  av_dump_format(ic, nb_input_files, filename, 0);
1303 
1305  f = av_mallocz(sizeof(*f));
1306  if (!f)
1307  exit_program(1);
1308  input_files[nb_input_files - 1] = f;
1309 
1310  f->ctx = ic;
1311  f->ist_index = nb_input_streams - ic->nb_streams;
1312  f->start_time = o->start_time;
1313  f->recording_time = o->recording_time;
1314  f->input_ts_offset = o->input_ts_offset;
1315  f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
1316  f->nb_streams = ic->nb_streams;
1317  f->rate_emu = o->rate_emu;
1318  f->accurate_seek = o->accurate_seek;
1319  f->loop = o->loop;
1320  f->duration = 0;
1321  f->time_base = (AVRational){ 1, 1 };
1322 
1323  f->readrate = o->readrate ? o->readrate : 0.0;
1324  if (f->readrate < 0.0f) {
1325  av_log(NULL, AV_LOG_ERROR, "Option -readrate for Input #%d is %0.3f; it must be non-negative.\n", nb_input_files, f->readrate);
1326  exit_program(1);
1327  }
1328  if (f->readrate && f->rate_emu) {
1329  av_log(NULL, AV_LOG_WARNING, "Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n", nb_input_files, f->readrate);
1330  f->rate_emu = 0;
1331  }
1332 
1333  f->pkt = av_packet_alloc();
1334  if (!f->pkt)
1335  exit_program(1);
1336 #if HAVE_THREADS
1337  f->thread_queue_size = o->thread_queue_size;
1338 #endif
1339 
1340  /* check if all codec options have been used */
1341  unused_opts = strip_specifiers(o->g->codec_opts);
1342  for (i = f->ist_index; i < nb_input_streams; i++) {
1343  e = NULL;
1344  while ((e = av_dict_get(input_streams[i]->decoder_opts, "", e,
1346  av_dict_set(&unused_opts, e->key, NULL, 0);
1347  }
1348 
1349  e = NULL;
1350  while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
1351  const AVClass *class = avcodec_get_class();
1352  const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
1354  const AVClass *fclass = avformat_get_class();
1355  const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1357  if (!option || foption)
1358  continue;
1359 
1360 
1361  if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1362  av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
1363  "input file #%d (%s) is not a decoding option.\n", e->key,
1364  option->help ? option->help : "", nb_input_files - 1,
1365  filename);
1366  exit_program(1);
1367  }
1368 
1369  av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
1370  "input file #%d (%s) has not been used for any stream. The most "
1371  "likely reason is either wrong type (e.g. a video option with "
1372  "no video streams) or that it is a private option of some decoder "
1373  "which was not actually used for any stream.\n", e->key,
1374  option->help ? option->help : "", nb_input_files - 1, filename);
1375  }
1376  av_dict_free(&unused_opts);
1377 
1378  for (i = 0; i < o->nb_dump_attachment; i++) {
1379  int j;
1380 
1381  for (j = 0; j < ic->nb_streams; j++) {
1382  AVStream *st = ic->streams[j];
1383 
1384  if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
1386  }
1387  }
1388 
1390 
1391  return 0;
1392 }
1393 
1394 static uint8_t *get_line(AVIOContext *s)
1395 {
1396  AVIOContext *line;
1397  uint8_t *buf;
1398  char c;
1399 
1400  if (avio_open_dyn_buf(&line) < 0) {
1401  av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
1402  exit_program(1);
1403  }
1404 
1405  while ((c = avio_r8(s)) && c != '\n')
1406  avio_w8(line, c);
1407  avio_w8(line, 0);
1408  avio_close_dyn_buf(line, &buf);
1409 
1410  return buf;
1411 }
1412 
1413 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
1414 {
1415  int i, ret = -1;
1416  char filename[1000];
1417  const char *base[3] = { getenv("AVCONV_DATADIR"),
1418  getenv("HOME"),
1419  AVCONV_DATADIR,
1420  };
1421 
1422  for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
1423  if (!base[i])
1424  continue;
1425  if (codec_name) {
1426  snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
1427  i != 1 ? "" : "/.avconv", codec_name, preset_name);
1428  ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
1429  }
1430  if (ret < 0) {
1431  snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
1432  i != 1 ? "" : "/.avconv", preset_name);
1433  ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
1434  }
1435  }
1436  return ret;
1437 }
1438 
1440 {
1442  char *codec_name = NULL;
1443 
1445  MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
1446  if (!codec_name) {
1447  ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
1448  NULL, ost->st->codecpar->codec_type);
1450  if (!ost->enc) {
1451  av_log(NULL, AV_LOG_FATAL, "Automatic encoder selection failed for "
1452  "output stream #%d:%d. Default encoder for format %s (codec %s) is "
1453  "probably disabled. Please choose an encoder manually.\n",
1454  ost->file_index, ost->index, s->oformat->name,
1457  }
1458  } else if (!strcmp(codec_name, "copy"))
1459  ost->stream_copy = 1;
1460  else {
1461  ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
1462  ost->st->codecpar->codec_id = ost->enc->id;
1463  }
1465  } else {
1466  /* no encoding supported for other media types */
1467  ost->stream_copy = 1;
1468  ost->encoding_needed = 0;
1469  }
1470 
1471  return 0;
1472 }
1473 
1475 {
1476  OutputStream *ost;
1477  AVStream *st = avformat_new_stream(oc, NULL);
1478  int idx = oc->nb_streams - 1, ret = 0;
1479  const char *bsfs = NULL, *time_base = NULL;
1480  char *next, *codec_tag = NULL;
1481  double qscale = -1;
1482  int i;
1483 
1484  if (!st) {
1485  av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
1486  exit_program(1);
1487  }
1488 
1489  if (oc->nb_streams - 1 < o->nb_streamid_map)
1490  st->id = o->streamid_map[oc->nb_streams - 1];
1491 
1493  if (!(ost = av_mallocz(sizeof(*ost))))
1494  exit_program(1);
1496 
1498  ost->index = idx;
1499  ost->st = st;
1501  st->codecpar->codec_type = type;
1502 
1503  ret = choose_encoder(o, oc, ost);
1504  if (ret < 0) {
1505  av_log(NULL, AV_LOG_FATAL, "Error selecting an encoder for stream "
1506  "%d:%d\n", ost->file_index, ost->index);
1507  exit_program(1);
1508  }
1509 
1511  if (!ost->enc_ctx) {
1512  av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n");
1513  exit_program(1);
1514  }
1515  ost->enc_ctx->codec_type = type;
1516 
1518  if (!ost->ref_par) {
1519  av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding parameters.\n");
1520  exit_program(1);
1521  }
1522 
1523  ost->pkt = av_packet_alloc();
1524  if (!ost->pkt)
1525  exit_program(1);
1526 
1527  if (ost->enc) {
1528  AVIOContext *s = NULL;
1529  char *buf = NULL, *arg = NULL, *preset = NULL;
1530 
1531  ost->encoder_opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
1532 
1534  ost->autoscale = 1;
1535  MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st);
1536  if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
1537  do {
1538  buf = get_line(s);
1539  if (!buf[0] || buf[0] == '#') {
1540  av_free(buf);
1541  continue;
1542  }
1543  if (!(arg = strchr(buf, '='))) {
1544  av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
1545  exit_program(1);
1546  }
1547  *arg++ = 0;
1549  av_free(buf);
1550  } while (!s->eof_reached);
1551  avio_closep(&s);
1552  }
1553  if (ret) {
1555  "Preset %s specified for stream %d:%d, but could not be opened.\n",
1556  preset, ost->file_index, ost->index);
1557  exit_program(1);
1558  }
1559  } else {
1561  }
1562 
1563 
1564  if (o->bitexact)
1566 
1567  MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
1568  if (time_base) {
1569  AVRational q;
1570  if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1571  q.num <= 0 || q.den <= 0) {
1572  av_log(NULL, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
1573  exit_program(1);
1574  }
1575  st->time_base = q;
1576  }
1577 
1578  MATCH_PER_STREAM_OPT(enc_time_bases, str, time_base, oc, st);
1579  if (time_base) {
1580  AVRational q;
1581  if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1582  q.den <= 0) {
1583  av_log(NULL, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
1584  exit_program(1);
1585  }
1586  ost->enc_timebase = q;
1587  }
1588 
1589  ost->max_frames = INT64_MAX;
1590  MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
1591  for (i = 0; i<o->nb_max_frames; i++) {
1592  char *p = o->max_frames[i].specifier;
1593  if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1594  av_log(NULL, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1595  break;
1596  }
1597  }
1598 
1599  ost->copy_prior_start = -1;
1600  MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
1601 
1602  MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
1603  if (bsfs && *bsfs) {
1604  ret = av_bsf_list_parse_str(bsfs, &ost->bsf_ctx);
1605  if (ret < 0) {
1606  av_log(NULL, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1607  exit_program(1);
1608  }
1609  }
1610 
1611  MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
1612  if (codec_tag) {
1613  uint32_t tag = strtol(codec_tag, &next, 0);
1614  if (*next)
1615  tag = AV_RL32(codec_tag);
1616  ost->st->codecpar->codec_tag =
1617  ost->enc_ctx->codec_tag = tag;
1618  }
1619 
1620  MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
1621  if (qscale >= 0) {
1623  ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1624  }
1625 
1626  MATCH_PER_STREAM_OPT(disposition, str, ost->disposition, oc, st);
1628 
1629  ost->max_muxing_queue_size = 128;
1630  MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
1631  ost->max_muxing_queue_size *= sizeof(ost->pkt);
1632 
1634 
1635  ost->muxing_queue_data_threshold = 50*1024*1024;
1636  MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ost->muxing_queue_data_threshold, oc, st);
1637 
1638  if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1640 
1641  av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
1642 
1643  av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0);
1644  if (ost->enc && av_get_exact_bits_per_sample(ost->enc->id) == 24)
1645  av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
1646 
1647  ost->source_index = source_index;
1648  if (source_index >= 0) {
1649  ost->sync_ist = input_streams[source_index];
1650  input_streams[source_index]->discard = 0;
1651  input_streams[source_index]->st->discard = input_streams[source_index]->user_set_discard;
1652  }
1654 
1655  ost->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket));
1656  if (!ost->muxing_queue)
1657  exit_program(1);
1658 
1659  return ost;
1660 }
1661 
1662 static void parse_matrix_coeffs(uint16_t *dest, const char *str)
1663 {
1664  int i;
1665  const char *p = str;
1666  for (i = 0;; i++) {
1667  dest[i] = atoi(p);
1668  if (i == 63)
1669  break;
1670  p = strchr(p, ',');
1671  if (!p) {
1672  av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
1673  exit_program(1);
1674  }
1675  p++;
1676  }
1677 }
1678 
1679 /* read file contents into a string */
1680 static uint8_t *read_file(const char *filename)
1681 {
1682  AVIOContext *pb = NULL;
1683  AVIOContext *dyn_buf = NULL;
1684  int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1685  uint8_t buf[1024], *str;
1686 
1687  if (ret < 0) {
1688  av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1689  return NULL;
1690  }
1691 
1692  ret = avio_open_dyn_buf(&dyn_buf);
1693  if (ret < 0) {
1694  avio_closep(&pb);
1695  return NULL;
1696  }
1697  while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
1698  avio_write(dyn_buf, buf, ret);
1699  avio_w8(dyn_buf, 0);
1700  avio_closep(&pb);
1701 
1702  ret = avio_close_dyn_buf(dyn_buf, &str);
1703  if (ret < 0)
1704  return NULL;
1705  return str;
1706 }
1707 
1709  OutputStream *ost)
1710 {
1711  AVStream *st = ost->st;
1712 
1713  if (ost->filters_script && ost->filters) {
1714  av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for "
1715  "output stream #%d:%d.\n", nb_output_files, st->index);
1716  exit_program(1);
1717  }
1718 
1719  if (ost->filters_script)
1720  return read_file(ost->filters_script);
1721  else if (ost->filters)
1722  return av_strdup(ost->filters);
1723 
1725  "null" : "anull");
1726 }
1727 
1729  const OutputStream *ost, enum AVMediaType type)
1730 {
1731  if (ost->filters_script || ost->filters) {
1733  "%s '%s' was defined for %s output stream %d:%d but codec copy was selected.\n"
1734  "Filtering and streamcopy cannot be used together.\n",
1735  ost->filters ? "Filtergraph" : "Filtergraph script",
1738  exit_program(1);
1739  }
1740 }
1741 
1743 {
1744  AVStream *st;
1745  OutputStream *ost;
1746  AVCodecContext *video_enc;
1747  char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
1748 
1749  ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
1750  st = ost->st;
1751  video_enc = ost->enc_ctx;
1752 
1753  MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
1754  if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
1755  av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
1756  exit_program(1);
1757  }
1758 
1759  MATCH_PER_STREAM_OPT(max_frame_rates, str, max_frame_rate, oc, st);
1760  if (max_frame_rate && av_parse_video_rate(&ost->max_frame_rate, max_frame_rate) < 0) {
1761  av_log(NULL, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
1762  exit_program(1);
1763  }
1764 
1765  if (frame_rate && max_frame_rate) {
1766  av_log(NULL, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
1767  exit_program(1);
1768  }
1769 
1770  if ((frame_rate || max_frame_rate) &&
1772  av_log(NULL, AV_LOG_ERROR, "Using -vsync 0 and -r/-fpsmax can produce invalid output files\n");
1773 
1774  MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
1775  if (frame_aspect_ratio) {
1776  AVRational q;
1777  if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1778  q.num <= 0 || q.den <= 0) {
1779  av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
1780  exit_program(1);
1781  }
1782  ost->frame_aspect_ratio = q;
1783  }
1784 
1785  MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
1787 
1788  if (!ost->stream_copy) {
1789  const char *p = NULL;
1790  char *frame_size = NULL;
1791  char *frame_pix_fmt = NULL;
1792  char *intra_matrix = NULL, *inter_matrix = NULL;
1793  char *chroma_intra_matrix = NULL;
1794  int do_pass = 0;
1795  int i;
1796 
1798  if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
1799  av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
1800  exit_program(1);
1801  }
1802 
1804  MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
1805  if (frame_pix_fmt && *frame_pix_fmt == '+') {
1806  ost->keep_pix_fmt = 1;
1807  if (!*++frame_pix_fmt)
1808  frame_pix_fmt = NULL;
1809  }
1810  if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
1811  av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
1812  exit_program(1);
1813  }
1814  st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
1815 
1816  if (intra_only)
1817  video_enc->gop_size = 0;
1818  MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
1819  if (intra_matrix) {
1820  if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
1821  av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
1822  exit_program(1);
1823  }
1824  parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
1825  }
1826  MATCH_PER_STREAM_OPT(chroma_intra_matrices, str, chroma_intra_matrix, oc, st);
1827  if (chroma_intra_matrix) {
1828  uint16_t *p = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64);
1829  if (!p) {
1830  av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
1831  exit_program(1);
1832  }
1833  video_enc->chroma_intra_matrix = p;
1834  parse_matrix_coeffs(p, chroma_intra_matrix);
1835  }
1836  MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
1837  if (inter_matrix) {
1838  if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
1839  av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
1840  exit_program(1);
1841  }
1842  parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
1843  }
1844 
1845  MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
1846  for (i = 0; p; i++) {
1847  int start, end, q;
1848  int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
1849  if (e != 3) {
1850  av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
1851  exit_program(1);
1852  }
1853  video_enc->rc_override =
1854  av_realloc_array(video_enc->rc_override,
1855  i + 1, sizeof(RcOverride));
1856  if (!video_enc->rc_override) {
1857  av_log(NULL, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
1858  exit_program(1);
1859  }
1860  video_enc->rc_override[i].start_frame = start;
1861  video_enc->rc_override[i].end_frame = end;
1862  if (q > 0) {
1863  video_enc->rc_override[i].qscale = q;
1864  video_enc->rc_override[i].quality_factor = 1.0;
1865  }
1866  else {
1867  video_enc->rc_override[i].qscale = 0;
1868  video_enc->rc_override[i].quality_factor = -q/100.0;
1869  }
1870  p = strchr(p, '/');
1871  if (p) p++;
1872  }
1873  video_enc->rc_override_count = i;
1874 
1875  if (do_psnr)
1876  video_enc->flags|= AV_CODEC_FLAG_PSNR;
1877 
1878  /* two pass mode */
1879  MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
1880  if (do_pass) {
1881  if (do_pass & 1) {
1882  video_enc->flags |= AV_CODEC_FLAG_PASS1;
1883  av_dict_set(&ost->encoder_opts, "flags", "+pass1", AV_DICT_APPEND);
1884  }
1885  if (do_pass & 2) {
1886  video_enc->flags |= AV_CODEC_FLAG_PASS2;
1887  av_dict_set(&ost->encoder_opts, "flags", "+pass2", AV_DICT_APPEND);
1888  }
1889  }
1890 
1891  MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
1892  if (ost->logfile_prefix &&
1894  exit_program(1);
1895 
1896  if (do_pass) {
1897  char logfilename[1024];
1898  FILE *f;
1899 
1900  snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1903  i);
1904  if (!strcmp(ost->enc->name, "libx264")) {
1905  av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
1906  } else {
1907  if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
1908  char *logbuffer = read_file(logfilename);
1909 
1910  if (!logbuffer) {
1911  av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
1912  logfilename);
1913  exit_program(1);
1914  }
1915  video_enc->stats_in = logbuffer;
1916  }
1917  if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
1918  f = av_fopen_utf8(logfilename, "wb");
1919  if (!f) {
1921  "Cannot write log file '%s' for pass-1 encoding: %s\n",
1922  logfilename, strerror(errno));
1923  exit_program(1);
1924  }
1925  ost->logfile = f;
1926  }
1927  }
1928  }
1929 
1930  MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st);
1931  if (ost->forced_keyframes)
1933 
1934  MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
1935 
1936  ost->top_field_first = -1;
1937  MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
1938 
1939 
1940  ost->avfilter = get_ost_filters(o, oc, ost);
1941  if (!ost->avfilter)
1942  exit_program(1);
1943  } else {
1944  MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
1945  }
1946 
1947  if (ost->stream_copy)
1949 
1950  return ost;
1951 }
1952 
1954 {
1955  int n;
1956  AVStream *st;
1957  OutputStream *ost;
1958  AVCodecContext *audio_enc;
1959 
1960  ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO, source_index);
1961  st = ost->st;
1962 
1963  audio_enc = ost->enc_ctx;
1964  audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
1965 
1966  MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
1968 
1969  if (!ost->stream_copy) {
1970  char *sample_fmt = NULL;
1971 
1972  MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
1973 
1974  MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
1975  if (sample_fmt &&
1976  (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
1977  av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
1978  exit_program(1);
1979  }
1980 
1981  MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
1982 
1983  MATCH_PER_STREAM_OPT(apad, str, ost->apad, oc, st);
1984  ost->apad = av_strdup(ost->apad);
1985 
1986  ost->avfilter = get_ost_filters(o, oc, ost);
1987  if (!ost->avfilter)
1988  exit_program(1);
1989 
1990  /* check for channel mapping for this audio stream */
1991  for (n = 0; n < o->nb_audio_channel_maps; n++) {
1993  if ((map->ofile_idx == -1 || ost->file_index == map->ofile_idx) &&
1994  (map->ostream_idx == -1 || ost->st->index == map->ostream_idx)) {
1995  InputStream *ist;
1996 
1997  if (map->channel_idx == -1) {
1998  ist = NULL;
1999  } else if (ost->source_index < 0) {
2000  av_log(NULL, AV_LOG_FATAL, "Cannot determine input stream for channel mapping %d.%d\n",
2001  ost->file_index, ost->st->index);
2002  continue;
2003  } else {
2005  }
2006 
2007  if (!ist || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) {
2010  sizeof(*ost->audio_channels_map)
2011  ) < 0 )
2012  exit_program(1);
2013 
2014  ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
2015  }
2016  }
2017  }
2018  }
2019 
2020  if (ost->stream_copy)
2022 
2023  return ost;
2024 }
2025 
2027 {
2028  OutputStream *ost;
2029 
2030  ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA, source_index);
2031  if (!ost->stream_copy) {
2032  av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
2033  exit_program(1);
2034  }
2035 
2036  return ost;
2037 }
2038 
2040 {
2041  OutputStream *ost;
2042 
2043  ost = new_output_stream(o, oc, AVMEDIA_TYPE_UNKNOWN, source_index);
2044  if (!ost->stream_copy) {
2045  av_log(NULL, AV_LOG_FATAL, "Unknown stream encoding not supported yet (only streamcopy)\n");
2046  exit_program(1);
2047  }
2048 
2049  return ost;
2050 }
2051 
2053 {
2054  OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT, source_index);
2055  ost->stream_copy = 1;
2056  ost->finished = 1;
2057  return ost;
2058 }
2059 
2061 {
2062  AVStream *st;
2063  OutputStream *ost;
2064  AVCodecContext *subtitle_enc;
2065 
2066  ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE, source_index);
2067  st = ost->st;
2068  subtitle_enc = ost->enc_ctx;
2069 
2070  subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2071 
2072  MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc, st);
2073 
2074  if (!ost->stream_copy) {
2075  char *frame_size = NULL;
2076 
2078  if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
2079  av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
2080  exit_program(1);
2081  }
2082  }
2083 
2084  return ost;
2085 }
2086 
2087 /* arg format is "output-stream-index:streamid-value". */
2088 static int opt_streamid(void *optctx, const char *opt, const char *arg)
2089 {
2090  OptionsContext *o = optctx;
2091  int idx;
2092  char *p;
2093  char idx_str[16];
2094 
2095  av_strlcpy(idx_str, arg, sizeof(idx_str));
2096  p = strchr(idx_str, ':');
2097  if (!p) {
2099  "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
2100  arg, opt);
2101  exit_program(1);
2102  }
2103  *p++ = '\0';
2104  idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
2105  o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
2106  o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
2107  return 0;
2108 }
2109 
2111 {
2112  AVFormatContext *is = ifile->ctx;
2113  AVFormatContext *os = ofile->ctx;
2114  AVChapter **tmp;
2115  int i;
2116 
2117  tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters));
2118  if (!tmp)
2119  return AVERROR(ENOMEM);
2120  os->chapters = tmp;
2121 
2122  for (i = 0; i < is->nb_chapters; i++) {
2123  AVChapter *in_ch = is->chapters[i], *out_ch;
2124  int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
2125  int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset,
2126  AV_TIME_BASE_Q, in_ch->time_base);
2127  int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
2128  av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
2129 
2130 
2131  if (in_ch->end < ts_off)
2132  continue;
2133  if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2134  break;
2135 
2136  out_ch = av_mallocz(sizeof(AVChapter));
2137  if (!out_ch)
2138  return AVERROR(ENOMEM);
2139 
2140  out_ch->id = in_ch->id;
2141  out_ch->time_base = in_ch->time_base;
2142  out_ch->start = FFMAX(0, in_ch->start - ts_off);
2143  out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2144 
2145  if (copy_metadata)
2146  av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2147 
2148  os->chapters[os->nb_chapters++] = out_ch;
2149  }
2150  return 0;
2151 }
2152 
2154  AVFormatContext *oc)
2155 {
2156  OutputStream *ost;
2157 
2158  switch (ofilter->type) {
2159  case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc, -1); break;
2160  case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc, -1); break;
2161  default:
2162  av_log(NULL, AV_LOG_FATAL, "Only video and audio filters are supported "
2163  "currently.\n");
2164  exit_program(1);
2165  }
2166 
2167  ost->source_index = -1;
2168  ost->filter = ofilter;
2169 
2170  ofilter->ost = ost;
2171  ofilter->format = -1;
2172 
2173  if (ost->stream_copy) {
2174  av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
2175  "which is fed from a complex filtergraph. Filtering and streamcopy "
2176  "cannot be used together.\n", ost->file_index, ost->index);
2177  exit_program(1);
2178  }
2179 
2180  if (ost->avfilter && (ost->filters || ost->filters_script)) {
2181  const char *opt = ost->filters ? "-vf/-af/-filter" : "-filter_script";
2183  "%s '%s' was specified through the %s option "
2184  "for output stream %d:%d, which is fed from a complex filtergraph.\n"
2185  "%s and -filter_complex cannot be used together for the same stream.\n",
2186  ost->filters ? "Filtergraph" : "Filtergraph script",
2188  opt, ost->file_index, ost->index, opt);
2189  exit_program(1);
2190  }
2191 
2192  avfilter_inout_free(&ofilter->out_tmp);
2193 }
2194 
2195 static int init_complex_filters(void)
2196 {
2197  int i, ret = 0;
2198 
2199  for (i = 0; i < nb_filtergraphs; i++) {
2201  if (ret < 0)
2202  return ret;
2203  }
2204  return 0;
2205 }
2206 
2207 static int open_output_file(OptionsContext *o, const char *filename)
2208 {
2209  AVFormatContext *oc;
2210  int i, j, err;
2211  OutputFile *of;
2212  OutputStream *ost;
2213  InputStream *ist;
2214  AVDictionary *unused_opts = NULL;
2215  AVDictionaryEntry *e = NULL;
2216 
2217  if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) {
2218  o->stop_time = INT64_MAX;
2219  av_log(NULL, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
2220  }
2221 
2222  if (o->stop_time != INT64_MAX && o->recording_time == INT64_MAX) {
2223  int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time;
2224  if (o->stop_time <= start_time) {
2225  av_log(NULL, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
2226  exit_program(1);
2227  } else {
2229  }
2230  }
2231 
2233  of = av_mallocz(sizeof(*of));
2234  if (!of)
2235  exit_program(1);
2236  output_files[nb_output_files - 1] = of;
2237 
2239  of->recording_time = o->recording_time;
2240  of->start_time = o->start_time;
2241  of->limit_filesize = o->limit_filesize;
2242  of->shortest = o->shortest;
2243  av_dict_copy(&of->opts, o->g->format_opts, 0);
2244 
2245  if (!strcmp(filename, "-"))
2246  filename = "pipe:";
2247 
2248  err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
2249  if (!oc) {
2250  print_error(filename, err);
2251  exit_program(1);
2252  }
2253 
2254  of->ctx = oc;
2255  if (o->recording_time != INT64_MAX)
2256  oc->duration = o->recording_time;
2257 
2258  oc->interrupt_callback = int_cb;
2259 
2260  if (o->bitexact) {
2261  oc->flags |= AVFMT_FLAG_BITEXACT;
2262  }
2263 
2264  /* create streams for all unlabeled output pads */
2265  for (i = 0; i < nb_filtergraphs; i++) {
2266  FilterGraph *fg = filtergraphs[i];
2267  for (j = 0; j < fg->nb_outputs; j++) {
2268  OutputFilter *ofilter = fg->outputs[j];
2269 
2270  if (!ofilter->out_tmp || ofilter->out_tmp->name)
2271  continue;
2272 
2273  switch (ofilter->type) {
2274  case AVMEDIA_TYPE_VIDEO: o->video_disable = 1; break;
2275  case AVMEDIA_TYPE_AUDIO: o->audio_disable = 1; break;
2276  case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
2277  }
2278  init_output_filter(ofilter, o, oc);
2279  }
2280  }
2281 
2282  if (!o->nb_stream_maps) {
2283  char *subtitle_codec_name = NULL;
2284  /* pick the "best" stream of each type */
2285 
2286  /* video: highest resolution */
2288  int best_score = 0, idx = -1;
2289  int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
2290  for (j = 0; j < nb_input_files; j++) {
2291  InputFile *ifile = input_files[j];
2292  int file_best_score = 0, file_best_idx = -1;
2293  for (i = 0; i < ifile->nb_streams; i++) {
2294  int score;
2295  ist = input_streams[ifile->ist_index + i];
2296  score = ist->st->codecpar->width * ist->st->codecpar->height
2297  + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2298  + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
2299  if (ist->user_set_discard == AVDISCARD_ALL)
2300  continue;
2301  if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2302  score = 1;
2303  if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2304  score > file_best_score) {
2305  if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2306  continue;
2307  file_best_score = score;
2308  file_best_idx = ifile->ist_index + i;
2309  }
2310  }
2311  if (file_best_idx >= 0) {
2312  if((qcr == MKTAG('A', 'P', 'I', 'C')) || !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2313  file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
2314  if (file_best_score > best_score) {
2315  best_score = file_best_score;
2316  idx = file_best_idx;
2317  }
2318  }
2319  }
2320  if (idx >= 0)
2321  new_video_stream(o, oc, idx);
2322  }
2323 
2324  /* audio: most channels */
2326  int best_score = 0, idx = -1;
2327  for (j = 0; j < nb_input_files; j++) {
2328  InputFile *ifile = input_files[j];
2329  int file_best_score = 0, file_best_idx = -1;
2330  for (i = 0; i < ifile->nb_streams; i++) {
2331  int score;
2332  ist = input_streams[ifile->ist_index + i];
2333  score = ist->st->codecpar->channels
2334  + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2335  + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
2336  if (ist->user_set_discard == AVDISCARD_ALL)
2337  continue;
2338  if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2339  score > file_best_score) {
2340  file_best_score = score;
2341  file_best_idx = ifile->ist_index + i;
2342  }
2343  }
2344  if (file_best_idx >= 0) {
2345  file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
2346  if (file_best_score > best_score) {
2347  best_score = file_best_score;
2348  idx = file_best_idx;
2349  }
2350  }
2351  }
2352  if (idx >= 0)
2353  new_audio_stream(o, oc, idx);
2354  }
2355 
2356  /* subtitles: pick first */
2357  MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, oc, "s");
2359  for (i = 0; i < nb_input_streams; i++)
2360  if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2361  AVCodecDescriptor const *input_descriptor =
2362  avcodec_descriptor_get(input_streams[i]->st->codecpar->codec_id);
2363  AVCodecDescriptor const *output_descriptor = NULL;
2364  AVCodec const *output_codec =
2366  int input_props = 0, output_props = 0;
2367  if (input_streams[i]->user_set_discard == AVDISCARD_ALL)
2368  continue;
2369  if (output_codec)
2370  output_descriptor = avcodec_descriptor_get(output_codec->id);
2371  if (input_descriptor)
2372  input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2373  if (output_descriptor)
2374  output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2375  if (subtitle_codec_name ||
2376  input_props & output_props ||
2377  // Map dvb teletext which has neither property to any output subtitle encoder
2378  input_descriptor && output_descriptor &&
2379  (!input_descriptor->props ||
2380  !output_descriptor->props)) {
2381  new_subtitle_stream(o, oc, i);
2382  break;
2383  }
2384  }
2385  }
2386  /* Data only if codec id match */
2387  if (!o->data_disable ) {
2389  for (i = 0; codec_id != AV_CODEC_ID_NONE && i < nb_input_streams; i++) {
2390  if (input_streams[i]->user_set_discard == AVDISCARD_ALL)
2391  continue;
2392  if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_DATA
2394  new_data_stream(o, oc, i);
2395  }
2396  }
2397  } else {
2398  for (i = 0; i < o->nb_stream_maps; i++) {
2399  StreamMap *map = &o->stream_maps[i];
2400 
2401  if (map->disabled)
2402  continue;
2403 
2404  if (map->linklabel) {
2405  FilterGraph *fg;
2406  OutputFilter *ofilter = NULL;
2407  int j, k;
2408 
2409  for (j = 0; j < nb_filtergraphs; j++) {
2410  fg = filtergraphs[j];
2411  for (k = 0; k < fg->nb_outputs; k++) {
2412  AVFilterInOut *out = fg->outputs[k]->out_tmp;
2413  if (out && !strcmp(out->name, map->linklabel)) {
2414  ofilter = fg->outputs[k];
2415  goto loop_end;
2416  }
2417  }
2418  }
2419 loop_end:
2420  if (!ofilter) {
2421  av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
2422  "in any defined filter graph, or was already used elsewhere.\n", map->linklabel);
2423  exit_program(1);
2424  }
2425  init_output_filter(ofilter, o, oc);
2426  } else {
2427  int src_idx = input_files[map->file_index]->ist_index + map->stream_index;
2428 
2429  ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
2430  if (ist->user_set_discard == AVDISCARD_ALL) {
2431  av_log(NULL, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n",
2432  map->file_index, map->stream_index);
2433  exit_program(1);
2434  }
2435  if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
2436  continue;
2437  if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2438  continue;
2439  if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2440  continue;
2441  if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2442  continue;
2443 
2444  ost = NULL;
2445  switch (ist->st->codecpar->codec_type) {
2446  case AVMEDIA_TYPE_VIDEO: ost = new_video_stream (o, oc, src_idx); break;
2447  case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream (o, oc, src_idx); break;
2448  case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream (o, oc, src_idx); break;
2449  case AVMEDIA_TYPE_DATA: ost = new_data_stream (o, oc, src_idx); break;
2450  case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc, src_idx); break;
2451  case AVMEDIA_TYPE_UNKNOWN:
2452  if (copy_unknown_streams) {
2453  ost = new_unknown_stream (o, oc, src_idx);
2454  break;
2455  }
2456  default:
2458  "Cannot map stream #%d:%d - unsupported type.\n",
2459  map->file_index, map->stream_index);
2460  if (!ignore_unknown_streams) {
2462  "If you want unsupported types ignored instead "
2463  "of failing, please use the -ignore_unknown option\n"
2464  "If you want them copied, please use -copy_unknown\n");
2465  exit_program(1);
2466  }
2467  }
2468  if (ost)
2469  ost->sync_ist = input_streams[ input_files[map->sync_file_index]->ist_index
2470  + map->sync_stream_index];
2471  }
2472  }
2473  }
2474 
2475  /* handle attached files */
2476  for (i = 0; i < o->nb_attachments; i++) {
2477  AVIOContext *pb;
2478  uint8_t *attachment;
2479  const char *p;
2480  int64_t len;
2481 
2482  if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
2483  av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
2484  o->attachments[i]);
2485  exit_program(1);
2486  }
2487  if ((len = avio_size(pb)) <= 0) {
2488  av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
2489  o->attachments[i]);
2490  exit_program(1);
2491  }
2492  if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
2493  !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
2494  av_log(NULL, AV_LOG_FATAL, "Attachment %s too large.\n",
2495  o->attachments[i]);
2496  exit_program(1);
2497  }
2498  avio_read(pb, attachment, len);
2499  memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2500 
2501  ost = new_attachment_stream(o, oc, -1);
2502  ost->stream_copy = 0;
2504  ost->st->codecpar->extradata = attachment;
2506 
2507  p = strrchr(o->attachments[i], '/');
2508  av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
2509  avio_closep(&pb);
2510  }
2511 
2512  if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2513  av_dump_format(oc, nb_output_files - 1, oc->url, 1);
2514  av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", nb_output_files - 1);
2515  exit_program(1);
2516  }
2517 
2518  /* check if all codec options have been used */
2519  unused_opts = strip_specifiers(o->g->codec_opts);
2520  for (i = of->ost_index; i < nb_output_streams; i++) {
2521  e = NULL;
2522  while ((e = av_dict_get(output_streams[i]->encoder_opts, "", e,
2524  av_dict_set(&unused_opts, e->key, NULL, 0);
2525  }
2526 
2527  e = NULL;
2528  while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
2529  const AVClass *class = avcodec_get_class();
2530  const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
2532  const AVClass *fclass = avformat_get_class();
2533  const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
2535  if (!option || foption)
2536  continue;
2537 
2538 
2539  if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
2540  av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
2541  "output file #%d (%s) is not an encoding option.\n", e->key,
2542  option->help ? option->help : "", nb_output_files - 1,
2543  filename);
2544  exit_program(1);
2545  }
2546 
2547  // gop_timecode is injected by generic code but not always used
2548  if (!strcmp(e->key, "gop_timecode"))
2549  continue;
2550 
2551  av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
2552  "output file #%d (%s) has not been used for any stream. The most "
2553  "likely reason is either wrong type (e.g. a video option with "
2554  "no video streams) or that it is a private option of some encoder "
2555  "which was not actually used for any stream.\n", e->key,
2556  option->help ? option->help : "", nb_output_files - 1, filename);
2557  }
2558  av_dict_free(&unused_opts);
2559 
2560  /* set the decoding_needed flags and create simple filtergraphs */
2561  for (i = of->ost_index; i < nb_output_streams; i++) {
2563 
2564  if (ost->encoding_needed && ost->source_index >= 0) {
2566  ist->decoding_needed |= DECODING_FOR_OST;
2567 
2571  if (err < 0) {
2573  "Error initializing a simple filtergraph between streams "
2574  "%d:%d->%d:%d\n", ist->file_index, ost->source_index,
2575  nb_output_files - 1, ost->st->index);
2576  exit_program(1);
2577  }
2578  }
2579  }
2580 
2581  /* set the filter output constraints */
2582  if (ost->filter) {
2583  OutputFilter *f = ost->filter;
2584  int count;
2585  switch (ost->enc_ctx->codec_type) {
2586  case AVMEDIA_TYPE_VIDEO:
2587  f->frame_rate = ost->frame_rate;
2588  f->width = ost->enc_ctx->width;
2589  f->height = ost->enc_ctx->height;
2590  if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
2591  f->format = ost->enc_ctx->pix_fmt;
2592  } else if (ost->enc->pix_fmts) {
2593  count = 0;
2594  while (ost->enc->pix_fmts[count] != AV_PIX_FMT_NONE)
2595  count++;
2596  f->formats = av_calloc(count + 1, sizeof(*f->formats));
2597  if (!f->formats)
2598  exit_program(1);
2599  memcpy(f->formats, ost->enc->pix_fmts, (count + 1) * sizeof(*f->formats));
2600  }
2601  break;
2602  case AVMEDIA_TYPE_AUDIO:
2604  f->format = ost->enc_ctx->sample_fmt;
2605  } else if (ost->enc->sample_fmts) {
2606  count = 0;
2607  while (ost->enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
2608  count++;
2609  f->formats = av_calloc(count + 1, sizeof(*f->formats));
2610  if (!f->formats)
2611  exit_program(1);
2612  memcpy(f->formats, ost->enc->sample_fmts, (count + 1) * sizeof(*f->formats));
2613  }
2614  if (ost->enc_ctx->sample_rate) {
2615  f->sample_rate = ost->enc_ctx->sample_rate;
2616  } else if (ost->enc->supported_samplerates) {
2617  count = 0;
2618  while (ost->enc->supported_samplerates[count])
2619  count++;
2620  f->sample_rates = av_calloc(count + 1, sizeof(*f->sample_rates));
2621  if (!f->sample_rates)
2622  exit_program(1);
2623  memcpy(f->sample_rates, ost->enc->supported_samplerates,
2624  (count + 1) * sizeof(*f->sample_rates));
2625  }
2626  if (ost->enc_ctx->channels) {
2627  f->channel_layout = av_get_default_channel_layout(ost->enc_ctx->channels);
2628  } else if (ost->enc->channel_layouts) {
2629  count = 0;
2630  while (ost->enc->channel_layouts[count])
2631  count++;
2632  f->channel_layouts = av_calloc(count + 1, sizeof(*f->channel_layouts));
2633  if (!f->channel_layouts)
2634  exit_program(1);
2635  memcpy(f->channel_layouts, ost->enc->channel_layouts,
2636  (count + 1) * sizeof(*f->channel_layouts));
2637  }
2638  break;
2639  }
2640  }
2641  }
2642 
2643  /* check filename in case of an image number is expected */
2644  if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2645  if (!av_filename_number_test(oc->url)) {
2646  print_error(oc->url, AVERROR(EINVAL));
2647  exit_program(1);
2648  }
2649  }
2650 
2653  "No input streams but output needs an input stream\n");
2654  exit_program(1);
2655  }
2656 
2657  if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2658  /* test if it already exists to avoid losing precious files */
2659  assert_file_overwrite(filename);
2660 
2661  /* open the file */
2662  if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
2663  &oc->interrupt_callback,
2664  &of->opts)) < 0) {
2665  print_error(filename, err);
2666  exit_program(1);
2667  }
2668  } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename))
2669  assert_file_overwrite(filename);
2670 
2671  if (o->mux_preload) {
2672  av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
2673  }
2674  oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
2675 
2676  /* copy metadata */
2677  for (i = 0; i < o->nb_metadata_map; i++) {
2678  char *p;
2679  int in_file_index = strtol(o->metadata_map[i].u.str, &p, 0);
2680 
2681  if (in_file_index >= nb_input_files) {
2682  av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d while processing metadata maps\n", in_file_index);
2683  exit_program(1);
2684  }
2685  copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc,
2686  in_file_index >= 0 ?
2687  input_files[in_file_index]->ctx : NULL, o);
2688  }
2689 
2690  /* copy chapters */
2691  if (o->chapters_input_file >= nb_input_files) {
2692  if (o->chapters_input_file == INT_MAX) {
2693  /* copy chapters from the first input file that has them*/
2694  o->chapters_input_file = -1;
2695  for (i = 0; i < nb_input_files; i++)
2696  if (input_files[i]->ctx->nb_chapters) {
2697  o->chapters_input_file = i;
2698  break;
2699  }
2700  } else {
2701  av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
2702  o->chapters_input_file);
2703  exit_program(1);
2704  }
2705  }
2706  if (o->chapters_input_file >= 0)
2709 
2710  /* copy global metadata by default */
2714  if(o->recording_time != INT64_MAX)
2715  av_dict_set(&oc->metadata, "duration", NULL, 0);
2716  av_dict_set(&oc->metadata, "creation_time", NULL, 0);
2717  av_dict_set(&oc->metadata, "company_name", NULL, 0);
2718  av_dict_set(&oc->metadata, "product_name", NULL, 0);
2719  av_dict_set(&oc->metadata, "product_version", NULL, 0);
2720  }
2721  if (!o->metadata_streams_manual)
2722  for (i = of->ost_index; i < nb_output_streams; i++) {
2723  InputStream *ist;
2724  if (output_streams[i]->source_index < 0) /* this is true e.g. for attached files */
2725  continue;
2728  if (!output_streams[i]->stream_copy) {
2729  av_dict_set(&output_streams[i]->st->metadata, "encoder", NULL, 0);
2730  }
2731  }
2732 
2733  /* process manually set programs */
2734  for (i = 0; i < o->nb_program; i++) {
2735  const char *p = o->program[i].u.str;
2736  int progid = i+1;
2737  AVProgram *program;
2738 
2739  while(*p) {
2740  const char *p2 = av_get_token(&p, ":");
2741  const char *to_dealloc = p2;
2742  char *key;
2743  if (!p2)
2744  break;
2745 
2746  if(*p) p++;
2747 
2748  key = av_get_token(&p2, "=");
2749  if (!key || !*p2) {
2750  av_freep(&to_dealloc);
2751  av_freep(&key);
2752  break;
2753  }
2754  p2++;
2755 
2756  if (!strcmp(key, "program_num"))
2757  progid = strtol(p2, NULL, 0);
2758  av_freep(&to_dealloc);
2759  av_freep(&key);
2760  }
2761 
2762  program = av_new_program(oc, progid);
2763 
2764  p = o->program[i].u.str;
2765  while(*p) {
2766  const char *p2 = av_get_token(&p, ":");
2767  const char *to_dealloc = p2;
2768  char *key;
2769  if (!p2)
2770  break;
2771  if(*p) p++;
2772 
2773  key = av_get_token(&p2, "=");
2774  if (!key) {
2776  "No '=' character in program string %s.\n",
2777  p2);
2778  exit_program(1);
2779  }
2780  if (!*p2)
2781  exit_program(1);
2782  p2++;
2783 
2784  if (!strcmp(key, "title")) {
2785  av_dict_set(&program->metadata, "title", p2, 0);
2786  } else if (!strcmp(key, "program_num")) {
2787  } else if (!strcmp(key, "st")) {
2788  int st_num = strtol(p2, NULL, 0);
2789  av_program_add_stream_index(oc, progid, st_num);
2790  } else {
2791  av_log(NULL, AV_LOG_FATAL, "Unknown program key %s.\n", key);
2792  exit_program(1);
2793  }
2794  av_freep(&to_dealloc);
2795  av_freep(&key);
2796  }
2797  }
2798 
2799  /* process manually set metadata */
2800  for (i = 0; i < o->nb_metadata; i++) {
2801  AVDictionary **m;
2802  char type, *val;
2803  const char *stream_spec;
2804  int index = 0, j, ret = 0;
2805 
2806  val = strchr(o->metadata[i].u.str, '=');
2807  if (!val) {
2808  av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
2809  o->metadata[i].u.str);
2810  exit_program(1);
2811  }
2812  *val++ = 0;
2813 
2814  parse_meta_type(o->metadata[i].specifier, &type, &index, &stream_spec);
2815  if (type == 's') {
2816  for (j = 0; j < oc->nb_streams; j++) {
2818  if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
2819  if (!strcmp(o->metadata[i].u.str, "rotate")) {
2820  char *tail;
2821  double theta = av_strtod(val, &tail);
2822  if (!*tail) {
2823  ost->rotate_overridden = 1;
2824  ost->rotate_override_value = theta;
2825  }
2826  } else {
2827  av_dict_set(&oc->streams[j]->metadata, o->metadata[i].u.str, *val ? val : NULL, 0);
2828  }
2829  } else if (ret < 0)
2830  exit_program(1);
2831  }
2832  }
2833  else {
2834  switch (type) {
2835  case 'g':
2836  m = &oc->metadata;
2837  break;
2838  case 'c':
2839  if (index < 0 || index >= oc->nb_chapters) {
2840  av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
2841  exit_program(1);
2842  }
2843  m = &oc->chapters[index]->metadata;
2844  break;
2845  case 'p':
2846  if (index < 0 || index >= oc->nb_programs) {
2847  av_log(NULL, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index);
2848  exit_program(1);
2849  }
2850  m = &oc->programs[index]->metadata;
2851  break;
2852  default:
2853  av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
2854  exit_program(1);
2855  }
2856  av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
2857  }
2858  }
2859 
2860  return 0;
2861 }
2862 
2863 static int opt_target(void *optctx, const char *opt, const char *arg)
2864 {
2865  OptionsContext *o = optctx;
2866  enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
2867  static const char *const frame_rates[] = { "25", "30000/1001", "24000/1001" };
2868 
2869  if (!strncmp(arg, "pal-", 4)) {
2870  norm = PAL;
2871  arg += 4;
2872  } else if (!strncmp(arg, "ntsc-", 5)) {
2873  norm = NTSC;
2874  arg += 5;
2875  } else if (!strncmp(arg, "film-", 5)) {
2876  norm = FILM;
2877  arg += 5;
2878  } else {
2879  /* Try to determine PAL/NTSC by peeking in the input files */
2880  if (nb_input_files) {
2881  int i, j;
2882  for (j = 0; j < nb_input_files; j++) {
2883  for (i = 0; i < input_files[j]->nb_streams; i++) {
2884  AVStream *st = input_files[j]->ctx->streams[i];
2885  int64_t fr;
2887  continue;
2888  fr = st->time_base.den * 1000LL / st->time_base.num;
2889  if (fr == 25000) {
2890  norm = PAL;
2891  break;
2892  } else if ((fr == 29970) || (fr == 23976)) {
2893  norm = NTSC;
2894  break;
2895  }
2896  }
2897  if (norm != UNKNOWN)
2898  break;
2899  }
2900  }
2901  if (norm != UNKNOWN)
2902  av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
2903  }
2904 
2905  if (norm == UNKNOWN) {
2906  av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
2907  av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
2908  av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
2909  exit_program(1);
2910  }
2911 
2912  if (!strcmp(arg, "vcd")) {
2913  opt_video_codec(o, "c:v", "mpeg1video");
2914  opt_audio_codec(o, "c:a", "mp2");
2915  parse_option(o, "f", "vcd", options);
2916 
2917  parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
2918  parse_option(o, "r", frame_rates[norm], options);
2919  opt_default(NULL, "g", norm == PAL ? "15" : "18");
2920 
2921  opt_default(NULL, "b:v", "1150000");
2922  opt_default(NULL, "maxrate:v", "1150000");
2923  opt_default(NULL, "minrate:v", "1150000");
2924  opt_default(NULL, "bufsize:v", "327680"); // 40*1024*8;
2925 
2926  opt_default(NULL, "b:a", "224000");
2927  parse_option(o, "ar", "44100", options);
2928  parse_option(o, "ac", "2", options);
2929 
2930  opt_default(NULL, "packetsize", "2324");
2931  opt_default(NULL, "muxrate", "1411200"); // 2352 * 75 * 8;
2932 
2933  /* We have to offset the PTS, so that it is consistent with the SCR.
2934  SCR starts at 36000, but the first two packs contain only padding
2935  and the first pack from the other stream, respectively, may also have
2936  been written before.
2937  So the real data starts at SCR 36000+3*1200. */
2938  o->mux_preload = (36000 + 3 * 1200) / 90000.0; // 0.44
2939  } else if (!strcmp(arg, "svcd")) {
2940 
2941  opt_video_codec(o, "c:v", "mpeg2video");
2942  opt_audio_codec(o, "c:a", "mp2");
2943  parse_option(o, "f", "svcd", options);
2944 
2945  parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
2946  parse_option(o, "r", frame_rates[norm], options);
2947  parse_option(o, "pix_fmt", "yuv420p", options);
2948  opt_default(NULL, "g", norm == PAL ? "15" : "18");
2949 
2950  opt_default(NULL, "b:v", "2040000");
2951  opt_default(NULL, "maxrate:v", "2516000");
2952  opt_default(NULL, "minrate:v", "0"); // 1145000;
2953  opt_default(NULL, "bufsize:v", "1835008"); // 224*1024*8;
2954  opt_default(NULL, "scan_offset", "1");
2955 
2956  opt_default(NULL, "b:a", "224000");
2957  parse_option(o, "ar", "44100", options);
2958 
2959  opt_default(NULL, "packetsize", "2324");
2960 
2961  } else if (!strcmp(arg, "dvd")) {
2962 
2963  opt_video_codec(o, "c:v", "mpeg2video");
2964  opt_audio_codec(o, "c:a", "ac3");
2965  parse_option(o, "f", "dvd", options);
2966 
2967  parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
2968  parse_option(o, "r", frame_rates[norm], options);
2969  parse_option(o, "pix_fmt", "yuv420p", options);
2970  opt_default(NULL, "g", norm == PAL ? "15" : "18");
2971 
2972  opt_default(NULL, "b:v", "6000000");
2973  opt_default(NULL, "maxrate:v", "9000000");
2974  opt_default(NULL, "minrate:v", "0"); // 1500000;
2975  opt_default(NULL, "bufsize:v", "1835008"); // 224*1024*8;
2976 
2977  opt_default(NULL, "packetsize", "2048"); // from www.mpucoder.com: DVD sectors contain 2048 bytes of data, this is also the size of one pack.
2978  opt_default(NULL, "muxrate", "10080000"); // from mplex project: data_rate = 1260000. mux_rate = data_rate * 8
2979 
2980  opt_default(NULL, "b:a", "448000");
2981  parse_option(o, "ar", "48000", options);
2982 
2983  } else if (!strncmp(arg, "dv", 2)) {
2984 
2985  parse_option(o, "f", "dv", options);
2986 
2987  parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
2988  parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
2989  norm == PAL ? "yuv420p" : "yuv411p", options);
2990  parse_option(o, "r", frame_rates[norm], options);
2991 
2992  parse_option(o, "ar", "48000", options);
2993  parse_option(o, "ac", "2", options);
2994 
2995  } else {
2996  av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
2997  return AVERROR(EINVAL);
2998  }
2999 
3002 
3003  return 0;
3004 }
3005 
3006 static int opt_vstats_file(void *optctx, const char *opt, const char *arg)
3007 {
3010  return 0;
3011 }
3012 
3013 static int opt_vstats(void *optctx, const char *opt, const char *arg)
3014 {
3015  char filename[40];
3016  time_t today2 = time(NULL);
3017  struct tm *today = localtime(&today2);
3018 
3019  if (!today) { // maybe tomorrow
3020  av_log(NULL, AV_LOG_FATAL, "Unable to get current time: %s\n", strerror(errno));
3021  exit_program(1);
3022  }
3023 
3024  snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
3025  today->tm_sec);
3026  return opt_vstats_file(NULL, opt, filename);
3027 }
3028 
3029 static int opt_video_frames(void *optctx, const char *opt, const char *arg)
3030 {
3031  OptionsContext *o = optctx;
3032  return parse_option(o, "frames:v", arg, options);
3033 }
3034 
3035 static int opt_audio_frames(void *optctx, const char *opt, const char *arg)
3036 {
3037  OptionsContext *o = optctx;
3038  return parse_option(o, "frames:a", arg, options);
3039 }
3040 
3041 static int opt_data_frames(void *optctx, const char *opt, const char *arg)
3042 {
3043  OptionsContext *o = optctx;
3044  return parse_option(o, "frames:d", arg, options);
3045 }
3046 
3047 static int opt_default_new(OptionsContext *o, const char *opt, const char *arg)
3048 {
3049  int ret;
3050  AVDictionary *cbak = codec_opts;
3051  AVDictionary *fbak = format_opts;
3052  codec_opts = NULL;
3053  format_opts = NULL;
3054 
3055  ret = opt_default(NULL, opt, arg);
3056 
3057  av_dict_copy(&o->g->codec_opts , codec_opts, 0);
3061  codec_opts = cbak;
3062  format_opts = fbak;
3063 
3064  return ret;
3065 }
3066 
3067 static int opt_preset(void *optctx, const char *opt, const char *arg)
3068 {
3069  OptionsContext *o = optctx;
3070  FILE *f=NULL;
3071  char filename[1000], line[1000], tmp_line[1000];
3072  const char *codec_name = NULL;
3073 
3074  tmp_line[0] = *opt;
3075  tmp_line[1] = 0;
3076  MATCH_PER_TYPE_OPT(codec_names, str, codec_name, NULL, tmp_line);
3077 
3078  if (!(f = get_preset_file(filename, sizeof(filename), arg, *opt == 'f', codec_name))) {
3079  if(!strncmp(arg, "libx264-lossless", strlen("libx264-lossless"))){
3080  av_log(NULL, AV_LOG_FATAL, "Please use -preset <speed> -qp 0\n");
3081  }else
3082  av_log(NULL, AV_LOG_FATAL, "File for preset '%s' not found\n", arg);
3083  exit_program(1);
3084  }
3085 
3086  while (fgets(line, sizeof(line), f)) {
3087  char *key = tmp_line, *value, *endptr;
3088 
3089  if (strcspn(line, "#\n\r") == 0)
3090  continue;
3091  av_strlcpy(tmp_line, line, sizeof(tmp_line));
3092  if (!av_strtok(key, "=", &value) ||
3093  !av_strtok(value, "\r\n", &endptr)) {
3094  av_log(NULL, AV_LOG_FATAL, "%s: Invalid syntax: '%s'\n", filename, line);
3095  exit_program(1);
3096  }
3097  av_log(NULL, AV_LOG_DEBUG, "ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
3098 
3099  if (!strcmp(key, "acodec")) opt_audio_codec (o, key, value);
3100  else if (!strcmp(key, "vcodec")) opt_video_codec (o, key, value);
3101  else if (!strcmp(key, "scodec")) opt_subtitle_codec(o, key, value);
3102  else if (!strcmp(key, "dcodec")) opt_data_codec (o, key, value);
3103  else if (opt_default_new(o, key, value) < 0) {
3104  av_log(NULL, AV_LOG_FATAL, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
3105  filename, line, key, value);
3106  exit_program(1);
3107  }
3108  }
3109 
3110  fclose(f);
3111 
3112  return 0;
3113 }
3114 
3115 static int opt_old2new(void *optctx, const char *opt, const char *arg)
3116 {
3117  OptionsContext *o = optctx;
3118  int ret;
3119  char *s = av_asprintf("%s:%c", opt + 1, *opt);
3120  if (!s)
3121  return AVERROR(ENOMEM);
3122  ret = parse_option(o, s, arg, options);
3123  av_free(s);
3124  return ret;
3125 }
3126 
3127 static int opt_bitrate(void *optctx, const char *opt, const char *arg)
3128 {
3129  OptionsContext *o = optctx;
3130 
3131  if(!strcmp(opt, "ab")){
3132  av_dict_set(&o->g->codec_opts, "b:a", arg, 0);
3133  return 0;
3134  } else if(!strcmp(opt, "b")){
3135  av_log(NULL, AV_LOG_WARNING, "Please use -b:a or -b:v, -b is ambiguous\n");
3136  av_dict_set(&o->g->codec_opts, "b:v", arg, 0);
3137  return 0;
3138  }
3139  av_dict_set(&o->g->codec_opts, opt, arg, 0);
3140  return 0;
3141 }
3142 
3143 static int opt_qscale(void *optctx, const char *opt, const char *arg)
3144 {
3145  OptionsContext *o = optctx;
3146  char *s;
3147  int ret;
3148  if(!strcmp(opt, "qscale")){
3149  av_log(NULL, AV_LOG_WARNING, "Please use -q:a or -q:v, -qscale is ambiguous\n");
3150  return parse_option(o, "q:v", arg, options);
3151  }
3152  s = av_asprintf("q%s", opt + 6);
3153  if (!s)
3154  return AVERROR(ENOMEM);
3155  ret = parse_option(o, s, arg, options);
3156  av_free(s);
3157  return ret;
3158 }
3159 
3160 static int opt_profile(void *optctx, const char *opt, const char *arg)
3161 {
3162  OptionsContext *o = optctx;
3163  if(!strcmp(opt, "profile")){
3164  av_log(NULL, AV_LOG_WARNING, "Please use -profile:a or -profile:v, -profile is ambiguous\n");
3165  av_dict_set(&o->g->codec_opts, "profile:v", arg, 0);
3166  return 0;
3167  }
3168  av_dict_set(&o->g->codec_opts, opt, arg, 0);
3169  return 0;
3170 }
3171 
3172 static int opt_video_filters(void *optctx, const char *opt, const char *arg)
3173 {
3174  OptionsContext *o = optctx;
3175  return parse_option(o, "filter:v", arg, options);
3176 }
3177 
3178 static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
3179 {
3180  OptionsContext *o = optctx;
3181  return parse_option(o, "filter:a", arg, options);
3182 }
3183 
3184 static int opt_vsync(void *optctx, const char *opt, const char *arg)
3185 {
3186  if (!av_strcasecmp(arg, "cfr")) video_sync_method = VSYNC_CFR;
3187  else if (!av_strcasecmp(arg, "vfr")) video_sync_method = VSYNC_VFR;
3188  else if (!av_strcasecmp(arg, "passthrough")) video_sync_method = VSYNC_PASSTHROUGH;
3189  else if (!av_strcasecmp(arg, "drop")) video_sync_method = VSYNC_DROP;
3190 
3193  return 0;
3194 }
3195 
3196 static int opt_timecode(void *optctx, const char *opt, const char *arg)
3197 {
3198  OptionsContext *o = optctx;
3199  int ret;
3200  char *tcr = av_asprintf("timecode=%s", arg);
3201  if (!tcr)
3202  return AVERROR(ENOMEM);
3203  ret = parse_option(o, "metadata:g", tcr, options);
3204  if (ret >= 0)
3205  ret = av_dict_set(&o->g->codec_opts, "gop_timecode", arg, 0);
3206  av_free(tcr);
3207  return ret;
3208 }
3209 
3210 static int opt_channel_layout(void *optctx, const char *opt, const char *arg)
3211 {
3212  OptionsContext *o = optctx;
3213  char layout_str[32];
3214  char *stream_str;
3215  char *ac_str;
3216  int ret, channels, ac_str_size;
3217  uint64_t layout;
3218 
3220  if (!layout) {
3221  av_log(NULL, AV_LOG_ERROR, "Unknown channel layout: %s\n", arg);
3222  return AVERROR(EINVAL);
3223  }
3224  snprintf(layout_str, sizeof(layout_str), "%"PRIu64, layout);
3225  ret = opt_default_new(o, opt, layout_str);
3226  if (ret < 0)
3227  return ret;
3228 
3229  /* set 'ac' option based on channel layout */
3231  snprintf(layout_str, sizeof(layout_str), "%d", channels);
3232  stream_str = strchr(opt, ':');
3233  ac_str_size = 3 + (stream_str ? strlen(stream_str) : 0);
3234  ac_str = av_mallocz(ac_str_size);
3235  if (!ac_str)
3236  return AVERROR(ENOMEM);
3237  av_strlcpy(ac_str, "ac", 3);
3238  if (stream_str)
3239  av_strlcat(ac_str, stream_str, ac_str_size);
3240  ret = parse_option(o, ac_str, layout_str, options);
3241  av_free(ac_str);
3242 
3243  return ret;
3244 }
3245 
3246 static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
3247 {
3248  OptionsContext *o = optctx;
3249  return parse_option(o, "q:a", arg, options);
3250 }
3251 
3252 static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
3253 {
3255  if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
3256  return AVERROR(ENOMEM);
3259  if (!filtergraphs[nb_filtergraphs - 1]->graph_desc)
3260  return AVERROR(ENOMEM);
3261 
3263 
3264  return 0;
3265 }
3266 
3267 static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
3268 {
3269  uint8_t *graph_desc = read_file(arg);
3270  if (!graph_desc)
3271  return AVERROR(EINVAL);
3272 
3274  if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
3275  return AVERROR(ENOMEM);
3277  filtergraphs[nb_filtergraphs - 1]->graph_desc = graph_desc;
3278 
3280 
3281  return 0;
3282 }
3283 
3284 void show_help_default(const char *opt, const char *arg)
3285 {
3286  /* per-file options have at least one of those set */
3287  const int per_file = OPT_SPEC | OPT_OFFSET | OPT_PERFILE;
3288  int show_advanced = 0, show_avoptions = 0;
3289 
3290  if (opt && *opt) {
3291  if (!strcmp(opt, "long"))
3292  show_advanced = 1;
3293  else if (!strcmp(opt, "full"))
3294  show_advanced = show_avoptions = 1;
3295  else
3296  av_log(NULL, AV_LOG_ERROR, "Unknown help option '%s'.\n", opt);
3297  }
3298 
3299  show_usage();
3300 
3301  printf("Getting help:\n"
3302  " -h -- print basic options\n"
3303  " -h long -- print more options\n"
3304  " -h full -- print all options (including all format and codec specific options, very long)\n"
3305  " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol\n"
3306  " See man %s for detailed description of the options.\n"
3307  "\n", program_name);
3308 
3309  show_help_options(options, "Print help / information / capabilities:",
3310  OPT_EXIT, 0, 0);
3311 
3312  show_help_options(options, "Global options (affect whole program "
3313  "instead of just one file):",
3314  0, per_file | OPT_EXIT | OPT_EXPERT, 0);
3315  if (show_advanced)
3316  show_help_options(options, "Advanced global options:", OPT_EXPERT,
3317  per_file | OPT_EXIT, 0);
3318 
3319  show_help_options(options, "Per-file main options:", 0,
3321  OPT_EXIT, per_file);
3322  if (show_advanced)
3323  show_help_options(options, "Advanced per-file options:",
3324  OPT_EXPERT, OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE, per_file);
3325 
3326  show_help_options(options, "Video options:",
3328  if (show_advanced)
3329  show_help_options(options, "Advanced Video options:",
3331 
3332  show_help_options(options, "Audio options:",
3334  if (show_advanced)
3335  show_help_options(options, "Advanced Audio options:",
3337  show_help_options(options, "Subtitle options:",
3338  OPT_SUBTITLE, 0, 0);
3339  printf("\n");
3340 
3341  if (show_avoptions) {
3345 #if CONFIG_SWSCALE
3347 #endif
3348 #if CONFIG_SWRESAMPLE
3350 #endif
3353  }
3354 }
3355 
3356 void show_usage(void)
3357 {
3358  av_log(NULL, AV_LOG_INFO, "Hyper fast Audio and Video encoder\n");
3359  av_log(NULL, AV_LOG_INFO, "usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
3360  av_log(NULL, AV_LOG_INFO, "\n");
3361 }
3362 
3363 enum OptGroup {
3364  GROUP_OUTFILE,
3365  GROUP_INFILE,
3366 };
3367 
3368 static const OptionGroupDef groups[] = {
3369  [GROUP_OUTFILE] = { "output url", NULL, OPT_OUTPUT },
3370  [GROUP_INFILE] = { "input url", "i", OPT_INPUT },
3371 };
3372 
3373 static int open_files(OptionGroupList *l, const char *inout,
3374  int (*open_file)(OptionsContext*, const char*))
3375 {
3376  int i, ret;
3377 
3378  for (i = 0; i < l->nb_groups; i++) {
3379  OptionGroup *g = &l->groups[i];
3380  OptionsContext o;
3381 
3382  init_options(&o);
3383  o.g = g;
3384 
3385  ret = parse_optgroup(&o, g);
3386  if (ret < 0) {
3387  av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
3388  "%s.\n", inout, g->arg);
3389  uninit_options(&o);
3390  return ret;
3391  }
3392 
3393  av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
3394  ret = open_file(&o, g->arg);
3395  uninit_options(&o);
3396  if (ret < 0) {
3397  av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
3398  inout, g->arg);
3399  return ret;
3400  }
3401  av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
3402  }
3403 
3404  return 0;
3405 }
3406 
3407 int ffmpeg_parse_options(int argc, char **argv)
3408 {
3409  OptionParseContext octx;
3410  uint8_t error[128];
3411  int ret;
3412 
3413  memset(&octx, 0, sizeof(octx));
3414 
3415  /* split the commandline into an internal representation */
3416  ret = split_commandline(&octx, argc, argv, options, groups,
3417  FF_ARRAY_ELEMS(groups));
3418  if (ret < 0) {
3419  av_log(NULL, AV_LOG_FATAL, "Error splitting the argument list: ");
3420  goto fail;
3421  }
3422 
3423  /* apply global options */
3424  ret = parse_optgroup(NULL, &octx.global_opts);
3425  if (ret < 0) {
3426  av_log(NULL, AV_LOG_FATAL, "Error parsing global options: ");
3427  goto fail;
3428  }
3429 
3430  /* configure terminal and setup signal handlers */
3431  term_init();
3432 
3433  /* open input files */
3434  ret = open_files(&octx.groups[GROUP_INFILE], "input", open_input_file);
3435  if (ret < 0) {
3436  av_log(NULL, AV_LOG_FATAL, "Error opening input files: ");
3437  goto fail;
3438  }
3439 
3440  /* create the complex filtergraphs */
3442  if (ret < 0) {
3443  av_log(NULL, AV_LOG_FATAL, "Error initializing complex filters.\n");
3444  goto fail;
3445  }
3446 
3447  /* open output files */
3448  ret = open_files(&octx.groups[GROUP_OUTFILE], "output", open_output_file);
3449  if (ret < 0) {
3450  av_log(NULL, AV_LOG_FATAL, "Error opening output files: ");
3451  goto fail;
3452  }
3453 
3455 
3456 fail:
3457  uninit_parse_context(&octx);
3458  if (ret < 0) {
3459  av_strerror(ret, error, sizeof(error));
3460  av_log(NULL, AV_LOG_FATAL, "%s\n", error);
3461  }
3462  return ret;
3463 }
3464 
3465 static int opt_progress(void *optctx, const char *opt, const char *arg)
3466 {
3467  AVIOContext *avio = NULL;
3468  int ret;
3469 
3470  if (!strcmp(arg, "-"))
3471  arg = "pipe:";
3472  ret = avio_open2(&avio, arg, AVIO_FLAG_WRITE, &int_cb, NULL);
3473  if (ret < 0) {
3474  av_log(NULL, AV_LOG_ERROR, "Failed to open progress URL \"%s\": %s\n",
3475  arg, av_err2str(ret));
3476  return ret;
3477  }
3478  progress_avio = avio;
3479  return 0;
3480 }
3481 
3482 #define OFFSET(x) offsetof(OptionsContext, x)
3483 const OptionDef options[] = {
3484  /* main options */
3486  { "f", HAS_ARG | OPT_STRING | OPT_OFFSET |
3487  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(format) },
3488  "force format", "fmt" },
3489  { "y", OPT_BOOL, { &file_overwrite },
3490  "overwrite output files" },
3491  { "n", OPT_BOOL, { &no_file_overwrite },
3492  "never overwrite output files" },
3493  { "ignore_unknown", OPT_BOOL, { &ignore_unknown_streams },
3494  "Ignore unknown stream types" },
3495  { "copy_unknown", OPT_BOOL | OPT_EXPERT, { &copy_unknown_streams },
3496  "Copy unknown stream types" },
3497  { "recast_media", OPT_BOOL | OPT_EXPERT, { &recast_media },
3498  "allow recasting stream type in order to force a decoder of different media type" },
3499  { "c", HAS_ARG | OPT_STRING | OPT_SPEC |
3500  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
3501  "codec name", "codec" },
3502  { "codec", HAS_ARG | OPT_STRING | OPT_SPEC |
3503  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
3504  "codec name", "codec" },
3505  { "pre", HAS_ARG | OPT_STRING | OPT_SPEC |
3506  OPT_OUTPUT, { .off = OFFSET(presets) },
3507  "preset name", "preset" },
3508  { "map", HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3509  OPT_OUTPUT, { .func_arg = opt_map },
3510  "set input stream mapping",
3511  "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
3512  { "map_channel", HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_map_channel },
3513  "map an audio channel from one stream to another", "file.stream.channel[:syncfile.syncstream]" },
3514  { "map_metadata", HAS_ARG | OPT_STRING | OPT_SPEC |
3515  OPT_OUTPUT, { .off = OFFSET(metadata_map) },
3516  "set metadata information of outfile from infile",
3517  "outfile[,metadata]:infile[,metadata]" },
3518  { "map_chapters", HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET |
3519  OPT_OUTPUT, { .off = OFFSET(chapters_input_file) },
3520  "set chapters mapping", "input_file_index" },
3521  { "t", HAS_ARG | OPT_TIME | OPT_OFFSET |
3522  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(recording_time) },
3523  "record or transcode \"duration\" seconds of audio/video",
3524  "duration" },
3525  { "to", HAS_ARG | OPT_TIME | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(stop_time) },
3526  "record or transcode stop time", "time_stop" },
3527  { "fs", HAS_ARG | OPT_INT64 | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(limit_filesize) },
3528  "set the limit file size in bytes", "limit_size" },
3529  { "ss", HAS_ARG | OPT_TIME | OPT_OFFSET |
3530  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(start_time) },
3531  "set the start time offset", "time_off" },
3532  { "sseof", HAS_ARG | OPT_TIME | OPT_OFFSET |
3533  OPT_INPUT, { .off = OFFSET(start_time_eof) },
3534  "set the start time offset relative to EOF", "time_off" },
3535  { "seek_timestamp", HAS_ARG | OPT_INT | OPT_OFFSET |
3536  OPT_INPUT, { .off = OFFSET(seek_timestamp) },
3537  "enable/disable seeking by timestamp with -ss" },
3538  { "accurate_seek", OPT_BOOL | OPT_OFFSET | OPT_EXPERT |
3539  OPT_INPUT, { .off = OFFSET(accurate_seek) },
3540  "enable/disable accurate seeking with -ss" },
3541  { "itsoffset", HAS_ARG | OPT_TIME | OPT_OFFSET |
3542  OPT_EXPERT | OPT_INPUT, { .off = OFFSET(input_ts_offset) },
3543  "set the input ts offset", "time_off" },
3544  { "itsscale", HAS_ARG | OPT_DOUBLE | OPT_SPEC |
3545  OPT_EXPERT | OPT_INPUT, { .off = OFFSET(ts_scale) },
3546  "set the input ts scale", "scale" },
3547  { "timestamp", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_recording_timestamp },
3548  "set the recording timestamp ('now' to set the current time)", "time" },
3549  { "metadata", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(metadata) },
3550  "add metadata", "string=string" },
3551  { "program", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(program) },
3552  "add program with specified streams", "title=string:st=number..." },
3553  { "dframes", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
3554  OPT_OUTPUT, { .func_arg = opt_data_frames },
3555  "set the number of data frames to output", "number" },
3556  { "benchmark", OPT_BOOL | OPT_EXPERT, { &do_benchmark },
3557  "add timings for benchmarking" },
3558  { "benchmark_all", OPT_BOOL | OPT_EXPERT, { &do_benchmark_all },
3559  "add timings for each task" },
3560  { "progress", HAS_ARG | OPT_EXPERT, { .func_arg = opt_progress },
3561  "write program-readable progress information", "url" },
3562  { "stdin", OPT_BOOL | OPT_EXPERT, { &stdin_interaction },
3563  "enable or disable interaction on standard input" },
3564  { "timelimit", HAS_ARG | OPT_EXPERT, { .func_arg = opt_timelimit },
3565  "set max runtime in seconds in CPU user time", "limit" },
3566  { "dump", OPT_BOOL | OPT_EXPERT, { &do_pkt_dump },
3567  "dump each input packet" },
3568  { "hex", OPT_BOOL | OPT_EXPERT, { &do_hex_dump },
3569  "when dumping packets, also dump the payload" },
3570  { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
3571  OPT_INPUT, { .off = OFFSET(rate_emu) },
3572  "read input at native frame rate; equivalent to -readrate 1", "" },
3573  { "readrate", HAS_ARG | OPT_FLOAT | OPT_OFFSET |
3574  OPT_EXPERT | OPT_INPUT, { .off = OFFSET(readrate) },
3575  "read input at specified rate", "speed" },
3576  { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target },
3577  "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" "
3578  "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")", "type" },
3579  { "vsync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vsync },
3580  "video sync method", "" },
3581  { "frame_drop_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &frame_drop_threshold },
3582  "frame drop threshold", "" },
3583  { "async", HAS_ARG | OPT_INT | OPT_EXPERT, { &audio_sync_method },
3584  "audio sync method", "" },
3585  { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &audio_drift_threshold },
3586  "audio drift threshold", "threshold" },
3587  { "copyts", OPT_BOOL | OPT_EXPERT, { &copy_ts },
3588  "copy timestamps" },
3589  { "start_at_zero", OPT_BOOL | OPT_EXPERT, { &start_at_zero },
3590  "shift input timestamps to start at 0 when using copyts" },
3591  { "copytb", HAS_ARG | OPT_INT | OPT_EXPERT, { &copy_tb },
3592  "copy input stream time base when stream copying", "mode" },
3593  { "shortest", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
3594  OPT_OUTPUT, { .off = OFFSET(shortest) },
3595  "finish encoding within shortest input" },
3596  { "bitexact", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
3597  OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(bitexact) },
3598  "bitexact mode" },
3599  { "apad", OPT_STRING | HAS_ARG | OPT_SPEC |
3600  OPT_OUTPUT, { .off = OFFSET(apad) },
3601  "audio pad", "" },
3602  { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &dts_delta_threshold },
3603  "timestamp discontinuity delta threshold", "threshold" },
3604  { "dts_error_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &dts_error_threshold },
3605  "timestamp error delta threshold", "threshold" },
3606  { "xerror", OPT_BOOL | OPT_EXPERT, { &exit_on_error },
3607  "exit on error", "error" },
3608  { "abort_on", HAS_ARG | OPT_EXPERT, { .func_arg = opt_abort_on },
3609  "abort on the specified condition flags", "flags" },
3610  { "copyinkf", OPT_BOOL | OPT_EXPERT | OPT_SPEC |
3611  OPT_OUTPUT, { .off = OFFSET(copy_initial_nonkeyframes) },
3612  "copy initial non-keyframes" },
3613  { "copypriorss", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(copy_prior_start) },
3614  "copy or discard frames before start time" },
3615  { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(max_frames) },
3616  "set the number of frames to output", "number" },
3617  { "tag", OPT_STRING | HAS_ARG | OPT_SPEC |
3618  OPT_EXPERT | OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(codec_tags) },
3619  "force codec tag/fourcc", "fourcc/tag" },
3620  { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
3621  OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(qscale) },
3622  "use fixed quality scale (VBR)", "q" },
3623  { "qscale", HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3624  OPT_OUTPUT, { .func_arg = opt_qscale },
3625  "use fixed quality scale (VBR)", "q" },
3626  { "profile", HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_profile },
3627  "set profile", "profile" },
3628  { "filter", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
3629  "set stream filtergraph", "filter_graph" },
3630  { "filter_threads", HAS_ARG, { .func_arg = opt_filter_threads },
3631  "number of non-complex filter threads" },
3632  { "filter_script", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
3633  "read stream filtergraph description from a file", "filename" },
3634  { "reinit_filter", HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT, { .off = OFFSET(reinit_filters) },
3635  "reinit filtergraph on input parameter changes", "" },
3636  { "filter_complex", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex },
3637  "create a complex filtergraph", "graph_description" },
3638  { "filter_complex_threads", HAS_ARG | OPT_INT, { &filter_complex_nbthreads },
3639  "number of threads for -filter_complex" },
3640  { "lavfi", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex },
3641  "create a complex filtergraph", "graph_description" },
3642  { "filter_complex_script", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex_script },
3643  "read complex filtergraph description from a file", "filename" },
3644  { "auto_conversion_filters", OPT_BOOL | OPT_EXPERT, { &auto_conversion_filters },
3645  "enable automatic conversion filters globally" },
3646  { "stats", OPT_BOOL, { &print_stats },
3647  "print progress report during encoding", },
3648  { "stats_period", HAS_ARG | OPT_EXPERT, { .func_arg = opt_stats_period },
3649  "set the period at which ffmpeg updates stats and -progress output", "time" },
3650  { "attach", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
3651  OPT_OUTPUT, { .func_arg = opt_attach },
3652  "add an attachment to the output file", "filename" },
3653  { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |
3654  OPT_EXPERT | OPT_INPUT, { .off = OFFSET(dump_attachment) },
3655  "extract an attachment into a file", "filename" },
3656  { "stream_loop", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_INPUT |
3657  OPT_OFFSET, { .off = OFFSET(loop) }, "set number of times input stream shall be looped", "loop count" },
3658  { "debug_ts", OPT_BOOL | OPT_EXPERT, { &debug_ts },
3659  "print timestamp debugging info" },
3660  { "max_error_rate", HAS_ARG | OPT_FLOAT, { &max_error_rate },
3661  "ratio of decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.", "maximum error rate" },
3662  { "discard", OPT_STRING | HAS_ARG | OPT_SPEC |
3663  OPT_INPUT, { .off = OFFSET(discard) },
3664  "discard", "" },
3665  { "disposition", OPT_STRING | HAS_ARG | OPT_SPEC |
3666  OPT_OUTPUT, { .off = OFFSET(disposition) },
3667  "disposition", "" },
3668  { "thread_queue_size", HAS_ARG | OPT_INT | OPT_OFFSET | OPT_EXPERT | OPT_INPUT,
3669  { .off = OFFSET(thread_queue_size) },
3670  "set the maximum number of queued packets from the demuxer" },
3671  { "find_stream_info", OPT_BOOL | OPT_PERFILE | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
3672  "read and decode the streams to fill missing information with heuristics" },
3673 
3674  /* video options */
3675  { "vframes", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_frames },
3676  "set the number of video frames to output", "number" },
3677  { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
3678  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },
3679  "set frame rate (Hz value, fraction or abbreviation)", "rate" },
3680  { "fpsmax", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
3681  OPT_OUTPUT, { .off = OFFSET(max_frame_rates) },
3682  "set max frame rate (Hz value, fraction or abbreviation)", "rate" },
3684  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },
3685  "set frame size (WxH or abbreviation)", "size" },
3686  { "aspect", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
3687  OPT_OUTPUT, { .off = OFFSET(frame_aspect_ratios) },
3688  "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
3689  { "pix_fmt", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
3690  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_pix_fmts) },
3691  "set pixel format", "format" },
3692  { "bits_per_raw_sample", OPT_VIDEO | OPT_INT | HAS_ARG, { &frame_bits_per_raw_sample },
3693  "set the number of bits per raw sample", "number" },
3694  { "intra", OPT_VIDEO | OPT_BOOL | OPT_EXPERT, { &intra_only },
3695  "deprecated use -g 1" },
3697  "disable video" },
3698  { "rc_override", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
3699  OPT_OUTPUT, { .off = OFFSET(rc_overrides) },
3700  "rate control override for specific intervals", "override" },
3701  { "vcodec", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_INPUT |
3702  OPT_OUTPUT, { .func_arg = opt_video_codec },
3703  "force video codec ('copy' to copy stream)", "codec" },
3704  { "sameq", OPT_VIDEO | OPT_EXPERT , { .func_arg = opt_sameq },
3705  "Removed" },
3706  { "same_quant", OPT_VIDEO | OPT_EXPERT , { .func_arg = opt_sameq },
3707  "Removed" },
3708  { "timecode", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_timecode },
3709  "set initial TimeCode value.", "hh:mm:ss[:;.]ff" },
3710  { "pass", OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT | OPT_OUTPUT, { .off = OFFSET(pass) },
3711  "select the pass number (1 to 3)", "n" },
3712  { "passlogfile", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC |
3713  OPT_OUTPUT, { .off = OFFSET(passlogfiles) },
3714  "select two pass log file name prefix", "prefix" },
3715  { "deinterlace", OPT_VIDEO | OPT_BOOL | OPT_EXPERT, { &do_deinterlace },
3716  "this option is deprecated, use the yadif filter instead" },
3717  { "psnr", OPT_VIDEO | OPT_BOOL | OPT_EXPERT, { &do_psnr },
3718  "calculate PSNR of compressed frames" },
3719  { "vstats", OPT_VIDEO | OPT_EXPERT , { .func_arg = opt_vstats },
3720  "dump video coding statistics to file" },
3721  { "vstats_file", OPT_VIDEO | HAS_ARG | OPT_EXPERT , { .func_arg = opt_vstats_file },
3722  "dump video coding statistics to file", "file" },
3723  { "vstats_version", OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT , { &vstats_version },
3724  "Version of the vstats format to use."},
3725  { "vf", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_filters },
3726  "set video filters", "filter_graph" },
3727  { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
3728  OPT_OUTPUT, { .off = OFFSET(intra_matrices) },
3729  "specify intra matrix coeffs", "matrix" },
3730  { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
3731  OPT_OUTPUT, { .off = OFFSET(inter_matrices) },
3732  "specify inter matrix coeffs", "matrix" },
3733  { "chroma_intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
3734  OPT_OUTPUT, { .off = OFFSET(chroma_intra_matrices) },
3735  "specify intra matrix coeffs", "matrix" },
3736  { "top", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_INT| OPT_SPEC |
3737  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(top_field_first) },
3738  "top=1/bottom=0/auto=-1 field first", "" },
3739  { "vtag", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3740  OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_old2new },
3741  "force video tag/fourcc", "fourcc/tag" },
3742  { "qphist", OPT_VIDEO | OPT_BOOL | OPT_EXPERT , { &qp_hist },
3743  "show QP histogram" },
3744  { "force_fps", OPT_VIDEO | OPT_BOOL | OPT_EXPERT | OPT_SPEC |
3745  OPT_OUTPUT, { .off = OFFSET(force_fps) },
3746  "force the selected framerate, disable the best supported framerate selection" },
3747  { "streamid", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3748  OPT_OUTPUT, { .func_arg = opt_streamid },
3749  "set the value of an outfile streamid", "streamIndex:value" },
3750  { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
3751  OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(forced_key_frames) },
3752  "force key frames at specified timestamps", "timestamps" },
3753  { "ab", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_bitrate },
3754  "audio bitrate (please use -b:a)", "bitrate" },
3755  { "b", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_bitrate },
3756  "video bitrate (please use -b:v)", "bitrate" },
3757  { "hwaccel", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
3758  OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccels) },
3759  "use HW accelerated decoding", "hwaccel name" },
3760  { "hwaccel_device", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
3761  OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_devices) },
3762  "select a device for HW acceleration", "devicename" },
3763  { "hwaccel_output_format", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
3764  OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_output_formats) },
3765  "select output format used with HW accelerated decoding", "format" },
3766 #if CONFIG_VIDEOTOOLBOX
3767  { "videotoolbox_pixfmt", HAS_ARG | OPT_STRING | OPT_EXPERT, { &videotoolbox_pixfmt}, "" },
3768 #endif
3769  { "hwaccels", OPT_EXIT, { .func_arg = show_hwaccels },
3770  "show available HW acceleration methods" },
3771  { "autorotate", HAS_ARG | OPT_BOOL | OPT_SPEC |
3772  OPT_EXPERT | OPT_INPUT, { .off = OFFSET(autorotate) },
3773  "automatically insert correct rotate filters" },
3774  { "autoscale", HAS_ARG | OPT_BOOL | OPT_SPEC |
3775  OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(autoscale) },
3776  "automatically insert a scale filter at the end of the filter graph" },
3777 
3778  /* audio options */
3779  { "aframes", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_frames },
3780  "set the number of audio frames to output", "number" },
3781  { "aq", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_qscale },
3782  "set audio quality (codec-specific)", "quality", },
3783  { "ar", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
3784  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_sample_rate) },
3785  "set audio sampling rate (in Hz)", "rate" },
3786  { "ac", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
3787  OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_channels) },
3788  "set number of audio channels", "channels" },
3790  "disable audio" },
3791  { "acodec", OPT_AUDIO | HAS_ARG | OPT_PERFILE |
3792  OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_audio_codec },
3793  "force audio codec ('copy' to copy stream)", "codec" },
3794  { "atag", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3795  OPT_OUTPUT, { .func_arg = opt_old2new },
3796  "force audio tag/fourcc", "fourcc/tag" },
3797  { "vol", OPT_AUDIO | HAS_ARG | OPT_INT, { &audio_volume },
3798  "change audio volume (256=normal)" , "volume" },
3799  { "sample_fmt", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_SPEC |
3801  "set sample format", "format" },
3802  { "channel_layout", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
3803  OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_channel_layout },
3804  "set channel layout", "layout" },
3805  { "af", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_filters },
3806  "set audio filters", "filter_graph" },
3807  { "guess_layout_max", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_INPUT, { .off = OFFSET(guess_layout_max) },
3808  "set the maximum number of channels to try to guess the channel layout" },
3809 
3810  /* subtitle options */
3812  "disable subtitle" },
3813  { "scodec", OPT_SUBTITLE | HAS_ARG | OPT_PERFILE | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_subtitle_codec },
3814  "force subtitle codec ('copy' to copy stream)", "codec" },
3815  { "stag", OPT_SUBTITLE | HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new }
3816  , "force subtitle tag/fourcc", "fourcc/tag" },
3817  { "fix_sub_duration", OPT_BOOL | OPT_EXPERT | OPT_SUBTITLE | OPT_SPEC | OPT_INPUT, { .off = OFFSET(fix_sub_duration) },
3818  "fix subtitles duration" },
3819  { "canvas_size", OPT_SUBTITLE | HAS_ARG | OPT_STRING | OPT_SPEC | OPT_INPUT, { .off = OFFSET(canvas_sizes) },
3820  "set canvas size (WxH or abbreviation)", "size" },
3821 
3822  /* grab options */
3823  { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_video_channel },
3824  "deprecated, use -channel", "channel" },
3825  { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_video_standard },
3826  "deprecated, use -standard", "standard" },
3827  { "isync", OPT_BOOL | OPT_EXPERT, { &input_sync }, "this option is deprecated and does nothing", "" },
3828 
3829  /* muxer options */
3830  { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_max_delay) },
3831  "set the maximum demux-decode delay", "seconds" },
3832  { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_preload) },
3833  "set the initial demux-decode delay", "seconds" },
3834  { "sdp_file", HAS_ARG | OPT_EXPERT | OPT_OUTPUT, { .func_arg = opt_sdp_file },
3835  "specify a file in which to print sdp information", "file" },
3836 
3837  { "time_base", HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(time_bases) },
3838  "set the desired time base hint for output stream (1:24, 1:48000 or 0.04166, 2.0833e-5)", "ratio" },
3839  { "enc_time_base", HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(enc_time_bases) },
3840  "set the desired time base for the encoder (1:24, 1:48000 or 0.04166, 2.0833e-5). "
3841  "two special values are defined - "
3842  "0 = use frame rate (video) or sample rate (audio),"
3843  "-1 = match source time base", "ratio" },
3844 
3845  { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(bitstream_filters) },
3846  "A comma-separated list of bitstream filters", "bitstream_filters" },
3847  { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
3848  "deprecated", "audio bitstream_filters" },
3849  { "vbsf", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
3850  "deprecated", "video bitstream_filters" },
3851 
3852  { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
3853  "set the audio options to the indicated preset", "preset" },
3854  { "vpre", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
3855  "set the video options to the indicated preset", "preset" },
3856  { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
3857  "set the subtitle options to the indicated preset", "preset" },
3858  { "fpre", HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
3859  "set options from indicated preset file", "filename" },
3860 
3861  { "max_muxing_queue_size", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(max_muxing_queue_size) },
3862  "maximum number of packets that can be buffered while waiting for all streams to initialize", "packets" },
3863  { "muxing_queue_data_threshold", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(muxing_queue_data_threshold) },
3864  "set the threshold after which max_muxing_queue_size is taken into account", "bytes" },
3865 
3866  /* data codec support */
3867  { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_data_codec },
3868  "force data codec ('copy' to copy stream)", "codec" },
3869  { "dn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(data_disable) },
3870  "disable data" },
3871 
3872 #if CONFIG_VAAPI
3873  { "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
3874  "set VAAPI hardware device (DRM path or X11 display name)", "device" },
3875 #endif
3876 
3877 #if CONFIG_QSV
3878  { "qsv_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_qsv_device },
3879  "set QSV hardware device (DirectX adapter index, DRM path or X11 display name)", "device"},
3880 #endif
3881 
3882  { "init_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_init_hw_device },
3883  "initialise hardware device", "args" },
3884  { "filter_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_hw_device },
3885  "set hardware device used when filtering", "device" },
3886 
3887  { NULL, },
3888 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
OptionsContext::readrate
float readrate
Definition: ffmpeg.h:122
OPT_FLOAT
#define OPT_FLOAT
Definition: cmdutils.h:173
OPT_EXIT
#define OPT_EXIT
Definition: cmdutils.h:176
AVCodec
AVCodec.
Definition: codec.h:202
OptionsContext::input_ts_offset
int64_t input_ts_offset
Definition: ffmpeg.h:119
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not loo