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