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
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. */
32 #include "libavformat/avformat.h"
33 #include "libavfilter/avfilter.h"
34 #include "libavdevice/avdevice.h"
35 #include "libavresample/avresample.h"
36 #include "libswscale/swscale.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/parseutils.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavutil/eval.h"
44 #include "libavutil/dict.h"
45 #include "libavutil/opt.h"
49 #include "libavformat/network.h"
51 #if HAVE_SYS_RESOURCE_H
53 #include <sys/resource.h>
56 struct SwsContext *sws_opts;
57 AVDictionary *format_opts, *codec_opts, *resample_opts;
59 static const int this_year = 2013;
64 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
69 void uninit_opts(void)
72 sws_freeContext(sws_opts);
75 av_dict_free(&format_opts);
76 av_dict_free(&codec_opts);
77 av_dict_free(&resample_opts);
80 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
82 vfprintf(stdout, fmt, vl);
85 static void (*program_exit)(int ret);
87 void register_exit(void (*cb)(int ret))
92 void exit_program(int ret)
100 double parse_number_or_die(const char *context, const char *numstr, int type,
101 double min, double max)
105 double d = av_strtod(numstr, &tail);
107 error = "Expected number for %s but found: %s\n";
108 else if (d < min || d > max)
109 error = "The value for %s was %s which is not within %f - %f\n";
110 else if (type == OPT_INT64 && (int64_t)d != d)
111 error = "Expected int64 for %s but found %s\n";
112 else if (type == OPT_INT && (int)d != d)
113 error = "Expected int for %s but found %s\n";
116 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
121 int64_t parse_time_or_die(const char *context, const char *timestr,
125 if (av_parse_time(&us, timestr, is_duration) < 0) {
126 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
127 is_duration ? "duration" : "date", context, timestr);
133 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
134 int rej_flags, int alt_flags)
140 for (po = options; po->name != NULL; po++) {
143 if (((po->flags & req_flags) != req_flags) ||
144 (alt_flags && !(po->flags & alt_flags)) ||
145 (po->flags & rej_flags))
152 av_strlcpy(buf, po->name, sizeof(buf));
154 av_strlcat(buf, " ", sizeof(buf));
155 av_strlcat(buf, po->argname, sizeof(buf));
157 printf("-%-17s %s\n", buf, po->help);
162 void show_help_children(const AVClass *class, int flags)
164 const AVClass *child = NULL;
165 av_opt_show2(&class, NULL, flags, 0);
168 while (child = av_opt_child_class_next(class, child))
169 show_help_children(child, flags);
172 static const OptionDef *find_option(const OptionDef *po, const char *name)
174 const char *p = strchr(name, ':');
175 int len = p ? p - name : strlen(name);
177 while (po->name != NULL) {
178 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
185 /* _WIN32 means using the windows libc - cygwin doesn't define that
186 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
187 * it doesn't provide the actual command line via GetCommandLineW(). */
188 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
190 #include <shellapi.h>
191 /* Will be leaked on exit */
192 static char** win32_argv_utf8 = NULL;
193 static int win32_argc = 0;
196 * Prepare command line arguments for executable.
197 * For Windows - perform wide-char to UTF-8 conversion.
198 * Input arguments should be main() function arguments.
199 * @param argc_ptr Arguments number (including executable)
200 * @param argv_ptr Arguments list.
202 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
206 int i, buffsize = 0, offset = 0;
208 if (win32_argv_utf8) {
209 *argc_ptr = win32_argc;
210 *argv_ptr = win32_argv_utf8;
215 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
216 if (win32_argc <= 0 || !argv_w)
219 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
220 for (i = 0; i < win32_argc; i++)
221 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
222 NULL, 0, NULL, NULL);
224 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
225 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
226 if (win32_argv_utf8 == NULL) {
231 for (i = 0; i < win32_argc; i++) {
232 win32_argv_utf8[i] = &argstr_flat[offset];
233 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
234 &argstr_flat[offset],
235 buffsize - offset, NULL, NULL);
237 win32_argv_utf8[i] = NULL;
240 *argc_ptr = win32_argc;
241 *argv_ptr = win32_argv_utf8;
244 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
248 #endif /* HAVE_COMMANDLINETOARGVW */
250 static int write_option(void *optctx, const OptionDef *po, const char *opt,
253 /* new-style options contain an offset into optctx, old-style address of
255 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
256 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
259 if (po->flags & OPT_SPEC) {
260 SpecifierOpt **so = dst;
261 char *p = strchr(opt, ':');
263 dstcount = (int *)(so + 1);
264 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
265 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
266 dst = &(*so)[*dstcount - 1].u;
269 if (po->flags & OPT_STRING) {
271 str = av_strdup(arg);
274 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
275 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
276 } else if (po->flags & OPT_INT64) {
277 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
278 } else if (po->flags & OPT_TIME) {
279 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
280 } else if (po->flags & OPT_FLOAT) {
281 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
282 } else if (po->flags & OPT_DOUBLE) {
283 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
284 } else if (po->u.func_arg) {
285 int ret = po->u.func_arg(optctx, opt, arg);
287 av_log(NULL, AV_LOG_ERROR,
288 "Failed to set value '%s' for option '%s'\n", arg, opt);
292 if (po->flags & OPT_EXIT)
298 int parse_option(void *optctx, const char *opt, const char *arg,
299 const OptionDef *options)
304 po = find_option(options, opt);
305 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
306 /* handle 'no' bool option */
307 po = find_option(options, opt + 2);
308 if ((po->name && (po->flags & OPT_BOOL)))
310 } else if (po->flags & OPT_BOOL)
314 po = find_option(options, "default");
316 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
317 return AVERROR(EINVAL);
319 if (po->flags & HAS_ARG && !arg) {
320 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
321 return AVERROR(EINVAL);
324 ret = write_option(optctx, po, opt, arg);
328 return !!(po->flags & HAS_ARG);
331 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
332 void (*parse_arg_function)(void *, const char*))
335 int optindex, handleoptions = 1, ret;
337 /* perform system-dependent conversions for arguments list */
338 prepare_app_arguments(&argc, &argv);
342 while (optindex < argc) {
343 opt = argv[optindex++];
345 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
346 if (opt[1] == '-' && opt[2] == '\0') {
352 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
356 if (parse_arg_function)
357 parse_arg_function(optctx, opt);
362 int parse_optgroup(void *optctx, OptionGroup *g)
366 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
367 g->group_def->name, g->arg);
369 for (i = 0; i < g->nb_opts; i++) {
370 Option *o = &g->opts[i];
372 if (g->group_def->flags &&
373 !(g->group_def->flags & o->opt->flags)) {
374 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
375 "%s %s -- you are trying to apply an input option to an "
376 "output file or vice versa. Move this option before the "
377 "file it belongs to.\n", o->key, o->opt->help,
378 g->group_def->name, g->arg);
379 return AVERROR(EINVAL);
382 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
383 o->key, o->opt->help, o->val);
385 ret = write_option(optctx, o->opt, o->key, o->val);
390 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
395 int locate_option(int argc, char **argv, const OptionDef *options,
401 for (i = 1; i < argc; i++) {
402 const char *cur_opt = argv[i];
404 if (*cur_opt++ != '-')
407 po = find_option(options, cur_opt);
408 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
409 po = find_option(options, cur_opt + 2);
411 if ((!po->name && !strcmp(cur_opt, optname)) ||
412 (po->name && !strcmp(optname, po->name)))
415 if (!po || po->flags & HAS_ARG)
421 void parse_loglevel(int argc, char **argv, const OptionDef *options)
423 int idx = locate_option(argc, argv, options, "loglevel");
425 idx = locate_option(argc, argv, options, "v");
426 if (idx && argv[idx + 1])
427 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
430 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
431 int opt_default(void *optctx, const char *opt, const char *arg)
434 char opt_stripped[128];
436 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
437 #if CONFIG_AVRESAMPLE
438 const AVClass *rc = avresample_get_class();
441 const AVClass *sc = sws_get_class();
444 if (!(p = strchr(opt, ':')))
445 p = opt + strlen(opt);
446 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
448 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
449 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
450 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
451 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
452 av_dict_set(&codec_opts, opt, arg, FLAGS);
453 else if ((o = av_opt_find(&fc, opt, NULL, 0,
454 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
455 av_dict_set(&format_opts, opt, arg, FLAGS);
456 #if CONFIG_AVRESAMPLE
457 else if ((o = av_opt_find(&rc, opt, NULL, 0,
458 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
459 av_dict_set(&resample_opts, opt, arg, FLAGS);
462 else if ((o = av_opt_find(&sc, opt, NULL, 0,
463 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
464 // XXX we only support sws_flags, not arbitrary sws options
465 int ret = av_opt_set(sws_opts, opt, arg, 0);
467 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
475 return AVERROR_OPTION_NOT_FOUND;
479 * Check whether given option is a group separator.
481 * @return index of the group definition that matched or -1 if none
483 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
488 for (i = 0; i < nb_groups; i++) {
489 const OptionGroupDef *p = &groups[i];
490 if (p->sep && !strcmp(p->sep, opt))
498 * Finish parsing an option group.
500 * @param group_idx which group definition should this group belong to
501 * @param arg argument of the group delimiting option
503 static void finish_group(OptionParseContext *octx, int group_idx,
506 OptionGroupList *l = &octx->groups[group_idx];
509 GROW_ARRAY(l->groups, l->nb_groups);
510 g = &l->groups[l->nb_groups - 1];
512 *g = octx->cur_group;
514 g->group_def = l->group_def;
516 g->sws_opts = sws_opts;
518 g->codec_opts = codec_opts;
519 g->format_opts = format_opts;
520 g->resample_opts = resample_opts;
524 resample_opts = NULL;
530 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
534 * Add an option instance to currently parsed group.
536 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
537 const char *key, const char *val)
539 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
540 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
542 GROW_ARRAY(g->opts, g->nb_opts);
543 g->opts[g->nb_opts - 1].opt = opt;
544 g->opts[g->nb_opts - 1].key = key;
545 g->opts[g->nb_opts - 1].val = val;
548 static void init_parse_context(OptionParseContext *octx,
549 const OptionGroupDef *groups, int nb_groups)
551 static const OptionGroupDef global_group = { "global" };
554 memset(octx, 0, sizeof(*octx));
556 octx->nb_groups = nb_groups;
557 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
561 for (i = 0; i < octx->nb_groups; i++)
562 octx->groups[i].group_def = &groups[i];
564 octx->global_opts.group_def = &global_group;
565 octx->global_opts.arg = "";
570 void uninit_parse_context(OptionParseContext *octx)
574 for (i = 0; i < octx->nb_groups; i++) {
575 OptionGroupList *l = &octx->groups[i];
577 for (j = 0; j < l->nb_groups; j++) {
578 av_freep(&l->groups[j].opts);
579 av_dict_free(&l->groups[j].codec_opts);
580 av_dict_free(&l->groups[j].format_opts);
581 av_dict_free(&l->groups[j].resample_opts);
583 sws_freeContext(l->groups[j].sws_opts);
586 av_freep(&l->groups);
588 av_freep(&octx->groups);
590 av_freep(&octx->cur_group.opts);
591 av_freep(&octx->global_opts.opts);
596 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
597 const OptionDef *options,
598 const OptionGroupDef *groups, int nb_groups)
602 /* perform system-dependent conversions for arguments list */
603 prepare_app_arguments(&argc, &argv);
605 init_parse_context(octx, groups, nb_groups);
606 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
608 while (optindex < argc) {
609 const char *opt = argv[optindex++], *arg;
613 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
615 /* unnamed group separators, e.g. output filename */
616 if (opt[0] != '-' || !opt[1]) {
617 finish_group(octx, 0, opt);
618 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
623 #define GET_ARG(arg) \
625 arg = argv[optindex++]; \
627 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
628 return AVERROR(EINVAL); \
632 /* named group separators, e.g. -i */
633 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
635 finish_group(octx, ret, arg);
636 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
637 groups[ret].name, arg);
642 po = find_option(options, opt);
644 if (po->flags & OPT_EXIT) {
645 /* optional argument, e.g. -h */
646 arg = argv[optindex++];
647 } else if (po->flags & HAS_ARG) {
653 add_opt(octx, po, opt, arg);
654 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
655 "argument '%s'.\n", po->name, po->help, arg);
660 if (argv[optindex]) {
661 ret = opt_default(NULL, opt, argv[optindex]);
663 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
664 "argument '%s'.\n", opt, argv[optindex]);
667 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
668 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
669 "with argument '%s'.\n", opt, argv[optindex]);
674 /* boolean -nofoo options */
675 if (opt[0] == 'n' && opt[1] == 'o' &&
676 (po = find_option(options, opt + 2)) &&
677 po->name && po->flags & OPT_BOOL) {
678 add_opt(octx, po, opt, "0");
679 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
680 "argument 0.\n", po->name, po->help);
684 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
685 return AVERROR_OPTION_NOT_FOUND;
688 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
689 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
692 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
697 int opt_loglevel(void *optctx, const char *opt, const char *arg)
699 const struct { const char *name; int level; } log_levels[] = {
700 { "quiet" , AV_LOG_QUIET },
701 { "panic" , AV_LOG_PANIC },
702 { "fatal" , AV_LOG_FATAL },
703 { "error" , AV_LOG_ERROR },
704 { "warning", AV_LOG_WARNING },
705 { "info" , AV_LOG_INFO },
706 { "verbose", AV_LOG_VERBOSE },
707 { "debug" , AV_LOG_DEBUG },
713 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
714 if (!strcmp(log_levels[i].name, arg)) {
715 av_log_set_level(log_levels[i].level);
720 level = strtol(arg, &tail, 10);
722 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
723 "Possible levels are numbers or:\n", arg);
724 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
725 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
728 av_log_set_level(level);
732 int opt_timelimit(void *optctx, const char *opt, const char *arg)
735 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
736 struct rlimit rl = { lim, lim + 1 };
737 if (setrlimit(RLIMIT_CPU, &rl))
740 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
745 void print_error(const char *filename, int err)
748 const char *errbuf_ptr = errbuf;
750 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
751 errbuf_ptr = strerror(AVUNERROR(err));
752 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
755 static int warned_cfg = 0;
758 #define SHOW_VERSION 2
759 #define SHOW_CONFIG 4
761 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
762 if (CONFIG_##LIBNAME) { \
763 const char *indent = flags & INDENT? " " : ""; \
764 if (flags & SHOW_VERSION) { \
765 unsigned int version = libname##_version(); \
766 av_log(NULL, level, \
767 "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
769 LIB##LIBNAME##_VERSION_MAJOR, \
770 LIB##LIBNAME##_VERSION_MINOR, \
771 LIB##LIBNAME##_VERSION_MICRO, \
772 version >> 16, version >> 8 & 0xff, version & 0xff); \
774 if (flags & SHOW_CONFIG) { \
775 const char *cfg = libname##_configuration(); \
776 if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
778 av_log(NULL, level, \
779 "%sWARNING: library configuration mismatch\n", \
783 av_log(NULL, level, "%s%-11s configuration: %s\n", \
784 indent, #libname, cfg); \
789 static void print_all_libs_info(int flags, int level)
791 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
792 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
793 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
794 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
795 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
796 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
797 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
800 void show_banner(void)
802 av_log(NULL, AV_LOG_INFO,
803 "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
804 program_name, program_birth_year, this_year);
805 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
806 __DATE__, __TIME__, CC_IDENT);
807 av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
808 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
809 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
812 int show_version(void *optctx, const char *opt, const char *arg)
814 av_log_set_callback(log_callback_help);
815 printf("%s " LIBAV_VERSION "\n", program_name);
816 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
821 int show_license(void *optctx, const char *opt, const char *arg)
825 "This version of %s has nonfree parts compiled in.\n"
826 "Therefore it is not legally redistributable.\n",
829 "%s is free software; you can redistribute it and/or modify\n"
830 "it under the terms of the GNU General Public License as published by\n"
831 "the Free Software Foundation; either version 3 of the License, or\n"
832 "(at your option) any later version.\n"
834 "%s is distributed in the hope that it will be useful,\n"
835 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
836 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
837 "GNU General Public License for more details.\n"
839 "You should have received a copy of the GNU General Public License\n"
840 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
841 program_name, program_name, program_name
843 "%s is free software; you can redistribute it and/or modify\n"
844 "it under the terms of the GNU General Public License as published by\n"
845 "the Free Software Foundation; either version 2 of the License, or\n"
846 "(at your option) any later version.\n"
848 "%s is distributed in the hope that it will be useful,\n"
849 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
850 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
851 "GNU General Public License for more details.\n"
853 "You should have received a copy of the GNU General Public License\n"
854 "along with %s; if not, write to the Free Software\n"
855 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
856 program_name, program_name, program_name
858 "%s is free software; you can redistribute it and/or modify\n"
859 "it under the terms of the GNU Lesser General Public License as published by\n"
860 "the Free Software Foundation; either version 3 of the License, or\n"
861 "(at your option) any later version.\n"
863 "%s is distributed in the hope that it will be useful,\n"
864 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
865 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
866 "GNU Lesser General Public License for more details.\n"
868 "You should have received a copy of the GNU Lesser General Public License\n"
869 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
870 program_name, program_name, program_name
872 "%s is free software; you can redistribute it and/or\n"
873 "modify it under the terms of the GNU Lesser General Public\n"
874 "License as published by the Free Software Foundation; either\n"
875 "version 2.1 of the License, or (at your option) any later version.\n"
877 "%s is distributed in the hope that it will be useful,\n"
878 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
879 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
880 "Lesser General Public License for more details.\n"
882 "You should have received a copy of the GNU Lesser General Public\n"
883 "License along with %s; if not, write to the Free Software\n"
884 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
885 program_name, program_name, program_name
892 int show_formats(void *optctx, const char *opt, const char *arg)
894 AVInputFormat *ifmt = NULL;
895 AVOutputFormat *ofmt = NULL;
896 const char *last_name;
898 printf("File formats:\n"
899 " D. = Demuxing supported\n"
900 " .E = Muxing supported\n"
906 const char *name = NULL;
907 const char *long_name = NULL;
909 while ((ofmt = av_oformat_next(ofmt))) {
910 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
911 strcmp(ofmt->name, last_name) > 0) {
913 long_name = ofmt->long_name;
917 while ((ifmt = av_iformat_next(ifmt))) {
918 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
919 strcmp(ifmt->name, last_name) > 0) {
921 long_name = ifmt->long_name;
924 if (name && strcmp(ifmt->name, name) == 0)
931 printf(" %s%s %-15s %s\n",
935 long_name ? long_name:" ");
940 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
941 if (codec->field) { \
942 const type *p = c->field; \
944 printf(" Supported " list_name ":"); \
945 while (*p != term) { \
947 printf(" %s", name); \
953 static void print_codec(const AVCodec *c)
955 int encoder = av_codec_is_encoder(c);
957 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
958 c->long_name ? c->long_name : "");
960 if (c->type == AVMEDIA_TYPE_VIDEO) {
961 printf(" Threading capabilities: ");
962 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
963 CODEC_CAP_SLICE_THREADS)) {
964 case CODEC_CAP_FRAME_THREADS |
965 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
966 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
967 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
968 default: printf("no"); break;
973 if (c->supported_framerates) {
974 const AVRational *fps = c->supported_framerates;
976 printf(" Supported framerates:");
978 printf(" %d/%d", fps->num, fps->den);
983 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
984 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
985 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
986 GET_SAMPLE_RATE_NAME);
987 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
988 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
989 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
990 0, GET_CH_LAYOUT_DESC);
993 show_help_children(c->priv_class,
994 AV_OPT_FLAG_ENCODING_PARAM |
995 AV_OPT_FLAG_DECODING_PARAM);
999 static char get_media_type_char(enum AVMediaType type)
1002 case AVMEDIA_TYPE_VIDEO: return 'V';
1003 case AVMEDIA_TYPE_AUDIO: return 'A';
1004 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1005 default: return '?';
1009 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1012 while ((prev = av_codec_next(prev))) {
1013 if (prev->id == id &&
1014 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1020 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1022 const AVCodec *codec = NULL;
1024 printf(" (%s: ", encoder ? "encoders" : "decoders");
1026 while ((codec = next_codec_for_id(id, codec, encoder)))
1027 printf("%s ", codec->name);
1032 int show_codecs(void *optctx, const char *opt, const char *arg)
1034 const AVCodecDescriptor *desc = NULL;
1037 " D..... = Decoding supported\n"
1038 " .E.... = Encoding supported\n"
1039 " ..V... = Video codec\n"
1040 " ..A... = Audio codec\n"
1041 " ..S... = Subtitle codec\n"
1042 " ...I.. = Intra frame-only codec\n"
1043 " ....L. = Lossy compression\n"
1044 " .....S = Lossless compression\n"
1046 while ((desc = avcodec_descriptor_next(desc))) {
1047 const AVCodec *codec = NULL;
1049 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1050 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1052 printf("%c", get_media_type_char(desc->type));
1053 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1054 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1055 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1057 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1059 /* print decoders/encoders when there's more than one or their
1060 * names are different from codec name */
1061 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1062 if (strcmp(codec->name, desc->name)) {
1063 print_codecs_for_id(desc->id, 0);
1068 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1069 if (strcmp(codec->name, desc->name)) {
1070 print_codecs_for_id(desc->id, 1);
1080 static void print_codecs(int encoder)
1082 const AVCodecDescriptor *desc = NULL;
1087 " S... = Subtitle\n"
1088 " .F.. = Frame-level multithreading\n"
1089 " ..S. = Slice-level multithreading\n"
1090 " ...X = Codec is experimental\n"
1092 encoder ? "Encoders" : "Decoders");
1093 while ((desc = avcodec_descriptor_next(desc))) {
1094 const AVCodec *codec = NULL;
1096 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1097 printf("%c", get_media_type_char(desc->type));
1098 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1099 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1100 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1102 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1103 if (strcmp(codec->name, desc->name))
1104 printf(" (codec %s)", desc->name);
1111 int show_decoders(void *optctx, const char *opt, const char *arg)
1117 int show_encoders(void *optctx, const char *opt, const char *arg)
1123 int show_bsfs(void *optctx, const char *opt, const char *arg)
1125 AVBitStreamFilter *bsf = NULL;
1127 printf("Bitstream filters:\n");
1128 while ((bsf = av_bitstream_filter_next(bsf)))
1129 printf("%s\n", bsf->name);
1134 int show_protocols(void *optctx, const char *opt, const char *arg)
1136 void *opaque = NULL;
1139 printf("Supported file protocols:\n"
1141 while ((name = avio_enum_protocols(&opaque, 0)))
1142 printf("%s\n", name);
1143 printf("Output:\n");
1144 while ((name = avio_enum_protocols(&opaque, 1)))
1145 printf("%s\n", name);
1149 int show_filters(void *optctx, const char *opt, const char *arg)
1151 const AVFilter av_unused(*filter) = NULL;
1153 printf("Filters:\n");
1155 while ((filter = avfilter_next(filter)))
1156 printf("%-16s %s\n", filter->name, filter->description);
1161 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1163 const AVPixFmtDescriptor *pix_desc = NULL;
1165 printf("Pixel formats:\n"
1166 "I.... = Supported Input format for conversion\n"
1167 ".O... = Supported Output format for conversion\n"
1168 "..H.. = Hardware accelerated format\n"
1169 "...P. = Paletted format\n"
1170 "....B = Bitstream format\n"
1171 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1175 # define sws_isSupportedInput(x) 0
1176 # define sws_isSupportedOutput(x) 0
1179 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1180 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1181 printf("%c%c%c%c%c %-16s %d %2d\n",
1182 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1183 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1184 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1185 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1186 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1188 pix_desc->nb_components,
1189 av_get_bits_per_pixel(pix_desc));
1194 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1198 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1199 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1203 static void show_help_codec(const char *name, int encoder)
1205 const AVCodecDescriptor *desc;
1206 const AVCodec *codec;
1209 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1213 codec = encoder ? avcodec_find_encoder_by_name(name) :
1214 avcodec_find_decoder_by_name(name);
1218 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1221 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1227 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
1228 "but no %s for it are available. Libav might need to be "
1229 "recompiled with additional external libraries.\n",
1230 name, encoder ? "encoders" : "decoders");
1233 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
1238 static void show_help_demuxer(const char *name)
1240 const AVInputFormat *fmt = av_find_input_format(name);
1243 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1247 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1249 if (fmt->extensions)
1250 printf(" Common extensions: %s.\n", fmt->extensions);
1252 if (fmt->priv_class)
1253 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1256 static void show_help_muxer(const char *name)
1258 const AVCodecDescriptor *desc;
1259 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1262 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1266 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1268 if (fmt->extensions)
1269 printf(" Common extensions: %s.\n", fmt->extensions);
1271 printf(" Mime type: %s.\n", fmt->mime_type);
1272 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1273 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1274 printf(" Default video codec: %s.\n", desc->name);
1276 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1277 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1278 printf(" Default audio codec: %s.\n", desc->name);
1280 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1281 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1282 printf(" Default subtitle codec: %s.\n", desc->name);
1285 if (fmt->priv_class)
1286 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1290 static void show_help_filter(const char *name)
1292 const AVFilter *f = avfilter_get_by_name(name);
1296 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1299 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1303 printf("Filter %s [%s]:\n", f->name, f->description);
1305 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1306 printf(" slice threading supported\n");
1308 printf(" Inputs:\n");
1309 count = avfilter_pad_count(f->inputs);
1310 for (i = 0; i < count; i++) {
1311 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1312 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1314 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1315 printf(" dynamic (depending on the options)\n");
1317 printf(" Outputs:\n");
1318 count = avfilter_pad_count(f->outputs);
1319 for (i = 0; i < count; i++) {
1320 printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1321 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1323 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1324 printf(" dynamic (depending on the options)\n");
1327 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
1328 AV_OPT_FLAG_AUDIO_PARAM);
1332 int show_help(void *optctx, const char *opt, const char *arg)
1335 av_log_set_callback(log_callback_help);
1337 topic = av_strdup(arg ? arg : "");
1338 par = strchr(topic, '=');
1343 show_help_default(topic, par);
1344 } else if (!strcmp(topic, "decoder")) {
1345 show_help_codec(par, 0);
1346 } else if (!strcmp(topic, "encoder")) {
1347 show_help_codec(par, 1);
1348 } else if (!strcmp(topic, "demuxer")) {
1349 show_help_demuxer(par);
1350 } else if (!strcmp(topic, "muxer")) {
1351 show_help_muxer(par);
1353 } else if (!strcmp(topic, "filter")) {
1354 show_help_filter(par);
1357 show_help_default(topic, par);
1364 int read_yesno(void)
1367 int yesno = (av_toupper(c) == 'Y');
1369 while (c != '\n' && c != EOF)
1375 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1378 FILE *f = fopen(filename, "rb");
1381 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1383 return AVERROR(errno);
1385 fseek(f, 0, SEEK_END);
1387 fseek(f, 0, SEEK_SET);
1388 *bufptr = av_malloc(*size + 1);
1390 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1392 return AVERROR(ENOMEM);
1394 ret = fread(*bufptr, 1, *size, f);
1398 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1399 filename, strerror(errno));
1400 ret = AVERROR(errno);
1405 (*bufptr)[(*size)++] = '\0';
1412 void init_pts_correction(PtsCorrectionContext *ctx)
1414 ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
1415 ctx->last_pts = ctx->last_dts = INT64_MIN;
1418 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
1421 int64_t pts = AV_NOPTS_VALUE;
1423 if (dts != AV_NOPTS_VALUE) {
1424 ctx->num_faulty_dts += dts <= ctx->last_dts;
1425 ctx->last_dts = dts;
1427 if (reordered_pts != AV_NOPTS_VALUE) {
1428 ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
1429 ctx->last_pts = reordered_pts;
1431 if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
1432 && reordered_pts != AV_NOPTS_VALUE)
1433 pts = reordered_pts;
1440 FILE *get_preset_file(char *filename, size_t filename_size,
1441 const char *preset_name, int is_path,
1442 const char *codec_name)
1446 const char *base[3] = { getenv("AVCONV_DATADIR"),
1451 av_strlcpy(filename, preset_name, filename_size);
1452 f = fopen(filename, "r");
1454 for (i = 0; i < 3 && !f; i++) {
1457 snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
1458 i != 1 ? "" : "/.avconv", preset_name);
1459 f = fopen(filename, "r");
1460 if (!f && codec_name) {
1461 snprintf(filename, filename_size,
1462 "%s%s/%s-%s.avpreset",
1463 base[i], i != 1 ? "" : "/.avconv", codec_name,
1465 f = fopen(filename, "r");
1473 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1475 if (*spec <= '9' && *spec >= '0') /* opt:index */
1476 return strtol(spec, NULL, 0) == st->index;
1477 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1478 *spec == 't') { /* opt:[vasdt] */
1479 enum AVMediaType type;
1482 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1483 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1484 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1485 case 'd': type = AVMEDIA_TYPE_DATA; break;
1486 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1487 default: av_assert0(0);
1489 if (type != st->codec->codec_type)
1491 if (*spec++ == ':') { /* possibly followed by :index */
1492 int i, index = strtol(spec, NULL, 0);
1493 for (i = 0; i < s->nb_streams; i++)
1494 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1495 return i == st->index;
1499 } else if (*spec == 'p' && *(spec + 1) == ':') {
1503 prog_id = strtol(spec, &endptr, 0);
1504 for (i = 0; i < s->nb_programs; i++) {
1505 if (s->programs[i]->id != prog_id)
1508 if (*endptr++ == ':') {
1509 int stream_idx = strtol(endptr, NULL, 0);
1510 return stream_idx >= 0 &&
1511 stream_idx < s->programs[i]->nb_stream_indexes &&
1512 st->index == s->programs[i]->stream_index[stream_idx];
1515 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1516 if (st->index == s->programs[i]->stream_index[j])
1520 } else if (!*spec) /* empty specifier, matches everything */
1523 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1524 return AVERROR(EINVAL);
1527 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1528 AVFormatContext *s, AVStream *st, AVCodec *codec)
1530 AVDictionary *ret = NULL;
1531 AVDictionaryEntry *t = NULL;
1532 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1533 : AV_OPT_FLAG_DECODING_PARAM;
1535 const AVClass *cc = avcodec_get_class();
1538 codec = s->oformat ? avcodec_find_encoder(codec_id)
1539 : avcodec_find_decoder(codec_id);
1541 switch (st->codec->codec_type) {
1542 case AVMEDIA_TYPE_VIDEO:
1544 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1546 case AVMEDIA_TYPE_AUDIO:
1548 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1550 case AVMEDIA_TYPE_SUBTITLE:
1552 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1556 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1557 char *p = strchr(t->key, ':');
1559 /* check stream specification in opt name */
1561 switch (check_stream_specifier(s, st, p + 1)) {
1562 case 1: *p = 0; break;
1564 default: return NULL;
1567 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1568 (codec && codec->priv_class &&
1569 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1570 AV_OPT_SEARCH_FAKE_OBJ)))
1571 av_dict_set(&ret, t->key, t->value, 0);
1572 else if (t->key[0] == prefix &&
1573 av_opt_find(&cc, t->key + 1, NULL, flags,
1574 AV_OPT_SEARCH_FAKE_OBJ))
1575 av_dict_set(&ret, t->key + 1, t->value, 0);
1583 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1584 AVDictionary *codec_opts)
1587 AVDictionary **opts;
1591 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1593 av_log(NULL, AV_LOG_ERROR,
1594 "Could not alloc memory for stream options.\n");
1597 for (i = 0; i < s->nb_streams; i++)
1598 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1599 s, s->streams[i], NULL);
1603 void *grow_array(void *array, int elem_size, int *size, int new_size)
1605 if (new_size >= INT_MAX / elem_size) {
1606 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1609 if (*size < new_size) {
1610 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1612 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1615 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1622 const char *media_type_string(enum AVMediaType media_type)
1624 switch (media_type) {
1625 case AVMEDIA_TYPE_VIDEO: return "video";
1626 case AVMEDIA_TYPE_AUDIO: return "audio";
1627 case AVMEDIA_TYPE_DATA: return "data";
1628 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
1629 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
1630 default: return "unknown";