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 "libavresample/avresample.h"
36 #include "libswscale/swscale.h"
37 #include "libswresample/swresample.h"
39 #include "libpostproc/postprocess.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/pixdesc.h"
45 #include "libavutil/eval.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/opt.h"
51 #include "libavformat/network.h"
53 #if HAVE_SYS_RESOURCE_H
54 #include <sys/resource.h>
57 struct SwsContext *sws_opts;
59 AVDictionary *format_opts, *codec_opts;
61 const int this_year = 2012;
63 static FILE *report_file;
68 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
71 swr_opts = swr_alloc();
74 void uninit_opts(void)
77 sws_freeContext(sws_opts);
81 av_dict_free(&format_opts);
82 av_dict_free(&codec_opts);
85 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
87 vfprintf(stdout, fmt, vl);
90 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
94 static int print_prefix = 1;
97 av_log_default_callback(ptr, level, fmt, vl);
98 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
100 fputs(line, report_file);
104 double parse_number_or_die(const char *context, const char *numstr, int type,
105 double min, double max)
109 double d = av_strtod(numstr, &tail);
111 error = "Expected number for %s but found: %s\n";
112 else if (d < min || d > max)
113 error = "The value for %s was %s which is not within %f - %f\n";
114 else if (type == OPT_INT64 && (int64_t)d != d)
115 error = "Expected int64 for %s but found %s\n";
116 else if (type == OPT_INT && (int)d != d)
117 error = "Expected int for %s but found %s\n";
120 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
125 int64_t parse_time_or_die(const char *context, const char *timestr,
129 if (av_parse_time(&us, timestr, is_duration) < 0) {
130 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
131 is_duration ? "duration" : "date", context, timestr);
137 void show_help_options(const OptionDef *options, const char *msg, int mask,
144 for (po = options; po->name != NULL; po++) {
146 if ((po->flags & mask) == value) {
151 av_strlcpy(buf, po->name, sizeof(buf));
152 if (po->flags & HAS_ARG) {
153 av_strlcat(buf, " ", sizeof(buf));
154 av_strlcat(buf, po->argname, sizeof(buf));
156 printf("-%-17s %s\n", buf, po->help);
161 void show_help_children(const AVClass *class, int flags)
163 const AVClass *child = NULL;
164 av_opt_show2(&class, NULL, flags, 0);
167 while (child = av_opt_child_class_next(class, child))
168 show_help_children(child, flags);
171 static const OptionDef *find_option(const OptionDef *po, const char *name)
173 const char *p = strchr(name, ':');
174 int len = p ? p - name : strlen(name);
176 while (po->name != NULL) {
177 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
184 #if defined(_WIN32) && !defined(__MINGW32CE__)
186 /* Will be leaked on exit */
187 static char** win32_argv_utf8 = NULL;
188 static int win32_argc = 0;
191 * Prepare command line arguments for executable.
192 * For Windows - perform wide-char to UTF-8 conversion.
193 * Input arguments should be main() function arguments.
194 * @param argc_ptr Arguments number (including executable)
195 * @param argv_ptr Arguments list.
197 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
201 int i, buffsize = 0, offset = 0;
203 if (win32_argv_utf8) {
204 *argc_ptr = win32_argc;
205 *argv_ptr = win32_argv_utf8;
210 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
211 if (win32_argc <= 0 || !argv_w)
214 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
215 for (i = 0; i < win32_argc; i++)
216 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
217 NULL, 0, NULL, NULL);
219 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
220 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
221 if (win32_argv_utf8 == NULL) {
226 for (i = 0; i < win32_argc; i++) {
227 win32_argv_utf8[i] = &argstr_flat[offset];
228 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
229 &argstr_flat[offset],
230 buffsize - offset, NULL, NULL);
232 win32_argv_utf8[i] = NULL;
235 *argc_ptr = win32_argc;
236 *argv_ptr = win32_argv_utf8;
239 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
243 #endif /* WIN32 && !__MINGW32CE__ */
245 int parse_option(void *optctx, const char *opt, const char *arg,
246 const OptionDef *options)
253 po = find_option(options, opt);
254 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
255 /* handle 'no' bool option */
256 po = find_option(options, opt + 2);
257 if ((po->name && (po->flags & OPT_BOOL)))
261 po = find_option(options, "default");
263 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
264 return AVERROR(EINVAL);
266 if (po->flags & HAS_ARG && !arg) {
267 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
268 return AVERROR(EINVAL);
271 /* new-style options contain an offset into optctx, old-style address of
273 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
276 if (po->flags & OPT_SPEC) {
277 SpecifierOpt **so = dst;
278 char *p = strchr(opt, ':');
280 dstcount = (int *)(so + 1);
281 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
282 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
283 dst = &(*so)[*dstcount - 1].u;
286 if (po->flags & OPT_STRING) {
288 str = av_strdup(arg);
290 } else if (po->flags & OPT_BOOL) {
291 *(int *)dst = bool_val;
292 } else if (po->flags & OPT_INT) {
293 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
294 } else if (po->flags & OPT_INT64) {
295 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
296 } else if (po->flags & OPT_TIME) {
297 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
298 } else if (po->flags & OPT_FLOAT) {
299 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
300 } else if (po->flags & OPT_DOUBLE) {
301 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
302 } else if (po->u.func_arg) {
303 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
304 : po->u.func_arg(opt, arg);
306 av_log(NULL, AV_LOG_ERROR,
307 "Failed to set value '%s' for option '%s'\n", arg, opt);
311 if (po->flags & OPT_EXIT)
313 return !!(po->flags & HAS_ARG);
316 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
317 void (*parse_arg_function)(void *, const char*))
320 int optindex, handleoptions = 1, ret;
322 /* perform system-dependent conversions for arguments list */
323 prepare_app_arguments(&argc, &argv);
327 while (optindex < argc) {
328 opt = argv[optindex++];
330 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
331 if (opt[1] == '-' && opt[2] == '\0') {
337 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
341 if (parse_arg_function)
342 parse_arg_function(optctx, opt);
347 int locate_option(int argc, char **argv, const OptionDef *options,
353 for (i = 1; i < argc; i++) {
354 const char *cur_opt = argv[i];
356 if (*cur_opt++ != '-')
359 po = find_option(options, cur_opt);
360 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
361 po = find_option(options, cur_opt + 2);
363 if ((!po->name && !strcmp(cur_opt, optname)) ||
364 (po->name && !strcmp(optname, po->name)))
367 if (!po || po->flags & HAS_ARG)
373 static void dump_argument(const char *a)
375 const unsigned char *p;
378 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
379 *p == '_' || (*p >= 'a' && *p <= 'z')))
382 fputs(a, report_file);
385 fputc('"', report_file);
386 for (p = a; *p; p++) {
387 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
388 fprintf(report_file, "\\%c", *p);
389 else if (*p < ' ' || *p > '~')
390 fprintf(report_file, "\\x%02x", *p);
392 fputc(*p, report_file);
394 fputc('"', report_file);
397 void parse_loglevel(int argc, char **argv, const OptionDef *options)
399 int idx = locate_option(argc, argv, options, "loglevel");
401 idx = locate_option(argc, argv, options, "v");
402 if (idx && argv[idx + 1])
403 opt_loglevel("loglevel", argv[idx + 1]);
404 idx = locate_option(argc, argv, options, "report");
405 if (idx || getenv("FFREPORT")) {
406 opt_report("report");
409 fprintf(report_file, "Command line:\n");
410 for (i = 0; i < argc; i++) {
411 dump_argument(argv[i]);
412 fputc(i < argc - 1 ? ' ' : '\n', report_file);
419 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
420 int opt_default(const char *opt, const char *arg)
422 const AVOption *oc, *of, *os, *oswr = NULL;
423 char opt_stripped[128];
425 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
427 if (!(p = strchr(opt, ':')))
428 p = opt + strlen(opt);
429 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
431 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0,
432 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
433 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
434 (oc = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
435 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
436 if ((of = av_opt_find(&fc, opt, NULL, 0,
437 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
438 av_dict_set(&format_opts, opt, arg, FLAGS(of));
440 sc = sws_get_class();
441 if ((os = av_opt_find(&sc, opt, NULL, 0,
442 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
443 // XXX we only support sws_flags, not arbitrary sws options
444 int ret = av_opt_set(sws_opts, opt, arg, 0);
446 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
451 swr_class = swr_get_class();
452 if (!oc && !of && !os && (oswr = av_opt_find(&swr_class, opt, NULL, 0,
453 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
454 int ret = av_opt_set(swr_opts, opt, arg, 0);
456 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
461 if (oc || of || os || oswr)
463 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
464 return AVERROR_OPTION_NOT_FOUND;
467 int opt_loglevel(const char *opt, const char *arg)
469 const struct { const char *name; int level; } log_levels[] = {
470 { "quiet" , AV_LOG_QUIET },
471 { "panic" , AV_LOG_PANIC },
472 { "fatal" , AV_LOG_FATAL },
473 { "error" , AV_LOG_ERROR },
474 { "warning", AV_LOG_WARNING },
475 { "info" , AV_LOG_INFO },
476 { "verbose", AV_LOG_VERBOSE },
477 { "debug" , AV_LOG_DEBUG },
483 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
484 if (!strcmp(log_levels[i].name, arg)) {
485 av_log_set_level(log_levels[i].level);
490 level = strtol(arg, &tail, 10);
492 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
493 "Possible levels are numbers or:\n", arg);
494 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
495 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
498 av_log_set_level(level);
502 int opt_report(const char *opt)
508 if (report_file) /* already opened */
511 tm = localtime(&now);
512 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
514 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
515 tm->tm_hour, tm->tm_min, tm->tm_sec);
516 report_file = fopen(filename, "w");
518 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
519 filename, strerror(errno));
520 return AVERROR(errno);
522 av_log_set_callback(log_callback_report);
523 av_log(NULL, AV_LOG_INFO,
524 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
525 "Report written to \"%s\"\n",
527 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
528 tm->tm_hour, tm->tm_min, tm->tm_sec,
530 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
534 int opt_max_alloc(const char *opt, const char *arg)
539 max = strtol(arg, &tail, 10);
541 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
548 int opt_cpuflags(const char *opt, const char *arg)
551 unsigned flags = av_get_cpu_flags();
553 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
556 av_force_cpu_flags(flags);
560 int opt_codec_debug(const char *opt, const char *arg)
562 av_log_set_level(AV_LOG_DEBUG);
563 return opt_default(opt, arg);
566 int opt_timelimit(const char *opt, const char *arg)
569 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
570 struct rlimit rl = { lim, lim + 1 };
571 if (setrlimit(RLIMIT_CPU, &rl))
574 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
579 void print_error(const char *filename, int err)
582 const char *errbuf_ptr = errbuf;
584 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
585 errbuf_ptr = strerror(AVUNERROR(err));
586 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
589 static int warned_cfg = 0;
592 #define SHOW_VERSION 2
593 #define SHOW_CONFIG 4
594 #define SHOW_COPYRIGHT 8
596 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
597 if (CONFIG_##LIBNAME) { \
598 const char *indent = flags & INDENT? " " : ""; \
599 if (flags & SHOW_VERSION) { \
600 unsigned int version = libname##_version(); \
601 av_log(NULL, level, \
602 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
604 LIB##LIBNAME##_VERSION_MAJOR, \
605 LIB##LIBNAME##_VERSION_MINOR, \
606 LIB##LIBNAME##_VERSION_MICRO, \
607 version >> 16, version >> 8 & 0xff, version & 0xff); \
609 if (flags & SHOW_CONFIG) { \
610 const char *cfg = libname##_configuration(); \
611 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
613 av_log(NULL, level, \
614 "%sWARNING: library configuration mismatch\n", \
618 av_log(NULL, level, "%s%-11s configuration: %s\n", \
619 indent, #libname, cfg); \
624 static void print_all_libs_info(int flags, int level)
626 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
627 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
628 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
629 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
630 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
631 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
632 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
633 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
635 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
639 static void print_program_info(int flags, int level)
641 const char *indent = flags & INDENT? " " : "";
643 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
644 if (flags & SHOW_COPYRIGHT)
645 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
646 program_birth_year, this_year);
647 av_log(NULL, level, "\n");
648 av_log(NULL, level, "%sbuilt on %s %s with %s %s\n",
649 indent, __DATE__, __TIME__, CC_TYPE, CC_VERSION);
650 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
653 void show_banner(int argc, char **argv, const OptionDef *options)
655 int idx = locate_option(argc, argv, options, "version");
659 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
660 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
661 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
664 int opt_version(const char *opt, const char *arg) {
665 av_log_set_callback(log_callback_help);
666 print_program_info (0 , AV_LOG_INFO);
667 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
671 int opt_license(const char *opt, const char *arg)
675 "This version of %s has nonfree parts compiled in.\n"
676 "Therefore it is not legally redistributable.\n",
679 "%s is free software; you can redistribute it and/or modify\n"
680 "it under the terms of the GNU General Public License as published by\n"
681 "the Free Software Foundation; either version 3 of the License, or\n"
682 "(at your option) any later version.\n"
684 "%s is distributed in the hope that it will be useful,\n"
685 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
686 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
687 "GNU General Public License for more details.\n"
689 "You should have received a copy of the GNU General Public License\n"
690 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
691 program_name, program_name, program_name
693 "%s is free software; you can redistribute it and/or modify\n"
694 "it under the terms of the GNU General Public License as published by\n"
695 "the Free Software Foundation; either version 2 of the License, or\n"
696 "(at your option) any later version.\n"
698 "%s is distributed in the hope that it will be useful,\n"
699 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
700 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
701 "GNU General Public License for more details.\n"
703 "You should have received a copy of the GNU General Public License\n"
704 "along with %s; if not, write to the Free Software\n"
705 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
706 program_name, program_name, program_name
708 "%s is free software; you can redistribute it and/or modify\n"
709 "it under the terms of the GNU Lesser General Public License as published by\n"
710 "the Free Software Foundation; either version 3 of the License, or\n"
711 "(at your option) any later version.\n"
713 "%s is distributed in the hope that it will be useful,\n"
714 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
715 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
716 "GNU Lesser General Public License for more details.\n"
718 "You should have received a copy of the GNU Lesser General Public License\n"
719 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
720 program_name, program_name, program_name
722 "%s is free software; you can redistribute it and/or\n"
723 "modify it under the terms of the GNU Lesser General Public\n"
724 "License as published by the Free Software Foundation; either\n"
725 "version 2.1 of the License, or (at your option) any later version.\n"
727 "%s is distributed in the hope that it will be useful,\n"
728 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
729 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
730 "Lesser General Public License for more details.\n"
732 "You should have received a copy of the GNU Lesser General Public\n"
733 "License along with %s; if not, write to the Free Software\n"
734 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
735 program_name, program_name, program_name
741 int opt_formats(const char *opt, const char *arg)
743 AVInputFormat *ifmt = NULL;
744 AVOutputFormat *ofmt = NULL;
745 const char *last_name;
747 printf("File formats:\n"
748 " D. = Demuxing supported\n"
749 " .E = Muxing supported\n"
755 const char *name = NULL;
756 const char *long_name = NULL;
758 while ((ofmt = av_oformat_next(ofmt))) {
759 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
760 strcmp(ofmt->name, last_name) > 0) {
762 long_name = ofmt->long_name;
766 while ((ifmt = av_iformat_next(ifmt))) {
767 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
768 strcmp(ifmt->name, last_name) > 0) {
770 long_name = ifmt->long_name;
773 if (name && strcmp(ifmt->name, name) == 0)
780 printf(" %s%s %-15s %s\n",
784 long_name ? long_name:" ");
789 static char get_media_type_char(enum AVMediaType type)
791 static const char map[AVMEDIA_TYPE_NB] = {
792 [AVMEDIA_TYPE_VIDEO] = 'V',
793 [AVMEDIA_TYPE_AUDIO] = 'A',
794 [AVMEDIA_TYPE_DATA] = 'D',
795 [AVMEDIA_TYPE_SUBTITLE] = 'S',
796 [AVMEDIA_TYPE_ATTACHMENT] = 'T',
798 return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
801 int opt_codecs(const char *opt, const char *arg)
803 AVCodec *p = NULL, *p2;
804 const char *last_name;
806 " D..... = Decoding supported\n"
807 " .E.... = Encoding supported\n"
808 " ..V... = Video codec\n"
809 " ..A... = Audio codec\n"
810 " ..S... = Subtitle codec\n"
811 " ...S.. = Supports draw_horiz_band\n"
812 " ....D. = Supports direct rendering method 1\n"
813 " .....T = Supports weird frame truncation\n"
822 while ((p = av_codec_next(p))) {
823 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
824 strcmp(p->name, last_name) > 0) {
826 decode = encode = cap = 0;
828 if (p2 && strcmp(p->name, p2->name) == 0) {
829 if (av_codec_is_decoder(p))
831 if (av_codec_is_encoder(p))
833 cap |= p->capabilities;
838 last_name = p2->name;
840 printf(" %s%s%c%s%s%s %-15s %s",
841 decode ? "D" : (/* p2->decoder ? "d" : */ " "),
843 get_media_type_char(p2->type),
844 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
845 cap & CODEC_CAP_DR1 ? "D" : " ",
846 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
848 p2->long_name ? p2->long_name : "");
850 if (p2->decoder && decode == 0)
851 printf(" use %s for decoding", p2->decoder->name);
856 printf("Note, the names of encoders and decoders do not always match, so there are\n"
857 "several cases where the above table shows encoder only or decoder only entries\n"
858 "even though both encoding and decoding are supported. For example, the h263\n"
859 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
864 int opt_bsfs(const char *opt, const char *arg)
866 AVBitStreamFilter *bsf = NULL;
868 printf("Bitstream filters:\n");
869 while ((bsf = av_bitstream_filter_next(bsf)))
870 printf("%s\n", bsf->name);
875 int opt_protocols(const char *opt, const char *arg)
880 printf("Supported file protocols:\n"
882 while ((name = avio_enum_protocols(&opaque, 0)))
883 printf("%s\n", name);
885 while ((name = avio_enum_protocols(&opaque, 1)))
886 printf("%s\n", name);
890 int opt_filters(const char *opt, const char *arg)
892 AVFilter av_unused(**filter) = NULL;
893 char descr[64], *descr_cur;
895 const AVFilterPad *pad;
897 printf("Filters:\n");
899 while ((filter = av_filter_next(filter)) && *filter) {
901 for (i = 0; i < 2; i++) {
903 *(descr_cur++) = '-';
904 *(descr_cur++) = '>';
906 pad = i ? (*filter)->outputs : (*filter)->inputs;
907 for (j = 0; pad[j].name; j++) {
908 if (descr_cur >= descr + sizeof(descr) - 4)
910 *(descr_cur++) = get_media_type_char(pad[j].type);
913 *(descr_cur++) = '|';
916 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
922 int opt_pix_fmts(const char *opt, const char *arg)
924 enum PixelFormat pix_fmt;
926 printf("Pixel formats:\n"
927 "I.... = Supported Input format for conversion\n"
928 ".O... = Supported Output format for conversion\n"
929 "..H.. = Hardware accelerated format\n"
930 "...P. = Paletted format\n"
931 "....B = Bitstream format\n"
932 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
936 # define sws_isSupportedInput(x) 0
937 # define sws_isSupportedOutput(x) 0
940 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
941 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
944 printf("%c%c%c%c%c %-16s %d %2d\n",
945 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
946 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
947 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
948 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
949 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
951 pix_desc->nb_components,
952 av_get_bits_per_pixel(pix_desc));
957 int show_sample_fmts(const char *opt, const char *arg)
961 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
962 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
969 int yesno = (toupper(c) == 'Y');
971 while (c != '\n' && c != EOF)
977 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
980 FILE *f = fopen(filename, "rb");
983 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
985 return AVERROR(errno);
987 fseek(f, 0, SEEK_END);
989 fseek(f, 0, SEEK_SET);
990 *bufptr = av_malloc(*size + 1);
992 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
994 return AVERROR(ENOMEM);
996 ret = fread(*bufptr, 1, *size, f);
1000 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1001 filename, strerror(errno));
1002 ret = AVERROR(errno);
1007 (*bufptr)[*size++] = '\0';
1014 FILE *get_preset_file(char *filename, size_t filename_size,
1015 const char *preset_name, int is_path,
1016 const char *codec_name)
1020 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1025 av_strlcpy(filename, preset_name, filename_size);
1026 f = fopen(filename, "r");
1029 char datadir[MAX_PATH], *ls;
1032 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1034 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1035 if (*ls == '\\') *ls = '/';
1037 if (ls = strrchr(datadir, '/'))
1040 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1045 for (i = 0; i < 3 && !f; i++) {
1048 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1049 i != 1 ? "" : "/.ffmpeg", preset_name);
1050 f = fopen(filename, "r");
1051 if (!f && codec_name) {
1052 snprintf(filename, filename_size,
1053 "%s%s/%s-%s.ffpreset",
1054 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1056 f = fopen(filename, "r");
1064 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1066 if (*spec <= '9' && *spec >= '0') /* opt:index */
1067 return strtol(spec, NULL, 0) == st->index;
1068 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1069 *spec == 't') { /* opt:[vasdt] */
1070 enum AVMediaType type;
1073 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1074 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1075 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1076 case 'd': type = AVMEDIA_TYPE_DATA; break;
1077 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1078 default: abort(); // never reached, silence warning
1080 if (type != st->codec->codec_type)
1082 if (*spec++ == ':') { /* possibly followed by :index */
1083 int i, index = strtol(spec, NULL, 0);
1084 for (i = 0; i < s->nb_streams; i++)
1085 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1086 return i == st->index;
1090 } else if (*spec == 'p' && *(spec + 1) == ':') {
1094 prog_id = strtol(spec, &endptr, 0);
1095 for (i = 0; i < s->nb_programs; i++) {
1096 if (s->programs[i]->id != prog_id)
1099 if (*endptr++ == ':') {
1100 int stream_idx = strtol(endptr, NULL, 0);
1101 return stream_idx >= 0 &&
1102 stream_idx < s->programs[i]->nb_stream_indexes &&
1103 st->index == s->programs[i]->stream_index[stream_idx];
1106 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1107 if (st->index == s->programs[i]->stream_index[j])
1111 } else if (!*spec) /* empty specifier, matches everything */
1114 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1115 return AVERROR(EINVAL);
1118 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
1119 AVFormatContext *s, AVStream *st)
1121 AVDictionary *ret = NULL;
1122 AVDictionaryEntry *t = NULL;
1123 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1124 : AV_OPT_FLAG_DECODING_PARAM;
1126 const AVClass *cc = avcodec_get_class();
1131 switch (codec->type) {
1132 case AVMEDIA_TYPE_VIDEO:
1134 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1136 case AVMEDIA_TYPE_AUDIO:
1138 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1140 case AVMEDIA_TYPE_SUBTITLE:
1142 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1146 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1147 char *p = strchr(t->key, ':');
1149 /* check stream specification in opt name */
1151 switch (check_stream_specifier(s, st, p + 1)) {
1152 case 1: *p = 0; break;
1154 default: return NULL;
1157 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1158 (codec && codec->priv_class &&
1159 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1160 AV_OPT_SEARCH_FAKE_OBJ)))
1161 av_dict_set(&ret, t->key, t->value, 0);
1162 else if (t->key[0] == prefix &&
1163 av_opt_find(&cc, t->key + 1, NULL, flags,
1164 AV_OPT_SEARCH_FAKE_OBJ))
1165 av_dict_set(&ret, t->key + 1, t->value, 0);
1173 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1174 AVDictionary *codec_opts)
1177 AVDictionary **opts;
1181 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1183 av_log(NULL, AV_LOG_ERROR,
1184 "Could not alloc memory for stream options.\n");
1187 for (i = 0; i < s->nb_streams; i++)
1188 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
1193 void *grow_array(void *array, int elem_size, int *size, int new_size)
1195 if (new_size >= INT_MAX / elem_size) {
1196 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1199 if (*size < new_size) {
1200 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1202 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1205 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);