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 #include "libavutil/opencl.h"
64 static int init_report(const char *env);
66 struct SwsContext *sws_opts;
67 AVDictionary *swr_opts;
68 AVDictionary *format_opts, *codec_opts, *resample_opts;
70 const int this_year = 2013;
72 static FILE *report_file;
78 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
82 void uninit_opts(void)
85 sws_freeContext(sws_opts);
89 av_dict_free(&swr_opts);
90 av_dict_free(&format_opts);
91 av_dict_free(&codec_opts);
92 av_dict_free(&resample_opts);
95 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
97 vfprintf(stdout, fmt, vl);
100 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
104 static int print_prefix = 1;
107 av_log_default_callback(ptr, level, fmt, vl);
108 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
110 fputs(line, report_file);
114 double parse_number_or_die(const char *context, const char *numstr, int type,
115 double min, double max)
119 double d = av_strtod(numstr, &tail);
121 error = "Expected number for %s but found: %s\n";
122 else if (d < min || d > max)
123 error = "The value for %s was %s which is not within %f - %f\n";
124 else if (type == OPT_INT64 && (int64_t)d != d)
125 error = "Expected int64 for %s but found %s\n";
126 else if (type == OPT_INT && (int)d != d)
127 error = "Expected int for %s but found %s\n";
130 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
135 int64_t parse_time_or_die(const char *context, const char *timestr,
139 if (av_parse_time(&us, timestr, is_duration) < 0) {
140 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
141 is_duration ? "duration" : "date", context, timestr);
147 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
148 int rej_flags, int alt_flags)
154 for (po = options; po->name != NULL; po++) {
157 if (((po->flags & req_flags) != req_flags) ||
158 (alt_flags && !(po->flags & alt_flags)) ||
159 (po->flags & rej_flags))
166 av_strlcpy(buf, po->name, sizeof(buf));
168 av_strlcat(buf, " ", sizeof(buf));
169 av_strlcat(buf, po->argname, sizeof(buf));
171 printf("-%-17s %s\n", buf, po->help);
176 void show_help_children(const AVClass *class, int flags)
178 const AVClass *child = NULL;
180 av_opt_show2(&class, NULL, flags, 0);
184 while (child = av_opt_child_class_next(class, child))
185 show_help_children(child, flags);
188 static const OptionDef *find_option(const OptionDef *po, const char *name)
190 const char *p = strchr(name, ':');
191 int len = p ? p - name : strlen(name);
193 while (po->name != NULL) {
194 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
201 #if HAVE_COMMANDLINETOARGVW
203 #include <shellapi.h>
204 /* Will be leaked on exit */
205 static char** win32_argv_utf8 = NULL;
206 static int win32_argc = 0;
209 * Prepare command line arguments for executable.
210 * For Windows - perform wide-char to UTF-8 conversion.
211 * Input arguments should be main() function arguments.
212 * @param argc_ptr Arguments number (including executable)
213 * @param argv_ptr Arguments list.
215 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
219 int i, buffsize = 0, offset = 0;
221 if (win32_argv_utf8) {
222 *argc_ptr = win32_argc;
223 *argv_ptr = win32_argv_utf8;
228 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
229 if (win32_argc <= 0 || !argv_w)
232 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
233 for (i = 0; i < win32_argc; i++)
234 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
235 NULL, 0, NULL, NULL);
237 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
238 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
239 if (win32_argv_utf8 == NULL) {
244 for (i = 0; i < win32_argc; i++) {
245 win32_argv_utf8[i] = &argstr_flat[offset];
246 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
247 &argstr_flat[offset],
248 buffsize - offset, NULL, NULL);
250 win32_argv_utf8[i] = NULL;
253 *argc_ptr = win32_argc;
254 *argv_ptr = win32_argv_utf8;
257 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
261 #endif /* HAVE_COMMANDLINETOARGVW */
263 static int write_option(void *optctx, const OptionDef *po, const char *opt,
266 /* new-style options contain an offset into optctx, old-style address of
268 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
269 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
272 if (po->flags & OPT_SPEC) {
273 SpecifierOpt **so = dst;
274 char *p = strchr(opt, ':');
276 dstcount = (int *)(so + 1);
277 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
278 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
279 dst = &(*so)[*dstcount - 1].u;
282 if (po->flags & OPT_STRING) {
284 str = av_strdup(arg);
287 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
288 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
289 } else if (po->flags & OPT_INT64) {
290 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
291 } else if (po->flags & OPT_TIME) {
292 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
293 } else if (po->flags & OPT_FLOAT) {
294 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
295 } else if (po->flags & OPT_DOUBLE) {
296 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
297 } else if (po->u.func_arg) {
298 int ret = po->u.func_arg(optctx, opt, arg);
300 av_log(NULL, AV_LOG_ERROR,
301 "Failed to set value '%s' for option '%s': %s\n",
302 arg, opt, av_err2str(ret));
306 if (po->flags & OPT_EXIT)
312 int parse_option(void *optctx, const char *opt, const char *arg,
313 const OptionDef *options)
318 po = find_option(options, opt);
319 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
320 /* handle 'no' bool option */
321 po = find_option(options, opt + 2);
322 if ((po->name && (po->flags & OPT_BOOL)))
324 } else if (po->flags & OPT_BOOL)
328 po = find_option(options, "default");
330 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
331 return AVERROR(EINVAL);
333 if (po->flags & HAS_ARG && !arg) {
334 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
335 return AVERROR(EINVAL);
338 ret = write_option(optctx, po, opt, arg);
342 return !!(po->flags & HAS_ARG);
345 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
346 void (*parse_arg_function)(void *, const char*))
349 int optindex, handleoptions = 1, ret;
351 /* perform system-dependent conversions for arguments list */
352 prepare_app_arguments(&argc, &argv);
356 while (optindex < argc) {
357 opt = argv[optindex++];
359 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
360 if (opt[1] == '-' && opt[2] == '\0') {
366 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
370 if (parse_arg_function)
371 parse_arg_function(optctx, opt);
376 int parse_optgroup(void *optctx, OptionGroup *g)
380 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
381 g->group_def->name, g->arg);
383 for (i = 0; i < g->nb_opts; i++) {
384 Option *o = &g->opts[i];
386 if (g->group_def->flags &&
387 !(g->group_def->flags & o->opt->flags)) {
388 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
389 "%s %s -- you are trying to apply an input option to an "
390 "output file or vice versa. Move this option before the "
391 "file it belongs to.\n", o->key, o->opt->help,
392 g->group_def->name, g->arg);
393 return AVERROR(EINVAL);
396 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
397 o->key, o->opt->help, o->val);
399 ret = write_option(optctx, o->opt, o->key, o->val);
404 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
409 int locate_option(int argc, char **argv, const OptionDef *options,
415 for (i = 1; i < argc; i++) {
416 const char *cur_opt = argv[i];
418 if (*cur_opt++ != '-')
421 po = find_option(options, cur_opt);
422 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
423 po = find_option(options, cur_opt + 2);
425 if ((!po->name && !strcmp(cur_opt, optname)) ||
426 (po->name && !strcmp(optname, po->name)))
429 if (po->flags & HAS_ARG)
435 static void dump_argument(const char *a)
437 const unsigned char *p;
440 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
441 *p == '_' || (*p >= 'a' && *p <= 'z')))
444 fputs(a, report_file);
447 fputc('"', report_file);
448 for (p = a; *p; p++) {
449 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
450 fprintf(report_file, "\\%c", *p);
451 else if (*p < ' ' || *p > '~')
452 fprintf(report_file, "\\x%02x", *p);
454 fputc(*p, report_file);
456 fputc('"', report_file);
459 void parse_loglevel(int argc, char **argv, const OptionDef *options)
461 int idx = locate_option(argc, argv, options, "loglevel");
464 idx = locate_option(argc, argv, options, "v");
465 if (idx && argv[idx + 1])
466 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
467 idx = locate_option(argc, argv, options, "report");
468 if ((env = getenv("FFREPORT")) || idx) {
472 fprintf(report_file, "Command line:\n");
473 for (i = 0; i < argc; i++) {
474 dump_argument(argv[i]);
475 fputc(i < argc - 1 ? ' ' : '\n', report_file);
482 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
483 int opt_default(void *optctx, const char *opt, const char *arg)
487 char opt_stripped[128];
489 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
490 #if CONFIG_AVRESAMPLE
491 const AVClass *rc = avresample_get_class();
493 const AVClass *sc, *swr_class;
495 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
496 av_log_set_level(AV_LOG_DEBUG);
498 if (!(p = strchr(opt, ':')))
499 p = opt + strlen(opt);
500 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
502 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
503 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
504 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
505 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
506 av_dict_set(&codec_opts, opt, arg, FLAGS);
509 if ((o = av_opt_find(&fc, opt, NULL, 0,
510 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
511 av_dict_set(&format_opts, opt, arg, FLAGS);
513 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
517 sc = sws_get_class();
518 if (!consumed && av_opt_find(&sc, opt, NULL, 0,
519 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
520 // XXX we only support sws_flags, not arbitrary sws options
521 int ret = av_opt_set(sws_opts, opt, arg, 0);
523 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
529 #if CONFIG_SWRESAMPLE
530 swr_class = swr_get_class();
531 if (!consumed && (o=av_opt_find(&swr_class, opt, NULL, 0,
532 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
533 struct SwrContext *swr = swr_alloc();
534 int ret = av_opt_set(swr, opt, arg, 0);
537 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
540 av_dict_set(&swr_opts, opt, arg, FLAGS);
544 #if CONFIG_AVRESAMPLE
545 if ((o=av_opt_find(&rc, opt, NULL, 0,
546 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
547 av_dict_set(&resample_opts, opt, arg, FLAGS);
554 return AVERROR_OPTION_NOT_FOUND;
558 * Check whether given option is a group separator.
560 * @return index of the group definition that matched or -1 if none
562 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
567 for (i = 0; i < nb_groups; i++) {
568 const OptionGroupDef *p = &groups[i];
569 if (p->sep && !strcmp(p->sep, opt))
577 * Finish parsing an option group.
579 * @param group_idx which group definition should this group belong to
580 * @param arg argument of the group delimiting option
582 static void finish_group(OptionParseContext *octx, int group_idx,
585 OptionGroupList *l = &octx->groups[group_idx];
588 GROW_ARRAY(l->groups, l->nb_groups);
589 g = &l->groups[l->nb_groups - 1];
591 *g = octx->cur_group;
593 g->group_def = l->group_def;
595 g->sws_opts = sws_opts;
597 g->swr_opts = swr_opts;
598 g->codec_opts = codec_opts;
599 g->format_opts = format_opts;
600 g->resample_opts = resample_opts;
604 resample_opts = NULL;
611 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
615 * Add an option instance to currently parsed group.
617 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
618 const char *key, const char *val)
620 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
621 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
623 GROW_ARRAY(g->opts, g->nb_opts);
624 g->opts[g->nb_opts - 1].opt = opt;
625 g->opts[g->nb_opts - 1].key = key;
626 g->opts[g->nb_opts - 1].val = val;
629 static void init_parse_context(OptionParseContext *octx,
630 const OptionGroupDef *groups, int nb_groups)
632 static const OptionGroupDef global_group = { "global" };
635 memset(octx, 0, sizeof(*octx));
637 octx->nb_groups = nb_groups;
638 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
642 for (i = 0; i < octx->nb_groups; i++)
643 octx->groups[i].group_def = &groups[i];
645 octx->global_opts.group_def = &global_group;
646 octx->global_opts.arg = "";
651 void uninit_parse_context(OptionParseContext *octx)
655 for (i = 0; i < octx->nb_groups; i++) {
656 OptionGroupList *l = &octx->groups[i];
658 for (j = 0; j < l->nb_groups; j++) {
659 av_freep(&l->groups[j].opts);
660 av_dict_free(&l->groups[j].codec_opts);
661 av_dict_free(&l->groups[j].format_opts);
662 av_dict_free(&l->groups[j].resample_opts);
664 sws_freeContext(l->groups[j].sws_opts);
666 av_dict_free(&l->groups[j].swr_opts);
668 av_freep(&l->groups);
670 av_freep(&octx->groups);
672 av_freep(&octx->cur_group.opts);
673 av_freep(&octx->global_opts.opts);
678 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
679 const OptionDef *options,
680 const OptionGroupDef *groups, int nb_groups)
685 /* perform system-dependent conversions for arguments list */
686 prepare_app_arguments(&argc, &argv);
688 init_parse_context(octx, groups, nb_groups);
689 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
691 while (optindex < argc) {
692 const char *opt = argv[optindex++], *arg;
696 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
698 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
702 /* unnamed group separators, e.g. output filename */
703 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
704 finish_group(octx, 0, opt);
705 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
710 #define GET_ARG(arg) \
712 arg = argv[optindex++]; \
714 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
715 return AVERROR(EINVAL); \
719 /* named group separators, e.g. -i */
720 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
722 finish_group(octx, ret, arg);
723 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
724 groups[ret].name, arg);
729 po = find_option(options, opt);
731 if (po->flags & OPT_EXIT) {
732 /* optional argument, e.g. -h */
733 arg = argv[optindex++];
734 } else if (po->flags & HAS_ARG) {
740 add_opt(octx, po, opt, arg);
741 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
742 "argument '%s'.\n", po->name, po->help, arg);
747 if (argv[optindex]) {
748 ret = opt_default(NULL, opt, argv[optindex]);
750 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
751 "argument '%s'.\n", opt, argv[optindex]);
754 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
755 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
756 "with argument '%s'.\n", opt, argv[optindex]);
761 /* boolean -nofoo options */
762 if (opt[0] == 'n' && opt[1] == 'o' &&
763 (po = find_option(options, opt + 2)) &&
764 po->name && po->flags & OPT_BOOL) {
765 add_opt(octx, po, opt, "0");
766 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
767 "argument 0.\n", po->name, po->help);
771 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
772 return AVERROR_OPTION_NOT_FOUND;
775 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
776 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
779 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
784 int opt_loglevel(void *optctx, const char *opt, const char *arg)
786 const struct { const char *name; int level; } log_levels[] = {
787 { "quiet" , AV_LOG_QUIET },
788 { "panic" , AV_LOG_PANIC },
789 { "fatal" , AV_LOG_FATAL },
790 { "error" , AV_LOG_ERROR },
791 { "warning", AV_LOG_WARNING },
792 { "info" , AV_LOG_INFO },
793 { "verbose", AV_LOG_VERBOSE },
794 { "debug" , AV_LOG_DEBUG },
800 tail = strstr(arg, "repeat");
801 av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED);
803 arg += 6 + (arg[6]=='+');
807 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
808 if (!strcmp(log_levels[i].name, arg)) {
809 av_log_set_level(log_levels[i].level);
814 level = strtol(arg, &tail, 10);
816 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
817 "Possible levels are numbers or:\n", arg);
818 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
819 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
822 av_log_set_level(level);
826 static void expand_filename_template(AVBPrint *bp, const char *template,
831 while ((c = *(template++))) {
833 if (!(c = *(template++)))
837 av_bprintf(bp, "%s", program_name);
840 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
841 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
842 tm->tm_hour, tm->tm_min, tm->tm_sec);
845 av_bprint_chars(bp, c, 1);
849 av_bprint_chars(bp, c, 1);
854 static int init_report(const char *env)
856 char *filename_template = NULL;
863 if (report_file) /* already opened */
866 tm = localtime(&now);
868 while (env && *env) {
869 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
871 av_log(NULL, AV_LOG_ERROR,
872 "Failed to parse FFREPORT environment variable: %s\n",
879 if (!strcmp(key, "file")) {
880 av_free(filename_template);
881 filename_template = val;
884 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
890 av_bprint_init(&filename, 0, 1);
891 expand_filename_template(&filename,
892 av_x_if_null(filename_template, "%p-%t.log"), tm);
893 av_free(filename_template);
894 if (!av_bprint_is_complete(&filename)) {
895 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
896 return AVERROR(ENOMEM);
899 report_file = fopen(filename.str, "w");
901 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
902 filename.str, strerror(errno));
903 return AVERROR(errno);
905 av_log_set_callback(log_callback_report);
906 av_log(NULL, AV_LOG_INFO,
907 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
908 "Report written to \"%s\"\n",
910 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
911 tm->tm_hour, tm->tm_min, tm->tm_sec,
913 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
914 av_bprint_finalize(&filename, NULL);
918 int opt_report(const char *opt)
920 return init_report(NULL);
923 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
928 max = strtol(arg, &tail, 10);
930 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
937 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
940 unsigned flags = av_get_cpu_flags();
942 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
945 av_force_cpu_flags(flags);
949 int opt_timelimit(void *optctx, const char *opt, const char *arg)
952 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
953 struct rlimit rl = { lim, lim + 1 };
954 if (setrlimit(RLIMIT_CPU, &rl))
957 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
963 int opt_opencl(void *optctx, const char *opt, const char *arg)
966 const char *opts = arg;
969 ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
972 ret = av_opencl_set_option(key, value);
982 void print_error(const char *filename, int err)
985 const char *errbuf_ptr = errbuf;
987 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
988 errbuf_ptr = strerror(AVUNERROR(err));
989 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
992 static int warned_cfg = 0;
995 #define SHOW_VERSION 2
996 #define SHOW_CONFIG 4
997 #define SHOW_COPYRIGHT 8
999 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
1000 if (CONFIG_##LIBNAME) { \
1001 const char *indent = flags & INDENT? " " : ""; \
1002 if (flags & SHOW_VERSION) { \
1003 unsigned int version = libname##_version(); \
1004 av_log(NULL, level, \
1005 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
1007 LIB##LIBNAME##_VERSION_MAJOR, \
1008 LIB##LIBNAME##_VERSION_MINOR, \
1009 LIB##LIBNAME##_VERSION_MICRO, \
1010 version >> 16, version >> 8 & 0xff, version & 0xff); \
1012 if (flags & SHOW_CONFIG) { \
1013 const char *cfg = libname##_configuration(); \
1014 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
1015 if (!warned_cfg) { \
1016 av_log(NULL, level, \
1017 "%sWARNING: library configuration mismatch\n", \
1021 av_log(NULL, level, "%s%-11s configuration: %s\n", \
1022 indent, #libname, cfg); \
1027 static void print_all_libs_info(int flags, int level)
1029 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
1030 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
1031 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1032 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1033 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1034 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1035 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
1036 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
1037 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1040 static void print_program_info(int flags, int level)
1042 const char *indent = flags & INDENT? " " : "";
1044 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1045 if (flags & SHOW_COPYRIGHT)
1046 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1047 program_birth_year, this_year);
1048 av_log(NULL, level, "\n");
1049 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
1050 indent, __DATE__, __TIME__, CC_IDENT);
1052 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1055 void show_banner(int argc, char **argv, const OptionDef *options)
1057 int idx = locate_option(argc, argv, options, "version");
1061 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1062 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
1063 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1066 int show_version(void *optctx, const char *opt, const char *arg)
1068 av_log_set_callback(log_callback_help);
1069 print_program_info (0 , AV_LOG_INFO);
1070 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1075 int show_license(void *optctx, const char *opt, const char *arg)
1079 "This version of %s has nonfree parts compiled in.\n"
1080 "Therefore it is not legally redistributable.\n",
1084 "%s is free software; you can redistribute it and/or modify\n"
1085 "it under the terms of the GNU General Public License as published by\n"
1086 "the Free Software Foundation; either version 3 of the License, or\n"
1087 "(at your option) any later version.\n"
1089 "%s is distributed in the hope that it will be useful,\n"
1090 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1091 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1092 "GNU General Public License for more details.\n"
1094 "You should have received a copy of the GNU General Public License\n"
1095 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1096 program_name, program_name, program_name );
1099 "%s is free software; you can redistribute it and/or modify\n"
1100 "it under the terms of the GNU General Public License as published by\n"
1101 "the Free Software Foundation; either version 2 of the License, or\n"
1102 "(at your option) any later version.\n"
1104 "%s is distributed in the hope that it will be useful,\n"
1105 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1106 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1107 "GNU General Public License for more details.\n"
1109 "You should have received a copy of the GNU General Public License\n"
1110 "along with %s; if not, write to the Free Software\n"
1111 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1112 program_name, program_name, program_name );
1115 "%s is free software; you can redistribute it and/or modify\n"
1116 "it under the terms of the GNU Lesser General Public License as published by\n"
1117 "the Free Software Foundation; either version 3 of the License, or\n"
1118 "(at your option) any later version.\n"
1120 "%s is distributed in the hope that it will be useful,\n"
1121 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1122 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1123 "GNU Lesser General Public License for more details.\n"
1125 "You should have received a copy of the GNU Lesser General Public License\n"
1126 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1127 program_name, program_name, program_name );
1130 "%s is free software; you can redistribute it and/or\n"
1131 "modify it under the terms of the GNU Lesser General Public\n"
1132 "License as published by the Free Software Foundation; either\n"
1133 "version 2.1 of the License, or (at your option) any later version.\n"
1135 "%s is distributed in the hope that it will be useful,\n"
1136 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1137 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1138 "Lesser General Public License for more details.\n"
1140 "You should have received a copy of the GNU Lesser General Public\n"
1141 "License along with %s; if not, write to the Free Software\n"
1142 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1143 program_name, program_name, program_name );
1149 int show_formats(void *optctx, const char *opt, const char *arg)
1151 AVInputFormat *ifmt = NULL;
1152 AVOutputFormat *ofmt = NULL;
1153 const char *last_name;
1155 printf("File formats:\n"
1156 " D. = Demuxing supported\n"
1157 " .E = Muxing supported\n"
1163 const char *name = NULL;
1164 const char *long_name = NULL;
1166 while ((ofmt = av_oformat_next(ofmt))) {
1167 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
1168 strcmp(ofmt->name, last_name) > 0) {
1170 long_name = ofmt->long_name;
1174 while ((ifmt = av_iformat_next(ifmt))) {
1175 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
1176 strcmp(ifmt->name, last_name) > 0) {
1178 long_name = ifmt->long_name;
1181 if (name && strcmp(ifmt->name, name) == 0)
1188 printf(" %s%s %-15s %s\n",
1192 long_name ? long_name:" ");
1197 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1198 if (codec->field) { \
1199 const type *p = codec->field; \
1201 printf(" Supported " list_name ":"); \
1202 while (*p != term) { \
1204 printf(" %s", name); \
1210 static void print_codec(const AVCodec *c)
1212 int encoder = av_codec_is_encoder(c);
1214 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1215 c->long_name ? c->long_name : "");
1217 if (c->type == AVMEDIA_TYPE_VIDEO) {
1218 printf(" Threading capabilities: ");
1219 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
1220 CODEC_CAP_SLICE_THREADS)) {
1221 case CODEC_CAP_FRAME_THREADS |
1222 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1223 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1224 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1225 default: printf("no"); break;
1230 if (c->supported_framerates) {
1231 const AVRational *fps = c->supported_framerates;
1233 printf(" Supported framerates:");
1235 printf(" %d/%d", fps->num, fps->den);
1240 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1241 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1242 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1243 GET_SAMPLE_RATE_NAME);
1244 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1245 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1246 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1247 0, GET_CH_LAYOUT_DESC);
1249 if (c->priv_class) {
1250 show_help_children(c->priv_class,
1251 AV_OPT_FLAG_ENCODING_PARAM |
1252 AV_OPT_FLAG_DECODING_PARAM);
1256 static char get_media_type_char(enum AVMediaType type)
1259 case AVMEDIA_TYPE_VIDEO: return 'V';
1260 case AVMEDIA_TYPE_AUDIO: return 'A';
1261 case AVMEDIA_TYPE_DATA: return 'D';
1262 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1263 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1264 default: return '?';
1268 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1271 while ((prev = av_codec_next(prev))) {
1272 if (prev->id == id &&
1273 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1279 static int compare_codec_desc(const void *a, const void *b)
1281 const AVCodecDescriptor * const *da = a;
1282 const AVCodecDescriptor * const *db = b;
1284 return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
1285 strcmp((*da)->name, (*db)->name);
1288 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1290 const AVCodecDescriptor *desc = NULL;
1291 const AVCodecDescriptor **codecs;
1292 unsigned nb_codecs = 0, i = 0;
1294 while ((desc = avcodec_descriptor_next(desc)))
1296 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1297 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1301 while ((desc = avcodec_descriptor_next(desc)))
1303 av_assert0(i == nb_codecs);
1304 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1309 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1311 const AVCodec *codec = NULL;
1313 printf(" (%s: ", encoder ? "encoders" : "decoders");
1315 while ((codec = next_codec_for_id(id, codec, encoder)))
1316 printf("%s ", codec->name);
1321 int show_codecs(void *optctx, const char *opt, const char *arg)
1323 const AVCodecDescriptor **codecs;
1324 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1327 " D..... = Decoding supported\n"
1328 " .E.... = Encoding supported\n"
1329 " ..V... = Video codec\n"
1330 " ..A... = Audio codec\n"
1331 " ..S... = Subtitle codec\n"
1332 " ...I.. = Intra frame-only codec\n"
1333 " ....L. = Lossy compression\n"
1334 " .....S = Lossless compression\n"
1336 for (i = 0; i < nb_codecs; i++) {
1337 const AVCodecDescriptor *desc = codecs[i];
1338 const AVCodec *codec = NULL;
1341 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1342 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1344 printf("%c", get_media_type_char(desc->type));
1345 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1346 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1347 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1349 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1351 /* print decoders/encoders when there's more than one or their
1352 * names are different from codec name */
1353 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1354 if (strcmp(codec->name, desc->name)) {
1355 print_codecs_for_id(desc->id, 0);
1360 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1361 if (strcmp(codec->name, desc->name)) {
1362 print_codecs_for_id(desc->id, 1);
1373 static void print_codecs(int encoder)
1375 const AVCodecDescriptor **codecs;
1376 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1381 " S..... = Subtitle\n"
1382 " .F.... = Frame-level multithreading\n"
1383 " ..S... = Slice-level multithreading\n"
1384 " ...X.. = Codec is experimental\n"
1385 " ....B. = Supports draw_horiz_band\n"
1386 " .....D = Supports direct rendering method 1\n"
1388 encoder ? "Encoders" : "Decoders");
1389 for (i = 0; i < nb_codecs; i++) {
1390 const AVCodecDescriptor *desc = codecs[i];
1391 const AVCodec *codec = NULL;
1393 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1394 printf(" %c", get_media_type_char(desc->type));
1395 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1396 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1397 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1398 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1399 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
1401 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1402 if (strcmp(codec->name, desc->name))
1403 printf(" (codec %s)", desc->name);
1411 int show_decoders(void *optctx, const char *opt, const char *arg)
1417 int show_encoders(void *optctx, const char *opt, const char *arg)
1423 int show_bsfs(void *optctx, const char *opt, const char *arg)
1425 AVBitStreamFilter *bsf = NULL;
1427 printf("Bitstream filters:\n");
1428 while ((bsf = av_bitstream_filter_next(bsf)))
1429 printf("%s\n", bsf->name);
1434 int show_protocols(void *optctx, const char *opt, const char *arg)
1436 void *opaque = NULL;
1439 printf("Supported file protocols:\n"
1441 while ((name = avio_enum_protocols(&opaque, 0)))
1442 printf("%s\n", name);
1443 printf("Output:\n");
1444 while ((name = avio_enum_protocols(&opaque, 1)))
1445 printf("%s\n", name);
1449 int show_filters(void *optctx, const char *opt, const char *arg)
1451 const AVFilter av_unused(*filter) = NULL;
1452 char descr[64], *descr_cur;
1454 const AVFilterPad *pad;
1457 " T. = Timeline support\n"
1458 " .S = Slice threading\n"
1459 " A = Audio input/output\n"
1460 " V = Video input/output\n"
1461 " N = Dynamic number and/or type of input/output\n"
1462 " | = Source or sink filter\n");
1464 while ((filter = avfilter_next(filter))) {
1466 for (i = 0; i < 2; i++) {
1468 *(descr_cur++) = '-';
1469 *(descr_cur++) = '>';
1471 pad = i ? filter->outputs : filter->inputs;
1472 for (j = 0; pad && pad[j].name; j++) {
1473 if (descr_cur >= descr + sizeof(descr) - 4)
1475 *(descr_cur++) = get_media_type_char(pad[j].type);
1478 *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1479 ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1482 printf(" %c%c %-16s %-10s %s\n",
1483 filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1484 filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
1485 filter->name, descr, filter->description);
1491 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1493 const AVPixFmtDescriptor *pix_desc = NULL;
1495 printf("Pixel formats:\n"
1496 "I.... = Supported Input format for conversion\n"
1497 ".O... = Supported Output format for conversion\n"
1498 "..H.. = Hardware accelerated format\n"
1499 "...P. = Paletted format\n"
1500 "....B = Bitstream format\n"
1501 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1505 # define sws_isSupportedInput(x) 0
1506 # define sws_isSupportedOutput(x) 0
1509 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1510 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1511 printf("%c%c%c%c%c %-16s %d %2d\n",
1512 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1513 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1514 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1515 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1516 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1518 pix_desc->nb_components,
1519 av_get_bits_per_pixel(pix_desc));
1524 int show_layouts(void *optctx, const char *opt, const char *arg)
1528 const char *name, *descr;
1530 printf("Individual channels:\n"
1531 "NAME DESCRIPTION\n");
1532 for (i = 0; i < 63; i++) {
1533 name = av_get_channel_name((uint64_t)1 << i);
1536 descr = av_get_channel_description((uint64_t)1 << i);
1537 printf("%-12s%s\n", name, descr);
1539 printf("\nStandard channel layouts:\n"
1540 "NAME DECOMPOSITION\n");
1541 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1543 printf("%-12s", name);
1544 for (j = 1; j; j <<= 1)
1546 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1553 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1557 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1558 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1562 static void show_help_codec(const char *name, int encoder)
1564 const AVCodecDescriptor *desc;
1565 const AVCodec *codec;
1568 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1572 codec = encoder ? avcodec_find_encoder_by_name(name) :
1573 avcodec_find_decoder_by_name(name);
1577 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1580 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1586 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1587 "but no %s for it are available. FFmpeg might need to be "
1588 "recompiled with additional external libraries.\n",
1589 name, encoder ? "encoders" : "decoders");
1592 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1597 static void show_help_demuxer(const char *name)
1599 const AVInputFormat *fmt = av_find_input_format(name);
1602 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1606 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1608 if (fmt->extensions)
1609 printf(" Common extensions: %s.\n", fmt->extensions);
1611 if (fmt->priv_class)
1612 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1615 static void show_help_muxer(const char *name)
1617 const AVCodecDescriptor *desc;
1618 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1621 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1625 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1627 if (fmt->extensions)
1628 printf(" Common extensions: %s.\n", fmt->extensions);
1630 printf(" Mime type: %s.\n", fmt->mime_type);
1631 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1632 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1633 printf(" Default video codec: %s.\n", desc->name);
1635 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1636 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1637 printf(" Default audio codec: %s.\n", desc->name);
1639 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1640 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1641 printf(" Default subtitle codec: %s.\n", desc->name);
1644 if (fmt->priv_class)
1645 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1649 static void show_help_filter(const char *name)
1652 const AVFilter *f = avfilter_get_by_name(name);
1656 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1659 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1663 printf("Filter %s\n", f->name);
1665 printf(" %s\n", f->description);
1667 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1668 printf(" slice threading supported\n");
1670 printf(" Inputs:\n");
1671 count = avfilter_pad_count(f->inputs);
1672 for (i = 0; i < count; i++) {
1673 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1674 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1676 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1677 printf(" dynamic (depending on the options)\n");
1679 printf(" none (source filter)\n");
1681 printf(" Outputs:\n");
1682 count = avfilter_pad_count(f->outputs);
1683 for (i = 0; i < count; i++) {
1684 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1685 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1687 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1688 printf(" dynamic (depending on the options)\n");
1690 printf(" none (sink filter)\n");
1693 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1694 AV_OPT_FLAG_AUDIO_PARAM);
1695 if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1696 printf("This filter has support for timeline through the 'enable' option.\n");
1698 av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1699 "can not to satisfy request\n");
1704 int show_help(void *optctx, const char *opt, const char *arg)
1707 av_log_set_callback(log_callback_help);
1709 topic = av_strdup(arg ? arg : "");
1710 par = strchr(topic, '=');
1715 show_help_default(topic, par);
1716 } else if (!strcmp(topic, "decoder")) {
1717 show_help_codec(par, 0);
1718 } else if (!strcmp(topic, "encoder")) {
1719 show_help_codec(par, 1);
1720 } else if (!strcmp(topic, "demuxer")) {
1721 show_help_demuxer(par);
1722 } else if (!strcmp(topic, "muxer")) {
1723 show_help_muxer(par);
1725 } else if (!strcmp(topic, "filter")) {
1726 show_help_filter(par);
1729 show_help_default(topic, par);
1736 int read_yesno(void)
1739 int yesno = (av_toupper(c) == 'Y');
1741 while (c != '\n' && c != EOF)
1747 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1750 FILE *f = fopen(filename, "rb");
1753 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1755 return AVERROR(errno);
1757 fseek(f, 0, SEEK_END);
1759 fseek(f, 0, SEEK_SET);
1760 if (*size == (size_t)-1) {
1761 av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1763 return AVERROR(errno);
1765 *bufptr = av_malloc(*size + 1);
1767 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1769 return AVERROR(ENOMEM);
1771 ret = fread(*bufptr, 1, *size, f);
1775 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1776 filename, strerror(errno));
1777 ret = AVERROR(errno);
1782 (*bufptr)[(*size)++] = '\0';
1789 FILE *get_preset_file(char *filename, size_t filename_size,
1790 const char *preset_name, int is_path,
1791 const char *codec_name)
1795 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1800 av_strlcpy(filename, preset_name, filename_size);
1801 f = fopen(filename, "r");
1804 char datadir[MAX_PATH], *ls;
1807 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1809 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1810 if (*ls == '\\') *ls = '/';
1812 if (ls = strrchr(datadir, '/'))
1815 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1820 for (i = 0; i < 3 && !f; i++) {
1823 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1824 i != 1 ? "" : "/.ffmpeg", preset_name);
1825 f = fopen(filename, "r");
1826 if (!f && codec_name) {
1827 snprintf(filename, filename_size,
1828 "%s%s/%s-%s.ffpreset",
1829 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1831 f = fopen(filename, "r");
1839 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1841 int ret = avformat_match_stream_specifier(s, st, spec);
1843 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1847 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1848 AVFormatContext *s, AVStream *st, AVCodec *codec)
1850 AVDictionary *ret = NULL;
1851 AVDictionaryEntry *t = NULL;
1852 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1853 : AV_OPT_FLAG_DECODING_PARAM;
1855 const AVClass *cc = avcodec_get_class();
1858 codec = s->oformat ? avcodec_find_encoder(codec_id)
1859 : avcodec_find_decoder(codec_id);
1861 switch (st->codec->codec_type) {
1862 case AVMEDIA_TYPE_VIDEO:
1864 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1866 case AVMEDIA_TYPE_AUDIO:
1868 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1870 case AVMEDIA_TYPE_SUBTITLE:
1872 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1876 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1877 char *p = strchr(t->key, ':');
1879 /* check stream specification in opt name */
1881 switch (check_stream_specifier(s, st, p + 1)) {
1882 case 1: *p = 0; break;
1884 default: return NULL;
1887 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1888 (codec && codec->priv_class &&
1889 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1890 AV_OPT_SEARCH_FAKE_OBJ)))
1891 av_dict_set(&ret, t->key, t->value, 0);
1892 else if (t->key[0] == prefix &&
1893 av_opt_find(&cc, t->key + 1, NULL, flags,
1894 AV_OPT_SEARCH_FAKE_OBJ))
1895 av_dict_set(&ret, t->key + 1, t->value, 0);
1903 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1904 AVDictionary *codec_opts)
1907 AVDictionary **opts;
1911 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1913 av_log(NULL, AV_LOG_ERROR,
1914 "Could not alloc memory for stream options.\n");
1917 for (i = 0; i < s->nb_streams; i++)
1918 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1919 s, s->streams[i], NULL);
1923 void *grow_array(void *array, int elem_size, int *size, int new_size)
1925 if (new_size >= INT_MAX / elem_size) {
1926 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1929 if (*size < new_size) {
1930 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1932 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1935 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);