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 "libswresample/swresample.h"
37 #include "libpostproc/postprocess.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/eval.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/opt.h"
48 #include "libavformat/network.h"
50 #if HAVE_SYS_RESOURCE_H
51 #include <sys/resource.h>
54 struct SwsContext *sws_opts;
55 AVDictionary *format_opts, *codec_opts;
57 static const int this_year = 2012;
59 static FILE *report_file;
64 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
69 void uninit_opts(void)
72 sws_freeContext(sws_opts);
75 av_dict_free(&format_opts);
76 av_dict_free(&codec_opts);
79 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
81 vfprintf(stdout, fmt, vl);
84 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
88 static int print_prefix = 1;
91 av_log_default_callback(ptr, level, fmt, vl);
92 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
94 fputs(line, report_file);
98 double parse_number_or_die(const char *context, const char *numstr, int type,
99 double min, double max)
103 double d = av_strtod(numstr, &tail);
105 error = "Expected number for %s but found: %s\n";
106 else if (d < min || d > max)
107 error = "The value for %s was %s which is not within %f - %f\n";
108 else if (type == OPT_INT64 && (int64_t)d != d)
109 error = "Expected int64 for %s but found %s\n";
110 else if (type == OPT_INT && (int)d != d)
111 error = "Expected int for %s but found %s\n";
114 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
119 int64_t parse_time_or_die(const char *context, const char *timestr,
123 if (av_parse_time(&us, timestr, is_duration) < 0) {
124 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
125 is_duration ? "duration" : "date", context, timestr);
131 void show_help_options(const OptionDef *options, const char *msg, int mask,
138 for (po = options; po->name != NULL; po++) {
140 if ((po->flags & mask) == value) {
145 av_strlcpy(buf, po->name, sizeof(buf));
146 if (po->flags & HAS_ARG) {
147 av_strlcat(buf, " ", sizeof(buf));
148 av_strlcat(buf, po->argname, sizeof(buf));
150 printf("-%-17s %s\n", buf, po->help);
155 void show_help_children(const AVClass *class, int flags)
157 const AVClass *child = NULL;
158 av_opt_show2(&class, NULL, flags, 0);
161 while (child = av_opt_child_class_next(class, child))
162 show_help_children(child, flags);
165 static const OptionDef *find_option(const OptionDef *po, const char *name)
167 const char *p = strchr(name, ':');
168 int len = p ? p - name : strlen(name);
170 while (po->name != NULL) {
171 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
178 #if defined(_WIN32) && !defined(__MINGW32CE__)
180 /* Will be leaked on exit */
181 static char** win32_argv_utf8 = NULL;
182 static int win32_argc = 0;
185 * Prepare command line arguments for executable.
186 * For Windows - perform wide-char to UTF-8 conversion.
187 * Input arguments should be main() function arguments.
188 * @param argc_ptr Arguments number (including executable)
189 * @param argv_ptr Arguments list.
191 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
195 int i, buffsize = 0, offset = 0;
197 if (win32_argv_utf8) {
198 *argc_ptr = win32_argc;
199 *argv_ptr = win32_argv_utf8;
204 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
205 if (win32_argc <= 0 || !argv_w)
208 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
209 for (i = 0; i < win32_argc; i++)
210 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
211 NULL, 0, NULL, NULL);
213 win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
214 argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
215 if (win32_argv_utf8 == NULL) {
220 for (i = 0; i < win32_argc; i++) {
221 win32_argv_utf8[i] = &argstr_flat[offset];
222 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
223 &argstr_flat[offset],
224 buffsize - offset, NULL, NULL);
226 win32_argv_utf8[i] = NULL;
229 *argc_ptr = win32_argc;
230 *argv_ptr = win32_argv_utf8;
233 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
237 #endif /* WIN32 && !__MINGW32CE__ */
239 int parse_option(void *optctx, const char *opt, const char *arg,
240 const OptionDef *options)
247 po = find_option(options, opt);
248 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
249 /* handle 'no' bool option */
250 po = find_option(options, opt + 2);
251 if (!(po->name && (po->flags & OPT_BOOL)))
256 po = find_option(options, "default");
259 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
260 return AVERROR(EINVAL);
262 if (po->flags & HAS_ARG && !arg) {
263 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
264 return AVERROR(EINVAL);
267 /* new-style options contain an offset into optctx, old-style address of
269 dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
272 if (po->flags & OPT_SPEC) {
273 SpecifierOpt **so = dst;
274 char *p = strchr(opt, ':');
276 dstcount = (int *)(so + 1);
277 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
278 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
279 dst = &(*so)[*dstcount - 1].u;
282 if (po->flags & OPT_STRING) {
284 str = av_strdup(arg);
286 } else if (po->flags & OPT_BOOL) {
287 *(int *)dst = bool_val;
288 } else if (po->flags & OPT_INT) {
289 *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
290 } else if (po->flags & OPT_INT64) {
291 *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
292 } else if (po->flags & OPT_TIME) {
293 *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
294 } else if (po->flags & OPT_FLOAT) {
295 *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
296 } else if (po->flags & OPT_DOUBLE) {
297 *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
298 } else if (po->u.func_arg) {
299 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
300 : po->u.func_arg(opt, arg);
302 av_log(NULL, AV_LOG_ERROR,
303 "Failed to set value '%s' for option '%s'\n", arg, opt);
307 if (po->flags & OPT_EXIT)
309 return !!(po->flags & HAS_ARG);
312 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
313 void (*parse_arg_function)(void *, const char*))
316 int optindex, handleoptions = 1, ret;
318 /* perform system-dependent conversions for arguments list */
319 prepare_app_arguments(&argc, &argv);
323 while (optindex < argc) {
324 opt = argv[optindex++];
326 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
327 if (opt[1] == '-' && opt[2] == '\0') {
333 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
337 if (parse_arg_function)
338 parse_arg_function(optctx, opt);
344 * Return index of option opt in argv or 0 if not found.
346 static int locate_option(int argc, char **argv, const OptionDef *options,
352 for (i = 1; i < argc; i++) {
353 const char *cur_opt = argv[i];
355 if (*cur_opt++ != '-')
358 po = find_option(options, cur_opt);
359 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
360 po = find_option(options, cur_opt + 2);
362 if ((!po->name && !strcmp(cur_opt, optname)) ||
363 (po->name && !strcmp(optname, po->name)))
366 if (!po || po->flags & HAS_ARG)
372 static void dump_argument(const char *a)
374 const unsigned char *p;
377 if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
378 *p == '_' || (*p >= 'a' && *p <= 'z')))
381 fputs(a, report_file);
384 fputc('"', report_file);
385 for (p = a; *p; p++) {
386 if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
387 fprintf(report_file, "\\%c", *p);
388 else if (*p < ' ' || *p > '~')
389 fprintf(report_file, "\\x%02x", *p);
391 fputc(*p, report_file);
393 fputc('"', report_file);
396 void parse_loglevel(int argc, char **argv, const OptionDef *options)
398 int idx = locate_option(argc, argv, options, "loglevel");
400 idx = locate_option(argc, argv, options, "v");
401 if (idx && argv[idx + 1])
402 opt_loglevel("loglevel", argv[idx + 1]);
403 idx = locate_option(argc, argv, options, "report");
404 if (idx || getenv("FFREPORT")) {
405 opt_report("report");
408 fprintf(report_file, "Command line:\n");
409 for (i = 0; i < argc; i++) {
410 dump_argument(argv[i]);
411 fputc(i < argc - 1 ? ' ' : '\n', report_file);
418 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
419 int opt_default(const char *opt, const char *arg)
421 const AVOption *oc, *of, *os;
422 char opt_stripped[128];
424 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
426 if (!(p = strchr(opt, ':')))
427 p = opt + strlen(opt);
428 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
430 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0,
431 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
432 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
433 (oc = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
434 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
435 if ((of = av_opt_find(&fc, opt, NULL, 0,
436 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
437 av_dict_set(&format_opts, opt, arg, FLAGS(of));
439 sc = sws_get_class();
440 if ((os = av_opt_find(&sc, opt, NULL, 0,
441 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
442 // XXX we only support sws_flags, not arbitrary sws options
443 int ret = av_opt_set(sws_opts, opt, arg, 0);
445 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
453 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
454 return AVERROR_OPTION_NOT_FOUND;
457 int opt_loglevel(const char *opt, const char *arg)
459 const struct { const char *name; int level; } log_levels[] = {
460 { "quiet" , AV_LOG_QUIET },
461 { "panic" , AV_LOG_PANIC },
462 { "fatal" , AV_LOG_FATAL },
463 { "error" , AV_LOG_ERROR },
464 { "warning", AV_LOG_WARNING },
465 { "info" , AV_LOG_INFO },
466 { "verbose", AV_LOG_VERBOSE },
467 { "debug" , AV_LOG_DEBUG },
473 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
474 if (!strcmp(log_levels[i].name, arg)) {
475 av_log_set_level(log_levels[i].level);
480 level = strtol(arg, &tail, 10);
482 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
483 "Possible levels are numbers or:\n", arg);
484 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
485 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
488 av_log_set_level(level);
492 int opt_report(const char *opt)
498 if (report_file) /* already opened */
501 tm = localtime(&now);
502 snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
504 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
505 tm->tm_hour, tm->tm_min, tm->tm_sec);
506 report_file = fopen(filename, "w");
508 av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
509 filename, strerror(errno));
510 return AVERROR(errno);
512 av_log_set_callback(log_callback_report);
513 av_log(NULL, AV_LOG_INFO,
514 "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
515 "Report written to \"%s\"\n",
517 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
518 tm->tm_hour, tm->tm_min, tm->tm_sec,
520 av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
524 int opt_max_alloc(const char *opt, const char *arg)
529 max = strtol(arg, &tail, 10);
531 av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
538 int opt_codec_debug(const char *opt, const char *arg)
540 av_log_set_level(AV_LOG_DEBUG);
541 return opt_default(opt, arg);
544 int opt_timelimit(const char *opt, const char *arg)
547 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
548 struct rlimit rl = { lim, lim + 1 };
549 if (setrlimit(RLIMIT_CPU, &rl))
552 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
557 void print_error(const char *filename, int err)
560 const char *errbuf_ptr = errbuf;
562 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
563 errbuf_ptr = strerror(AVUNERROR(err));
564 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
567 static int warned_cfg = 0;
570 #define SHOW_VERSION 2
571 #define SHOW_CONFIG 4
573 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
574 if (CONFIG_##LIBNAME) { \
575 const char *indent = flags & INDENT? " " : ""; \
576 if (flags & SHOW_VERSION) { \
577 unsigned int version = libname##_version(); \
578 av_log(NULL, level, "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",\
580 LIB##LIBNAME##_VERSION_MAJOR, \
581 LIB##LIBNAME##_VERSION_MINOR, \
582 LIB##LIBNAME##_VERSION_MICRO, \
583 version >> 16, version >> 8 & 0xff, version & 0xff); \
585 if (flags & SHOW_CONFIG) { \
586 const char *cfg = libname##_configuration(); \
587 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
589 av_log(NULL, level, \
590 "%sWARNING: library configuration mismatch\n", \
594 av_log(NULL, level, "%s%-11s configuration: %s\n", \
595 indent, #libname, cfg); \
600 static void print_all_libs_info(int flags, int level)
602 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
603 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
604 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
605 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
606 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
607 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
608 PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
609 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
612 void show_banner(int argc, char **argv, const OptionDef *options)
614 int idx = locate_option(argc, argv, options, "version");
618 av_log(NULL, AV_LOG_INFO,
619 "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
620 program_name, program_birth_year, this_year);
621 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s %s\n",
622 __DATE__, __TIME__, CC_TYPE, CC_VERSION);
623 av_log(NULL, AV_LOG_INFO, " configuration: " FFMPEG_CONFIGURATION "\n");
624 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
625 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
628 int opt_version(const char *opt, const char *arg) {
629 av_log_set_callback(log_callback_help);
630 printf("%s " FFMPEG_VERSION "\n", program_name);
631 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
635 int opt_license(const char *opt, const char *arg)
639 "This version of %s has nonfree parts compiled in.\n"
640 "Therefore it is not legally redistributable.\n",
643 "%s is free software; you can redistribute it and/or modify\n"
644 "it under the terms of the GNU General Public License as published by\n"
645 "the Free Software Foundation; either version 3 of the License, or\n"
646 "(at your option) any later version.\n"
648 "%s is distributed in the hope that it will be useful,\n"
649 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
650 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
651 "GNU General Public License for more details.\n"
653 "You should have received a copy of the GNU General Public License\n"
654 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
655 program_name, program_name, program_name
657 "%s is free software; you can redistribute it and/or modify\n"
658 "it under the terms of the GNU General Public License as published by\n"
659 "the Free Software Foundation; either version 2 of the License, or\n"
660 "(at your option) any later version.\n"
662 "%s is distributed in the hope that it will be useful,\n"
663 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
664 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
665 "GNU General Public License for more details.\n"
667 "You should have received a copy of the GNU General Public License\n"
668 "along with %s; if not, write to the Free Software\n"
669 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
670 program_name, program_name, program_name
672 "%s is free software; you can redistribute it and/or modify\n"
673 "it under the terms of the GNU Lesser General Public License as published by\n"
674 "the Free Software Foundation; either version 3 of the License, or\n"
675 "(at your option) any later version.\n"
677 "%s is distributed in the hope that it will be useful,\n"
678 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
679 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
680 "GNU Lesser General Public License for more details.\n"
682 "You should have received a copy of the GNU Lesser General Public License\n"
683 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
684 program_name, program_name, program_name
686 "%s is free software; you can redistribute it and/or\n"
687 "modify it under the terms of the GNU Lesser General Public\n"
688 "License as published by the Free Software Foundation; either\n"
689 "version 2.1 of the License, or (at your option) any later version.\n"
691 "%s is distributed in the hope that it will be useful,\n"
692 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
693 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
694 "Lesser General Public License for more details.\n"
696 "You should have received a copy of the GNU Lesser General Public\n"
697 "License along with %s; if not, write to the Free Software\n"
698 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
699 program_name, program_name, program_name
705 int opt_formats(const char *opt, const char *arg)
707 AVInputFormat *ifmt = NULL;
708 AVOutputFormat *ofmt = NULL;
709 const char *last_name;
711 printf("File formats:\n"
712 " D. = Demuxing supported\n"
713 " .E = Muxing supported\n"
719 const char *name = NULL;
720 const char *long_name = NULL;
722 while ((ofmt = av_oformat_next(ofmt))) {
723 if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
724 strcmp(ofmt->name, last_name) > 0) {
726 long_name = ofmt->long_name;
730 while ((ifmt = av_iformat_next(ifmt))) {
731 if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
732 strcmp(ifmt->name, last_name) > 0) {
734 long_name = ifmt->long_name;
737 if (name && strcmp(ifmt->name, name) == 0)
744 printf(" %s%s %-15s %s\n",
748 long_name ? long_name:" ");
753 int opt_codecs(const char *opt, const char *arg)
755 AVCodec *p = NULL, *p2;
756 const char *last_name;
758 " D..... = Decoding supported\n"
759 " .E.... = Encoding supported\n"
760 " ..V... = Video codec\n"
761 " ..A... = Audio codec\n"
762 " ..S... = Subtitle codec\n"
763 " ...S.. = Supports draw_horiz_band\n"
764 " ....D. = Supports direct rendering method 1\n"
765 " .....T = Supports weird frame truncation\n"
772 const char *type_str;
775 while ((p = av_codec_next(p))) {
776 if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
777 strcmp(p->name, last_name) > 0) {
779 decode = encode = cap = 0;
781 if (p2 && strcmp(p->name, p2->name) == 0) {
786 cap |= p->capabilities;
791 last_name = p2->name;
794 case AVMEDIA_TYPE_VIDEO:
797 case AVMEDIA_TYPE_AUDIO:
800 case AVMEDIA_TYPE_SUBTITLE:
807 printf(" %s%s%s%s%s%s %-15s %s",
808 decode ? "D" : (/* p2->decoder ? "d" : */ " "),
811 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
812 cap & CODEC_CAP_DR1 ? "D" : " ",
813 cap & CODEC_CAP_TRUNCATED ? "T" : " ",
815 p2->long_name ? p2->long_name : "");
817 if (p2->decoder && decode == 0)
818 printf(" use %s for decoding", p2->decoder->name);
823 printf("Note, the names of encoders and decoders do not always match, so there are\n"
824 "several cases where the above table shows encoder only or decoder only entries\n"
825 "even though both encoding and decoding are supported. For example, the h263\n"
826 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
831 int opt_bsfs(const char *opt, const char *arg)
833 AVBitStreamFilter *bsf = NULL;
835 printf("Bitstream filters:\n");
836 while ((bsf = av_bitstream_filter_next(bsf)))
837 printf("%s\n", bsf->name);
842 int opt_protocols(const char *opt, const char *arg)
844 URLProtocol *up=NULL;
846 printf("Supported file protocols:\n"
847 "I.. = Input supported\n"
848 ".O. = Output supported\n"
849 "..S = Seek supported\n"
852 while((up = av_protocol_next(up)))
853 printf("%c%c%c %s\n",
854 up->url_read ? 'I' : '.',
855 up->url_write ? 'O' : '.',
856 up->url_seek ? 'S' : '.',
861 int opt_filters(const char *opt, const char *arg)
863 AVFilter av_unused(**filter) = NULL;
865 printf("Filters:\n");
867 while ((filter = av_filter_next(filter)) && *filter)
868 printf("%-16s %s\n", (*filter)->name, (*filter)->description);
873 int opt_pix_fmts(const char *opt, const char *arg)
875 enum PixelFormat pix_fmt;
877 printf("Pixel formats:\n"
878 "I.... = Supported Input format for conversion\n"
879 ".O... = Supported Output format for conversion\n"
880 "..H.. = Hardware accelerated format\n"
881 "...P. = Paletted format\n"
882 "....B = Bitstream format\n"
883 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
887 # define sws_isSupportedInput(x) 0
888 # define sws_isSupportedOutput(x) 0
891 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
892 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
895 printf("%c%c%c%c%c %-16s %d %2d\n",
896 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
897 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
898 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
899 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
900 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
902 pix_desc->nb_components,
903 av_get_bits_per_pixel(pix_desc));
908 int show_sample_fmts(const char *opt, const char *arg)
912 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
913 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
920 int yesno = (toupper(c) == 'Y');
922 while (c != '\n' && c != EOF)
928 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
931 FILE *f = fopen(filename, "rb");
934 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
936 return AVERROR(errno);
938 fseek(f, 0, SEEK_END);
940 fseek(f, 0, SEEK_SET);
941 *bufptr = av_malloc(*size + 1);
943 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
945 return AVERROR(ENOMEM);
947 ret = fread(*bufptr, 1, *size, f);
951 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
952 filename, strerror(errno));
953 ret = AVERROR(errno);
958 (*bufptr)[*size++] = '\0';
965 FILE *get_preset_file(char *filename, size_t filename_size,
966 const char *preset_name, int is_path,
967 const char *codec_name)
971 const char *base[3] = { getenv("FFMPEG_DATADIR"),
976 av_strlcpy(filename, preset_name, filename_size);
977 f = fopen(filename, "r");
980 char datadir[MAX_PATH], *ls;
983 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
985 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
986 if (*ls == '\\') *ls = '/';
988 if (ls = strrchr(datadir, '/'))
991 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
996 for (i = 0; i < 3 && !f; i++) {
999 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1000 i != 1 ? "" : "/.ffmpeg", preset_name);
1001 f = fopen(filename, "r");
1002 if (!f && codec_name) {
1003 snprintf(filename, filename_size,
1004 "%s%s/%s-%s.ffpreset",
1005 base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1007 f = fopen(filename, "r");
1015 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1017 if (*spec <= '9' && *spec >= '0') /* opt:index */
1018 return strtol(spec, NULL, 0) == st->index;
1019 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1020 *spec == 't') { /* opt:[vasdt] */
1021 enum AVMediaType type;
1024 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1025 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1026 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1027 case 'd': type = AVMEDIA_TYPE_DATA; break;
1028 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1029 default: abort(); // never reached, silence warning
1031 if (type != st->codec->codec_type)
1033 if (*spec++ == ':') { /* possibly followed by :index */
1034 int i, index = strtol(spec, NULL, 0);
1035 for (i = 0; i < s->nb_streams; i++)
1036 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1037 return i == st->index;
1041 } else if (*spec == 'p' && *(spec + 1) == ':') {
1045 prog_id = strtol(spec, &endptr, 0);
1046 for (i = 0; i < s->nb_programs; i++) {
1047 if (s->programs[i]->id != prog_id)
1050 if (*endptr++ == ':') {
1051 int stream_idx = strtol(endptr, NULL, 0);
1052 return stream_idx >= 0 &&
1053 stream_idx < s->programs[i]->nb_stream_indexes &&
1054 st->index == s->programs[i]->stream_index[stream_idx];
1057 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1058 if (st->index == s->programs[i]->stream_index[j])
1062 } else if (!*spec) /* empty specifier, matches everything */
1065 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1066 return AVERROR(EINVAL);
1069 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
1070 AVFormatContext *s, AVStream *st)
1072 AVDictionary *ret = NULL;
1073 AVDictionaryEntry *t = NULL;
1074 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1075 : AV_OPT_FLAG_DECODING_PARAM;
1077 const AVClass *cc = avcodec_get_class();
1082 switch (codec->type) {
1083 case AVMEDIA_TYPE_VIDEO:
1085 flags |= AV_OPT_FLAG_VIDEO_PARAM;
1087 case AVMEDIA_TYPE_AUDIO:
1089 flags |= AV_OPT_FLAG_AUDIO_PARAM;
1091 case AVMEDIA_TYPE_SUBTITLE:
1093 flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1097 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1098 char *p = strchr(t->key, ':');
1100 /* check stream specification in opt name */
1102 switch (check_stream_specifier(s, st, p + 1)) {
1103 case 1: *p = 0; break;
1105 default: return NULL;
1108 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1109 (codec && codec->priv_class &&
1110 av_opt_find(&codec->priv_class, t->key, NULL, flags,
1111 AV_OPT_SEARCH_FAKE_OBJ)))
1112 av_dict_set(&ret, t->key, t->value, 0);
1113 else if (t->key[0] == prefix &&
1114 av_opt_find(&cc, t->key + 1, NULL, flags,
1115 AV_OPT_SEARCH_FAKE_OBJ))
1116 av_dict_set(&ret, t->key + 1, t->value, 0);
1124 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1125 AVDictionary *codec_opts)
1128 AVDictionary **opts;
1132 opts = av_mallocz(s->nb_streams * sizeof(*opts));
1134 av_log(NULL, AV_LOG_ERROR,
1135 "Could not alloc memory for stream options.\n");
1138 for (i = 0; i < s->nb_streams; i++)
1139 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
1144 void *grow_array(void *array, int elem_size, int *size, int new_size)
1146 if (new_size >= INT_MAX / elem_size) {
1147 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1150 if (*size < new_size) {
1151 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1153 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1156 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);