You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3828 lines
154KB

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