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
28 /* Include only the enabled headers since some compilers (namely, Sun
29 Studio) will not omit unused inline functions and create undefined
30 references to libraries that are not being built. */
33 #include "compat/va_copy.h"
34 #include "libavformat/avformat.h"
35 #include "libavfilter/avfilter.h"
36 #include "libavdevice/avdevice.h"
37 #include "libavresample/avresample.h"
38 #include "libswscale/swscale.h"
39 #include "libswresample/swresample.h"
40 #include "libpostproc/postprocess.h"
41 #include "libavutil/avassert.h"
42 #include "libavutil/avstring.h"
43 #include "libavutil/bprint.h"
44 #include "libavutil/display.h"
45 #include "libavutil/mathematics.h"
46 #include "libavutil/imgutils.h"
47 #include "libavutil/libm.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/pixdesc.h"
50 #include "libavutil/eval.h"
51 #include "libavutil/dict.h"
52 #include "libavutil/opt.h"
53 #include "libavutil/cpu.h"
54 #include "libavutil/ffversion.h"
55 #include "libavutil/version.h"
58 #include "libavformat/network.h"
60 #if HAVE_SYS_RESOURCE_H
62 #include <sys/resource.h>
68 static int init_report(const char *env);
70 AVDictionary *sws_dict;
71 AVDictionary *swr_opts;
72 AVDictionary *format_opts, *codec_opts, *resample_opts;
74 static FILE *report_file;
75 static int report_file_level = AV_LOG_DEBUG;
80 av_dict_set(&sws_dict, "flags", "bicubic", 0);
83 void uninit_opts(void)
85 av_dict_free(&swr_opts);
86 av_dict_free(&sws_dict);
87 av_dict_free(&format_opts);
88 av_dict_free(&codec_opts);
89 av_dict_free(&resample_opts);
92 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
94 vfprintf(stdout, fmt, vl);
97 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
101 static int print_prefix = 1;
104 av_log_default_callback(ptr, level, fmt, vl);
105 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
107 if (report_file_level >= level) {
108 fputs(line, report_file);
113 void init_dynload(void)
116 /* Calling SetDllDirectory with the empty string (but not NULL) removes the
117 * current working directory from the DLL search path as a security pre-caution. */
122 static void (*program_exit)(int ret);
124 void register_exit(void (*cb)(int ret))
129 void exit_program(int ret)
137 double parse_number_or_die(const char *context, const char *numstr, int type,
138 double min, double max)
142 double d = av_strtod(numstr, &tail);
144 error = "Expected number for %s but found: %s\n";
145 else if (d < min || d > max)
146 error = "The value for %s was %s which is not within %f - %f\n";
147 else if (type == OPT_INT64 && (int64_t)d != d)
148 error = "Expected int64 for %s but found %s\n";
149 else if (type == OPT_INT && (int)d != d)
150 error = "Expected int for %s but found %s\n";
153 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
158 int64_t parse_time_or_die(const char *context, const char *timestr,
162 if (av_parse_time(&us, timestr, is_duration) < 0) {
163 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
164 is_duration ? "duration" : "date", context, timestr);
170 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
171 int rej_flags, int alt_flags)
177 for (po = options; po->name; po++) {
180 if (((po->flags & req_flags) != req_flags) ||
181 (alt_flags && !(po->flags & alt_flags)) ||
182 (po->flags & rej_flags))
189 av_strlcpy(buf, po->name, sizeof(buf));
191 av_strlcat(buf, " ", sizeof(buf));
192 av_strlcat(buf, po->argname, sizeof(buf));
194 printf("-%-17s %s\n", buf, po->help);
199 void show_help_children(const AVClass *class, int flags)
201 const AVClass *child = NULL;
203 av_opt_show2(&class, NULL, flags, 0);
207 while (child = av_opt_child_class_next(class, child))
208 show_help_children(child, flags);
211 static const OptionDef *find_option(const OptionDef *po, const char *name)
213 const char *p = strchr(name, ':');
214 int len = p ? p - name : strlen(name);
217 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
224 /* _WIN32 means using the windows libc - cygwin doesn't define that
225 * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
226 * it doesn't provide the actual command line via GetCommandLineW(). */
227 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
229 #include <shellapi.h>
230 /* Will be leaked on exit */
231 static char** win32_argv_utf8 = NULL;
232 static int win32_argc = 0;
235 * Prepare command line arguments for executable.
236 * For Windows - perform wide-char to UTF-8 conversion.
237 * Input arguments should be main() function arguments.
238 * @param argc_ptr Arguments number (including executable)
239 * @param argv_ptr Arguments list.
241 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
245 int i, buffsize = 0, offset = 0;
247 if (win32_argv_utf8) {
248 *argc_ptr = win32_argc;
249 *argv_ptr = win32_argv_utf8;
254 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
255 if (win32_argc <= 0 || !argv_w)
258 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
259 for (i = 0; i < win32_argc; i++)
260 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
261 NULL, 0, NULL, NULL);
263 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
264 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
265 if (!win32_argv_utf8) {
270 for (i = 0; i < win32_argc; i++) {
271 win32_argv_utf8[i] = &argstr_flat[offset];
272 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
273 &argstr_flat[offset],
274 buffsize - offset, NULL, NULL);
276 win32_argv_utf8[i] = NULL;
279 *argc_ptr = win32_argc;
280 *argv_ptr = win32_argv_utf8;
283 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
287 #endif /* HAVE_COMMANDLINETOARGVW */
289 static int write_option(void *optctx, const OptionDef *po, const char *opt,
292 /* new-style options contain an offset into optctx, old-style address of
294 void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
295 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
298 if (po->flags & OPT_SPEC) {
299 SpecifierOpt **so = dst;
300 char *p = strchr(opt, ':');
303 dstcount = (int *)(so + 1);
304 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
305 str = av_strdup(p ? p + 1 : "");
307 return AVERROR(ENOMEM);
308 (*so)[*dstcount - 1].specifier = str;
309 dst = &(*so)[*dstcount - 1].u;
312 if (po->flags & OPT_STRING) {
314 str = av_strdup(arg);
317 return AVERROR(ENOMEM);
319 } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
320 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
321 } else if (po->flags & OPT_INT64) {
322 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
323 } else if (po->flags & OPT_TIME) {
324 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
325 } else if (po->flags & OPT_FLOAT) {
326 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
327 } else if (po->flags & OPT_DOUBLE) {
328 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
329 } else if (po->u.func_arg) {
330 int ret = po->u.func_arg(optctx, opt, arg);
332 av_log(NULL, AV_LOG_ERROR,
333 "Failed to set value '%s' for option '%s': %s\n",
334 arg, opt, av_err2str(ret));
338 if (po->flags & OPT_EXIT)
344 int parse_option(void *optctx, const char *opt, const char *arg,
345 const OptionDef *options)
350 po = find_option(options, opt);
351 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
352 /* handle 'no' bool option */
353 po = find_option(options, opt + 2);
354 if ((po->name && (po->flags & OPT_BOOL)))
356 } else if (po->flags & OPT_BOOL)
360 po = find_option(options, "default");
362 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
363 return AVERROR(EINVAL);
365 if (po->flags & HAS_ARG && !arg) {
366 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
367 return AVERROR(EINVAL);
370 ret = write_option(optctx, po, opt, arg);
374 return !!(po->flags & HAS_ARG);
377 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
378 void (*parse_arg_function)(void *, const char*))
381 int optindex, handleoptions = 1, ret;
383 /* perform system-dependent conversions for arguments list */
384 prepare_app_arguments(&argc, &argv);
388 while (optindex < argc) {
389 opt = argv[optindex++];
391 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
392 if (opt[1] == '-' && opt[2] == '\0') {
398 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
402 if (parse_arg_function)
403 parse_arg_function(optctx, opt);
408 int parse_optgroup(void *optctx, OptionGroup *g)
412 av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
413 g->group_def->name, g->arg);
415 for (i = 0; i < g->nb_opts; i++) {
416 Option *o = &g->opts[i];
418 if (g->group_def->flags &&
419 !(g->group_def->flags & o->opt->flags)) {
420 av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
421 "%s %s -- you are trying to apply an input option to an "
422 "output file or vice versa. Move this option before the "
423 "file it belongs to.\n", o->key, o->opt->help,
424 g->group_def->name, g->arg);
425 return AVERROR(EINVAL);
428 av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
429 o->key, o->opt->help, o->val);
431 ret = write_option(optctx, o->opt, o->key, o->val);
436 av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
441 int locate_option(int argc, char **argv, const OptionDef *options,
447 for (i = 1; i < argc; i++) {
448 const char *cur_opt = argv[i];
450 if (*cur_opt++ != '-')
453 po = find_option(options, cur_opt);
454 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
455 po = find_option(options, cur_opt + 2);
457 if ((!po->name && !strcmp(cur_opt, optname)) ||
458 (po->name && !strcmp(optname, po->name)))
461 if (!po->name || po->flags & HAS_ARG)
467 static void dump_argument(const char *a)
469 const unsigned char *p;
472 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
473 *p == '_' || (*p >= 'a' && *p <= 'z')))
476 fputs(a, report_file);
479 fputc('"', report_file);
480 for (p = a; *p; p++) {
481 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
482 fprintf(report_file, "\\%c", *p);
483 else if (*p < ' ' || *p > '~')
484 fprintf(report_file, "\\x%02x", *p);
486 fputc(*p, report_file);
488 fputc('"', report_file);
491 static void check_options(const OptionDef *po)
494 if (po->flags & OPT_PERFILE)
495 av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
500 void parse_loglevel(int argc, char **argv, const OptionDef *options)
502 int idx = locate_option(argc, argv, options, "loglevel");
505 check_options(options);
508 idx = locate_option(argc, argv, options, "v");
509 if (idx && argv[idx + 1])
510 opt_loglevel(NULL, "loglevel", argv[idx + 1]);
511 idx = locate_option(argc, argv, options, "report");
512 if ((env = getenv("FFREPORT")) || idx) {
516 fprintf(report_file, "Command line:\n");
517 for (i = 0; i < argc; i++) {
518 dump_argument(argv[i]);
519 fputc(i < argc - 1 ? ' ' : '\n', report_file);
524 idx = locate_option(argc, argv, options, "hide_banner");
529 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
530 int opt_flags, int search_flags)
532 const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
538 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
539 int opt_default(void *optctx, const char *opt, const char *arg)
543 char opt_stripped[128];
545 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
546 #if CONFIG_AVRESAMPLE
547 const AVClass *rc = avresample_get_class();
550 const AVClass *sc = sws_get_class();
552 #if CONFIG_SWRESAMPLE
553 const AVClass *swr_class = swr_get_class();
556 if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
557 av_log_set_level(AV_LOG_DEBUG);
559 if (!(p = strchr(opt, ':')))
560 p = opt + strlen(opt);
561 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
563 if ((o = opt_find(&cc, opt_stripped, NULL, 0,
564 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
565 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
566 (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
567 av_dict_set(&codec_opts, opt, arg, FLAGS);
570 if ((o = opt_find(&fc, opt, NULL, 0,
571 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
572 av_dict_set(&format_opts, opt, arg, FLAGS);
574 av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
578 if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
579 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
580 struct SwsContext *sws = sws_alloc_context();
581 int ret = av_opt_set(sws, opt, arg, 0);
582 sws_freeContext(sws);
583 if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
584 !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
585 !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
586 av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
587 return AVERROR(EINVAL);
590 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
594 av_dict_set(&sws_dict, opt, arg, FLAGS);
599 if (!consumed && !strcmp(opt, "sws_flags")) {
600 av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
604 #if CONFIG_SWRESAMPLE
605 if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
606 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
607 struct SwrContext *swr = swr_alloc();
608 int ret = av_opt_set(swr, opt, arg, 0);
611 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
614 av_dict_set(&swr_opts, opt, arg, FLAGS);
618 #if CONFIG_AVRESAMPLE
619 if ((o=opt_find(&rc, opt, NULL, 0,
620 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
621 av_dict_set(&resample_opts, opt, arg, FLAGS);
628 return AVERROR_OPTION_NOT_FOUND;
632 * Check whether given option is a group separator.
634 * @return index of the group definition that matched or -1 if none
636 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
641 for (i = 0; i < nb_groups; i++) {
642 const OptionGroupDef *p = &groups[i];
643 if (p->sep && !strcmp(p->sep, opt))
651 * Finish parsing an option group.
653 * @param group_idx which group definition should this group belong to
654 * @param arg argument of the group delimiting option
656 static void finish_group(OptionParseContext *octx, int group_idx,
659 OptionGroupList *l = &octx->groups[group_idx];
662 GROW_ARRAY(l->groups, l->nb_groups);
663 g = &l->groups[l->nb_groups - 1];
665 *g = octx->cur_group;
667 g->group_def = l->group_def;
668 g->sws_dict = sws_dict;
669 g->swr_opts = swr_opts;
670 g->codec_opts = codec_opts;
671 g->format_opts = format_opts;
672 g->resample_opts = resample_opts;
676 resample_opts = NULL;
681 memset(&octx->cur_group, 0, sizeof(octx->cur_group));
685 * Add an option instance to currently parsed group.
687 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
688 const char *key, const char *val)
690 int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
691 OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
693 GROW_ARRAY(g->opts, g->nb_opts);
694 g->opts[g->nb_opts - 1].opt = opt;
695 g->opts[g->nb_opts - 1].key = key;
696 g->opts[g->nb_opts - 1].val = val;
699 static void init_parse_context(OptionParseContext *octx,
700 const OptionGroupDef *groups, int nb_groups)
702 static const OptionGroupDef global_group = { "global" };
705 memset(octx, 0, sizeof(*octx));
707 octx->nb_groups = nb_groups;
708 octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
712 for (i = 0; i < octx->nb_groups; i++)
713 octx->groups[i].group_def = &groups[i];
715 octx->global_opts.group_def = &global_group;
716 octx->global_opts.arg = "";
721 void uninit_parse_context(OptionParseContext *octx)
725 for (i = 0; i < octx->nb_groups; i++) {
726 OptionGroupList *l = &octx->groups[i];
728 for (j = 0; j < l->nb_groups; j++) {
729 av_freep(&l->groups[j].opts);
730 av_dict_free(&l->groups[j].codec_opts);
731 av_dict_free(&l->groups[j].format_opts);
732 av_dict_free(&l->groups[j].resample_opts);
734 av_dict_free(&l->groups[j].sws_dict);
735 av_dict_free(&l->groups[j].swr_opts);
737 av_freep(&l->groups);
739 av_freep(&octx->groups);
741 av_freep(&octx->cur_group.opts);
742 av_freep(&octx->global_opts.opts);
747 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
748 const OptionDef *options,
749 const OptionGroupDef *groups, int nb_groups)
754 /* perform system-dependent conversions for arguments list */
755 prepare_app_arguments(&argc, &argv);
757 init_parse_context(octx, groups, nb_groups);
758 av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
760 while (optindex < argc) {
761 const char *opt = argv[optindex++], *arg;
765 av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
767 if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
771 /* unnamed group separators, e.g. output filename */
772 if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
773 finish_group(octx, 0, opt);
774 av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
779 #define GET_ARG(arg) \
781 arg = argv[optindex++]; \
783 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
784 return AVERROR(EINVAL); \
788 /* named group separators, e.g. -i */
789 if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
791 finish_group(octx, ret, arg);
792 av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
793 groups[ret].name, arg);
798 po = find_option(options, opt);
800 if (po->flags & OPT_EXIT) {
801 /* optional argument, e.g. -h */
802 arg = argv[optindex++];
803 } else if (po->flags & HAS_ARG) {
809 add_opt(octx, po, opt, arg);
810 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
811 "argument '%s'.\n", po->name, po->help, arg);
816 if (argv[optindex]) {
817 ret = opt_default(NULL, opt, argv[optindex]);
819 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
820 "argument '%s'.\n", opt, argv[optindex]);
823 } else if (ret != AVERROR_OPTION_NOT_FOUND) {
824 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
825 "with argument '%s'.\n", opt, argv[optindex]);
830 /* boolean -nofoo options */
831 if (opt[0] == 'n' && opt[1] == 'o' &&
832 (po = find_option(options, opt + 2)) &&
833 po->name && po->flags & OPT_BOOL) {
834 add_opt(octx, po, opt, "0");
835 av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
836 "argument 0.\n", po->name, po->help);
840 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
841 return AVERROR_OPTION_NOT_FOUND;
844 if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
845 av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
848 av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
853 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
856 unsigned flags = av_get_cpu_flags();
858 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
861 av_force_cpu_flags(flags);
865 int opt_loglevel(void *optctx, const char *opt, const char *arg)
867 const struct { const char *name; int level; } log_levels[] = {
868 { "quiet" , AV_LOG_QUIET },
869 { "panic" , AV_LOG_PANIC },
870 { "fatal" , AV_LOG_FATAL },
871 { "error" , AV_LOG_ERROR },
872 { "warning", AV_LOG_WARNING },
873 { "info" , AV_LOG_INFO },
874 { "verbose", AV_LOG_VERBOSE },
875 { "debug" , AV_LOG_DEBUG },
876 { "trace" , AV_LOG_TRACE },
883 flags = av_log_get_flags();
884 tail = strstr(arg, "repeat");
886 flags &= ~AV_LOG_SKIP_REPEATED;
888 flags |= AV_LOG_SKIP_REPEATED;
890 av_log_set_flags(flags);
892 arg += 6 + (arg[6]=='+');
896 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
897 if (!strcmp(log_levels[i].name, arg)) {
898 av_log_set_level(log_levels[i].level);
903 level = strtol(arg, &tail, 10);
905 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
906 "Possible levels are numbers or:\n", arg);
907 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
908 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
911 av_log_set_level(level);
915 static void expand_filename_template(AVBPrint *bp, const char *template,
920 while ((c = *(template++))) {
922 if (!(c = *(template++)))
926 av_bprintf(bp, "%s", program_name);
929 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
930 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
931 tm->tm_hour, tm->tm_min, tm->tm_sec);
934 av_bprint_chars(bp, c, 1);
938 av_bprint_chars(bp, c, 1);
943 static int init_report(const char *env)
945 char *filename_template = NULL;
952 if (report_file) /* already opened */
955 tm = localtime(&now);
957 while (env && *env) {
958 if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
960 av_log(NULL, AV_LOG_ERROR,
961 "Failed to parse FFREPORT environment variable: %s\n",
968 if (!strcmp(key, "file")) {
969 av_free(filename_template);
970 filename_template = val;
972 } else if (!strcmp(key, "level")) {
974 report_file_level = strtol(val, &tail, 10);
976 av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
980 av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
986 av_bprint_init(&filename, 0, 1);
987 expand_filename_template(&filename,
988 av_x_if_null(filename_template, "%p-%t.log"), tm);
989 av_free(filename_template);
990 if (!av_bprint_is_complete(&filename)) {
991 av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
992 return AVERROR(ENOMEM);
995 report_file = fopen(filename.str, "w");
997 int ret = AVERROR(errno);
998 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
999 filename.str, strerror(errno));
1002 av_log_set_callback(log_callback_report);
1003 av_log(NULL, AV_LOG_INFO,
1004 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
1005 "Report written to \"%s\"\n",
1007 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1008 tm->tm_hour, tm->tm_min, tm->tm_sec,
1010 av_bprint_finalize(&filename, NULL);
1014 int opt_report(const char *opt)
1016 return init_report(NULL);
1019 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
1024 max = strtol(arg, &tail, 10);
1026 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
1033 int opt_timelimit(void *optctx, const char *opt, const char *arg)
1036 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
1037 struct rlimit rl = { lim, lim + 1 };
1038 if (setrlimit(RLIMIT_CPU, &rl))
1039 perror("setrlimit");
1041 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
1046 void print_error(const char *filename, int err)
1049 const char *errbuf_ptr = errbuf;
1051 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1052 errbuf_ptr = strerror(AVUNERROR(err));
1053 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
1056 static int warned_cfg = 0;
1059 #define SHOW_VERSION 2
1060 #define SHOW_CONFIG 4
1061 #define SHOW_COPYRIGHT 8
1063 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
1064 if (CONFIG_##LIBNAME) { \
1065 const char *indent = flags & INDENT? " " : ""; \
1066 if (flags & SHOW_VERSION) { \
1067 unsigned int version = libname##_version(); \
1068 av_log(NULL, level, \
1069 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
1071 LIB##LIBNAME##_VERSION_MAJOR, \
1072 LIB##LIBNAME##_VERSION_MINOR, \
1073 LIB##LIBNAME##_VERSION_MICRO, \
1074 AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
1075 AV_VERSION_MICRO(version)); \
1077 if (flags & SHOW_CONFIG) { \
1078 const char *cfg = libname##_configuration(); \
1079 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
1080 if (!warned_cfg) { \
1081 av_log(NULL, level, \
1082 "%sWARNING: library configuration mismatch\n", \
1086 av_log(NULL, level, "%s%-11s configuration: %s\n", \
1087 indent, #libname, cfg); \
1092 static void print_all_libs_info(int flags, int level)
1094 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
1095 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
1096 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1097 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1098 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1099 PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1100 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
1101 PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
1102 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1105 static void print_program_info(int flags, int level)
1107 const char *indent = flags & INDENT? " " : "";
1109 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1110 if (flags & SHOW_COPYRIGHT)
1111 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1112 program_birth_year, CONFIG_THIS_YEAR);
1113 av_log(NULL, level, "\n");
1114 av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
1116 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1119 static void print_buildconf(int flags, int level)
1121 const char *indent = flags & INDENT ? " " : "";
1122 char str[] = { FFMPEG_CONFIGURATION };
1123 char *conflist, *remove_tilde, *splitconf;
1125 // Change all the ' --' strings to '~--' so that
1126 // they can be identified as tokens.
1127 while ((conflist = strstr(str, " --")) != NULL) {
1128 strncpy(conflist, "~--", 3);
1131 // Compensate for the weirdness this would cause
1132 // when passing 'pkg-config --static'.
1133 while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
1134 strncpy(remove_tilde, "pkg-config ", 11);
1137 splitconf = strtok(str, "~");
1138 av_log(NULL, level, "\n%sconfiguration:\n", indent);
1139 while (splitconf != NULL) {
1140 av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
1141 splitconf = strtok(NULL, "~");
1145 void show_banner(int argc, char **argv, const OptionDef *options)
1147 int idx = locate_option(argc, argv, options, "version");
1148 if (hide_banner || idx)
1151 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1152 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
1153 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1156 int show_version(void *optctx, const char *opt, const char *arg)
1158 av_log_set_callback(log_callback_help);
1159 print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
1160 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1165 int show_buildconf(void *optctx, const char *opt, const char *arg)
1167 av_log_set_callback(log_callback_help);
1168 print_buildconf (INDENT|0, AV_LOG_INFO);
1173 int show_license(void *optctx, const char *opt, const char *arg)
1177 "This version of %s has nonfree parts compiled in.\n"
1178 "Therefore it is not legally redistributable.\n",
1182 "%s is free software; you can redistribute it and/or modify\n"
1183 "it under the terms of the GNU General Public License as published by\n"
1184 "the Free Software Foundation; either version 3 of the License, or\n"
1185 "(at your option) any later version.\n"
1187 "%s is distributed in the hope that it will be useful,\n"
1188 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1189 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1190 "GNU General Public License for more details.\n"
1192 "You should have received a copy of the GNU General Public License\n"
1193 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1194 program_name, program_name, program_name );
1197 "%s is free software; you can redistribute it and/or modify\n"
1198 "it under the terms of the GNU General Public License as published by\n"
1199 "the Free Software Foundation; either version 2 of the License, or\n"
1200 "(at your option) any later version.\n"
1202 "%s is distributed in the hope that it will be useful,\n"
1203 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1204 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1205 "GNU General Public License for more details.\n"
1207 "You should have received a copy of the GNU General Public License\n"
1208 "along with %s; if not, write to the Free Software\n"
1209 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1210 program_name, program_name, program_name );
1213 "%s is free software; you can redistribute it and/or modify\n"
1214 "it under the terms of the GNU Lesser General Public License as published by\n"
1215 "the Free Software Foundation; either version 3 of the License, or\n"
1216 "(at your option) any later version.\n"
1218 "%s is distributed in the hope that it will be useful,\n"
1219 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1220 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1221 "GNU Lesser General Public License for more details.\n"
1223 "You should have received a copy of the GNU Lesser General Public License\n"
1224 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1225 program_name, program_name, program_name );
1228 "%s is free software; you can redistribute it and/or\n"
1229 "modify it under the terms of the GNU Lesser General Public\n"
1230 "License as published by the Free Software Foundation; either\n"
1231 "version 2.1 of the License, or (at your option) any later version.\n"
1233 "%s is distributed in the hope that it will be useful,\n"
1234 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1235 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1236 "Lesser General Public License for more details.\n"
1238 "You should have received a copy of the GNU Lesser General Public\n"
1239 "License along with %s; if not, write to the Free Software\n"
1240 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1241 program_name, program_name, program_name );
1247 static int is_device(const AVClass *avclass)
1251 return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
1254 static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only)
1256 AVInputFormat *ifmt = NULL;
1257 AVOutputFormat *ofmt = NULL;
1258 const char *last_name;
1262 " D. = Demuxing supported\n"
1263 " .E = Muxing supported\n"
1264 " --\n", device_only ? "Devices:" : "File formats:");
1269 const char *name = NULL;
1270 const char *long_name = NULL;
1272 while ((ofmt = av_oformat_next(ofmt))) {
1273 is_dev = is_device(ofmt->priv_class);
1274 if (!is_dev && device_only)
1276 if ((!name || strcmp(ofmt->name, name) < 0) &&
1277 strcmp(ofmt->name, last_name) > 0) {
1279 long_name = ofmt->long_name;
1283 while ((ifmt = av_iformat_next(ifmt))) {
1284 is_dev = is_device(ifmt->priv_class);
1285 if (!is_dev && device_only)
1287 if ((!name || strcmp(ifmt->name, name) < 0) &&
1288 strcmp(ifmt->name, last_name) > 0) {
1290 long_name = ifmt->long_name;
1293 if (name && strcmp(ifmt->name, name) == 0)
1300 printf(" %s%s %-15s %s\n",
1304 long_name ? long_name:" ");
1309 int show_formats(void *optctx, const char *opt, const char *arg)
1311 return show_formats_devices(optctx, opt, arg, 0);
1314 int show_devices(void *optctx, const char *opt, const char *arg)
1316 return show_formats_devices(optctx, opt, arg, 1);
1319 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1320 if (codec->field) { \
1321 const type *p = codec->field; \
1323 printf(" Supported " list_name ":"); \
1324 while (*p != term) { \
1326 printf(" %s", name); \
1332 static void print_codec(const AVCodec *c)
1334 int encoder = av_codec_is_encoder(c);
1336 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1337 c->long_name ? c->long_name : "");
1339 printf(" General capabilities: ");
1340 if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
1341 printf("horizband ");
1342 if (c->capabilities & AV_CODEC_CAP_DR1)
1344 if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
1346 if (c->capabilities & AV_CODEC_CAP_DELAY)
1348 if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
1350 if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
1351 printf("subframes ");
1352 if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
1354 if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
1356 if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
1357 printf("paramchange ");
1358 if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
1359 printf("variable ");
1360 if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1361 AV_CODEC_CAP_SLICE_THREADS |
1362 AV_CODEC_CAP_AUTO_THREADS))
1364 if (!c->capabilities)
1368 if (c->type == AVMEDIA_TYPE_VIDEO ||
1369 c->type == AVMEDIA_TYPE_AUDIO) {
1370 printf(" Threading capabilities: ");
1371 switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1372 AV_CODEC_CAP_SLICE_THREADS |
1373 AV_CODEC_CAP_AUTO_THREADS)) {
1374 case AV_CODEC_CAP_FRAME_THREADS |
1375 AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1376 case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1377 case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1378 case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
1379 default: printf("none"); break;
1384 if (c->supported_framerates) {
1385 const AVRational *fps = c->supported_framerates;
1387 printf(" Supported framerates:");
1389 printf(" %d/%d", fps->num, fps->den);
1394 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1395 AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1396 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1397 GET_SAMPLE_RATE_NAME);
1398 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1399 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1400 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1401 0, GET_CH_LAYOUT_DESC);
1403 if (c->priv_class) {
1404 show_help_children(c->priv_class,
1405 AV_OPT_FLAG_ENCODING_PARAM |
1406 AV_OPT_FLAG_DECODING_PARAM);
1410 static char get_media_type_char(enum AVMediaType type)
1413 case AVMEDIA_TYPE_VIDEO: return 'V';
1414 case AVMEDIA_TYPE_AUDIO: return 'A';
1415 case AVMEDIA_TYPE_DATA: return 'D';
1416 case AVMEDIA_TYPE_SUBTITLE: return 'S';
1417 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1418 default: return '?';
1422 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1425 while ((prev = av_codec_next(prev))) {
1426 if (prev->id == id &&
1427 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1433 static int compare_codec_desc(const void *a, const void *b)
1435 const AVCodecDescriptor * const *da = a;
1436 const AVCodecDescriptor * const *db = b;
1438 return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
1439 strcmp((*da)->name, (*db)->name);
1442 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1444 const AVCodecDescriptor *desc = NULL;
1445 const AVCodecDescriptor **codecs;
1446 unsigned nb_codecs = 0, i = 0;
1448 while ((desc = avcodec_descriptor_next(desc)))
1450 if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1451 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1455 while ((desc = avcodec_descriptor_next(desc)))
1457 av_assert0(i == nb_codecs);
1458 qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1463 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1465 const AVCodec *codec = NULL;
1467 printf(" (%s: ", encoder ? "encoders" : "decoders");
1469 while ((codec = next_codec_for_id(id, codec, encoder)))
1470 printf("%s ", codec->name);
1475 int show_codecs(void *optctx, const char *opt, const char *arg)
1477 const AVCodecDescriptor **codecs;
1478 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1481 " D..... = Decoding supported\n"
1482 " .E.... = Encoding supported\n"
1483 " ..V... = Video codec\n"
1484 " ..A... = Audio codec\n"
1485 " ..S... = Subtitle codec\n"
1486 " ...I.. = Intra frame-only codec\n"
1487 " ....L. = Lossy compression\n"
1488 " .....S = Lossless compression\n"
1490 for (i = 0; i < nb_codecs; i++) {
1491 const AVCodecDescriptor *desc = codecs[i];
1492 const AVCodec *codec = NULL;
1494 if (strstr(desc->name, "_deprecated"))
1498 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1499 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1501 printf("%c", get_media_type_char(desc->type));
1502 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1503 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1504 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1506 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1508 /* print decoders/encoders when there's more than one or their
1509 * names are different from codec name */
1510 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1511 if (strcmp(codec->name, desc->name)) {
1512 print_codecs_for_id(desc->id, 0);
1517 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1518 if (strcmp(codec->name, desc->name)) {
1519 print_codecs_for_id(desc->id, 1);
1530 static void print_codecs(int encoder)
1532 const AVCodecDescriptor **codecs;
1533 unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1538 " S..... = Subtitle\n"
1539 " .F.... = Frame-level multithreading\n"
1540 " ..S... = Slice-level multithreading\n"
1541 " ...X.. = Codec is experimental\n"
1542 " ....B. = Supports draw_horiz_band\n"
1543 " .....D = Supports direct rendering method 1\n"
1545 encoder ? "Encoders" : "Decoders");
1546 for (i = 0; i < nb_codecs; i++) {
1547 const AVCodecDescriptor *desc = codecs[i];
1548 const AVCodec *codec = NULL;
1550 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1551 printf(" %c", get_media_type_char(desc->type));
1552 printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1553 printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1554 printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1555 printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1556 printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
1558 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1559 if (strcmp(codec->name, desc->name))
1560 printf(" (codec %s)", desc->name);
1568 int show_decoders(void *optctx, const char *opt, const char *arg)
1574 int show_encoders(void *optctx, const char *opt, const char *arg)
1580 int show_bsfs(void *optctx, const char *opt, const char *arg)
1582 AVBitStreamFilter *bsf = NULL;
1584 printf("Bitstream filters:\n");
1585 while ((bsf = av_bitstream_filter_next(bsf)))
1586 printf("%s\n", bsf->name);
1591 int show_protocols(void *optctx, const char *opt, const char *arg)
1593 void *opaque = NULL;
1596 printf("Supported file protocols:\n"
1598 while ((name = avio_enum_protocols(&opaque, 0)))
1599 printf(" %s\n", name);
1600 printf("Output:\n");
1601 while ((name = avio_enum_protocols(&opaque, 1)))
1602 printf(" %s\n", name);
1606 int show_filters(void *optctx, const char *opt, const char *arg)
1609 const AVFilter *filter = NULL;
1610 char descr[64], *descr_cur;
1612 const AVFilterPad *pad;
1615 " T.. = Timeline support\n"
1616 " .S. = Slice threading\n"
1617 " ..C = Command support\n"
1618 " A = Audio input/output\n"
1619 " V = Video input/output\n"
1620 " N = Dynamic number and/or type of input/output\n"
1621 " | = Source or sink filter\n");
1622 while ((filter = avfilter_next(filter))) {
1624 for (i = 0; i < 2; i++) {
1626 *(descr_cur++) = '-';
1627 *(descr_cur++) = '>';
1629 pad = i ? filter->outputs : filter->inputs;
1630 for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
1631 if (descr_cur >= descr + sizeof(descr) - 4)
1633 *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
1636 *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1637 ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1640 printf(" %c%c%c %-17s %-10s %s\n",
1641 filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1642 filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
1643 filter->process_command ? 'C' : '.',
1644 filter->name, descr, filter->description);
1647 printf("No filters available: libavfilter disabled\n");
1652 int show_colors(void *optctx, const char *opt, const char *arg)
1658 printf("%-32s #RRGGBB\n", "name");
1660 for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
1661 printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
1666 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1668 const AVPixFmtDescriptor *pix_desc = NULL;
1670 printf("Pixel formats:\n"
1671 "I.... = Supported Input format for conversion\n"
1672 ".O... = Supported Output format for conversion\n"
1673 "..H.. = Hardware accelerated format\n"
1674 "...P. = Paletted format\n"
1675 "....B = Bitstream format\n"
1676 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1680 # define sws_isSupportedInput(x) 0
1681 # define sws_isSupportedOutput(x) 0
1684 while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1685 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1686 printf("%c%c%c%c%c %-16s %d %2d\n",
1687 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1688 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1689 pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1690 pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1691 pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1693 pix_desc->nb_components,
1694 av_get_bits_per_pixel(pix_desc));
1699 int show_layouts(void *optctx, const char *opt, const char *arg)
1703 const char *name, *descr;
1705 printf("Individual channels:\n"
1706 "NAME DESCRIPTION\n");
1707 for (i = 0; i < 63; i++) {
1708 name = av_get_channel_name((uint64_t)1 << i);
1711 descr = av_get_channel_description((uint64_t)1 << i);
1712 printf("%-14s %s\n", name, descr);
1714 printf("\nStandard channel layouts:\n"
1715 "NAME DECOMPOSITION\n");
1716 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1718 printf("%-14s ", name);
1719 for (j = 1; j; j <<= 1)
1721 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1728 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1732 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1733 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1737 static void show_help_codec(const char *name, int encoder)
1739 const AVCodecDescriptor *desc;
1740 const AVCodec *codec;
1743 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1747 codec = encoder ? avcodec_find_encoder_by_name(name) :
1748 avcodec_find_decoder_by_name(name);
1752 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1755 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1761 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1762 "but no %s for it are available. FFmpeg might need to be "
1763 "recompiled with additional external libraries.\n",
1764 name, encoder ? "encoders" : "decoders");
1767 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1772 static void show_help_demuxer(const char *name)
1774 const AVInputFormat *fmt = av_find_input_format(name);
1777 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1781 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1783 if (fmt->extensions)
1784 printf(" Common extensions: %s.\n", fmt->extensions);
1786 if (fmt->priv_class)
1787 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1790 static void show_help_muxer(const char *name)
1792 const AVCodecDescriptor *desc;
1793 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1796 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1800 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1802 if (fmt->extensions)
1803 printf(" Common extensions: %s.\n", fmt->extensions);
1805 printf(" Mime type: %s.\n", fmt->mime_type);
1806 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1807 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1808 printf(" Default video codec: %s.\n", desc->name);
1810 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1811 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1812 printf(" Default audio codec: %s.\n", desc->name);
1814 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1815 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1816 printf(" Default subtitle codec: %s.\n", desc->name);
1819 if (fmt->priv_class)
1820 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1824 static void show_help_filter(const char *name)
1827 const AVFilter *f = avfilter_get_by_name(name);
1831 av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1834 av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1838 printf("Filter %s\n", f->name);
1840 printf(" %s\n", f->description);
1842 if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1843 printf(" slice threading supported\n");
1845 printf(" Inputs:\n");
1846 count = avfilter_pad_count(f->inputs);
1847 for (i = 0; i < count; i++) {
1848 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1849 media_type_string(avfilter_pad_get_type(f->inputs, i)));
1851 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1852 printf(" dynamic (depending on the options)\n");
1854 printf(" none (source filter)\n");
1856 printf(" Outputs:\n");
1857 count = avfilter_pad_count(f->outputs);
1858 for (i = 0; i < count; i++) {
1859 printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1860 media_type_string(avfilter_pad_get_type(f->outputs, i)));
1862 if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1863 printf(" dynamic (depending on the options)\n");
1865 printf(" none (sink filter)\n");
1868 show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1869 AV_OPT_FLAG_AUDIO_PARAM);
1870 if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1871 printf("This filter has support for timeline through the 'enable' option.\n");
1873 av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1874 "can not to satisfy request\n");
1879 int show_help(void *optctx, const char *opt, const char *arg)
1882 av_log_set_callback(log_callback_help);
1884 topic = av_strdup(arg ? arg : "");
1886 return AVERROR(ENOMEM);
1887 par = strchr(topic, '=');
1892 show_help_default(topic, par);
1893 } else if (!strcmp(topic, "decoder")) {
1894 show_help_codec(par, 0);
1895 } else if (!strcmp(topic, "encoder")) {
1896 show_help_codec(par, 1);
1897 } else if (!strcmp(topic, "demuxer")) {
1898 show_help_demuxer(par);
1899 } else if (!strcmp(topic, "muxer")) {
1900 show_help_muxer(par);
1902 } else if (!strcmp(topic, "filter")) {
1903 show_help_filter(par);
1906 show_help_default(topic, par);
1913 int read_yesno(void)
1916 int yesno = (av_toupper(c) == 'Y');
1918 while (c != '\n' && c != EOF)
1924 FILE *get_preset_file(char *filename, size_t filename_size,
1925 const char *preset_name, int is_path,
1926 const char *codec_name)
1930 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1935 av_strlcpy(filename, preset_name, filename_size);
1936 f = fopen(filename, "r");
1939 char datadir[MAX_PATH], *ls;
1942 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1944 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1945 if (*ls == '\\') *ls = '/';
1947 if (ls = strrchr(datadir, '/'))
1950 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1955 for (i = 0; i < 3 && !f; i++) {
1958 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1959 i != 1 ? "" : "/.ffmpeg", preset_name);
1960 f = fopen(filename, "r");
1961 if (!f && codec_name) {
1962 snprintf(filename, filename_size,
1963 "%s%s/%s-%s.ffpreset",
1964 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1966 f = fopen(filename, "r");
1974 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1976 int ret = avformat_match_stream_specifier(s, st, spec);
1978 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1982 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1983 AVFormatContext *s, AVStream *st, AVCodec *codec)
1985 AVDictionary *ret = NULL;
1986 AVDictionaryEntry *t = NULL;
1987 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1988 : AV_OPT_FLAG_DECODING_PARAM;
1990 const AVClass *cc = avcodec_get_class();
1993 codec = s->oformat ? avcodec_find_encoder(codec_id)
1994 : avcodec_find_decoder(codec_id);
1996 switch (st->codecpar->codec_type) {
1997 case AVMEDIA_TYPE_VIDEO:
1999 flags |= AV_OPT_FLAG_VIDEO_PARAM;
2001 case AVMEDIA_TYPE_AUDIO:
2003 flags |= AV_OPT_FLAG_AUDIO_PARAM;
2005 case AVMEDIA_TYPE_SUBTITLE:
2007 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
2011 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
2012 char *p = strchr(t->key, ':');
2014 /* check stream specification in opt name */
2016 switch (check_stream_specifier(s, st, p + 1)) {
2017 case 1: *p = 0; break;
2019 default: exit_program(1);
2022 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
2024 (codec->priv_class &&
2025 av_opt_find(&codec->priv_class, t->key, NULL, flags,
2026 AV_OPT_SEARCH_FAKE_OBJ)))
2027 av_dict_set(&ret, t->key, t->value, 0);
2028 else if (t->key[0] == prefix &&
2029 av_opt_find(&cc, t->key + 1, NULL, flags,
2030 AV_OPT_SEARCH_FAKE_OBJ))
2031 av_dict_set(&ret, t->key + 1, t->value, 0);
2039 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
2040 AVDictionary *codec_opts)
2043 AVDictionary **opts;
2047 opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
2049 av_log(NULL, AV_LOG_ERROR,
2050 "Could not alloc memory for stream options.\n");
2053 for (i = 0; i < s->nb_streams; i++)
2054 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
2055 s, s->streams[i], NULL);
2059 void *grow_array(void *array, int elem_size, int *size, int new_size)
2061 if (new_size >= INT_MAX / elem_size) {
2062 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
2065 if (*size < new_size) {
2066 uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
2068 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
2071 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
2078 double get_rotation(AVStream *st)
2080 AVDictionaryEntry *rotate_tag = av_dict_get(st->metadata, "rotate", NULL, 0);
2081 uint8_t* displaymatrix = av_stream_get_side_data(st,
2082 AV_PKT_DATA_DISPLAYMATRIX, NULL);
2085 if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
2087 theta = av_strtod(rotate_tag->value, &tail);
2091 if (displaymatrix && !theta)
2092 theta = -av_display_rotation_get((int32_t*) displaymatrix);
2094 theta -= 360*floor(theta/360 + 0.9/360);
2096 if (fabs(theta - 90*round(theta/90)) > 2)
2097 av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
2098 "If you want to help, upload a sample "
2099 "of this file to ftp://upload.ffmpeg.org/incoming/ "
2100 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
2106 static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
2109 AVDeviceInfoList *device_list = NULL;
2111 if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
2112 return AVERROR(EINVAL);
2114 printf("Audo-detected sources for %s:\n", fmt->name);
2115 if (!fmt->get_device_list) {
2116 ret = AVERROR(ENOSYS);
2117 printf("Cannot list sources. Not implemented.\n");
2121 if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
2122 printf("Cannot list sources.\n");
2126 for (i = 0; i < device_list->nb_devices; i++) {
2127 printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
2128 device_list->devices[i]->device_name, device_list->devices[i]->device_description);
2132 avdevice_free_list_devices(&device_list);
2136 static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
2139 AVDeviceInfoList *device_list = NULL;
2141 if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
2142 return AVERROR(EINVAL);
2144 printf("Audo-detected sinks for %s:\n", fmt->name);
2145 if (!fmt->get_device_list) {
2146 ret = AVERROR(ENOSYS);
2147 printf("Cannot list sinks. Not implemented.\n");
2151 if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
2152 printf("Cannot list sinks.\n");
2156 for (i = 0; i < device_list->nb_devices; i++) {
2157 printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
2158 device_list->devices[i]->device_name, device_list->devices[i]->device_description);
2162 avdevice_free_list_devices(&device_list);
2166 static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
2170 char *opts_str = NULL;
2171 av_assert0(dev && opts);
2172 *dev = av_strdup(arg);
2174 return AVERROR(ENOMEM);
2175 if ((opts_str = strchr(*dev, ','))) {
2176 *(opts_str++) = '\0';
2177 if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
2183 printf("\nDevice name is not provided.\n"
2184 "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
2188 int show_sources(void *optctx, const char *opt, const char *arg)
2190 AVInputFormat *fmt = NULL;
2192 AVDictionary *opts = NULL;
2194 int error_level = av_log_get_level();
2196 av_log_set_level(AV_LOG_ERROR);
2198 if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
2202 fmt = av_input_audio_device_next(fmt);
2204 if (!strcmp(fmt->name, "lavfi"))
2205 continue; //it's pointless to probe lavfi
2206 if (dev && !av_match_name(dev, fmt->name))
2208 print_device_sources(fmt, opts);
2212 fmt = av_input_video_device_next(fmt);
2214 if (dev && !av_match_name(dev, fmt->name))
2216 print_device_sources(fmt, opts);
2220 av_dict_free(&opts);
2222 av_log_set_level(error_level);
2226 int show_sinks(void *optctx, const char *opt, const char *arg)
2228 AVOutputFormat *fmt = NULL;
2230 AVDictionary *opts = NULL;
2232 int error_level = av_log_get_level();
2234 av_log_set_level(AV_LOG_ERROR);
2236 if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
2240 fmt = av_output_audio_device_next(fmt);
2242 if (dev && !av_match_name(dev, fmt->name))
2244 print_device_sinks(fmt, opts);
2248 fmt = av_output_video_device_next(fmt);
2250 if (dev && !av_match_name(dev, fmt->name))
2252 print_device_sinks(fmt, opts);
2256 av_dict_free(&opts);
2258 av_log_set_level(error_level);