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/avassert.h"
42 #include "libavutil/avstring.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/imgutils.h"
45 #include "libavutil/parseutils.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/eval.h"
48 #include "libavutil/dict.h"
49 #include "libavutil/opt.h"
53 #include "libavformat/network.h"
55 #if HAVE_SYS_RESOURCE_H
56 #include <sys/resource.h>
59 struct SwsContext *sws_opts;
61 AVDictionary *format_opts, *codec_opts;
63 const int this_year = 2012;
65 static FILE *report_file;
71 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
75 swr_opts = swr_alloc();
78 void uninit_opts(void)
81 sws_freeContext(sws_opts);
88 av_dict_free(&format_opts);
89 av_dict_free(&codec_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 fputs(line, report_file);
111 double parse_number_or_die(const char *context, const char *numstr, int type,
112 double min, double max)
116 double d = av_strtod(numstr, &tail);
118 error = "Expected number for %s but found: %s\n";
119 else if (d < min || d > max)
120 error = "The value for %s was %s which is not within %f - %f\n";
121 else if (type == OPT_INT64 && (int64_t)d != d)
122 error = "Expected int64 for %s but found %s\n";
123 else if (type == OPT_INT && (int)d != d)
124 error = "Expected int for %s but found %s\n";
127 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
132 int64_t parse_time_or_die(const char *context, const char *timestr,
136 if (av_parse_time(&us, timestr, is_duration) < 0) {
137 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
138 is_duration ? "duration" : "date", context, timestr);
144 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
145 int rej_flags, int alt_flags)
151 for (po = options; po->name != NULL; po++) {
154 if (((po->flags & req_flags) != req_flags) ||
155 (alt_flags && !(po->flags & alt_flags)) ||
156 (po->flags & rej_flags))
163 av_strlcpy(buf, po->name, sizeof(buf));
165 av_strlcat(buf, " ", sizeof(buf));
166 av_strlcat(buf, po->argname, sizeof(buf));
168 printf("-%-17s %s\n", buf, po->help);
173 void show_help_children(const AVClass *class, int flags)
175 const AVClass *child = NULL;
177 av_opt_show2(&class, NULL, flags, 0);
181 while (child = av_opt_child_class_next(class, child))
182 show_help_children(child, flags);
185 static const OptionDef *find_option(const OptionDef *po, const char *name)
187 const char *p = strchr(name, ':');
188 int len = p ? p - name : strlen(name);
190 while (po->name != NULL) {
191 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
198 #if defined(_WIN32) && !defined(__MINGW32CE__)
200 #include <shellapi.h>
201 /* Will be leaked on exit */
202 static char** win32_argv_utf8 = NULL;
203 static int win32_argc = 0;
206 * Prepare command line arguments for executable.
207 * For Windows - perform wide-char to UTF-8 conversion.
208 * Input arguments should be main() function arguments.
209 * @param argc_ptr Arguments number (including executable)
210 * @param argv_ptr Arguments list.
212 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
216 int i, buffsize = 0, offset = 0;
218 if (win32_argv_utf8) {
219 *argc_ptr = win32_argc;
220 *argv_ptr = win32_argv_utf8;
225 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
226 if (win32_argc <= 0 || !argv_w)
229 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
230 for (i = 0; i < win32_argc; i++)
231 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
232 NULL, 0, NULL, NULL);
234 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
235 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
236 if (win32_argv_utf8 == NULL) {
241 for (i = 0; i < win32_argc; i++) {
242 win32_argv_utf8[i] = &argstr_flat[offset];
243 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
244 &argstr_flat[offset],
245 buffsize - offset, NULL, NULL);
247 win32_argv_utf8[i] = NULL;
250 *argc_ptr = win32_argc;
251 *argv_ptr = win32_argv_utf8;
254 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
258 #endif /* WIN32 && !__MINGW32CE__ */
260 int parse_option(void *optctx, const char *opt, const char *arg,
261 const OptionDef *options)
268 po = find_option(options, opt);
269 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
270 /* handle 'no' bool option */
271 po = find_option(options, opt + 2);
272 if ((po->name && (po->flags & OPT_BOOL)))
276 po = find_option(options, "default");
278 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
279 return AVERROR(EINVAL);
281 if (po->flags & HAS_ARG && !arg) {
282 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
283 return AVERROR(EINVAL);
286 /* new-style options contain an offset into optctx, old-style address of
288 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
291 if (po->flags & OPT_SPEC) {
292 SpecifierOpt **so = dst;
293 char *p = strchr(opt, ':');
295 dstcount = (int *)(so + 1);
296 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
297 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
298 dst = &(*so)[*dstcount - 1].u;
301 if (po->flags & OPT_STRING) {
303 str = av_strdup(arg);
305 } else if (po->flags & OPT_BOOL) {
306 *(int *)dst = bool_val;
307 } else if (po->flags & OPT_INT) {
308 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
309 } else if (po->flags & OPT_INT64) {
310 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
311 } else if (po->flags & OPT_TIME) {
312 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
313 } else if (po->flags & OPT_FLOAT) {
314 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
315 } else if (po->flags & OPT_DOUBLE) {
316 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
317 } else if (po->u.func_arg) {
318 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
319 : po->u.func_arg(opt, arg);
321 av_log(NULL, AV_LOG_ERROR,
322 "Failed to set value '%s' for option '%s'\n", arg, opt);
326 if (po->flags & OPT_EXIT)
328 return !!(po->flags & HAS_ARG);
331 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
332 void (*parse_arg_function)(void *, const char*))
335 int optindex, handleoptions = 1, ret;
337 /* perform system-dependent conversions for arguments list */
338 prepare_app_arguments(&argc, &argv);
342 while (optindex < argc) {
343 opt = argv[optindex++];
345 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
346 if (opt[1] == '-' && opt[2] == '\0') {
352 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
356 if (parse_arg_function)
357 parse_arg_function(optctx, opt);
362 int locate_option(int argc, char **argv, const OptionDef *options,
368 for (i = 1; i < argc; i++) {
369 const char *cur_opt = argv[i];
371 if (*cur_opt++ != '-')
374 po = find_option(options, cur_opt);
375 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
376 po = find_option(options, cur_opt + 2);
378 if ((!po->name && !strcmp(cur_opt, optname)) ||
379 (po->name && !strcmp(optname, po->name)))
382 if (!po || po->flags & HAS_ARG)
388 static void dump_argument(const char *a)
390 const unsigned char *p;
393 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
394 *p == '_' || (*p >= 'a' && *p <= 'z')))
397 fputs(a, report_file);
400 fputc('"', report_file);
401 for (p = a; *p; p++) {
402 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
403 fprintf(report_file, "\\%c", *p);
404 else if (*p < ' ' || *p > '~')
405 fprintf(report_file, "\\x%02x", *p);
407 fputc(*p, report_file);
409 fputc('"', report_file);
412 void parse_loglevel(int argc, char **argv, const OptionDef *options)
414 int idx = locate_option(argc, argv, options, "loglevel");
416 idx = locate_option(argc, argv, options, "v");
417 if (idx && argv[idx + 1])
418 opt_loglevel("loglevel", argv[idx + 1]);
419 idx = locate_option(argc, argv, options, "report");
420 if (idx || getenv("FFREPORT")) {
421 opt_report("report");
424 fprintf(report_file, "Command line:\n");
425 for (i = 0; i < argc; i++) {
426 dump_argument(argv[i]);
427 fputc(i < argc - 1 ? ' ' : '\n', report_file);
434 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
435 int opt_default(const char *opt, const char *arg)
438 char opt_stripped[128];
440 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
442 if (!(p = strchr(opt, ':')))
443 p = opt + strlen(opt);
444 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
446 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
447 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
448 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
449 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
450 av_dict_set(&codec_opts, opt, arg, FLAGS);
451 else if ((o = av_opt_find(&fc, opt, NULL, 0,
452 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
453 av_dict_set(&format_opts, opt, arg, FLAGS);
455 sc = sws_get_class();
456 if (!o && (o = av_opt_find(&sc, opt, NULL, 0,
457 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
458 // XXX we only support sws_flags, not arbitrary sws options
459 int ret = av_opt_set(sws_opts, opt, arg, 0);
461 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
466 #if CONFIG_SWRESAMPLE
467 swr_class = swr_get_class();
468 if (!o && (o = av_opt_find(&swr_class, opt, NULL, 0,
469 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
470 int ret = av_opt_set(swr_opts, opt, arg, 0);
472 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
480 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
481 return AVERROR_OPTION_NOT_FOUND;
484 int opt_loglevel(const char *opt, const char *arg)
486 const struct { const char *name; int level; } log_levels[] = {
487 { "quiet" , AV_LOG_QUIET },
488 { "panic" , AV_LOG_PANIC },
489 { "fatal" , AV_LOG_FATAL },
490 { "error" , AV_LOG_ERROR },
491 { "warning", AV_LOG_WARNING },
492 { "info" , AV_LOG_INFO },
493 { "verbose", AV_LOG_VERBOSE },
494 { "debug" , AV_LOG_DEBUG },
500 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
501 if (!strcmp(log_levels[i].name, arg)) {
502 av_log_set_level(log_levels[i].level);
507 level = strtol(arg, &tail, 10);
509 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
510 "Possible levels are numbers or:\n", arg);
511 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
512 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
515 av_log_set_level(level);
519 int opt_report(const char *opt)
525 if (report_file) /* already opened */
528 tm = localtime(&now);
529 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
531 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
532 tm->tm_hour, tm->tm_min, tm->tm_sec);
533 report_file = fopen(filename, "w");
535 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
536 filename, strerror(errno));
537 return AVERROR(errno);
539 av_log_set_callback(log_callback_report);
540 av_log(NULL, AV_LOG_INFO,
541 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
542 "Report written to \"%s\"\n",
544 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
545 tm->tm_hour, tm->tm_min, tm->tm_sec,
547 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
551 int opt_max_alloc(const char *opt, const char *arg)
556 max = strtol(arg, &tail, 10);
558 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
565 int opt_cpuflags(const char *opt, const char *arg)
568 unsigned flags = av_get_cpu_flags();
570 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
573 av_force_cpu_flags(flags);
577 int opt_codec_debug(const char *opt, const char *arg)
579 av_log_set_level(AV_LOG_DEBUG);
580 return opt_default(opt, arg);
583 int opt_timelimit(const char *opt, const char *arg)
586 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
587 struct rlimit rl = { lim, lim + 1 };
588 if (setrlimit(RLIMIT_CPU, &rl))
591 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
596 void print_error(const char *filename, int err)
599 const char *errbuf_ptr = errbuf;
601 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
602 errbuf_ptr = strerror(AVUNERROR(err));
603 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
606 static int warned_cfg = 0;
609 #define SHOW_VERSION 2
610 #define SHOW_CONFIG 4
611 #define SHOW_COPYRIGHT 8
613 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
614 if (CONFIG_##LIBNAME) { \
615 const char *indent = flags & INDENT? " " : ""; \
616 if (flags & SHOW_VERSION) { \
617 unsigned int version = libname##_version(); \
618 av_log(NULL, level, \
619 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
621 LIB##LIBNAME##_VERSION_MAJOR, \
622 LIB##LIBNAME##_VERSION_MINOR, \
623 LIB##LIBNAME##_VERSION_MICRO, \
624 version >> 16, version >> 8 & 0xff, version & 0xff); \
626 if (flags & SHOW_CONFIG) { \
627 const char *cfg = libname##_configuration(); \
628 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
630 av_log(NULL, level, \
631 "%sWARNING: library configuration mismatch\n", \
635 av_log(NULL, level, "%s%-11s configuration: %s\n", \
636 indent, #libname, cfg); \
641 static void print_all_libs_info(int flags, int level)
643 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
644 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
645 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
646 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
647 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
648 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
649 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
650 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
652 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
656 static void print_program_info(int flags, int level)
658 const char *indent = flags & INDENT? " " : "";
660 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
661 if (flags & SHOW_COPYRIGHT)
662 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
663 program_birth_year, this_year);
664 av_log(NULL, level, "\n");
665 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
666 indent, __DATE__, __TIME__, CC_IDENT);
668 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
671 void show_banner(int argc, char **argv, const OptionDef *options)
673 int idx = locate_option(argc, argv, options, "version");
677 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
678 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
679 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
682 int show_version(const char *opt, const char *arg)
684 av_log_set_callback(log_callback_help);
685 print_program_info (0 , AV_LOG_INFO);
686 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
691 int show_license(const char *opt, const char *arg)
695 "This version of %s has nonfree parts compiled in.\n"
696 "Therefore it is not legally redistributable.\n",
699 "%s is free software; you can redistribute it and/or modify\n"
700 "it under the terms of the GNU General Public License as published by\n"
701 "the Free Software Foundation; either version 3 of the License, or\n"
702 "(at your option) any later version.\n"
704 "%s is distributed in the hope that it will be useful,\n"
705 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
706 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
707 "GNU General Public License for more details.\n"
709 "You should have received a copy of the GNU General Public License\n"
710 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
711 program_name, program_name, program_name
713 "%s is free software; you can redistribute it and/or modify\n"
714 "it under the terms of the GNU General Public License as published by\n"
715 "the Free Software Foundation; either version 2 of the License, or\n"
716 "(at your option) any later version.\n"
718 "%s is distributed in the hope that it will be useful,\n"
719 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
720 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
721 "GNU General Public License for more details.\n"
723 "You should have received a copy of the GNU General Public License\n"
724 "along with %s; if not, write to the Free Software\n"
725 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
726 program_name, program_name, program_name
728 "%s is free software; you can redistribute it and/or modify\n"
729 "it under the terms of the GNU Lesser General Public License as published by\n"
730 "the Free Software Foundation; either version 3 of the License, or\n"
731 "(at your option) any later version.\n"
733 "%s is distributed in the hope that it will be useful,\n"
734 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
735 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
736 "GNU Lesser General Public License for more details.\n"
738 "You should have received a copy of the GNU Lesser General Public License\n"
739 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
740 program_name, program_name, program_name
742 "%s is free software; you can redistribute it and/or\n"
743 "modify it under the terms of the GNU Lesser General Public\n"
744 "License as published by the Free Software Foundation; either\n"
745 "version 2.1 of the License, or (at your option) any later version.\n"
747 "%s is distributed in the hope that it will be useful,\n"
748 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
749 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
750 "Lesser General Public License for more details.\n"
752 "You should have received a copy of the GNU Lesser General Public\n"
753 "License along with %s; if not, write to the Free Software\n"
754 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
755 program_name, program_name, program_name
762 int show_formats(const char *opt, const char *arg)
764 AVInputFormat *ifmt = NULL;
765 AVOutputFormat *ofmt = NULL;
766 const char *last_name;
768 printf("File formats:\n"
769 " D. = Demuxing supported\n"
770 " .E = Muxing supported\n"
776 const char *name = NULL;
777 const char *long_name = NULL;
779 while ((ofmt = av_oformat_next(ofmt))) {
780 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
781 strcmp(ofmt->name, last_name) > 0) {
783 long_name = ofmt->long_name;
787 while ((ifmt = av_iformat_next(ifmt))) {
788 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
789 strcmp(ifmt->name, last_name) > 0) {
791 long_name = ifmt->long_name;
794 if (name && strcmp(ifmt->name, name) == 0)
801 printf(" %s%s %-15s %s\n",
805 long_name ? long_name:" ");
810 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
811 if (codec->field) { \
812 const type *p = c->field; \
814 printf(" Supported " list_name ":"); \
815 while (*p != term) { \
817 printf(" %s", name); \
823 static void print_codec(const AVCodec *c)
825 int encoder = av_codec_is_encoder(c);
827 printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
828 c->long_name ? c->long_name : "");
830 if (c->type == AVMEDIA_TYPE_VIDEO) {
831 printf(" Threading capabilities: ");
832 switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
833 CODEC_CAP_SLICE_THREADS)) {
834 case CODEC_CAP_FRAME_THREADS |
835 CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
836 case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
837 case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
838 default: printf("no"); break;
843 if (c->supported_framerates) {
844 const AVRational *fps = c->supported_framerates;
846 printf(" Supported framerates:");
848 printf(" %d/%d", fps->num, fps->den);
853 PRINT_CODEC_SUPPORTED(c, pix_fmts, enum PixelFormat, "pixel formats",
854 PIX_FMT_NONE, GET_PIX_FMT_NAME);
855 PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
856 GET_SAMPLE_RATE_NAME);
857 PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
858 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
859 PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
860 0, GET_CH_LAYOUT_DESC);
863 show_help_children(c->priv_class,
864 AV_OPT_FLAG_ENCODING_PARAM |
865 AV_OPT_FLAG_DECODING_PARAM);
869 static char get_media_type_char(enum AVMediaType type)
872 case AVMEDIA_TYPE_VIDEO: return 'V';
873 case AVMEDIA_TYPE_AUDIO: return 'A';
874 case AVMEDIA_TYPE_DATA: return 'D';
875 case AVMEDIA_TYPE_SUBTITLE: return 'S';
876 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
881 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
884 while ((prev = av_codec_next(prev))) {
885 if (prev->id == id &&
886 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
892 static void print_codecs_for_id(enum AVCodecID id, int encoder)
894 const AVCodec *codec = NULL;
896 printf(" (%s: ", encoder ? "encoders" : "decoders");
898 while ((codec = next_codec_for_id(id, codec, encoder)))
899 printf("%s ", codec->name);
904 int show_codecs(const char *opt, const char *arg)
906 const AVCodecDescriptor *desc = NULL;
909 " D... = Decoding supported\n"
910 " .E.. = Encoding supported\n"
911 " ..V. = Video codec\n"
912 " ..A. = Audio codec\n"
913 " ..S. = Subtitle codec\n"
914 " ...I = Intra frame-only codec\n"
916 while ((desc = avcodec_descriptor_next(desc))) {
917 const AVCodec *codec = NULL;
919 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
920 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
922 printf("%c", get_media_type_char(desc->type));
923 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
925 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
927 /* print decoders/encoders when there's more than one or their
928 * names are different from codec name */
929 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
930 if (strcmp(codec->name, desc->name)) {
931 print_codecs_for_id(desc->id, 0);
936 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
937 if (strcmp(codec->name, desc->name)) {
938 print_codecs_for_id(desc->id, 1);
948 static void print_codecs(int encoder)
950 const AVCodecDescriptor *desc = NULL;
955 " S..... = Subtitle\n"
956 " .F.... = Frame-level multithreading\n"
957 " ..S... = Slice-level multithreading\n"
958 " ...X.. = Codec is experimental\n"
959 " ....B. = Supports draw_horiz_band\n"
960 " .....D = Supports direct rendering method 1\n"
962 encoder ? "Encoders" : "Decoders");
963 while ((desc = avcodec_descriptor_next(desc))) {
964 const AVCodec *codec = NULL;
966 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
967 printf("%c", get_media_type_char(desc->type));
968 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
969 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
970 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
971 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
972 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
974 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
975 if (strcmp(codec->name, desc->name))
976 printf(" (codec %s)", desc->name);
983 int show_decoders(const char *opt, const char *arg)
989 int show_encoders(const char *opt, const char *arg)
995 int show_bsfs(const char *opt, const char *arg)
997 AVBitStreamFilter *bsf = NULL;
999 printf("Bitstream filters:\n");
1000 while ((bsf = av_bitstream_filter_next(bsf)))
1001 printf("%s\n", bsf->name);
1006 int show_protocols(const char *opt, const char *arg)
1008 void *opaque = NULL;
1011 printf("Supported file protocols:\n"
1013 while ((name = avio_enum_protocols(&opaque, 0)))
1014 printf("%s\n", name);
1015 printf("Output:\n");
1016 while ((name = avio_enum_protocols(&opaque, 1)))
1017 printf("%s\n", name);
1021 int show_filters(const char *opt, const char *arg)
1023 AVFilter av_unused(**filter) = NULL;
1024 char descr[64], *descr_cur;
1026 const AVFilterPad *pad;
1028 printf("Filters:\n");
1030 while ((filter = av_filter_next(filter)) && *filter) {
1032 for (i = 0; i < 2; i++) {
1034 *(descr_cur++) = '-';
1035 *(descr_cur++) = '>';
1037 pad = i ? (*filter)->outputs : (*filter)->inputs;
1038 for (j = 0; pad[j].name; j++) {
1039 if (descr_cur >= descr + sizeof(descr) - 4)
1041 *(descr_cur++) = get_media_type_char(pad[j].type);
1044 *(descr_cur++) = '|';
1047 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
1053 int show_pix_fmts(const char *opt, const char *arg)
1055 enum PixelFormat pix_fmt;
1057 printf("Pixel formats:\n"
1058 "I.... = Supported Input format for conversion\n"
1059 ".O... = Supported Output format for conversion\n"
1060 "..H.. = Hardware accelerated format\n"
1061 "...P. = Paletted format\n"
1062 "....B = Bitstream format\n"
1063 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1067 # define sws_isSupportedInput(x) 0
1068 # define sws_isSupportedOutput(x) 0
1071 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
1072 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
1075 printf("%c%c%c%c%c %-16s %d %2d\n",
1076 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1077 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1078 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
1079 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
1080 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
1082 pix_desc->nb_components,
1083 av_get_bits_per_pixel(pix_desc));
1088 int show_sample_fmts(const char *opt, const char *arg)
1092 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1093 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1097 static void show_help_codec(const char *name, int encoder)
1099 const AVCodecDescriptor *desc;
1100 const AVCodec *codec;
1103 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1107 codec = encoder ? avcodec_find_encoder_by_name(name) :
1108 avcodec_find_decoder_by_name(name);
1112 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1115 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1121 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1122 "but no %s for it are available. FFmpeg might need to be "
1123 "recompiled with additional external libraries.\n",
1124 name, encoder ? "encoders" : "decoders");
1127 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1132 static void show_help_demuxer(const char *name)
1134 const AVInputFormat *fmt = av_find_input_format(name);
1137 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1141 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1143 if (fmt->extensions)
1144 printf(" Common extensions: %s.\n", fmt->extensions);
1146 if (fmt->priv_class)
1147 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1150 static void show_help_muxer(const char *name)
1152 const AVCodecDescriptor *desc;
1153 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1156 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1160 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1162 if (fmt->extensions)
1163 printf(" Common extensions: %s.\n", fmt->extensions);
1165 printf(" Mime type: %s.\n", fmt->mime_type);
1166 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1167 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1168 printf(" Default video codec: %s.\n", desc->name);
1170 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1171 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1172 printf(" Default audio codec: %s.\n", desc->name);
1174 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1175 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1176 printf(" Default subtitle codec: %s.\n", desc->name);
1179 if (fmt->priv_class)
1180 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1183 int show_help(const char *opt, const char *arg)
1186 av_log_set_callback(log_callback_help);
1188 topic = av_strdup(arg ? arg : "");
1189 par = strchr(topic, '=');
1194 show_help_default(topic, par);
1195 } else if (!strcmp(topic, "decoder")) {
1196 show_help_codec(par, 0);
1197 } else if (!strcmp(topic, "encoder")) {
1198 show_help_codec(par, 1);
1199 } else if (!strcmp(topic, "demuxer")) {
1200 show_help_demuxer(par);
1201 } else if (!strcmp(topic, "muxer")) {
1202 show_help_muxer(par);
1204 show_help_default(topic, par);
1211 int read_yesno(void)
1214 int yesno = (toupper(c) == 'Y');
1216 while (c != '\n' && c != EOF)
1222 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1225 FILE *f = fopen(filename, "rb");
1228 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1230 return AVERROR(errno);
1232 fseek(f, 0, SEEK_END);
1234 fseek(f, 0, SEEK_SET);
1235 *bufptr = av_malloc(*size + 1);
1237 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1239 return AVERROR(ENOMEM);
1241 ret = fread(*bufptr, 1, *size, f);
1245 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1246 filename, strerror(errno));
1247 ret = AVERROR(errno);
1252 (*bufptr)[*size++] = '\0';
1259 FILE *get_preset_file(char *filename, size_t filename_size,
1260 const char *preset_name, int is_path,
1261 const char *codec_name)
1265 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1270 av_strlcpy(filename, preset_name, filename_size);
1271 f = fopen(filename, "r");
1274 char datadir[MAX_PATH], *ls;
1277 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1279 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1280 if (*ls == '\\') *ls = '/';
1282 if (ls = strrchr(datadir, '/'))
1285 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1290 for (i = 0; i < 3 && !f; i++) {
1293 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1294 i != 1 ? "" : "/.ffmpeg", preset_name);
1295 f = fopen(filename, "r");
1296 if (!f && codec_name) {
1297 snprintf(filename, filename_size,
1298 "%s%s/%s-%s.ffpreset",
1299 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1301 f = fopen(filename, "r");
1309 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1311 int ret = avformat_match_stream_specifier(s, st, spec);
1313 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1317 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1318 AVFormatContext *s, AVStream *st, AVCodec *codec)
1320 AVDictionary *ret = NULL;
1321 AVDictionaryEntry *t = NULL;
1322 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1323 : AV_OPT_FLAG_DECODING_PARAM;
1325 const AVClass *cc = avcodec_get_class();
1328 codec = s->oformat ? avcodec_find_encoder(codec_id)
1329 : avcodec_find_decoder(codec_id);
1333 switch (codec->type) {
1334 case AVMEDIA_TYPE_VIDEO:
1336 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1338 case AVMEDIA_TYPE_AUDIO:
1340 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1342 case AVMEDIA_TYPE_SUBTITLE:
1344 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1348 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1349 char *p = strchr(t->key, ':');
1351 /* check stream specification in opt name */
1353 switch (check_stream_specifier(s, st, p + 1)) {
1354 case 1: *p = 0; break;
1356 default: return NULL;
1359 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1360 (codec && codec->priv_class &&
1361 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1362 AV_OPT_SEARCH_FAKE_OBJ)))
1363 av_dict_set(&ret, t->key, t->value, 0);
1364 else if (t->key[0] == prefix &&
1365 av_opt_find(&cc, t->key + 1, NULL, flags,
1366 AV_OPT_SEARCH_FAKE_OBJ))
1367 av_dict_set(&ret, t->key + 1, t->value, 0);
1375 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1376 AVDictionary *codec_opts)
1379 AVDictionary **opts;
1383 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1385 av_log(NULL, AV_LOG_ERROR,
1386 "Could not alloc memory for stream options.\n");
1389 for (i = 0; i < s->nb_streams; i++)
1390 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1391 s, s->streams[i], NULL);
1395 void *grow_array(void *array, int elem_size, int *size, int new_size)
1397 if (new_size >= INT_MAX / elem_size) {
1398 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1401 if (*size < new_size) {
1402 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1404 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1407 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1414 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1416 FrameBuffer *buf = av_mallocz(sizeof(*buf));
1418 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
1419 int h_chroma_shift, v_chroma_shift;
1420 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1421 int w = s->width, h = s->height;
1424 return AVERROR(ENOMEM);
1426 avcodec_align_dimensions(s, &w, &h);
1428 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1433 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1434 s->pix_fmt, 32)) < 0) {
1436 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
1439 /* XXX this shouldn't be needed, but some tests break without this line
1440 * those decoders are buggy and need to be fixed.
1441 * the following tests fail:
1442 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1444 memset(buf->base[0], 128, ret);
1446 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1447 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1448 const int h_shift = i==0 ? 0 : h_chroma_shift;
1449 const int v_shift = i==0 ? 0 : v_chroma_shift;
1450 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
1451 buf->data[i] = buf->base[i];
1453 buf->data[i] = buf->base[i] +
1454 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1455 (pixel_size*edge >> h_shift), 32);
1459 buf->pix_fmt = s->pix_fmt;
1466 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1468 FrameBuffer **pool = s->opaque;
1472 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
1473 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
1477 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1483 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1484 av_freep(&buf->base[0]);
1486 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1489 av_assert0(!buf->refcount);
1492 frame->opaque = buf;
1493 frame->type = FF_BUFFER_TYPE_USER;
1494 frame->extended_data = frame->data;
1495 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1496 frame->width = buf->w;
1497 frame->height = buf->h;
1498 frame->format = buf->pix_fmt;
1499 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1501 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1502 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1503 frame->data[i] = buf->data[i];
1504 frame->linesize[i] = buf->linesize[i];
1510 static void unref_buffer(FrameBuffer *buf)
1512 FrameBuffer **pool = buf->pool;
1514 av_assert0(buf->refcount > 0);
1516 if (!buf->refcount) {
1518 for(tmp= *pool; tmp; tmp= tmp->next)
1519 av_assert1(tmp != buf);
1526 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1528 FrameBuffer *buf = frame->opaque;
1531 if(frame->type!=FF_BUFFER_TYPE_USER) {
1532 avcodec_default_release_buffer(s, frame);
1536 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1537 frame->data[i] = NULL;
1542 void filter_release_buffer(AVFilterBuffer *fb)
1544 FrameBuffer *buf = fb->priv;
1549 void free_buffer_pool(FrameBuffer **pool)
1551 FrameBuffer *buf = *pool;
1554 av_freep(&buf->base[0]);