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 "libswscale/swscale.h"
36 #include "libpostproc/postprocess.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/eval.h"
42 #include "libavutil/dict.h"
43 #include "libavutil/opt.h"
47 #include "libavformat/network.h"
49 #if HAVE_SYS_RESOURCE_H
50 #include <sys/resource.h>
53 struct SwsContext *sws_opts;
54 AVDictionary *format_opts, *codec_opts;
56 static const int this_year = 2011;
58 static FILE *report_file;
63 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
67 void uninit_opts(void)
70 sws_freeContext(sws_opts);
73 av_dict_free(&format_opts);
74 av_dict_free(&codec_opts);
77 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
79 vfprintf(stdout, fmt, vl);
82 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
86 static int print_prefix = 1;
89 av_log_default_callback(ptr, level, fmt, vl);
90 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
92 fputs(line, report_file);
96 double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
100 double d = av_strtod(numstr, &tail);
102 error= "Expected number for %s but found: %s\n";
103 else if (d < min || d > max)
104 error= "The value for %s was %s which is not within %f - %f\n";
105 else if(type == OPT_INT64 && (int64_t)d != d)
106 error= "Expected int64 for %s but found %s\n";
107 else if (type == OPT_INT && (int)d != d)
108 error= "Expected int for %s but found %s\n";
111 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
116 int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
119 if (av_parse_time(&us, timestr, is_duration) < 0) {
120 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
121 is_duration ? "duration" : "date", context, timestr);
127 void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
133 for(po = options; po->name != NULL; po++) {
135 if ((po->flags & mask) == value) {
140 av_strlcpy(buf, po->name, sizeof(buf));
141 if (po->flags & HAS_ARG) {
142 av_strlcat(buf, " ", sizeof(buf));
143 av_strlcat(buf, po->argname, sizeof(buf));
145 printf("-%-17s %s\n", buf, po->help);
150 void show_help_children(const AVClass *class, int flags)
152 const AVClass *child = NULL;
153 av_opt_show2(&class, NULL, flags, 0);
156 while (child = av_opt_child_class_next(class, child))
157 show_help_children(child, flags);
160 static const OptionDef* find_option(const OptionDef *po, const char *name){
161 const char *p = strchr(name, ':');
162 int len = p ? p - name : strlen(name);
164 while (po->name != NULL) {
165 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
172 #if defined(_WIN32) && !defined(__MINGW32CE__)
174 /* Will be leaked on exit */
175 static char** win32_argv_utf8 = NULL;
176 static int win32_argc = 0;
179 * Prepare command line arguments for executable.
180 * For Windows - perform wide-char to UTF-8 conversion.
181 * Input arguments should be main() function arguments.
182 * @param argc_ptr Arguments number (including executable)
183 * @param argv_ptr Arguments list.
185 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
189 int i, buffsize = 0, offset = 0;
191 if (win32_argv_utf8) {
192 *argc_ptr = win32_argc;
193 *argv_ptr = win32_argv_utf8;
198 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
199 if (win32_argc <= 0 || !argv_w)
202 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
203 for (i = 0; i < win32_argc; i++)
204 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
205 NULL, 0, NULL, NULL);
207 win32_argv_utf8 = av_mallocz(sizeof(char*) * (win32_argc + 1) + buffsize);
208 argstr_flat = (char*)win32_argv_utf8 + sizeof(char*) * (win32_argc + 1);
209 if (win32_argv_utf8 == NULL) {
214 for (i = 0; i < win32_argc; i++) {
215 win32_argv_utf8[i] = &argstr_flat[offset];
216 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
217 &argstr_flat[offset],
218 buffsize - offset, NULL, NULL);
220 win32_argv_utf8[i] = NULL;
223 *argc_ptr = win32_argc;
224 *argv_ptr = win32_argv_utf8;
227 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
231 #endif /* WIN32 && !__MINGW32CE__ */
234 int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
241 po = find_option(options, opt);
242 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
243 /* handle 'no' bool option */
244 po = find_option(options, opt + 2);
245 if (!(po->name && (po->flags & OPT_BOOL)))
250 po = find_option(options, "default");
253 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
254 return AVERROR(EINVAL);
256 if (po->flags & HAS_ARG && !arg) {
257 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
258 return AVERROR(EINVAL);
261 /* new-style options contain an offset into optctx, old-style address of
263 dst = po->flags & (OPT_OFFSET|OPT_SPEC) ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr;
265 if (po->flags & OPT_SPEC) {
266 SpecifierOpt **so = dst;
267 char *p = strchr(opt, ':');
269 dstcount = (int*)(so + 1);
270 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
271 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
272 dst = &(*so)[*dstcount - 1].u;
275 if (po->flags & OPT_STRING) {
277 str = av_strdup(arg);
279 } else if (po->flags & OPT_BOOL) {
280 *(int*)dst = bool_val;
281 } else if (po->flags & OPT_INT) {
282 *(int*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
283 } else if (po->flags & OPT_INT64) {
284 *(int64_t*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
285 } else if (po->flags & OPT_TIME) {
286 *(int64_t*)dst = parse_time_or_die(opt, arg, 1);
287 } else if (po->flags & OPT_FLOAT) {
288 *(float*)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
289 } else if (po->flags & OPT_DOUBLE) {
290 *(double*)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
291 } else if (po->u.func_arg) {
292 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) :
293 po->u.func_arg(opt, arg);
295 av_log(NULL, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", arg, opt);
299 if (po->flags & OPT_EXIT)
301 return !!(po->flags & HAS_ARG);
304 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
305 void (* parse_arg_function)(void *, const char*))
308 int optindex, handleoptions = 1, ret;
310 /* perform system-dependent conversions for arguments list */
311 prepare_app_arguments(&argc, &argv);
315 while (optindex < argc) {
316 opt = argv[optindex++];
318 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
319 if (opt[1] == '-' && opt[2] == '\0') {
325 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
329 if (parse_arg_function)
330 parse_arg_function(optctx, opt);
336 * Return index of option opt in argv or 0 if not found.
338 static int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
343 for (i = 1; i < argc; i++) {
344 const char *cur_opt = argv[i];
346 if (*cur_opt++ != '-')
349 po = find_option(options, cur_opt);
350 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
351 po = find_option(options, cur_opt + 2);
353 if ((!po->name && !strcmp(cur_opt, optname)) ||
354 (po->name && !strcmp(optname, po->name)))
357 if (!po || po->flags & HAS_ARG)
363 static void dump_argument(const char *a)
365 const unsigned char *p;
368 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
369 *p == '_' || (*p >= 'a' && *p <= 'z')))
372 fputs(a, report_file);
375 fputc('"', report_file);
376 for (p = a; *p; p++) {
377 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
378 fprintf(report_file, "\\%c", *p);
379 else if (*p < ' ' || *p > '~')
380 fprintf(report_file, "\\x%02x", *p);
382 fputc(*p, report_file);
384 fputc('"', report_file);
387 void parse_loglevel(int argc, char **argv, const OptionDef *options)
389 int idx = locate_option(argc, argv, options, "loglevel");
391 idx = locate_option(argc, argv, options, "v");
392 if (idx && argv[idx + 1])
393 opt_loglevel("loglevel", argv[idx + 1]);
394 idx = locate_option(argc, argv, options, "report");
395 if (idx || getenv("FFREPORT")) {
396 opt_report("report");
399 fprintf(report_file, "Command line:\n");
400 for (i = 0; i < argc; i++) {
401 dump_argument(argv[i]);
402 fputc(i < argc - 1 ? ' ' : '\n', report_file);
409 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
410 int opt_default(const char *opt, const char *arg)
412 const AVOption *oc, *of, *os;
413 char opt_stripped[128];
415 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
417 if (!(p = strchr(opt, ':')))
418 p = opt + strlen(opt);
419 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
421 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
422 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
423 (oc = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
424 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
425 if ((of = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
426 av_dict_set(&format_opts, opt, arg, FLAGS(of));
428 sc = sws_get_class();
429 if ((os = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
430 // XXX we only support sws_flags, not arbitrary sws options
431 int ret = av_opt_set(sws_opts, opt, arg, 0);
433 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
441 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
442 return AVERROR_OPTION_NOT_FOUND;
445 int opt_loglevel(const char *opt, const char *arg)
447 const struct { const char *name; int level; } log_levels[] = {
448 { "quiet" , AV_LOG_QUIET },
449 { "panic" , AV_LOG_PANIC },
450 { "fatal" , AV_LOG_FATAL },
451 { "error" , AV_LOG_ERROR },
452 { "warning", AV_LOG_WARNING },
453 { "info" , AV_LOG_INFO },
454 { "verbose", AV_LOG_VERBOSE },
455 { "debug" , AV_LOG_DEBUG },
461 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
462 if (!strcmp(log_levels[i].name, arg)) {
463 av_log_set_level(log_levels[i].level);
468 level = strtol(arg, &tail, 10);
470 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
471 "Possible levels are numbers or:\n", arg);
472 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
473 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
476 av_log_set_level(level);
480 int opt_report(const char *opt)
486 if (report_file) /* already opened */
489 tm = localtime(&now);
490 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
492 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
493 tm->tm_hour, tm->tm_min, tm->tm_sec);
494 report_file = fopen(filename, "w");
496 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
497 filename, strerror(errno));
498 return AVERROR(errno);
500 av_log_set_callback(log_callback_report);
501 av_log(NULL, AV_LOG_INFO,
502 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
503 "Report written to \"%s\"\n",
505 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
506 tm->tm_hour, tm->tm_min, tm->tm_sec,
508 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
512 int opt_codec_debug(const char *opt, const char *arg)
514 av_log_set_level(AV_LOG_DEBUG);
515 return opt_default(opt, arg);
518 int opt_timelimit(const char *opt, const char *arg)
521 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
522 struct rlimit rl = { lim, lim + 1 };
523 if (setrlimit(RLIMIT_CPU, &rl))
526 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
531 void print_error(const char *filename, int err)
534 const char *errbuf_ptr = errbuf;
536 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
537 errbuf_ptr = strerror(AVUNERROR(err));
538 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
541 static int warned_cfg = 0;
544 #define SHOW_VERSION 2
545 #define SHOW_CONFIG 4
547 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
548 if (CONFIG_##LIBNAME) { \
549 const char *indent = flags & INDENT? " " : ""; \
550 if (flags & SHOW_VERSION) { \
551 unsigned int version = libname##_version(); \
552 av_log(NULL, level, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n",\
554 LIB##LIBNAME##_VERSION_MAJOR, \
555 LIB##LIBNAME##_VERSION_MINOR, \
556 LIB##LIBNAME##_VERSION_MICRO, \
557 version >> 16, version >> 8 & 0xff, version & 0xff); \
559 if (flags & SHOW_CONFIG) { \
560 const char *cfg = libname##_configuration(); \
561 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
563 av_log(NULL, level, \
564 "%sWARNING: library configuration mismatch\n", \
568 av_log(NULL, level, "%s%-11s configuration: %s\n", \
569 indent, #libname, cfg); \
574 static void print_all_libs_info(int flags, int level)
576 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
577 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
578 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
579 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
580 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
581 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
582 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
585 void show_banner(void)
587 av_log(NULL, AV_LOG_INFO, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
588 program_name, program_birth_year, this_year);
589 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s %s\n",
590 __DATE__, __TIME__, CC_TYPE, CC_VERSION);
591 av_log(NULL, AV_LOG_INFO, " configuration: " FFMPEG_CONFIGURATION "\n");
592 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
593 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
596 int opt_version(const char *opt, const char *arg) {
597 av_log_set_callback(log_callback_help);
598 printf("%s " FFMPEG_VERSION "\n", program_name);
599 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
603 int opt_license(const char *opt, const char *arg)
607 "This version of %s has nonfree parts compiled in.\n"
608 "Therefore it is not legally redistributable.\n",
611 "%s is free software; you can redistribute it and/or modify\n"
612 "it under the terms of the GNU General Public License as published by\n"
613 "the Free Software Foundation; either version 3 of the License, or\n"
614 "(at your option) any later version.\n"
616 "%s is distributed in the hope that it will be useful,\n"
617 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
618 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
619 "GNU General Public License for more details.\n"
621 "You should have received a copy of the GNU General Public License\n"
622 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
623 program_name, program_name, program_name
625 "%s is free software; you can redistribute it and/or modify\n"
626 "it under the terms of the GNU General Public License as published by\n"
627 "the Free Software Foundation; either version 2 of the License, or\n"
628 "(at your option) any later version.\n"
630 "%s is distributed in the hope that it will be useful,\n"
631 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
632 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
633 "GNU General Public License for more details.\n"
635 "You should have received a copy of the GNU General Public License\n"
636 "along with %s; if not, write to the Free Software\n"
637 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
638 program_name, program_name, program_name
640 "%s is free software; you can redistribute it and/or modify\n"
641 "it under the terms of the GNU Lesser General Public License as published by\n"
642 "the Free Software Foundation; either version 3 of the License, or\n"
643 "(at your option) any later version.\n"
645 "%s is distributed in the hope that it will be useful,\n"
646 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
647 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
648 "GNU Lesser General Public License for more details.\n"
650 "You should have received a copy of the GNU Lesser General Public License\n"
651 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
652 program_name, program_name, program_name
654 "%s is free software; you can redistribute it and/or\n"
655 "modify it under the terms of the GNU Lesser General Public\n"
656 "License as published by the Free Software Foundation; either\n"
657 "version 2.1 of the License, or (at your option) any later version.\n"
659 "%s is distributed in the hope that it will be useful,\n"
660 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
661 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
662 "Lesser General Public License for more details.\n"
664 "You should have received a copy of the GNU Lesser General Public\n"
665 "License along with %s; if not, write to the Free Software\n"
666 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
667 program_name, program_name, program_name
673 int opt_formats(const char *opt, const char *arg)
675 AVInputFormat *ifmt=NULL;
676 AVOutputFormat *ofmt=NULL;
677 const char *last_name;
681 " D. = Demuxing supported\n"
682 " .E = Muxing supported\n"
688 const char *name=NULL;
689 const char *long_name=NULL;
691 while((ofmt= av_oformat_next(ofmt))) {
692 if((name == NULL || strcmp(ofmt->name, name)<0) &&
693 strcmp(ofmt->name, last_name)>0){
695 long_name= ofmt->long_name;
699 while((ifmt= av_iformat_next(ifmt))) {
700 if((name == NULL || strcmp(ifmt->name, name)<0) &&
701 strcmp(ifmt->name, last_name)>0){
703 long_name= ifmt->long_name;
706 if(name && strcmp(ifmt->name, name)==0)
718 long_name ? long_name:" ");
723 int opt_codecs(const char *opt, const char *arg)
725 AVCodec *p=NULL, *p2;
726 const char *last_name;
729 " D..... = Decoding supported\n"
730 " .E.... = Encoding supported\n"
731 " ..V... = Video codec\n"
732 " ..A... = Audio codec\n"
733 " ..S... = Subtitle codec\n"
734 " ...S.. = Supports draw_horiz_band\n"
735 " ....D. = Supports direct rendering method 1\n"
736 " .....T = Supports weird frame truncation\n"
743 const char *type_str;
746 while((p= av_codec_next(p))) {
747 if((p2==NULL || strcmp(p->name, p2->name)<0) &&
748 strcmp(p->name, last_name)>0){
750 decode= encode= cap=0;
752 if(p2 && strcmp(p->name, p2->name)==0){
753 if(p->decode) decode=1;
754 if(p->encode) encode=1;
755 cap |= p->capabilities;
763 case AVMEDIA_TYPE_VIDEO:
766 case AVMEDIA_TYPE_AUDIO:
769 case AVMEDIA_TYPE_SUBTITLE:
777 " %s%s%s%s%s%s %-15s %s",
778 decode ? "D": (/*p2->decoder ? "d":*/" "),
781 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
782 cap & CODEC_CAP_DR1 ? "D":" ",
783 cap & CODEC_CAP_TRUNCATED ? "T":" ",
785 p2->long_name ? p2->long_name : "");
786 /* if(p2->decoder && decode==0)
787 printf(" use %s for decoding", p2->decoder->name);*/
792 "Note, the names of encoders and decoders do not always match, so there are\n"
793 "several cases where the above table shows encoder only or decoder only entries\n"
794 "even though both encoding and decoding are supported. For example, the h263\n"
795 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
800 int opt_bsfs(const char *opt, const char *arg)
802 AVBitStreamFilter *bsf=NULL;
804 printf("Bitstream filters:\n");
805 while((bsf = av_bitstream_filter_next(bsf)))
806 printf("%s\n", bsf->name);
811 int opt_protocols(const char *opt, const char *arg)
813 URLProtocol *up=NULL;
815 printf("Supported file protocols:\n"
816 "I.. = Input supported\n"
817 ".O. = Output supported\n"
818 "..S = Seek supported\n"
821 while((up = av_protocol_next(up)))
822 printf("%c%c%c %s\n",
823 up->url_read ? 'I' : '.',
824 up->url_write ? 'O' : '.',
825 up->url_seek ? 'S' : '.',
830 int opt_filters(const char *opt, const char *arg)
832 AVFilter av_unused(**filter) = NULL;
834 printf("Filters:\n");
836 while ((filter = av_filter_next(filter)) && *filter)
837 printf("%-16s %s\n", (*filter)->name, (*filter)->description);
842 int opt_pix_fmts(const char *opt, const char *arg)
844 enum PixelFormat pix_fmt;
848 "I.... = Supported Input format for conversion\n"
849 ".O... = Supported Output format for conversion\n"
850 "..H.. = Hardware accelerated format\n"
851 "...P. = Paletted format\n"
852 "....B = Bitstream format\n"
853 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
857 # define sws_isSupportedInput(x) 0
858 # define sws_isSupportedOutput(x) 0
861 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
862 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
865 printf("%c%c%c%c%c %-16s %d %2d\n",
866 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
867 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
868 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
869 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
870 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
872 pix_desc->nb_components,
873 av_get_bits_per_pixel(pix_desc));
878 int show_sample_fmts(const char *opt, const char *arg)
882 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
883 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
890 int yesno = (toupper(c) == 'Y');
892 while (c != '\n' && c != EOF)
898 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
901 FILE *f = fopen(filename, "rb");
904 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
905 return AVERROR(errno);
907 fseek(f, 0, SEEK_END);
909 fseek(f, 0, SEEK_SET);
910 *bufptr = av_malloc(*size + 1);
912 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
914 return AVERROR(ENOMEM);
916 ret = fread(*bufptr, 1, *size, f);
920 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
921 filename, strerror(errno));
922 ret = AVERROR(errno);
927 (*bufptr)[*size++] = '\0';
934 FILE *get_preset_file(char *filename, size_t filename_size,
935 const char *preset_name, int is_path, const char *codec_name)
939 const char *base[3]= { getenv("FFMPEG_DATADIR"),
945 av_strlcpy(filename, preset_name, filename_size);
946 f = fopen(filename, "r");
949 char datadir[MAX_PATH], *ls;
952 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
954 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
955 if (*ls == '\\') *ls = '/';
957 if (ls = strrchr(datadir, '/'))
960 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
965 for (i = 0; i < 3 && !f; i++) {
968 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
969 f = fopen(filename, "r");
970 if (!f && codec_name) {
971 snprintf(filename, filename_size,
972 "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
973 f = fopen(filename, "r");
981 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
983 if (*spec <= '9' && *spec >= '0') /* opt:index */
984 return strtol(spec, NULL, 0) == st->index;
985 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || *spec == 't') { /* opt:[vasdt] */
986 enum AVMediaType type;
989 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
990 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
991 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
992 case 'd': type = AVMEDIA_TYPE_DATA; break;
993 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
994 default: abort(); // never reached, silence warning
996 if (type != st->codec->codec_type)
998 if (*spec++ == ':') { /* possibly followed by :index */
999 int i, index = strtol(spec, NULL, 0);
1000 for (i = 0; i < s->nb_streams; i++)
1001 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1002 return i == st->index;
1006 } else if (*spec == 'p' && *(spec + 1) == ':') {
1010 prog_id = strtol(spec, &endptr, 0);
1011 for (i = 0; i < s->nb_programs; i++) {
1012 if (s->programs[i]->id != prog_id)
1015 if (*endptr++ == ':') {
1016 int stream_idx = strtol(endptr, NULL, 0);
1017 return (stream_idx >= 0 && stream_idx < s->programs[i]->nb_stream_indexes &&
1018 st->index == s->programs[i]->stream_index[stream_idx]);
1021 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1022 if (st->index == s->programs[i]->stream_index[j])
1026 } else if (!*spec) /* empty specifier, matches everything */
1029 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1030 return AVERROR(EINVAL);
1033 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec, AVFormatContext *s, AVStream *st)
1035 AVDictionary *ret = NULL;
1036 AVDictionaryEntry *t = NULL;
1037 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
1039 const AVClass *cc = avcodec_get_class();
1044 switch (codec->type) {
1045 case AVMEDIA_TYPE_VIDEO: prefix = 'v'; flags |= AV_OPT_FLAG_VIDEO_PARAM; break;
1046 case AVMEDIA_TYPE_AUDIO: prefix = 'a'; flags |= AV_OPT_FLAG_AUDIO_PARAM; break;
1047 case AVMEDIA_TYPE_SUBTITLE: prefix = 's'; flags |= AV_OPT_FLAG_SUBTITLE_PARAM; break;
1050 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1051 char *p = strchr(t->key, ':');
1053 /* check stream specification in opt name */
1055 switch (check_stream_specifier(s, st, p + 1)) {
1056 case 1: *p = 0; break;
1058 default: return NULL;
1061 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1062 (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ)))
1063 av_dict_set(&ret, t->key, t->value, 0);
1064 else if (t->key[0] == prefix && av_opt_find(&cc, t->key+1, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ))
1065 av_dict_set(&ret, t->key+1, t->value, 0);
1073 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
1076 AVDictionary **opts;
1080 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1082 av_log(NULL, AV_LOG_ERROR, "Could not alloc memory for stream options.\n");
1085 for (i = 0; i < s->nb_streams; i++)
1086 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id), s, s->streams[i]);
1090 void *grow_array(void *array, int elem_size, int *size, int new_size)
1092 if (new_size >= INT_MAX / elem_size) {
1093 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1096 if (*size < new_size) {
1097 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1099 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1102 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);