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 mask,
151 for (po = options; po->name != NULL; po++) {
153 if ((po->flags & mask) == value) {
158 av_strlcpy(buf, po->name, sizeof(buf));
159 if (po->flags & HAS_ARG) {
160 av_strlcat(buf, " ", sizeof(buf));
161 av_strlcat(buf, po->argname, sizeof(buf));
163 printf("-%-17s %s\n", buf, po->help);
168 void show_help_children(const AVClass *class, int flags)
170 const AVClass *child = NULL;
171 av_opt_show2(&class, NULL, flags, 0);
174 while (child = av_opt_child_class_next(class, child))
175 show_help_children(child, flags);
178 static const OptionDef *find_option(const OptionDef *po, const char *name)
180 const char *p = strchr(name, ':');
181 int len = p ? p - name : strlen(name);
183 while (po->name != NULL) {
184 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
191 #if defined(_WIN32) && !defined(__MINGW32CE__)
193 /* Will be leaked on exit */
194 static char** win32_argv_utf8 = NULL;
195 static int win32_argc = 0;
198 * Prepare command line arguments for executable.
199 * For Windows - perform wide-char to UTF-8 conversion.
200 * Input arguments should be main() function arguments.
201 * @param argc_ptr Arguments number (including executable)
202 * @param argv_ptr Arguments list.
204 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
208 int i, buffsize = 0, offset = 0;
210 if (win32_argv_utf8) {
211 *argc_ptr = win32_argc;
212 *argv_ptr = win32_argv_utf8;
217 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
218 if (win32_argc <= 0 || !argv_w)
221 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
222 for (i = 0; i < win32_argc; i++)
223 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
224 NULL, 0, NULL, NULL);
226 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
227 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
228 if (win32_argv_utf8 == NULL) {
233 for (i = 0; i < win32_argc; i++) {
234 win32_argv_utf8[i] = &argstr_flat[offset];
235 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
236 &argstr_flat[offset],
237 buffsize - offset, NULL, NULL);
239 win32_argv_utf8[i] = NULL;
242 *argc_ptr = win32_argc;
243 *argv_ptr = win32_argv_utf8;
246 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
250 #endif /* WIN32 && !__MINGW32CE__ */
252 int parse_option(void *optctx, const char *opt, const char *arg,
253 const OptionDef *options)
260 po = find_option(options, opt);
261 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
262 /* handle 'no' bool option */
263 po = find_option(options, opt + 2);
264 if ((po->name && (po->flags & OPT_BOOL)))
268 po = find_option(options, "default");
270 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
271 return AVERROR(EINVAL);
273 if (po->flags & HAS_ARG && !arg) {
274 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
275 return AVERROR(EINVAL);
278 /* new-style options contain an offset into optctx, old-style address of
280 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
283 if (po->flags & OPT_SPEC) {
284 SpecifierOpt **so = dst;
285 char *p = strchr(opt, ':');
287 dstcount = (int *)(so + 1);
288 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
289 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
290 dst = &(*so)[*dstcount - 1].u;
293 if (po->flags & OPT_STRING) {
295 str = av_strdup(arg);
297 } else if (po->flags & OPT_BOOL) {
298 *(int *)dst = bool_val;
299 } else if (po->flags & OPT_INT) {
300 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
301 } else if (po->flags & OPT_INT64) {
302 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
303 } else if (po->flags & OPT_TIME) {
304 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
305 } else if (po->flags & OPT_FLOAT) {
306 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
307 } else if (po->flags & OPT_DOUBLE) {
308 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
309 } else if (po->u.func_arg) {
310 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
311 : po->u.func_arg(opt, arg);
313 av_log(NULL, AV_LOG_ERROR,
314 "Failed to set value '%s' for option '%s'\n", arg, opt);
318 if (po->flags & OPT_EXIT)
320 return !!(po->flags & HAS_ARG);
323 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
324 void (*parse_arg_function)(void *, const char*))
327 int optindex, handleoptions = 1, ret;
329 /* perform system-dependent conversions for arguments list */
330 prepare_app_arguments(&argc, &argv);
334 while (optindex < argc) {
335 opt = argv[optindex++];
337 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
338 if (opt[1] == '-' && opt[2] == '\0') {
344 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
348 if (parse_arg_function)
349 parse_arg_function(optctx, opt);
354 int locate_option(int argc, char **argv, const OptionDef *options,
360 for (i = 1; i < argc; i++) {
361 const char *cur_opt = argv[i];
363 if (*cur_opt++ != '-')
366 po = find_option(options, cur_opt);
367 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
368 po = find_option(options, cur_opt + 2);
370 if ((!po->name && !strcmp(cur_opt, optname)) ||
371 (po->name && !strcmp(optname, po->name)))
374 if (!po || po->flags & HAS_ARG)
380 static void dump_argument(const char *a)
382 const unsigned char *p;
385 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
386 *p == '_' || (*p >= 'a' && *p <= 'z')))
389 fputs(a, report_file);
392 fputc('"', report_file);
393 for (p = a; *p; p++) {
394 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
395 fprintf(report_file, "\\%c", *p);
396 else if (*p < ' ' || *p > '~')
397 fprintf(report_file, "\\x%02x", *p);
399 fputc(*p, report_file);
401 fputc('"', report_file);
404 void parse_loglevel(int argc, char **argv, const OptionDef *options)
406 int idx = locate_option(argc, argv, options, "loglevel");
408 idx = locate_option(argc, argv, options, "v");
409 if (idx && argv[idx + 1])
410 opt_loglevel("loglevel", argv[idx + 1]);
411 idx = locate_option(argc, argv, options, "report");
412 if (idx || getenv("FFREPORT")) {
413 opt_report("report");
416 fprintf(report_file, "Command line:\n");
417 for (i = 0; i < argc; i++) {
418 dump_argument(argv[i]);
419 fputc(i < argc - 1 ? ' ' : '\n', report_file);
426 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
427 int opt_default(const char *opt, const char *arg)
430 char opt_stripped[128];
432 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
434 if (!(p = strchr(opt, ':')))
435 p = opt + strlen(opt);
436 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
438 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
439 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
440 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
441 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
442 av_dict_set(&codec_opts, opt, arg, FLAGS);
443 else if ((o = av_opt_find(&fc, opt, NULL, 0,
444 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
445 av_dict_set(&format_opts, opt, arg, FLAGS);
447 sc = sws_get_class();
448 if (!o && (o = av_opt_find(&sc, opt, NULL, 0,
449 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
450 // XXX we only support sws_flags, not arbitrary sws options
451 int ret = av_opt_set(sws_opts, opt, arg, 0);
453 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
458 #if CONFIG_SWRESAMPLE
459 swr_class = swr_get_class();
460 if (!o && (o = av_opt_find(&swr_class, opt, NULL, 0,
461 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
462 int ret = av_opt_set(swr_opts, opt, arg, 0);
464 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
472 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
473 return AVERROR_OPTION_NOT_FOUND;
476 int opt_loglevel(const char *opt, const char *arg)
478 const struct { const char *name; int level; } log_levels[] = {
479 { "quiet" , AV_LOG_QUIET },
480 { "panic" , AV_LOG_PANIC },
481 { "fatal" , AV_LOG_FATAL },
482 { "error" , AV_LOG_ERROR },
483 { "warning", AV_LOG_WARNING },
484 { "info" , AV_LOG_INFO },
485 { "verbose", AV_LOG_VERBOSE },
486 { "debug" , AV_LOG_DEBUG },
492 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
493 if (!strcmp(log_levels[i].name, arg)) {
494 av_log_set_level(log_levels[i].level);
499 level = strtol(arg, &tail, 10);
501 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
502 "Possible levels are numbers or:\n", arg);
503 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
504 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
507 av_log_set_level(level);
511 int opt_report(const char *opt)
517 if (report_file) /* already opened */
520 tm = localtime(&now);
521 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
523 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
524 tm->tm_hour, tm->tm_min, tm->tm_sec);
525 report_file = fopen(filename, "w");
527 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
528 filename, strerror(errno));
529 return AVERROR(errno);
531 av_log_set_callback(log_callback_report);
532 av_log(NULL, AV_LOG_INFO,
533 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
534 "Report written to \"%s\"\n",
536 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
537 tm->tm_hour, tm->tm_min, tm->tm_sec,
539 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
543 int opt_max_alloc(const char *opt, const char *arg)
548 max = strtol(arg, &tail, 10);
550 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
557 int opt_cpuflags(const char *opt, const char *arg)
560 unsigned flags = av_get_cpu_flags();
562 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
565 av_force_cpu_flags(flags);
569 int opt_codec_debug(const char *opt, const char *arg)
571 av_log_set_level(AV_LOG_DEBUG);
572 return opt_default(opt, arg);
575 int opt_timelimit(const char *opt, const char *arg)
578 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
579 struct rlimit rl = { lim, lim + 1 };
580 if (setrlimit(RLIMIT_CPU, &rl))
583 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
588 void print_error(const char *filename, int err)
591 const char *errbuf_ptr = errbuf;
593 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
594 errbuf_ptr = strerror(AVUNERROR(err));
595 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
598 static int warned_cfg = 0;
601 #define SHOW_VERSION 2
602 #define SHOW_CONFIG 4
603 #define SHOW_COPYRIGHT 8
605 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
606 if (CONFIG_##LIBNAME) { \
607 const char *indent = flags & INDENT? " " : ""; \
608 if (flags & SHOW_VERSION) { \
609 unsigned int version = libname##_version(); \
610 av_log(NULL, level, \
611 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
613 LIB##LIBNAME##_VERSION_MAJOR, \
614 LIB##LIBNAME##_VERSION_MINOR, \
615 LIB##LIBNAME##_VERSION_MICRO, \
616 version >> 16, version >> 8 & 0xff, version & 0xff); \
618 if (flags & SHOW_CONFIG) { \
619 const char *cfg = libname##_configuration(); \
620 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
622 av_log(NULL, level, \
623 "%sWARNING: library configuration mismatch\n", \
627 av_log(NULL, level, "%s%-11s configuration: %s\n", \
628 indent, #libname, cfg); \
633 static void print_all_libs_info(int flags, int level)
635 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
636 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
637 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
638 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
639 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
640 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
641 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
642 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
644 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
648 static void print_program_info(int flags, int level)
650 const char *indent = flags & INDENT? " " : "";
652 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
653 if (flags & SHOW_COPYRIGHT)
654 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
655 program_birth_year, this_year);
656 av_log(NULL, level, "\n");
657 av_log(NULL, level, "%sbuilt on %s %s with %s %s\n",
658 indent, __DATE__, __TIME__, CC_TYPE, CC_VERSION);
659 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
662 void show_banner(int argc, char **argv, const OptionDef *options)
664 int idx = locate_option(argc, argv, options, "version");
668 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
669 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
670 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
673 int opt_version(const char *opt, const char *arg) {
674 av_log_set_callback(log_callback_help);
675 print_program_info (0 , AV_LOG_INFO);
676 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
680 int opt_license(const char *opt, const char *arg)
684 "This version of %s has nonfree parts compiled in.\n"
685 "Therefore it is not legally redistributable.\n",
688 "%s is free software; you can redistribute it and/or modify\n"
689 "it under the terms of the GNU General Public License as published by\n"
690 "the Free Software Foundation; either version 3 of the License, or\n"
691 "(at your option) any later version.\n"
693 "%s is distributed in the hope that it will be useful,\n"
694 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
695 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
696 "GNU General Public License for more details.\n"
698 "You should have received a copy of the GNU General Public License\n"
699 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
700 program_name, program_name, program_name
702 "%s is free software; you can redistribute it and/or modify\n"
703 "it under the terms of the GNU General Public License as published by\n"
704 "the Free Software Foundation; either version 2 of the License, or\n"
705 "(at your option) any later version.\n"
707 "%s is distributed in the hope that it will be useful,\n"
708 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
709 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
710 "GNU General Public License for more details.\n"
712 "You should have received a copy of the GNU General Public License\n"
713 "along with %s; if not, write to the Free Software\n"
714 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
715 program_name, program_name, program_name
717 "%s is free software; you can redistribute it and/or modify\n"
718 "it under the terms of the GNU Lesser General Public License as published by\n"
719 "the Free Software Foundation; either version 3 of the License, or\n"
720 "(at your option) any later version.\n"
722 "%s is distributed in the hope that it will be useful,\n"
723 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
724 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
725 "GNU Lesser General Public License for more details.\n"
727 "You should have received a copy of the GNU Lesser General Public License\n"
728 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
729 program_name, program_name, program_name
731 "%s is free software; you can redistribute it and/or\n"
732 "modify it under the terms of the GNU Lesser General Public\n"
733 "License as published by the Free Software Foundation; either\n"
734 "version 2.1 of the License, or (at your option) any later version.\n"
736 "%s is distributed in the hope that it will be useful,\n"
737 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
738 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
739 "Lesser General Public License for more details.\n"
741 "You should have received a copy of the GNU Lesser General Public\n"
742 "License along with %s; if not, write to the Free Software\n"
743 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
744 program_name, program_name, program_name
750 int opt_formats(const char *opt, const char *arg)
752 AVInputFormat *ifmt = NULL;
753 AVOutputFormat *ofmt = NULL;
754 const char *last_name;
756 printf("File formats:\n"
757 " D. = Demuxing supported\n"
758 " .E = Muxing supported\n"
764 const char *name = NULL;
765 const char *long_name = NULL;
767 while ((ofmt = av_oformat_next(ofmt))) {
768 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
769 strcmp(ofmt->name, last_name) > 0) {
771 long_name = ofmt->long_name;
775 while ((ifmt = av_iformat_next(ifmt))) {
776 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
777 strcmp(ifmt->name, last_name) > 0) {
779 long_name = ifmt->long_name;
782 if (name && strcmp(ifmt->name, name) == 0)
789 printf(" %s%s %-15s %s\n",
793 long_name ? long_name:" ");
798 static char get_media_type_char(enum AVMediaType type)
800 static const char map[AVMEDIA_TYPE_NB] = {
801 [AVMEDIA_TYPE_VIDEO] = 'V',
802 [AVMEDIA_TYPE_AUDIO] = 'A',
803 [AVMEDIA_TYPE_DATA] = 'D',
804 [AVMEDIA_TYPE_SUBTITLE] = 'S',
805 [AVMEDIA_TYPE_ATTACHMENT] = 'T',
807 return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
810 int opt_codecs(const char *opt, const char *arg)
812 AVCodec *p = NULL, *p2;
813 const char *last_name;
815 " D...... = Decoding supported\n"
816 " .E..... = Encoding supported\n"
817 " ..V.... = Video codec\n"
818 " ..A.... = Audio codec\n"
819 " ..S.... = Subtitle codec\n"
820 " ...S... = Supports draw_horiz_band\n"
821 " ....D.. = Supports direct rendering method 1\n"
822 " .....T. = Supports weird frame truncation\n"
823 " ......F = Supports frame-based multi-threading\n"
824 " ......S = Supports slice-based multi-threading\n"
825 " ......B = Supports both frame-based and slice-based multi-threading\n"
834 while ((p = av_codec_next(p))) {
835 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
836 strcmp(p->name, last_name) > 0) {
838 decode = encode = cap = 0;
840 if (p2 && strcmp(p->name, p2->name) == 0) {
841 if (av_codec_is_decoder(p))
843 if (av_codec_is_encoder(p))
845 cap |= p->capabilities;
850 last_name = p2->name;
852 printf(" %s%s%c%s%s%s%s %-15s %s",
853 decode ? "D" : (/* p2->decoder ? "d" : */ " "),
855 get_media_type_char(p2->type),
856 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
857 cap & CODEC_CAP_DR1 ? "D" : " ",
858 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
859 cap & CODEC_CAP_FRAME_THREADS ? cap & CODEC_CAP_SLICE_THREADS ? "B" : "F" :
860 cap & CODEC_CAP_SLICE_THREADS ? "S" : " ",
862 p2->long_name ? p2->long_name : "");
864 if (p2->decoder && decode == 0)
865 printf(" use %s for decoding", p2->decoder->name);
870 printf("Note, the names of encoders and decoders do not always match, so there are\n"
871 "several cases where the above table shows encoder only or decoder only entries\n"
872 "even though both encoding and decoding are supported. For example, the h263\n"
873 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
878 int opt_bsfs(const char *opt, const char *arg)
880 AVBitStreamFilter *bsf = NULL;
882 printf("Bitstream filters:\n");
883 while ((bsf = av_bitstream_filter_next(bsf)))
884 printf("%s\n", bsf->name);
889 int opt_protocols(const char *opt, const char *arg)
894 printf("Supported file protocols:\n"
896 while ((name = avio_enum_protocols(&opaque, 0)))
897 printf("%s\n", name);
899 while ((name = avio_enum_protocols(&opaque, 1)))
900 printf("%s\n", name);
904 int opt_filters(const char *opt, const char *arg)
906 AVFilter av_unused(**filter) = NULL;
907 char descr[64], *descr_cur;
909 const AVFilterPad *pad;
911 printf("Filters:\n");
913 while ((filter = av_filter_next(filter)) && *filter) {
915 for (i = 0; i < 2; i++) {
917 *(descr_cur++) = '-';
918 *(descr_cur++) = '>';
920 pad = i ? (*filter)->outputs : (*filter)->inputs;
921 for (j = 0; pad[j].name; j++) {
922 if (descr_cur >= descr + sizeof(descr) - 4)
924 *(descr_cur++) = get_media_type_char(pad[j].type);
927 *(descr_cur++) = '|';
930 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
936 int opt_pix_fmts(const char *opt, const char *arg)
938 enum PixelFormat pix_fmt;
940 printf("Pixel formats:\n"
941 "I.... = Supported Input format for conversion\n"
942 ".O... = Supported Output format for conversion\n"
943 "..H.. = Hardware accelerated format\n"
944 "...P. = Paletted format\n"
945 "....B = Bitstream format\n"
946 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
950 # define sws_isSupportedInput(x) 0
951 # define sws_isSupportedOutput(x) 0
954 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
955 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
958 printf("%c%c%c%c%c %-16s %d %2d\n",
959 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
960 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
961 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
962 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
963 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
965 pix_desc->nb_components,
966 av_get_bits_per_pixel(pix_desc));
971 int show_sample_fmts(const char *opt, const char *arg)
975 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
976 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
983 int yesno = (toupper(c) == 'Y');
985 while (c != '\n' && c != EOF)
991 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
994 FILE *f = fopen(filename, "rb");
997 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
999 return AVERROR(errno);
1001 fseek(f, 0, SEEK_END);
1003 fseek(f, 0, SEEK_SET);
1004 *bufptr = av_malloc(*size + 1);
1006 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1008 return AVERROR(ENOMEM);
1010 ret = fread(*bufptr, 1, *size, f);
1014 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1015 filename, strerror(errno));
1016 ret = AVERROR(errno);
1021 (*bufptr)[*size++] = '\0';
1028 FILE *get_preset_file(char *filename, size_t filename_size,
1029 const char *preset_name, int is_path,
1030 const char *codec_name)
1034 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1039 av_strlcpy(filename, preset_name, filename_size);
1040 f = fopen(filename, "r");
1043 char datadir[MAX_PATH], *ls;
1046 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1048 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1049 if (*ls == '\\') *ls = '/';
1051 if (ls = strrchr(datadir, '/'))
1054 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1059 for (i = 0; i < 3 && !f; i++) {
1062 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1063 i != 1 ? "" : "/.ffmpeg", preset_name);
1064 f = fopen(filename, "r");
1065 if (!f && codec_name) {
1066 snprintf(filename, filename_size,
1067 "%s%s/%s-%s.ffpreset",
1068 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1070 f = fopen(filename, "r");
1078 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1080 if (*spec <= '9' && *spec >= '0') /* opt:index */
1081 return strtol(spec, NULL, 0) == st->index;
1082 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1083 *spec == 't') { /* opt:[vasdt] */
1084 enum AVMediaType type;
1087 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1088 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1089 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1090 case 'd': type = AVMEDIA_TYPE_DATA; break;
1091 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1092 default: av_assert0(0);
1094 if (type != st->codec->codec_type)
1096 if (*spec++ == ':') { /* possibly followed by :index */
1097 int i, index = strtol(spec, NULL, 0);
1098 for (i = 0; i < s->nb_streams; i++)
1099 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1100 return i == st->index;
1104 } else if (*spec == 'p' && *(spec + 1) == ':') {
1108 prog_id = strtol(spec, &endptr, 0);
1109 for (i = 0; i < s->nb_programs; i++) {
1110 if (s->programs[i]->id != prog_id)
1113 if (*endptr++ == ':') {
1114 int stream_idx = strtol(endptr, NULL, 0);
1115 return stream_idx >= 0 &&
1116 stream_idx < s->programs[i]->nb_stream_indexes &&
1117 st->index == s->programs[i]->stream_index[stream_idx];
1120 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1121 if (st->index == s->programs[i]->stream_index[j])
1125 } else if (*spec == '#') {
1128 sid = strtol(spec + 1, &endptr, 0);
1130 return st->id == sid;
1131 } else if (!*spec) /* empty specifier, matches everything */
1134 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1135 return AVERROR(EINVAL);
1138 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
1139 AVFormatContext *s, AVStream *st)
1141 AVDictionary *ret = NULL;
1142 AVDictionaryEntry *t = NULL;
1143 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1144 : AV_OPT_FLAG_DECODING_PARAM;
1146 const AVClass *cc = avcodec_get_class();
1151 switch (codec->type) {
1152 case AVMEDIA_TYPE_VIDEO:
1154 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1156 case AVMEDIA_TYPE_AUDIO:
1158 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1160 case AVMEDIA_TYPE_SUBTITLE:
1162 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1166 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1167 char *p = strchr(t->key, ':');
1169 /* check stream specification in opt name */
1171 switch (check_stream_specifier(s, st, p + 1)) {
1172 case 1: *p = 0; break;
1174 default: return NULL;
1177 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1178 (codec && codec->priv_class &&
1179 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1180 AV_OPT_SEARCH_FAKE_OBJ)))
1181 av_dict_set(&ret, t->key, t->value, 0);
1182 else if (t->key[0] == prefix &&
1183 av_opt_find(&cc, t->key + 1, NULL, flags,
1184 AV_OPT_SEARCH_FAKE_OBJ))
1185 av_dict_set(&ret, t->key + 1, t->value, 0);
1193 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1194 AVDictionary *codec_opts)
1197 AVDictionary **opts;
1201 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1203 av_log(NULL, AV_LOG_ERROR,
1204 "Could not alloc memory for stream options.\n");
1207 for (i = 0; i < s->nb_streams; i++)
1208 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
1213 void *grow_array(void *array, int elem_size, int *size, int new_size)
1215 if (new_size >= INT_MAX / elem_size) {
1216 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1219 if (*size < new_size) {
1220 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1222 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1225 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1232 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1234 FrameBuffer *buf = av_mallocz(sizeof(*buf));
1236 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
1237 int h_chroma_shift, v_chroma_shift;
1238 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1239 int w = s->width, h = s->height;
1242 return AVERROR(ENOMEM);
1244 avcodec_align_dimensions(s, &w, &h);
1246 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1251 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1252 s->pix_fmt, 32)) < 0) {
1256 /* XXX this shouldn't be needed, but some tests break without this line
1257 * those decoders are buggy and need to be fixed.
1258 * the following tests fail:
1259 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1261 memset(buf->base[0], 128, ret);
1263 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1264 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1265 const int h_shift = i==0 ? 0 : h_chroma_shift;
1266 const int v_shift = i==0 ? 0 : v_chroma_shift;
1267 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[1] || !buf->base[i])
1268 buf->data[i] = buf->base[i];
1270 buf->data[i] = buf->base[i] +
1271 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1272 (pixel_size*edge >> h_shift), 32);
1276 buf->pix_fmt = s->pix_fmt;
1283 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1285 FrameBuffer **pool = s->opaque;
1289 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0)
1292 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1298 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1299 av_freep(&buf->base[0]);
1301 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1304 av_assert0(!buf->refcount);
1307 frame->opaque = buf;
1308 frame->type = FF_BUFFER_TYPE_USER;
1309 frame->extended_data = frame->data;
1310 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1311 frame->width = buf->w;
1312 frame->height = buf->h;
1313 frame->format = buf->pix_fmt;
1314 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1316 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1317 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1318 frame->data[i] = buf->data[i];
1319 frame->linesize[i] = buf->linesize[i];
1325 static void unref_buffer(FrameBuffer *buf)
1327 FrameBuffer **pool = buf->pool;
1329 av_assert0(buf->refcount > 0);
1331 if (!buf->refcount) {
1333 for(tmp= *pool; tmp; tmp= tmp->next)
1334 av_assert1(tmp != buf);
1341 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1343 FrameBuffer *buf = frame->opaque;
1346 if(frame->type!=FF_BUFFER_TYPE_USER)
1347 return avcodec_default_release_buffer(s, frame);
1349 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1350 frame->data[i] = NULL;
1355 void filter_release_buffer(AVFilterBuffer *fb)
1357 FrameBuffer *buf = fb->priv;
1362 void free_buffer_pool(FrameBuffer **pool)
1364 FrameBuffer *buf = *pool;
1367 av_freep(&buf->base[0]);