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