aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / avtools / cmdutils.c
blobcf8a4c8c1c664e3c2b2b4a0b209dd86efd5c0e4a
1 /*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <string.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <math.h>
28 /* Include only the enabled headers since some compilers (namely, Sun
29 Studio) will not omit unused inline functions and create undefined
30 references to libraries that are not being built. */
32 #include "config.h"
33 #include "libavformat/avformat.h"
34 #include "libavfilter/avfilter.h"
35 #include "libavdevice/avdevice.h"
36 #include "libavresample/avresample.h"
37 #include "libswscale/swscale.h"
38 #include "libavutil/attributes.h"
39 #include "libavutil/avassert.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/pixdesc.h"
45 #include "libavutil/eval.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/cpu.h"
49 #include "avversion.h"
50 #include "cmdutils.h"
51 #if CONFIG_NETWORK
52 #include "libavformat/network.h"
53 #endif
54 #if HAVE_SYS_RESOURCE_H
55 #include <sys/time.h>
56 #include <sys/resource.h>
57 #endif
59 struct SwsContext *sws_opts;
60 AVDictionary *format_opts, *codec_opts, *resample_opts;
62 static const int this_year = 2018;
64 void init_opts(void)
66 #if CONFIG_SWSCALE
67 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
68 NULL, NULL, NULL);
69 #endif
72 void uninit_opts(void)
74 #if CONFIG_SWSCALE
75 sws_freeContext(sws_opts);
76 sws_opts = NULL;
77 #endif
78 av_dict_free(&format_opts);
79 av_dict_free(&codec_opts);
80 av_dict_free(&resample_opts);
83 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
85 vfprintf(stdout, fmt, vl);
88 static void (*program_exit)(int ret);
90 void register_exit(void (*cb)(int ret))
92 program_exit = cb;
95 void exit_program(int ret)
97 if (program_exit)
98 program_exit(ret);
100 exit(ret);
103 double parse_number_or_die(const char *context, const char *numstr, int type,
104 double min, double max)
106 char *tail;
107 const char *error;
108 double d = av_strtod(numstr, &tail);
109 if (*tail)
110 error = "Expected number for %s but found: %s\n";
111 else if (d < min || d > max)
112 error = "The value for %s was %s which is not within %f - %f\n";
113 else if (type == OPT_INT64 && (int64_t)d != d)
114 error = "Expected int64 for %s but found %s\n";
115 else if (type == OPT_INT && (int)d != d)
116 error = "Expected int for %s but found %s\n";
117 else
118 return d;
119 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
120 exit_program(1);
121 return 0;
124 int64_t parse_time_or_die(const char *context, const char *timestr,
125 int is_duration)
127 int64_t us;
128 if (av_parse_time(&us, timestr, is_duration) < 0) {
129 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
130 is_duration ? "duration" : "date", context, timestr);
131 exit_program(1);
133 return us;
136 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
137 int rej_flags, int alt_flags)
139 const OptionDef *po;
140 int first;
142 first = 1;
143 for (po = options; po->name != NULL; po++) {
144 char buf[64];
146 if (((po->flags & req_flags) != req_flags) ||
147 (alt_flags && !(po->flags & alt_flags)) ||
148 (po->flags & rej_flags))
149 continue;
151 if (first) {
152 printf("%s\n", msg);
153 first = 0;
155 av_strlcpy(buf, po->name, sizeof(buf));
156 if (po->argname) {
157 av_strlcat(buf, " ", sizeof(buf));
158 av_strlcat(buf, po->argname, sizeof(buf));
160 printf("-%-17s %s\n", buf, po->help);
162 printf("\n");
165 void show_help_children(const AVClass *class, int flags)
167 const AVClass *child = NULL;
168 av_opt_show2(&class, NULL, flags, 0);
169 printf("\n");
171 while (child = av_opt_child_class_next(class, child))
172 show_help_children(child, flags);
175 static const OptionDef *find_option(const OptionDef *po, const char *name)
177 const char *p = strchr(name, ':');
178 int len = p ? p - name : strlen(name);
180 while (po->name) {
181 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
182 break;
183 po++;
185 return po;
188 /* _WIN32 means using the windows libc - cygwin doesn't define that
189 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
190 * it doesn't provide the actual command line via GetCommandLineW(). */
191 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
192 #include <windows.h>
193 #include <shellapi.h>
194 /* Will be leaked on exit */
195 static char** win32_argv_utf8 = NULL;
196 static int win32_argc = 0;
199 * Prepare command line arguments for executable.
200 * For Windows - perform wide-char to UTF-8 conversion.
201 * Input arguments should be main() function arguments.
202 * @param argc_ptr Arguments number (including executable)
203 * @param argv_ptr Arguments list.
205 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
207 char *argstr_flat;
208 wchar_t **argv_w;
209 int i, buffsize = 0, offset = 0;
211 if (win32_argv_utf8) {
212 *argc_ptr = win32_argc;
213 *argv_ptr = win32_argv_utf8;
214 return;
217 win32_argc = 0;
218 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
219 if (win32_argc <= 0 || !argv_w)
220 return;
222 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
223 for (i = 0; i < win32_argc; i++)
224 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
225 NULL, 0, NULL, NULL);
227 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
228 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
229 if (!win32_argv_utf8) {
230 LocalFree(argv_w);
231 return;
234 for (i = 0; i < win32_argc; i++) {
235 win32_argv_utf8[i] = &argstr_flat[offset];
236 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
237 &argstr_flat[offset],
238 buffsize - offset, NULL, NULL);
240 win32_argv_utf8[i] = NULL;
241 LocalFree(argv_w);
243 *argc_ptr = win32_argc;
244 *argv_ptr = win32_argv_utf8;
246 #else
247 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
249 /* nothing to do */
251 #endif /* HAVE_COMMANDLINETOARGVW */
253 static int write_option(void *optctx, const OptionDef *po, const char *opt,
254 const char *arg)
256 /* new-style options contain an offset into optctx, old-style address of
257 * a global var*/
258 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
259 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
260 int *dstcount;
262 if (po->flags & OPT_SPEC) {
263 SpecifierOpt **so = dst;
264 char *p = strchr(opt, ':');
265 char *str;
267 dstcount = (int *)(so + 1);
268 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
269 str = av_strdup(p ? p + 1 : "");
270 if (!str)
271 return AVERROR(ENOMEM);
272 (*so)[*dstcount - 1].specifier = str;
273 dst = &(*so)[*dstcount - 1].u;
276 if (po->flags & OPT_STRING) {
277 char *str;
278 str = av_strdup(arg);
279 av_freep(dst);
280 if (!str)
281 return AVERROR(ENOMEM);
282 *(char **)dst = str;
283 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
284 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
285 } else if (po->flags & OPT_INT64) {
286 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
287 } else if (po->flags & OPT_TIME) {
288 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
289 } else if (po->flags & OPT_FLOAT) {
290 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
291 } else if (po->flags & OPT_DOUBLE) {
292 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
293 } else if (po->u.func_arg) {
294 int ret = po->u.func_arg(optctx, opt, arg);
295 if (ret < 0) {
296 av_log(NULL, AV_LOG_ERROR,
297 "Failed to set value '%s' for option '%s'\n", arg, opt);
298 return ret;
301 if (po->flags & OPT_EXIT)
302 exit_program(0);
304 return 0;
307 int parse_option(void *optctx, const char *opt, const char *arg,
308 const OptionDef *options)
310 const OptionDef *po;
311 int ret;
313 po = find_option(options, opt);
314 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
315 /* handle 'no' bool option */
316 po = find_option(options, opt + 2);
317 if ((po->name && (po->flags & OPT_BOOL)))
318 arg = "0";
319 } else if (po->flags & OPT_BOOL)
320 arg = "1";
322 if (!po->name)
323 po = find_option(options, "default");
324 if (!po->name) {
325 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
326 return AVERROR(EINVAL);
328 if (po->flags & HAS_ARG && !arg) {
329 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
330 return AVERROR(EINVAL);
333 ret = write_option(optctx, po, opt, arg);
334 if (ret < 0)
335 return ret;
337 return !!(po->flags & HAS_ARG);
340 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
341 void (*parse_arg_function)(void *, const char*))
343 const char *opt;
344 int optindex, handleoptions = 1, ret;
346 /* perform system-dependent conversions for arguments list */
347 prepare_app_arguments(&argc, &argv);
349 /* parse options */
350 optindex = 1;
351 while (optindex < argc) {
352 opt = argv[optindex++];
354 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
355 if (opt[1] == '-' && opt[2] == '\0') {
356 handleoptions = 0;
357 continue;
359 opt++;
361 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
362 exit_program(1);
363 optindex += ret;
364 } else {
365 if (parse_arg_function)
366 parse_arg_function(optctx, opt);
371 int parse_optgroup(void *optctx, OptionGroup *g)
373 int i, ret;
375 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
376 g->group_def->name, g->arg);
378 for (i = 0; i < g->nb_opts; i++) {
379 Option *o = &g->opts[i];
381 if (g->group_def->flags &&
382 !(g->group_def->flags & o->opt->flags)) {
383 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
384 "%s %s -- you are trying to apply an input option to an "
385 "output file or vice versa. Move this option before the "
386 "file it belongs to.\n", o->key, o->opt->help,
387 g->group_def->name, g->arg);
388 return AVERROR(EINVAL);
391 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
392 o->key, o->opt->help, o->val);
394 ret = write_option(optctx, o->opt, o->key, o->val);
395 if (ret < 0)
396 return ret;
399 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
401 return 0;
404 int locate_option(int argc, char **argv, const OptionDef *options,
405 const char *optname)
407 const OptionDef *po;
408 int i;
410 for (i = 1; i < argc; i++) {
411 const char *cur_opt = argv[i];
413 if (*cur_opt++ != '-')
414 continue;
416 po = find_option(options, cur_opt);
417 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
418 po = find_option(options, cur_opt + 2);
420 if ((!po->name && !strcmp(cur_opt, optname)) ||
421 (po->name && !strcmp(optname, po->name)))
422 return i;
424 if (!po->name || po->flags & HAS_ARG)
425 i++;
427 return 0;
430 void parse_loglevel(int argc, char **argv, const OptionDef *options)
432 int idx = locate_option(argc, argv, options, "loglevel");
433 if (!idx)
434 idx = locate_option(argc, argv, options, "v");
435 if (idx && argv[idx + 1])
436 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
439 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
440 int opt_default(void *optctx, const char *opt, const char *arg)
442 const AVOption *o;
443 char opt_stripped[128];
444 const char *p;
445 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
446 #if CONFIG_AVRESAMPLE
447 const AVClass *rc = avresample_get_class();
448 #endif
449 #if CONFIG_SWSCALE
450 const AVClass *sc = sws_get_class();
451 #endif
453 if (!(p = strchr(opt, ':')))
454 p = opt + strlen(opt);
455 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
457 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
458 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
459 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
460 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
461 av_dict_set(&codec_opts, opt, arg, FLAGS);
462 else if ((o = av_opt_find(&fc, opt, NULL, 0,
463 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
464 av_dict_set(&format_opts, opt, arg, FLAGS);
465 #if CONFIG_AVRESAMPLE
466 else if ((o = av_opt_find(&rc, opt, NULL, 0,
467 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
468 av_dict_set(&resample_opts, opt, arg, FLAGS);
469 #endif
470 #if CONFIG_SWSCALE
471 else if ((o = av_opt_find(&sc, opt, NULL, 0,
472 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
473 // XXX we only support sws_flags, not arbitrary sws options
474 int ret = av_opt_set(sws_opts, opt, arg, 0);
475 if (ret < 0) {
476 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
477 return ret;
480 #endif
482 if (o)
483 return 0;
484 return AVERROR_OPTION_NOT_FOUND;
488 * Check whether given option is a group separator.
490 * @return index of the group definition that matched or -1 if none
492 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
493 const char *opt)
495 int i;
497 for (i = 0; i < nb_groups; i++) {
498 const OptionGroupDef *p = &groups[i];
499 if (p->sep && !strcmp(p->sep, opt))
500 return i;
503 return -1;
507 * Finish parsing an option group.
509 * @param group_idx which group definition should this group belong to
510 * @param arg argument of the group delimiting option
512 static void finish_group(OptionParseContext *octx, int group_idx,
513 const char *arg)
515 OptionGroupList *l = &octx->groups[group_idx];
516 OptionGroup *g;
518 GROW_ARRAY(l->groups, l->nb_groups);
519 g = &l->groups[l->nb_groups - 1];
521 *g = octx->cur_group;
522 g->arg = arg;
523 g->group_def = l->group_def;
524 #if CONFIG_SWSCALE
525 g->sws_opts = sws_opts;
526 #endif
527 g->codec_opts = codec_opts;
528 g->format_opts = format_opts;
529 g->resample_opts = resample_opts;
531 codec_opts = NULL;
532 format_opts = NULL;
533 resample_opts = NULL;
534 #if CONFIG_SWSCALE
535 sws_opts = NULL;
536 #endif
537 init_opts();
539 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
543 * Add an option instance to currently parsed group.
545 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
546 const char *key, const char *val)
548 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
549 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
551 GROW_ARRAY(g->opts, g->nb_opts);
552 g->opts[g->nb_opts - 1].opt = opt;
553 g->opts[g->nb_opts - 1].key = key;
554 g->opts[g->nb_opts - 1].val = val;
557 static void init_parse_context(OptionParseContext *octx,
558 const OptionGroupDef *groups, int nb_groups)
560 static const OptionGroupDef global_group = { "global" };
561 int i;
563 memset(octx, 0, sizeof(*octx));
565 octx->nb_groups = nb_groups;
566 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
567 if (!octx->groups)
568 exit_program(1);
570 for (i = 0; i < octx->nb_groups; i++)
571 octx->groups[i].group_def = &groups[i];
573 octx->global_opts.group_def = &global_group;
574 octx->global_opts.arg = "";
576 init_opts();
579 void uninit_parse_context(OptionParseContext *octx)
581 int i, j;
583 for (i = 0; i < octx->nb_groups; i++) {
584 OptionGroupList *l = &octx->groups[i];
586 for (j = 0; j < l->nb_groups; j++) {
587 av_freep(&l->groups[j].opts);
588 av_dict_free(&l->groups[j].codec_opts);
589 av_dict_free(&l->groups[j].format_opts);
590 av_dict_free(&l->groups[j].resample_opts);
591 #if CONFIG_SWSCALE
592 sws_freeContext(l->groups[j].sws_opts);
593 #endif
595 av_freep(&l->groups);
597 av_freep(&octx->groups);
599 av_freep(&octx->cur_group.opts);
600 av_freep(&octx->global_opts.opts);
602 uninit_opts();
605 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
606 const OptionDef *options,
607 const OptionGroupDef *groups, int nb_groups)
609 int optindex = 1;
611 /* perform system-dependent conversions for arguments list */
612 prepare_app_arguments(&argc, &argv);
614 init_parse_context(octx, groups, nb_groups);
615 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
617 while (optindex < argc) {
618 const char *opt = argv[optindex++], *arg;
619 const OptionDef *po;
620 int ret;
622 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
624 /* unnamed group separators, e.g. output filename */
625 if (opt[0] != '-' || !opt[1]) {
626 finish_group(octx, 0, opt);
627 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
628 continue;
630 opt++;
632 #define GET_ARG(arg) \
633 do { \
634 arg = argv[optindex++]; \
635 if (!arg) { \
636 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
637 return AVERROR(EINVAL); \
639 } while (0)
641 /* named group separators, e.g. -i */
642 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
643 GET_ARG(arg);
644 finish_group(octx, ret, arg);
645 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
646 groups[ret].name, arg);
647 continue;
650 /* normal options */
651 po = find_option(options, opt);
652 if (po->name) {
653 if (po->flags & OPT_EXIT) {
654 /* optional argument, e.g. -h */
655 arg = argv[optindex++];
656 } else if (po->flags & HAS_ARG) {
657 GET_ARG(arg);
658 } else {
659 arg = "1";
662 add_opt(octx, po, opt, arg);
663 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
664 "argument '%s'.\n", po->name, po->help, arg);
665 continue;
668 /* AVOptions */
669 if (argv[optindex]) {
670 ret = opt_default(NULL, opt, argv[optindex]);
671 if (ret >= 0) {
672 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
673 "argument '%s'.\n", opt, argv[optindex]);
674 optindex++;
675 continue;
676 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
677 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
678 "with argument '%s'.\n", opt, argv[optindex]);
679 return ret;
683 /* boolean -nofoo options */
684 if (opt[0] == 'n' && opt[1] == 'o' &&
685 (po = find_option(options, opt + 2)) &&
686 po->name && po->flags & OPT_BOOL) {
687 add_opt(octx, po, opt, "0");
688 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
689 "argument 0.\n", po->name, po->help);
690 continue;
693 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
694 return AVERROR_OPTION_NOT_FOUND;
697 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
698 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
699 "commandline.\n");
701 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
703 return 0;
706 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
708 int flags = av_parse_cpu_flags(arg);
710 if (flags < 0)
711 return flags;
713 av_set_cpu_flags_mask(flags);
714 return 0;
717 int opt_loglevel(void *optctx, const char *opt, const char *arg)
719 const struct { const char *name; int level; } log_levels[] = {
720 { "quiet" , AV_LOG_QUIET },
721 { "panic" , AV_LOG_PANIC },
722 { "fatal" , AV_LOG_FATAL },
723 { "error" , AV_LOG_ERROR },
724 { "warning", AV_LOG_WARNING },
725 { "info" , AV_LOG_INFO },
726 { "verbose", AV_LOG_VERBOSE },
727 { "debug" , AV_LOG_DEBUG },
728 { "trace" , AV_LOG_TRACE },
730 char *tail;
731 int level;
732 int i;
734 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
735 if (!strcmp(log_levels[i].name, arg)) {
736 av_log_set_level(log_levels[i].level);
737 return 0;
741 level = strtol(arg, &tail, 10);
742 if (*tail) {
743 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
744 "Possible levels are numbers or:\n", arg);
745 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
746 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
747 exit_program(1);
749 av_log_set_level(level);
750 return 0;
753 int opt_timelimit(void *optctx, const char *opt, const char *arg)
755 #if HAVE_SETRLIMIT
756 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
757 struct rlimit rl = { lim, lim + 1 };
758 if (setrlimit(RLIMIT_CPU, &rl))
759 perror("setrlimit");
760 #else
761 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
762 #endif
763 return 0;
766 void print_error(const char *filename, int err)
768 char errbuf[128];
769 const char *errbuf_ptr = errbuf;
771 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
772 errbuf_ptr = strerror(AVUNERROR(err));
773 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
776 static int warned_cfg = 0;
778 #define INDENT 1
779 #define SHOW_VERSION 2
780 #define SHOW_CONFIG 4
782 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
783 if (CONFIG_##LIBNAME) { \
784 const char *indent = flags & INDENT? " " : ""; \
785 if (flags & SHOW_VERSION) { \
786 unsigned int version = libname##_version(); \
787 av_log(NULL, level, \
788 "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
789 indent, #libname, \
790 LIB##LIBNAME##_VERSION_MAJOR, \
791 LIB##LIBNAME##_VERSION_MINOR, \
792 LIB##LIBNAME##_VERSION_MICRO, \
793 version >> 16, version >> 8 & 0xff, version & 0xff); \
795 if (flags & SHOW_CONFIG) { \
796 const char *cfg = libname##_configuration(); \
797 if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
798 if (!warned_cfg) { \
799 av_log(NULL, level, \
800 "%sWARNING: library configuration mismatch\n", \
801 indent); \
802 warned_cfg = 1; \
804 av_log(NULL, level, "%s%-11s configuration: %s\n", \
805 indent, #libname, cfg); \
810 static void print_all_libs_info(int flags, int level)
812 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
813 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
814 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
815 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
816 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
817 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
818 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
821 void show_banner(void)
823 av_log(NULL, AV_LOG_INFO,
824 "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
825 program_name, program_birth_year, this_year);
826 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
827 __DATE__, __TIME__, CC_IDENT);
828 av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
829 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
830 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
833 int show_version(void *optctx, const char *opt, const char *arg)
835 av_log_set_callback(log_callback_help);
836 printf("%s " LIBAV_VERSION "\n", program_name);
837 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
839 return 0;
842 int show_license(void *optctx, const char *opt, const char *arg)
844 printf(
845 #if CONFIG_NONFREE
846 "This version of %s has nonfree parts compiled in.\n"
847 "Therefore it is not legally redistributable.\n",
848 program_name
849 #elif CONFIG_GPLV3
850 "%s is free software; you can redistribute it and/or modify\n"
851 "it under the terms of the GNU General Public License as published by\n"
852 "the Free Software Foundation; either version 3 of the License, or\n"
853 "(at your option) any later version.\n"
854 "\n"
855 "%s is distributed in the hope that it will be useful,\n"
856 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
857 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
858 "GNU General Public License for more details.\n"
859 "\n"
860 "You should have received a copy of the GNU General Public License\n"
861 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
862 program_name, program_name, program_name
863 #elif CONFIG_GPL
864 "%s is free software; you can redistribute it and/or modify\n"
865 "it under the terms of the GNU General Public License as published by\n"
866 "the Free Software Foundation; either version 2 of the License, or\n"
867 "(at your option) any later version.\n"
868 "\n"
869 "%s is distributed in the hope that it will be useful,\n"
870 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
871 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
872 "GNU General Public License for more details.\n"
873 "\n"
874 "You should have received a copy of the GNU General Public License\n"
875 "along with %s; if not, write to the Free Software\n"
876 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
877 program_name, program_name, program_name
878 #elif CONFIG_LGPLV3
879 "%s is free software; you can redistribute it and/or modify\n"
880 "it under the terms of the GNU Lesser General Public License as published by\n"
881 "the Free Software Foundation; either version 3 of the License, or\n"
882 "(at your option) any later version.\n"
883 "\n"
884 "%s is distributed in the hope that it will be useful,\n"
885 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
886 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
887 "GNU Lesser General Public License for more details.\n"
888 "\n"
889 "You should have received a copy of the GNU Lesser General Public License\n"
890 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
891 program_name, program_name, program_name
892 #else
893 "%s is free software; you can redistribute it and/or\n"
894 "modify it under the terms of the GNU Lesser General Public\n"
895 "License as published by the Free Software Foundation; either\n"
896 "version 2.1 of the License, or (at your option) any later version.\n"
897 "\n"
898 "%s is distributed in the hope that it will be useful,\n"
899 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
900 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
901 "Lesser General Public License for more details.\n"
902 "\n"
903 "You should have received a copy of the GNU Lesser General Public\n"
904 "License along with %s; if not, write to the Free Software\n"
905 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
906 program_name, program_name, program_name
907 #endif
910 return 0;
913 int show_formats(void *optctx, const char *opt, const char *arg)
915 AVInputFormat *ifmt = NULL;
916 AVOutputFormat *ofmt = NULL;
917 const char *last_name;
919 printf("File formats:\n"
920 " D. = Demuxing supported\n"
921 " .E = Muxing supported\n"
922 " --\n");
923 last_name = "000";
924 for (;;) {
925 int decode = 0;
926 int encode = 0;
927 const char *name = NULL;
928 const char *long_name = NULL;
930 while ((ofmt = av_oformat_next(ofmt))) {
931 if ((!name || strcmp(ofmt->name, name) < 0) &&
932 strcmp(ofmt->name, last_name) > 0) {
933 name = ofmt->name;
934 long_name = ofmt->long_name;
935 encode = 1;
938 while ((ifmt = av_iformat_next(ifmt))) {
939 if ((!name || strcmp(ifmt->name, name) < 0) &&
940 strcmp(ifmt->name, last_name) > 0) {
941 name = ifmt->name;
942 long_name = ifmt->long_name;
943 encode = 0;
945 if (name && strcmp(ifmt->name, name) == 0)
946 decode = 1;
948 if (!name)
949 break;
950 last_name = name;
952 printf(" %s%s %-15s %s\n",
953 decode ? "D" : " ",
954 encode ? "E" : " ",
955 name,
956 long_name ? long_name:" ");
958 return 0;
961 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
962 if (codec->field) { \
963 const type *p = c->field; \
965 printf(" Supported " list_name ":"); \
966 while (*p != term) { \
967 get_name(*p); \
968 printf(" %s", name); \
969 p++; \
971 printf("\n"); \
974 static void print_codec(const AVCodec *c)
976 int encoder = av_codec_is_encoder(c);
978 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
979 c->long_name ? c->long_name : "");
981 printf(" General capabilities: ");
982 if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
983 printf("horizband ");
984 if (c->capabilities & AV_CODEC_CAP_DR1)
985 printf("dr1 ");
986 if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
987 printf("trunc ");
988 if (c->capabilities & AV_CODEC_CAP_DELAY)
989 printf("delay ");
990 if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
991 printf("small ");
992 if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
993 printf("subframes ");
994 if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
995 printf("exp ");
996 if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
997 printf("chconf ");
998 if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
999 printf("paramchange ");
1000 if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
1001 printf("variable ");
1002 if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1003 AV_CODEC_CAP_SLICE_THREADS |
1004 AV_CODEC_CAP_AUTO_THREADS))
1005 printf("threads ");
1006 if (!c->capabilities)
1007 printf("none");
1008 printf("\n");
1010 if (c->type == AVMEDIA_TYPE_VIDEO) {
1011 printf(" Threading capabilities: ");
1012 switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1013 AV_CODEC_CAP_SLICE_THREADS |
1014 AV_CODEC_CAP_AUTO_THREADS)) {
1015 case AV_CODEC_CAP_FRAME_THREADS |
1016 AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1017 case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1018 case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1019 case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
1020 default: printf("none"); break;
1022 printf("\n");
1025 if (c->supported_framerates) {
1026 const AVRational *fps = c->supported_framerates;
1028 printf(" Supported framerates:");
1029 while (fps->num) {
1030 printf(" %d/%d", fps->num, fps->den);
1031 fps++;
1033 printf("\n");
1035 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1036 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1037 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1038 GET_SAMPLE_RATE_NAME);
1039 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1040 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1041 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1042 0, GET_CH_LAYOUT_DESC);
1044 if (c->priv_class) {
1045 show_help_children(c->priv_class,
1046 AV_OPT_FLAG_ENCODING_PARAM |
1047 AV_OPT_FLAG_DECODING_PARAM);
1051 static char get_media_type_char(enum AVMediaType type)
1053 switch (type) {
1054 case AVMEDIA_TYPE_VIDEO: return 'V';
1055 case AVMEDIA_TYPE_AUDIO: return 'A';
1056 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1057 default: return '?';
1061 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1062 int encoder)
1064 while ((prev = av_codec_next(prev))) {
1065 if (prev->id == id &&
1066 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1067 return prev;
1069 return NULL;
1072 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1074 const AVCodec *codec = NULL;
1076 printf(" (%s: ", encoder ? "encoders" : "decoders");
1078 while ((codec = next_codec_for_id(id, codec, encoder)))
1079 printf("%s ", codec->name);
1081 printf(")");
1084 int show_codecs(void *optctx, const char *opt, const char *arg)
1086 const AVCodecDescriptor *desc = NULL;
1088 printf("Codecs:\n"
1089 " D..... = Decoding supported\n"
1090 " .E.... = Encoding supported\n"
1091 " ..V... = Video codec\n"
1092 " ..A... = Audio codec\n"
1093 " ..S... = Subtitle codec\n"
1094 " ...I.. = Intra frame-only codec\n"
1095 " ....L. = Lossy compression\n"
1096 " .....S = Lossless compression\n"
1097 " -------\n");
1098 while ((desc = avcodec_descriptor_next(desc))) {
1099 const AVCodec *codec = NULL;
1101 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1102 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1104 printf("%c", get_media_type_char(desc->type));
1105 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1106 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1107 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1109 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1111 /* print decoders/encoders when there's more than one or their
1112 * names are different from codec name */
1113 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1114 if (strcmp(codec->name, desc->name)) {
1115 print_codecs_for_id(desc->id, 0);
1116 break;
1119 codec = NULL;
1120 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1121 if (strcmp(codec->name, desc->name)) {
1122 print_codecs_for_id(desc->id, 1);
1123 break;
1127 printf("\n");
1129 return 0;
1132 static void print_codecs(int encoder)
1134 const AVCodecDescriptor *desc = NULL;
1136 printf("%s:\n"
1137 " V... = Video\n"
1138 " A... = Audio\n"
1139 " S... = Subtitle\n"
1140 " .F.. = Frame-level multithreading\n"
1141 " ..S. = Slice-level multithreading\n"
1142 " ...X = Codec is experimental\n"
1143 " ---\n",
1144 encoder ? "Encoders" : "Decoders");
1145 while ((desc = avcodec_descriptor_next(desc))) {
1146 const AVCodec *codec = NULL;
1148 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1149 printf("%c", get_media_type_char(desc->type));
1150 printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1151 printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1152 printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1154 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1155 if (strcmp(codec->name, desc->name))
1156 printf(" (codec %s)", desc->name);
1158 printf("\n");
1163 int show_decoders(void *optctx, const char *opt, const char *arg)
1165 print_codecs(0);
1166 return 0;
1169 int show_encoders(void *optctx, const char *opt, const char *arg)
1171 print_codecs(1);
1172 return 0;
1175 int show_bsfs(void *optctx, const char *opt, const char *arg)
1177 const AVBitStreamFilter *bsf = NULL;
1178 void *opaque = NULL;
1180 printf("Bitstream filters:\n");
1181 while ((bsf = av_bsf_next(&opaque)))
1182 printf("%s\n", bsf->name);
1183 printf("\n");
1184 return 0;
1187 int show_protocols(void *optctx, const char *opt, const char *arg)
1189 void *opaque = NULL;
1190 const char *name;
1192 printf("Supported file protocols:\n"
1193 "Input:\n");
1194 while ((name = avio_enum_protocols(&opaque, 0)))
1195 printf("%s\n", name);
1196 printf("Output:\n");
1197 while ((name = avio_enum_protocols(&opaque, 1)))
1198 printf("%s\n", name);
1199 return 0;
1202 int show_filters(void *optctx, const char *opt, const char *arg)
1204 #if CONFIG_AVFILTER
1205 const AVFilter *filter = NULL;
1207 printf("Filters:\n");
1208 while ((filter = avfilter_next(filter)))
1209 printf("%-16s %s\n", filter->name, filter->description);
1210 #else
1211 printf("No filters available: libavfilter disabled\n");
1212 #endif
1213 return 0;
1216 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1218 const AVPixFmtDescriptor *pix_desc = NULL;
1220 printf("Pixel formats:\n"
1221 "I.... = Supported Input format for conversion\n"
1222 ".O... = Supported Output format for conversion\n"
1223 "..H.. = Hardware accelerated format\n"
1224 "...P. = Paletted format\n"
1225 "....B = Bitstream format\n"
1226 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1227 "-----\n");
1229 #if !CONFIG_SWSCALE
1230 # define sws_isSupportedInput(x) 0
1231 # define sws_isSupportedOutput(x) 0
1232 #endif
1234 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1235 enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1236 printf("%c%c%c%c%c %-16s %d %2d\n",
1237 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1238 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1239 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1240 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1241 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1242 pix_desc->name,
1243 pix_desc->nb_components,
1244 av_get_bits_per_pixel(pix_desc));
1246 return 0;
1249 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1251 int i;
1252 char fmt_str[128];
1253 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1254 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1255 return 0;
1258 static void show_help_codec(const char *name, int encoder)
1260 const AVCodecDescriptor *desc;
1261 const AVCodec *codec;
1263 if (!name) {
1264 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1265 return;
1268 codec = encoder ? avcodec_find_encoder_by_name(name) :
1269 avcodec_find_decoder_by_name(name);
1271 if (codec)
1272 print_codec(codec);
1273 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1274 int printed = 0;
1276 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1277 printed = 1;
1278 print_codec(codec);
1281 if (!printed) {
1282 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
1283 "but no %s for it are available. Libav might need to be "
1284 "recompiled with additional external libraries.\n",
1285 name, encoder ? "encoders" : "decoders");
1287 } else {
1288 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
1289 name);
1293 static void show_help_demuxer(const char *name)
1295 const AVInputFormat *fmt = av_find_input_format(name);
1297 if (!fmt) {
1298 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1299 return;
1302 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1304 if (fmt->extensions)
1305 printf(" Common extensions: %s.\n", fmt->extensions);
1307 if (fmt->priv_class)
1308 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1311 static void show_help_muxer(const char *name)
1313 const AVCodecDescriptor *desc;
1314 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1316 if (!fmt) {
1317 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1318 return;
1321 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1323 if (fmt->extensions)
1324 printf(" Common extensions: %s.\n", fmt->extensions);
1325 if (fmt->mime_type)
1326 printf(" Mime type: %s.\n", fmt->mime_type);
1327 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1328 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1329 printf(" Default video codec: %s.\n", desc->name);
1331 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1332 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1333 printf(" Default audio codec: %s.\n", desc->name);
1335 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1336 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1337 printf(" Default subtitle codec: %s.\n", desc->name);
1340 if (fmt->priv_class)
1341 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1344 #if CONFIG_AVFILTER
1345 static void show_help_filter(const char *name)
1347 const AVFilter *f = avfilter_get_by_name(name);
1348 int i, count;
1350 if (!name) {
1351 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1352 return;
1353 } else if (!f) {
1354 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1355 return;
1358 printf("Filter %s [%s]:\n", f->name, f->description);
1360 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1361 printf(" slice threading supported\n");
1363 printf(" Inputs:\n");
1364 count = avfilter_pad_count(f->inputs);
1365 for (i = 0; i < count; i++) {
1366 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1367 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1369 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1370 printf(" dynamic (depending on the options)\n");
1372 printf(" Outputs:\n");
1373 count = avfilter_pad_count(f->outputs);
1374 for (i = 0; i < count; i++) {
1375 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1376 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1378 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1379 printf(" dynamic (depending on the options)\n");
1381 if (f->priv_class)
1382 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
1383 AV_OPT_FLAG_AUDIO_PARAM);
1385 #endif
1387 int show_help(void *optctx, const char *opt, const char *arg)
1389 char *topic, *par;
1390 av_log_set_callback(log_callback_help);
1392 topic = av_strdup(arg ? arg : "");
1393 if (!topic)
1394 return AVERROR(ENOMEM);
1395 par = strchr(topic, '=');
1396 if (par)
1397 *par++ = 0;
1399 if (!*topic) {
1400 show_help_default(topic, par);
1401 } else if (!strcmp(topic, "decoder")) {
1402 show_help_codec(par, 0);
1403 } else if (!strcmp(topic, "encoder")) {
1404 show_help_codec(par, 1);
1405 } else if (!strcmp(topic, "demuxer")) {
1406 show_help_demuxer(par);
1407 } else if (!strcmp(topic, "muxer")) {
1408 show_help_muxer(par);
1409 #if CONFIG_AVFILTER
1410 } else if (!strcmp(topic, "filter")) {
1411 show_help_filter(par);
1412 #endif
1413 } else {
1414 show_help_default(topic, par);
1417 av_freep(&topic);
1418 return 0;
1421 int read_yesno(void)
1423 int c = getchar();
1424 int yesno = (av_toupper(c) == 'Y');
1426 while (c != '\n' && c != EOF)
1427 c = getchar();
1429 return yesno;
1432 void init_pts_correction(PtsCorrectionContext *ctx)
1434 ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
1435 ctx->last_pts = ctx->last_dts = INT64_MIN;
1438 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
1439 int64_t dts)
1441 int64_t pts = AV_NOPTS_VALUE;
1443 if (dts != AV_NOPTS_VALUE) {
1444 ctx->num_faulty_dts += dts <= ctx->last_dts;
1445 ctx->last_dts = dts;
1447 if (reordered_pts != AV_NOPTS_VALUE) {
1448 ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
1449 ctx->last_pts = reordered_pts;
1451 if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
1452 && reordered_pts != AV_NOPTS_VALUE)
1453 pts = reordered_pts;
1454 else
1455 pts = dts;
1457 return pts;
1460 FILE *get_preset_file(char *filename, size_t filename_size,
1461 const char *preset_name, int is_path,
1462 const char *codec_name)
1464 FILE *f = NULL;
1465 int i;
1466 const char *base[3] = { getenv("AVCONV_DATADIR"),
1467 getenv("HOME"),
1468 AVCONV_DATADIR, };
1470 if (is_path) {
1471 av_strlcpy(filename, preset_name, filename_size);
1472 f = fopen(filename, "r");
1473 } else {
1474 for (i = 0; i < 3 && !f; i++) {
1475 if (!base[i])
1476 continue;
1477 snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
1478 i != 1 ? "" : "/.avconv", preset_name);
1479 f = fopen(filename, "r");
1480 if (!f && codec_name) {
1481 snprintf(filename, filename_size,
1482 "%s%s/%s-%s.avpreset",
1483 base[i], i != 1 ? "" : "/.avconv", codec_name,
1484 preset_name);
1485 f = fopen(filename, "r");
1490 return f;
1493 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1495 if (*spec <= '9' && *spec >= '0') /* opt:index */
1496 return strtol(spec, NULL, 0) == st->index;
1497 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1498 *spec == 't') { /* opt:[vasdt] */
1499 enum AVMediaType type;
1501 switch (*spec++) {
1502 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1503 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1504 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1505 case 'd': type = AVMEDIA_TYPE_DATA; break;
1506 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1507 default: av_assert0(0);
1509 if (type != st->codecpar->codec_type)
1510 return 0;
1511 if (*spec++ == ':') { /* possibly followed by :index */
1512 int i, index = strtol(spec, NULL, 0);
1513 for (i = 0; i < s->nb_streams; i++)
1514 if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
1515 return i == st->index;
1516 return 0;
1518 return 1;
1519 } else if (*spec == 'p' && *(spec + 1) == ':') {
1520 int prog_id, i, j;
1521 char *endptr;
1522 spec += 2;
1523 prog_id = strtol(spec, &endptr, 0);
1524 for (i = 0; i < s->nb_programs; i++) {
1525 if (s->programs[i]->id != prog_id)
1526 continue;
1528 if (*endptr++ == ':') {
1529 int stream_idx = strtol(endptr, NULL, 0);
1530 return stream_idx >= 0 &&
1531 stream_idx < s->programs[i]->nb_stream_indexes &&
1532 st->index == s->programs[i]->stream_index[stream_idx];
1535 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1536 if (st->index == s->programs[i]->stream_index[j])
1537 return 1;
1539 return 0;
1540 } else if (*spec == 'i' && *(spec + 1) == ':') {
1541 int stream_id;
1542 char *endptr;
1543 spec += 2;
1544 stream_id = strtol(spec, &endptr, 0);
1545 return stream_id == st->id;
1546 } else if (*spec == 'm' && *(spec + 1) == ':') {
1547 AVDictionaryEntry *tag;
1548 char *key, *val;
1549 int ret;
1551 spec += 2;
1552 val = strchr(spec, ':');
1554 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
1555 if (!key)
1556 return AVERROR(ENOMEM);
1558 tag = av_dict_get(st->metadata, key, NULL, 0);
1559 if (tag) {
1560 if (!val || !strcmp(tag->value, val + 1))
1561 ret = 1;
1562 else
1563 ret = 0;
1564 } else
1565 ret = 0;
1567 av_freep(&key);
1568 return ret;
1569 } else if (*spec == 'u') {
1570 AVCodecParameters *par = st->codecpar;
1571 int val;
1572 switch (par->codec_type) {
1573 case AVMEDIA_TYPE_AUDIO:
1574 val = par->sample_rate && par->channels;
1575 if (par->format == AV_SAMPLE_FMT_NONE)
1576 return 0;
1577 break;
1578 case AVMEDIA_TYPE_VIDEO:
1579 val = par->width && par->height;
1580 if (par->format == AV_PIX_FMT_NONE)
1581 return 0;
1582 break;
1583 case AVMEDIA_TYPE_UNKNOWN:
1584 val = 0;
1585 break;
1586 default:
1587 val = 1;
1588 break;
1590 return par->codec_id != AV_CODEC_ID_NONE && val != 0;
1591 } else if (!*spec) /* empty specifier, matches everything */
1592 return 1;
1594 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1595 return AVERROR(EINVAL);
1598 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1599 AVFormatContext *s, AVStream *st, AVCodec *codec)
1601 AVDictionary *ret = NULL;
1602 AVDictionaryEntry *t = NULL;
1603 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1604 : AV_OPT_FLAG_DECODING_PARAM;
1605 char prefix = 0;
1606 const AVClass *cc = avcodec_get_class();
1608 if (!codec)
1609 codec = s->oformat ? avcodec_find_encoder(codec_id)
1610 : avcodec_find_decoder(codec_id);
1612 switch (st->codecpar->codec_type) {
1613 case AVMEDIA_TYPE_VIDEO:
1614 prefix = 'v';
1615 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1616 break;
1617 case AVMEDIA_TYPE_AUDIO:
1618 prefix = 'a';
1619 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1620 break;
1621 case AVMEDIA_TYPE_SUBTITLE:
1622 prefix = 's';
1623 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1624 break;
1627 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1628 char *p = strchr(t->key, ':');
1630 /* check stream specification in opt name */
1631 if (p)
1632 switch (check_stream_specifier(s, st, p + 1)) {
1633 case 1: *p = 0; break;
1634 case 0: continue;
1635 default: return NULL;
1638 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1639 (codec && codec->priv_class &&
1640 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1641 AV_OPT_SEARCH_FAKE_OBJ)))
1642 av_dict_set(&ret, t->key, t->value, 0);
1643 else if (t->key[0] == prefix &&
1644 av_opt_find(&cc, t->key + 1, NULL, flags,
1645 AV_OPT_SEARCH_FAKE_OBJ))
1646 av_dict_set(&ret, t->key + 1, t->value, 0);
1648 if (p)
1649 *p = ':';
1651 return ret;
1654 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1655 AVDictionary *codec_opts)
1657 int i;
1658 AVDictionary **opts;
1660 if (!s->nb_streams)
1661 return NULL;
1662 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1663 if (!opts) {
1664 av_log(NULL, AV_LOG_ERROR,
1665 "Could not alloc memory for stream options.\n");
1666 return NULL;
1668 for (i = 0; i < s->nb_streams; i++)
1669 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
1670 s, s->streams[i], NULL);
1671 return opts;
1674 void *grow_array(void *array, int elem_size, int *size, int new_size)
1676 if (new_size >= INT_MAX / elem_size) {
1677 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1678 exit_program(1);
1680 if (*size < new_size) {
1681 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1682 if (!tmp) {
1683 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1684 exit_program(1);
1686 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1687 *size = new_size;
1688 return tmp;
1690 return array;
1693 const char *media_type_string(enum AVMediaType media_type)
1695 switch (media_type) {
1696 case AVMEDIA_TYPE_VIDEO: return "video";
1697 case AVMEDIA_TYPE_AUDIO: return "audio";
1698 case AVMEDIA_TYPE_DATA: return "data";
1699 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
1700 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
1701 default: return "unknown";