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
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. */
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/avassert.h"
39 #include "libavutil/avstring.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/eval.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/cpu.h"
48 #include "avversion.h"
51 #include "libavformat/network.h"
53 #if HAVE_SYS_RESOURCE_H
55 #include <sys/resource.h>
58 struct SwsContext *sws_opts;
59 AVDictionary *format_opts, *codec_opts, *resample_opts;
61 static const int this_year = 2015;
66 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
71 void uninit_opts(void)
74 sws_freeContext(sws_opts);
77 av_dict_free(&format_opts);
78 av_dict_free(&codec_opts);
79 av_dict_free(&resample_opts);
82 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
84 vfprintf(stdout, fmt, vl);
87 static void (*program_exit)(int ret);
89 void register_exit(void (*cb)(int ret))
94 void exit_program(int ret)
102 double parse_number_or_die(const char *context, const char *numstr, int type,
103 double min, double max)
107 double d = av_strtod(numstr, &tail);
109 error = "Expected number for %s but found: %s\n";
110 else if (d < min || d > max)
111 error = "The value for %s was %s which is not within %f - %f\n";
112 else if (type == OPT_INT64 && (int64_t)d != d)
113 error = "Expected int64 for %s but found %s\n";
114 else if (type == OPT_INT && (int)d != d)
115 error = "Expected int for %s but found %s\n";
118 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
123 int64_t parse_time_or_die(const char *context, const char *timestr,
127 if (av_parse_time(&us, timestr, is_duration) < 0) {
128 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
129 is_duration ? "duration" : "date", context, timestr);
135 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
136 int rej_flags, int alt_flags)
142 for (po = options; po->name != NULL; po++) {
145 if (((po->flags & req_flags) != req_flags) ||
146 (alt_flags && !(po->flags & alt_flags)) ||
147 (po->flags & rej_flags))
154 av_strlcpy(buf, po->name, sizeof(buf));
156 av_strlcat(buf, " ", sizeof(buf));
157 av_strlcat(buf, po->argname, sizeof(buf));
159 printf("-%-17s %s\n", buf, po->help);
164 void show_help_children(const AVClass *class, int flags)
166 const AVClass *child = NULL;
167 av_opt_show2(&class, NULL, flags, 0);
170 while (child = av_opt_child_class_next(class, child))
171 show_help_children(child, flags);
174 static const OptionDef *find_option(const OptionDef *po, const char *name)
176 const char *p = strchr(name, ':');
177 int len = p ? p - name : strlen(name);
180 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
187 /* _WIN32 means using the windows libc - cygwin doesn't define that
188 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
189 * it doesn't provide the actual command line via GetCommandLineW(). */
190 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
192 #include <shellapi.h>
193 /* Will be leaked on exit */
194 static char** win32_argv_utf8 = NULL;
195 static int win32_argc = 0;
198 * Prepare command line arguments for executable.
199 * For Windows - perform wide-char to UTF-8 conversion.
200 * Input arguments should be main() function arguments.
201 * @param argc_ptr Arguments number (including executable)
202 * @param argv_ptr Arguments list.
204 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
208 int i, buffsize = 0, offset = 0;
210 if (win32_argv_utf8) {
211 *argc_ptr = win32_argc;
212 *argv_ptr = win32_argv_utf8;
217 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
218 if (win32_argc <= 0 || !argv_w)
221 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
222 for (i = 0; i < win32_argc; i++)
223 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
224 NULL, 0, NULL, NULL);
226 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
227 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
228 if (!win32_argv_utf8) {
233 for (i = 0; i < win32_argc; i++) {
234 win32_argv_utf8[i] = &argstr_flat[offset];
235 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
236 &argstr_flat[offset],
237 buffsize - offset, NULL, NULL);
239 win32_argv_utf8[i] = NULL;
242 *argc_ptr = win32_argc;
243 *argv_ptr = win32_argv_utf8;
246 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
250 #endif /* HAVE_COMMANDLINETOARGVW */
252 static int write_option(void *optctx, const OptionDef *po, const char *opt,
255 /* new-style options contain an offset into optctx, old-style address of
257 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
258 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
261 if (po->flags & OPT_SPEC) {
262 SpecifierOpt **so = dst;
263 char *p = strchr(opt, ':');
266 dstcount = (int *)(so + 1);
267 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
268 str = av_strdup(p ? p + 1 : "");
270 return AVERROR(ENOMEM);
271 (*so)[*dstcount - 1].specifier = str;
272 dst = &(*so)[*dstcount - 1].u;
275 if (po->flags & OPT_STRING) {
277 str = av_strdup(arg);
280 return AVERROR(ENOMEM);
282 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
283 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
284 } else if (po->flags & OPT_INT64) {
285 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
286 } else if (po->flags & OPT_TIME) {
287 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
288 } else if (po->flags & OPT_FLOAT) {
289 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
290 } else if (po->flags & OPT_DOUBLE) {
291 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
292 } else if (po->u.func_arg) {
293 int ret = po->u.func_arg(optctx, opt, arg);
295 av_log(NULL, AV_LOG_ERROR,
296 "Failed to set value '%s' for option '%s'\n", arg, opt);
300 if (po->flags & OPT_EXIT)
306 int parse_option(void *optctx, const char *opt, const char *arg,
307 const OptionDef *options)
312 po = find_option(options, opt);
313 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
314 /* handle 'no' bool option */
315 po = find_option(options, opt + 2);
316 if ((po->name && (po->flags & OPT_BOOL)))
318 } else if (po->flags & OPT_BOOL)
322 po = find_option(options, "default");
324 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
325 return AVERROR(EINVAL);
327 if (po->flags & HAS_ARG && !arg) {
328 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
329 return AVERROR(EINVAL);
332 ret = write_option(optctx, po, opt, arg);
336 return !!(po->flags & HAS_ARG);
339 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
340 void (*parse_arg_function)(void *, const char*))
343 int optindex, handleoptions = 1, ret;
345 /* perform system-dependent conversions for arguments list */
346 prepare_app_arguments(&argc, &argv);
350 while (optindex < argc) {
351 opt = argv[optindex++];
353 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
354 if (opt[1] == '-' && opt[2] == '\0') {
360 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
364 if (parse_arg_function)
365 parse_arg_function(optctx, opt);
370 int parse_optgroup(void *optctx, OptionGroup *g)
374 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
375 g->group_def->name, g->arg);
377 for (i = 0; i < g->nb_opts; i++) {
378 Option *o = &g->opts[i];
380 if (g->group_def->flags &&
381 !(g->group_def->flags & o->opt->flags)) {
382 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
383 "%s %s -- you are trying to apply an input option to an "
384 "output file or vice versa. Move this option before the "
385 "file it belongs to.\n", o->key, o->opt->help,
386 g->group_def->name, g->arg);
387 return AVERROR(EINVAL);
390 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
391 o->key, o->opt->help, o->val);
393 ret = write_option(optctx, o->opt, o->key, o->val);
398 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
403 int locate_option(int argc, char **argv, const OptionDef *options,
409 for (i = 1; i < argc; i++) {
410 const char *cur_opt = argv[i];
412 if (*cur_opt++ != '-')
415 po = find_option(options, cur_opt);
416 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
417 po = find_option(options, cur_opt + 2);
419 if ((!po->name && !strcmp(cur_opt, optname)) ||
420 (po->name && !strcmp(optname, po->name)))
423 if (!po->name || po->flags & HAS_ARG)
429 void parse_loglevel(int argc, char **argv, const OptionDef *options)
431 int idx = locate_option(argc, argv, options, "loglevel");
433 idx = locate_option(argc, argv, options, "v");
434 if (idx && argv[idx + 1])
435 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
438 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
439 int opt_default(void *optctx, const char *opt, const char *arg)
442 char opt_stripped[128];
444 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
445 #if CONFIG_AVRESAMPLE
446 const AVClass *rc = avresample_get_class();
449 const AVClass *sc = sws_get_class();
452 if (!(p = strchr(opt, ':')))
453 p = opt + strlen(opt);
454 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
456 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
457 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
458 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
459 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
460 av_dict_set(&codec_opts, opt, arg, FLAGS);
461 else if ((o = av_opt_find(&fc, opt, NULL, 0,
462 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
463 av_dict_set(&format_opts, opt, arg, FLAGS);
464 #if CONFIG_AVRESAMPLE
465 else if ((o = av_opt_find(&rc, opt, NULL, 0,
466 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
467 av_dict_set(&resample_opts, opt, arg, FLAGS);
470 else if ((o = av_opt_find(&sc, opt, NULL, 0,
471 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
472 // XXX we only support sws_flags, not arbitrary sws options
473 int ret = av_opt_set(sws_opts, opt, arg, 0);
475 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
483 return AVERROR_OPTION_NOT_FOUND;
487 * Check whether given option is a group separator.
489 * @return index of the group definition that matched or -1 if none
491 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
496 for (i = 0; i < nb_groups; i++) {
497 const OptionGroupDef *p = &groups[i];
498 if (p->sep && !strcmp(p->sep, opt))
506 * Finish parsing an option group.
508 * @param group_idx which group definition should this group belong to
509 * @param arg argument of the group delimiting option
511 static void finish_group(OptionParseContext *octx, int group_idx,
514 OptionGroupList *l = &octx->groups[group_idx];
517 GROW_ARRAY(l->groups, l->nb_groups);
518 g = &l->groups[l->nb_groups - 1];
520 *g = octx->cur_group;
522 g->group_def = l->group_def;
524 g->sws_opts = sws_opts;
526 g->codec_opts = codec_opts;
527 g->format_opts = format_opts;
528 g->resample_opts = resample_opts;
532 resample_opts = NULL;
538 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
542 * Add an option instance to currently parsed group.
544 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
545 const char *key, const char *val)
547 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
548 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
550 GROW_ARRAY(g->opts, g->nb_opts);
551 g->opts[g->nb_opts - 1].opt = opt;
552 g->opts[g->nb_opts - 1].key = key;
553 g->opts[g->nb_opts - 1].val = val;
556 static void init_parse_context(OptionParseContext *octx,
557 const OptionGroupDef *groups, int nb_groups)
559 static const OptionGroupDef global_group = { "global" };
562 memset(octx, 0, sizeof(*octx));
564 octx->nb_groups = nb_groups;
565 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
569 for (i = 0; i < octx->nb_groups; i++)
570 octx->groups[i].group_def = &groups[i];
572 octx->global_opts.group_def = &global_group;
573 octx->global_opts.arg = "";
578 void uninit_parse_context(OptionParseContext *octx)
582 for (i = 0; i < octx->nb_groups; i++) {
583 OptionGroupList *l = &octx->groups[i];
585 for (j = 0; j < l->nb_groups; j++) {
586 av_freep(&l->groups[j].opts);
587 av_dict_free(&l->groups[j].codec_opts);
588 av_dict_free(&l->groups[j].format_opts);
589 av_dict_free(&l->groups[j].resample_opts);
591 sws_freeContext(l->groups[j].sws_opts);
594 av_freep(&l->groups);
596 av_freep(&octx->groups);
598 av_freep(&octx->cur_group.opts);
599 av_freep(&octx->global_opts.opts);
604 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
605 const OptionDef *options,
606 const OptionGroupDef *groups, int nb_groups)
610 /* perform system-dependent conversions for arguments list */
611 prepare_app_arguments(&argc, &argv);
613 init_parse_context(octx, groups, nb_groups);
614 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
616 while (optindex < argc) {
617 const char *opt = argv[optindex++], *arg;
621 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
623 /* unnamed group separators, e.g. output filename */
624 if (opt[0] != '-' || !opt[1]) {
625 finish_group(octx, 0, opt);
626 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
631 #define GET_ARG(arg) \
633 arg = argv[optindex++]; \
635 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
636 return AVERROR(EINVAL); \
640 /* named group separators, e.g. -i */
641 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
643 finish_group(octx, ret, arg);
644 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
645 groups[ret].name, arg);
650 po = find_option(options, opt);
652 if (po->flags & OPT_EXIT) {
653 /* optional argument, e.g. -h */
654 arg = argv[optindex++];
655 } else if (po->flags & HAS_ARG) {
661 add_opt(octx, po, opt, arg);
662 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
663 "argument '%s'.\n", po->name, po->help, arg);
668 if (argv[optindex]) {
669 ret = opt_default(NULL, opt, argv[optindex]);
671 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
672 "argument '%s'.\n", opt, argv[optindex]);
675 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
676 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
677 "with argument '%s'.\n", opt, argv[optindex]);
682 /* boolean -nofoo options */
683 if (opt[0] == 'n' && opt[1] == 'o' &&
684 (po = find_option(options, opt + 2)) &&
685 po->name && po->flags & OPT_BOOL) {
686 add_opt(octx, po, opt, "0");
687 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
688 "argument 0.\n", po->name, po->help);
692 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
693 return AVERROR_OPTION_NOT_FOUND;
696 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
697 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
700 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
705 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
707 int flags = av_parse_cpu_flags(arg);
712 av_set_cpu_flags_mask(flags);
716 int opt_loglevel(void *optctx, const char *opt, const char *arg)
718 const struct { const char *name; int level; } log_levels[] = {
719 { "quiet" , AV_LOG_QUIET },
720 { "panic" , AV_LOG_PANIC },
721 { "fatal" , AV_LOG_FATAL },
722 { "error" , AV_LOG_ERROR },
723 { "warning", AV_LOG_WARNING },
724 { "info" , AV_LOG_INFO },
725 { "verbose", AV_LOG_VERBOSE },
726 { "debug" , AV_LOG_DEBUG },
727 { "trace" , AV_LOG_TRACE },
733 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
734 if (!strcmp(log_levels[i].name, arg)) {
735 av_log_set_level(log_levels[i].level);
740 level = strtol(arg, &tail, 10);
742 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
743 "Possible levels are numbers or:\n", arg);
744 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
745 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
748 av_log_set_level(level);
752 int opt_timelimit(void *optctx, const char *opt, const char *arg)
755 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
756 struct rlimit rl = { lim, lim + 1 };
757 if (setrlimit(RLIMIT_CPU, &rl))
760 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
765 void print_error(const char *filename, int err)
768 const char *errbuf_ptr = errbuf;
770 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
771 errbuf_ptr = strerror(AVUNERROR(err));
772 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
775 static int warned_cfg = 0;
778 #define SHOW_VERSION 2
779 #define SHOW_CONFIG 4
781 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
782 if (CONFIG_##LIBNAME) { \
783 const char *indent = flags & INDENT? " " : ""; \
784 if (flags & SHOW_VERSION) { \
785 unsigned int version = libname##_version(); \
786 av_log(NULL, level, \
787 "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
789 LIB##LIBNAME##_VERSION_MAJOR, \
790 LIB##LIBNAME##_VERSION_MINOR, \
791 LIB##LIBNAME##_VERSION_MICRO, \
792 version >> 16, version >> 8 & 0xff, version & 0xff); \
794 if (flags & SHOW_CONFIG) { \
795 const char *cfg = libname##_configuration(); \
796 if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
798 av_log(NULL, level, \
799 "%sWARNING: library configuration mismatch\n", \
803 av_log(NULL, level, "%s%-11s configuration: %s\n", \
804 indent, #libname, cfg); \
809 static void print_all_libs_info(int flags, int level)
811 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
812 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
813 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
814 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
815 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
816 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
817 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
820 void show_banner(void)
822 av_log(NULL, AV_LOG_INFO,
823 "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
824 program_name, program_birth_year, this_year);
825 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
826 __DATE__, __TIME__, CC_IDENT);
827 av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
828 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
829 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
832 int show_version(void *optctx, const char *opt, const char *arg)
834 av_log_set_callback(log_callback_help);
835 printf("%s " LIBAV_VERSION "\n", program_name);
836 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
841 int show_license(void *optctx, const char *opt, const char *arg)
845 "This version of %s has nonfree parts compiled in.\n"
846 "Therefore it is not legally redistributable.\n",
849 "%s is free software; you can redistribute it and/or modify\n"
850 "it under the terms of the GNU General Public License as published by\n"
851 "the Free Software Foundation; either version 3 of the License, or\n"
852 "(at your option) any later version.\n"
854 "%s is distributed in the hope that it will be useful,\n"
855 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
856 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
857 "GNU General Public License for more details.\n"
859 "You should have received a copy of the GNU General Public License\n"
860 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
861 program_name, program_name, program_name
863 "%s is free software; you can redistribute it and/or modify\n"
864 "it under the terms of the GNU General Public License as published by\n"
865 "the Free Software Foundation; either version 2 of the License, or\n"
866 "(at your option) any later version.\n"
868 "%s is distributed in the hope that it will be useful,\n"
869 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
870 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
871 "GNU General Public License for more details.\n"
873 "You should have received a copy of the GNU General Public License\n"
874 "along with %s; if not, write to the Free Software\n"
875 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
876 program_name, program_name, program_name
878 "%s is free software; you can redistribute it and/or modify\n"
879 "it under the terms of the GNU Lesser General Public License as published by\n"
880 "the Free Software Foundation; either version 3 of the License, or\n"
881 "(at your option) any later version.\n"
883 "%s is distributed in the hope that it will be useful,\n"
884 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
885 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
886 "GNU Lesser General Public License for more details.\n"
888 "You should have received a copy of the GNU Lesser General Public License\n"
889 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
890 program_name, program_name, program_name
892 "%s is free software; you can redistribute it and/or\n"
893 "modify it under the terms of the GNU Lesser General Public\n"
894 "License as published by the Free Software Foundation; either\n"
895 "version 2.1 of the License, or (at your option) any later version.\n"
897 "%s is distributed in the hope that it will be useful,\n"
898 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
899 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
900 "Lesser General Public License for more details.\n"
902 "You should have received a copy of the GNU Lesser General Public\n"
903 "License along with %s; if not, write to the Free Software\n"
904 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
905 program_name, program_name, program_name
912 int show_formats(void *optctx, const char *opt, const char *arg)
914 AVInputFormat *ifmt = NULL;
915 AVOutputFormat *ofmt = NULL;
916 const char *last_name;
918 printf("File formats:\n"
919 " D. = Demuxing supported\n"
920 " .E = Muxing supported\n"
926 const char *name = NULL;
927 const char *long_name = NULL;
929 while ((ofmt = av_oformat_next(ofmt))) {
930 if ((!name || strcmp(ofmt->name, name) < 0) &&
931 strcmp(ofmt->name, last_name) > 0) {
933 long_name = ofmt->long_name;
937 while ((ifmt = av_iformat_next(ifmt))) {
938 if ((!name || strcmp(ifmt->name, name) < 0) &&
939 strcmp(ifmt->name, last_name) > 0) {
941 long_name = ifmt->long_name;
944 if (name && strcmp(ifmt->name, name) == 0)
951 printf(" %s%s %-15s %s\n",
955 long_name ? long_name:" ");
960 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
961 if (codec->field) { \
962 const type *p = c->field; \
964 printf(" Supported " list_name ":"); \
965 while (*p != term) { \
967 printf(" %s", name); \
973 static void print_codec(const AVCodec *c)
975 int encoder = av_codec_is_encoder(c);
977 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
978 c->long_name ? c->long_name : "");
980 if (c->type == AVMEDIA_TYPE_VIDEO) {
981 printf(" Threading capabilities: ");
982 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
983 CODEC_CAP_SLICE_THREADS)) {
984 case CODEC_CAP_FRAME_THREADS |
985 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
986 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
987 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
988 default: printf("no"); break;
993 if (c->supported_framerates) {
994 const AVRational *fps = c->supported_framerates;
996 printf(" Supported framerates:");
998 printf(" %d/%d", fps->num, fps->den);
1003 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1004 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1005 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1006 GET_SAMPLE_RATE_NAME);
1007 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1008 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1009 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1010 0, GET_CH_LAYOUT_DESC);
1012 if (c->priv_class) {
1013 show_help_children(c->priv_class,
1014 AV_OPT_FLAG_ENCODING_PARAM |
1015 AV_OPT_FLAG_DECODING_PARAM);
1019 static char get_media_type_char(enum AVMediaType type)
1022 case AVMEDIA_TYPE_VIDEO: return 'V';
1023 case AVMEDIA_TYPE_AUDIO: return 'A';
1024 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1025 default: return '?';
1029 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1032 while ((prev = av_codec_next(prev))) {
1033 if (prev->id == id &&
1034 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1040 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1042 const AVCodec *codec = NULL;
1044 printf(" (%s: ", encoder ? "encoders" : "decoders");
1046 while ((codec = next_codec_for_id(id, codec, encoder)))
1047 printf("%s ", codec->name);
1052 int show_codecs(void *optctx, const char *opt, const char *arg)
1054 const AVCodecDescriptor *desc = NULL;
1057 " D..... = Decoding supported\n"
1058 " .E.... = Encoding supported\n"
1059 " ..V... = Video codec\n"
1060 " ..A... = Audio codec\n"
1061 " ..S... = Subtitle codec\n"
1062 " ...I.. = Intra frame-only codec\n"
1063 " ....L. = Lossy compression\n"
1064 " .....S = Lossless compression\n"
1066 while ((desc = avcodec_descriptor_next(desc))) {
1067 const AVCodec *codec = NULL;
1069 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1070 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1072 printf("%c", get_media_type_char(desc->type));
1073 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1074 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1075 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1077 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1079 /* print decoders/encoders when there's more than one or their
1080 * names are different from codec name */
1081 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1082 if (strcmp(codec->name, desc->name)) {
1083 print_codecs_for_id(desc->id, 0);
1088 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1089 if (strcmp(codec->name, desc->name)) {
1090 print_codecs_for_id(desc->id, 1);
1100 static void print_codecs(int encoder)
1102 const AVCodecDescriptor *desc = NULL;
1107 " S... = Subtitle\n"
1108 " .F.. = Frame-level multithreading\n"
1109 " ..S. = Slice-level multithreading\n"
1110 " ...X = Codec is experimental\n"
1112 encoder ? "Encoders" : "Decoders");
1113 while ((desc = avcodec_descriptor_next(desc))) {
1114 const AVCodec *codec = NULL;
1116 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1117 printf("%c", get_media_type_char(desc->type));
1118 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1119 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1120 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1122 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1123 if (strcmp(codec->name, desc->name))
1124 printf(" (codec %s)", desc->name);
1131 int show_decoders(void *optctx, const char *opt, const char *arg)
1137 int show_encoders(void *optctx, const char *opt, const char *arg)
1143 int show_bsfs(void *optctx, const char *opt, const char *arg)
1145 AVBitStreamFilter *bsf = NULL;
1147 printf("Bitstream filters:\n");
1148 while ((bsf = av_bitstream_filter_next(bsf)))
1149 printf("%s\n", bsf->name);
1154 int show_protocols(void *optctx, const char *opt, const char *arg)
1156 void *opaque = NULL;
1159 printf("Supported file protocols:\n"
1161 while ((name = avio_enum_protocols(&opaque, 0)))
1162 printf("%s\n", name);
1163 printf("Output:\n");
1164 while ((name = avio_enum_protocols(&opaque, 1)))
1165 printf("%s\n", name);
1169 int show_filters(void *optctx, const char *opt, const char *arg)
1172 const AVFilter *filter = NULL;
1174 printf("Filters:\n");
1175 while ((filter = avfilter_next(filter)))
1176 printf("%-16s %s\n", filter->name, filter->description);
1178 printf("No filters available: libavfilter disabled\n");
1183 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1185 const AVPixFmtDescriptor *pix_desc = NULL;
1187 printf("Pixel formats:\n"
1188 "I.... = Supported Input format for conversion\n"
1189 ".O... = Supported Output format for conversion\n"
1190 "..H.. = Hardware accelerated format\n"
1191 "...P. = Paletted format\n"
1192 "....B = Bitstream format\n"
1193 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1197 # define sws_isSupportedInput(x) 0
1198 # define sws_isSupportedOutput(x) 0
1201 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1202 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1203 printf("%c%c%c%c%c %-16s %d %2d\n",
1204 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1205 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1206 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1207 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1208 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1210 pix_desc->nb_components,
1211 av_get_bits_per_pixel(pix_desc));
1216 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1220 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1221 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1225 static void show_help_codec(const char *name, int encoder)
1227 const AVCodecDescriptor *desc;
1228 const AVCodec *codec;
1231 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1235 codec = encoder ? avcodec_find_encoder_by_name(name) :
1236 avcodec_find_decoder_by_name(name);
1240 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1243 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1249 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
1250 "but no %s for it are available. Libav might need to be "
1251 "recompiled with additional external libraries.\n",
1252 name, encoder ? "encoders" : "decoders");
1255 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
1260 static void show_help_demuxer(const char *name)
1262 const AVInputFormat *fmt = av_find_input_format(name);
1265 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1269 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1271 if (fmt->extensions)
1272 printf(" Common extensions: %s.\n", fmt->extensions);
1274 if (fmt->priv_class)
1275 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1278 static void show_help_muxer(const char *name)
1280 const AVCodecDescriptor *desc;
1281 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1284 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1288 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1290 if (fmt->extensions)
1291 printf(" Common extensions: %s.\n", fmt->extensions);
1293 printf(" Mime type: %s.\n", fmt->mime_type);
1294 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1295 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1296 printf(" Default video codec: %s.\n", desc->name);
1298 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1299 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1300 printf(" Default audio codec: %s.\n", desc->name);
1302 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1303 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1304 printf(" Default subtitle codec: %s.\n", desc->name);
1307 if (fmt->priv_class)
1308 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1312 static void show_help_filter(const char *name)
1314 const AVFilter *f = avfilter_get_by_name(name);
1318 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1321 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1325 printf("Filter %s [%s]:\n", f->name, f->description);
1327 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1328 printf(" slice threading supported\n");
1330 printf(" Inputs:\n");
1331 count = avfilter_pad_count(f->inputs);
1332 for (i = 0; i < count; i++) {
1333 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1334 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1336 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1337 printf(" dynamic (depending on the options)\n");
1339 printf(" Outputs:\n");
1340 count = avfilter_pad_count(f->outputs);
1341 for (i = 0; i < count; i++) {
1342 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1343 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1345 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1346 printf(" dynamic (depending on the options)\n");
1349 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
1350 AV_OPT_FLAG_AUDIO_PARAM);
1354 int show_help(void *optctx, const char *opt, const char *arg)
1357 av_log_set_callback(log_callback_help);
1359 topic = av_strdup(arg ? arg : "");
1361 return AVERROR(ENOMEM);
1362 par = strchr(topic, '=');
1367 show_help_default(topic, par);
1368 } else if (!strcmp(topic, "decoder")) {
1369 show_help_codec(par, 0);
1370 } else if (!strcmp(topic, "encoder")) {
1371 show_help_codec(par, 1);
1372 } else if (!strcmp(topic, "demuxer")) {
1373 show_help_demuxer(par);
1374 } else if (!strcmp(topic, "muxer")) {
1375 show_help_muxer(par);
1377 } else if (!strcmp(topic, "filter")) {
1378 show_help_filter(par);
1381 show_help_default(topic, par);
1388 int read_yesno(void)
1391 int yesno = (av_toupper(c) == 'Y');
1393 while (c != '\n' && c != EOF)
1399 void init_pts_correction(PtsCorrectionContext *ctx)
1401 ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
1402 ctx->last_pts = ctx->last_dts = INT64_MIN;
1405 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
1408 int64_t pts = AV_NOPTS_VALUE;
1410 if (dts != AV_NOPTS_VALUE) {
1411 ctx->num_faulty_dts += dts <= ctx->last_dts;
1412 ctx->last_dts = dts;
1414 if (reordered_pts != AV_NOPTS_VALUE) {
1415 ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
1416 ctx->last_pts = reordered_pts;
1418 if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
1419 && reordered_pts != AV_NOPTS_VALUE)
1420 pts = reordered_pts;
1427 FILE *get_preset_file(char *filename, size_t filename_size,
1428 const char *preset_name, int is_path,
1429 const char *codec_name)
1433 const char *base[3] = { getenv("AVCONV_DATADIR"),
1438 av_strlcpy(filename, preset_name, filename_size);
1439 f = fopen(filename, "r");
1441 for (i = 0; i < 3 && !f; i++) {
1444 snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
1445 i != 1 ? "" : "/.avconv", preset_name);
1446 f = fopen(filename, "r");
1447 if (!f && codec_name) {
1448 snprintf(filename, filename_size,
1449 "%s%s/%s-%s.avpreset",
1450 base[i], i != 1 ? "" : "/.avconv", codec_name,
1452 f = fopen(filename, "r");
1460 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1462 if (*spec <= '9' && *spec >= '0') /* opt:index */
1463 return strtol(spec, NULL, 0) == st->index;
1464 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1465 *spec == 't') { /* opt:[vasdt] */
1466 enum AVMediaType type;
1469 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1470 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1471 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1472 case 'd': type = AVMEDIA_TYPE_DATA; break;
1473 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1474 default: av_assert0(0);
1476 if (type != st->codec->codec_type)
1478 if (*spec++ == ':') { /* possibly followed by :index */
1479 int i, index = strtol(spec, NULL, 0);
1480 for (i = 0; i < s->nb_streams; i++)
1481 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1482 return i == st->index;
1486 } else if (*spec == 'p' && *(spec + 1) == ':') {
1490 prog_id = strtol(spec, &endptr, 0);
1491 for (i = 0; i < s->nb_programs; i++) {
1492 if (s->programs[i]->id != prog_id)
1495 if (*endptr++ == ':') {
1496 int stream_idx = strtol(endptr, NULL, 0);
1497 return stream_idx >= 0 &&
1498 stream_idx < s->programs[i]->nb_stream_indexes &&
1499 st->index == s->programs[i]->stream_index[stream_idx];
1502 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1503 if (st->index == s->programs[i]->stream_index[j])
1507 } else if (*spec == 'i' && *(spec + 1) == ':') {
1511 stream_id = strtol(spec, &endptr, 0);
1512 return stream_id == st->id;
1513 } else if (*spec == 'm' && *(spec + 1) == ':') {
1514 AVDictionaryEntry *tag;
1519 val = strchr(spec, ':');
1521 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
1523 return AVERROR(ENOMEM);
1525 tag = av_dict_get(st->metadata, key, NULL, 0);
1527 if (!val || !strcmp(tag->value, val + 1))
1536 } else if (*spec == 'u') {
1537 AVCodecContext *avctx = st->codec;
1539 switch (avctx->codec_type) {
1540 case AVMEDIA_TYPE_AUDIO:
1541 val = avctx->sample_rate && avctx->channels;
1542 if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1545 case AVMEDIA_TYPE_VIDEO:
1546 val = avctx->width && avctx->height;
1547 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
1550 case AVMEDIA_TYPE_UNKNOWN:
1557 return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
1558 } else if (!*spec) /* empty specifier, matches everything */
1561 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1562 return AVERROR(EINVAL);
1565 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1566 AVFormatContext *s, AVStream *st, AVCodec *codec)
1568 AVDictionary *ret = NULL;
1569 AVDictionaryEntry *t = NULL;
1570 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1571 : AV_OPT_FLAG_DECODING_PARAM;
1573 const AVClass *cc = avcodec_get_class();
1576 codec = s->oformat ? avcodec_find_encoder(codec_id)
1577 : avcodec_find_decoder(codec_id);
1579 switch (st->codec->codec_type) {
1580 case AVMEDIA_TYPE_VIDEO:
1582 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1584 case AVMEDIA_TYPE_AUDIO:
1586 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1588 case AVMEDIA_TYPE_SUBTITLE:
1590 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1594 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1595 char *p = strchr(t->key, ':');
1597 /* check stream specification in opt name */
1599 switch (check_stream_specifier(s, st, p + 1)) {
1600 case 1: *p = 0; break;
1602 default: return NULL;
1605 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1606 (codec && codec->priv_class &&
1607 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1608 AV_OPT_SEARCH_FAKE_OBJ)))
1609 av_dict_set(&ret, t->key, t->value, 0);
1610 else if (t->key[0] == prefix &&
1611 av_opt_find(&cc, t->key + 1, NULL, flags,
1612 AV_OPT_SEARCH_FAKE_OBJ))
1613 av_dict_set(&ret, t->key + 1, t->value, 0);
1621 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1622 AVDictionary *codec_opts)
1625 AVDictionary **opts;
1629 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1631 av_log(NULL, AV_LOG_ERROR,
1632 "Could not alloc memory for stream options.\n");
1635 for (i = 0; i < s->nb_streams; i++)
1636 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1637 s, s->streams[i], NULL);
1641 void *grow_array(void *array, int elem_size, int *size, int new_size)
1643 if (new_size >= INT_MAX / elem_size) {
1644 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1647 if (*size < new_size) {
1648 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1650 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1653 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1660 const char *media_type_string(enum AVMediaType media_type)
1662 switch (media_type) {
1663 case AVMEDIA_TYPE_VIDEO: return "video";
1664 case AVMEDIA_TYPE_AUDIO: return "audio";
1665 case AVMEDIA_TYPE_DATA: return "data";
1666 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
1667 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
1668 default: return "unknown";