tests: Only run noproxy test if networking is enabled
[libav.git] / cmdutils.c
CommitLineData
01310af2
FB
1/*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
01310af2
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
01310af2 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
01310af2
FB
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
01310af2 20 */
364a9607 21
0f4e8165
RB
22#include <string.h>
23#include <stdlib.h>
24#include <errno.h>
7c84b8bc 25#include <math.h>
0f4e8165 26
2cd39dbf
DP
27/* Include only the enabled headers since some compilers (namely, Sun
28 Studio) will not omit unused inline functions and create undefined
29 references to libraries that are not being built. */
30
63d026b1 31#include "config.h"
245976da
DB
32#include "libavformat/avformat.h"
33#include "libavfilter/avfilter.h"
34#include "libavdevice/avdevice.h"
bcb82fe1 35#include "libavresample/avresample.h"
db6d50c7 36#include "libswscale/swscale.h"
7cf78b34 37#include "libavutil/avassert.h"
245976da 38#include "libavutil/avstring.h"
85770d6e 39#include "libavutil/mathematics.h"
d55c2e05 40#include "libavutil/imgutils.h"
f6c7375a 41#include "libavutil/parseutils.h"
9cb5c760 42#include "libavutil/pixdesc.h"
25e25617 43#include "libavutil/eval.h"
9ba38229 44#include "libavutil/dict.h"
41d0eb1c 45#include "libavutil/opt.h"
01310af2 46#include "cmdutils.h"
86074ed1 47#include "version.h"
da2dc39e 48#if CONFIG_NETWORK
245976da 49#include "libavformat/network.h"
da2dc39e 50#endif
ffcc6e24 51#if HAVE_SYS_RESOURCE_H
3f65eff4 52#include <sys/time.h>
ffcc6e24
MR
53#include <sys/resource.h>
54#endif
01310af2 55
85663ef3 56struct SwsContext *sws_opts;
5c7db097 57AVDictionary *format_opts, *codec_opts, *resample_opts;
086ab001 58
7768a635 59static const int this_year = 2013;
ef4c0bb1 60
a5c33faa
RD
61void init_opts(void)
62{
be4876ea 63#if CONFIG_SWSCALE
cbaef7d1
AD
64 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
65 NULL, NULL, NULL);
be4876ea 66#endif
a5c33faa
RD
67}
68
69void uninit_opts(void)
70{
be4876ea 71#if CONFIG_SWSCALE
faf8d3dd
RB
72 sws_freeContext(sws_opts);
73 sws_opts = NULL;
be4876ea 74#endif
9ba38229 75 av_dict_free(&format_opts);
8c8eab8b 76 av_dict_free(&codec_opts);
5c7db097 77 av_dict_free(&resample_opts);
a5c33faa
RD
78}
79
cbaef7d1 80void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
1790d3b3
SS
81{
82 vfprintf(stdout, fmt, vl);
83}
84
cbaef7d1
AD
85double parse_number_or_die(const char *context, const char *numstr, int type,
86 double min, double max)
086ab001
MN
87{
88 char *tail;
89 const char *error;
25e25617 90 double d = av_strtod(numstr, &tail);
086ab001 91 if (*tail)
cbaef7d1 92 error = "Expected number for %s but found: %s\n";
086ab001 93 else if (d < min || d > max)
cbaef7d1
AD
94 error = "The value for %s was %s which is not within %f - %f\n";
95 else if (type == OPT_INT64 && (int64_t)d != d)
96 error = "Expected int64 for %s but found %s\n";
3372ec32 97 else if (type == OPT_INT && (int)d != d)
cbaef7d1 98 error = "Expected int for %s but found %s\n";
086ab001
MN
99 else
100 return d;
1c5e9551 101 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
5e3f9979 102 exit(1);
dad09ff9 103 return 0;
086ab001
MN
104}
105
cbaef7d1
AD
106int64_t parse_time_or_die(const char *context, const char *timestr,
107 int is_duration)
7542157d 108{
f6c7375a
SS
109 int64_t us;
110 if (av_parse_time(&us, timestr, is_duration) < 0) {
1c5e9551
AK
111 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
112 is_duration ? "duration" : "date", context, timestr);
5e3f9979 113 exit(1);
7542157d
SS
114 }
115 return us;
116}
117
7c501212 118void show_help_options(const OptionDef *options, const char *msg, int req_flags,
f9fada27 119 int rej_flags, int alt_flags)
01310af2
FB
120{
121 const OptionDef *po;
02d504a7 122 int first;
01310af2 123
02d504a7 124 first = 1;
cbaef7d1 125 for (po = options; po->name != NULL; po++) {
02d504a7 126 char buf[64];
7c501212
AK
127
128 if (((po->flags & req_flags) != req_flags) ||
f9fada27 129 (alt_flags && !(po->flags & alt_flags)) ||
7c501212
AK
130 (po->flags & rej_flags))
131 continue;
132
133 if (first) {
134 printf("%s\n", msg);
135 first = 0;
136 }
137 av_strlcpy(buf, po->name, sizeof(buf));
a3ad68d3 138 if (po->argname) {
7c501212
AK
139 av_strlcat(buf, " ", sizeof(buf));
140 av_strlcat(buf, po->argname, sizeof(buf));
01310af2 141 }
7c501212 142 printf("-%-17s %s\n", buf, po->help);
01310af2 143 }
dc4c24a3 144 printf("\n");
01310af2
FB
145}
146
7a6cd995
AK
147void show_help_children(const AVClass *class, int flags)
148{
149 const AVClass *child = NULL;
150 av_opt_show2(&class, NULL, flags, 0);
151 printf("\n");
152
153 while (child = av_opt_child_class_next(class, child))
154 show_help_children(child, flags);
155}
156
cbaef7d1
AD
157static const OptionDef *find_option(const OptionDef *po, const char *name)
158{
d70e5122
AK
159 const char *p = strchr(name, ':');
160 int len = p ? p - name : strlen(name);
161
8bbf6db9 162 while (po->name != NULL) {
d70e5122 163 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
8bbf6db9
MN
164 break;
165 po++;
166 }
167 return po;
168}
169
15caccb9 170#if HAVE_COMMANDLINETOARGVW
ba9327ea 171#include <windows.h>
66a29797 172#include <shellapi.h>
b1ac139d
KG
173/* Will be leaked on exit */
174static char** win32_argv_utf8 = NULL;
175static int win32_argc = 0;
176
177/**
178 * Prepare command line arguments for executable.
179 * For Windows - perform wide-char to UTF-8 conversion.
180 * Input arguments should be main() function arguments.
181 * @param argc_ptr Arguments number (including executable)
182 * @param argv_ptr Arguments list.
183 */
184static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
185{
186 char *argstr_flat;
187 wchar_t **argv_w;
188 int i, buffsize = 0, offset = 0;
189
190 if (win32_argv_utf8) {
191 *argc_ptr = win32_argc;
192 *argv_ptr = win32_argv_utf8;
193 return;
194 }
195
196 win32_argc = 0;
197 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
198 if (win32_argc <= 0 || !argv_w)
199 return;
200
201 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
202 for (i = 0; i < win32_argc; i++)
203 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
204 NULL, 0, NULL, NULL);
205
cbaef7d1
AD
206 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
207 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
b1ac139d
KG
208 if (win32_argv_utf8 == NULL) {
209 LocalFree(argv_w);
210 return;
211 }
212
213 for (i = 0; i < win32_argc; i++) {
214 win32_argv_utf8[i] = &argstr_flat[offset];
215 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
216 &argstr_flat[offset],
217 buffsize - offset, NULL, NULL);
218 }
219 win32_argv_utf8[i] = NULL;
220 LocalFree(argv_w);
221
222 *argc_ptr = win32_argc;
223 *argv_ptr = win32_argv_utf8;
224}
225#else
226static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
227{
228 /* nothing to do */
229}
15caccb9 230#endif /* HAVE_COMMANDLINETOARGVW */
b1ac139d 231
a01c02bc
AK
232static int write_option(void *optctx, const OptionDef *po, const char *opt,
233 const char *arg)
2f8e586d 234{
2f8e586d
AK
235 /* new-style options contain an offset into optctx, old-style address of
236 * a global var*/
a01c02bc
AK
237 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
238 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
239 int *dstcount;
6361c5e1
AK
240
241 if (po->flags & OPT_SPEC) {
242 SpecifierOpt **so = dst;
243 char *p = strchr(opt, ':');
244
cbaef7d1 245 dstcount = (int *)(so + 1);
6361c5e1
AK
246 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
247 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
248 dst = &(*so)[*dstcount - 1].u;
249 }
2f8e586d
AK
250
251 if (po->flags & OPT_STRING) {
252 char *str;
253 str = av_strdup(arg);
a1bcc76e 254 av_freep(dst);
cbaef7d1 255 *(char **)dst = str;
a01c02bc 256 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
cbaef7d1 257 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
2f8e586d 258 } else if (po->flags & OPT_INT64) {
cbaef7d1 259 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
6b779ccc 260 } else if (po->flags & OPT_TIME) {
cbaef7d1 261 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
2f8e586d 262 } else if (po->flags & OPT_FLOAT) {
cbaef7d1 263 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
33f75d72 264 } else if (po->flags & OPT_DOUBLE) {
cbaef7d1 265 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
2f8e586d 266 } else if (po->u.func_arg) {
11d957fb 267 int ret = po->u.func_arg(optctx, opt, arg);
2f8e586d 268 if (ret < 0) {
cbaef7d1
AD
269 av_log(NULL, AV_LOG_ERROR,
270 "Failed to set value '%s' for option '%s'\n", arg, opt);
2f8e586d
AK
271 return ret;
272 }
273 }
274 if (po->flags & OPT_EXIT)
5e3f9979 275 exit(0);
a01c02bc
AK
276
277 return 0;
278}
279
280int parse_option(void *optctx, const char *opt, const char *arg,
281 const OptionDef *options)
282{
283 const OptionDef *po;
284 int ret;
285
286 po = find_option(options, opt);
287 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
288 /* handle 'no' bool option */
289 po = find_option(options, opt + 2);
290 if ((po->name && (po->flags & OPT_BOOL)))
291 arg = "0";
292 } else if (po->flags & OPT_BOOL)
293 arg = "1";
294
295 if (!po->name)
296 po = find_option(options, "default");
297 if (!po->name) {
298 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
299 return AVERROR(EINVAL);
300 }
301 if (po->flags & HAS_ARG && !arg) {
302 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
303 return AVERROR(EINVAL);
304 }
305
306 ret = write_option(optctx, po, opt, arg);
307 if (ret < 0)
308 return ret;
309
2f8e586d
AK
310 return !!(po->flags & HAS_ARG);
311}
312
7cc8d638 313void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
cbaef7d1 314 void (*parse_arg_function)(void *, const char*))
01310af2 315{
2f8e586d
AK
316 const char *opt;
317 int optindex, handleoptions = 1, ret;
01310af2 318
b1ac139d
KG
319 /* perform system-dependent conversions for arguments list */
320 prepare_app_arguments(&argc, &argv);
321
01310af2
FB
322 /* parse options */
323 optindex = 1;
324 while (optindex < argc) {
325 opt = argv[optindex++];
115329f1 326
84bf226b 327 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
3749076c
SS
328 if (opt[1] == '-' && opt[2] == '\0') {
329 handleoptions = 0;
330 continue;
331 }
c3c78324 332 opt++;
2f8e586d
AK
333
334 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
5e3f9979 335 exit(1);
2f8e586d 336 optindex += ret;
01310af2 337 } else {
60a9966e 338 if (parse_arg_function)
7cc8d638 339 parse_arg_function(optctx, opt);
01310af2
FB
340 }
341 }
342}
343
8c9af5b2
AK
344int parse_optgroup(void *optctx, OptionGroup *g)
345{
346 int i, ret;
347
348 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
349 g->group_def->name, g->arg);
350
351 for (i = 0; i < g->nb_opts; i++) {
352 Option *o = &g->opts[i];
353
9d3009c6
AK
354 if (g->group_def->flags &&
355 !(g->group_def->flags & o->opt->flags)) {
356 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
357 "%s %s -- you are trying to apply an input option to an "
358 "output file or vice versa. Move this option before the "
359 "file it belongs to.\n", o->key, o->opt->help,
360 g->group_def->name, g->arg);
361 return AVERROR(EINVAL);
362 }
363
8c9af5b2
AK
364 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
365 o->key, o->opt->help, o->val);
366
367 ret = write_option(optctx, o->opt, o->key, o->val);
368 if (ret < 0)
369 return ret;
370 }
371
372 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
373
374 return 0;
375}
376
4138cd29
AK
377int locate_option(int argc, char **argv, const OptionDef *options,
378 const char *optname)
182cbe43
AK
379{
380 const OptionDef *po;
381 int i;
382
383 for (i = 1; i < argc; i++) {
384 const char *cur_opt = argv[i];
385
386 if (*cur_opt++ != '-')
387 continue;
388
389 po = find_option(options, cur_opt);
390 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
391 po = find_option(options, cur_opt + 2);
392
393 if ((!po->name && !strcmp(cur_opt, optname)) ||
394 (po->name && !strcmp(optname, po->name)))
395 return i;
396
397 if (!po || po->flags & HAS_ARG)
398 i++;
399 }
400 return 0;
401}
402
403void parse_loglevel(int argc, char **argv, const OptionDef *options)
404{
405 int idx = locate_option(argc, argv, options, "loglevel");
8124d8ad
MR
406 if (!idx)
407 idx = locate_option(argc, argv, options, "v");
182cbe43 408 if (idx && argv[idx + 1])
11d957fb 409 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
182cbe43
AK
410}
411
145f741e 412#define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
11d957fb 413int opt_default(void *optctx, const char *opt, const char *arg)
9ba38229
AK
414{
415 const AVOption *o;
d4863fc1
AK
416 char opt_stripped[128];
417 const char *p;
ab799664 418 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
e8da8075 419#if CONFIG_AVRESAMPLE
5c7db097 420 const AVClass *rc = avresample_get_class();
e8da8075 421#endif
ab799664
DB
422#if CONFIG_SWSCALE
423 const AVClass *sc = sws_get_class();
424#endif
d4863fc1
AK
425
426 if (!(p = strchr(opt, ':')))
427 p = opt + strlen(opt);
428 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
429
cbaef7d1
AD
430 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
431 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
432 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
433 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
8c8eab8b 434 av_dict_set(&codec_opts, opt, arg, FLAGS);
cbaef7d1
AD
435 else if ((o = av_opt_find(&fc, opt, NULL, 0,
436 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
9ba38229 437 av_dict_set(&format_opts, opt, arg, FLAGS);
e8da8075 438#if CONFIG_AVRESAMPLE
5c7db097
JR
439 else if ((o = av_opt_find(&rc, opt, NULL, 0,
440 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
441 av_dict_set(&resample_opts, opt, arg, FLAGS);
e8da8075 442#endif
ab799664 443#if CONFIG_SWSCALE
cbaef7d1
AD
444 else if ((o = av_opt_find(&sc, opt, NULL, 0,
445 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
9ba38229 446 // XXX we only support sws_flags, not arbitrary sws options
3b3ea346 447 int ret = av_opt_set(sws_opts, opt, arg, 0);
9ba38229
AK
448 if (ret < 0) {
449 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
450 return ret;
451 }
452 }
ab799664 453#endif
9ba38229 454
9ba38229
AK
455 if (o)
456 return 0;
9ba38229
AK
457 return AVERROR_OPTION_NOT_FOUND;
458}
459
8c9af5b2
AK
460/*
461 * Check whether given option is a group separator.
462 *
463 * @return index of the group definition that matched or -1 if none
464 */
c661cb66
AK
465static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
466 const char *opt)
8c9af5b2 467{
c661cb66 468 int i;
8c9af5b2 469
c661cb66
AK
470 for (i = 0; i < nb_groups; i++) {
471 const OptionGroupDef *p = &groups[i];
8c9af5b2 472 if (p->sep && !strcmp(p->sep, opt))
c661cb66 473 return i;
8c9af5b2
AK
474 }
475
476 return -1;
477}
478
479/*
480 * Finish parsing an option group.
481 *
482 * @param group_idx which group definition should this group belong to
483 * @param arg argument of the group delimiting option
484 */
485static void finish_group(OptionParseContext *octx, int group_idx,
486 const char *arg)
487{
488 OptionGroupList *l = &octx->groups[group_idx];
489 OptionGroup *g;
490
491 GROW_ARRAY(l->groups, l->nb_groups);
492 g = &l->groups[l->nb_groups - 1];
493
494 *g = octx->cur_group;
495 g->arg = arg;
496 g->group_def = l->group_def;
497#if CONFIG_SWSCALE
498 g->sws_opts = sws_opts;
499#endif
500 g->codec_opts = codec_opts;
501 g->format_opts = format_opts;
5c7db097 502 g->resample_opts = resample_opts;
8c9af5b2
AK
503
504 codec_opts = NULL;
505 format_opts = NULL;
5c7db097 506 resample_opts = NULL;
8c9af5b2
AK
507#if CONFIG_SWSCALE
508 sws_opts = NULL;
509#endif
510 init_opts();
511
512 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
513}
514
515/*
516 * Add an option instance to currently parsed group.
517 */
518static void add_opt(OptionParseContext *octx, const OptionDef *opt,
519 const char *key, const char *val)
520{
521 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
522 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
523
524 GROW_ARRAY(g->opts, g->nb_opts);
525 g->opts[g->nb_opts - 1].opt = opt;
526 g->opts[g->nb_opts - 1].key = key;
527 g->opts[g->nb_opts - 1].val = val;
528}
529
530static void init_parse_context(OptionParseContext *octx,
c661cb66 531 const OptionGroupDef *groups, int nb_groups)
8c9af5b2
AK
532{
533 static const OptionGroupDef global_group = { "global" };
8c9af5b2
AK
534 int i;
535
536 memset(octx, 0, sizeof(*octx));
537
c661cb66 538 octx->nb_groups = nb_groups;
8c9af5b2
AK
539 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
540 if (!octx->groups)
541 exit(1);
542
543 for (i = 0; i < octx->nb_groups; i++)
544 octx->groups[i].group_def = &groups[i];
545
546 octx->global_opts.group_def = &global_group;
547 octx->global_opts.arg = "";
548
549 init_opts();
550}
551
552void uninit_parse_context(OptionParseContext *octx)
553{
554 int i, j;
555
556 for (i = 0; i < octx->nb_groups; i++) {
557 OptionGroupList *l = &octx->groups[i];
558
559 for (j = 0; j < l->nb_groups; j++) {
560 av_freep(&l->groups[j].opts);
561 av_dict_free(&l->groups[j].codec_opts);
562 av_dict_free(&l->groups[j].format_opts);
5c7db097 563 av_dict_free(&l->groups[j].resample_opts);
8c9af5b2
AK
564#if CONFIG_SWSCALE
565 sws_freeContext(l->groups[j].sws_opts);
566#endif
567 }
568 av_freep(&l->groups);
569 }
570 av_freep(&octx->groups);
571
572 av_freep(&octx->cur_group.opts);
573 av_freep(&octx->global_opts.opts);
574
575 uninit_opts();
576}
577
578int split_commandline(OptionParseContext *octx, int argc, char *argv[],
579 const OptionDef *options,
c661cb66 580 const OptionGroupDef *groups, int nb_groups)
8c9af5b2
AK
581{
582 int optindex = 1;
583
584 /* perform system-dependent conversions for arguments list */
585 prepare_app_arguments(&argc, &argv);
586
c661cb66 587 init_parse_context(octx, groups, nb_groups);
8c9af5b2
AK
588 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
589
590 while (optindex < argc) {
591 const char *opt = argv[optindex++], *arg;
592 const OptionDef *po;
593 int ret;
594
595 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
596
597 /* unnamed group separators, e.g. output filename */
598 if (opt[0] != '-' || !opt[1]) {
599 finish_group(octx, 0, opt);
600 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
601 continue;
602 }
603 opt++;
604
605#define GET_ARG(arg) \
606do { \
607 arg = argv[optindex++]; \
608 if (!arg) { \
609 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
610 return AVERROR(EINVAL); \
611 } \
612} while (0)
613
614 /* named group separators, e.g. -i */
c661cb66 615 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
8c9af5b2
AK
616 GET_ARG(arg);
617 finish_group(octx, ret, arg);
618 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
619 groups[ret].name, arg);
620 continue;
621 }
622
623 /* normal options */
624 po = find_option(options, opt);
625 if (po->name) {
626 if (po->flags & OPT_EXIT) {
627 /* optional argument, e.g. -h */
628 arg = argv[optindex++];
629 } else if (po->flags & HAS_ARG) {
630 GET_ARG(arg);
631 } else {
632 arg = "1";
633 }
634
635 add_opt(octx, po, opt, arg);
636 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
637 "argument '%s'.\n", po->name, po->help, arg);
638 continue;
639 }
640
641 /* AVOptions */
642 if (argv[optindex]) {
643 ret = opt_default(NULL, opt, argv[optindex]);
644 if (ret >= 0) {
645 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
646 "argument '%s'.\n", opt, argv[optindex]);
647 optindex++;
648 continue;
649 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
650 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
651 "with argument '%s'.\n", opt, argv[optindex]);
652 return ret;
653 }
654 }
655
656 /* boolean -nofoo options */
657 if (opt[0] == 'n' && opt[1] == 'o' &&
658 (po = find_option(options, opt + 2)) &&
659 po->name && po->flags & OPT_BOOL) {
660 add_opt(octx, po, opt, "0");
661 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
662 "argument 0.\n", po->name, po->help);
663 continue;
664 }
665
666 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
667 return AVERROR_OPTION_NOT_FOUND;
668 }
669
5c7db097 670 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
8c9af5b2
AK
671 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
672 "commandline.\n");
673
674 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
675
676 return 0;
677}
678
11d957fb 679int opt_loglevel(void *optctx, const char *opt, const char *arg)
4c97a6fa 680{
da4c2dab 681 const struct { const char *name; int level; } log_levels[] = {
4c97a6fa
SS
682 { "quiet" , AV_LOG_QUIET },
683 { "panic" , AV_LOG_PANIC },
684 { "fatal" , AV_LOG_FATAL },
685 { "error" , AV_LOG_ERROR },
686 { "warning", AV_LOG_WARNING },
687 { "info" , AV_LOG_INFO },
688 { "verbose", AV_LOG_VERBOSE },
689 { "debug" , AV_LOG_DEBUG },
690 };
691 char *tail;
692 int level;
693 int i;
694
695 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
696 if (!strcmp(log_levels[i].name, arg)) {
697 av_log_set_level(log_levels[i].level);
698 return 0;
699 }
700 }
701
702 level = strtol(arg, &tail, 10);
703 if (*tail) {
1c5e9551
AK
704 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
705 "Possible levels are numbers or:\n", arg);
4c97a6fa 706 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
1c5e9551 707 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
5e3f9979 708 exit(1);
4c97a6fa
SS
709 }
710 av_log_set_level(level);
711 return 0;
712}
713
11d957fb 714int opt_timelimit(void *optctx, const char *opt, const char *arg)
ffcc6e24 715{
0104b608 716#if HAVE_SETRLIMIT
ffcc6e24
MR
717 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
718 struct rlimit rl = { lim, lim + 1 };
719 if (setrlimit(RLIMIT_CPU, &rl))
720 perror("setrlimit");
721#else
1c5e9551 722 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
ffcc6e24
MR
723#endif
724 return 0;
725}
726
01310af2
FB
727void print_error(const char *filename, int err)
728{
7a5bdd79 729 char errbuf[128];
735ef67b 730 const char *errbuf_ptr = errbuf;
7a5bdd79 731
9e94bd3e
SS
732 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
733 errbuf_ptr = strerror(AVUNERROR(err));
1c5e9551 734 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
01310af2 735}
f35917b2 736
d101e731
SS
737static int warned_cfg = 0;
738
208749a0
SS
739#define INDENT 1
740#define SHOW_VERSION 2
d101e731 741#define SHOW_CONFIG 4
208749a0 742
1c5e9551 743#define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
5116571d 744 if (CONFIG_##LIBNAME) { \
65dd2ded 745 const char *indent = flags & INDENT? " " : ""; \
208749a0 746 if (flags & SHOW_VERSION) { \
b6525b4b 747 unsigned int version = libname##_version(); \
bcb82fe1
JR
748 av_log(NULL, level, \
749 "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
1c5e9551
AK
750 indent, #libname, \
751 LIB##LIBNAME##_VERSION_MAJOR, \
752 LIB##LIBNAME##_VERSION_MINOR, \
753 LIB##LIBNAME##_VERSION_MICRO, \
754 version >> 16, version >> 8 & 0xff, version & 0xff); \
208749a0 755 } \
d101e731
SS
756 if (flags & SHOW_CONFIG) { \
757 const char *cfg = libname##_configuration(); \
29ba0911 758 if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
d101e731 759 if (!warned_cfg) { \
1c5e9551 760 av_log(NULL, level, \
d101e731 761 "%sWARNING: library configuration mismatch\n", \
65dd2ded 762 indent); \
d101e731
SS
763 warned_cfg = 1; \
764 } \
1c5e9551 765 av_log(NULL, level, "%s%-11s configuration: %s\n", \
65dd2ded 766 indent, #libname, cfg); \
d101e731
SS
767 } \
768 } \
208749a0 769 } \
9a109272 770
1c5e9551 771static void print_all_libs_info(int flags, int level)
9a109272 772{
1c5e9551
AK
773 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
774 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
775 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
776 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
777 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
bcb82fe1 778 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1c5e9551 779 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
9a109272
SS
780}
781
ea9c581f 782void show_banner(void)
86074ed1 783{
cbaef7d1
AD
784 av_log(NULL, AV_LOG_INFO,
785 "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
1c5e9551 786 program_name, program_birth_year, this_year);
45870f8b
MR
787 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
788 __DATE__, __TIME__, CC_IDENT);
1c5e9551
AK
789 av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
790 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
791 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
86074ed1
SS
792}
793
11d957fb 794int show_version(void *optctx, const char *opt, const char *arg)
f617135b 795{
1c5e9551 796 av_log_set_callback(log_callback_help);
a03be6e1 797 printf("%s " LIBAV_VERSION "\n", program_name);
1c5e9551 798 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
f617135b
AK
799
800 return 0;
86074ed1
SS
801}
802
11d957fb 803int show_license(void *optctx, const char *opt, const char *arg)
f35917b2 804{
7ead693b 805 printf(
3bf28f9d 806#if CONFIG_NONFREE
304ba23a
SS
807 "This version of %s has nonfree parts compiled in.\n"
808 "Therefore it is not legally redistributable.\n",
809 program_name
9cad0e4e
DB
810#elif CONFIG_GPLV3
811 "%s is free software; you can redistribute it and/or modify\n"
812 "it under the terms of the GNU General Public License as published by\n"
813 "the Free Software Foundation; either version 3 of the License, or\n"
814 "(at your option) any later version.\n"
815 "\n"
816 "%s is distributed in the hope that it will be useful,\n"
817 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
818 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
819 "GNU General Public License for more details.\n"
820 "\n"
821 "You should have received a copy of the GNU General Public License\n"
822 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
823 program_name, program_name, program_name
7ead693b 824#elif CONFIG_GPL
304ba23a 825 "%s is free software; you can redistribute it and/or modify\n"
f35917b2
SS
826 "it under the terms of the GNU General Public License as published by\n"
827 "the Free Software Foundation; either version 2 of the License, or\n"
828 "(at your option) any later version.\n"
829 "\n"
304ba23a 830 "%s is distributed in the hope that it will be useful,\n"
f35917b2
SS
831 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
832 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
833 "GNU General Public License for more details.\n"
834 "\n"
835 "You should have received a copy of the GNU General Public License\n"
304ba23a
SS
836 "along with %s; if not, write to the Free Software\n"
837 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
838 program_name, program_name, program_name
9cad0e4e
DB
839#elif CONFIG_LGPLV3
840 "%s is free software; you can redistribute it and/or modify\n"
841 "it under the terms of the GNU Lesser General Public License as published by\n"
842 "the Free Software Foundation; either version 3 of the License, or\n"
843 "(at your option) any later version.\n"
844 "\n"
845 "%s is distributed in the hope that it will be useful,\n"
846 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
847 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
848 "GNU Lesser General Public License for more details.\n"
849 "\n"
850 "You should have received a copy of the GNU Lesser General Public License\n"
851 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
852 program_name, program_name, program_name
f35917b2 853#else
304ba23a 854 "%s is free software; you can redistribute it and/or\n"
f35917b2
SS
855 "modify it under the terms of the GNU Lesser General Public\n"
856 "License as published by the Free Software Foundation; either\n"
857 "version 2.1 of the License, or (at your option) any later version.\n"
858 "\n"
304ba23a 859 "%s is distributed in the hope that it will be useful,\n"
f35917b2
SS
860 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
861 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
862 "Lesser General Public License for more details.\n"
863 "\n"
864 "You should have received a copy of the GNU Lesser General Public\n"
304ba23a
SS
865 "License along with %s; if not, write to the Free Software\n"
866 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
867 program_name, program_name, program_name
f35917b2 868#endif
3bf28f9d 869 );
f617135b
AK
870
871 return 0;
f35917b2 872}
ba9880c1 873
11d957fb 874int show_formats(void *optctx, const char *opt, const char *arg)
ba9880c1 875{
cbaef7d1
AD
876 AVInputFormat *ifmt = NULL;
877 AVOutputFormat *ofmt = NULL;
ba9880c1
SS
878 const char *last_name;
879
cbaef7d1
AD
880 printf("File formats:\n"
881 " D. = Demuxing supported\n"
882 " .E = Muxing supported\n"
883 " --\n");
884 last_name = "000";
885 for (;;) {
886 int decode = 0;
887 int encode = 0;
888 const char *name = NULL;
889 const char *long_name = NULL;
890
891 while ((ofmt = av_oformat_next(ofmt))) {
892 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
893 strcmp(ofmt->name, last_name) > 0) {
894 name = ofmt->name;
895 long_name = ofmt->long_name;
896 encode = 1;
ba9880c1
SS
897 }
898 }
cbaef7d1
AD
899 while ((ifmt = av_iformat_next(ifmt))) {
900 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
901 strcmp(ifmt->name, last_name) > 0) {
902 name = ifmt->name;
903 long_name = ifmt->long_name;
904 encode = 0;
ba9880c1 905 }
cbaef7d1
AD
906 if (name && strcmp(ifmt->name, name) == 0)
907 decode = 1;
ba9880c1 908 }
cbaef7d1 909 if (name == NULL)
ba9880c1 910 break;
cbaef7d1 911 last_name = name;
ba9880c1 912
cbaef7d1
AD
913 printf(" %s%s %-15s %s\n",
914 decode ? "D" : " ",
915 encode ? "E" : " ",
916 name,
ba9880c1
SS
917 long_name ? long_name:" ");
918 }
f617135b 919 return 0;
8447f0bd 920}
ba9880c1 921
a3ad68d3
AK
922#define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
923 if (codec->field) { \
924 const type *p = c->field; \
925 \
926 printf(" Supported " list_name ":"); \
927 while (*p != term) { \
928 get_name(*p); \
929 printf(" %s", name); \
930 p++; \
931 } \
932 printf("\n"); \
933 } \
934
935static void print_codec(const AVCodec *c)
936{
937 int encoder = av_codec_is_encoder(c);
938
939 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
940 c->long_name ? c->long_name : "");
941
942 if (c->type == AVMEDIA_TYPE_VIDEO) {
943 printf(" Threading capabilities: ");
944 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
945 CODEC_CAP_SLICE_THREADS)) {
946 case CODEC_CAP_FRAME_THREADS |
947 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
948 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
949 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
950 default: printf("no"); break;
951 }
952 printf("\n");
953 }
954
955 if (c->supported_framerates) {
956 const AVRational *fps = c->supported_framerates;
957
958 printf(" Supported framerates:");
959 while (fps->num) {
960 printf(" %d/%d", fps->num, fps->den);
961 fps++;
962 }
963 printf("\n");
964 }
716d413c
AK
965 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
966 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
a3ad68d3
AK
967 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
968 GET_SAMPLE_RATE_NAME);
969 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
970 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
971 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
972 0, GET_CH_LAYOUT_DESC);
973
974 if (c->priv_class) {
975 show_help_children(c->priv_class,
976 AV_OPT_FLAG_ENCODING_PARAM |
977 AV_OPT_FLAG_DECODING_PARAM);
978 }
979}
980
7c101949
AK
981static char get_media_type_char(enum AVMediaType type)
982{
983 switch (type) {
984 case AVMEDIA_TYPE_VIDEO: return 'V';
985 case AVMEDIA_TYPE_AUDIO: return 'A';
986 case AVMEDIA_TYPE_SUBTITLE: return 'S';
987 default: return '?';
988 }
989}
990
991static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
992 int encoder)
993{
994 while ((prev = av_codec_next(prev))) {
995 if (prev->id == id &&
996 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
997 return prev;
998 }
999 return NULL;
1000}
1001
1002static void print_codecs_for_id(enum AVCodecID id, int encoder)
1003{
1004 const AVCodec *codec = NULL;
1005
1006 printf(" (%s: ", encoder ? "encoders" : "decoders");
1007
1008 while ((codec = next_codec_for_id(id, codec, encoder)))
1009 printf("%s ", codec->name);
1010
1011 printf(")");
1012}
1013
11d957fb 1014int show_codecs(void *optctx, const char *opt, const char *arg)
8447f0bd 1015{
7c101949
AK
1016 const AVCodecDescriptor *desc = NULL;
1017
cbaef7d1 1018 printf("Codecs:\n"
e2785fa7
AK
1019 " D..... = Decoding supported\n"
1020 " .E.... = Encoding supported\n"
1021 " ..V... = Video codec\n"
1022 " ..A... = Audio codec\n"
1023 " ..S... = Subtitle codec\n"
1024 " ...I.. = Intra frame-only codec\n"
1025 " ....L. = Lossy compression\n"
1026 " .....S = Lossless compression\n"
1027 " -------\n");
7c101949
AK
1028 while ((desc = avcodec_descriptor_next(desc))) {
1029 const AVCodec *codec = NULL;
1030
1031 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1032 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1033
1034 printf("%c", get_media_type_char(desc->type));
1035 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
e2785fa7
AK
1036 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1037 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
7c101949
AK
1038
1039 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1040
1041 /* print decoders/encoders when there's more than one or their
1042 * names are different from codec name */
1043 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1044 if (strcmp(codec->name, desc->name)) {
1045 print_codecs_for_id(desc->id, 0);
1046 break;
ba9880c1 1047 }
7c101949
AK
1048 }
1049 codec = NULL;
1050 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1051 if (strcmp(codec->name, desc->name)) {
1052 print_codecs_for_id(desc->id, 1);
1053 break;
ba9880c1
SS
1054 }
1055 }
ba9880c1 1056
ba9880c1
SS
1057 printf("\n");
1058 }
f617135b 1059 return 0;
7c101949
AK
1060}
1061
1062static void print_codecs(int encoder)
1063{
1064 const AVCodecDescriptor *desc = NULL;
1065
1066 printf("%s:\n"
1067 " V... = Video\n"
1068 " A... = Audio\n"
1069 " S... = Subtitle\n"
1070 " .F.. = Frame-level multithreading\n"
1071 " ..S. = Slice-level multithreading\n"
1072 " ...X = Codec is experimental\n"
1073 " ---\n",
1074 encoder ? "Encoders" : "Decoders");
1075 while ((desc = avcodec_descriptor_next(desc))) {
1076 const AVCodec *codec = NULL;
1077
1078 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1079 printf("%c", get_media_type_char(desc->type));
1080 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1081 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1082 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1083
1084 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1085 if (strcmp(codec->name, desc->name))
1086 printf(" (codec %s)", desc->name);
1087
1088 printf("\n");
1089 }
1090 }
1091}
1092
11d957fb 1093int show_decoders(void *optctx, const char *opt, const char *arg)
7c101949
AK
1094{
1095 print_codecs(0);
1096 return 0;
1097}
1098
11d957fb 1099int show_encoders(void *optctx, const char *opt, const char *arg)
7c101949
AK
1100{
1101 print_codecs(1);
1102 return 0;
8447f0bd
MN
1103}
1104
11d957fb 1105int show_bsfs(void *optctx, const char *opt, const char *arg)
8447f0bd 1106{
cbaef7d1 1107 AVBitStreamFilter *bsf = NULL;
ba9880c1
SS
1108
1109 printf("Bitstream filters:\n");
cbaef7d1 1110 while ((bsf = av_bitstream_filter_next(bsf)))
2091b27b 1111 printf("%s\n", bsf->name);
ba9880c1 1112 printf("\n");
f617135b 1113 return 0;
8447f0bd
MN
1114}
1115
11d957fb 1116int show_protocols(void *optctx, const char *opt, const char *arg)
8447f0bd 1117{
cdc6a87f
AK
1118 void *opaque = NULL;
1119 const char *name;
ba9880c1 1120
2ee5c789 1121 printf("Supported file protocols:\n"
cdc6a87f
AK
1122 "Input:\n");
1123 while ((name = avio_enum_protocols(&opaque, 0)))
1124 printf("%s\n", name);
1125 printf("Output:\n");
1126 while ((name = avio_enum_protocols(&opaque, 1)))
1127 printf("%s\n", name);
f617135b 1128 return 0;
ba9880c1 1129}
090b61b2 1130
11d957fb 1131int show_filters(void *optctx, const char *opt, const char *arg)
62d75662 1132{
fa2a34cd 1133 const AVFilter av_unused(*filter) = NULL;
62d75662
SS
1134
1135 printf("Filters:\n");
663c2edf 1136#if CONFIG_AVFILTER
fa2a34cd
AK
1137 while ((filter = avfilter_next(filter)))
1138 printf("%-16s %s\n", filter->name, filter->description);
663c2edf 1139#endif
f617135b 1140 return 0;
62d75662
SS
1141}
1142
11d957fb 1143int show_pix_fmts(void *optctx, const char *opt, const char *arg)
3f7bb426 1144{
b7f1010c 1145 const AVPixFmtDescriptor *pix_desc = NULL;
9cb5c760 1146
cbaef7d1
AD
1147 printf("Pixel formats:\n"
1148 "I.... = Supported Input format for conversion\n"
1149 ".O... = Supported Output format for conversion\n"
1150 "..H.. = Hardware accelerated format\n"
1151 "...P. = Paletted format\n"
1152 "....B = Bitstream format\n"
1153 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1154 "-----\n");
9cb5c760 1155
e24db3e3
MR
1156#if !CONFIG_SWSCALE
1157# define sws_isSupportedInput(x) 0
1158# define sws_isSupportedOutput(x) 0
1159#endif
1160
b7f1010c
AK
1161 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1162 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
9cb5c760 1163 printf("%c%c%c%c%c %-16s %d %2d\n",
e6c4ac7b
AK
1164 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1165 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1166 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1167 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1168 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
9cb5c760
SS
1169 pix_desc->name,
1170 pix_desc->nb_components,
1171 av_get_bits_per_pixel(pix_desc));
1172 }
f617135b 1173 return 0;
3f7bb426
SS
1174}
1175
11d957fb 1176int show_sample_fmts(void *optctx, const char *opt, const char *arg)
05bffc12
AK
1177{
1178 int i;
1179 char fmt_str[128];
1180 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1181 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1182 return 0;
1183}
1184
a3ad68d3
AK
1185static void show_help_codec(const char *name, int encoder)
1186{
1187 const AVCodecDescriptor *desc;
1188 const AVCodec *codec;
1189
1190 if (!name) {
1191 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1192 return;
1193 }
1194
1195 codec = encoder ? avcodec_find_encoder_by_name(name) :
1196 avcodec_find_decoder_by_name(name);
1197
1198 if (codec)
1199 print_codec(codec);
1200 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1201 int printed = 0;
1202
1203 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1204 printed = 1;
1205 print_codec(codec);
1206 }
1207
1208 if (!printed) {
1209 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
1210 "but no %s for it are available. Libav might need to be "
1211 "recompiled with additional external libraries.\n",
1212 name, encoder ? "encoders" : "decoders");
1213 }
1214 } else {
1215 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
1216 name);
1217 }
1218}
1219
1136bd36
AK
1220static void show_help_demuxer(const char *name)
1221{
1222 const AVInputFormat *fmt = av_find_input_format(name);
1223
1224 if (!fmt) {
1225 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1226 return;
1227 }
1228
1229 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1230
1231 if (fmt->extensions)
1232 printf(" Common extensions: %s.\n", fmt->extensions);
1233
1234 if (fmt->priv_class)
1235 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1236}
1237
1238static void show_help_muxer(const char *name)
1239{
1240 const AVCodecDescriptor *desc;
1241 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1242
1243 if (!fmt) {
1244 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1245 return;
1246 }
1247
1248 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1249
1250 if (fmt->extensions)
1251 printf(" Common extensions: %s.\n", fmt->extensions);
1252 if (fmt->mime_type)
1253 printf(" Mime type: %s.\n", fmt->mime_type);
1254 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1255 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1256 printf(" Default video codec: %s.\n", desc->name);
1257 }
1258 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1259 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1260 printf(" Default audio codec: %s.\n", desc->name);
1261 }
1262 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1263 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1264 printf(" Default subtitle codec: %s.\n", desc->name);
1265 }
1266
1267 if (fmt->priv_class)
1268 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1269}
1270
7f1fd976 1271#if CONFIG_AVFILTER
92104600
AK
1272static void show_help_filter(const char *name)
1273{
1274 const AVFilter *f = avfilter_get_by_name(name);
1275 int i, count;
1276
1277 if (!name) {
1278 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1279 return;
1280 } else if (!f) {
1281 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1282 return;
1283 }
1284
1285 printf("Filter %s [%s]:\n", f->name, f->description);
129bb238
AK
1286
1287 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1288 printf(" slice threading supported\n");
92104600
AK
1289
1290 printf(" Inputs:\n");
1291 count = avfilter_pad_count(f->inputs);
1292 for (i = 0; i < count; i++) {
1293 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1294 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1295 }
1296 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1297 printf(" dynamic (depending on the options)\n");
1298
1299 printf(" Outputs:\n");
1300 count = avfilter_pad_count(f->outputs);
1301 for (i = 0; i < count; i++) {
1302 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1303 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1304 }
1305 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1306 printf(" dynamic (depending on the options)\n");
1307
1308 if (f->priv_class)
1309 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
1310 AV_OPT_FLAG_AUDIO_PARAM);
1311}
7f1fd976 1312#endif
92104600 1313
11d957fb 1314int show_help(void *optctx, const char *opt, const char *arg)
a3ad68d3
AK
1315{
1316 char *topic, *par;
1317 av_log_set_callback(log_callback_help);
1318
1319 topic = av_strdup(arg ? arg : "");
1320 par = strchr(topic, '=');
1321 if (par)
1322 *par++ = 0;
1323
1324 if (!*topic) {
1325 show_help_default(topic, par);
1326 } else if (!strcmp(topic, "decoder")) {
1327 show_help_codec(par, 0);
1328 } else if (!strcmp(topic, "encoder")) {
1329 show_help_codec(par, 1);
1136bd36
AK
1330 } else if (!strcmp(topic, "demuxer")) {
1331 show_help_demuxer(par);
1332 } else if (!strcmp(topic, "muxer")) {
1333 show_help_muxer(par);
7f1fd976 1334#if CONFIG_AVFILTER
92104600
AK
1335 } else if (!strcmp(topic, "filter")) {
1336 show_help_filter(par);
7f1fd976 1337#endif
a3ad68d3
AK
1338 } else {
1339 show_help_default(topic, par);
1340 }
1341
1342 av_freep(&topic);
1343 return 0;
1344}
1345
090b61b2
SS
1346int read_yesno(void)
1347{
1348 int c = getchar();
efa7f420 1349 int yesno = (av_toupper(c) == 'Y');
090b61b2
SS
1350
1351 while (c != '\n' && c != EOF)
1352 c = getchar();
1353
1354 return yesno;
1355}
458b062d 1356
02170990 1357int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
458b062d 1358{
d94c577d 1359 int ret;
c56e9e05 1360 FILE *f = fopen(filename, "rb");
458b062d
SS
1361
1362 if (!f) {
cbaef7d1
AD
1363 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1364 strerror(errno));
458b062d
SS
1365 return AVERROR(errno);
1366 }
1367 fseek(f, 0, SEEK_END);
1368 *size = ftell(f);
1369 fseek(f, 0, SEEK_SET);
1370 *bufptr = av_malloc(*size + 1);
1371 if (!*bufptr) {
1c5e9551 1372 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
458b062d
SS
1373 fclose(f);
1374 return AVERROR(ENOMEM);
1375 }
d94c577d
JG
1376 ret = fread(*bufptr, 1, *size, f);
1377 if (ret < *size) {
1378 av_free(*bufptr);
1379 if (ferror(f)) {
1380 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1381 filename, strerror(errno));
1382 ret = AVERROR(errno);
1383 } else
1384 ret = AVERROR_EOF;
1385 } else {
1386 ret = 0;
b94e4acb 1387 (*bufptr)[(*size)++] = '\0';
d94c577d 1388 }
458b062d
SS
1389
1390 fclose(f);
d94c577d 1391 return ret;
458b062d 1392}
7a8bfa5d
AS
1393
1394void init_pts_correction(PtsCorrectionContext *ctx)
1395{
1396 ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
1397 ctx->last_pts = ctx->last_dts = INT64_MIN;
1398}
1399
cbaef7d1
AD
1400int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
1401 int64_t dts)
7a8bfa5d
AS
1402{
1403 int64_t pts = AV_NOPTS_VALUE;
1404
1405 if (dts != AV_NOPTS_VALUE) {
1406 ctx->num_faulty_dts += dts <= ctx->last_dts;
1407 ctx->last_dts = dts;
1408 }
1409 if (reordered_pts != AV_NOPTS_VALUE) {
1410 ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
1411 ctx->last_pts = reordered_pts;
1412 }
01d46198 1413 if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
cbaef7d1 1414 && reordered_pts != AV_NOPTS_VALUE)
7a8bfa5d
AS
1415 pts = reordered_pts;
1416 else
1417 pts = dts;
1418
1419 return pts;
1420}
f7ead94c 1421
6e872935 1422FILE *get_preset_file(char *filename, size_t filename_size,
cbaef7d1
AD
1423 const char *preset_name, int is_path,
1424 const char *codec_name)
6e872935
SS
1425{
1426 FILE *f = NULL;
1427 int i;
cbaef7d1
AD
1428 const char *base[3] = { getenv("AVCONV_DATADIR"),
1429 getenv("HOME"),
1430 AVCONV_DATADIR, };
6e872935
SS
1431
1432 if (is_path) {
1433 av_strlcpy(filename, preset_name, filename_size);
1434 f = fopen(filename, "r");
1435 } else {
1436 for (i = 0; i < 3 && !f; i++) {
1437 if (!base[i])
1438 continue;
050dc127 1439 snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
cbaef7d1 1440 i != 1 ? "" : "/.avconv", preset_name);
6e872935
SS
1441 f = fopen(filename, "r");
1442 if (!f && codec_name) {
1443 snprintf(filename, filename_size,
050dc127 1444 "%s%s/%s-%s.avpreset",
cbaef7d1
AD
1445 base[i], i != 1 ? "" : "/.avconv", codec_name,
1446 preset_name);
6e872935
SS
1447 f = fopen(filename, "r");
1448 }
1449 }
1450 }
1451
1452 return f;
1453}
1454
d4863fc1
AK
1455int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1456{
cbaef7d1 1457 if (*spec <= '9' && *spec >= '0') /* opt:index */
d4863fc1 1458 return strtol(spec, NULL, 0) == st->index;
cbaef7d1
AD
1459 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1460 *spec == 't') { /* opt:[vasdt] */
d4863fc1
AK
1461 enum AVMediaType type;
1462
1463 switch (*spec++) {
cbaef7d1
AD
1464 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1465 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1466 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1467 case 'd': type = AVMEDIA_TYPE_DATA; break;
3ccd1580 1468 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
7cf78b34 1469 default: av_assert0(0);
d4863fc1
AK
1470 }
1471 if (type != st->codec->codec_type)
1472 return 0;
cbaef7d1 1473 if (*spec++ == ':') { /* possibly followed by :index */
d4863fc1
AK
1474 int i, index = strtol(spec, NULL, 0);
1475 for (i = 0; i < s->nb_streams; i++)
1476 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1477 return i == st->index;
1478 return 0;
1479 }
1480 return 1;
2c474ddb
AK
1481 } else if (*spec == 'p' && *(spec + 1) == ':') {
1482 int prog_id, i, j;
1483 char *endptr;
1484 spec += 2;
1485 prog_id = strtol(spec, &endptr, 0);
1486 for (i = 0; i < s->nb_programs; i++) {
1487 if (s->programs[i]->id != prog_id)
1488 continue;
1489
1490 if (*endptr++ == ':') {
1491 int stream_idx = strtol(endptr, NULL, 0);
d4b63054
DB
1492 return stream_idx >= 0 &&
1493 stream_idx < s->programs[i]->nb_stream_indexes &&
1494 st->index == s->programs[i]->stream_index[stream_idx];
2c474ddb
AK
1495 }
1496
1497 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1498 if (st->index == s->programs[i]->stream_index[j])
1499 return 1;
1500 }
1501 return 0;
d4863fc1
AK
1502 } else if (!*spec) /* empty specifier, matches everything */
1503 return 1;
1504
1505 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1506 return AVERROR(EINVAL);
1507}
1508
36ef5369 1509AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
d3ed1c95 1510 AVFormatContext *s, AVStream *st, AVCodec *codec)
8c8eab8b
AK
1511{
1512 AVDictionary *ret = NULL;
1513 AVDictionaryEntry *t = NULL;
cbaef7d1
AD
1514 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1515 : AV_OPT_FLAG_DECODING_PARAM;
8c8eab8b 1516 char prefix = 0;
20c21f8b 1517 const AVClass *cc = avcodec_get_class();
8c8eab8b
AK
1518
1519 if (!codec)
d3ed1c95
MS
1520 codec = s->oformat ? avcodec_find_encoder(codec_id)
1521 : avcodec_find_decoder(codec_id);
8c8eab8b 1522
cb6f8245 1523 switch (st->codec->codec_type) {
cbaef7d1
AD
1524 case AVMEDIA_TYPE_VIDEO:
1525 prefix = 'v';
1526 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1527 break;
1528 case AVMEDIA_TYPE_AUDIO:
1529 prefix = 'a';
1530 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1531 break;
1532 case AVMEDIA_TYPE_SUBTITLE:
1533 prefix = 's';
1534 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1535 break;
8c8eab8b
AK
1536 }
1537
1538 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
d4863fc1
AK
1539 char *p = strchr(t->key, ':');
1540
1541 /* check stream specification in opt name */
1542 if (p)
1543 switch (check_stream_specifier(s, st, p + 1)) {
1544 case 1: *p = 0; break;
1545 case 0: continue;
1546 default: return NULL;
1547 }
1548
20c21f8b 1549 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
cbaef7d1
AD
1550 (codec && codec->priv_class &&
1551 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1552 AV_OPT_SEARCH_FAKE_OBJ)))
8c8eab8b 1553 av_dict_set(&ret, t->key, t->value, 0);
cbaef7d1
AD
1554 else if (t->key[0] == prefix &&
1555 av_opt_find(&cc, t->key + 1, NULL, flags,
1556 AV_OPT_SEARCH_FAKE_OBJ))
1557 av_dict_set(&ret, t->key + 1, t->value, 0);
d4863fc1
AK
1558
1559 if (p)
1560 *p = ':';
8c8eab8b
AK
1561 }
1562 return ret;
1563}
1564
cbaef7d1
AD
1565AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1566 AVDictionary *codec_opts)
3009f521
AK
1567{
1568 int i;
1569 AVDictionary **opts;
1570
1571 if (!s->nb_streams)
1572 return NULL;
1573 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1574 if (!opts) {
cbaef7d1
AD
1575 av_log(NULL, AV_LOG_ERROR,
1576 "Could not alloc memory for stream options.\n");
3009f521
AK
1577 return NULL;
1578 }
1579 for (i = 0; i < s->nb_streams; i++)
cbaef7d1 1580 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
d3ed1c95 1581 s, s->streams[i], NULL);
3009f521
AK
1582 return opts;
1583}
1584
cac651c8
AK
1585void *grow_array(void *array, int elem_size, int *size, int new_size)
1586{
1587 if (new_size >= INT_MAX / elem_size) {
1588 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
5e3f9979 1589 exit(1);
cac651c8
AK
1590 }
1591 if (*size < new_size) {
1592 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1593 if (!tmp) {
1594 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
5e3f9979 1595 exit(1);
cac651c8
AK
1596 }
1597 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1598 *size = new_size;
1599 return tmp;
1600 }
1601 return array;
1602}
e19e8aee
AK
1603
1604const char *media_type_string(enum AVMediaType media_type)
1605{
1606 switch (media_type) {
1607 case AVMEDIA_TYPE_VIDEO: return "video";
1608 case AVMEDIA_TYPE_AUDIO: return "audio";
1609 case AVMEDIA_TYPE_DATA: return "data";
1610 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
1611 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
1612 default: return "unknown";
1613 }
1614}