2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "compat/va_copy.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 "libswresample/swresample.h"
39 #include "libpostproc/postprocess.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/bprint.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/imgutils.h"
45 #include "libavutil/parseutils.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/eval.h"
48 #include "libavutil/dict.h"
49 #include "libavutil/opt.h"
53 #include "libavformat/network.h"
55 #if HAVE_SYS_RESOURCE_H
57 #include <sys/resource.h>
60 static int init_report(const char *env);
62 struct SwsContext *sws_opts;
63 AVDictionary *swr_opts;
64 AVDictionary *format_opts, *codec_opts, *resample_opts;
66 const int this_year = 2013;
68 static FILE *report_file;
74 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
78 void uninit_opts(void)
81 sws_freeContext(sws_opts);
85 av_dict_free(&swr_opts);
86 av_dict_free(&format_opts);
87 av_dict_free(&codec_opts);
88 av_dict_free(&resample_opts);
91 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
93 vfprintf(stdout, fmt, vl);
96 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
100 static int print_prefix = 1;
103 av_log_default_callback(ptr, level, fmt, vl);
104 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
106 fputs(line, report_file);
110 double parse_number_or_die(const char *context, const char *numstr, int type,
111 double min, double max)
115 double d = av_strtod(numstr, &tail);
117 error = "Expected number for %s but found: %s\n";
118 else if (d < min || d > max)
119 error = "The value for %s was %s which is not within %f - %f\n";
120 else if (type == OPT_INT64 && (int64_t)d != d)
121 error = "Expected int64 for %s but found %s\n";
122 else if (type == OPT_INT && (int)d != d)
123 error = "Expected int for %s but found %s\n";
126 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
131 int64_t parse_time_or_die(const char *context, const char *timestr,
135 if (av_parse_time(&us, timestr, is_duration) < 0) {
136 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
137 is_duration ? "duration" : "date", context, timestr);
143 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
144 int rej_flags, int alt_flags)
150 for (po = options; po->name != NULL; po++) {
153 if (((po->flags & req_flags) != req_flags) ||
154 (alt_flags && !(po->flags & alt_flags)) ||
155 (po->flags & rej_flags))
162 av_strlcpy(buf, po->name, sizeof(buf));
164 av_strlcat(buf, " ", sizeof(buf));
165 av_strlcat(buf, po->argname, sizeof(buf));
167 printf("-%-17s %s\n", buf, po->help);
172 void show_help_children(const AVClass *class, int flags)
174 const AVClass *child = NULL;
176 av_opt_show2(&class, NULL, flags, 0);
180 while (child = av_opt_child_class_next(class, child))
181 show_help_children(child, flags);
184 static const OptionDef *find_option(const OptionDef *po, const char *name)
186 const char *p = strchr(name, ':');
187 int len = p ? p - name : strlen(name);
189 while (po->name != NULL) {
190 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
197 #if HAVE_COMMANDLINETOARGVW
199 #include <shellapi.h>
200 /* Will be leaked on exit */
201 static char** win32_argv_utf8 = NULL;
202 static int win32_argc = 0;
205 * Prepare command line arguments for executable.
206 * For Windows - perform wide-char to UTF-8 conversion.
207 * Input arguments should be main() function arguments.
208 * @param argc_ptr Arguments number (including executable)
209 * @param argv_ptr Arguments list.
211 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
215 int i, buffsize = 0, offset = 0;
217 if (win32_argv_utf8) {
218 *argc_ptr = win32_argc;
219 *argv_ptr = win32_argv_utf8;
224 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
225 if (win32_argc <= 0 || !argv_w)
228 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
229 for (i = 0; i < win32_argc; i++)
230 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
231 NULL, 0, NULL, NULL);
233 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
234 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
235 if (win32_argv_utf8 == NULL) {
240 for (i = 0; i < win32_argc; i++) {
241 win32_argv_utf8[i] = &argstr_flat[offset];
242 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
243 &argstr_flat[offset],
244 buffsize - offset, NULL, NULL);
246 win32_argv_utf8[i] = NULL;
249 *argc_ptr = win32_argc;
250 *argv_ptr = win32_argv_utf8;
253 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
257 #endif /* HAVE_COMMANDLINETOARGVW */
259 static int write_option(void *optctx, const OptionDef *po, const char *opt,
262 /* new-style options contain an offset into optctx, old-style address of
264 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
265 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
268 if (po->flags & OPT_SPEC) {
269 SpecifierOpt **so = dst;
270 char *p = strchr(opt, ':');
272 dstcount = (int *)(so + 1);
273 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
274 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
275 dst = &(*so)[*dstcount - 1].u;
278 if (po->flags & OPT_STRING) {
280 str = av_strdup(arg);
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);
296 av_log(NULL, AV_LOG_ERROR,
297 "Failed to set value '%s' for option '%s': %s\n",
298 arg, opt, av_err2str(ret));
302 if (po->flags & OPT_EXIT)
308 int parse_option(void *optctx, const char *opt, const char *arg,
309 const OptionDef *options)
314 po = find_option(options, opt);
315 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
316 /* handle 'no' bool option */
317 po = find_option(options, opt + 2);
318 if ((po->name && (po->flags & OPT_BOOL)))
320 } else if (po->flags & OPT_BOOL)
324 po = find_option(options, "default");
326 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
327 return AVERROR(EINVAL);
329 if (po->flags & HAS_ARG && !arg) {
330 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
331 return AVERROR(EINVAL);
334 ret = write_option(optctx, po, opt, arg);
338 return !!(po->flags & HAS_ARG);
341 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
342 void (*parse_arg_function)(void *, const char*))
345 int optindex, handleoptions = 1, ret;
347 /* perform system-dependent conversions for arguments list */
348 prepare_app_arguments(&argc, &argv);
352 while (optindex < argc) {
353 opt = argv[optindex++];
355 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
356 if (opt[1] == '-' && opt[2] == '\0') {
362 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
366 if (parse_arg_function)
367 parse_arg_function(optctx, opt);
372 int parse_optgroup(void *optctx, OptionGroup *g)
376 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
377 g->group_def->name, g->arg);
379 for (i = 0; i < g->nb_opts; i++) {
380 Option *o = &g->opts[i];
382 if (g->group_def->flags &&
383 !(g->group_def->flags & o->opt->flags)) {
384 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
385 "%s %s -- you are trying to apply an input option to an "
386 "output file or vice versa. Move this option before the "
387 "file it belongs to.\n", o->key, o->opt->help,
388 g->group_def->name, g->arg);
389 return AVERROR(EINVAL);
392 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
393 o->key, o->opt->help, o->val);
395 ret = write_option(optctx, o->opt, o->key, o->val);
400 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
405 int locate_option(int argc, char **argv, const OptionDef *options,
411 for (i = 1; i < argc; i++) {
412 const char *cur_opt = argv[i];
414 if (*cur_opt++ != '-')
417 po = find_option(options, cur_opt);
418 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
419 po = find_option(options, cur_opt + 2);
421 if ((!po->name && !strcmp(cur_opt, optname)) ||
422 (po->name && !strcmp(optname, po->name)))
425 if (po->flags & HAS_ARG)
431 static void dump_argument(const char *a)
433 const unsigned char *p;
436 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
437 *p == '_' || (*p >= 'a' && *p <= 'z')))
440 fputs(a, report_file);
443 fputc('"', report_file);
444 for (p = a; *p; p++) {
445 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
446 fprintf(report_file, "\\%c", *p);
447 else if (*p < ' ' || *p > '~')
448 fprintf(report_file, "\\x%02x", *p);
450 fputc(*p, report_file);
452 fputc('"', report_file);
455 void parse_loglevel(int argc, char **argv, const OptionDef *options)
457 int idx = locate_option(argc, argv, options, "loglevel");
460 idx = locate_option(argc, argv, options, "v");
461 if (idx && argv[idx + 1])
462 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
463 idx = locate_option(argc, argv, options, "report");
464 if ((env = getenv("FFREPORT")) || idx) {
468 fprintf(report_file, "Command line:\n");
469 for (i = 0; i < argc; i++) {
470 dump_argument(argv[i]);
471 fputc(i < argc - 1 ? ' ' : '\n', report_file);
478 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
479 int opt_default(void *optctx, const char *opt, const char *arg)
483 char opt_stripped[128];
485 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
486 #if CONFIG_AVRESAMPLE
487 const AVClass *rc = avresample_get_class();
489 const AVClass *sc, *swr_class;
491 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
492 av_log_set_level(AV_LOG_DEBUG);
494 if (!(p = strchr(opt, ':')))
495 p = opt + strlen(opt);
496 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
498 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
499 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
500 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
501 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
502 av_dict_set(&codec_opts, opt, arg, FLAGS);
505 if ((o = av_opt_find(&fc, opt, NULL, 0,
506 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
507 av_dict_set(&format_opts, opt, arg, FLAGS);
509 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
513 sc = sws_get_class();
514 if (!consumed && av_opt_find(&sc, opt, NULL, 0,
515 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
516 // XXX we only support sws_flags, not arbitrary sws options
517 int ret = av_opt_set(sws_opts, opt, arg, 0);
519 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
525 #if CONFIG_SWRESAMPLE
526 swr_class = swr_get_class();
527 if (!consumed && (o=av_opt_find(&swr_class, opt, NULL, 0,
528 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
529 struct SwrContext *swr = swr_alloc();
530 int ret = av_opt_set(swr, opt, arg, 0);
533 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
536 av_dict_set(&swr_opts, opt, arg, FLAGS);
540 #if CONFIG_AVRESAMPLE
541 if ((o=av_opt_find(&rc, opt, NULL, 0,
542 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
543 av_dict_set(&resample_opts, opt, arg, FLAGS);
550 av_log(NULL, AV_LOG_ERROR, "Could not find option '%s' in any of the FFmpeg subsystems "
551 "(codec, format, scaler, resampler contexts)\n", opt);
552 return AVERROR_OPTION_NOT_FOUND;
556 * Check whether given option is a group separator.
558 * @return index of the group definition that matched or -1 if none
560 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
565 for (i = 0; i < nb_groups; i++) {
566 const OptionGroupDef *p = &groups[i];
567 if (p->sep && !strcmp(p->sep, opt))
575 * Finish parsing an option group.
577 * @param group_idx which group definition should this group belong to
578 * @param arg argument of the group delimiting option
580 static void finish_group(OptionParseContext *octx, int group_idx,
583 OptionGroupList *l = &octx->groups[group_idx];
586 GROW_ARRAY(l->groups, l->nb_groups);
587 g = &l->groups[l->nb_groups - 1];
589 *g = octx->cur_group;
591 g->group_def = l->group_def;
593 g->sws_opts = sws_opts;
595 g->swr_opts = swr_opts;
596 g->codec_opts = codec_opts;
597 g->format_opts = format_opts;
598 g->resample_opts = resample_opts;
602 resample_opts = NULL;
609 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
613 * Add an option instance to currently parsed group.
615 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
616 const char *key, const char *val)
618 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
619 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
621 GROW_ARRAY(g->opts, g->nb_opts);
622 g->opts[g->nb_opts - 1].opt = opt;
623 g->opts[g->nb_opts - 1].key = key;
624 g->opts[g->nb_opts - 1].val = val;
627 static void init_parse_context(OptionParseContext *octx,
628 const OptionGroupDef *groups, int nb_groups)
630 static const OptionGroupDef global_group = { "global" };
633 memset(octx, 0, sizeof(*octx));
635 octx->nb_groups = nb_groups;
636 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
640 for (i = 0; i < octx->nb_groups; i++)
641 octx->groups[i].group_def = &groups[i];
643 octx->global_opts.group_def = &global_group;
644 octx->global_opts.arg = "";
649 void uninit_parse_context(OptionParseContext *octx)
653 for (i = 0; i < octx->nb_groups; i++) {
654 OptionGroupList *l = &octx->groups[i];
656 for (j = 0; j < l->nb_groups; j++) {
657 av_freep(&l->groups[j].opts);
658 av_dict_free(&l->groups[j].codec_opts);
659 av_dict_free(&l->groups[j].format_opts);
660 av_dict_free(&l->groups[j].resample_opts);
662 sws_freeContext(l->groups[j].sws_opts);
664 av_dict_free(&l->groups[j].swr_opts);
666 av_freep(&l->groups);
668 av_freep(&octx->groups);
670 av_freep(&octx->cur_group.opts);
671 av_freep(&octx->global_opts.opts);
676 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
677 const OptionDef *options,
678 const OptionGroupDef *groups, int nb_groups)
683 /* perform system-dependent conversions for arguments list */
684 prepare_app_arguments(&argc, &argv);
686 init_parse_context(octx, groups, nb_groups);
687 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
689 while (optindex < argc) {
690 const char *opt = argv[optindex++], *arg;
694 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
696 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
700 /* unnamed group separators, e.g. output filename */
701 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
702 finish_group(octx, 0, opt);
703 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
708 #define GET_ARG(arg) \
710 arg = argv[optindex++]; \
712 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
713 return AVERROR(EINVAL); \
717 /* named group separators, e.g. -i */
718 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
720 finish_group(octx, ret, arg);
721 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
722 groups[ret].name, arg);
727 po = find_option(options, opt);
729 if (po->flags & OPT_EXIT) {
730 /* optional argument, e.g. -h */
731 arg = argv[optindex++];
732 } else if (po->flags & HAS_ARG) {
738 add_opt(octx, po, opt, arg);
739 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
740 "argument '%s'.\n", po->name, po->help, arg);
745 if (argv[optindex]) {
746 ret = opt_default(NULL, opt, argv[optindex]);
748 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
749 "argument '%s'.\n", opt, argv[optindex]);
752 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
753 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
754 "with argument '%s'.\n", opt, argv[optindex]);
759 /* boolean -nofoo options */
760 if (opt[0] == 'n' && opt[1] == 'o' &&
761 (po = find_option(options, opt + 2)) &&
762 po->name && po->flags & OPT_BOOL) {
763 add_opt(octx, po, opt, "0");
764 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
765 "argument 0.\n", po->name, po->help);
769 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
770 return AVERROR_OPTION_NOT_FOUND;
773 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
774 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
777 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
782 int opt_loglevel(void *optctx, const char *opt, const char *arg)
784 const struct { const char *name; int level; } log_levels[] = {
785 { "quiet" , AV_LOG_QUIET },
786 { "panic" , AV_LOG_PANIC },
787 { "fatal" , AV_LOG_FATAL },
788 { "error" , AV_LOG_ERROR },
789 { "warning", AV_LOG_WARNING },
790 { "info" , AV_LOG_INFO },
791 { "verbose", AV_LOG_VERBOSE },
792 { "debug" , AV_LOG_DEBUG },
798 tail = strstr(arg, "repeat");
799 av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED);
801 arg += 6 + (arg[6]=='+');
805 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
806 if (!strcmp(log_levels[i].name, arg)) {
807 av_log_set_level(log_levels[i].level);
812 level = strtol(arg, &tail, 10);
814 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
815 "Possible levels are numbers or:\n", arg);
816 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
817 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
820 av_log_set_level(level);
824 static void expand_filename_template(AVBPrint *bp, const char *template,
829 while ((c = *(template++))) {
831 if (!(c = *(template++)))
835 av_bprintf(bp, "%s", program_name);
838 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
839 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
840 tm->tm_hour, tm->tm_min, tm->tm_sec);
843 av_bprint_chars(bp, c, 1);
847 av_bprint_chars(bp, c, 1);
852 static int init_report(const char *env)
854 char *filename_template = NULL;
861 if (report_file) /* already opened */
864 tm = localtime(&now);
866 while (env && *env) {
867 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
869 av_log(NULL, AV_LOG_ERROR,
870 "Failed to parse FFREPORT environment variable: %s\n",
877 if (!strcmp(key, "file")) {
878 av_free(filename_template);
879 filename_template = val;
882 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
888 av_bprint_init(&filename, 0, 1);
889 expand_filename_template(&filename,
890 av_x_if_null(filename_template, "%p-%t.log"), tm);
891 av_free(filename_template);
892 if (!av_bprint_is_complete(&filename)) {
893 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
894 return AVERROR(ENOMEM);
897 report_file = fopen(filename.str, "w");
899 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
900 filename.str, strerror(errno));
901 return AVERROR(errno);
903 av_log_set_callback(log_callback_report);
904 av_log(NULL, AV_LOG_INFO,
905 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
906 "Report written to \"%s\"\n",
908 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
909 tm->tm_hour, tm->tm_min, tm->tm_sec,
911 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
912 av_bprint_finalize(&filename, NULL);
916 int opt_report(const char *opt)
918 return init_report(NULL);
921 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
926 max = strtol(arg, &tail, 10);
928 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
935 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
938 unsigned flags = av_get_cpu_flags();
940 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
943 av_force_cpu_flags(flags);
947 int opt_timelimit(void *optctx, const char *opt, const char *arg)
950 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
951 struct rlimit rl = { lim, lim + 1 };
952 if (setrlimit(RLIMIT_CPU, &rl))
955 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
960 void print_error(const char *filename, int err)
963 const char *errbuf_ptr = errbuf;
965 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
966 errbuf_ptr = strerror(AVUNERROR(err));
967 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
970 static int warned_cfg = 0;
973 #define SHOW_VERSION 2
974 #define SHOW_CONFIG 4
975 #define SHOW_COPYRIGHT 8
977 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
978 if (CONFIG_##LIBNAME) { \
979 const char *indent = flags & INDENT? " " : ""; \
980 if (flags & SHOW_VERSION) { \
981 unsigned int version = libname##_version(); \
982 av_log(NULL, level, \
983 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
985 LIB##LIBNAME##_VERSION_MAJOR, \
986 LIB##LIBNAME##_VERSION_MINOR, \
987 LIB##LIBNAME##_VERSION_MICRO, \
988 version >> 16, version >> 8 & 0xff, version & 0xff); \
990 if (flags & SHOW_CONFIG) { \
991 const char *cfg = libname##_configuration(); \
992 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
994 av_log(NULL, level, \
995 "%sWARNING: library configuration mismatch\n", \
999 av_log(NULL, level, "%s%-11s configuration: %s\n", \
1000 indent, #libname, cfg); \
1005 static void print_all_libs_info(int flags, int level)
1007 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
1008 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
1009 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1010 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1011 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1012 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1013 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
1014 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
1015 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1018 static void print_program_info(int flags, int level)
1020 const char *indent = flags & INDENT? " " : "";
1022 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1023 if (flags & SHOW_COPYRIGHT)
1024 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1025 program_birth_year, this_year);
1026 av_log(NULL, level, "\n");
1027 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
1028 indent, __DATE__, __TIME__, CC_IDENT);
1030 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1033 void show_banner(int argc, char **argv, const OptionDef *options)
1035 int idx = locate_option(argc, argv, options, "version");
1039 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1040 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
1041 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1044 int show_version(void *optctx, const char *opt, const char *arg)
1046 av_log_set_callback(log_callback_help);
1047 print_program_info (0 , AV_LOG_INFO);
1048 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1053 int show_license(void *optctx, const char *opt, const char *arg)
1057 "This version of %s has nonfree parts compiled in.\n"
1058 "Therefore it is not legally redistributable.\n",
1062 "%s is free software; you can redistribute it and/or modify\n"
1063 "it under the terms of the GNU General Public License as published by\n"
1064 "the Free Software Foundation; either version 3 of the License, or\n"
1065 "(at your option) any later version.\n"
1067 "%s is distributed in the hope that it will be useful,\n"
1068 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1069 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1070 "GNU General Public License for more details.\n"
1072 "You should have received a copy of the GNU General Public License\n"
1073 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1074 program_name, program_name, program_name );
1077 "%s is free software; you can redistribute it and/or modify\n"
1078 "it under the terms of the GNU General Public License as published by\n"
1079 "the Free Software Foundation; either version 2 of the License, or\n"
1080 "(at your option) any later version.\n"
1082 "%s is distributed in the hope that it will be useful,\n"
1083 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1084 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1085 "GNU General Public License for more details.\n"
1087 "You should have received a copy of the GNU General Public License\n"
1088 "along with %s; if not, write to the Free Software\n"
1089 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1090 program_name, program_name, program_name );
1093 "%s is free software; you can redistribute it and/or modify\n"
1094 "it under the terms of the GNU Lesser General Public License as published by\n"
1095 "the Free Software Foundation; either version 3 of the License, or\n"
1096 "(at your option) any later version.\n"
1098 "%s is distributed in the hope that it will be useful,\n"
1099 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1100 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1101 "GNU Lesser General Public License for more details.\n"
1103 "You should have received a copy of the GNU Lesser General Public License\n"
1104 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1105 program_name, program_name, program_name );
1108 "%s is free software; you can redistribute it and/or\n"
1109 "modify it under the terms of the GNU Lesser General Public\n"
1110 "License as published by the Free Software Foundation; either\n"
1111 "version 2.1 of the License, or (at your option) any later version.\n"
1113 "%s is distributed in the hope that it will be useful,\n"
1114 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1115 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1116 "Lesser General Public License for more details.\n"
1118 "You should have received a copy of the GNU Lesser General Public\n"
1119 "License along with %s; if not, write to the Free Software\n"
1120 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1121 program_name, program_name, program_name );
1127 int show_formats(void *optctx, const char *opt, const char *arg)
1129 AVInputFormat *ifmt = NULL;
1130 AVOutputFormat *ofmt = NULL;
1131 const char *last_name;
1133 printf("File formats:\n"
1134 " D. = Demuxing supported\n"
1135 " .E = Muxing supported\n"
1141 const char *name = NULL;
1142 const char *long_name = NULL;
1144 while ((ofmt = av_oformat_next(ofmt))) {
1145 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
1146 strcmp(ofmt->name, last_name) > 0) {
1148 long_name = ofmt->long_name;
1152 while ((ifmt = av_iformat_next(ifmt))) {
1153 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
1154 strcmp(ifmt->name, last_name) > 0) {
1156 long_name = ifmt->long_name;
1159 if (name && strcmp(ifmt->name, name) == 0)
1166 printf(" %s%s %-15s %s\n",
1170 long_name ? long_name:" ");
1175 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1176 if (codec->field) { \
1177 const type *p = codec->field; \
1179 printf(" Supported " list_name ":"); \
1180 while (*p != term) { \
1182 printf(" %s", name); \
1188 static void print_codec(const AVCodec *c)
1190 int encoder = av_codec_is_encoder(c);
1192 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1193 c->long_name ? c->long_name : "");
1195 if (c->type == AVMEDIA_TYPE_VIDEO) {
1196 printf(" Threading capabilities: ");
1197 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
1198 CODEC_CAP_SLICE_THREADS)) {
1199 case CODEC_CAP_FRAME_THREADS |
1200 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1201 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1202 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1203 default: printf("no"); break;
1208 if (c->supported_framerates) {
1209 const AVRational *fps = c->supported_framerates;
1211 printf(" Supported framerates:");
1213 printf(" %d/%d", fps->num, fps->den);
1218 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1219 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1220 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1221 GET_SAMPLE_RATE_NAME);
1222 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1223 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1224 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1225 0, GET_CH_LAYOUT_DESC);
1227 if (c->priv_class) {
1228 show_help_children(c->priv_class,
1229 AV_OPT_FLAG_ENCODING_PARAM |
1230 AV_OPT_FLAG_DECODING_PARAM);
1234 static char get_media_type_char(enum AVMediaType type)
1237 case AVMEDIA_TYPE_VIDEO: return 'V';
1238 case AVMEDIA_TYPE_AUDIO: return 'A';
1239 case AVMEDIA_TYPE_DATA: return 'D';
1240 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1241 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1242 default: return '?';
1246 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1249 while ((prev = av_codec_next(prev))) {
1250 if (prev->id == id &&
1251 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1257 static int compare_codec_desc(const void *a, const void *b)
1259 const AVCodecDescriptor * const *da = a;
1260 const AVCodecDescriptor * const *db = b;
1262 return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
1263 strcmp((*da)->name, (*db)->name);
1266 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1268 const AVCodecDescriptor *desc = NULL;
1269 const AVCodecDescriptor **codecs;
1270 unsigned nb_codecs = 0, i = 0;
1272 while ((desc = avcodec_descriptor_next(desc)))
1274 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1275 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1279 while ((desc = avcodec_descriptor_next(desc)))
1281 av_assert0(i == nb_codecs);
1282 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1287 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1289 const AVCodec *codec = NULL;
1291 printf(" (%s: ", encoder ? "encoders" : "decoders");
1293 while ((codec = next_codec_for_id(id, codec, encoder)))
1294 printf("%s ", codec->name);
1299 int show_codecs(void *optctx, const char *opt, const char *arg)
1301 const AVCodecDescriptor **codecs;
1302 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1305 " D..... = Decoding supported\n"
1306 " .E.... = Encoding supported\n"
1307 " ..V... = Video codec\n"
1308 " ..A... = Audio codec\n"
1309 " ..S... = Subtitle codec\n"
1310 " ...I.. = Intra frame-only codec\n"
1311 " ....L. = Lossy compression\n"
1312 " .....S = Lossless compression\n"
1314 for (i = 0; i < nb_codecs; i++) {
1315 const AVCodecDescriptor *desc = codecs[i];
1316 const AVCodec *codec = NULL;
1319 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1320 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1322 printf("%c", get_media_type_char(desc->type));
1323 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1324 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1325 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1327 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1329 /* print decoders/encoders when there's more than one or their
1330 * names are different from codec name */
1331 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1332 if (strcmp(codec->name, desc->name)) {
1333 print_codecs_for_id(desc->id, 0);
1338 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1339 if (strcmp(codec->name, desc->name)) {
1340 print_codecs_for_id(desc->id, 1);
1351 static void print_codecs(int encoder)
1353 const AVCodecDescriptor **codecs;
1354 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1359 " S..... = Subtitle\n"
1360 " .F.... = Frame-level multithreading\n"
1361 " ..S... = Slice-level multithreading\n"
1362 " ...X.. = Codec is experimental\n"
1363 " ....B. = Supports draw_horiz_band\n"
1364 " .....D = Supports direct rendering method 1\n"
1366 encoder ? "Encoders" : "Decoders");
1367 for (i = 0; i < nb_codecs; i++) {
1368 const AVCodecDescriptor *desc = codecs[i];
1369 const AVCodec *codec = NULL;
1371 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1372 printf(" %c", get_media_type_char(desc->type));
1373 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1374 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1375 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1376 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1377 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
1379 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1380 if (strcmp(codec->name, desc->name))
1381 printf(" (codec %s)", desc->name);
1389 int show_decoders(void *optctx, const char *opt, const char *arg)
1395 int show_encoders(void *optctx, const char *opt, const char *arg)
1401 int show_bsfs(void *optctx, const char *opt, const char *arg)
1403 AVBitStreamFilter *bsf = NULL;
1405 printf("Bitstream filters:\n");
1406 while ((bsf = av_bitstream_filter_next(bsf)))
1407 printf("%s\n", bsf->name);
1412 int show_protocols(void *optctx, const char *opt, const char *arg)
1414 void *opaque = NULL;
1417 printf("Supported file protocols:\n"
1419 while ((name = avio_enum_protocols(&opaque, 0)))
1420 printf("%s\n", name);
1421 printf("Output:\n");
1422 while ((name = avio_enum_protocols(&opaque, 1)))
1423 printf("%s\n", name);
1427 int show_filters(void *optctx, const char *opt, const char *arg)
1429 AVFilter av_unused(**filter) = NULL;
1430 char descr[64], *descr_cur;
1432 const AVFilterPad *pad;
1434 printf("Filters:\n");
1436 while ((filter = av_filter_next(filter)) && *filter) {
1438 for (i = 0; i < 2; i++) {
1440 *(descr_cur++) = '-';
1441 *(descr_cur++) = '>';
1443 pad = i ? (*filter)->outputs : (*filter)->inputs;
1444 for (j = 0; pad && pad[j].name; j++) {
1445 if (descr_cur >= descr + sizeof(descr) - 4)
1447 *(descr_cur++) = get_media_type_char(pad[j].type);
1450 *(descr_cur++) = '|';
1453 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
1459 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1461 const AVPixFmtDescriptor *pix_desc = NULL;
1463 printf("Pixel formats:\n"
1464 "I.... = Supported Input format for conversion\n"
1465 ".O... = Supported Output format for conversion\n"
1466 "..H.. = Hardware accelerated format\n"
1467 "...P. = Paletted format\n"
1468 "....B = Bitstream format\n"
1469 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1473 # define sws_isSupportedInput(x) 0
1474 # define sws_isSupportedOutput(x) 0
1477 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1478 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1479 printf("%c%c%c%c%c %-16s %d %2d\n",
1480 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1481 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1482 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
1483 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
1484 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
1486 pix_desc->nb_components,
1487 av_get_bits_per_pixel(pix_desc));
1492 int show_layouts(void *optctx, const char *opt, const char *arg)
1496 const char *name, *descr;
1498 printf("Individual channels:\n"
1499 "NAME DESCRIPTION\n");
1500 for (i = 0; i < 63; i++) {
1501 name = av_get_channel_name((uint64_t)1 << i);
1504 descr = av_get_channel_description((uint64_t)1 << i);
1505 printf("%-12s%s\n", name, descr);
1507 printf("\nStandard channel layouts:\n"
1508 "NAME DECOMPOSITION\n");
1509 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1511 printf("%-12s", name);
1512 for (j = 1; j; j <<= 1)
1514 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1521 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1525 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1526 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1530 static void show_help_codec(const char *name, int encoder)
1532 const AVCodecDescriptor *desc;
1533 const AVCodec *codec;
1536 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1540 codec = encoder ? avcodec_find_encoder_by_name(name) :
1541 avcodec_find_decoder_by_name(name);
1545 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1548 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1554 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1555 "but no %s for it are available. FFmpeg might need to be "
1556 "recompiled with additional external libraries.\n",
1557 name, encoder ? "encoders" : "decoders");
1560 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1565 static void show_help_demuxer(const char *name)
1567 const AVInputFormat *fmt = av_find_input_format(name);
1570 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1574 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1576 if (fmt->extensions)
1577 printf(" Common extensions: %s.\n", fmt->extensions);
1579 if (fmt->priv_class)
1580 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1583 static void show_help_muxer(const char *name)
1585 const AVCodecDescriptor *desc;
1586 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1589 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1593 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1595 if (fmt->extensions)
1596 printf(" Common extensions: %s.\n", fmt->extensions);
1598 printf(" Mime type: %s.\n", fmt->mime_type);
1599 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1600 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1601 printf(" Default video codec: %s.\n", desc->name);
1603 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1604 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1605 printf(" Default audio codec: %s.\n", desc->name);
1607 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1608 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1609 printf(" Default subtitle codec: %s.\n", desc->name);
1612 if (fmt->priv_class)
1613 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1616 static void show_help_filter(const char *name)
1618 const AVFilter *filter;
1621 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1624 filter = avfilter_get_by_name(name);
1626 av_log(NULL, AV_LOG_ERROR, "Filter '%s' not found.\n", name);
1629 printf("Filter %s\n", filter->name);
1630 if (filter->description)
1631 printf(" %s\n", filter->description);
1632 if (filter->priv_class)
1633 show_help_children(filter->priv_class, AV_OPT_FLAG_FILTERING_PARAM);
1635 printf("No AVOption available\n");
1638 int show_help(void *optctx, const char *opt, const char *arg)
1641 av_log_set_callback(log_callback_help);
1643 topic = av_strdup(arg ? arg : "");
1644 par = strchr(topic, '=');
1649 show_help_default(topic, par);
1650 } else if (!strcmp(topic, "decoder")) {
1651 show_help_codec(par, 0);
1652 } else if (!strcmp(topic, "encoder")) {
1653 show_help_codec(par, 1);
1654 } else if (!strcmp(topic, "demuxer")) {
1655 show_help_demuxer(par);
1656 } else if (!strcmp(topic, "muxer")) {
1657 show_help_muxer(par);
1658 } else if (!strcmp(topic, "filter")) {
1659 show_help_filter(par);
1661 show_help_default(topic, par);
1668 int read_yesno(void)
1671 int yesno = (av_toupper(c) == 'Y');
1673 while (c != '\n' && c != EOF)
1679 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1682 FILE *f = fopen(filename, "rb");
1685 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1687 return AVERROR(errno);
1689 fseek(f, 0, SEEK_END);
1691 fseek(f, 0, SEEK_SET);
1692 if (*size == (size_t)-1) {
1693 av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1695 return AVERROR(errno);
1697 *bufptr = av_malloc(*size + 1);
1699 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1701 return AVERROR(ENOMEM);
1703 ret = fread(*bufptr, 1, *size, f);
1707 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1708 filename, strerror(errno));
1709 ret = AVERROR(errno);
1714 (*bufptr)[(*size)++] = '\0';
1721 FILE *get_preset_file(char *filename, size_t filename_size,
1722 const char *preset_name, int is_path,
1723 const char *codec_name)
1727 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1732 av_strlcpy(filename, preset_name, filename_size);
1733 f = fopen(filename, "r");
1736 char datadir[MAX_PATH], *ls;
1739 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1741 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1742 if (*ls == '\\') *ls = '/';
1744 if (ls = strrchr(datadir, '/'))
1747 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1752 for (i = 0; i < 3 && !f; i++) {
1755 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1756 i != 1 ? "" : "/.ffmpeg", preset_name);
1757 f = fopen(filename, "r");
1758 if (!f && codec_name) {
1759 snprintf(filename, filename_size,
1760 "%s%s/%s-%s.ffpreset",
1761 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1763 f = fopen(filename, "r");
1771 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1773 int ret = avformat_match_stream_specifier(s, st, spec);
1775 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1779 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1780 AVFormatContext *s, AVStream *st, AVCodec *codec)
1782 AVDictionary *ret = NULL;
1783 AVDictionaryEntry *t = NULL;
1784 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1785 : AV_OPT_FLAG_DECODING_PARAM;
1787 const AVClass *cc = avcodec_get_class();
1790 codec = s->oformat ? avcodec_find_encoder(codec_id)
1791 : avcodec_find_decoder(codec_id);
1793 switch (st->codec->codec_type) {
1794 case AVMEDIA_TYPE_VIDEO:
1796 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1798 case AVMEDIA_TYPE_AUDIO:
1800 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1802 case AVMEDIA_TYPE_SUBTITLE:
1804 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1808 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1809 char *p = strchr(t->key, ':');
1811 /* check stream specification in opt name */
1813 switch (check_stream_specifier(s, st, p + 1)) {
1814 case 1: *p = 0; break;
1816 default: return NULL;
1819 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1820 (codec && codec->priv_class &&
1821 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1822 AV_OPT_SEARCH_FAKE_OBJ)))
1823 av_dict_set(&ret, t->key, t->value, 0);
1824 else if (t->key[0] == prefix &&
1825 av_opt_find(&cc, t->key + 1, NULL, flags,
1826 AV_OPT_SEARCH_FAKE_OBJ))
1827 av_dict_set(&ret, t->key + 1, t->value, 0);
1835 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1836 AVDictionary *codec_opts)
1839 AVDictionary **opts;
1843 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1845 av_log(NULL, AV_LOG_ERROR,
1846 "Could not alloc memory for stream options.\n");
1849 for (i = 0; i < s->nb_streams; i++)
1850 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1851 s, s->streams[i], NULL);
1855 void *grow_array(void *array, int elem_size, int *size, int new_size)
1857 if (new_size >= INT_MAX / elem_size) {
1858 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1861 if (*size < new_size) {
1862 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1864 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1867 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);