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 "libavformat/avformat.h"
33 #include "libavfilter/avfilter.h"
34 #include "libavdevice/avdevice.h"
35 #include "libswscale/swscale.h"
36 #include "libswresample/swresample.h"
38 #include "libpostproc/postprocess.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/eval.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/opt.h"
50 #include "libavformat/network.h"
52 #if HAVE_SYS_RESOURCE_H
53 #include <sys/resource.h>
56 struct SwsContext *sws_opts;
57 AVDictionary *format_opts, *codec_opts;
59 const int this_year = 2012;
61 static FILE *report_file;
66 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
71 void uninit_opts(void)
74 sws_freeContext(sws_opts);
77 av_dict_free(&format_opts);
78 av_dict_free(&codec_opts);
81 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
83 vfprintf(stdout, fmt, vl);
86 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
90 static int print_prefix = 1;
93 av_log_default_callback(ptr, level, fmt, vl);
94 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
96 fputs(line, report_file);
100 double parse_number_or_die(const char *context, const char *numstr, int type,
101 double min, double max)
105 double d = av_strtod(numstr, &tail);
107 error = "Expected number for %s but found: %s\n";
108 else if (d < min || d > max)
109 error = "The value for %s was %s which is not within %f - %f\n";
110 else if (type == OPT_INT64 && (int64_t)d != d)
111 error = "Expected int64 for %s but found %s\n";
112 else if (type == OPT_INT && (int)d != d)
113 error = "Expected int for %s but found %s\n";
116 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
121 int64_t parse_time_or_die(const char *context, const char *timestr,
125 if (av_parse_time(&us, timestr, is_duration) < 0) {
126 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
127 is_duration ? "duration" : "date", context, timestr);
133 void show_help_options(const OptionDef *options, const char *msg, int mask,
140 for (po = options; po->name != NULL; po++) {
142 if ((po->flags & mask) == value) {
147 av_strlcpy(buf, po->name, sizeof(buf));
148 if (po->flags & HAS_ARG) {
149 av_strlcat(buf, " ", sizeof(buf));
150 av_strlcat(buf, po->argname, sizeof(buf));
152 printf("-%-17s %s\n", buf, po->help);
157 void show_help_children(const AVClass *class, int flags)
159 const AVClass *child = NULL;
160 av_opt_show2(&class, NULL, flags, 0);
163 while (child = av_opt_child_class_next(class, child))
164 show_help_children(child, flags);
167 static const OptionDef *find_option(const OptionDef *po, const char *name)
169 const char *p = strchr(name, ':');
170 int len = p ? p - name : strlen(name);
172 while (po->name != NULL) {
173 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
180 #if defined(_WIN32) && !defined(__MINGW32CE__)
182 /* Will be leaked on exit */
183 static char** win32_argv_utf8 = NULL;
184 static int win32_argc = 0;
187 * Prepare command line arguments for executable.
188 * For Windows - perform wide-char to UTF-8 conversion.
189 * Input arguments should be main() function arguments.
190 * @param argc_ptr Arguments number (including executable)
191 * @param argv_ptr Arguments list.
193 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
197 int i, buffsize = 0, offset = 0;
199 if (win32_argv_utf8) {
200 *argc_ptr = win32_argc;
201 *argv_ptr = win32_argv_utf8;
206 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
207 if (win32_argc <= 0 || !argv_w)
210 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
211 for (i = 0; i < win32_argc; i++)
212 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
213 NULL, 0, NULL, NULL);
215 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
216 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
217 if (win32_argv_utf8 == NULL) {
222 for (i = 0; i < win32_argc; i++) {
223 win32_argv_utf8[i] = &argstr_flat[offset];
224 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
225 &argstr_flat[offset],
226 buffsize - offset, NULL, NULL);
228 win32_argv_utf8[i] = NULL;
231 *argc_ptr = win32_argc;
232 *argv_ptr = win32_argv_utf8;
235 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
239 #endif /* WIN32 && !__MINGW32CE__ */
241 int parse_option(void *optctx, const char *opt, const char *arg,
242 const OptionDef *options)
249 po = find_option(options, opt);
250 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
251 /* handle 'no' bool option */
252 po = find_option(options, opt + 2);
253 if ((po->name && (po->flags & OPT_BOOL)))
257 po = find_option(options, "default");
259 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
260 return AVERROR(EINVAL);
262 if (po->flags & HAS_ARG && !arg) {
263 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
264 return AVERROR(EINVAL);
267 /* new-style options contain an offset into optctx, old-style address of
269 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
272 if (po->flags & OPT_SPEC) {
273 SpecifierOpt **so = dst;
274 char *p = strchr(opt, ':');
276 dstcount = (int *)(so + 1);
277 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
278 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
279 dst = &(*so)[*dstcount - 1].u;
282 if (po->flags & OPT_STRING) {
284 str = av_strdup(arg);
286 } else if (po->flags & OPT_BOOL) {
287 *(int *)dst = bool_val;
288 } else if (po->flags & OPT_INT) {
289 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
290 } else if (po->flags & OPT_INT64) {
291 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
292 } else if (po->flags & OPT_TIME) {
293 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
294 } else if (po->flags & OPT_FLOAT) {
295 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
296 } else if (po->flags & OPT_DOUBLE) {
297 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
298 } else if (po->u.func_arg) {
299 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
300 : po->u.func_arg(opt, arg);
302 av_log(NULL, AV_LOG_ERROR,
303 "Failed to set value '%s' for option '%s'\n", arg, opt);
307 if (po->flags & OPT_EXIT)
309 return !!(po->flags & HAS_ARG);
312 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
313 void (*parse_arg_function)(void *, const char*))
316 int optindex, handleoptions = 1, ret;
318 /* perform system-dependent conversions for arguments list */
319 prepare_app_arguments(&argc, &argv);
323 while (optindex < argc) {
324 opt = argv[optindex++];
326 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
327 if (opt[1] == '-' && opt[2] == '\0') {
333 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
337 if (parse_arg_function)
338 parse_arg_function(optctx, opt);
343 int locate_option(int argc, char **argv, const OptionDef *options,
349 for (i = 1; i < argc; i++) {
350 const char *cur_opt = argv[i];
352 if (*cur_opt++ != '-')
355 po = find_option(options, cur_opt);
356 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
357 po = find_option(options, cur_opt + 2);
359 if ((!po->name && !strcmp(cur_opt, optname)) ||
360 (po->name && !strcmp(optname, po->name)))
363 if (!po || po->flags & HAS_ARG)
369 static void dump_argument(const char *a)
371 const unsigned char *p;
374 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
375 *p == '_' || (*p >= 'a' && *p <= 'z')))
378 fputs(a, report_file);
381 fputc('"', report_file);
382 for (p = a; *p; p++) {
383 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
384 fprintf(report_file, "\\%c", *p);
385 else if (*p < ' ' || *p > '~')
386 fprintf(report_file, "\\x%02x", *p);
388 fputc(*p, report_file);
390 fputc('"', report_file);
393 void parse_loglevel(int argc, char **argv, const OptionDef *options)
395 int idx = locate_option(argc, argv, options, "loglevel");
397 idx = locate_option(argc, argv, options, "v");
398 if (idx && argv[idx + 1])
399 opt_loglevel("loglevel", argv[idx + 1]);
400 idx = locate_option(argc, argv, options, "report");
401 if (idx || getenv("FFREPORT")) {
402 opt_report("report");
405 fprintf(report_file, "Command line:\n");
406 for (i = 0; i < argc; i++) {
407 dump_argument(argv[i]);
408 fputc(i < argc - 1 ? ' ' : '\n', report_file);
415 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
416 int opt_default(const char *opt, const char *arg)
418 const AVOption *oc, *of, *os;
419 char opt_stripped[128];
421 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
423 if (!(p = strchr(opt, ':')))
424 p = opt + strlen(opt);
425 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
427 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0,
428 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
429 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
430 (oc = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
431 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
432 if ((of = av_opt_find(&fc, opt, NULL, 0,
433 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
434 av_dict_set(&format_opts, opt, arg, FLAGS(of));
436 sc = sws_get_class();
437 if ((os = av_opt_find(&sc, opt, NULL, 0,
438 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
439 // XXX we only support sws_flags, not arbitrary sws options
440 int ret = av_opt_set(sws_opts, opt, arg, 0);
442 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
450 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
451 return AVERROR_OPTION_NOT_FOUND;
454 int opt_loglevel(const char *opt, const char *arg)
456 const struct { const char *name; int level; } log_levels[] = {
457 { "quiet" , AV_LOG_QUIET },
458 { "panic" , AV_LOG_PANIC },
459 { "fatal" , AV_LOG_FATAL },
460 { "error" , AV_LOG_ERROR },
461 { "warning", AV_LOG_WARNING },
462 { "info" , AV_LOG_INFO },
463 { "verbose", AV_LOG_VERBOSE },
464 { "debug" , AV_LOG_DEBUG },
470 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
471 if (!strcmp(log_levels[i].name, arg)) {
472 av_log_set_level(log_levels[i].level);
477 level = strtol(arg, &tail, 10);
479 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
480 "Possible levels are numbers or:\n", arg);
481 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
482 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
485 av_log_set_level(level);
489 int opt_report(const char *opt)
495 if (report_file) /* already opened */
498 tm = localtime(&now);
499 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
501 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
502 tm->tm_hour, tm->tm_min, tm->tm_sec);
503 report_file = fopen(filename, "w");
505 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
506 filename, strerror(errno));
507 return AVERROR(errno);
509 av_log_set_callback(log_callback_report);
510 av_log(NULL, AV_LOG_INFO,
511 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
512 "Report written to \"%s\"\n",
514 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
515 tm->tm_hour, tm->tm_min, tm->tm_sec,
517 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
521 int opt_max_alloc(const char *opt, const char *arg)
526 max = strtol(arg, &tail, 10);
528 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
535 int opt_cpuflags(const char *opt, const char *arg)
537 #define CPUFLAG_MMX2 (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMX2)
538 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
539 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
540 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMX2)
541 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
542 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
543 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
544 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
545 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
546 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
547 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
548 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
549 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
550 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
551 static const AVOption cpuflags_opts[] = {
552 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
553 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
554 { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_MMX }, .unit = "flags" },
555 { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_MMX2 }, .unit = "flags" },
556 { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE }, .unit = "flags" },
557 { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2 }, .unit = "flags" },
558 { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2SLOW }, .unit = "flags" },
559 { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3 }, .unit = "flags" },
560 { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3SLOW }, .unit = "flags" },
561 { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSSE3 }, .unit = "flags" },
562 { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ATOM }, .unit = "flags" },
563 { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE4 }, .unit = "flags" },
564 { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE42 }, .unit = "flags" },
565 { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_AVX }, .unit = "flags" },
566 { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_XOP }, .unit = "flags" },
567 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_FMA4 }, .unit = "flags" },
568 { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOW }, .unit = "flags" },
569 { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOWEXT }, .unit = "flags" },
572 static const AVClass class = {
573 .class_name = "cpuflags",
574 .item_name = av_default_item_name,
575 .option = cpuflags_opts,
576 .version = LIBAVUTIL_VERSION_INT,
578 int flags = av_get_cpu_flags();
580 const AVClass *pclass = &class;
582 if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], arg, &flags)) < 0)
585 av_force_cpu_flags(flags);
589 int opt_codec_debug(const char *opt, const char *arg)
591 av_log_set_level(AV_LOG_DEBUG);
592 return opt_default(opt, arg);
595 int opt_timelimit(const char *opt, const char *arg)
598 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
599 struct rlimit rl = { lim, lim + 1 };
600 if (setrlimit(RLIMIT_CPU, &rl))
603 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
608 void print_error(const char *filename, int err)
611 const char *errbuf_ptr = errbuf;
613 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
614 errbuf_ptr = strerror(AVUNERROR(err));
615 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
618 static int warned_cfg = 0;
621 #define SHOW_VERSION 2
622 #define SHOW_CONFIG 4
623 #define SHOW_COPYRIGHT 8
625 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
626 if (CONFIG_##LIBNAME) { \
627 const char *indent = flags & INDENT? " " : ""; \
628 if (flags & SHOW_VERSION) { \
629 unsigned int version = libname##_version(); \
630 av_log(NULL, level, "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",\
632 LIB##LIBNAME##_VERSION_MAJOR, \
633 LIB##LIBNAME##_VERSION_MINOR, \
634 LIB##LIBNAME##_VERSION_MICRO, \
635 version >> 16, version >> 8 & 0xff, version & 0xff); \
637 if (flags & SHOW_CONFIG) { \
638 const char *cfg = libname##_configuration(); \
639 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
641 av_log(NULL, level, \
642 "%sWARNING: library configuration mismatch\n", \
646 av_log(NULL, level, "%s%-11s configuration: %s\n", \
647 indent, #libname, cfg); \
652 static void print_all_libs_info(int flags, int level)
654 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
655 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
656 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
657 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
658 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
659 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
660 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
662 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
666 static void print_program_info(int flags, int level)
668 const char *indent = flags & INDENT? " " : "";
670 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
671 if (flags & SHOW_COPYRIGHT)
672 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
673 program_birth_year, this_year);
674 av_log(NULL, level, "\n");
675 av_log(NULL, level, "%sbuilt on %s %s with %s %s\n",
676 indent, __DATE__, __TIME__, CC_TYPE, CC_VERSION);
677 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
680 void show_banner(int argc, char **argv, const OptionDef *options)
682 int idx = locate_option(argc, argv, options, "version");
686 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
687 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
688 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
691 int opt_version(const char *opt, const char *arg) {
692 av_log_set_callback(log_callback_help);
693 print_program_info (0 , AV_LOG_INFO);
694 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
698 int opt_license(const char *opt, const char *arg)
702 "This version of %s has nonfree parts compiled in.\n"
703 "Therefore it is not legally redistributable.\n",
706 "%s is free software; you can redistribute it and/or modify\n"
707 "it under the terms of the GNU General Public License as published by\n"
708 "the Free Software Foundation; either version 3 of the License, or\n"
709 "(at your option) any later version.\n"
711 "%s is distributed in the hope that it will be useful,\n"
712 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
713 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
714 "GNU General Public License for more details.\n"
716 "You should have received a copy of the GNU General Public License\n"
717 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
718 program_name, program_name, program_name
720 "%s is free software; you can redistribute it and/or modify\n"
721 "it under the terms of the GNU General Public License as published by\n"
722 "the Free Software Foundation; either version 2 of the License, or\n"
723 "(at your option) any later version.\n"
725 "%s is distributed in the hope that it will be useful,\n"
726 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
727 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
728 "GNU General Public License for more details.\n"
730 "You should have received a copy of the GNU General Public License\n"
731 "along with %s; if not, write to the Free Software\n"
732 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
733 program_name, program_name, program_name
735 "%s is free software; you can redistribute it and/or modify\n"
736 "it under the terms of the GNU Lesser General Public License as published by\n"
737 "the Free Software Foundation; either version 3 of the License, or\n"
738 "(at your option) any later version.\n"
740 "%s is distributed in the hope that it will be useful,\n"
741 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
742 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
743 "GNU Lesser General Public License for more details.\n"
745 "You should have received a copy of the GNU Lesser General Public License\n"
746 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
747 program_name, program_name, program_name
749 "%s is free software; you can redistribute it and/or\n"
750 "modify it under the terms of the GNU Lesser General Public\n"
751 "License as published by the Free Software Foundation; either\n"
752 "version 2.1 of the License, or (at your option) any later version.\n"
754 "%s is distributed in the hope that it will be useful,\n"
755 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
756 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
757 "Lesser General Public License for more details.\n"
759 "You should have received a copy of the GNU Lesser General Public\n"
760 "License along with %s; if not, write to the Free Software\n"
761 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
762 program_name, program_name, program_name
768 int opt_formats(const char *opt, const char *arg)
770 AVInputFormat *ifmt = NULL;
771 AVOutputFormat *ofmt = NULL;
772 const char *last_name;
774 printf("File formats:\n"
775 " D. = Demuxing supported\n"
776 " .E = Muxing supported\n"
782 const char *name = NULL;
783 const char *long_name = NULL;
785 while ((ofmt = av_oformat_next(ofmt))) {
786 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
787 strcmp(ofmt->name, last_name) > 0) {
789 long_name = ofmt->long_name;
793 while ((ifmt = av_iformat_next(ifmt))) {
794 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
795 strcmp(ifmt->name, last_name) > 0) {
797 long_name = ifmt->long_name;
800 if (name && strcmp(ifmt->name, name) == 0)
807 printf(" %s%s %-15s %s\n",
811 long_name ? long_name:" ");
816 static char get_media_type_char(enum AVMediaType type)
818 static const char map[AVMEDIA_TYPE_NB] = {
819 [AVMEDIA_TYPE_VIDEO] = 'V',
820 [AVMEDIA_TYPE_AUDIO] = 'A',
821 [AVMEDIA_TYPE_DATA] = 'D',
822 [AVMEDIA_TYPE_SUBTITLE] = 'S',
823 [AVMEDIA_TYPE_ATTACHMENT] = 'T',
825 return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
828 int opt_codecs(const char *opt, const char *arg)
830 AVCodec *p = NULL, *p2;
831 const char *last_name;
833 " D..... = Decoding supported\n"
834 " .E.... = Encoding supported\n"
835 " ..V... = Video codec\n"
836 " ..A... = Audio codec\n"
837 " ..S... = Subtitle codec\n"
838 " ...S.. = Supports draw_horiz_band\n"
839 " ....D. = Supports direct rendering method 1\n"
840 " .....T = Supports weird frame truncation\n"
849 while ((p = av_codec_next(p))) {
850 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
851 strcmp(p->name, last_name) > 0) {
853 decode = encode = cap = 0;
855 if (p2 && strcmp(p->name, p2->name) == 0) {
856 if (av_codec_is_decoder(p))
858 if (av_codec_is_encoder(p))
860 cap |= p->capabilities;
865 last_name = p2->name;
867 printf(" %s%s%c%s%s%s %-15s %s",
868 decode ? "D" : (/* p2->decoder ? "d" : */ " "),
870 get_media_type_char(p2->type),
871 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
872 cap & CODEC_CAP_DR1 ? "D" : " ",
873 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
875 p2->long_name ? p2->long_name : "");
877 if (p2->decoder && decode == 0)
878 printf(" use %s for decoding", p2->decoder->name);
883 printf("Note, the names of encoders and decoders do not always match, so there are\n"
884 "several cases where the above table shows encoder only or decoder only entries\n"
885 "even though both encoding and decoding are supported. For example, the h263\n"
886 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
891 int opt_bsfs(const char *opt, const char *arg)
893 AVBitStreamFilter *bsf = NULL;
895 printf("Bitstream filters:\n");
896 while ((bsf = av_bitstream_filter_next(bsf)))
897 printf("%s\n", bsf->name);
902 int opt_protocols(const char *opt, const char *arg)
907 printf("Supported file protocols:\n"
909 while ((name = avio_enum_protocols(&opaque, 0)))
910 printf("%s\n", name);
912 while ((name = avio_enum_protocols(&opaque, 1)))
913 printf("%s\n", name);
917 int opt_filters(const char *opt, const char *arg)
919 AVFilter av_unused(**filter) = NULL;
920 char descr[64], *descr_cur;
922 const AVFilterPad *pad;
924 printf("Filters:\n");
926 while ((filter = av_filter_next(filter)) && *filter) {
928 for (i = 0; i < 2; i++) {
930 *(descr_cur++) = '-';
931 *(descr_cur++) = '>';
933 pad = i ? (*filter)->outputs : (*filter)->inputs;
934 for (j = 0; pad[j].name; j++) {
935 if (descr_cur >= descr + sizeof(descr) - 4)
937 *(descr_cur++) = get_media_type_char(pad[j].type);
940 *(descr_cur++) = '|';
943 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
949 int opt_pix_fmts(const char *opt, const char *arg)
951 enum PixelFormat pix_fmt;
953 printf("Pixel formats:\n"
954 "I.... = Supported Input format for conversion\n"
955 ".O... = Supported Output format for conversion\n"
956 "..H.. = Hardware accelerated format\n"
957 "...P. = Paletted format\n"
958 "....B = Bitstream format\n"
959 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
963 # define sws_isSupportedInput(x) 0
964 # define sws_isSupportedOutput(x) 0
967 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
968 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
971 printf("%c%c%c%c%c %-16s %d %2d\n",
972 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
973 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
974 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
975 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
976 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
978 pix_desc->nb_components,
979 av_get_bits_per_pixel(pix_desc));
984 int show_sample_fmts(const char *opt, const char *arg)
988 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
989 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
996 int yesno = (toupper(c) == 'Y');
998 while (c != '\n' && c != EOF)
1004 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1007 FILE *f = fopen(filename, "rb");
1010 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1012 return AVERROR(errno);
1014 fseek(f, 0, SEEK_END);
1016 fseek(f, 0, SEEK_SET);
1017 *bufptr = av_malloc(*size + 1);
1019 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1021 return AVERROR(ENOMEM);
1023 ret = fread(*bufptr, 1, *size, f);
1027 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1028 filename, strerror(errno));
1029 ret = AVERROR(errno);
1034 (*bufptr)[*size++] = '\0';
1041 FILE *get_preset_file(char *filename, size_t filename_size,
1042 const char *preset_name, int is_path,
1043 const char *codec_name)
1047 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1052 av_strlcpy(filename, preset_name, filename_size);
1053 f = fopen(filename, "r");
1056 char datadir[MAX_PATH], *ls;
1059 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1061 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1062 if (*ls == '\\') *ls = '/';
1064 if (ls = strrchr(datadir, '/'))
1067 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1072 for (i = 0; i < 3 && !f; i++) {
1075 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1076 i != 1 ? "" : "/.ffmpeg", preset_name);
1077 f = fopen(filename, "r");
1078 if (!f && codec_name) {
1079 snprintf(filename, filename_size,
1080 "%s%s/%s-%s.ffpreset",
1081 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1083 f = fopen(filename, "r");
1091 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1093 if (*spec <= '9' && *spec >= '0') /* opt:index */
1094 return strtol(spec, NULL, 0) == st->index;
1095 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1096 *spec == 't') { /* opt:[vasdt] */
1097 enum AVMediaType type;
1100 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1101 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1102 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1103 case 'd': type = AVMEDIA_TYPE_DATA; break;
1104 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1105 default: abort(); // never reached, silence warning
1107 if (type != st->codec->codec_type)
1109 if (*spec++ == ':') { /* possibly followed by :index */
1110 int i, index = strtol(spec, NULL, 0);
1111 for (i = 0; i < s->nb_streams; i++)
1112 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1113 return i == st->index;
1117 } else if (*spec == 'p' && *(spec + 1) == ':') {
1121 prog_id = strtol(spec, &endptr, 0);
1122 for (i = 0; i < s->nb_programs; i++) {
1123 if (s->programs[i]->id != prog_id)
1126 if (*endptr++ == ':') {
1127 int stream_idx = strtol(endptr, NULL, 0);
1128 return stream_idx >= 0 &&
1129 stream_idx < s->programs[i]->nb_stream_indexes &&
1130 st->index == s->programs[i]->stream_index[stream_idx];
1133 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1134 if (st->index == s->programs[i]->stream_index[j])
1138 } else if (!*spec) /* empty specifier, matches everything */
1141 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1142 return AVERROR(EINVAL);
1145 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
1146 AVFormatContext *s, AVStream *st)
1148 AVDictionary *ret = NULL;
1149 AVDictionaryEntry *t = NULL;
1150 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1151 : AV_OPT_FLAG_DECODING_PARAM;
1153 const AVClass *cc = avcodec_get_class();
1158 switch (codec->type) {
1159 case AVMEDIA_TYPE_VIDEO:
1161 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1163 case AVMEDIA_TYPE_AUDIO:
1165 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1167 case AVMEDIA_TYPE_SUBTITLE:
1169 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1173 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1174 char *p = strchr(t->key, ':');
1176 /* check stream specification in opt name */
1178 switch (check_stream_specifier(s, st, p + 1)) {
1179 case 1: *p = 0; break;
1181 default: return NULL;
1184 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1185 (codec && codec->priv_class &&
1186 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1187 AV_OPT_SEARCH_FAKE_OBJ)))
1188 av_dict_set(&ret, t->key, t->value, 0);
1189 else if (t->key[0] == prefix &&
1190 av_opt_find(&cc, t->key + 1, NULL, flags,
1191 AV_OPT_SEARCH_FAKE_OBJ))
1192 av_dict_set(&ret, t->key + 1, t->value, 0);
1200 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1201 AVDictionary *codec_opts)
1204 AVDictionary **opts;
1208 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1210 av_log(NULL, AV_LOG_ERROR,
1211 "Could not alloc memory for stream options.\n");
1214 for (i = 0; i < s->nb_streams; i++)
1215 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
1220 void *grow_array(void *array, int elem_size, int *size, int new_size)
1222 if (new_size >= INT_MAX / elem_size) {
1223 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1226 if (*size < new_size) {
1227 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1229 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1232 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);