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);
306 } else if (po->flags & OPT_BOOL) {
307 *(int *)dst = bool_val;
308 } else if (po->flags & OPT_INT) {
309 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
310 } else if (po->flags & OPT_INT64) {
311 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
312 } else if (po->flags & OPT_TIME) {
313 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
314 } else if (po->flags & OPT_FLOAT) {
315 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
316 } else if (po->flags & OPT_DOUBLE) {
317 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
318 } else if (po->u.func_arg) {
319 int ret = po->u.func_arg(optctx, 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(NULL, "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(void *optctx, 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(void *optctx, 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(void *optctx, 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(void *optctx, 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(void *optctx, const char *opt, const char *arg)
579 av_log_set_level(AV_LOG_DEBUG);
580 return opt_default(NULL, opt, arg);
583 int opt_timelimit(void *optctx, 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(void *optctx, 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(void *optctx, 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(void *optctx, 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(void *optctx, 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"
915 " ....L. = Lossy compression\n"
916 " .....S = Lossless compression\n"
918 while ((desc = avcodec_descriptor_next(desc))) {
919 const AVCodec *codec = NULL;
922 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
923 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
925 printf("%c", get_media_type_char(desc->type));
926 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
927 printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
928 printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
930 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
932 /* print decoders/encoders when there's more than one or their
933 * names are different from codec name */
934 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
935 if (strcmp(codec->name, desc->name)) {
936 print_codecs_for_id(desc->id, 0);
941 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
942 if (strcmp(codec->name, desc->name)) {
943 print_codecs_for_id(desc->id, 1);
953 static void print_codecs(int encoder)
955 const AVCodecDescriptor *desc = NULL;
960 " S..... = Subtitle\n"
961 " .F.... = Frame-level multithreading\n"
962 " ..S... = Slice-level multithreading\n"
963 " ...X.. = Codec is experimental\n"
964 " ....B. = Supports draw_horiz_band\n"
965 " .....D = Supports direct rendering method 1\n"
967 encoder ? "Encoders" : "Decoders");
968 while ((desc = avcodec_descriptor_next(desc))) {
969 const AVCodec *codec = NULL;
971 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
972 printf(" %c", get_media_type_char(desc->type));
973 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
974 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
975 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
976 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
977 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
979 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
980 if (strcmp(codec->name, desc->name))
981 printf(" (codec %s)", desc->name);
988 int show_decoders(void *optctx, const char *opt, const char *arg)
994 int show_encoders(void *optctx, const char *opt, const char *arg)
1000 int show_bsfs(void *optctx, const char *opt, const char *arg)
1002 AVBitStreamFilter *bsf = NULL;
1004 printf("Bitstream filters:\n");
1005 while ((bsf = av_bitstream_filter_next(bsf)))
1006 printf("%s\n", bsf->name);
1011 int show_protocols(void *optctx, const char *opt, const char *arg)
1013 void *opaque = NULL;
1016 printf("Supported file protocols:\n"
1018 while ((name = avio_enum_protocols(&opaque, 0)))
1019 printf("%s\n", name);
1020 printf("Output:\n");
1021 while ((name = avio_enum_protocols(&opaque, 1)))
1022 printf("%s\n", name);
1026 int show_filters(void *optctx, const char *opt, const char *arg)
1028 AVFilter av_unused(**filter) = NULL;
1029 char descr[64], *descr_cur;
1031 const AVFilterPad *pad;
1033 printf("Filters:\n");
1035 while ((filter = av_filter_next(filter)) && *filter) {
1037 for (i = 0; i < 2; i++) {
1039 *(descr_cur++) = '-';
1040 *(descr_cur++) = '>';
1042 pad = i ? (*filter)->outputs : (*filter)->inputs;
1043 for (j = 0; pad[j].name; j++) {
1044 if (descr_cur >= descr + sizeof(descr) - 4)
1046 *(descr_cur++) = get_media_type_char(pad[j].type);
1049 *(descr_cur++) = '|';
1052 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
1058 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1060 enum PixelFormat pix_fmt;
1062 printf("Pixel formats:\n"
1063 "I.... = Supported Input format for conversion\n"
1064 ".O... = Supported Output format for conversion\n"
1065 "..H.. = Hardware accelerated format\n"
1066 "...P. = Paletted format\n"
1067 "....B = Bitstream format\n"
1068 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1072 # define sws_isSupportedInput(x) 0
1073 # define sws_isSupportedOutput(x) 0
1076 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
1077 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
1080 printf("%c%c%c%c%c %-16s %d %2d\n",
1081 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1082 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1083 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
1084 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
1085 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
1087 pix_desc->nb_components,
1088 av_get_bits_per_pixel(pix_desc));
1093 int show_layouts(void *optctx, const char *opt, const char *arg)
1097 const char *name, *descr;
1099 printf("Individual channels:\n"
1100 "NAME DESCRIPTION\n");
1101 for (i = 0; i < 63; i++) {
1102 name = av_get_channel_name((uint64_t)1 << i);
1105 descr = av_get_channel_description((uint64_t)1 << i);
1106 printf("%-12s%s\n", name, descr);
1108 printf("\nStandard channel layouts:\n"
1109 "NAME DECOMPOSITION\n");
1110 for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1112 printf("%-12s", name);
1113 for (j = 1; j; j <<= 1)
1115 printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1122 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1126 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1127 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1131 static void show_help_codec(const char *name, int encoder)
1133 const AVCodecDescriptor *desc;
1134 const AVCodec *codec;
1137 av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1141 codec = encoder ? avcodec_find_encoder_by_name(name) :
1142 avcodec_find_decoder_by_name(name);
1146 else if ((desc = avcodec_descriptor_get_by_name(name))) {
1149 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1155 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1156 "but no %s for it are available. FFmpeg might need to be "
1157 "recompiled with additional external libraries.\n",
1158 name, encoder ? "encoders" : "decoders");
1161 av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1166 static void show_help_demuxer(const char *name)
1168 const AVInputFormat *fmt = av_find_input_format(name);
1171 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1175 printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1177 if (fmt->extensions)
1178 printf(" Common extensions: %s.\n", fmt->extensions);
1180 if (fmt->priv_class)
1181 show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1184 static void show_help_muxer(const char *name)
1186 const AVCodecDescriptor *desc;
1187 const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1190 av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1194 printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1196 if (fmt->extensions)
1197 printf(" Common extensions: %s.\n", fmt->extensions);
1199 printf(" Mime type: %s.\n", fmt->mime_type);
1200 if (fmt->video_codec != AV_CODEC_ID_NONE &&
1201 (desc = avcodec_descriptor_get(fmt->video_codec))) {
1202 printf(" Default video codec: %s.\n", desc->name);
1204 if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1205 (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1206 printf(" Default audio codec: %s.\n", desc->name);
1208 if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1209 (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1210 printf(" Default subtitle codec: %s.\n", desc->name);
1213 if (fmt->priv_class)
1214 show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1217 int show_help(void *optctx, const char *opt, const char *arg)
1220 av_log_set_callback(log_callback_help);
1222 topic = av_strdup(arg ? arg : "");
1223 par = strchr(topic, '=');
1228 show_help_default(topic, par);
1229 } else if (!strcmp(topic, "decoder")) {
1230 show_help_codec(par, 0);
1231 } else if (!strcmp(topic, "encoder")) {
1232 show_help_codec(par, 1);
1233 } else if (!strcmp(topic, "demuxer")) {
1234 show_help_demuxer(par);
1235 } else if (!strcmp(topic, "muxer")) {
1236 show_help_muxer(par);
1238 show_help_default(topic, par);
1245 int read_yesno(void)
1248 int yesno = (toupper(c) == 'Y');
1250 while (c != '\n' && c != EOF)
1256 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1259 FILE *f = fopen(filename, "rb");
1262 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1264 return AVERROR(errno);
1266 fseek(f, 0, SEEK_END);
1268 fseek(f, 0, SEEK_SET);
1269 *bufptr = av_malloc(*size + 1);
1271 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1273 return AVERROR(ENOMEM);
1275 ret = fread(*bufptr, 1, *size, f);
1279 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1280 filename, strerror(errno));
1281 ret = AVERROR(errno);
1286 (*bufptr)[*size++] = '\0';
1293 FILE *get_preset_file(char *filename, size_t filename_size,
1294 const char *preset_name, int is_path,
1295 const char *codec_name)
1299 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1304 av_strlcpy(filename, preset_name, filename_size);
1305 f = fopen(filename, "r");
1308 char datadir[MAX_PATH], *ls;
1311 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1313 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1314 if (*ls == '\\') *ls = '/';
1316 if (ls = strrchr(datadir, '/'))
1319 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1324 for (i = 0; i < 3 && !f; i++) {
1327 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1328 i != 1 ? "" : "/.ffmpeg", preset_name);
1329 f = fopen(filename, "r");
1330 if (!f && codec_name) {
1331 snprintf(filename, filename_size,
1332 "%s%s/%s-%s.ffpreset",
1333 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1335 f = fopen(filename, "r");
1343 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1345 int ret = avformat_match_stream_specifier(s, st, spec);
1347 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1351 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1352 AVFormatContext *s, AVStream *st, AVCodec *codec)
1354 AVDictionary *ret = NULL;
1355 AVDictionaryEntry *t = NULL;
1356 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1357 : AV_OPT_FLAG_DECODING_PARAM;
1359 const AVClass *cc = avcodec_get_class();
1362 codec = s->oformat ? avcodec_find_encoder(codec_id)
1363 : avcodec_find_decoder(codec_id);
1367 switch (codec->type) {
1368 case AVMEDIA_TYPE_VIDEO:
1370 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1372 case AVMEDIA_TYPE_AUDIO:
1374 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1376 case AVMEDIA_TYPE_SUBTITLE:
1378 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1382 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1383 char *p = strchr(t->key, ':');
1385 /* check stream specification in opt name */
1387 switch (check_stream_specifier(s, st, p + 1)) {
1388 case 1: *p = 0; break;
1390 default: return NULL;
1393 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1394 (codec && codec->priv_class &&
1395 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1396 AV_OPT_SEARCH_FAKE_OBJ)))
1397 av_dict_set(&ret, t->key, t->value, 0);
1398 else if (t->key[0] == prefix &&
1399 av_opt_find(&cc, t->key + 1, NULL, flags,
1400 AV_OPT_SEARCH_FAKE_OBJ))
1401 av_dict_set(&ret, t->key + 1, t->value, 0);
1409 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1410 AVDictionary *codec_opts)
1413 AVDictionary **opts;
1417 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1419 av_log(NULL, AV_LOG_ERROR,
1420 "Could not alloc memory for stream options.\n");
1423 for (i = 0; i < s->nb_streams; i++)
1424 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1425 s, s->streams[i], NULL);
1429 void *grow_array(void *array, int elem_size, int *size, int new_size)
1431 if (new_size >= INT_MAX / elem_size) {
1432 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1435 if (*size < new_size) {
1436 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1438 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1441 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1448 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1450 FrameBuffer *buf = av_mallocz(sizeof(*buf));
1452 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
1453 int h_chroma_shift, v_chroma_shift;
1454 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1455 int w = s->width, h = s->height;
1458 return AVERROR(ENOMEM);
1460 avcodec_align_dimensions(s, &w, &h);
1462 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1467 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1468 s->pix_fmt, 32)) < 0) {
1470 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
1473 /* XXX this shouldn't be needed, but some tests break without this line
1474 * those decoders are buggy and need to be fixed.
1475 * the following tests fail:
1476 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1478 memset(buf->base[0], 128, ret);
1480 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1481 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1482 const int h_shift = i==0 ? 0 : h_chroma_shift;
1483 const int v_shift = i==0 ? 0 : v_chroma_shift;
1484 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
1485 buf->data[i] = buf->base[i];
1487 buf->data[i] = buf->base[i] +
1488 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1489 (pixel_size*edge >> h_shift), 32);
1493 buf->pix_fmt = s->pix_fmt;
1500 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1502 FrameBuffer **pool = s->opaque;
1506 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
1507 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
1511 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1517 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1518 av_freep(&buf->base[0]);
1520 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1523 av_assert0(!buf->refcount);
1526 frame->opaque = buf;
1527 frame->type = FF_BUFFER_TYPE_USER;
1528 frame->extended_data = frame->data;
1529 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1530 frame->width = buf->w;
1531 frame->height = buf->h;
1532 frame->format = buf->pix_fmt;
1533 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1535 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1536 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1537 frame->data[i] = buf->data[i];
1538 frame->linesize[i] = buf->linesize[i];
1544 static void unref_buffer(FrameBuffer *buf)
1546 FrameBuffer **pool = buf->pool;
1548 av_assert0(buf->refcount > 0);
1550 if (!buf->refcount) {
1552 for(tmp= *pool; tmp; tmp= tmp->next)
1553 av_assert1(tmp != buf);
1560 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1562 FrameBuffer *buf = frame->opaque;
1565 if(frame->type!=FF_BUFFER_TYPE_USER) {
1566 avcodec_default_release_buffer(s, frame);
1570 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1571 frame->data[i] = NULL;
1576 void filter_release_buffer(AVFilterBuffer *fb)
1578 FrameBuffer *buf = fb->priv;
1583 void free_buffer_pool(FrameBuffer **pool)
1585 FrameBuffer *buf = *pool;
1588 av_freep(&buf->base[0]);