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 #include <shellapi.h>
194 /* Will be leaked on exit */
195 static char** win32_argv_utf8 = NULL;
196 static int win32_argc = 0;
199 * Prepare command line arguments for executable.
200 * For Windows - perform wide-char to UTF-8 conversion.
201 * Input arguments should be main() function arguments.
202 * @param argc_ptr Arguments number (including executable)
203 * @param argv_ptr Arguments list.
205 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
209 int i, buffsize = 0, offset = 0;
211 if (win32_argv_utf8) {
212 *argc_ptr = win32_argc;
213 *argv_ptr = win32_argv_utf8;
218 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
219 if (win32_argc <= 0 || !argv_w)
222 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
223 for (i = 0; i < win32_argc; i++)
224 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
225 NULL, 0, NULL, NULL);
227 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
228 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
229 if (win32_argv_utf8 == NULL) {
234 for (i = 0; i < win32_argc; i++) {
235 win32_argv_utf8[i] = &argstr_flat[offset];
236 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
237 &argstr_flat[offset],
238 buffsize - offset, NULL, NULL);
240 win32_argv_utf8[i] = NULL;
243 *argc_ptr = win32_argc;
244 *argv_ptr = win32_argv_utf8;
247 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
251 #endif /* WIN32 && !__MINGW32CE__ */
253 int parse_option(void *optctx, const char *opt, const char *arg,
254 const OptionDef *options)
261 po = find_option(options, opt);
262 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
263 /* handle 'no' bool option */
264 po = find_option(options, opt + 2);
265 if ((po->name && (po->flags & OPT_BOOL)))
269 po = find_option(options, "default");
271 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
272 return AVERROR(EINVAL);
274 if (po->flags & HAS_ARG && !arg) {
275 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
276 return AVERROR(EINVAL);
279 /* new-style options contain an offset into optctx, old-style address of
281 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
284 if (po->flags & OPT_SPEC) {
285 SpecifierOpt **so = dst;
286 char *p = strchr(opt, ':');
288 dstcount = (int *)(so + 1);
289 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
290 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
291 dst = &(*so)[*dstcount - 1].u;
294 if (po->flags & OPT_STRING) {
296 str = av_strdup(arg);
298 } else if (po->flags & OPT_BOOL) {
299 *(int *)dst = bool_val;
300 } else if (po->flags & OPT_INT) {
301 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
302 } else if (po->flags & OPT_INT64) {
303 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
304 } else if (po->flags & OPT_TIME) {
305 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
306 } else if (po->flags & OPT_FLOAT) {
307 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
308 } else if (po->flags & OPT_DOUBLE) {
309 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
310 } else if (po->u.func_arg) {
311 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
312 : po->u.func_arg(opt, arg);
314 av_log(NULL, AV_LOG_ERROR,
315 "Failed to set value '%s' for option '%s'\n", arg, opt);
319 if (po->flags & OPT_EXIT)
321 return !!(po->flags & HAS_ARG);
324 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
325 void (*parse_arg_function)(void *, const char*))
328 int optindex, handleoptions = 1, ret;
330 /* perform system-dependent conversions for arguments list */
331 prepare_app_arguments(&argc, &argv);
335 while (optindex < argc) {
336 opt = argv[optindex++];
338 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
339 if (opt[1] == '-' && opt[2] == '\0') {
345 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
349 if (parse_arg_function)
350 parse_arg_function(optctx, opt);
355 int locate_option(int argc, char **argv, const OptionDef *options,
361 for (i = 1; i < argc; i++) {
362 const char *cur_opt = argv[i];
364 if (*cur_opt++ != '-')
367 po = find_option(options, cur_opt);
368 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
369 po = find_option(options, cur_opt + 2);
371 if ((!po->name && !strcmp(cur_opt, optname)) ||
372 (po->name && !strcmp(optname, po->name)))
375 if (!po || po->flags & HAS_ARG)
381 static void dump_argument(const char *a)
383 const unsigned char *p;
386 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
387 *p == '_' || (*p >= 'a' && *p <= 'z')))
390 fputs(a, report_file);
393 fputc('"', report_file);
394 for (p = a; *p; p++) {
395 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
396 fprintf(report_file, "\\%c", *p);
397 else if (*p < ' ' || *p > '~')
398 fprintf(report_file, "\\x%02x", *p);
400 fputc(*p, report_file);
402 fputc('"', report_file);
405 void parse_loglevel(int argc, char **argv, const OptionDef *options)
407 int idx = locate_option(argc, argv, options, "loglevel");
409 idx = locate_option(argc, argv, options, "v");
410 if (idx && argv[idx + 1])
411 opt_loglevel("loglevel", argv[idx + 1]);
412 idx = locate_option(argc, argv, options, "report");
413 if (idx || getenv("FFREPORT")) {
414 opt_report("report");
417 fprintf(report_file, "Command line:\n");
418 for (i = 0; i < argc; i++) {
419 dump_argument(argv[i]);
420 fputc(i < argc - 1 ? ' ' : '\n', report_file);
427 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
428 int opt_default(const char *opt, const char *arg)
431 char opt_stripped[128];
433 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
435 if (!(p = strchr(opt, ':')))
436 p = opt + strlen(opt);
437 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
439 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
440 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
441 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
442 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
443 av_dict_set(&codec_opts, opt, arg, FLAGS);
444 else if ((o = av_opt_find(&fc, opt, NULL, 0,
445 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
446 av_dict_set(&format_opts, opt, arg, FLAGS);
448 sc = sws_get_class();
449 if (!o && (o = av_opt_find(&sc, opt, NULL, 0,
450 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
451 // XXX we only support sws_flags, not arbitrary sws options
452 int ret = av_opt_set(sws_opts, opt, arg, 0);
454 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
459 #if CONFIG_SWRESAMPLE
460 swr_class = swr_get_class();
461 if (!o && (o = av_opt_find(&swr_class, opt, NULL, 0,
462 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
463 int ret = av_opt_set(swr_opts, opt, arg, 0);
465 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
473 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
474 return AVERROR_OPTION_NOT_FOUND;
477 int opt_loglevel(const char *opt, const char *arg)
479 const struct { const char *name; int level; } log_levels[] = {
480 { "quiet" , AV_LOG_QUIET },
481 { "panic" , AV_LOG_PANIC },
482 { "fatal" , AV_LOG_FATAL },
483 { "error" , AV_LOG_ERROR },
484 { "warning", AV_LOG_WARNING },
485 { "info" , AV_LOG_INFO },
486 { "verbose", AV_LOG_VERBOSE },
487 { "debug" , AV_LOG_DEBUG },
493 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
494 if (!strcmp(log_levels[i].name, arg)) {
495 av_log_set_level(log_levels[i].level);
500 level = strtol(arg, &tail, 10);
502 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
503 "Possible levels are numbers or:\n", arg);
504 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
505 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
508 av_log_set_level(level);
512 int opt_report(const char *opt)
518 if (report_file) /* already opened */
521 tm = localtime(&now);
522 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
524 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
525 tm->tm_hour, tm->tm_min, tm->tm_sec);
526 report_file = fopen(filename, "w");
528 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
529 filename, strerror(errno));
530 return AVERROR(errno);
532 av_log_set_callback(log_callback_report);
533 av_log(NULL, AV_LOG_INFO,
534 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
535 "Report written to \"%s\"\n",
537 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
538 tm->tm_hour, tm->tm_min, tm->tm_sec,
540 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
544 int opt_max_alloc(const char *opt, const char *arg)
549 max = strtol(arg, &tail, 10);
551 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
558 int opt_cpuflags(const char *opt, const char *arg)
561 unsigned flags = av_get_cpu_flags();
563 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
566 av_force_cpu_flags(flags);
570 int opt_codec_debug(const char *opt, const char *arg)
572 av_log_set_level(AV_LOG_DEBUG);
573 return opt_default(opt, arg);
576 int opt_timelimit(const char *opt, const char *arg)
579 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
580 struct rlimit rl = { lim, lim + 1 };
581 if (setrlimit(RLIMIT_CPU, &rl))
584 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
589 void print_error(const char *filename, int err)
592 const char *errbuf_ptr = errbuf;
594 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
595 errbuf_ptr = strerror(AVUNERROR(err));
596 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
599 static int warned_cfg = 0;
602 #define SHOW_VERSION 2
603 #define SHOW_CONFIG 4
604 #define SHOW_COPYRIGHT 8
606 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
607 if (CONFIG_##LIBNAME) { \
608 const char *indent = flags & INDENT? " " : ""; \
609 if (flags & SHOW_VERSION) { \
610 unsigned int version = libname##_version(); \
611 av_log(NULL, level, \
612 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
614 LIB##LIBNAME##_VERSION_MAJOR, \
615 LIB##LIBNAME##_VERSION_MINOR, \
616 LIB##LIBNAME##_VERSION_MICRO, \
617 version >> 16, version >> 8 & 0xff, version & 0xff); \
619 if (flags & SHOW_CONFIG) { \
620 const char *cfg = libname##_configuration(); \
621 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
623 av_log(NULL, level, \
624 "%sWARNING: library configuration mismatch\n", \
628 av_log(NULL, level, "%s%-11s configuration: %s\n", \
629 indent, #libname, cfg); \
634 static void print_all_libs_info(int flags, int level)
636 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
637 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
638 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
639 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
640 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
641 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
642 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
643 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
645 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
649 static void print_program_info(int flags, int level)
651 const char *indent = flags & INDENT? " " : "";
653 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
654 if (flags & SHOW_COPYRIGHT)
655 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
656 program_birth_year, this_year);
657 av_log(NULL, level, "\n");
658 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
659 indent, __DATE__, __TIME__, CC_IDENT);
661 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
664 void show_banner(int argc, char **argv, const OptionDef *options)
666 int idx = locate_option(argc, argv, options, "version");
670 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
671 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
672 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
675 int opt_version(const char *opt, const char *arg) {
676 av_log_set_callback(log_callback_help);
677 print_program_info (0 , AV_LOG_INFO);
678 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
682 int opt_license(const char *opt, const char *arg)
686 "This version of %s has nonfree parts compiled in.\n"
687 "Therefore it is not legally redistributable.\n",
690 "%s is free software; you can redistribute it and/or modify\n"
691 "it under the terms of the GNU General Public License as published by\n"
692 "the Free Software Foundation; either version 3 of the License, or\n"
693 "(at your option) any later version.\n"
695 "%s is distributed in the hope that it will be useful,\n"
696 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
697 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
698 "GNU General Public License for more details.\n"
700 "You should have received a copy of the GNU General Public License\n"
701 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
702 program_name, program_name, program_name
704 "%s is free software; you can redistribute it and/or modify\n"
705 "it under the terms of the GNU General Public License as published by\n"
706 "the Free Software Foundation; either version 2 of the License, or\n"
707 "(at your option) any later version.\n"
709 "%s is distributed in the hope that it will be useful,\n"
710 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
711 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
712 "GNU General Public License for more details.\n"
714 "You should have received a copy of the GNU General Public License\n"
715 "along with %s; if not, write to the Free Software\n"
716 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
717 program_name, program_name, program_name
719 "%s is free software; you can redistribute it and/or modify\n"
720 "it under the terms of the GNU Lesser General Public License as published by\n"
721 "the Free Software Foundation; either version 3 of the License, or\n"
722 "(at your option) any later version.\n"
724 "%s is distributed in the hope that it will be useful,\n"
725 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
726 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
727 "GNU Lesser General Public License for more details.\n"
729 "You should have received a copy of the GNU Lesser General Public License\n"
730 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
731 program_name, program_name, program_name
733 "%s is free software; you can redistribute it and/or\n"
734 "modify it under the terms of the GNU Lesser General Public\n"
735 "License as published by the Free Software Foundation; either\n"
736 "version 2.1 of the License, or (at your option) any later version.\n"
738 "%s is distributed in the hope that it will be useful,\n"
739 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
740 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
741 "Lesser General Public License for more details.\n"
743 "You should have received a copy of the GNU Lesser General Public\n"
744 "License along with %s; if not, write to the Free Software\n"
745 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
746 program_name, program_name, program_name
752 int opt_formats(const char *opt, const char *arg)
754 AVInputFormat *ifmt = NULL;
755 AVOutputFormat *ofmt = NULL;
756 const char *last_name;
758 printf("File formats:\n"
759 " D. = Demuxing supported\n"
760 " .E = Muxing supported\n"
766 const char *name = NULL;
767 const char *long_name = NULL;
769 while ((ofmt = av_oformat_next(ofmt))) {
770 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
771 strcmp(ofmt->name, last_name) > 0) {
773 long_name = ofmt->long_name;
777 while ((ifmt = av_iformat_next(ifmt))) {
778 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
779 strcmp(ifmt->name, last_name) > 0) {
781 long_name = ifmt->long_name;
784 if (name && strcmp(ifmt->name, name) == 0)
791 printf(" %s%s %-15s %s\n",
795 long_name ? long_name:" ");
800 static char get_media_type_char(enum AVMediaType type)
802 static const char map[AVMEDIA_TYPE_NB] = {
803 [AVMEDIA_TYPE_VIDEO] = 'V',
804 [AVMEDIA_TYPE_AUDIO] = 'A',
805 [AVMEDIA_TYPE_DATA] = 'D',
806 [AVMEDIA_TYPE_SUBTITLE] = 'S',
807 [AVMEDIA_TYPE_ATTACHMENT] = 'T',
809 return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
812 int opt_codecs(const char *opt, const char *arg)
814 AVCodec *p = NULL, *p2;
815 const char *last_name;
817 " D....... = Decoding supported\n"
818 " .E...... = Encoding supported\n"
819 " ..V..... = Video codec\n"
820 " ..A..... = Audio codec\n"
821 " ..S..... = Subtitle codec\n"
822 " ...S.... = Supports draw_horiz_band\n"
823 " ....D... = Supports direct rendering method 1\n"
824 " .....T.. = Supports weird frame truncation\n"
825 " ......F. = Supports frame-based multi-threaded decoding\n"
826 " ......S. = Supports slice-based multi-threaded decoding\n"
827 " ......B. = Supports both frame-based and slice-based multi-threaded decoding\n"
828 " .......F = Supports frame-based multi-threaded encoding\n"
829 " .......S = Supports slice-based multi-threaded encoding\n"
830 " .......B = Supports both frame-based and slice-based multi-threaded encoding\n"
839 while ((p = av_codec_next(p))) {
840 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
841 strcmp(p->name, last_name) > 0) {
843 decode = encode = cap = 0;
845 if (p2 && strcmp(p->name, p2->name) == 0) {
846 if (av_codec_is_decoder(p))
848 if (av_codec_is_encoder(p))
850 cap |= p->capabilities;
855 last_name = p2->name;
857 printf(" %s%s%c%s%s%s%s%s %-15s %s",
858 decode ? "D" : (/* p2->decoder ? "d" : */ " "),
860 get_media_type_char(p2->type),
861 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
862 cap & CODEC_CAP_DR1 ? "D" : " ",
863 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
865 cap & CODEC_CAP_FRAME_THREADS ? cap & CODEC_CAP_SLICE_THREADS ? "B" : "F" :
866 cap & CODEC_CAP_SLICE_THREADS ? "S" : " "
869 cap & CODEC_CAP_FRAME_THREADS ? cap & CODEC_CAP_SLICE_THREADS ? "B" : "F" :
870 cap & CODEC_CAP_SLICE_THREADS ? "S" : " "
873 p2->long_name ? p2->long_name : "");
875 if (p2->decoder && decode == 0)
876 printf(" use %s for decoding", p2->decoder->name);
881 printf("Note, the names of encoders and decoders do not always match, so there are\n"
882 "several cases where the above table shows encoder only or decoder only entries\n"
883 "even though both encoding and decoding are supported. For example, the h263\n"
884 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
889 int opt_bsfs(const char *opt, const char *arg)
891 AVBitStreamFilter *bsf = NULL;
893 printf("Bitstream filters:\n");
894 while ((bsf = av_bitstream_filter_next(bsf)))
895 printf("%s\n", bsf->name);
900 int opt_protocols(const char *opt, const char *arg)
905 printf("Supported file protocols:\n"
907 while ((name = avio_enum_protocols(&opaque, 0)))
908 printf("%s\n", name);
910 while ((name = avio_enum_protocols(&opaque, 1)))
911 printf("%s\n", name);
915 int opt_filters(const char *opt, const char *arg)
917 AVFilter av_unused(**filter) = NULL;
918 char descr[64], *descr_cur;
920 const AVFilterPad *pad;
922 printf("Filters:\n");
924 while ((filter = av_filter_next(filter)) && *filter) {
926 for (i = 0; i < 2; i++) {
928 *(descr_cur++) = '-';
929 *(descr_cur++) = '>';
931 pad = i ? (*filter)->outputs : (*filter)->inputs;
932 for (j = 0; pad[j].name; j++) {
933 if (descr_cur >= descr + sizeof(descr) - 4)
935 *(descr_cur++) = get_media_type_char(pad[j].type);
938 *(descr_cur++) = '|';
941 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
947 int opt_pix_fmts(const char *opt, const char *arg)
949 enum PixelFormat pix_fmt;
951 printf("Pixel formats:\n"
952 "I.... = Supported Input format for conversion\n"
953 ".O... = Supported Output format for conversion\n"
954 "..H.. = Hardware accelerated format\n"
955 "...P. = Paletted format\n"
956 "....B = Bitstream format\n"
957 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
961 # define sws_isSupportedInput(x) 0
962 # define sws_isSupportedOutput(x) 0
965 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
966 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
969 printf("%c%c%c%c%c %-16s %d %2d\n",
970 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
971 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
972 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
973 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
974 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
976 pix_desc->nb_components,
977 av_get_bits_per_pixel(pix_desc));
982 int show_sample_fmts(const char *opt, const char *arg)
986 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
987 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
994 int yesno = (toupper(c) == 'Y');
996 while (c != '\n' && c != EOF)
1002 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1005 FILE *f = fopen(filename, "rb");
1008 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1010 return AVERROR(errno);
1012 fseek(f, 0, SEEK_END);
1014 fseek(f, 0, SEEK_SET);
1015 *bufptr = av_malloc(*size + 1);
1017 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1019 return AVERROR(ENOMEM);
1021 ret = fread(*bufptr, 1, *size, f);
1025 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1026 filename, strerror(errno));
1027 ret = AVERROR(errno);
1032 (*bufptr)[*size++] = '\0';
1039 FILE *get_preset_file(char *filename, size_t filename_size,
1040 const char *preset_name, int is_path,
1041 const char *codec_name)
1045 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1050 av_strlcpy(filename, preset_name, filename_size);
1051 f = fopen(filename, "r");
1054 char datadir[MAX_PATH], *ls;
1057 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1059 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1060 if (*ls == '\\') *ls = '/';
1062 if (ls = strrchr(datadir, '/'))
1065 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1070 for (i = 0; i < 3 && !f; i++) {
1073 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1074 i != 1 ? "" : "/.ffmpeg", preset_name);
1075 f = fopen(filename, "r");
1076 if (!f && codec_name) {
1077 snprintf(filename, filename_size,
1078 "%s%s/%s-%s.ffpreset",
1079 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1081 f = fopen(filename, "r");
1089 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1091 int ret = avformat_match_stream_specifier(s, st, spec);
1093 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1097 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1098 AVFormatContext *s, AVStream *st, AVCodec *codec)
1100 AVDictionary *ret = NULL;
1101 AVDictionaryEntry *t = NULL;
1102 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1103 : AV_OPT_FLAG_DECODING_PARAM;
1105 const AVClass *cc = avcodec_get_class();
1108 codec = s->oformat ? avcodec_find_encoder(codec_id)
1109 : avcodec_find_decoder(codec_id);
1113 switch (codec->type) {
1114 case AVMEDIA_TYPE_VIDEO:
1116 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1118 case AVMEDIA_TYPE_AUDIO:
1120 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1122 case AVMEDIA_TYPE_SUBTITLE:
1124 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1128 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1129 char *p = strchr(t->key, ':');
1131 /* check stream specification in opt name */
1133 switch (check_stream_specifier(s, st, p + 1)) {
1134 case 1: *p = 0; break;
1136 default: return NULL;
1139 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1140 (codec && codec->priv_class &&
1141 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1142 AV_OPT_SEARCH_FAKE_OBJ)))
1143 av_dict_set(&ret, t->key, t->value, 0);
1144 else if (t->key[0] == prefix &&
1145 av_opt_find(&cc, t->key + 1, NULL, flags,
1146 AV_OPT_SEARCH_FAKE_OBJ))
1147 av_dict_set(&ret, t->key + 1, t->value, 0);
1155 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1156 AVDictionary *codec_opts)
1159 AVDictionary **opts;
1163 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1165 av_log(NULL, AV_LOG_ERROR,
1166 "Could not alloc memory for stream options.\n");
1169 for (i = 0; i < s->nb_streams; i++)
1170 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1171 s, s->streams[i], NULL);
1175 void *grow_array(void *array, int elem_size, int *size, int new_size)
1177 if (new_size >= INT_MAX / elem_size) {
1178 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1181 if (*size < new_size) {
1182 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1184 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1187 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1194 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1196 FrameBuffer *buf = av_mallocz(sizeof(*buf));
1198 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
1199 int h_chroma_shift, v_chroma_shift;
1200 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1201 int w = s->width, h = s->height;
1204 return AVERROR(ENOMEM);
1206 avcodec_align_dimensions(s, &w, &h);
1208 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1213 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1214 s->pix_fmt, 32)) < 0) {
1216 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
1219 /* XXX this shouldn't be needed, but some tests break without this line
1220 * those decoders are buggy and need to be fixed.
1221 * the following tests fail:
1222 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1224 memset(buf->base[0], 128, ret);
1226 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1227 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1228 const int h_shift = i==0 ? 0 : h_chroma_shift;
1229 const int v_shift = i==0 ? 0 : v_chroma_shift;
1230 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
1231 buf->data[i] = buf->base[i];
1233 buf->data[i] = buf->base[i] +
1234 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1235 (pixel_size*edge >> h_shift), 32);
1239 buf->pix_fmt = s->pix_fmt;
1246 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1248 FrameBuffer **pool = s->opaque;
1252 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
1253 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
1257 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1263 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1264 av_freep(&buf->base[0]);
1266 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1269 av_assert0(!buf->refcount);
1272 frame->opaque = buf;
1273 frame->type = FF_BUFFER_TYPE_USER;
1274 frame->extended_data = frame->data;
1275 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1276 frame->width = buf->w;
1277 frame->height = buf->h;
1278 frame->format = buf->pix_fmt;
1279 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1281 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1282 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1283 frame->data[i] = buf->data[i];
1284 frame->linesize[i] = buf->linesize[i];
1290 static void unref_buffer(FrameBuffer *buf)
1292 FrameBuffer **pool = buf->pool;
1294 av_assert0(buf->refcount > 0);
1296 if (!buf->refcount) {
1298 for(tmp= *pool; tmp; tmp= tmp->next)
1299 av_assert1(tmp != buf);
1306 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1308 FrameBuffer *buf = frame->opaque;
1311 if(frame->type!=FF_BUFFER_TYPE_USER) {
1312 avcodec_default_release_buffer(s, frame);
1316 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1317 frame->data[i] = NULL;
1322 void filter_release_buffer(AVFilterBuffer *fb)
1324 FrameBuffer *buf = fb->priv;
1329 void free_buffer_pool(FrameBuffer **pool)
1331 FrameBuffer *buf = *pool;
1334 av_freep(&buf->base[0]);