h264dec: add a CUVID hwaccel
[libav.git] / avtools / avconv_opt.c
CommitLineData
f5e66827
AK
1/*
2 * avconv option parsing
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22
23#include "avconv.h"
24#include "cmdutils.h"
25
26#include "libavformat/avformat.h"
27
28#include "libavcodec/avcodec.h"
29
30#include "libavfilter/avfilter.h"
f5e66827 31
f5e66827
AK
32#include "libavutil/avassert.h"
33#include "libavutil/avstring.h"
34#include "libavutil/avutil.h"
a903f8f0 35#include "libavutil/channel_layout.h"
f5e66827
AK
36#include "libavutil/intreadwrite.h"
37#include "libavutil/fifo.h"
38#include "libavutil/mathematics.h"
39#include "libavutil/opt.h"
40#include "libavutil/parseutils.h"
41#include "libavutil/pixdesc.h"
42#include "libavutil/pixfmt.h"
43
6d5d9246
AK
44#define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
45
f5e66827
AK
46#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
47{\
48 int i, ret;\
49 for (i = 0; i < o->nb_ ## name; i++) {\
50 char *spec = o->name[i].specifier;\
51 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
52 outvar = o->name[i].u.type;\
53 else if (ret < 0)\
b85dbe68 54 exit_program(1);\
f5e66827
AK
55 }\
56}
57
07fd0a22 58const HWAccel hwaccels[] = {
7671dd7c 59#if HAVE_VDPAU_X11
aa6b2e08
MT
60 { "vdpau", hwaccel_decode_init, HWACCEL_VDPAU, AV_PIX_FMT_VDPAU,
61 AV_HWDEVICE_TYPE_VDPAU },
7671dd7c 62#endif
f9e7a2f9 63#if CONFIG_D3D11VA
64 { "d3d11va", hwaccel_decode_init, HWACCEL_D3D11VA, AV_PIX_FMT_D3D11,
65 AV_HWDEVICE_TYPE_D3D11VA },
66#endif
67#if CONFIG_DXVA2
68 { "dxva2", hwaccel_decode_init, HWACCEL_DXVA2, AV_PIX_FMT_DXVA2_VLD,
69 AV_HWDEVICE_TYPE_DXVA2 },
35177ba7 70#endif
1839fafa 71#if CONFIG_VDA
d2e6dd32
MT
72 { "vda", vda_init, HWACCEL_VDA, AV_PIX_FMT_VDA,
73 AV_HWDEVICE_TYPE_NONE },
1839fafa 74#endif
fb472e1a 75#if CONFIG_LIBMFX
d2e6dd32
MT
76 { "qsv", qsv_init, HWACCEL_QSV, AV_PIX_FMT_QSV,
77 AV_HWDEVICE_TYPE_NONE },
fb472e1a 78#endif
5d273d3e 79#if CONFIG_VAAPI
62a1ef9f
MT
80 { "vaapi", hwaccel_decode_init, HWACCEL_VAAPI, AV_PIX_FMT_VAAPI,
81 AV_HWDEVICE_TYPE_VAAPI },
5d273d3e 82#endif
b9129ec4
AK
83#if CONFIG_CUVID
84 { "cuvid", hwaccel_decode_init, HWACCEL_CUVID, AV_PIX_FMT_CUDA,
85 AV_HWDEVICE_TYPE_CUDA },
86#endif
07fd0a22
AK
87 { 0 },
88};
5d273d3e
MT
89int hwaccel_lax_profile_check = 0;
90AVBufferRef *hw_device_ctx;
e669db76 91HWDevice *filter_hw_device;
07fd0a22 92
f5e66827
AK
93char *vstats_filename;
94
95float audio_drift_threshold = 0.1;
96float dts_delta_threshold = 10;
97
98int audio_volume = 256;
99int audio_sync_method = 0;
100int video_sync_method = VSYNC_AUTO;
f5e66827
AK
101int do_benchmark = 0;
102int do_hex_dump = 0;
103int do_pkt_dump = 0;
104int copy_ts = 0;
105int copy_tb = 1;
f5e66827
AK
106int exit_on_error = 0;
107int print_stats = 1;
108int qp_hist = 0;
f5e66827
AK
109
110static int file_overwrite = 0;
7748dd41 111static int file_skip = 0;
f5e66827
AK
112static int video_discard = 0;
113static int intra_dc_precision = 8;
f5e66827
AK
114static int using_stdin = 0;
115static int input_sync;
116
77bd1bc7 117static void uninit_options(OptionsContext *o)
f5e66827
AK
118{
119 const OptionDef *po = options;
120 int i;
121
122 /* all OPT_SPEC and OPT_STRING can be freed in generic way */
123 while (po->name) {
124 void *dst = (uint8_t*)o + po->u.off;
125
126 if (po->flags & OPT_SPEC) {
127 SpecifierOpt **so = dst;
128 int i, *count = (int*)(so + 1);
129 for (i = 0; i < *count; i++) {
130 av_freep(&(*so)[i].specifier);
131 if (po->flags & OPT_STRING)
132 av_freep(&(*so)[i].u.str);
133 }
134 av_freep(so);
135 *count = 0;
136 } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
137 av_freep(dst);
138 po++;
139 }
140
141 for (i = 0; i < o->nb_stream_maps; i++)
142 av_freep(&o->stream_maps[i].linklabel);
143 av_freep(&o->stream_maps);
144 av_freep(&o->meta_data_maps);
145 av_freep(&o->streamid_map);
77bd1bc7 146}
f5e66827 147
77bd1bc7
AK
148static void init_options(OptionsContext *o)
149{
f5e66827
AK
150 memset(o, 0, sizeof(*o));
151
152 o->mux_max_delay = 0.7;
56ee3f9d 153 o->start_time = AV_NOPTS_VALUE;
f5e66827
AK
154 o->recording_time = INT64_MAX;
155 o->limit_filesize = UINT64_MAX;
156 o->chapters_input_file = INT_MAX;
811bd078 157 o->accurate_seek = 1;
f5e66827
AK
158}
159
e7553f4c
AK
160/* return a copy of the input with the stream specifiers removed from the keys */
161static AVDictionary *strip_specifiers(AVDictionary *dict)
162{
163 AVDictionaryEntry *e = NULL;
164 AVDictionary *ret = NULL;
165
166 while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) {
167 char *p = strchr(e->key, ':');
168
169 if (p)
170 *p = 0;
171 av_dict_set(&ret, e->key, e->value, 0);
172 if (p)
173 *p = ':';
174 }
175 return ret;
176}
177
f5e66827
AK
178static double parse_frame_aspect_ratio(const char *arg)
179{
180 int x = 0, y = 0;
181 double ar = 0;
182 const char *p;
183 char *end;
184
185 p = strchr(arg, ':');
186 if (p) {
187 x = strtol(arg, &end, 10);
188 if (end == p)
189 y = strtol(end + 1, &end, 10);
190 if (x > 0 && y > 0)
191 ar = (double)x / (double)y;
192 } else
193 ar = strtod(arg, NULL);
194
195 if (!ar) {
196 av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
b85dbe68 197 exit_program(1);
f5e66827
AK
198 }
199 return ar;
200}
201
c23999be
TG
202static int show_hwaccels(void *optctx, const char *opt, const char *arg)
203{
204 int i;
205
206 printf("Supported hardware acceleration:\n");
01348e41 207 for (i = 0; hwaccels[i].name; i++) {
c23999be
TG
208 printf("%s\n", hwaccels[i].name);
209 }
210 printf("\n");
211 return 0;
212}
213
d3810c47 214static int opt_audio_codec(void *optctx, const char *opt, const char *arg)
f5e66827 215{
d3810c47 216 OptionsContext *o = optctx;
f5e66827
AK
217 return parse_option(o, "codec:a", arg, options);
218}
219
d3810c47 220static int opt_video_codec(void *optctx, const char *opt, const char *arg)
f5e66827 221{
d3810c47 222 OptionsContext *o = optctx;
f5e66827
AK
223 return parse_option(o, "codec:v", arg, options);
224}
225
d3810c47 226static int opt_subtitle_codec(void *optctx, const char *opt, const char *arg)
f5e66827 227{
d3810c47 228 OptionsContext *o = optctx;
f5e66827
AK
229 return parse_option(o, "codec:s", arg, options);
230}
231
d3810c47 232static int opt_data_codec(void *optctx, const char *opt, const char *arg)
f5e66827 233{
d3810c47 234 OptionsContext *o = optctx;
f5e66827
AK
235 return parse_option(o, "codec:d", arg, options);
236}
237
d3810c47 238static int opt_map(void *optctx, const char *opt, const char *arg)
f5e66827 239{
d3810c47 240 OptionsContext *o = optctx;
f5e66827
AK
241 StreamMap *m = NULL;
242 int i, negative = 0, file_idx;
243 int sync_file_idx = -1, sync_stream_idx;
244 char *p, *sync;
245 char *map;
246
247 if (*arg == '-') {
248 negative = 1;
249 arg++;
250 }
251 map = av_strdup(arg);
b1306823
VG
252 if (!map)
253 return AVERROR(ENOMEM);
f5e66827
AK
254
255 /* parse sync stream first, just pick first matching stream */
256 if (sync = strchr(map, ',')) {
257 *sync = 0;
258 sync_file_idx = strtol(sync + 1, &sync, 0);
259 if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
260 av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
b85dbe68 261 exit_program(1);
f5e66827
AK
262 }
263 if (*sync)
264 sync++;
265 for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
266 if (check_stream_specifier(input_files[sync_file_idx]->ctx,
267 input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
268 sync_stream_idx = i;
269 break;
270 }
271 if (i == input_files[sync_file_idx]->nb_streams) {
272 av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
273 "match any streams.\n", arg);
b85dbe68 274 exit_program(1);
f5e66827
AK
275 }
276 }
277
278
279 if (map[0] == '[') {
280 /* this mapping refers to lavfi output */
281 const char *c = map + 1;
10bca661 282 GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
f5e66827
AK
283 m = &o->stream_maps[o->nb_stream_maps - 1];
284 m->linklabel = av_get_token(&c, "]");
285 if (!m->linklabel) {
286 av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
b85dbe68 287 exit_program(1);
f5e66827
AK
288 }
289 } else {
290 file_idx = strtol(map, &p, 0);
291 if (file_idx >= nb_input_files || file_idx < 0) {
292 av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
b85dbe68 293 exit_program(1);
f5e66827
AK
294 }
295 if (negative)
296 /* disable some already defined maps */
297 for (i = 0; i < o->nb_stream_maps; i++) {
298 m = &o->stream_maps[i];
299 if (file_idx == m->file_index &&
300 check_stream_specifier(input_files[m->file_index]->ctx,
301 input_files[m->file_index]->ctx->streams[m->stream_index],
302 *p == ':' ? p + 1 : p) > 0)
303 m->disabled = 1;
304 }
305 else
306 for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
307 if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
308 *p == ':' ? p + 1 : p) <= 0)
309 continue;
10bca661 310 GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
f5e66827
AK
311 m = &o->stream_maps[o->nb_stream_maps - 1];
312
313 m->file_index = file_idx;
314 m->stream_index = i;
315
316 if (sync_file_idx >= 0) {
317 m->sync_file_index = sync_file_idx;
318 m->sync_stream_index = sync_stream_idx;
319 } else {
320 m->sync_file_index = file_idx;
321 m->sync_stream_index = i;
322 }
323 }
324 }
325
326 if (!m) {
327 av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
b85dbe68 328 exit_program(1);
f5e66827
AK
329 }
330
331 av_freep(&map);
332 return 0;
333}
334
d3810c47 335static int opt_attach(void *optctx, const char *opt, const char *arg)
f5e66827 336{
d3810c47 337 OptionsContext *o = optctx;
10bca661 338 GROW_ARRAY(o->attachments, o->nb_attachments);
f5e66827
AK
339 o->attachments[o->nb_attachments - 1] = arg;
340 return 0;
341}
342
5d273d3e
MT
343#if CONFIG_VAAPI
344static int opt_vaapi_device(void *optctx, const char *opt, const char *arg)
345{
62a1ef9f
MT
346 HWDevice *dev;
347 const char *prefix = "vaapi:";
348 char *tmp;
5d273d3e 349 int err;
62a1ef9f
MT
350 tmp = av_malloc(strlen(prefix) + strlen(arg) + 1);
351 if (!tmp)
352 return AVERROR(ENOMEM);
353 strcpy(tmp, prefix);
354 strcat(tmp, arg);
355 err = hw_device_init_from_string(tmp, &dev);
356 av_free(tmp);
5d273d3e 357 if (err < 0)
62a1ef9f
MT
358 return err;
359 hw_device_ctx = av_buffer_ref(dev->device_ref);
360 if (!hw_device_ctx)
361 return AVERROR(ENOMEM);
5d273d3e
MT
362 return 0;
363}
364#endif
365
d2e6dd32
MT
366static int opt_init_hw_device(void *optctx, const char *opt, const char *arg)
367{
368 if (!strcmp(arg, "list")) {
369 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
370 printf("Supported hardware device types:\n");
371 while ((type = av_hwdevice_iterate_types(type)) !=
372 AV_HWDEVICE_TYPE_NONE)
373 printf("%s\n", av_hwdevice_get_type_name(type));
374 printf("\n");
375 exit_program(0);
376 } else {
377 return hw_device_init_from_string(arg, NULL);
378 }
379}
380
e669db76
MT
381static int opt_filter_hw_device(void *optctx, const char *opt, const char *arg)
382{
383 if (filter_hw_device) {
384 av_log(NULL, AV_LOG_ERROR, "Only one filter device can be used.\n");
385 return AVERROR(EINVAL);
386 }
387 filter_hw_device = hw_device_get_by_name(arg);
388 if (!filter_hw_device) {
389 av_log(NULL, AV_LOG_ERROR, "Invalid filter device %s.\n", arg);
390 return AVERROR(EINVAL);
391 }
392 return 0;
393}
394
f5e66827 395/**
c1ef30a6 396 * Parse a metadata specifier passed as 'arg' parameter.
02e42751 397 * @param arg metadata string to parse
f5e66827
AK
398 * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
399 * @param index for type c/p, chapter/program index is written here
400 * @param stream_spec for type s, the stream specifier is written here
401 */
402static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)
403{
404 if (*arg) {
405 *type = *arg;
406 switch (*arg) {
407 case 'g':
408 break;
409 case 's':
410 if (*(++arg) && *arg != ':') {
411 av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
b85dbe68 412 exit_program(1);
f5e66827
AK
413 }
414 *stream_spec = *arg == ':' ? arg + 1 : "";
415 break;
416 case 'c':
417 case 'p':
418 if (*(++arg) == ':')
419 *index = strtol(++arg, NULL, 0);
420 break;
421 default:
422 av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
b85dbe68 423 exit_program(1);
f5e66827
AK
424 }
425 } else
426 *type = 'g';
427}
428
429static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
430{
431 AVDictionary **meta_in = NULL;
432 AVDictionary **meta_out;
433 int i, ret = 0;
434 char type_in, type_out;
435 const char *istream_spec = NULL, *ostream_spec = NULL;
436 int idx_in = 0, idx_out = 0;
437
438 parse_meta_type(inspec, &type_in, &idx_in, &istream_spec);
439 parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
440
441 if (type_in == 'g' || type_out == 'g')
442 o->metadata_global_manual = 1;
443 if (type_in == 's' || type_out == 's')
444 o->metadata_streams_manual = 1;
445 if (type_in == 'c' || type_out == 'c')
446 o->metadata_chapters_manual = 1;
447
a119c64e
AK
448 /* ic is NULL when just disabling automatic mappings */
449 if (!ic)
450 return 0;
451
f5e66827
AK
452#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
453 if ((index) < 0 || (index) >= (nb_elems)) {\
454 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
455 (desc), (index));\
b85dbe68 456 exit_program(1);\
f5e66827
AK
457 }
458
459#define SET_DICT(type, meta, context, index)\
460 switch (type) {\
461 case 'g':\
462 meta = &context->metadata;\
463 break;\
464 case 'c':\
465 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
466 meta = &context->chapters[index]->metadata;\
467 break;\
468 case 'p':\
469 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
470 meta = &context->programs[index]->metadata;\
471 break;\
4632abc7
AK
472 case 's':\
473 break; /* handled separately below */ \
f5e66827
AK
474 default: av_assert0(0);\
475 }\
476
477 SET_DICT(type_in, meta_in, ic, idx_in);
478 SET_DICT(type_out, meta_out, oc, idx_out);
479
480 /* for input streams choose first matching stream */
481 if (type_in == 's') {
482 for (i = 0; i < ic->nb_streams; i++) {
483 if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
484 meta_in = &ic->streams[i]->metadata;
485 break;
486 } else if (ret < 0)
b85dbe68 487 exit_program(1);
f5e66827
AK
488 }
489 if (!meta_in) {
490 av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
b85dbe68 491 exit_program(1);
f5e66827
AK
492 }
493 }
494
495 if (type_out == 's') {
496 for (i = 0; i < oc->nb_streams; i++) {
497 if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
498 meta_out = &oc->streams[i]->metadata;
499 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
500 } else if (ret < 0)
b85dbe68 501 exit_program(1);
f5e66827
AK
502 }
503 } else
504 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
505
506 return 0;
507}
508
509static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
510{
db4766ad 511 const AVCodecDescriptor *desc;
f5e66827
AK
512 const char *codec_string = encoder ? "encoder" : "decoder";
513 AVCodec *codec;
514
515 codec = encoder ?
516 avcodec_find_encoder_by_name(name) :
517 avcodec_find_decoder_by_name(name);
db4766ad
AK
518
519 if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
520 codec = encoder ? avcodec_find_encoder(desc->id) :
521 avcodec_find_decoder(desc->id);
522 if (codec)
523 av_log(NULL, AV_LOG_VERBOSE, "Matched %s '%s' for codec '%s'.\n",
524 codec_string, codec->name, desc->name);
525 }
526
f5e66827
AK
527 if (!codec) {
528 av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
b85dbe68 529 exit_program(1);
f5e66827
AK
530 }
531 if (codec->type != type) {
532 av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
b85dbe68 533 exit_program(1);
f5e66827
AK
534 }
535 return codec;
536}
537
538static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
539{
540 char *codec_name = NULL;
541
542 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
543 if (codec_name) {
15e84ed3
AK
544 AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
545 st->codecpar->codec_id = codec->id;
f5e66827
AK
546 return codec;
547 } else
15e84ed3 548 return avcodec_find_decoder(st->codecpar->codec_id);
f5e66827
AK
549}
550
c1ef30a6
DB
551/* Add all the streams from the given input file to the global
552 * list of input streams. */
f5e66827
AK
553static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
554{
41776ba9 555 int i, ret;
f5e66827
AK
556
557 for (i = 0; i < ic->nb_streams; i++) {
558 AVStream *st = ic->streams[i];
15e84ed3 559 AVCodecParameters *par = st->codecpar;
f5e66827 560 InputStream *ist = av_mallocz(sizeof(*ist));
07fd0a22 561 char *framerate = NULL, *hwaccel = NULL, *hwaccel_device = NULL;
5d273d3e 562 char *hwaccel_output_format = NULL;
746dca48
AK
563 char *codec_tag = NULL;
564 char *next;
f5e66827
AK
565
566 if (!ist)
b85dbe68 567 exit_program(1);
f5e66827 568
10bca661 569 GROW_ARRAY(input_streams, nb_input_streams);
f5e66827
AK
570 input_streams[nb_input_streams - 1] = ist;
571
572 ist->st = st;
573 ist->file_index = nb_input_files;
574 ist->discard = 1;
575 st->discard = AVDISCARD_ALL;
16b0c929
AH
576 ist->nb_samples = 0;
577 ist->min_pts = INT64_MAX;
578 ist->max_pts = INT64_MIN;
f5e66827
AK
579
580 ist->ts_scale = 1.0;
581 MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
582
16302246
MS
583 ist->autorotate = 1;
584 MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
585
746dca48
AK
586 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
587 if (codec_tag) {
588 uint32_t tag = strtol(codec_tag, &next, 0);
589 if (*next)
590 tag = AV_RL32(codec_tag);
15e84ed3 591 st->codecpar->codec_tag = tag;
746dca48
AK
592 }
593
f5e66827 594 ist->dec = choose_decoder(o, ic, st);
15e84ed3 595 ist->decoder_opts = filter_codec_opts(o->g->codec_opts, par->codec_id, ic, st, ist->dec);
f5e66827 596
41776ba9
AK
597 ist->dec_ctx = avcodec_alloc_context3(ist->dec);
598 if (!ist->dec_ctx) {
599 av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n");
600 exit_program(1);
601 }
602
15e84ed3 603 ret = avcodec_parameters_to_context(ist->dec_ctx, par);
41776ba9
AK
604 if (ret < 0) {
605 av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
606 exit_program(1);
607 }
608
15e84ed3 609 switch (par->codec_type) {
f5e66827 610 case AVMEDIA_TYPE_VIDEO:
f5e66827
AK
611 MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
612 if (framerate && av_parse_video_rate(&ist->framerate,
613 framerate) < 0) {
614 av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
615 framerate);
b85dbe68 616 exit_program(1);
f5e66827
AK
617 }
618
07fd0a22
AK
619 MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
620 if (hwaccel) {
621 if (!strcmp(hwaccel, "none"))
622 ist->hwaccel_id = HWACCEL_NONE;
623 else if (!strcmp(hwaccel, "auto"))
624 ist->hwaccel_id = HWACCEL_AUTO;
625 else {
626 int i;
627 for (i = 0; hwaccels[i].name; i++) {
628 if (!strcmp(hwaccels[i].name, hwaccel)) {
629 ist->hwaccel_id = hwaccels[i].id;
630 break;
631 }
632 }
633
634 if (!ist->hwaccel_id) {
635 av_log(NULL, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
636 hwaccel);
637 av_log(NULL, AV_LOG_FATAL, "Supported hwaccels: ");
638 for (i = 0; hwaccels[i].name; i++)
639 av_log(NULL, AV_LOG_FATAL, "%s ", hwaccels[i].name);
640 av_log(NULL, AV_LOG_FATAL, "\n");
641 exit_program(1);
642 }
643 }
644 }
645
646 MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
647 if (hwaccel_device) {
648 ist->hwaccel_device = av_strdup(hwaccel_device);
649 if (!ist->hwaccel_device)
650 exit_program(1);
651 }
5d273d3e
MT
652
653 MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
654 hwaccel_output_format, ic, st);
655 if (hwaccel_output_format) {
656 ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
657 if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
658 av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
659 "format: %s", hwaccel_output_format);
660 }
661 } else {
662 ist->hwaccel_output_format = AV_PIX_FMT_NONE;
663 }
664
c255f0b0 665 ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
07fd0a22 666
f5e66827
AK
667 break;
668 case AVMEDIA_TYPE_AUDIO:
669 guess_input_channel_layout(ist);
f5e66827
AK
670 break;
671 case AVMEDIA_TYPE_DATA:
672 case AVMEDIA_TYPE_SUBTITLE:
673 case AVMEDIA_TYPE_ATTACHMENT:
674 case AVMEDIA_TYPE_UNKNOWN:
675 break;
676 default:
677 abort();
678 }
679 }
680}
681
682static void assert_file_overwrite(const char *filename)
683{
7748dd41
VG
684 if (file_overwrite && file_skip) {
685 fprintf(stderr, "Error, both -y and -n supplied. Exiting.\n");
686 exit_program(1);
687 }
688
f5e66827 689 if (!file_overwrite &&
f929ab05 690 (!strchr(filename, ':') || filename[1] == ':' ||
f5e66827
AK
691 av_strstart(filename, "file:", NULL))) {
692 if (avio_check(filename, 0) == 0) {
7748dd41 693 if (!using_stdin && !file_skip) {
f5e66827
AK
694 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
695 fflush(stderr);
696 if (!read_yesno()) {
697 fprintf(stderr, "Not overwriting - exiting\n");
b85dbe68 698 exit_program(1);
f5e66827
AK
699 }
700 }
701 else {
702 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
b85dbe68 703 exit_program(1);
f5e66827
AK
704 }
705 }
706 }
707}
708
709static void dump_attachment(AVStream *st, const char *filename)
710{
711 int ret;
712 AVIOContext *out = NULL;
713 AVDictionaryEntry *e;
714
15e84ed3 715 if (!st->codecpar->extradata_size) {
f5e66827
AK
716 av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
717 nb_input_files - 1, st->index);
718 return;
719 }
720 if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
721 filename = e->value;
722 if (!*filename) {
723 av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
724 "in stream #%d:%d.\n", nb_input_files - 1, st->index);
b85dbe68 725 exit_program(1);
f5e66827
AK
726 }
727
728 assert_file_overwrite(filename);
729
730 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
731 av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
732 filename);
b85dbe68 733 exit_program(1);
f5e66827
AK
734 }
735
15e84ed3 736 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
f5e66827
AK
737 avio_flush(out);
738 avio_close(out);
739}
740
77bd1bc7 741static int open_input_file(OptionsContext *o, const char *filename)
f5e66827 742{
41d2008b 743 InputFile *f;
f5e66827
AK
744 AVFormatContext *ic;
745 AVInputFormat *file_iformat = NULL;
746 int err, i, ret;
747 int64_t timestamp;
748 uint8_t buf[128];
749 AVDictionary **opts;
e7553f4c
AK
750 AVDictionary *unused_opts = NULL;
751 AVDictionaryEntry *e = NULL;
f5e66827
AK
752 int orig_nb_streams; // number of streams before avformat_find_stream_info
753
754 if (o->format) {
755 if (!(file_iformat = av_find_input_format(o->format))) {
756 av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
b85dbe68 757 exit_program(1);
f5e66827
AK
758 }
759 }
760
761 if (!strcmp(filename, "-"))
762 filename = "pipe:";
763
764 using_stdin |= !strncmp(filename, "pipe:", 5) ||
765 !strcmp(filename, "/dev/stdin");
766
767 /* get default parameters from command line */
768 ic = avformat_alloc_context();
769 if (!ic) {
770 print_error(filename, AVERROR(ENOMEM));
b85dbe68 771 exit_program(1);
f5e66827
AK
772 }
773 if (o->nb_audio_sample_rate) {
774 snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
77bd1bc7 775 av_dict_set(&o->g->format_opts, "sample_rate", buf, 0);
f5e66827
AK
776 }
777 if (o->nb_audio_channels) {
778 /* because we set audio_channels based on both the "ac" and
779 * "channel_layout" options, we need to check that the specified
780 * demuxer actually has the "channels" option before setting it */
781 if (file_iformat && file_iformat->priv_class &&
782 av_opt_find(&file_iformat->priv_class, "channels", NULL, 0,
783 AV_OPT_SEARCH_FAKE_OBJ)) {
784 snprintf(buf, sizeof(buf), "%d",
785 o->audio_channels[o->nb_audio_channels - 1].u.i);
77bd1bc7 786 av_dict_set(&o->g->format_opts, "channels", buf, 0);
f5e66827
AK
787 }
788 }
789 if (o->nb_frame_rates) {
790 /* set the format-level framerate option;
791 * this is important for video grabbers, e.g. x11 */
792 if (file_iformat && file_iformat->priv_class &&
793 av_opt_find(&file_iformat->priv_class, "framerate", NULL, 0,
794 AV_OPT_SEARCH_FAKE_OBJ)) {
77bd1bc7 795 av_dict_set(&o->g->format_opts, "framerate",
f5e66827
AK
796 o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
797 }
798 }
799 if (o->nb_frame_sizes) {
77bd1bc7 800 av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
f5e66827
AK
801 }
802 if (o->nb_frame_pix_fmts)
77bd1bc7 803 av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
f5e66827
AK
804
805 ic->flags |= AVFMT_FLAG_NONBLOCK;
806 ic->interrupt_callback = int_cb;
807
41ed7ab4 808 /* open the input file with generic Libav function */
77bd1bc7 809 err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
f5e66827
AK
810 if (err < 0) {
811 print_error(filename, err);
b85dbe68 812 exit_program(1);
f5e66827 813 }
77bd1bc7 814 assert_avoptions(o->g->format_opts);
f5e66827
AK
815
816 /* apply forced codec ids */
817 for (i = 0; i < ic->nb_streams; i++)
818 choose_decoder(o, ic, ic->streams[i]);
819
820 /* Set AVCodecContext options for avformat_find_stream_info */
77bd1bc7 821 opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
f5e66827
AK
822 orig_nb_streams = ic->nb_streams;
823
824 /* If not enough info to get the stream parameters, we decode the
825 first frames to get it. (used in mpeg case for example) */
826 ret = avformat_find_stream_info(ic, opts);
827 if (ret < 0) {
828 av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
829 avformat_close_input(&ic);
b85dbe68 830 exit_program(1);
f5e66827
AK
831 }
832
56ee3f9d 833 timestamp = (o->start_time == AV_NOPTS_VALUE) ? 0 : o->start_time;
f5e66827
AK
834 /* add the stream start time */
835 if (ic->start_time != AV_NOPTS_VALUE)
836 timestamp += ic->start_time;
837
838 /* if seeking requested, we execute it */
56ee3f9d 839 if (o->start_time != AV_NOPTS_VALUE) {
f5e66827
AK
840 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
841 if (ret < 0) {
842 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
843 filename, (double)timestamp / AV_TIME_BASE);
844 }
845 }
846
847 /* update the current parameters so that they match the one of the input stream */
848 add_input_streams(o, ic);
849
850 /* dump the file content */
851 av_dump_format(ic, nb_input_files, filename, 0);
852
10bca661 853 GROW_ARRAY(input_files, nb_input_files);
41d2008b
AK
854 f = av_mallocz(sizeof(*f));
855 if (!f)
b85dbe68 856 exit_program(1);
41d2008b 857 input_files[nb_input_files - 1] = f;
f5e66827 858
41d2008b
AK
859 f->ctx = ic;
860 f->ist_index = nb_input_streams - ic->nb_streams;
811bd078 861 f->start_time = o->start_time;
488a0fa6 862 f->recording_time = o->recording_time;
41d2008b
AK
863 f->ts_offset = o->input_ts_offset - (copy_ts ? 0 : timestamp);
864 f->nb_streams = ic->nb_streams;
865 f->rate_emu = o->rate_emu;
811bd078 866 f->accurate_seek = o->accurate_seek;
16b0c929
AH
867 f->loop = o->loop;
868 f->duration = 0;
869 f->time_base = (AVRational){ 1, 1 };
f5e66827 870
e7553f4c
AK
871 /* check if all codec options have been used */
872 unused_opts = strip_specifiers(o->g->codec_opts);
873 for (i = f->ist_index; i < nb_input_streams; i++) {
874 e = NULL;
f3a6ad22 875 while ((e = av_dict_get(input_streams[i]->decoder_opts, "", e,
e7553f4c
AK
876 AV_DICT_IGNORE_SUFFIX)))
877 av_dict_set(&unused_opts, e->key, NULL, 0);
878 }
879
880 e = NULL;
881 while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
882 const AVClass *class = avcodec_get_class();
883 const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
884 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
885 if (!option)
886 continue;
887 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
888 av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
889 "input file #%d (%s) is not a decoding option.\n", e->key,
890 option->help ? option->help : "", nb_input_files - 1,
891 filename);
b85dbe68 892 exit_program(1);
e7553f4c
AK
893 }
894
895 av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
896 "input file #%d (%s) has not been used for any stream. The most "
897 "likely reason is either wrong type (e.g. a video option with "
898 "no video streams) or that it is a private option of some decoder "
899 "which was not actually used for any stream.\n", e->key,
900 option->help ? option->help : "", nb_input_files - 1, filename);
901 }
902 av_dict_free(&unused_opts);
903
f5e66827
AK
904 for (i = 0; i < o->nb_dump_attachment; i++) {
905 int j;
906
907 for (j = 0; j < ic->nb_streams; j++) {
908 AVStream *st = ic->streams[j];
909
910 if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
911 dump_attachment(st, o->dump_attachment[i].u.str);
912 }
913 }
914
915 for (i = 0; i < orig_nb_streams; i++)
916 av_dict_free(&opts[i]);
917 av_freep(&opts);
918
f5e66827
AK
919 return 0;
920}
921
922static uint8_t *get_line(AVIOContext *s)
923{
924 AVIOContext *line;
925 uint8_t *buf;
926 char c;
927
928 if (avio_open_dyn_buf(&line) < 0) {
929 av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
b85dbe68 930 exit_program(1);
f5e66827
AK
931 }
932
933 while ((c = avio_r8(s)) && c != '\n')
934 avio_w8(line, c);
935 avio_w8(line, 0);
936 avio_close_dyn_buf(line, &buf);
937
938 return buf;
939}
940
941static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
942{
9e4e35b4 943 int i, ret = -1;
f5e66827
AK
944 char filename[1000];
945 const char *base[3] = { getenv("AVCONV_DATADIR"),
946 getenv("HOME"),
947 AVCONV_DATADIR,
948 };
949
9e4e35b4 950 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
f5e66827
AK
951 if (!base[i])
952 continue;
953 if (codec_name) {
954 snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
955 i != 1 ? "" : "/.avconv", codec_name, preset_name);
956 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
957 }
9e4e35b4 958 if (ret < 0) {
f5e66827
AK
959 snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
960 i != 1 ? "" : "/.avconv", preset_name);
961 ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
962 }
963 }
964 return ret;
965}
966
90944ee3 967static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
f5e66827 968{
90944ee3 969 enum AVMediaType type = ost->st->codecpar->codec_type;
f5e66827
AK
970 char *codec_name = NULL;
971
90944ee3
AK
972 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
973 MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
974 if (!codec_name) {
975 ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
976 NULL, ost->st->codecpar->codec_type);
977 ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
978 if (!ost->enc) {
979 av_log(NULL, AV_LOG_FATAL, "Automatic encoder selection failed for "
980 "output stream #%d:%d. Default encoder for format %s is "
981 "probably disabled. Please choose an encoder manually.\n",
982 ost->file_index, ost->index, s->oformat->name);
983 return AVERROR_ENCODER_NOT_FOUND;
984 }
985 } else if (!strcmp(codec_name, "copy"))
986 ost->stream_copy = 1;
987 else {
988 ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
989 ost->st->codecpar->codec_id = ost->enc->id;
990 }
991
992 ost->encoding_needed = !ost->stream_copy;
993 } else {
994 /* no encoding supported for other media types */
995 ost->stream_copy = 1;
996 ost->encoding_needed = 0;
f5e66827 997 }
90944ee3
AK
998
999 return 0;
f5e66827
AK
1000}
1001
1002static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
1003{
1004 OutputStream *ost;
1005 AVStream *st = avformat_new_stream(oc, NULL);
1006 int idx = oc->nb_streams - 1, ret = 0;
fe7b21c8
AK
1007 const char *bsfs = NULL;
1008 char *next, *codec_tag = NULL;
f5e66827 1009 double qscale = -1;
b420a27e 1010 int bitrate = 0;
f5e66827
AK
1011
1012 if (!st) {
1013 av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
b85dbe68 1014 exit_program(1);
f5e66827
AK
1015 }
1016
1017 if (oc->nb_streams - 1 < o->nb_streamid_map)
1018 st->id = o->streamid_map[oc->nb_streams - 1];
1019
10bca661 1020 GROW_ARRAY(output_streams, nb_output_streams);
f5e66827 1021 if (!(ost = av_mallocz(sizeof(*ost))))
b85dbe68 1022 exit_program(1);
f5e66827
AK
1023 output_streams[nb_output_streams - 1] = ost;
1024
3d624420 1025 ost->file_index = nb_output_files - 1;
f5e66827
AK
1026 ost->index = idx;
1027 ost->st = st;
15e84ed3 1028 st->codecpar->codec_type = type;
90944ee3
AK
1029
1030 ret = choose_encoder(o, oc, ost);
1031 if (ret < 0) {
1032 av_log(NULL, AV_LOG_FATAL, "Error selecting an encoder for stream "
1033 "%d:%d\n", ost->file_index, ost->index);
1034 exit_program(1);
1035 }
38313626
AK
1036
1037 ost->enc_ctx = avcodec_alloc_context3(ost->enc);
1038 if (!ost->enc_ctx) {
1039 av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n");
1040 exit_program(1);
1041 }
1042 ost->enc_ctx->codec_type = type;
1043
f5e66827 1044 if (ost->enc) {
4e61a38a
AK
1045 AVIOContext *s = NULL;
1046 char *buf = NULL, *arg = NULL, *preset = NULL;
1047
9580818c 1048 ost->encoder_opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
4e61a38a
AK
1049
1050 MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
1051 if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
1052 do {
1053 buf = get_line(s);
1054 if (!buf[0] || buf[0] == '#') {
1055 av_free(buf);
1056 continue;
1057 }
1058 if (!(arg = strchr(buf, '='))) {
1059 av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
b85dbe68 1060 exit_program(1);
4e61a38a
AK
1061 }
1062 *arg++ = 0;
9580818c 1063 av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
4e61a38a
AK
1064 av_free(buf);
1065 } while (!s->eof_reached);
1066 avio_close(s);
1067 }
1068 if (ret) {
1069 av_log(NULL, AV_LOG_FATAL,
1070 "Preset %s specified for stream %d:%d, but could not be opened.\n",
1071 preset, ost->file_index, ost->index);
b85dbe68 1072 exit_program(1);
4e61a38a 1073 }
df0229a7 1074 } else {
9580818c 1075 ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
f5e66827
AK
1076 }
1077
f5e66827
AK
1078 ost->max_frames = INT64_MAX;
1079 MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
1080
fe7b21c8
AK
1081 MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
1082 while (bsfs && *bsfs) {
4426540f 1083 const AVBitStreamFilter *filter;
064f19f3
JA
1084 const char *bsf, *bsf_options_str, *bsf_name;
1085 AVDictionary *bsf_options = NULL;
4426540f 1086
064f19f3 1087 bsf = bsf_options_str = av_get_token(&bsfs, ",");
fe7b21c8
AK
1088 if (!bsf)
1089 exit_program(1);
064f19f3
JA
1090 bsf_name = av_get_token(&bsf_options_str, "=");
1091 if (!bsf_name)
1092 exit_program(1);
4426540f 1093
064f19f3 1094 filter = av_bsf_get_by_name(bsf_name);
80fb19bc 1095 if (!filter) {
064f19f3 1096 av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf_name);
b85dbe68 1097 exit_program(1);
f5e66827 1098 }
064f19f3
JA
1099 if (*bsf_options_str++) {
1100 ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
1101 if (ret < 0) {
1102 av_log(NULL, AV_LOG_ERROR, "Error parsing options for bitstream filter %s\n", bsf_name);
1103 exit_program(1);
1104 }
1105 }
fe7b21c8 1106 av_freep(&bsf);
f5e66827 1107
064f19f3
JA
1108 ost->bsf_ctx = av_realloc_array(ost->bsf_ctx,
1109 ost->nb_bitstream_filters + 1,
1110 sizeof(*ost->bsf_ctx));
1111 if (!ost->bsf_ctx)
1112 exit_program(1);
1113
1114 ret = av_bsf_alloc(filter, &ost->bsf_ctx[ost->nb_bitstream_filters]);
1115 if (ret < 0) {
1116 av_log(NULL, AV_LOG_ERROR, "Error allocating a bistream filter context\n");
4426540f 1117 exit_program(1);
064f19f3
JA
1118 }
1119 ost->nb_bitstream_filters++;
1120
1121 if (bsf_options) {
1122 ret = av_opt_set_dict(ost->bsf_ctx[ost->nb_bitstream_filters-1]->priv_data, &bsf_options);
1123 if (ret < 0) {
1124 av_log(NULL, AV_LOG_ERROR, "Error setting options for bitstream filter %s\n", bsf_name);
1125 exit_program(1);
1126 }
1127 assert_avoptions(bsf_options);
1128 av_dict_free(&bsf_options);
1129 }
1130 av_freep(&bsf_name);
4426540f 1131
fe7b21c8
AK
1132 if (*bsfs)
1133 bsfs++;
f5e66827
AK
1134 }
1135
1136 MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
1137 if (codec_tag) {
1138 uint32_t tag = strtol(codec_tag, &next, 0);
1139 if (*next)
1140 tag = AV_RL32(codec_tag);
38313626 1141 ost->enc_ctx->codec_tag = tag;
f5e66827
AK
1142 }
1143
1144 MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
fb722a90 1145 if (qscale >= 0) {
7c6eb0a1 1146 ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
38313626 1147 ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
f5e66827
AK
1148 }
1149
b420a27e
AK
1150 MATCH_PER_STREAM_OPT(bitrates, i, bitrate, oc, st);
1151 if (bitrate > 0) {
1152 if (ost->stream_copy)
1153 ost->bitrate_override = bitrate;
1154 else
1155 ost->enc_ctx->bit_rate = bitrate;
1156 }
1157
398f015f
AK
1158 ost->max_muxing_queue_size = 128;
1159 MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
1160 ost->max_muxing_queue_size *= sizeof(AVPacket);
1161
f5e66827 1162 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
7c6eb0a1 1163 ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
f5e66827 1164
77bd1bc7 1165 av_opt_get_int(o->g->sws_opts, "sws_flags", 0, &ost->sws_flags);
f5e66827 1166
5c7db097
JR
1167 av_dict_copy(&ost->resample_opts, o->g->resample_opts, 0);
1168
716d413c 1169 ost->pix_fmts[0] = ost->pix_fmts[1] = AV_PIX_FMT_NONE;
76d23f40 1170 ost->last_mux_dts = AV_NOPTS_VALUE;
f5e66827 1171
398f015f
AK
1172 ost->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket));
1173 if (!ost->muxing_queue)
1174 exit_program(1);
1175
f5e66827
AK
1176 return ost;
1177}
1178
1179static void parse_matrix_coeffs(uint16_t *dest, const char *str)
1180{
1181 int i;
1182 const char *p = str;
1183 for (i = 0;; i++) {
1184 dest[i] = atoi(p);
1185 if (i == 63)
1186 break;
1187 p = strchr(p, ',');
1188 if (!p) {
1189 av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
b85dbe68 1190 exit_program(1);
f5e66827
AK
1191 }
1192 p++;
1193 }
1194}
1195
a4208b9b
AK
1196/* read file contents into a string */
1197static uint8_t *read_file(const char *filename)
1198{
1199 AVIOContext *pb = NULL;
1200 AVIOContext *dyn_buf = NULL;
1201 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1202 uint8_t buf[1024], *str;
1203
1204 if (ret < 0) {
1205 av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
1206 return NULL;
1207 }
1208
1209 ret = avio_open_dyn_buf(&dyn_buf);
1210 if (ret < 0) {
1211 avio_closep(&pb);
1212 return NULL;
1213 }
1214 while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
1215 avio_write(dyn_buf, buf, ret);
1216 avio_w8(dyn_buf, 0);
1217 avio_closep(&pb);
1218
1219 ret = avio_close_dyn_buf(dyn_buf, &str);
1220 if (ret < 0)
1221 return NULL;
1222 return str;
1223}
1224
1225static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
1226 OutputStream *ost)
1227{
1228 AVStream *st = ost->st;
1229 char *filter = NULL, *filter_script = NULL;
1230
1231 MATCH_PER_STREAM_OPT(filter_scripts, str, filter_script, oc, st);
1232 MATCH_PER_STREAM_OPT(filters, str, filter, oc, st);
1233
1234 if (filter_script && filter) {
1235 av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for "
1236 "output stream #%d:%d.\n", nb_output_files, st->index);
b85dbe68 1237 exit_program(1);
a4208b9b
AK
1238 }
1239
1240 if (filter_script)
1241 return read_file(filter_script);
1242 else if (filter)
1243 return av_strdup(filter);
1244
15e84ed3 1245 return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
a4208b9b
AK
1246 "null" : "anull");
1247}
1248
f5e66827
AK
1249static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
1250{
1251 AVStream *st;
1252 OutputStream *ost;
1253 AVCodecContext *video_enc;
538bf767 1254 char *frame_aspect_ratio = NULL;
f5e66827
AK
1255
1256 ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO);
1257 st = ost->st;
38313626 1258 video_enc = ost->enc_ctx;
f5e66827 1259
538bf767
AK
1260 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
1261 if (frame_aspect_ratio)
1262 ost->frame_aspect_ratio = parse_frame_aspect_ratio(frame_aspect_ratio);
1263
f5e66827
AK
1264 if (!ost->stream_copy) {
1265 const char *p = NULL;
1266 char *frame_rate = NULL, *frame_size = NULL;
538bf767 1267 char *frame_pix_fmt = NULL;
f5e66827 1268 char *intra_matrix = NULL, *inter_matrix = NULL;
038c0b1e 1269 int do_pass = 0;
f5e66827
AK
1270 int i;
1271
1272 MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
1273 if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
1274 av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
b85dbe68 1275 exit_program(1);
f5e66827
AK
1276 }
1277
1278 MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
1279 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
1280 av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
b85dbe68 1281 exit_program(1);
f5e66827
AK
1282 }
1283
f5e66827 1284 MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
716d413c 1285 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
f5e66827 1286 av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
b85dbe68 1287 exit_program(1);
f5e66827
AK
1288 }
1289 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
1290
1291 MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
1292 if (intra_matrix) {
1293 if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
1294 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
b85dbe68 1295 exit_program(1);
f5e66827
AK
1296 }
1297 parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
1298 }
1299 MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
1300 if (inter_matrix) {
1301 if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
1302 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
b85dbe68 1303 exit_program(1);
f5e66827
AK
1304 }
1305 parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
1306 }
1307
1308 MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
1309 for (i = 0; p; i++) {
1310 int start, end, q;
1311 int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
1312 if (e != 3) {
1313 av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
b85dbe68 1314 exit_program(1);
f5e66827
AK
1315 }
1316 video_enc->rc_override =
1317 av_realloc(video_enc->rc_override,
1318 sizeof(RcOverride) * (i + 1));
81688e68
VG
1319 if (!video_enc->rc_override) {
1320 av_log(NULL, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
1321 exit_program(1);
1322 }
f5e66827
AK
1323 video_enc->rc_override[i].start_frame = start;
1324 video_enc->rc_override[i].end_frame = end;
1325 if (q > 0) {
1326 video_enc->rc_override[i].qscale = q;
1327 video_enc->rc_override[i].quality_factor = 1.0;
1328 }
1329 else {
1330 video_enc->rc_override[i].qscale = 0;
1331 video_enc->rc_override[i].quality_factor = -q/100.0;
1332 }
1333 p = strchr(p, '/');
1334 if (p) p++;
1335 }
1336 video_enc->rc_override_count = i;
f5e66827
AK
1337 video_enc->intra_dc_precision = intra_dc_precision - 8;
1338
1339 /* two pass mode */
038c0b1e 1340 MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
f5e66827
AK
1341 if (do_pass) {
1342 if (do_pass == 1) {
7c6eb0a1 1343 video_enc->flags |= AV_CODEC_FLAG_PASS1;
f5e66827 1344 } else {
7c6eb0a1 1345 video_enc->flags |= AV_CODEC_FLAG_PASS2;
f5e66827
AK
1346 }
1347 }
1348
bbcedade
AK
1349 MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
1350 if (ost->logfile_prefix &&
1351 !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
b85dbe68 1352 exit_program(1);
bbcedade 1353
6d5d9246
AK
1354 if (do_pass) {
1355 char logfilename[1024];
1356 FILE *f;
1357
1358 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1359 ost->logfile_prefix ? ost->logfile_prefix :
1360 DEFAULT_PASS_LOGFILENAME_PREFIX,
1361 i);
1362 if (!strcmp(ost->enc->name, "libx264")) {
1363 av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
1364 } else {
7c6eb0a1 1365 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
6d5d9246
AK
1366 f = fopen(logfilename, "wb");
1367 if (!f) {
1368 av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
1369 logfilename, strerror(errno));
1370 exit_program(1);
1371 }
1372 ost->logfile = f;
1373 } else {
bd2ab27c
AK
1374 char *logbuffer = read_file(logfilename);
1375
1376 if (!logbuffer) {
6d5d9246
AK
1377 av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
1378 logfilename);
1379 exit_program(1);
1380 }
1381 video_enc->stats_in = logbuffer;
1382 }
1383 }
1384 }
1385
f5e66827
AK
1386 MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st);
1387 if (ost->forced_keyframes)
1388 ost->forced_keyframes = av_strdup(ost->forced_keyframes);
1389
1390 MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
1391
1392 ost->top_field_first = -1;
1393 MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
1394
a4208b9b
AK
1395
1396 ost->avfilter = get_ost_filters(o, oc, ost);
1397 if (!ost->avfilter)
b85dbe68 1398 exit_program(1);
f5e66827
AK
1399 } else {
1400 MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
1401 }
1402
1403 return ost;
1404}
1405
1406static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
1407{
1408 AVStream *st;
1409 OutputStream *ost;
1410 AVCodecContext *audio_enc;
1411
1412 ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO);
1413 st = ost->st;
1414
38313626 1415 audio_enc = ost->enc_ctx;
f5e66827
AK
1416 audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
1417
1418 if (!ost->stream_copy) {
1419 char *sample_fmt = NULL;
f5e66827
AK
1420
1421 MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
1422
1423 MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
1424 if (sample_fmt &&
1425 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
1426 av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
b85dbe68 1427 exit_program(1);
f5e66827
AK
1428 }
1429
1430 MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
1431
a4208b9b
AK
1432 ost->avfilter = get_ost_filters(o, oc, ost);
1433 if (!ost->avfilter)
b85dbe68 1434 exit_program(1);
f5e66827
AK
1435 }
1436
1437 return ost;
1438}
1439
1440static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc)
1441{
1442 OutputStream *ost;
1443
1444 ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
1445 if (!ost->stream_copy) {
1446 av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
b85dbe68 1447 exit_program(1);
f5e66827
AK
1448 }
1449
1450 return ost;
1451}
1452
1453static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
1454{
1455 OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
1456 ost->stream_copy = 1;
3e175a2a 1457 ost->finished = 1;
f5e66827
AK
1458 return ost;
1459}
1460
1461static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
1462{
f5e66827
AK
1463 OutputStream *ost;
1464 AVCodecContext *subtitle_enc;
1465
1466 ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE);
38313626 1467 subtitle_enc = ost->enc_ctx;
f5e66827
AK
1468
1469 subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
1470
1471 return ost;
1472}
1473
1474/* arg format is "output-stream-index:streamid-value". */
d3810c47 1475static int opt_streamid(void *optctx, const char *opt, const char *arg)
f5e66827 1476{
d3810c47 1477 OptionsContext *o = optctx;
f5e66827
AK
1478 int idx;
1479 char *p;
1480 char idx_str[16];
1481
1482 av_strlcpy(idx_str, arg, sizeof(idx_str));
1483 p = strchr(idx_str, ':');
1484 if (!p) {
1485 av_log(NULL, AV_LOG_FATAL,
1486 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
1487 arg, opt);
b85dbe68 1488 exit_program(1);
f5e66827
AK
1489 }
1490 *p++ = '\0';
1491 idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX);
1492 o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
1493 o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
1494 return 0;
1495}
1496
1497static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
1498{
1499 AVFormatContext *is = ifile->ctx;
1500 AVFormatContext *os = ofile->ctx;
18ff4d20 1501 AVChapter **tmp;
f5e66827
AK
1502 int i;
1503
18ff4d20
JG
1504 tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));
1505 if (!tmp)
1506 return AVERROR(ENOMEM);
1507 os->chapters = tmp;
1508
f5e66827
AK
1509 for (i = 0; i < is->nb_chapters; i++) {
1510 AVChapter *in_ch = is->chapters[i], *out_ch;
56ee3f9d
AK
1511 int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
1512 int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset,
f5e66827
AK
1513 AV_TIME_BASE_Q, in_ch->time_base);
1514 int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
1515 av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
1516
1517
1518 if (in_ch->end < ts_off)
1519 continue;
1520 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
1521 break;
1522
1523 out_ch = av_mallocz(sizeof(AVChapter));
1524 if (!out_ch)
1525 return AVERROR(ENOMEM);
1526
1527 out_ch->id = in_ch->id;
1528 out_ch->time_base = in_ch->time_base;
1529 out_ch->start = FFMAX(0, in_ch->start - ts_off);
1530 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
1531
1532 if (copy_metadata)
1533 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
1534
18ff4d20 1535 os->chapters[os->nb_chapters++] = out_ch;
f5e66827
AK
1536 }
1537 return 0;
1538}
1539
1540static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
1541 AVFormatContext *oc)
1542{
1543 OutputStream *ost;
1544
6d592fbd 1545 switch (ofilter->type) {
f5e66827
AK
1546 case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
1547 case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
1548 default:
1549 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters are supported "
1550 "currently.\n");
b85dbe68 1551 exit_program(1);
f5e66827
AK
1552 }
1553
1554 ost->source_index = -1;
1555 ost->filter = ofilter;
1556
1557 ofilter->ost = ost;
50722b4f 1558 ofilter->format = -1;
f5e66827
AK
1559
1560 if (ost->stream_copy) {
1561 av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
1562 "which is fed from a complex filtergraph. Filtering and streamcopy "
1563 "cannot be used together.\n", ost->file_index, ost->index);
b85dbe68 1564 exit_program(1);
f5e66827
AK
1565 }
1566
f5e66827
AK
1567 avfilter_inout_free(&ofilter->out_tmp);
1568}
1569
6d592fbd
AK
1570static int init_complex_filters(void)
1571{
1572 int i, ret = 0;
1573
1574 for (i = 0; i < nb_filtergraphs; i++) {
1575 ret = init_complex_filtergraph(filtergraphs[i]);
1576 if (ret < 0)
1577 return ret;
1578 }
1579 return 0;
1580}
1581
77bd1bc7 1582static int open_output_file(OptionsContext *o, const char *filename)
f5e66827 1583{
f5e66827
AK
1584 AVFormatContext *oc;
1585 int i, j, err;
1586 AVOutputFormat *file_oformat;
1da54e90 1587 OutputFile *of;
f5e66827
AK
1588 OutputStream *ost;
1589 InputStream *ist;
e7553f4c
AK
1590 AVDictionary *unused_opts = NULL;
1591 AVDictionaryEntry *e = NULL;
f5e66827 1592
3d624420
AK
1593 GROW_ARRAY(output_files, nb_output_files);
1594 of = av_mallocz(sizeof(*of));
1595 if (!of)
b85dbe68 1596 exit_program(1);
3d624420
AK
1597 output_files[nb_output_files - 1] = of;
1598
1599 of->ost_index = nb_output_streams;
1600 of->recording_time = o->recording_time;
1601 of->start_time = o->start_time;
1602 of->limit_filesize = o->limit_filesize;
1603 of->shortest = o->shortest;
1604 av_dict_copy(&of->opts, o->g->format_opts, 0);
1605
f5e66827
AK
1606 if (!strcmp(filename, "-"))
1607 filename = "pipe:";
1608
1609 oc = avformat_alloc_context();
1610 if (!oc) {
1611 print_error(filename, AVERROR(ENOMEM));
b85dbe68 1612 exit_program(1);
f5e66827 1613 }
3d624420
AK
1614 of->ctx = oc;
1615 if (o->recording_time != INT64_MAX)
1616 oc->duration = o->recording_time;
f5e66827
AK
1617
1618 if (o->format) {
1619 file_oformat = av_guess_format(o->format, NULL, NULL);
1620 if (!file_oformat) {
1621 av_log(NULL, AV_LOG_FATAL, "Requested output format '%s' is not a suitable output format\n", o->format);
b85dbe68 1622 exit_program(1);
f5e66827
AK
1623 }
1624 } else {
1625 file_oformat = av_guess_format(NULL, filename, NULL);
1626 if (!file_oformat) {
1627 av_log(NULL, AV_LOG_FATAL, "Unable to find a suitable output format for '%s'\n",
1628 filename);
b85dbe68 1629 exit_program(1);
f5e66827
AK
1630 }
1631 }
1632
1633 oc->oformat = file_oformat;
1634 oc->interrupt_callback = int_cb;
1635 av_strlcpy(oc->filename, filename, sizeof(oc->filename));
1636
1637 /* create streams for all unlabeled output pads */
1638 for (i = 0; i < nb_filtergraphs; i++) {
1639 FilterGraph *fg = filtergraphs[i];
1640 for (j = 0; j < fg->nb_outputs; j++) {
1641 OutputFilter *ofilter = fg->outputs[j];
1642
1643 if (!ofilter->out_tmp || ofilter->out_tmp->name)
1644 continue;
1645
6d592fbd 1646 switch (ofilter->type) {
f5e66827
AK
1647 case AVMEDIA_TYPE_VIDEO: o->video_disable = 1; break;
1648 case AVMEDIA_TYPE_AUDIO: o->audio_disable = 1; break;
1649 case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
1650 }
1651 init_output_filter(ofilter, o, oc);
1652 }
1653 }
1654
1655 if (!o->nb_stream_maps) {
1656 /* pick the "best" stream of each type */
1657#define NEW_STREAM(type, index)\
1658 if (index >= 0) {\
1659 ost = new_ ## type ## _stream(o, oc);\
1660 ost->source_index = index;\
1661 ost->sync_ist = input_streams[index];\
1662 input_streams[index]->discard = 0;\
1663 input_streams[index]->st->discard = AVDISCARD_NONE;\
1664 }
1665
1666 /* video: highest resolution */
1667 if (!o->video_disable && oc->oformat->video_codec != AV_CODEC_ID_NONE) {
1668 int area = 0, idx = -1;
1669 for (i = 0; i < nb_input_streams; i++) {
1670 ist = input_streams[i];
15e84ed3
AK
1671 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1672 ist->st->codecpar->width * ist->st->codecpar->height > area) {
1673 area = ist->st->codecpar->width * ist->st->codecpar->height;
f5e66827
AK
1674 idx = i;
1675 }
1676 }
1677 NEW_STREAM(video, idx);
1678 }
1679
1680 /* audio: most channels */
1681 if (!o->audio_disable && oc->oformat->audio_codec != AV_CODEC_ID_NONE) {
1682 int channels = 0, idx = -1;
1683 for (i = 0; i < nb_input_streams; i++) {
1684 ist = input_streams[i];
15e84ed3
AK
1685 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1686 ist->st->codecpar->channels > channels) {
1687 channels = ist->st->codecpar->channels;
f5e66827
AK
1688 idx = i;
1689 }
1690 }
1691 NEW_STREAM(audio, idx);
1692 }
1693
1694 /* subtitles: pick first */
1695 if (!o->subtitle_disable && oc->oformat->subtitle_codec != AV_CODEC_ID_NONE) {
1696 for (i = 0; i < nb_input_streams; i++)
15e84ed3 1697 if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
f5e66827
AK
1698 NEW_STREAM(subtitle, i);
1699 break;
1700 }
1701 }
1702 /* do something with data? */
1703 } else {
1704 for (i = 0; i < o->nb_stream_maps; i++) {
1705 StreamMap *map = &o->stream_maps[i];
1706
1707 if (map->disabled)
1708 continue;
1709
1710 if (map->linklabel) {
1711 FilterGraph *fg;
1712 OutputFilter *ofilter = NULL;
1713 int j, k;
1714
1715 for (j = 0; j < nb_filtergraphs; j++) {
1716 fg = filtergraphs[j];
1717 for (k = 0; k < fg->nb_outputs; k++) {
1718 AVFilterInOut *out = fg->outputs[k]->out_tmp;
1719 if (out && !strcmp(out->name, map->linklabel)) {
1720 ofilter = fg->outputs[k];
1721 goto loop_end;
1722 }
1723 }
1724 }
1725loop_end:
1726 if (!ofilter) {
1727 av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
1728 "in any defined filter graph.\n", map->linklabel);
b85dbe68 1729 exit_program(1);
f5e66827
AK
1730 }
1731 init_output_filter(ofilter, o, oc);
1732 } else {
1733 ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
15e84ed3 1734 switch (ist->st->codecpar->codec_type) {
f5e66827
AK
1735 case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
1736 case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
1737 case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
1738 case AVMEDIA_TYPE_DATA: ost = new_data_stream(o, oc); break;
1739 case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc); break;
1740 default:
1741 av_log(NULL, AV_LOG_FATAL, "Cannot map stream #%d:%d - unsupported type.\n",
1742 map->file_index, map->stream_index);
b85dbe68 1743 exit_program(1);
f5e66827
AK
1744 }
1745
1746 ost->source_index = input_files[map->file_index]->ist_index + map->stream_index;
1747 ost->sync_ist = input_streams[input_files[map->sync_file_index]->ist_index +
1748 map->sync_stream_index];
1749 ist->discard = 0;
1750 ist->st->discard = AVDISCARD_NONE;
1751 }
1752 }
1753 }
1754
1755 /* handle attached files */
1756 for (i = 0; i < o->nb_attachments; i++) {
1757 AVIOContext *pb;
1758 uint8_t *attachment;
1759 const char *p;
1760 int64_t len;
1761
1762 if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
1763 av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
1764 o->attachments[i]);
b85dbe68 1765 exit_program(1);
f5e66827
AK
1766 }
1767 if ((len = avio_size(pb)) <= 0) {
1768 av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
1769 o->attachments[i]);
b85dbe68 1770 exit_program(1);
f5e66827
AK
1771 }
1772 if (!(attachment = av_malloc(len))) {
1773 av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
1774 o->attachments[i]);
b85dbe68 1775 exit_program(1);
f5e66827
AK
1776 }
1777 avio_read(pb, attachment, len);
1778
1779 ost = new_attachment_stream(o, oc);
1780 ost->stream_copy = 0;
1781 ost->source_index = -1;
1782 ost->attachment_filename = o->attachments[i];
15e84ed3
AK
1783 ost->st->codecpar->extradata = attachment;
1784 ost->st->codecpar->extradata_size = len;
f5e66827
AK
1785
1786 p = strrchr(o->attachments[i], '/');
1787 av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
1788 avio_close(pb);
1789 }
1790
1959351a
AK
1791 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
1792 av_dump_format(oc, nb_output_files - 1, oc->filename, 1);
1793 av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", nb_output_files - 1);
1794 exit_program(1);
1795 }
1796
e7553f4c
AK
1797 /* check if all codec options have been used */
1798 unused_opts = strip_specifiers(o->g->codec_opts);
1799 for (i = of->ost_index; i < nb_output_streams; i++) {
1800 e = NULL;
9580818c 1801 while ((e = av_dict_get(output_streams[i]->encoder_opts, "", e,
e7553f4c
AK
1802 AV_DICT_IGNORE_SUFFIX)))
1803 av_dict_set(&unused_opts, e->key, NULL, 0);
1804 }
1805
1806 e = NULL;
1807 while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
1808 const AVClass *class = avcodec_get_class();
1809 const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
1810 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1811 if (!option)
1812 continue;
1813 if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
1814 av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
1815 "output file #%d (%s) is not an encoding option.\n", e->key,
1816 option->help ? option->help : "", nb_output_files - 1,
1817 filename);
b85dbe68 1818 exit_program(1);
e7553f4c
AK
1819 }
1820
1821 av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
1822 "output file #%d (%s) has not been used for any stream. The most "
1823 "likely reason is either wrong type (e.g. a video option with "
1824 "no video streams) or that it is a private option of some encoder "
1825 "which was not actually used for any stream.\n", e->key,
1826 option->help ? option->help : "", nb_output_files - 1, filename);
1827 }
1828 av_dict_free(&unused_opts);
1829
90944ee3 1830 /* set the decoding_needed flags and create simple filtergraphs */
59245e0c
AK
1831 for (i = of->ost_index; i < nb_output_streams; i++) {
1832 OutputStream *ost = output_streams[i];
1833
59245e0c
AK
1834 if (ost->encoding_needed && ost->source_index >= 0) {
1835 InputStream *ist = input_streams[ost->source_index];
1836 ist->decoding_needed = 1;
73c6ec6d
AK
1837
1838 if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
1839 ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1840 err = init_simple_filtergraph(ist, ost);
1841 if (err < 0) {
1842 av_log(NULL, AV_LOG_ERROR,
1843 "Error initializing a simple filtergraph between streams "
1844 "%d:%d->%d:%d\n", ist->file_index, ost->source_index,
1845 nb_output_files - 1, ost->st->index);
1846 exit_program(1);
1847 }
1848 }
59245e0c 1849 }
5fa255b6
AK
1850
1851 /*
1852 * We want CFR output if and only if one of those is true:
1853 * 1) user specified output framerate with -r
1854 * 2) user specified -vsync cfr
1855 * 3) output format is CFR and the user didn't force vsync to
1856 * something else than CFR
1857 *
1858 * in such a case, set ost->frame_rate
1859 */
1860 if (ost->encoding_needed && ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1861 int format_cfr = !(oc->oformat->flags & (AVFMT_NOTIMESTAMPS | AVFMT_VARIABLE_FPS));
1862 int need_cfr = !!ost->frame_rate.num;
1863
1864 if (video_sync_method == VSYNC_CFR ||
1865 (video_sync_method == VSYNC_AUTO && format_cfr))
1866 need_cfr = 1;
1867
1868 if (need_cfr && !ost->frame_rate.num) {
1869 InputStream *ist = ost->source_index >= 0 ? input_streams[ost->source_index] : NULL;
1870
1871 if (ist && ist->framerate.num)
1872 ost->frame_rate = ist->framerate;
1873 else if (ist && ist->st->avg_frame_rate.num)
1874 ost->frame_rate = ist->st->avg_frame_rate;
1875 else {
1876 av_log(NULL, AV_LOG_WARNING, "Constant framerate requested "
1877 "for the output stream #%d:%d, but no information "
1878 "about the input framerate is available. Falling "
1879 "back to a default value of 25fps. Use the -r option "
1880 "if you want a different framerate.\n",
1881 ost->file_index, ost->index);
1882 ost->frame_rate = (AVRational){ 25, 1 };
1883 }
1884 }
1885
1886 if (need_cfr && ost->enc->supported_framerates && !ost->force_fps) {
1887 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
1888 ost->frame_rate = ost->enc->supported_framerates[idx];
1889 }
1890 }
1891
50722b4f
AK
1892 /* set the filter output constraints */
1893 if (ost->filter) {
1894 OutputFilter *f = ost->filter;
1895 int count;
1896 switch (ost->enc_ctx->codec_type) {
1897 case AVMEDIA_TYPE_VIDEO:
1898 f->frame_rate = ost->frame_rate;
1899 f->width = ost->enc_ctx->width;
1900 f->height = ost->enc_ctx->height;
1901 if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
1902 f->format = ost->enc_ctx->pix_fmt;
1903 } else if (ost->enc->pix_fmts) {
1904 count = 0;
1905 while (ost->enc->pix_fmts[count] != AV_PIX_FMT_NONE)
1906 count++;
1907 f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
1908 if (!f->formats)
1909 exit_program(1);
1910 memcpy(f->formats, ost->enc->pix_fmts, (count + 1) * sizeof(*f->formats));
1911 }
1912 break;
1913 case AVMEDIA_TYPE_AUDIO:
1914 if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
1915 f->format = ost->enc_ctx->sample_fmt;
1916 } else if (ost->enc->sample_fmts) {
1917 count = 0;
1918 while (ost->enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
1919 count++;
1920 f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
1921 if (!f->formats)
1922 exit_program(1);
1923 memcpy(f->formats, ost->enc->sample_fmts, (count + 1) * sizeof(*f->formats));
1924 }
1925 if (ost->enc_ctx->sample_rate) {
1926 f->sample_rate = ost->enc_ctx->sample_rate;
1927 } else if (ost->enc->supported_samplerates) {
1928 count = 0;
1929 while (ost->enc->supported_samplerates[count])
1930 count++;
1931 f->sample_rates = av_mallocz_array(count + 1, sizeof(*f->sample_rates));
1932 if (!f->sample_rates)
1933 exit_program(1);
1934 memcpy(f->sample_rates, ost->enc->supported_samplerates,
1935 (count + 1) * sizeof(*f->sample_rates));
1936 }
1937 if (ost->enc_ctx->channels) {
1938 f->channel_layout = av_get_default_channel_layout(ost->enc_ctx->channels);
1939 } else if (ost->enc->channel_layouts) {
1940 count = 0;
1941 while (ost->enc->channel_layouts[count])
1942 count++;
1943 f->channel_layouts = av_mallocz_array(count + 1, sizeof(*f->channel_layouts));
1944 if (!f->channel_layouts)
1945 exit_program(1);
1946 memcpy(f->channel_layouts, ost->enc->channel_layouts,
1947 (count + 1) * sizeof(*f->channel_layouts));
1948 }
1949 break;
1950 }
1951 }
1952
59245e0c
AK
1953 }
1954
f5e66827
AK
1955 /* check filename in case of an image number is expected */
1956 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
1957 if (!av_filename_number_test(oc->filename)) {
1958 print_error(oc->filename, AVERROR(EINVAL));
b85dbe68 1959 exit_program(1);
f5e66827
AK
1960 }
1961 }
1962
1963 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
1964 /* test if it already exists to avoid losing precious files */
1965 assert_file_overwrite(filename);
1966
1967 /* open the file */
1968 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
1969 &oc->interrupt_callback,
1da54e90 1970 &of->opts)) < 0) {
f5e66827 1971 print_error(filename, err);
b85dbe68 1972 exit_program(1);
f5e66827
AK
1973 }
1974 }
1975
1976 if (o->mux_preload) {
1977 uint8_t buf[64];
1978 snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
1da54e90 1979 av_dict_set(&of->opts, "preload", buf, 0);
f5e66827
AK
1980 }
1981 oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
1982 oc->flags |= AVFMT_FLAG_NONBLOCK;
1983
1984 /* copy metadata */
1985 for (i = 0; i < o->nb_metadata_map; i++) {
1986 char *p;
1987 int in_file_index = strtol(o->metadata_map[i].u.str, &p, 0);
1988
f5e66827
AK
1989 if (in_file_index >= nb_input_files) {
1990 av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d while processing metadata maps\n", in_file_index);
b85dbe68 1991 exit_program(1);
f5e66827 1992 }
a119c64e
AK
1993 copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc,
1994 in_file_index >= 0 ?
1995 input_files[in_file_index]->ctx : NULL, o);
f5e66827
AK
1996 }
1997
1998 /* copy chapters */
1999 if (o->chapters_input_file >= nb_input_files) {
2000 if (o->chapters_input_file == INT_MAX) {
2001 /* copy chapters from the first input file that has them*/
2002 o->chapters_input_file = -1;
2003 for (i = 0; i < nb_input_files; i++)
2004 if (input_files[i]->ctx->nb_chapters) {
2005 o->chapters_input_file = i;
2006 break;
2007 }
2008 } else {
2009 av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
2010 o->chapters_input_file);
b85dbe68 2011 exit_program(1);
f5e66827
AK
2012 }
2013 }
2014 if (o->chapters_input_file >= 0)
1da54e90 2015 copy_chapters(input_files[o->chapters_input_file], of,
f5e66827
AK
2016 !o->metadata_chapters_manual);
2017
2018 /* copy global metadata by default */
2019 if (!o->metadata_global_manual && nb_input_files)
2020 av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata,
2021 AV_DICT_DONT_OVERWRITE);
2022 if (!o->metadata_streams_manual)
1da54e90 2023 for (i = of->ost_index; i < nb_output_streams; i++) {
f5e66827
AK
2024 InputStream *ist;
2025 if (output_streams[i]->source_index < 0) /* this is true e.g. for attached files */
2026 continue;
2027 ist = input_streams[output_streams[i]->source_index];
2028 av_dict_copy(&output_streams[i]->st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
2029 }
2030
2031 /* process manually set metadata */
2032 for (i = 0; i < o->nb_metadata; i++) {
2033 AVDictionary **m;
2034 char type, *val;
2035 const char *stream_spec;
2036 int index = 0, j, ret;
2037
2038 val = strchr(o->metadata[i].u.str, '=');
2039 if (!val) {
2040 av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
2041 o->metadata[i].u.str);
b85dbe68 2042 exit_program(1);
f5e66827
AK
2043 }
2044 *val++ = 0;
2045
2046 parse_meta_type(o->metadata[i].specifier, &type, &index, &stream_spec);
2047 if (type == 's') {
2048 for (j = 0; j < oc->nb_streams; j++) {
2049 if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
2050 av_dict_set(&oc->streams[j]->metadata, o->metadata[i].u.str, *val ? val : NULL, 0);
2051 } else if (ret < 0)
b85dbe68 2052 exit_program(1);
f5e66827
AK
2053 }
2054 }
2055 else {
2056 switch (type) {
2057 case 'g':
2058 m = &oc->metadata;
2059 break;
2060 case 'c':
2061 if (index < 0 || index >= oc->nb_chapters) {
2062 av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
b85dbe68 2063 exit_program(1);
f5e66827
AK
2064 }
2065 m = &oc->chapters[index]->metadata;
2066 break;
2067 default:
2068 av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
b85dbe68 2069 exit_program(1);
f5e66827
AK
2070 }
2071 av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
2072 }
2073 }
2074
77bd1bc7 2075 return 0;
f5e66827
AK
2076}
2077
d3810c47 2078static int opt_target(void *optctx, const char *opt, const char *arg)
f5e66827 2079{
d3810c47 2080 OptionsContext *o = optctx;
f5e66827
AK
2081 enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
2082 static const char *const frame_rates[] = { "25", "30000/1001", "24000/1001" };
2083
2084 if (!strncmp(arg, "pal-", 4)) {
2085 norm = PAL;
2086 arg += 4;
2087 } else if (!strncmp(arg, "ntsc-", 5)) {
2088 norm = NTSC;
2089 arg += 5;
2090 } else if (!strncmp(arg, "film-", 5)) {
2091 norm = FILM;
2092 arg += 5;
2093 } else {
2094 /* Try to determine PAL/NTSC by peeking in the input files */
2095 if (nb_input_files) {
2096 int i, j, fr;
2097 for (j = 0; j < nb_input_files; j++) {
2098 for (i = 0; i < input_files[j]->nb_streams; i++) {
15e84ed3
AK
2099 AVStream *st = input_files[j]->ctx->streams[i];
2100 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
f5e66827 2101 continue;
15e84ed3 2102 fr = st->time_base.den * 1000 / st->time_base.num;
f5e66827
AK
2103 if (fr == 25000) {
2104 norm = PAL;
2105 break;
2106 } else if ((fr == 29970) || (fr == 23976)) {
2107 norm = NTSC;
2108 break;
2109 }
2110 }
2111 if (norm != UNKNOWN)
2112 break;
2113 }
2114 }
2115 if (norm != UNKNOWN)
2116 av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
2117 }
2118
2119 if (norm == UNKNOWN) {
2120 av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
2121 av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
2122 av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
b85dbe68 2123 exit_program(1);
f5e66827
AK
2124 }
2125
2126 if (!strcmp(arg, "vcd")) {
2127 opt_video_codec(o, "c:v", "mpeg1video");
2128 opt_audio_codec(o, "c:a", "mp2");
2129 parse_option(o, "f", "vcd", options);
2130
2131 parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
2132 parse_option(o, "r", frame_rates[norm], options);
11d957fb 2133 opt_default(NULL, "g", norm == PAL ? "15" : "18");
f5e66827 2134
11d957fb
AK
2135 opt_default(NULL, "b", "1150000");
2136 opt_default(NULL, "maxrate", "1150000");
2137 opt_default(NULL, "minrate", "1150000");
2138 opt_default(NULL, "bufsize", "327680"); // 40*1024*8;
f5e66827 2139
11d957fb 2140 opt_default(NULL, "b:a", "224000");
f5e66827
AK
2141 parse_option(o, "ar", "44100", options);
2142 parse_option(o, "ac", "2", options);
2143
11d957fb 2144 opt_default(NULL, "packetsize", "2324");
1688eef2 2145 opt_default(NULL, "muxrate", "3528"); // 2352 * 75 / 50;
f5e66827
AK
2146
2147 /* We have to offset the PTS, so that it is consistent with the SCR.
2148 SCR starts at 36000, but the first two packs contain only padding
2149 and the first pack from the other stream, respectively, may also have
2150 been written before.
2151 So the real data starts at SCR 36000+3*1200. */
2152 o->mux_preload = (36000 + 3 * 1200) / 90000.0; // 0.44
2153 } else if (!strcmp(arg, "svcd")) {
2154
2155 opt_video_codec(o, "c:v", "mpeg2video");
2156 opt_audio_codec(o, "c:a", "mp2");
2157 parse_option(o, "f", "svcd", options);
2158
2159 parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
2160 parse_option(o, "r", frame_rates[norm], options);
11d957fb 2161 opt_default(NULL, "g", norm == PAL ? "15" : "18");
f5e66827 2162
11d957fb
AK
2163 opt_default(NULL, "b", "2040000");
2164 opt_default(NULL, "maxrate", "2516000");
2165 opt_default(NULL, "minrate", "0"); // 1145000;
2166 opt_default(NULL, "bufsize", "1835008"); // 224*1024*8;
fd665f7f 2167 opt_default(NULL, "scan_offset", "1");
f5e66827
AK
2168
2169
11d957fb 2170 opt_default(NULL, "b:a", "224000");
f5e66827
AK
2171 parse_option(o, "ar", "44100", options);
2172
11d957fb 2173 opt_default(NULL, "packetsize", "2324");
f5e66827
AK
2174
2175 } else if (!strcmp(arg, "dvd")) {
2176
2177 opt_video_codec(o, "c:v", "mpeg2video");
2178 opt_audio_codec(o, "c:a", "ac3");
2179 parse_option(o, "f", "dvd", options);
2180
2181 parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
2182 parse_option(o, "r", frame_rates[norm], options);
11d957fb 2183 opt_default(NULL, "g", norm == PAL ? "15" : "18");
f5e66827 2184
11d957fb
AK
2185 opt_default(NULL, "b", "6000000");
2186 opt_default(NULL, "maxrate", "9000000");
2187 opt_default(NULL, "minrate", "0"); // 1500000;
2188 opt_default(NULL, "bufsize", "1835008"); // 224*1024*8;
f5e66827 2189
11d957fb 2190 opt_default(NULL, "packetsize", "2048"); // from www.mpucoder.com: DVD sectors contain 2048 bytes of data, this is also the size of one pack.
1688eef2 2191 opt_default(NULL, "muxrate", "25200"); // from mplex project: data_rate = 1260000. mux_rate = data_rate / 50
f5e66827 2192
11d957fb 2193 opt_default(NULL, "b:a", "448000");
f5e66827
AK
2194 parse_option(o, "ar", "48000", options);
2195
2196 } else if (!strncmp(arg, "dv", 2)) {
2197
2198 parse_option(o, "f", "dv", options);
2199
2200 parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
2201 parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
2202 norm == PAL ? "yuv420p" : "yuv411p", options);
2203 parse_option(o, "r", frame_rates[norm], options);
2204
2205 parse_option(o, "ar", "48000", options);
2206 parse_option(o, "ac", "2", options);
2207
2208 } else {
2209 av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
2210 return AVERROR(EINVAL);
2211 }
f5245a9c
AK
2212
2213 av_dict_copy(&o->g->codec_opts, codec_opts, 0);
2214 av_dict_copy(&o->g->format_opts, format_opts, 0);
2215
f5e66827
AK
2216 return 0;
2217}
2218
11d957fb 2219static int opt_vstats_file(void *optctx, const char *opt, const char *arg)
f5e66827
AK
2220{
2221 av_free (vstats_filename);
2222 vstats_filename = av_strdup (arg);
2223 return 0;
2224}
2225
11d957fb 2226static int opt_vstats(void *optctx, const char *opt, const char *arg)
f5e66827
AK
2227{
2228 char filename[40];
2229 time_t today2 = time(NULL);
2230 struct tm *today = localtime(&today2);
2231
a9b2a511
VG
2232 if (!today) { // maybe tomorrow
2233 av_log(NULL, AV_LOG_FATAL, "Unable to get current time.\n");
2234 exit_program(1);
2235 }
2236
f5e66827
AK
2237 snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
2238 today->tm_sec);
11d957fb 2239 return opt_vstats_file(NULL, opt, filename);
f5e66827
AK
2240}
2241
d3810c47 2242static int opt_video_frames(void *optctx, const char *opt, const char *arg)
f5e66827 2243{
d3810c47 2244 OptionsContext *o = optctx;
f5e66827
AK
2245 return parse_option(o, "frames:v", arg, options);
2246}
2247
d3810c47 2248static int opt_audio_frames(void *optctx, const char *opt, const char *arg)
f5e66827 2249{
d3810c47 2250 OptionsContext *o = optctx;
f5e66827
AK
2251 return parse_option(o, "frames:a", arg, options);
2252}
2253
d3810c47 2254static int opt_data_frames(void *optctx, const char *opt, const char *arg)
f5e66827 2255{
d3810c47 2256 OptionsContext *o = optctx;
f5e66827
AK
2257 return parse_option(o, "frames:d", arg, options);
2258}
2259
d3810c47 2260static int opt_video_tag(void *optctx, const char *opt, const char *arg)
f5e66827 2261{
d3810c47 2262 OptionsContext *o = optctx;
f5e66827
AK
2263 return parse_option(o, "tag:v", arg, options);
2264}
2265
d3810c47 2266static int opt_audio_tag(void *optctx, const char *opt, const char *arg)
f5e66827 2267{
d3810c47 2268 OptionsContext *o = optctx;
f5e66827
AK
2269 return parse_option(o, "tag:a", arg, options);
2270}
2271
d3810c47 2272static int opt_subtitle_tag(void *optctx, const char *opt, const char *arg)
f5e66827 2273{
d3810c47 2274 OptionsContext *o = optctx;
f5e66827
AK
2275 return parse_option(o, "tag:s", arg, options);
2276}
2277
d3810c47 2278static int opt_video_filters(void *optctx, const char *opt, const char *arg)
f5e66827 2279{
d3810c47 2280 OptionsContext *o = optctx;
f5e66827
AK
2281 return parse_option(o, "filter:v", arg, options);
2282}
2283
d3810c47 2284static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
f5e66827 2285{
d3810c47 2286 OptionsContext *o = optctx;
f5e66827
AK
2287 return parse_option(o, "filter:a", arg, options);
2288}
2289
11d957fb 2290static int opt_vsync(void *optctx, const char *opt, const char *arg)
f5e66827
AK
2291{
2292 if (!av_strcasecmp(arg, "cfr")) video_sync_method = VSYNC_CFR;
2293 else if (!av_strcasecmp(arg, "vfr")) video_sync_method = VSYNC_VFR;
2294 else if (!av_strcasecmp(arg, "passthrough")) video_sync_method = VSYNC_PASSTHROUGH;
2295
2296 if (video_sync_method == VSYNC_AUTO)
2297 video_sync_method = parse_number_or_die("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR);
2298 return 0;
2299}
2300
d3810c47 2301static int opt_channel_layout(void *optctx, const char *opt, const char *arg)
f5e66827 2302{
d3810c47 2303 OptionsContext *o = optctx;
f5e66827
AK
2304 char layout_str[32];
2305 char *stream_str;
2306 char *ac_str;
2307 int ret, channels, ac_str_size;
2308 uint64_t layout;
2309
2310 layout = av_get_channel_layout(arg);
2311 if (!layout) {
2312 av_log(NULL, AV_LOG_ERROR, "Unknown channel layout: %s\n", arg);
2313 return AVERROR(EINVAL);
2314 }
2315 snprintf(layout_str, sizeof(layout_str), "%"PRIu64, layout);
11d957fb 2316 ret = opt_default(NULL, opt, layout_str);
f5e66827
AK
2317 if (ret < 0)
2318 return ret;
2319
2320 /* set 'ac' option based on channel layout */
2321 channels = av_get_channel_layout_nb_channels(layout);
2322 snprintf(layout_str, sizeof(layout_str), "%d", channels);
2323 stream_str = strchr(opt, ':');
2324 ac_str_size = 3 + (stream_str ? strlen(stream_str) : 0);
2325 ac_str = av_mallocz(ac_str_size);
2326 if (!ac_str)
2327 return AVERROR(ENOMEM);
2328 av_strlcpy(ac_str, "ac", 3);
2329 if (stream_str)
2330 av_strlcat(ac_str, stream_str, ac_str_size);
2331 ret = parse_option(o, ac_str, layout_str, options);
2332 av_free(ac_str);
2333
2334 return ret;
2335}
2336
d3810c47 2337static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
f5e66827 2338{
d3810c47 2339 OptionsContext *o = optctx;
f5e66827
AK
2340 return parse_option(o, "q:a", arg, options);
2341}
2342
11d957fb 2343static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
f5e66827 2344{
10bca661 2345 GROW_ARRAY(filtergraphs, nb_filtergraphs);
f5e66827
AK
2346 if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
2347 return AVERROR(ENOMEM);
a4208b9b
AK
2348 filtergraphs[nb_filtergraphs - 1]->index = nb_filtergraphs - 1;
2349 filtergraphs[nb_filtergraphs - 1]->graph_desc = av_strdup(arg);
2350 if (!filtergraphs[nb_filtergraphs - 1]->graph_desc)
2351 return AVERROR(ENOMEM);
2352 return 0;
2353}
2354
2355static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
2356{
2357 uint8_t *graph_desc = read_file(arg);
2358 if (!graph_desc)
2359 return AVERROR(EINVAL);
2360
2361 GROW_ARRAY(filtergraphs, nb_filtergraphs);
2362 if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
2363 return AVERROR(ENOMEM);
2364 filtergraphs[nb_filtergraphs - 1]->index = nb_filtergraphs - 1;
2365 filtergraphs[nb_filtergraphs - 1]->graph_desc = graph_desc;
f5e66827
AK
2366 return 0;
2367}
2368
a3ad68d3 2369void show_help_default(const char *opt, const char *arg)
f5e66827 2370{
f9fada27 2371 /* per-file options have at least one of those set */
11d957fb 2372 const int per_file = OPT_SPEC | OPT_OFFSET | OPT_PERFILE;
6e3857fa
AK
2373 int show_advanced = 0, show_avoptions = 0;
2374
8d09d39a 2375 if (opt && *opt) {
6e3857fa
AK
2376 if (!strcmp(opt, "long"))
2377 show_advanced = 1;
2378 else if (!strcmp(opt, "full"))
2379 show_advanced = show_avoptions = 1;
2380 else
2381 av_log(NULL, AV_LOG_ERROR, "Unknown help option '%s'.\n", opt);
2382 }
a3ad68d3 2383
f5e66827 2384 show_usage();
6e3857fa
AK
2385
2386 printf("Getting help:\n"
2387 " -h -- print basic options\n"
2388 " -h long -- print more options\n"
2389 " -h full -- print all options (including all format and codec specific options, very long)\n"
b5e4f393 2390 " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter\n"
6e3857fa
AK
2391 " See man %s for detailed description of the options.\n"
2392 "\n", program_name);
2393
f8b1e665 2394 show_help_options(options, "Print help / information / capabilities:",
f9fada27
AK
2395 OPT_EXIT, 0, 0);
2396
2397 show_help_options(options, "Global options (affect whole program "
2398 "instead of just one file:",
2399 0, per_file | OPT_EXIT | OPT_EXPERT, 0);
2400 if (show_advanced)
2401 show_help_options(options, "Advanced global options:", OPT_EXPERT,
2402 per_file | OPT_EXIT, 0);
2403
2404 show_help_options(options, "Per-file main options:", 0,
2405 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE |
2406 OPT_EXIT, per_file);
6e3857fa 2407 if (show_advanced)
f9fada27
AK
2408 show_help_options(options, "Advanced per-file options:",
2409 OPT_EXPERT, OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE, per_file);
6e3857fa 2410
dc4c24a3 2411 show_help_options(options, "Video options:",
f9fada27 2412 OPT_VIDEO, OPT_EXPERT | OPT_AUDIO, 0);
6e3857fa
AK
2413 if (show_advanced)
2414 show_help_options(options, "Advanced Video options:",
f9fada27 2415 OPT_EXPERT | OPT_VIDEO, OPT_AUDIO, 0);
6e3857fa 2416
dc4c24a3 2417 show_help_options(options, "Audio options:",
f9fada27 2418 OPT_AUDIO, OPT_EXPERT | OPT_VIDEO, 0);
6e3857fa
AK
2419 if (show_advanced)
2420 show_help_options(options, "Advanced Audio options:",
f9fada27 2421 OPT_EXPERT | OPT_AUDIO, OPT_VIDEO, 0);
dc4c24a3 2422 show_help_options(options, "Subtitle options:",
f9fada27 2423 OPT_SUBTITLE, 0, 0);
f5e66827 2424 printf("\n");
6e3857fa
AK
2425
2426 if (show_avoptions) {
2427 int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
2428 show_help_children(avcodec_get_class(), flags);
2429 show_help_children(avformat_get_class(), flags);
2430 show_help_children(sws_get_class(), flags);
dc574658 2431 show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM);
6e3857fa 2432 }
f5e66827
AK
2433}
2434
2435void show_usage(void)
2436{
2437 printf("Hyper fast Audio and Video encoder\n");
2438 printf("usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
2439 printf("\n");
2440}
2441
77bd1bc7
AK
2442enum OptGroup {
2443 GROUP_OUTFILE,
2444 GROUP_INFILE,
2445};
2446
2447static const OptionGroupDef groups[] = {
9d3009c6
AK
2448 [GROUP_OUTFILE] = { "output file", NULL, OPT_OUTPUT },
2449 [GROUP_INFILE] = { "input file", "i", OPT_INPUT },
77bd1bc7
AK
2450};
2451
2452static int open_files(OptionGroupList *l, const char *inout,
2453 int (*open_file)(OptionsContext*, const char*))
2454{
2455 int i, ret;
2456
2457 for (i = 0; i < l->nb_groups; i++) {
2458 OptionGroup *g = &l->groups[i];
2459 OptionsContext o;
2460
2461 init_options(&o);
2462 o.g = g;
2463
2464 ret = parse_optgroup(&o, g);
2465 if (ret < 0) {
2466 av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
2467 "%s.\n", inout, g->arg);
2468 return ret;
2469 }
2470
2471 av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
2472 ret = open_file(&o, g->arg);
2473 uninit_options(&o);
2474 if (ret < 0) {
2475 av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
2476 inout, g->arg);
2477 return ret;
2478 }
db2d65ce 2479 av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
77bd1bc7
AK
2480 }
2481
2482 return 0;
2483}
2484
2485int avconv_parse_options(int argc, char **argv)
2486{
2487 OptionParseContext octx;
2488 uint8_t error[128];
2489 int ret;
2490
2491 memset(&octx, 0, sizeof(octx));
2492
2493 /* split the commandline into an internal representation */
c661cb66
AK
2494 ret = split_commandline(&octx, argc, argv, options, groups,
2495 FF_ARRAY_ELEMS(groups));
77bd1bc7
AK
2496 if (ret < 0) {
2497 av_log(NULL, AV_LOG_FATAL, "Error splitting the argument list: ");
2498 goto fail;
2499 }
2500
2501 /* apply global options */
2502 ret = parse_optgroup(NULL, &octx.global_opts);
2503 if (ret < 0) {
2504 av_log(NULL, AV_LOG_FATAL, "Error parsing global options: ");
2505 goto fail;
2506 }
2507
2508 /* open input files */
2509 ret = open_files(&octx.groups[GROUP_INFILE], "input", open_input_file);
2510 if (ret < 0) {
2511 av_log(NULL, AV_LOG_FATAL, "Error opening input files: ");
2512 goto fail;
2513 }
2514
be101bc1 2515 /* create the complex filtergraphs */
6d592fbd 2516 ret = init_complex_filters();
be101bc1 2517 if (ret < 0) {
6d592fbd 2518 av_log(NULL, AV_LOG_FATAL, "Error initializing complex filters.\n");
be101bc1
AK
2519 goto fail;
2520 }
2521
77bd1bc7
AK
2522 /* open output files */
2523 ret = open_files(&octx.groups[GROUP_OUTFILE], "output", open_output_file);
2524 if (ret < 0) {
2525 av_log(NULL, AV_LOG_FATAL, "Error opening output files: ");
2526 goto fail;
2527 }
2528
2529fail:
2530 uninit_parse_context(&octx);
2531 if (ret < 0) {
2532 av_strerror(ret, error, sizeof(error));
2533 av_log(NULL, AV_LOG_FATAL, "%s\n", error);
2534 }
2535 return ret;
2536}
f5e66827
AK
2537
2538#define OFFSET(x) offsetof(OptionsContext, x)
2539const OptionDef options[] = {
2540 /* main options */
122de16d 2541 CMDUTILS_COMMON_OPTIONS
9d3009c6
AK
2542 { "f", HAS_ARG | OPT_STRING | OPT_OFFSET |
2543 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(format) },
8fc83fb0 2544 "force format", "fmt" },
8fc83fb0
AK
2545 { "y", OPT_BOOL, { &file_overwrite },
2546 "overwrite output files" },
7748dd41
VG
2547 { "n", OPT_BOOL, { &file_skip },
2548 "never overwrite output files" },
9d3009c6
AK
2549 { "c", HAS_ARG | OPT_STRING | OPT_SPEC |
2550 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
8fc83fb0 2551 "codec name", "codec" },
9d3009c6
AK
2552 { "codec", HAS_ARG | OPT_STRING | OPT_SPEC |
2553 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
8fc83fb0 2554 "codec name", "codec" },
9d3009c6
AK
2555 { "pre", HAS_ARG | OPT_STRING | OPT_SPEC |
2556 OPT_OUTPUT, { .off = OFFSET(presets) },
8fc83fb0 2557 "preset name", "preset" },
9d3009c6
AK
2558 { "map", HAS_ARG | OPT_EXPERT | OPT_PERFILE |
2559 OPT_OUTPUT, { .func_arg = opt_map },
8fc83fb0
AK
2560 "set input stream mapping",
2561 "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
9d3009c6
AK
2562 { "map_metadata", HAS_ARG | OPT_STRING | OPT_SPEC |
2563 OPT_OUTPUT, { .off = OFFSET(metadata_map) },
8fc83fb0
AK
2564 "set metadata information of outfile from infile",
2565 "outfile[,metadata]:infile[,metadata]" },
9d3009c6
AK
2566 { "map_chapters", HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET |
2567 OPT_OUTPUT, { .off = OFFSET(chapters_input_file) },
8fc83fb0 2568 "set chapters mapping", "input_file_index" },
488a0fa6
AK
2569 { "t", HAS_ARG | OPT_TIME | OPT_OFFSET |
2570 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(recording_time) },
8fc83fb0
AK
2571 "record or transcode \"duration\" seconds of audio/video",
2572 "duration" },
9d3009c6 2573 { "fs", HAS_ARG | OPT_INT64 | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(limit_filesize) },
8fc83fb0 2574 "set the limit file size in bytes", "limit_size" },
9d3009c6
AK
2575 { "ss", HAS_ARG | OPT_TIME | OPT_OFFSET |
2576 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(start_time) },
8fc83fb0 2577 "set the start time offset", "time_off" },
811bd078
AK
2578 { "accurate_seek", OPT_BOOL | OPT_OFFSET | OPT_EXPERT |
2579 OPT_INPUT, { .off = OFFSET(accurate_seek) },
2580 "enable/disable accurate seeking with -ss" },
9d3009c6
AK
2581 { "itsoffset", HAS_ARG | OPT_TIME | OPT_OFFSET |
2582 OPT_EXPERT | OPT_INPUT, { .off = OFFSET(input_ts_offset) },
8fc83fb0 2583 "set the input ts offset", "time_off" },
9d3009c6
AK
2584 { "itsscale", HAS_ARG | OPT_DOUBLE | OPT_SPEC |
2585 OPT_EXPERT | OPT_INPUT, { .off = OFFSET(ts_scale) },
8fc83fb0 2586 "set the input ts scale", "scale" },
9d3009c6 2587 { "metadata", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(metadata) },
8fc83fb0 2588 "add metadata", "string=string" },
9d3009c6
AK
2589 { "dframes", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
2590 OPT_OUTPUT, { .func_arg = opt_data_frames },
8fc83fb0
AK
2591 "set the number of data frames to record", "number" },
2592 { "benchmark", OPT_BOOL | OPT_EXPERT, { &do_benchmark },
2593 "add timings for benchmarking" },
602b1898 2594 { "timelimit", HAS_ARG | OPT_EXPERT, { .func_arg = opt_timelimit },
8fc83fb0
AK
2595 "set max runtime in seconds", "limit" },
2596 { "dump", OPT_BOOL | OPT_EXPERT, { &do_pkt_dump },
2597 "dump each input packet" },
2598 { "hex", OPT_BOOL | OPT_EXPERT, { &do_hex_dump },
2599 "when dumping packets, also dump the payload" },
9d3009c6
AK
2600 { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
2601 OPT_INPUT, { .off = OFFSET(rate_emu) },
8fc83fb0 2602 "read input at native frame rate", "" },
9d3009c6 2603 { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target },
8fc83fb0
AK
2604 "specify target file type (\"vcd\", \"svcd\", \"dvd\","
2605 " \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
943533d6 2606 { "vsync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vsync },
8fc83fb0
AK
2607 "video sync method", "" },
2608 { "async", HAS_ARG | OPT_INT | OPT_EXPERT, { &audio_sync_method },
2609 "audio sync method", "" },
2610 { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &audio_drift_threshold },
2611 "audio drift threshold", "threshold" },
2612 { "copyts", OPT_BOOL | OPT_EXPERT, { &copy_ts },
2613 "copy timestamps" },
2614 { "copytb", OPT_BOOL | OPT_EXPERT, { &copy_tb },
2615 "copy input stream time base when stream copying" },
9d3009c6
AK
2616 { "shortest", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
2617 OPT_OUTPUT, { .off = OFFSET(shortest) },
8fc83fb0
AK
2618 "finish encoding within shortest input" },
2619 { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &dts_delta_threshold },
2620 "timestamp discontinuity delta threshold", "threshold" },
602b1898 2621 { "xerror", OPT_BOOL | OPT_EXPERT, { &exit_on_error },
8fc83fb0 2622 "exit on error", "error" },
9d3009c6
AK
2623 { "copyinkf", OPT_BOOL | OPT_EXPERT | OPT_SPEC |
2624 OPT_OUTPUT, { .off = OFFSET(copy_initial_nonkeyframes) },
8fc83fb0 2625 "copy initial non-keyframes" },
9d3009c6 2626 { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(max_frames) },
8fc83fb0 2627 "set the number of frames to record", "number" },
9d3009c6 2628 { "tag", OPT_STRING | HAS_ARG | OPT_SPEC |
746dca48 2629 OPT_EXPERT | OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(codec_tags) },
8fc83fb0 2630 "force codec tag/fourcc", "fourcc/tag" },
9d3009c6
AK
2631 { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
2632 OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(qscale) },
8fc83fb0 2633 "use fixed quality scale (VBR)", "q" },
9d3009c6
AK
2634 { "qscale", HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
2635 OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(qscale) },
8fc83fb0 2636 "use fixed quality scale (VBR)", "q" },
b420a27e
AK
2637 { "b", HAS_ARG | OPT_INT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(bitrates) },
2638 "set stream bitrate in bits/second", "bitrate" },
9d3009c6 2639 { "filter", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
8fc83fb0 2640 "set stream filterchain", "filter_list" },
a4208b9b
AK
2641 { "filter_script", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
2642 "read stream filtergraph description from a file", "filename" },
8fc83fb0
AK
2643 { "filter_complex", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex },
2644 "create a complex filtergraph", "graph_description" },
a4208b9b
AK
2645 { "filter_complex_script", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex_script },
2646 "read complex filtergraph description from a file", "filename" },
8fc83fb0
AK
2647 { "stats", OPT_BOOL, { &print_stats },
2648 "print progress report during encoding", },
9d3009c6
AK
2649 { "attach", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
2650 OPT_OUTPUT, { .func_arg = opt_attach },
8fc83fb0 2651 "add an attachment to the output file", "filename" },
9d3009c6
AK
2652 { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |
2653 OPT_EXPERT | OPT_INPUT, { .off = OFFSET(dump_attachment) },
8fc83fb0 2654 "extract an attachment into a file", "filename" },
16b0c929
AH
2655 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_INPUT |
2656 OPT_OFFSET, { .off = OFFSET(loop) }, "set number of times input stream shall be looped", "loop count" },
f5e66827
AK
2657
2658 /* video options */
9d3009c6 2659 { "vframes", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_frames },
8fc83fb0 2660 "set the number of video frames to record", "number" },
9d3009c6
AK
2661 { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
2662 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },
8fc83fb0 2663 "set frame rate (Hz value, fraction or abbreviation)", "rate" },
9d3009c6
AK
2664 { "s", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
2665 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },
8fc83fb0 2666 "set frame size (WxH or abbreviation)", "size" },
9d3009c6
AK
2667 { "aspect", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
2668 OPT_OUTPUT, { .off = OFFSET(frame_aspect_ratios) },
8fc83fb0 2669 "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
9d3009c6
AK
2670 { "pix_fmt", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
2671 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_pix_fmts) },
8fc83fb0 2672 "set pixel format", "format" },
9d3009c6 2673 { "vn", OPT_VIDEO | OPT_BOOL | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(video_disable) },
8fc83fb0
AK
2674 "disable video" },
2675 { "vdt", OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT , { &video_discard },
2676 "discard threshold", "n" },
9d3009c6
AK
2677 { "rc_override", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
2678 OPT_OUTPUT, { .off = OFFSET(rc_overrides) },
8fc83fb0 2679 "rate control override for specific intervals", "override" },
9d3009c6
AK
2680 { "vcodec", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_INPUT |
2681 OPT_OUTPUT, { .func_arg = opt_video_codec },
8fc83fb0 2682 "force video codec ('copy' to copy stream)", "codec" },
9d3009c6 2683 { "pass", OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT | OPT_OUTPUT, { .off = OFFSET(pass) },
8fc83fb0 2684 "select the pass number (1 or 2)", "n" },
9d3009c6
AK
2685 { "passlogfile", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC |
2686 OPT_OUTPUT, { .off = OFFSET(passlogfiles) },
8fc83fb0 2687 "select two pass log file name prefix", "prefix" },
943533d6 2688 { "vstats", OPT_VIDEO | OPT_EXPERT , { .func_arg = &opt_vstats },
8fc83fb0 2689 "dump video coding statistics to file" },
943533d6 2690 { "vstats_file", OPT_VIDEO | HAS_ARG | OPT_EXPERT , { .func_arg = opt_vstats_file },
8fc83fb0 2691 "dump video coding statistics to file", "file" },
9d3009c6 2692 { "vf", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_filters },
8fc83fb0 2693 "video filters", "filter list" },
9d3009c6
AK
2694 { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
2695 OPT_OUTPUT, { .off = OFFSET(intra_matrices) },
8fc83fb0 2696 "specify intra matrix coeffs", "matrix" },
9d3009c6
AK
2697 { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
2698 OPT_OUTPUT, { .off = OFFSET(inter_matrices) },
8fc83fb0 2699 "specify inter matrix coeffs", "matrix" },
9d3009c6
AK
2700 { "top", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_INT| OPT_SPEC |
2701 OPT_OUTPUT, { .off = OFFSET(top_field_first) },
8fc83fb0
AK
2702 "top=1/bottom=0/auto=-1 field first", "" },
2703 { "dc", OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT , { &intra_dc_precision },
2704 "intra_dc_precision", "precision" },
9d3009c6
AK
2705 { "vtag", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
2706 OPT_OUTPUT, { .func_arg = opt_video_tag },
8fc83fb0
AK
2707 "force video tag/fourcc", "fourcc/tag" },
2708 { "qphist", OPT_VIDEO | OPT_BOOL | OPT_EXPERT , { &qp_hist },
2709 "show QP histogram" },
9d3009c6
AK
2710 { "force_fps", OPT_VIDEO | OPT_BOOL | OPT_EXPERT | OPT_SPEC |
2711 OPT_OUTPUT, { .off = OFFSET(force_fps) },
8fc83fb0 2712 "force the selected framerate, disable the best supported framerate selection" },
9d3009c6
AK
2713 { "streamid", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
2714 OPT_OUTPUT, { .func_arg = opt_streamid },
8fc83fb0 2715 "set the value of an outfile streamid", "streamIndex:value" },
9d3009c6
AK
2716 { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
2717 OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(forced_key_frames) },
2718 "force key frames at specified timestamps", "timestamps" },
07fd0a22
AK
2719 { "hwaccel", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
2720 OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccels) },
2721 "use HW accelerated decoding", "hwaccel name" },
2722 { "hwaccel_device", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
2723 OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_devices) },
629d4c5b 2724 "select a device for HW acceleration", "devicename" },
5d273d3e
MT
2725 { "hwaccel_output_format", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
2726 OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_output_formats) },
2727 "select output format used with HW accelerated decoding", "format" },
2728
c23999be
TG
2729 { "hwaccels", OPT_EXIT, { .func_arg = show_hwaccels },
2730 "show available HW acceleration methods" },
16302246
MS
2731 { "autorotate", HAS_ARG | OPT_BOOL | OPT_SPEC |
2732 OPT_EXPERT | OPT_INPUT, { .off = OFFSET(autorotate) },
2733 "automatically insert correct rotate filters" },
5d273d3e
MT
2734 { "hwaccel_lax_profile_check", OPT_BOOL | OPT_EXPERT, { &hwaccel_lax_profile_check},
2735 "attempt to decode anyway if HW accelerated decoder's supported profiles do not exactly match the stream" },
f5e66827
AK
2736
2737 /* audio options */
9d3009c6 2738 { "aframes", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_frames },
8fc83fb0 2739 "set the number of audio frames to record", "number" },
9d3009c6 2740 { "aq", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_qscale },
8fc83fb0 2741 "set audio quality (codec-specific)", "quality", },
9d3009c6
AK
2742 { "ar", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
2743 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_sample_rate) },
8fc83fb0 2744 "set audio sampling rate (in Hz)", "rate" },
9d3009c6
AK
2745 { "ac", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
2746 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_channels) },
8fc83fb0 2747 "set number of audio channels", "channels" },
9d3009c6 2748 { "an", OPT_AUDIO | OPT_BOOL | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(audio_disable) },
8fc83fb0 2749 "disable audio" },
9d3009c6
AK
2750 { "acodec", OPT_AUDIO | HAS_ARG | OPT_PERFILE |
2751 OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_audio_codec },
8fc83fb0 2752 "force audio codec ('copy' to copy stream)", "codec" },
9d3009c6
AK
2753 { "atag", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
2754 OPT_OUTPUT, { .func_arg = opt_audio_tag },
8fc83fb0
AK
2755 "force audio tag/fourcc", "fourcc/tag" },
2756 { "vol", OPT_AUDIO | HAS_ARG | OPT_INT, { &audio_volume },
2757 "change audio volume (256=normal)" , "volume" },
9d3009c6
AK
2758 { "sample_fmt", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_SPEC |
2759 OPT_STRING | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(sample_fmts) },
8fc83fb0 2760 "set sample format", "format" },
9d3009c6
AK
2761 { "channel_layout", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
2762 OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_channel_layout },
8fc83fb0 2763 "set channel layout", "layout" },
9d3009c6 2764 { "af", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_filters },
8fc83fb0 2765 "audio filters", "filter list" },
f5e66827
AK
2766
2767 /* subtitle options */
9d3009c6 2768 { "sn", OPT_SUBTITLE | OPT_BOOL | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(subtitle_disable) },
8fc83fb0 2769 "disable subtitle" },
9d3009c6 2770 { "scodec", OPT_SUBTITLE | HAS_ARG | OPT_PERFILE | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_subtitle_codec },
8fc83fb0 2771 "force subtitle codec ('copy' to copy stream)", "codec" },
9d3009c6 2772 { "stag", OPT_SUBTITLE | HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_subtitle_tag }
8fc83fb0 2773 , "force subtitle tag/fourcc", "fourcc/tag" },
f5e66827
AK
2774
2775 /* grab options */
79600a83 2776 { "isync", OPT_BOOL | OPT_EXPERT, { &input_sync }, "this option is deprecated and does nothing", "" },
f5e66827
AK
2777
2778 /* muxer options */
9d3009c6 2779 { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_max_delay) },
8fc83fb0 2780 "set the maximum demux-decode delay", "seconds" },
9d3009c6 2781 { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_preload) },
8fc83fb0 2782 "set the initial demux-decode delay", "seconds" },
f5e66827 2783
9d3009c6 2784 { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(bitstream_filters) },
8fc83fb0 2785 "A comma-separated list of bitstream filters", "bitstream_filters" },
f5e66827 2786
398f015f
AK
2787 { "max_muxing_queue_size", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(max_muxing_queue_size) },
2788 "maximum number of packets that can be buffered while waiting for all streams to initialize", "packets" },
2789
f5e66827 2790 /* data codec support */
9d3009c6 2791 { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_data_codec },
8fc83fb0 2792 "force data codec ('copy' to copy stream)", "codec" },
f5e66827 2793
5d273d3e
MT
2794#if CONFIG_VAAPI
2795 { "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
2796 "set VAAPI hardware device (DRM path or X11 display name)", "device" },
2797#endif
2798
d2e6dd32
MT
2799 { "init_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_init_hw_device },
2800 "initialise hardware device", "args" },
e669db76
MT
2801 { "filter_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_hw_device },
2802 "set hardware device used when filtering", "device" },
d2e6dd32 2803
f5e66827
AK
2804 { NULL, },
2805};