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,
151 for (po = options; po->name != NULL; po++) {
154 if (((po->flags & req_flags) != req_flags) ||
155 (po->flags & rej_flags))
162 av_strlcpy(buf, po->name, sizeof(buf));
163 if (po->flags & HAS_ARG) {
164 av_strlcat(buf, " ", sizeof(buf));
165 av_strlcat(buf, po->argname, sizeof(buf));
167 printf("-%-17s %s\n", buf, po->help);
172 void show_help_children(const AVClass *class, int flags)
174 const AVClass *child = NULL;
176 av_opt_show2(&class, NULL, flags, 0);
180 while (child = av_opt_child_class_next(class, child))
181 show_help_children(child, flags);
184 static const OptionDef *find_option(const OptionDef *po, const char *name)
186 const char *p = strchr(name, ':');
187 int len = p ? p - name : strlen(name);
189 while (po->name != NULL) {
190 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
197 #if defined(_WIN32) && !defined(__MINGW32CE__)
199 #include <shellapi.h>
200 /* Will be leaked on exit */
201 static char** win32_argv_utf8 = NULL;
202 static int win32_argc = 0;
205 * Prepare command line arguments for executable.
206 * For Windows - perform wide-char to UTF-8 conversion.
207 * Input arguments should be main() function arguments.
208 * @param argc_ptr Arguments number (including executable)
209 * @param argv_ptr Arguments list.
211 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
215 int i, buffsize = 0, offset = 0;
217 if (win32_argv_utf8) {
218 *argc_ptr = win32_argc;
219 *argv_ptr = win32_argv_utf8;
224 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
225 if (win32_argc <= 0 || !argv_w)
228 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
229 for (i = 0; i < win32_argc; i++)
230 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
231 NULL, 0, NULL, NULL);
233 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
234 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
235 if (win32_argv_utf8 == NULL) {
240 for (i = 0; i < win32_argc; i++) {
241 win32_argv_utf8[i] = &argstr_flat[offset];
242 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
243 &argstr_flat[offset],
244 buffsize - offset, NULL, NULL);
246 win32_argv_utf8[i] = NULL;
249 *argc_ptr = win32_argc;
250 *argv_ptr = win32_argv_utf8;
253 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
257 #endif /* WIN32 && !__MINGW32CE__ */
259 int parse_option(void *optctx, const char *opt, const char *arg,
260 const OptionDef *options)
267 po = find_option(options, opt);
268 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
269 /* handle 'no' bool option */
270 po = find_option(options, opt + 2);
271 if ((po->name && (po->flags & OPT_BOOL)))
275 po = find_option(options, "default");
277 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
278 return AVERROR(EINVAL);
280 if (po->flags & HAS_ARG && !arg) {
281 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
282 return AVERROR(EINVAL);
285 /* new-style options contain an offset into optctx, old-style address of
287 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
290 if (po->flags & OPT_SPEC) {
291 SpecifierOpt **so = dst;
292 char *p = strchr(opt, ':');
294 dstcount = (int *)(so + 1);
295 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
296 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
297 dst = &(*so)[*dstcount - 1].u;
300 if (po->flags & OPT_STRING) {
302 str = av_strdup(arg);
304 } else if (po->flags & OPT_BOOL) {
305 *(int *)dst = bool_val;
306 } else if (po->flags & OPT_INT) {
307 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
308 } else if (po->flags & OPT_INT64) {
309 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
310 } else if (po->flags & OPT_TIME) {
311 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
312 } else if (po->flags & OPT_FLOAT) {
313 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
314 } else if (po->flags & OPT_DOUBLE) {
315 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
316 } else if (po->u.func_arg) {
317 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
318 : po->u.func_arg(opt, arg);
320 av_log(NULL, AV_LOG_ERROR,
321 "Failed to set value '%s' for option '%s'\n", arg, opt);
325 if (po->flags & OPT_EXIT)
327 return !!(po->flags & HAS_ARG);
330 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
331 void (*parse_arg_function)(void *, const char*))
334 int optindex, handleoptions = 1, ret;
336 /* perform system-dependent conversions for arguments list */
337 prepare_app_arguments(&argc, &argv);
341 while (optindex < argc) {
342 opt = argv[optindex++];
344 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
345 if (opt[1] == '-' && opt[2] == '\0') {
351 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
355 if (parse_arg_function)
356 parse_arg_function(optctx, opt);
361 int locate_option(int argc, char **argv, const OptionDef *options,
367 for (i = 1; i < argc; i++) {
368 const char *cur_opt = argv[i];
370 if (*cur_opt++ != '-')
373 po = find_option(options, cur_opt);
374 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
375 po = find_option(options, cur_opt + 2);
377 if ((!po->name && !strcmp(cur_opt, optname)) ||
378 (po->name && !strcmp(optname, po->name)))
381 if (!po || po->flags & HAS_ARG)
387 static void dump_argument(const char *a)
389 const unsigned char *p;
392 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
393 *p == '_' || (*p >= 'a' && *p <= 'z')))
396 fputs(a, report_file);
399 fputc('"', report_file);
400 for (p = a; *p; p++) {
401 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
402 fprintf(report_file, "\\%c", *p);
403 else if (*p < ' ' || *p > '~')
404 fprintf(report_file, "\\x%02x", *p);
406 fputc(*p, report_file);
408 fputc('"', report_file);
411 void parse_loglevel(int argc, char **argv, const OptionDef *options)
413 int idx = locate_option(argc, argv, options, "loglevel");
415 idx = locate_option(argc, argv, options, "v");
416 if (idx && argv[idx + 1])
417 opt_loglevel("loglevel", argv[idx + 1]);
418 idx = locate_option(argc, argv, options, "report");
419 if (idx || getenv("FFREPORT")) {
420 opt_report("report");
423 fprintf(report_file, "Command line:\n");
424 for (i = 0; i < argc; i++) {
425 dump_argument(argv[i]);
426 fputc(i < argc - 1 ? ' ' : '\n', report_file);
433 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
434 int opt_default(const char *opt, const char *arg)
437 char opt_stripped[128];
439 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
441 if (!(p = strchr(opt, ':')))
442 p = opt + strlen(opt);
443 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
445 if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
446 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
447 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
448 (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
449 av_dict_set(&codec_opts, opt, arg, FLAGS);
450 else if ((o = av_opt_find(&fc, opt, NULL, 0,
451 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
452 av_dict_set(&format_opts, opt, arg, FLAGS);
454 sc = sws_get_class();
455 if (!o && (o = av_opt_find(&sc, opt, NULL, 0,
456 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
457 // XXX we only support sws_flags, not arbitrary sws options
458 int ret = av_opt_set(sws_opts, opt, arg, 0);
460 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
465 #if CONFIG_SWRESAMPLE
466 swr_class = swr_get_class();
467 if (!o && (o = av_opt_find(&swr_class, opt, NULL, 0,
468 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
469 int ret = av_opt_set(swr_opts, opt, arg, 0);
471 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
479 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
480 return AVERROR_OPTION_NOT_FOUND;
483 int opt_loglevel(const char *opt, const char *arg)
485 const struct { const char *name; int level; } log_levels[] = {
486 { "quiet" , AV_LOG_QUIET },
487 { "panic" , AV_LOG_PANIC },
488 { "fatal" , AV_LOG_FATAL },
489 { "error" , AV_LOG_ERROR },
490 { "warning", AV_LOG_WARNING },
491 { "info" , AV_LOG_INFO },
492 { "verbose", AV_LOG_VERBOSE },
493 { "debug" , AV_LOG_DEBUG },
499 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
500 if (!strcmp(log_levels[i].name, arg)) {
501 av_log_set_level(log_levels[i].level);
506 level = strtol(arg, &tail, 10);
508 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
509 "Possible levels are numbers or:\n", arg);
510 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
511 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
514 av_log_set_level(level);
518 int opt_report(const char *opt)
524 if (report_file) /* already opened */
527 tm = localtime(&now);
528 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
530 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
531 tm->tm_hour, tm->tm_min, tm->tm_sec);
532 report_file = fopen(filename, "w");
534 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
535 filename, strerror(errno));
536 return AVERROR(errno);
538 av_log_set_callback(log_callback_report);
539 av_log(NULL, AV_LOG_INFO,
540 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
541 "Report written to \"%s\"\n",
543 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
544 tm->tm_hour, tm->tm_min, tm->tm_sec,
546 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
550 int opt_max_alloc(const char *opt, const char *arg)
555 max = strtol(arg, &tail, 10);
557 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
564 int opt_cpuflags(const char *opt, const char *arg)
567 unsigned flags = av_get_cpu_flags();
569 if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
572 av_force_cpu_flags(flags);
576 int opt_codec_debug(const char *opt, const char *arg)
578 av_log_set_level(AV_LOG_DEBUG);
579 return opt_default(opt, arg);
582 int opt_timelimit(const char *opt, const char *arg)
585 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
586 struct rlimit rl = { lim, lim + 1 };
587 if (setrlimit(RLIMIT_CPU, &rl))
590 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
595 void print_error(const char *filename, int err)
598 const char *errbuf_ptr = errbuf;
600 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
601 errbuf_ptr = strerror(AVUNERROR(err));
602 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
605 static int warned_cfg = 0;
608 #define SHOW_VERSION 2
609 #define SHOW_CONFIG 4
610 #define SHOW_COPYRIGHT 8
612 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
613 if (CONFIG_##LIBNAME) { \
614 const char *indent = flags & INDENT? " " : ""; \
615 if (flags & SHOW_VERSION) { \
616 unsigned int version = libname##_version(); \
617 av_log(NULL, level, \
618 "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
620 LIB##LIBNAME##_VERSION_MAJOR, \
621 LIB##LIBNAME##_VERSION_MINOR, \
622 LIB##LIBNAME##_VERSION_MICRO, \
623 version >> 16, version >> 8 & 0xff, version & 0xff); \
625 if (flags & SHOW_CONFIG) { \
626 const char *cfg = libname##_configuration(); \
627 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
629 av_log(NULL, level, \
630 "%sWARNING: library configuration mismatch\n", \
634 av_log(NULL, level, "%s%-11s configuration: %s\n", \
635 indent, #libname, cfg); \
640 static void print_all_libs_info(int flags, int level)
642 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
643 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
644 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
645 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
646 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
647 // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
648 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
649 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
651 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
655 static void print_program_info(int flags, int level)
657 const char *indent = flags & INDENT? " " : "";
659 av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
660 if (flags & SHOW_COPYRIGHT)
661 av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
662 program_birth_year, this_year);
663 av_log(NULL, level, "\n");
664 av_log(NULL, level, "%sbuilt on %s %s with %s\n",
665 indent, __DATE__, __TIME__, CC_IDENT);
667 av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
670 void show_banner(int argc, char **argv, const OptionDef *options)
672 int idx = locate_option(argc, argv, options, "version");
676 print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
677 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
678 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
681 int show_version(const char *opt, const char *arg)
683 av_log_set_callback(log_callback_help);
684 print_program_info (0 , AV_LOG_INFO);
685 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
690 int show_license(const char *opt, const char *arg)
694 "This version of %s has nonfree parts compiled in.\n"
695 "Therefore it is not legally redistributable.\n",
698 "%s is free software; you can redistribute it and/or modify\n"
699 "it under the terms of the GNU General Public License as published by\n"
700 "the Free Software Foundation; either version 3 of the License, or\n"
701 "(at your option) any later version.\n"
703 "%s is distributed in the hope that it will be useful,\n"
704 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
705 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
706 "GNU General Public License for more details.\n"
708 "You should have received a copy of the GNU General Public License\n"
709 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
710 program_name, program_name, program_name
712 "%s is free software; you can redistribute it and/or modify\n"
713 "it under the terms of the GNU General Public License as published by\n"
714 "the Free Software Foundation; either version 2 of the License, or\n"
715 "(at your option) any later version.\n"
717 "%s is distributed in the hope that it will be useful,\n"
718 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
719 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
720 "GNU General Public License for more details.\n"
722 "You should have received a copy of the GNU General Public License\n"
723 "along with %s; if not, write to the Free Software\n"
724 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
725 program_name, program_name, program_name
727 "%s is free software; you can redistribute it and/or modify\n"
728 "it under the terms of the GNU Lesser General Public License as published by\n"
729 "the Free Software Foundation; either version 3 of the License, or\n"
730 "(at your option) any later version.\n"
732 "%s is distributed in the hope that it will be useful,\n"
733 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
734 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
735 "GNU Lesser General Public License for more details.\n"
737 "You should have received a copy of the GNU Lesser General Public License\n"
738 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
739 program_name, program_name, program_name
741 "%s is free software; you can redistribute it and/or\n"
742 "modify it under the terms of the GNU Lesser General Public\n"
743 "License as published by the Free Software Foundation; either\n"
744 "version 2.1 of the License, or (at your option) any later version.\n"
746 "%s is distributed in the hope that it will be useful,\n"
747 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
748 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
749 "Lesser General Public License for more details.\n"
751 "You should have received a copy of the GNU Lesser General Public\n"
752 "License along with %s; if not, write to the Free Software\n"
753 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
754 program_name, program_name, program_name
761 int show_formats(const char *opt, const char *arg)
763 AVInputFormat *ifmt = NULL;
764 AVOutputFormat *ofmt = NULL;
765 const char *last_name;
767 printf("File formats:\n"
768 " D. = Demuxing supported\n"
769 " .E = Muxing supported\n"
775 const char *name = NULL;
776 const char *long_name = NULL;
778 while ((ofmt = av_oformat_next(ofmt))) {
779 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
780 strcmp(ofmt->name, last_name) > 0) {
782 long_name = ofmt->long_name;
786 while ((ifmt = av_iformat_next(ifmt))) {
787 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
788 strcmp(ifmt->name, last_name) > 0) {
790 long_name = ifmt->long_name;
793 if (name && strcmp(ifmt->name, name) == 0)
800 printf(" %s%s %-15s %s\n",
804 long_name ? long_name:" ");
809 static char get_media_type_char(enum AVMediaType type)
812 case AVMEDIA_TYPE_VIDEO: return 'V';
813 case AVMEDIA_TYPE_AUDIO: return 'A';
814 case AVMEDIA_TYPE_DATA: return 'D';
815 case AVMEDIA_TYPE_SUBTITLE: return 'S';
816 case AVMEDIA_TYPE_ATTACHMENT:return 'T';
821 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
824 while ((prev = av_codec_next(prev))) {
825 if (prev->id == id &&
826 (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
832 static void print_codecs_for_id(enum AVCodecID id, int encoder)
834 const AVCodec *codec = NULL;
836 printf(" (%s: ", encoder ? "encoders" : "decoders");
838 while ((codec = next_codec_for_id(id, codec, encoder)))
839 printf("%s ", codec->name);
844 int show_codecs(const char *opt, const char *arg)
846 const AVCodecDescriptor *desc = NULL;
849 " D... = Decoding supported\n"
850 " .E.. = Encoding supported\n"
851 " ..V. = Video codec\n"
852 " ..A. = Audio codec\n"
853 " ..S. = Subtitle codec\n"
854 " ...I = Intra frame-only codec\n"
856 while ((desc = avcodec_descriptor_next(desc))) {
857 const AVCodec *codec = NULL;
859 printf(avcodec_find_decoder(desc->id) ? "D" : ".");
860 printf(avcodec_find_encoder(desc->id) ? "E" : ".");
862 printf("%c", get_media_type_char(desc->type));
863 printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
865 printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
867 /* print decoders/encoders when there's more than one or their
868 * names are different from codec name */
869 while ((codec = next_codec_for_id(desc->id, codec, 0))) {
870 if (strcmp(codec->name, desc->name)) {
871 print_codecs_for_id(desc->id, 0);
876 while ((codec = next_codec_for_id(desc->id, codec, 1))) {
877 if (strcmp(codec->name, desc->name)) {
878 print_codecs_for_id(desc->id, 1);
888 static void print_codecs(int encoder)
890 const AVCodecDescriptor *desc = NULL;
895 " S..... = Subtitle\n"
896 " .F.... = Frame-level multithreading\n"
897 " ..S... = Slice-level multithreading\n"
898 " ...X.. = Codec is experimental\n"
899 " ....B. = Supports draw_horiz_band\n"
900 " .....D = Supports direct rendering method 1\n"
902 encoder ? "Encoders" : "Decoders");
903 while ((desc = avcodec_descriptor_next(desc))) {
904 const AVCodec *codec = NULL;
906 while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
907 printf("%c", get_media_type_char(desc->type));
908 printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
909 printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
910 printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
911 printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
912 printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
914 printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
915 if (strcmp(codec->name, desc->name))
916 printf(" (codec %s)", desc->name);
923 int show_decoders(const char *opt, const char *arg)
929 int show_encoders(const char *opt, const char *arg)
935 int show_bsfs(const char *opt, const char *arg)
937 AVBitStreamFilter *bsf = NULL;
939 printf("Bitstream filters:\n");
940 while ((bsf = av_bitstream_filter_next(bsf)))
941 printf("%s\n", bsf->name);
946 int show_protocols(const char *opt, const char *arg)
951 printf("Supported file protocols:\n"
953 while ((name = avio_enum_protocols(&opaque, 0)))
954 printf("%s\n", name);
956 while ((name = avio_enum_protocols(&opaque, 1)))
957 printf("%s\n", name);
961 int show_filters(const char *opt, const char *arg)
963 AVFilter av_unused(**filter) = NULL;
964 char descr[64], *descr_cur;
966 const AVFilterPad *pad;
968 printf("Filters:\n");
970 while ((filter = av_filter_next(filter)) && *filter) {
972 for (i = 0; i < 2; i++) {
974 *(descr_cur++) = '-';
975 *(descr_cur++) = '>';
977 pad = i ? (*filter)->outputs : (*filter)->inputs;
978 for (j = 0; pad[j].name; j++) {
979 if (descr_cur >= descr + sizeof(descr) - 4)
981 *(descr_cur++) = get_media_type_char(pad[j].type);
984 *(descr_cur++) = '|';
987 printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
993 int show_pix_fmts(const char *opt, const char *arg)
995 enum PixelFormat pix_fmt;
997 printf("Pixel formats:\n"
998 "I.... = Supported Input format for conversion\n"
999 ".O... = Supported Output format for conversion\n"
1000 "..H.. = Hardware accelerated format\n"
1001 "...P. = Paletted format\n"
1002 "....B = Bitstream format\n"
1003 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1007 # define sws_isSupportedInput(x) 0
1008 # define sws_isSupportedOutput(x) 0
1011 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
1012 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
1015 printf("%c%c%c%c%c %-16s %d %2d\n",
1016 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1017 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1018 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
1019 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
1020 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
1022 pix_desc->nb_components,
1023 av_get_bits_per_pixel(pix_desc));
1028 int show_sample_fmts(const char *opt, const char *arg)
1032 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1033 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1037 int read_yesno(void)
1040 int yesno = (toupper(c) == 'Y');
1042 while (c != '\n' && c != EOF)
1048 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1051 FILE *f = fopen(filename, "rb");
1054 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1056 return AVERROR(errno);
1058 fseek(f, 0, SEEK_END);
1060 fseek(f, 0, SEEK_SET);
1061 *bufptr = av_malloc(*size + 1);
1063 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1065 return AVERROR(ENOMEM);
1067 ret = fread(*bufptr, 1, *size, f);
1071 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1072 filename, strerror(errno));
1073 ret = AVERROR(errno);
1078 (*bufptr)[*size++] = '\0';
1085 FILE *get_preset_file(char *filename, size_t filename_size,
1086 const char *preset_name, int is_path,
1087 const char *codec_name)
1091 const char *base[3] = { getenv("FFMPEG_DATADIR"),
1096 av_strlcpy(filename, preset_name, filename_size);
1097 f = fopen(filename, "r");
1100 char datadir[MAX_PATH], *ls;
1103 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1105 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1106 if (*ls == '\\') *ls = '/';
1108 if (ls = strrchr(datadir, '/'))
1111 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
1116 for (i = 0; i < 3 && !f; i++) {
1119 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1120 i != 1 ? "" : "/.ffmpeg", preset_name);
1121 f = fopen(filename, "r");
1122 if (!f && codec_name) {
1123 snprintf(filename, filename_size,
1124 "%s%s/%s-%s.ffpreset",
1125 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1127 f = fopen(filename, "r");
1135 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1137 int ret = avformat_match_stream_specifier(s, st, spec);
1139 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1143 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1144 AVFormatContext *s, AVStream *st, AVCodec *codec)
1146 AVDictionary *ret = NULL;
1147 AVDictionaryEntry *t = NULL;
1148 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1149 : AV_OPT_FLAG_DECODING_PARAM;
1151 const AVClass *cc = avcodec_get_class();
1154 codec = s->oformat ? avcodec_find_encoder(codec_id)
1155 : avcodec_find_decoder(codec_id);
1159 switch (codec->type) {
1160 case AVMEDIA_TYPE_VIDEO:
1162 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1164 case AVMEDIA_TYPE_AUDIO:
1166 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1168 case AVMEDIA_TYPE_SUBTITLE:
1170 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1174 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1175 char *p = strchr(t->key, ':');
1177 /* check stream specification in opt name */
1179 switch (check_stream_specifier(s, st, p + 1)) {
1180 case 1: *p = 0; break;
1182 default: return NULL;
1185 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1186 (codec && codec->priv_class &&
1187 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1188 AV_OPT_SEARCH_FAKE_OBJ)))
1189 av_dict_set(&ret, t->key, t->value, 0);
1190 else if (t->key[0] == prefix &&
1191 av_opt_find(&cc, t->key + 1, NULL, flags,
1192 AV_OPT_SEARCH_FAKE_OBJ))
1193 av_dict_set(&ret, t->key + 1, t->value, 0);
1201 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1202 AVDictionary *codec_opts)
1205 AVDictionary **opts;
1209 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1211 av_log(NULL, AV_LOG_ERROR,
1212 "Could not alloc memory for stream options.\n");
1215 for (i = 0; i < s->nb_streams; i++)
1216 opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1217 s, s->streams[i], NULL);
1221 void *grow_array(void *array, int elem_size, int *size, int new_size)
1223 if (new_size >= INT_MAX / elem_size) {
1224 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1227 if (*size < new_size) {
1228 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1230 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1233 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1240 static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
1242 FrameBuffer *buf = av_mallocz(sizeof(*buf));
1244 const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
1245 int h_chroma_shift, v_chroma_shift;
1246 int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
1247 int w = s->width, h = s->height;
1250 return AVERROR(ENOMEM);
1252 avcodec_align_dimensions(s, &w, &h);
1254 if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
1259 if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
1260 s->pix_fmt, 32)) < 0) {
1262 av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
1265 /* XXX this shouldn't be needed, but some tests break without this line
1266 * those decoders are buggy and need to be fixed.
1267 * the following tests fail:
1268 * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
1270 memset(buf->base[0], 128, ret);
1272 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1273 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1274 const int h_shift = i==0 ? 0 : h_chroma_shift;
1275 const int v_shift = i==0 ? 0 : v_chroma_shift;
1276 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
1277 buf->data[i] = buf->base[i];
1279 buf->data[i] = buf->base[i] +
1280 FFALIGN((buf->linesize[i]*edge >> v_shift) +
1281 (pixel_size*edge >> h_shift), 32);
1285 buf->pix_fmt = s->pix_fmt;
1292 int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
1294 FrameBuffer **pool = s->opaque;
1298 if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
1299 av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
1303 if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
1309 if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
1310 av_freep(&buf->base[0]);
1312 if ((ret = alloc_buffer(pool, s, &buf)) < 0)
1315 av_assert0(!buf->refcount);
1318 frame->opaque = buf;
1319 frame->type = FF_BUFFER_TYPE_USER;
1320 frame->extended_data = frame->data;
1321 frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
1322 frame->width = buf->w;
1323 frame->height = buf->h;
1324 frame->format = buf->pix_fmt;
1325 frame->sample_aspect_ratio = s->sample_aspect_ratio;
1327 for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
1328 frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
1329 frame->data[i] = buf->data[i];
1330 frame->linesize[i] = buf->linesize[i];
1336 static void unref_buffer(FrameBuffer *buf)
1338 FrameBuffer **pool = buf->pool;
1340 av_assert0(buf->refcount > 0);
1342 if (!buf->refcount) {
1344 for(tmp= *pool; tmp; tmp= tmp->next)
1345 av_assert1(tmp != buf);
1352 void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
1354 FrameBuffer *buf = frame->opaque;
1357 if(frame->type!=FF_BUFFER_TYPE_USER) {
1358 avcodec_default_release_buffer(s, frame);
1362 for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
1363 frame->data[i] = NULL;
1368 void filter_release_buffer(AVFilterBuffer *fb)
1370 FrameBuffer *buf = fb->priv;
1375 void free_buffer_pool(FrameBuffer **pool)
1377 FrameBuffer *buf = *pool;
1380 av_freep(&buf->base[0]);