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 static void (*program_exit)(int ret);
116 void register_exit(void (*cb)(int ret))
121 void exit_program(int ret)
129 double parse_number_or_die(const char *context, const char *numstr, int type,
130 double min, double max)
134 double d = av_strtod(numstr, &tail);
136 error = "Expected number for %s but found: %s\n";
137 else if (d < min || d > max)
138 error = "The value for %s was %s which is not within %f - %f\n";
139 else if (type == OPT_INT64 && (int64_t)d != d)
140 error = "Expected int64 for %s but found %s\n";
141 else if (type == OPT_INT && (int)d != d)
142 error = "Expected int for %s but found %s\n";
145 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
150 int64_t parse_time_or_die(const char *context, const char *timestr,
154 if (av_parse_time(&us, timestr, is_duration) < 0) {
155 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
156 is_duration ? "duration" : "date", context, timestr);
162 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
163 int rej_flags, int alt_flags)
169 for (po = options; po->name != NULL; po++) {
172 if (((po->flags & req_flags) != req_flags) ||
173 (alt_flags && !(po->flags & alt_flags)) ||
174 (po->flags & rej_flags))
181 av_strlcpy(buf, po->name, sizeof(buf));
183 av_strlcat(buf, " ", sizeof(buf));
184 av_strlcat(buf, po->argname, sizeof(buf));
186 printf("-%-17s %s\n", buf, po->help);
191 void show_help_children(const AVClass *class, int flags)
193 const AVClass *child = NULL;
195 av_opt_show2(&class, NULL, flags, 0);
199 while (child = av_opt_child_class_next(class, child))
200 show_help_children(child, flags);
203 static const OptionDef *find_option(const OptionDef *po, const char *name)
205 const char *p = strchr(name, ':');
206 int len = p ? p - name : strlen(name);
208 while (po->name != NULL) {
209 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
216 #if HAVE_COMMANDLINETOARGVW
218 #include <shellapi.h>
219 /* Will be leaked on exit */
220 static char** win32_argv_utf8 = NULL;
221 static int win32_argc = 0;
224 * Prepare command line arguments for executable.
225 * For Windows - perform wide-char to UTF-8 conversion.
226 * Input arguments should be main() function arguments.
227 * @param argc_ptr Arguments number (including executable)
228 * @param argv_ptr Arguments list.
230 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
234 int i, buffsize = 0, offset = 0;
236 if (win32_argv_utf8) {
237 *argc_ptr = win32_argc;
238 *argv_ptr = win32_argv_utf8;
243 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
244 if (win32_argc <= 0 || !argv_w)
247 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
248 for (i = 0; i < win32_argc; i++)
249 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
250 NULL, 0, NULL, NULL);
252 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
253 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
254 if (win32_argv_utf8 == NULL) {
259 for (i = 0; i < win32_argc; i++) {
260 win32_argv_utf8[i] = &argstr_flat[offset];
261 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
262 &argstr_flat[offset],
263 buffsize - offset, NULL, NULL);
265 win32_argv_utf8[i] = NULL;
268 *argc_ptr = win32_argc;
269 *argv_ptr = win32_argv_utf8;
272 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
276 #endif /* HAVE_COMMANDLINETOARGVW */
278 static int write_option(void *optctx, const OptionDef *po, const char *opt,
281 /* new-style options contain an offset into optctx, old-style address of
283 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
284 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
287 if (po->flags & OPT_SPEC) {
288 SpecifierOpt **so = dst;
289 char *p = strchr(opt, ':');
291 dstcount = (int *)(so + 1);
292 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
293 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
294 dst = &(*so)[*dstcount - 1].u;
297 if (po->flags & OPT_STRING) {
299 str = av_strdup(arg);
302 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
303 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
304 } else if (po->flags & OPT_INT64) {
305 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
306 } else if (po->flags & OPT_TIME) {
307 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
308 } else if (po->flags & OPT_FLOAT) {
309 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
310 } else if (po->flags & OPT_DOUBLE) {
311 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
312 } else if (po->u.func_arg) {
313 int ret = po->u.func_arg(optctx, opt, arg);
315 av_log(NULL, AV_LOG_ERROR,
316 "Failed to set value '%s' for option '%s': %s\n",
317 arg, opt, av_err2str(ret));
321 if (po->flags & OPT_EXIT)
327 int parse_option(void *optctx, const char *opt, const char *arg,
328 const OptionDef *options)
333 po = find_option(options, opt);
334 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
335 /* handle 'no' bool option */
336 po = find_option(options, opt + 2);
337 if ((po->name && (po->flags & OPT_BOOL)))
339 } else if (po->flags & OPT_BOOL)
343 po = find_option(options, "default");
345 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
346 return AVERROR(EINVAL);
348 if (po->flags & HAS_ARG && !arg) {
349 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
350 return AVERROR(EINVAL);
353 ret = write_option(optctx, po, opt, arg);
357 return !!(po->flags & HAS_ARG);
360 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
361 void (*parse_arg_function)(void *, const char*))
364 int optindex, handleoptions = 1, ret;
366 /* perform system-dependent conversions for arguments list */
367 prepare_app_arguments(&argc, &argv);
371 while (optindex < argc) {
372 opt = argv[optindex++];
374 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
375 if (opt[1] == '-' && opt[2] == '\0') {
381 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
385 if (parse_arg_function)
386 parse_arg_function(optctx, opt);
391 int parse_optgroup(void *optctx, OptionGroup *g)
395 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
396 g->group_def->name, g->arg);
398 for (i = 0; i < g->nb_opts; i++) {
399 Option *o = &g->opts[i];
401 if (g->group_def->flags &&
402 !(g->group_def->flags & o->opt->flags)) {
403 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
404 "%s %s -- you are trying to apply an input option to an "
405 "output file or vice versa. Move this option before the "
406 "file it belongs to.\n", o->key, o->opt->help,
407 g->group_def->name, g->arg);
408 return AVERROR(EINVAL);
411 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
412 o->key, o->opt->help, o->val);
414 ret = write_option(optctx, o->opt, o->key, o->val);
419 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
424 int locate_option(int argc, char **argv, const OptionDef *options,
430 for (i = 1; i < argc; i++) {
431 const char *cur_opt = argv[i];
433 if (*cur_opt++ != '-')
436 po = find_option(options, cur_opt);
437 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
438 po = find_option(options, cur_opt + 2);
440 if ((!po->name && !strcmp(cur_opt, optname)) ||
441 (po->name && !strcmp(optname, po->name)))
444 if (po->flags & HAS_ARG)
450 static void dump_argument(const char *a)
452 const unsigned char *p;
455 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
456 *p == '_' || (*p >= 'a' && *p <= 'z')))
459 fputs(a, report_file);
462 fputc('"', report_file);
463 for (p = a; *p; p++) {
464 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
465 fprintf(report_file, "\\%c", *p);
466 else if (*p < ' ' || *p > '~')
467 fprintf(report_file, "\\x%02x", *p);
469 fputc(*p, report_file);
471 fputc('"', report_file);
474 void parse_loglevel(int argc, char **argv, const OptionDef *options)
476 int idx = locate_option(argc, argv, options, "loglevel");
479 idx = locate_option(argc, argv, options, "v");
480 if (idx && argv[idx + 1])
481 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
482 idx = locate_option(argc, argv, options, "report");
483 if ((env = getenv("FFREPORT")) || idx) {
487 fprintf(report_file, "Command line:\n");
488 for (i = 0; i < argc; i++) {
489 dump_argument(argv[i]);
490 fputc(i < argc - 1 ? ' ' : '\n', report_file);
497 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
498 int opt_default(void *optctx, const char *opt, const char *arg)
502 char opt_stripped[128];
504 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
505 #if CONFIG_AVRESAMPLE
506 const AVClass *rc = avresample_get_class();
508 const AVClass *sc, *swr_class;
510 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
511 av_log_set_level(AV_LOG_DEBUG);
513 if (!(p = strchr(opt, ':')))
514 p = opt + strlen(opt);
515 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
517 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
518 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
519 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
520 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
521 av_dict_set(&codec_opts, opt, arg, FLAGS);
524 if ((o = av_opt_find(&fc, opt, NULL, 0,
525 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
526 av_dict_set(&format_opts, opt, arg, FLAGS);
528 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
532 sc = sws_get_class();
533 if (!consumed && av_opt_find(&sc, opt, NULL, 0,
534 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
535 // XXX we only support sws_flags, not arbitrary sws options
536 int ret = av_opt_set(sws_opts, opt, arg, 0);
538 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
544 #if CONFIG_SWRESAMPLE
545 swr_class = swr_get_class();
546 if (!consumed && (o=av_opt_find(&swr_class, opt, NULL, 0,
547 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
548 struct SwrContext *swr = swr_alloc();
549 int ret = av_opt_set(swr, opt, arg, 0);
552 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
555 av_dict_set(&swr_opts, opt, arg, FLAGS);
559 #if CONFIG_AVRESAMPLE
560 if ((o=av_opt_find(&rc, opt, NULL, 0,
561 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
562 av_dict_set(&resample_opts, opt, arg, FLAGS);
569 return AVERROR_OPTION_NOT_FOUND;
573 * Check whether given option is a group separator.
575 * @return index of the group definition that matched or -1 if none
577 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
582 for (i = 0; i < nb_groups; i++) {
583 const OptionGroupDef *p = &groups[i];
584 if (p->sep && !strcmp(p->sep, opt))
592 * Finish parsing an option group.
594 * @param group_idx which group definition should this group belong to
595 * @param arg argument of the group delimiting option
597 static void finish_group(OptionParseContext *octx, int group_idx,
600 OptionGroupList *l = &octx->groups[group_idx];
603 GROW_ARRAY(l->groups, l->nb_groups);
604 g = &l->groups[l->nb_groups - 1];
606 *g = octx->cur_group;
608 g->group_def = l->group_def;
610 g->sws_opts = sws_opts;
612 g->swr_opts = swr_opts;
613 g->codec_opts = codec_opts;
614 g->format_opts = format_opts;
615 g->resample_opts = resample_opts;
619 resample_opts = NULL;
626 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
630 * Add an option instance to currently parsed group.
632 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
633 const char *key, const char *val)
635 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
636 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
638 GROW_ARRAY(g->opts, g->nb_opts);
639 g->opts[g->nb_opts - 1].opt = opt;
640 g->opts[g->nb_opts - 1].key = key;
641 g->opts[g->nb_opts - 1].val = val;
644 static void init_parse_context(OptionParseContext *octx,
645 const OptionGroupDef *groups, int nb_groups)
647 static const OptionGroupDef global_group = { "global" };
650 memset(octx, 0, sizeof(*octx));
652 octx->nb_groups = nb_groups;
653 octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
657 for (i = 0; i < octx->nb_groups; i++)
658 octx->groups[i].group_def = &groups[i];
660 octx->global_opts.group_def = &global_group;
661 octx->global_opts.arg = "";
666 void uninit_parse_context(OptionParseContext *octx)
670 for (i = 0; i < octx->nb_groups; i++) {
671 OptionGroupList *l = &octx->groups[i];
673 for (j = 0; j < l->nb_groups; j++) {
674 av_freep(&l->groups[j].opts);
675 av_dict_free(&l->groups[j].codec_opts);
676 av_dict_free(&l->groups[j].format_opts);
677 av_dict_free(&l->groups[j].resample_opts);
679 sws_freeContext(l->groups[j].sws_opts);
681 av_dict_free(&l->groups[j].swr_opts);
683 av_freep(&l->groups);
685 av_freep(&octx->groups);
687 av_freep(&octx->cur_group.opts);
688 av_freep(&octx->global_opts.opts);
693 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
694 const OptionDef *options,
695 const OptionGroupDef *groups, int nb_groups)
700 /* perform system-dependent conversions for arguments list */
701 prepare_app_arguments(&argc, &argv);
703 init_parse_context(octx, groups, nb_groups);
704 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
706 while (optindex < argc) {
707 const char *opt = argv[optindex++], *arg;
711 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
713 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
717 /* unnamed group separators, e.g. output filename */
718 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
719 finish_group(octx, 0, opt);
720 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
725 #define GET_ARG(arg) \
727 arg = argv[optindex++]; \
729 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
730 return AVERROR(EINVAL); \
734 /* named group separators, e.g. -i */
735 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
737 finish_group(octx, ret, arg);
738 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
739 groups[ret].name, arg);
744 po = find_option(options, opt);
746 if (po->flags & OPT_EXIT) {
747 /* optional argument, e.g. -h */
748 arg = argv[optindex++];
749 } else if (po->flags & HAS_ARG) {
755 add_opt(octx, po, opt, arg);
756 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
757 "argument '%s'.\n", po->name, po->help, arg);
762 if (argv[optindex]) {
763 ret = opt_default(NULL, opt, argv[optindex]);
765 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
766 "argument '%s'.\n", opt, argv[optindex]);
769 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
770 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
771 "with argument '%s'.\n", opt, argv[optindex]);
776 /* boolean -nofoo options */
777 if (opt[0] == 'n' && opt[1] == 'o' &&
778 (po = find_option(options, opt + 2)) &&
779 po->name && po->flags & OPT_BOOL) {
780 add_opt(octx, po, opt, "0");
781 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
782 "argument 0.\n", po->name, po->help);
786 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
787 return AVERROR_OPTION_NOT_FOUND;
790 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
791 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
794 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
799 int opt_loglevel(void *optctx, const char *opt, const char *arg)
801 const struct { const char *name; int level; } log_levels[] = {
802 { "quiet" , AV_LOG_QUIET },
803 { "panic" , AV_LOG_PANIC },
804 { "fatal" , AV_LOG_FATAL },
805 { "error" , AV_LOG_ERROR },
806 { "warning", AV_LOG_WARNING },
807 { "info" , AV_LOG_INFO },
808 { "verbose", AV_LOG_VERBOSE },
809 { "debug" , AV_LOG_DEBUG },
815 tail = strstr(arg, "repeat");
816 av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED);
818 arg += 6 + (arg[6]=='+');
822 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
823 if (!strcmp(log_levels[i].name, arg)) {
824 av_log_set_level(log_levels[i].level);
829 level = strtol(arg, &tail, 10);
831 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
832 "Possible levels are numbers or:\n", arg);
833 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
834 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
837 av_log_set_level(level);
841 static void expand_filename_template(AVBPrint *bp, const char *template,
846 while ((c = *(template++))) {
848 if (!(c = *(template++)))
852 av_bprintf(bp, "%s", program_name);
855 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
856 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
857 tm->tm_hour, tm->tm_min, tm->tm_sec);
860 av_bprint_chars(bp, c, 1);
864 av_bprint_chars(bp, c, 1);
869 static int init_report(const char *env)
871 char *filename_template = NULL;
878 if (report_file) /* already opened */
881 tm = localtime(&now);
883 while (env && *env) {
884 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
886 av_log(NULL, AV_LOG_ERROR,
887 "Failed to parse FFREPORT environment variable: %s\n",
894 if (!strcmp(key, "file")) {
895 av_free(filename_template);
896 filename_template = val;
899 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
905 av_bprint_init(&filename, 0, 1);
906 expand_filename_template(&filename,
907 av_x_if_null(filename_template, "%p-%t.log"), tm);
908 av_free(filename_template);
909 if (!av_bprint_is_complete(&filename)) {
910 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
911 return AVERROR(ENOMEM);
914 report_file = fopen(filename.str, "w");
916 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
917 filename.str, strerror(errno));
918 return AVERROR(errno);
920 av_log_set_callback(log_callback_report);
921 av_log(NULL, AV_LOG_INFO,
922 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
923 "Report written to \"%s\"\n",
925 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
926 tm->tm_hour, tm->tm_min, tm->tm_sec,
928 av_bprint_finalize(&filename, NULL);
932 int opt_report(const char *opt)
934 return init_report(NULL);
937 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
942 max = strtol(arg, &tail, 10);
944 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
951 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
954 unsigned flags = av_get_cpu_flags();
956 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
959 av_force_cpu_flags(flags);
963 int opt_timelimit(void *optctx, const char *opt, const char *arg)
966 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
967 struct rlimit rl = { lim, lim + 1 };
968 if (setrlimit(RLIMIT_CPU, &rl))
971 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
977 int opt_opencl(void *optctx, const char *opt, const char *arg)
980 const char *opts = arg;
983 ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
986 ret = av_opencl_set_option(key, value);
996 void print_error(const char *filename, int err)
999 const char *errbuf_ptr = errbuf;
1001 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1002 errbuf_ptr = strerror(AVUNERROR(err));
1003 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
1006 static int warned_cfg = 0;
1009 #define SHOW_VERSION 2
1010 #define SHOW_CONFIG 4
1011 #define SHOW_COPYRIGHT 8
1013 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
1014 if (CONFIG_##LIBNAME) { \
1015 const char *indent = flags & INDENT? " " : ""; \
1016 if (flags & SHOW_VERSION) { \
1017 unsigned int version = libname##_version(); \
1018 av_log(NULL, level, \
1019 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
1021 LIB##LIBNAME##_VERSION_MAJOR, \
1022 LIB##LIBNAME##_VERSION_MINOR, \
1023 LIB##LIBNAME##_VERSION_MICRO, \
1024 version >> 16, version >> 8 & 0xff, version & 0xff); \
1026 if (flags & SHOW_CONFIG) { \
1027 const char *cfg = libname##_configuration(); \
1028 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
1029 if (!warned_cfg) { \
1030 av_log(NULL, level, \
1031 "%sWARNING: library configuration mismatch\n", \
1035 av_log(NULL, level, "%s%-11s configuration: %s\n", \
1036 indent, #libname, cfg); \
1041 static void print_all_libs_info(int flags, int level)
1043 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
1044 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
1045 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1046 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1047 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1048 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1049 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
1050 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
1051 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1054 static void print_program_info(int flags, int level)
1056 const char *indent = flags & INDENT? " " : "";
1058 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1059 if (flags & SHOW_COPYRIGHT)
1060 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1061 program_birth_year, this_year);
1062 av_log(NULL, level, "\n");
1063 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
1064 indent, __DATE__, __TIME__, CC_IDENT);
1066 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1069 void show_banner(int argc, char **argv, const OptionDef *options)
1071 int idx = locate_option(argc, argv, options, "version");
1075 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1076 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
1077 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1080 int show_version(void *optctx, const char *opt, const char *arg)
1082 av_log_set_callback(log_callback_help);
1083 print_program_info (0 , AV_LOG_INFO);
1084 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1089 int show_license(void *optctx, const char *opt, const char *arg)
1093 "This version of %s has nonfree parts compiled in.\n"
1094 "Therefore it is not legally redistributable.\n",
1098 "%s is free software; you can redistribute it and/or modify\n"
1099 "it under the terms of the GNU General Public License as published by\n"
1100 "the Free Software Foundation; either version 3 of the License, or\n"
1101 "(at your option) any later version.\n"
1103 "%s is distributed in the hope that it will be useful,\n"
1104 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1105 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1106 "GNU General Public License for more details.\n"
1108 "You should have received a copy of the GNU General Public License\n"
1109 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1110 program_name, program_name, program_name );
1113 "%s is free software; you can redistribute it and/or modify\n"
1114 "it under the terms of the GNU General Public License as published by\n"
1115 "the Free Software Foundation; either version 2 of the License, or\n"
1116 "(at your option) any later version.\n"
1118 "%s is distributed in the hope that it will be useful,\n"
1119 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1120 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1121 "GNU General Public License for more details.\n"
1123 "You should have received a copy of the GNU General Public License\n"
1124 "along with %s; if not, write to the Free Software\n"
1125 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1126 program_name, program_name, program_name );
1129 "%s is free software; you can redistribute it and/or modify\n"
1130 "it under the terms of the GNU Lesser General Public License as published by\n"
1131 "the Free Software Foundation; either version 3 of the License, or\n"
1132 "(at your option) any later version.\n"
1134 "%s is distributed in the hope that it will be useful,\n"
1135 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1136 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1137 "GNU Lesser General Public License for more details.\n"
1139 "You should have received a copy of the GNU Lesser General Public License\n"
1140 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1141 program_name, program_name, program_name );
1144 "%s is free software; you can redistribute it and/or\n"
1145 "modify it under the terms of the GNU Lesser General Public\n"
1146 "License as published by the Free Software Foundation; either\n"
1147 "version 2.1 of the License, or (at your option) any later version.\n"
1149 "%s is distributed in the hope that it will be useful,\n"
1150 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1151 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1152 "Lesser General Public License for more details.\n"
1154 "You should have received a copy of the GNU Lesser General Public\n"
1155 "License along with %s; if not, write to the Free Software\n"
1156 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1157 program_name, program_name, program_name );
1163 int show_formats(void *optctx, const char *opt, const char *arg)
1165 AVInputFormat *ifmt = NULL;
1166 AVOutputFormat *ofmt = NULL;
1167 const char *last_name;
1169 printf("File formats:\n"
1170 " D. = Demuxing supported\n"
1171 " .E = Muxing supported\n"
1177 const char *name = NULL;
1178 const char *long_name = NULL;
1180 while ((ofmt = av_oformat_next(ofmt))) {
1181 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
1182 strcmp(ofmt->name, last_name) > 0) {
1184 long_name = ofmt->long_name;
1188 while ((ifmt = av_iformat_next(ifmt))) {
1189 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
1190 strcmp(ifmt->name, last_name) > 0) {
1192 long_name = ifmt->long_name;
1195 if (name && strcmp(ifmt->name, name) == 0)
1202 printf(" %s%s %-15s %s\n",
1206 long_name ? long_name:" ");
1211 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1212 if (codec->field) { \
1213 const type *p = codec->field; \
1215 printf(" Supported " list_name ":"); \
1216 while (*p != term) { \
1218 printf(" %s", name); \
1224 static void print_codec(const AVCodec *c)
1226 int encoder = av_codec_is_encoder(c);
1228 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1229 c->long_name ? c->long_name : "");
1231 if (c->type == AVMEDIA_TYPE_VIDEO ||
1232 c->type == AVMEDIA_TYPE_AUDIO) {
1233 printf(" Threading capabilities: ");
1234 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
1235 CODEC_CAP_SLICE_THREADS)) {
1236 case CODEC_CAP_FRAME_THREADS |
1237 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1238 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1239 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1240 default: printf("no"); break;
1245 if (c->supported_framerates) {
1246 const AVRational *fps = c->supported_framerates;
1248 printf(" Supported framerates:");
1250 printf(" %d/%d", fps->num, fps->den);
1255 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1256 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1257 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1258 GET_SAMPLE_RATE_NAME);
1259 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1260 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1261 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1262 0, GET_CH_LAYOUT_DESC);
1264 if (c->priv_class) {
1265 show_help_children(c->priv_class,
1266 AV_OPT_FLAG_ENCODING_PARAM |
1267 AV_OPT_FLAG_DECODING_PARAM);
1271 static char get_media_type_char(enum AVMediaType type)
1274 case AVMEDIA_TYPE_VIDEO: return 'V';
1275 case AVMEDIA_TYPE_AUDIO: return 'A';
1276 case AVMEDIA_TYPE_DATA: return 'D';
1277 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1278 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1279 default: return '?';
1283 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1286 while ((prev = av_codec_next(prev))) {
1287 if (prev->id == id &&
1288 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1294 static int compare_codec_desc(const void *a, const void *b)
1296 const AVCodecDescriptor * const *da = a;
1297 const AVCodecDescriptor * const *db = b;
1299 return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
1300 strcmp((*da)->name, (*db)->name);
1303 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1305 const AVCodecDescriptor *desc = NULL;
1306 const AVCodecDescriptor **codecs;
1307 unsigned nb_codecs = 0, i = 0;
1309 while ((desc = avcodec_descriptor_next(desc)))
1311 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1312 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1316 while ((desc = avcodec_descriptor_next(desc)))
1318 av_assert0(i == nb_codecs);
1319 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1324 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1326 const AVCodec *codec = NULL;
1328 printf(" (%s: ", encoder ? "encoders" : "decoders");
1330 while ((codec = next_codec_for_id(id, codec, encoder)))
1331 printf("%s ", codec->name);
1336 int show_codecs(void *optctx, const char *opt, const char *arg)
1338 const AVCodecDescriptor **codecs;
1339 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1342 " D..... = Decoding supported\n"
1343 " .E.... = Encoding supported\n"
1344 " ..V... = Video codec\n"
1345 " ..A... = Audio codec\n"
1346 " ..S... = Subtitle codec\n"
1347 " ...I.. = Intra frame-only codec\n"
1348 " ....L. = Lossy compression\n"
1349 " .....S = Lossless compression\n"
1351 for (i = 0; i < nb_codecs; i++) {
1352 const AVCodecDescriptor *desc = codecs[i];
1353 const AVCodec *codec = NULL;
1356 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1357 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1359 printf("%c", get_media_type_char(desc->type));
1360 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1361 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1362 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1364 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1366 /* print decoders/encoders when there's more than one or their
1367 * names are different from codec name */
1368 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1369 if (strcmp(codec->name, desc->name)) {
1370 print_codecs_for_id(desc->id, 0);
1375 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1376 if (strcmp(codec->name, desc->name)) {
1377 print_codecs_for_id(desc->id, 1);
1388 static void print_codecs(int encoder)
1390 const AVCodecDescriptor **codecs;
1391 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1396 " S..... = Subtitle\n"
1397 " .F.... = Frame-level multithreading\n"
1398 " ..S... = Slice-level multithreading\n"
1399 " ...X.. = Codec is experimental\n"
1400 " ....B. = Supports draw_horiz_band\n"
1401 " .....D = Supports direct rendering method 1\n"
1403 encoder ? "Encoders" : "Decoders");
1404 for (i = 0; i < nb_codecs; i++) {
1405 const AVCodecDescriptor *desc = codecs[i];
1406 const AVCodec *codec = NULL;
1408 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1409 printf(" %c", get_media_type_char(desc->type));
1410 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1411 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1412 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1413 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1414 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
1416 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1417 if (strcmp(codec->name, desc->name))
1418 printf(" (codec %s)", desc->name);
1426 int show_decoders(void *optctx, const char *opt, const char *arg)
1432 int show_encoders(void *optctx, const char *opt, const char *arg)
1438 int show_bsfs(void *optctx, const char *opt, const char *arg)
1440 AVBitStreamFilter *bsf = NULL;
1442 printf("Bitstream filters:\n");
1443 while ((bsf = av_bitstream_filter_next(bsf)))
1444 printf("%s\n", bsf->name);
1449 int show_protocols(void *optctx, const char *opt, const char *arg)
1451 void *opaque = NULL;
1454 printf("Supported file protocols:\n"
1456 while ((name = avio_enum_protocols(&opaque, 0)))
1457 printf("%s\n", name);
1458 printf("Output:\n");
1459 while ((name = avio_enum_protocols(&opaque, 1)))
1460 printf("%s\n", name);
1464 int show_filters(void *optctx, const char *opt, const char *arg)
1466 const AVFilter av_unused(*filter) = NULL;
1467 char descr[64], *descr_cur;
1469 const AVFilterPad *pad;
1472 " T. = Timeline support\n"
1473 " .S = Slice threading\n"
1474 " A = Audio input/output\n"
1475 " V = Video input/output\n"
1476 " N = Dynamic number and/or type of input/output\n"
1477 " | = Source or sink filter\n");
1479 while ((filter = avfilter_next(filter))) {
1481 for (i = 0; i < 2; i++) {
1483 *(descr_cur++) = '-';
1484 *(descr_cur++) = '>';
1486 pad = i ? filter->outputs : filter->inputs;
1487 for (j = 0; pad && pad[j].name; j++) {
1488 if (descr_cur >= descr + sizeof(descr) - 4)
1490 *(descr_cur++) = get_media_type_char(pad[j].type);
1493 *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1494 ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1497 printf(" %c%c %-16s %-10s %s\n",
1498 filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1499 filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
1500 filter->name, descr, filter->description);
1506 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1508 const AVPixFmtDescriptor *pix_desc = NULL;
1510 printf("Pixel formats:\n"
1511 "I.... = Supported Input format for conversion\n"
1512 ".O... = Supported Output format for conversion\n"
1513 "..H.. = Hardware accelerated format\n"
1514 "...P. = Paletted format\n"
1515 "....B = Bitstream format\n"
1516 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1520 # define sws_isSupportedInput(x) 0
1521 # define sws_isSupportedOutput(x) 0
1524 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1525 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1526 printf("%c%c%c%c%c %-16s %d %2d\n",
1527 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1528 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1529 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1530 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1531 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1533 pix_desc->nb_components,
1534 av_get_bits_per_pixel(pix_desc));
1539 int show_layouts(void *optctx, const char *opt, const char *arg)
1543 const char *name, *descr;
1545 printf("Individual channels:\n"
1546 "NAME DESCRIPTION\n");
1547 for (i = 0; i < 63; i++) {
1548 name = av_get_channel_name((uint64_t)1 << i);
1551 descr = av_get_channel_description((uint64_t)1 << i);
1552 printf("%-12s%s\n", name, descr);
1554 printf("\nStandard channel layouts:\n"
1555 "NAME DECOMPOSITION\n");
1556 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1558 printf("%-12s", name);
1559 for (j = 1; j; j <<= 1)
1561 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1568 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1572 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1573 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1577 static void show_help_codec(const char *name, int encoder)
1579 const AVCodecDescriptor *desc;
1580 const AVCodec *codec;
1583 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1587 codec = encoder ? avcodec_find_encoder_by_name(name) :
1588 avcodec_find_decoder_by_name(name);
1592 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1595 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1601 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1602 "but no %s for it are available. FFmpeg might need to be "
1603 "recompiled with additional external libraries.\n",
1604 name, encoder ? "encoders" : "decoders");
1607 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1612 static void show_help_demuxer(const char *name)
1614 const AVInputFormat *fmt = av_find_input_format(name);
1617 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1621 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1623 if (fmt->extensions)
1624 printf(" Common extensions: %s.\n", fmt->extensions);
1626 if (fmt->priv_class)
1627 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1630 static void show_help_muxer(const char *name)
1632 const AVCodecDescriptor *desc;
1633 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1636 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1640 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1642 if (fmt->extensions)
1643 printf(" Common extensions: %s.\n", fmt->extensions);
1645 printf(" Mime type: %s.\n", fmt->mime_type);
1646 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1647 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1648 printf(" Default video codec: %s.\n", desc->name);
1650 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1651 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1652 printf(" Default audio codec: %s.\n", desc->name);
1654 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1655 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1656 printf(" Default subtitle codec: %s.\n", desc->name);
1659 if (fmt->priv_class)
1660 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1664 static void show_help_filter(const char *name)
1667 const AVFilter *f = avfilter_get_by_name(name);
1671 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1674 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1678 printf("Filter %s\n", f->name);
1680 printf(" %s\n", f->description);
1682 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1683 printf(" slice threading supported\n");
1685 printf(" Inputs:\n");
1686 count = avfilter_pad_count(f->inputs);
1687 for (i = 0; i < count; i++) {
1688 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1689 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1691 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1692 printf(" dynamic (depending on the options)\n");
1694 printf(" none (source filter)\n");
1696 printf(" Outputs:\n");
1697 count = avfilter_pad_count(f->outputs);
1698 for (i = 0; i < count; i++) {
1699 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1700 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1702 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1703 printf(" dynamic (depending on the options)\n");
1705 printf(" none (sink filter)\n");
1708 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1709 AV_OPT_FLAG_AUDIO_PARAM);
1710 if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1711 printf("This filter has support for timeline through the 'enable' option.\n");
1713 av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1714 "can not to satisfy request\n");
1719 int show_help(void *optctx, const char *opt, const char *arg)
1722 av_log_set_callback(log_callback_help);
1724 topic = av_strdup(arg ? arg : "");
1725 par = strchr(topic, '=');
1730 show_help_default(topic, par);
1731 } else if (!strcmp(topic, "decoder")) {
1732 show_help_codec(par, 0);
1733 } else if (!strcmp(topic, "encoder")) {
1734 show_help_codec(par, 1);
1735 } else if (!strcmp(topic, "demuxer")) {
1736 show_help_demuxer(par);
1737 } else if (!strcmp(topic, "muxer")) {
1738 show_help_muxer(par);
1740 } else if (!strcmp(topic, "filter")) {
1741 show_help_filter(par);
1744 show_help_default(topic, par);
1751 int read_yesno(void)
1754 int yesno = (av_toupper(c) == 'Y');
1756 while (c != '\n' && c != EOF)
1762 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1765 FILE *f = fopen(filename, "rb");
1768 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1770 return AVERROR(errno);
1772 fseek(f, 0, SEEK_END);
1774 fseek(f, 0, SEEK_SET);
1775 if (*size == (size_t)-1) {
1776 av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1778 return AVERROR(errno);
1780 *bufptr = av_malloc(*size + 1);
1782 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1784 return AVERROR(ENOMEM);
1786 ret = fread(*bufptr, 1, *size, f);
1790 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1791 filename, strerror(errno));
1792 ret = AVERROR(errno);
1797 (*bufptr)[(*size)++] = '\0';
1804 FILE *get_preset_file(char *filename, size_t filename_size,
1805 const char *preset_name, int is_path,
1806 const char *codec_name)
1810 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1815 av_strlcpy(filename, preset_name, filename_size);
1816 f = fopen(filename, "r");
1819 char datadir[MAX_PATH], *ls;
1822 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1824 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1825 if (*ls == '\\') *ls = '/';
1827 if (ls = strrchr(datadir, '/'))
1830 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1835 for (i = 0; i < 3 && !f; i++) {
1838 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1839 i != 1 ? "" : "/.ffmpeg", preset_name);
1840 f = fopen(filename, "r");
1841 if (!f && codec_name) {
1842 snprintf(filename, filename_size,
1843 "%s%s/%s-%s.ffpreset",
1844 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1846 f = fopen(filename, "r");
1854 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1856 int ret = avformat_match_stream_specifier(s, st, spec);
1858 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1862 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1863 AVFormatContext *s, AVStream *st, AVCodec *codec)
1865 AVDictionary *ret = NULL;
1866 AVDictionaryEntry *t = NULL;
1867 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1868 : AV_OPT_FLAG_DECODING_PARAM;
1870 const AVClass *cc = avcodec_get_class();
1873 codec = s->oformat ? avcodec_find_encoder(codec_id)
1874 : avcodec_find_decoder(codec_id);
1876 switch (st->codec->codec_type) {
1877 case AVMEDIA_TYPE_VIDEO:
1879 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1881 case AVMEDIA_TYPE_AUDIO:
1883 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1885 case AVMEDIA_TYPE_SUBTITLE:
1887 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1891 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1892 char *p = strchr(t->key, ':');
1894 /* check stream specification in opt name */
1896 switch (check_stream_specifier(s, st, p + 1)) {
1897 case 1: *p = 0; break;
1899 default: return NULL;
1902 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1903 (codec && codec->priv_class &&
1904 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1905 AV_OPT_SEARCH_FAKE_OBJ)))
1906 av_dict_set(&ret, t->key, t->value, 0);
1907 else if (t->key[0] == prefix &&
1908 av_opt_find(&cc, t->key + 1, NULL, flags,
1909 AV_OPT_SEARCH_FAKE_OBJ))
1910 av_dict_set(&ret, t->key + 1, t->value, 0);
1918 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1919 AVDictionary *codec_opts)
1922 AVDictionary **opts;
1926 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1928 av_log(NULL, AV_LOG_ERROR,
1929 "Could not alloc memory for stream options.\n");
1932 for (i = 0; i < s->nb_streams; i++)
1933 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1934 s, s->streams[i], NULL);
1938 void *grow_array(void *array, int elem_size, int *size, int new_size)
1940 if (new_size >= INT_MAX / elem_size) {
1941 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1944 if (*size < new_size) {
1945 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1947 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1950 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);