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;
61 sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
65 void uninit_opts(void)
68 sws_freeContext(sws_opts);
71 av_dict_free(&format_opts);
72 av_dict_free(&codec_opts);
75 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
77 vfprintf(stdout, fmt, vl);
80 double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
84 double d = av_strtod(numstr, &tail);
86 error= "Expected number for %s but found: %s\n";
87 else if (d < min || d > max)
88 error= "The value for %s was %s which is not within %f - %f\n";
89 else if(type == OPT_INT64 && (int64_t)d != d)
90 error= "Expected int64 for %s but found %s\n";
91 else if (type == OPT_INT && (int)d != d)
92 error= "Expected int for %s but found %s\n";
95 av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
100 int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
103 if (av_parse_time(&us, timestr, is_duration) < 0) {
104 av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
105 is_duration ? "duration" : "date", context, timestr);
111 void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
117 for(po = options; po->name != NULL; po++) {
119 if ((po->flags & mask) == value) {
124 av_strlcpy(buf, po->name, sizeof(buf));
125 if (po->flags & HAS_ARG) {
126 av_strlcat(buf, " ", sizeof(buf));
127 av_strlcat(buf, po->argname, sizeof(buf));
129 printf("-%-17s %s\n", buf, po->help);
134 void show_help_children(const AVClass *class, int flags)
136 const AVClass *child = NULL;
137 av_opt_show2(&class, NULL, flags, 0);
140 while (child = av_opt_child_class_next(class, child))
141 show_help_children(child, flags);
144 static const OptionDef* find_option(const OptionDef *po, const char *name){
145 const char *p = strchr(name, ':');
146 int len = p ? p - name : strlen(name);
148 while (po->name != NULL) {
149 if (!strncmp(name, po->name, len) && strlen(po->name) == len)
156 #if defined(_WIN32) && !defined(__MINGW32CE__)
158 /* Will be leaked on exit */
159 static char** win32_argv_utf8 = NULL;
160 static int win32_argc = 0;
163 * Prepare command line arguments for executable.
164 * For Windows - perform wide-char to UTF-8 conversion.
165 * Input arguments should be main() function arguments.
166 * @param argc_ptr Arguments number (including executable)
167 * @param argv_ptr Arguments list.
169 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
173 int i, buffsize = 0, offset = 0;
175 if (win32_argv_utf8) {
176 *argc_ptr = win32_argc;
177 *argv_ptr = win32_argv_utf8;
182 argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
183 if (win32_argc <= 0 || !argv_w)
186 /* determine the UTF-8 buffer size (including NULL-termination symbols) */
187 for (i = 0; i < win32_argc; i++)
188 buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
189 NULL, 0, NULL, NULL);
191 win32_argv_utf8 = av_mallocz(sizeof(char*) * (win32_argc + 1) + buffsize);
192 argstr_flat = (char*)win32_argv_utf8 + sizeof(char*) * (win32_argc + 1);
193 if (win32_argv_utf8 == NULL) {
198 for (i = 0; i < win32_argc; i++) {
199 win32_argv_utf8[i] = &argstr_flat[offset];
200 offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
201 &argstr_flat[offset],
202 buffsize - offset, NULL, NULL);
204 win32_argv_utf8[i] = NULL;
207 *argc_ptr = win32_argc;
208 *argv_ptr = win32_argv_utf8;
211 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
215 #endif /* WIN32 && !__MINGW32CE__ */
218 int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
225 po = find_option(options, opt);
226 if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
227 /* handle 'no' bool option */
228 po = find_option(options, opt + 2);
229 if (!(po->name && (po->flags & OPT_BOOL)))
234 po = find_option(options, "default");
237 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
238 return AVERROR(EINVAL);
240 if (po->flags & HAS_ARG && !arg) {
241 av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
242 return AVERROR(EINVAL);
245 /* new-style options contain an offset into optctx, old-style address of
247 dst = po->flags & (OPT_OFFSET|OPT_SPEC) ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr;
249 if (po->flags & OPT_SPEC) {
250 SpecifierOpt **so = dst;
251 char *p = strchr(opt, ':');
253 dstcount = (int*)(so + 1);
254 *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
255 (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
256 dst = &(*so)[*dstcount - 1].u;
259 if (po->flags & OPT_STRING) {
261 str = av_strdup(arg);
263 } else if (po->flags & OPT_BOOL) {
264 *(int*)dst = bool_val;
265 } else if (po->flags & OPT_INT) {
266 *(int*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
267 } else if (po->flags & OPT_INT64) {
268 *(int64_t*)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
269 } else if (po->flags & OPT_TIME) {
270 *(int64_t*)dst = parse_time_or_die(opt, arg, 1);
271 } else if (po->flags & OPT_FLOAT) {
272 *(float*)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
273 } else if (po->flags & OPT_DOUBLE) {
274 *(double*)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
275 } else if (po->u.func_arg) {
276 int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) :
277 po->u.func_arg(opt, arg);
279 av_log(NULL, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", arg, opt);
283 if (po->flags & OPT_EXIT)
285 return !!(po->flags & HAS_ARG);
288 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
289 void (* parse_arg_function)(void *, const char*))
292 int optindex, handleoptions = 1, ret;
294 /* perform system-dependent conversions for arguments list */
295 prepare_app_arguments(&argc, &argv);
299 while (optindex < argc) {
300 opt = argv[optindex++];
302 if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
303 if (opt[1] == '-' && opt[2] == '\0') {
309 if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
313 if (parse_arg_function)
314 parse_arg_function(optctx, opt);
320 * Return index of option opt in argv or 0 if not found.
322 static int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
327 for (i = 1; i < argc; i++) {
328 const char *cur_opt = argv[i];
330 if (*cur_opt++ != '-')
333 po = find_option(options, cur_opt);
334 if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
335 po = find_option(options, cur_opt + 2);
337 if ((!po->name && !strcmp(cur_opt, optname)) ||
338 (po->name && !strcmp(optname, po->name)))
341 if (!po || po->flags & HAS_ARG)
347 void parse_loglevel(int argc, char **argv, const OptionDef *options)
349 int idx = locate_option(argc, argv, options, "loglevel");
351 idx = locate_option(argc, argv, options, "v");
352 if (idx && argv[idx + 1])
353 opt_loglevel("loglevel", argv[idx + 1]);
356 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
357 int opt_default(const char *opt, const char *arg)
359 const AVOption *oc, *of, *os;
360 char opt_stripped[128];
362 const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc;
364 if (!(p = strchr(opt, ':')))
365 p = opt + strlen(opt);
366 av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
368 if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
369 ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
370 (oc = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
371 av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
372 if ((of = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
373 av_dict_set(&format_opts, opt, arg, FLAGS(of));
375 sc = sws_get_class();
376 if ((os = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
377 // XXX we only support sws_flags, not arbitrary sws options
378 int ret = av_opt_set(sws_opts, opt, arg, 0);
380 av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
388 av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
389 return AVERROR_OPTION_NOT_FOUND;
392 int opt_loglevel(const char *opt, const char *arg)
394 const struct { const char *name; int level; } log_levels[] = {
395 { "quiet" , AV_LOG_QUIET },
396 { "panic" , AV_LOG_PANIC },
397 { "fatal" , AV_LOG_FATAL },
398 { "error" , AV_LOG_ERROR },
399 { "warning", AV_LOG_WARNING },
400 { "info" , AV_LOG_INFO },
401 { "verbose", AV_LOG_VERBOSE },
402 { "debug" , AV_LOG_DEBUG },
408 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
409 if (!strcmp(log_levels[i].name, arg)) {
410 av_log_set_level(log_levels[i].level);
415 level = strtol(arg, &tail, 10);
417 av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
418 "Possible levels are numbers or:\n", arg);
419 for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
420 av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
423 av_log_set_level(level);
427 int opt_codec_debug(const char *opt, const char *arg)
429 av_log_set_level(AV_LOG_DEBUG);
430 return opt_default(opt, arg);
433 int opt_timelimit(const char *opt, const char *arg)
436 int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
437 struct rlimit rl = { lim, lim + 1 };
438 if (setrlimit(RLIMIT_CPU, &rl))
441 av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
446 void print_error(const char *filename, int err)
449 const char *errbuf_ptr = errbuf;
451 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
452 errbuf_ptr = strerror(AVUNERROR(err));
453 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
456 static int warned_cfg = 0;
459 #define SHOW_VERSION 2
460 #define SHOW_CONFIG 4
462 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
463 if (CONFIG_##LIBNAME) { \
464 const char *indent = flags & INDENT? " " : ""; \
465 if (flags & SHOW_VERSION) { \
466 unsigned int version = libname##_version(); \
467 av_log(NULL, level, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n",\
469 LIB##LIBNAME##_VERSION_MAJOR, \
470 LIB##LIBNAME##_VERSION_MINOR, \
471 LIB##LIBNAME##_VERSION_MICRO, \
472 version >> 16, version >> 8 & 0xff, version & 0xff); \
474 if (flags & SHOW_CONFIG) { \
475 const char *cfg = libname##_configuration(); \
476 if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
478 av_log(NULL, level, \
479 "%sWARNING: library configuration mismatch\n", \
483 av_log(NULL, level, "%s%-11s configuration: %s\n", \
484 indent, #libname, cfg); \
489 static void print_all_libs_info(int flags, int level)
491 PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
492 PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
493 PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
494 PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
495 PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
496 PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
497 PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
500 void show_banner(void)
502 av_log(NULL, AV_LOG_INFO, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
503 program_name, program_birth_year, this_year);
504 av_log(NULL, AV_LOG_INFO, " built on %s %s with %s %s\n",
505 __DATE__, __TIME__, CC_TYPE, CC_VERSION);
506 av_log(NULL, AV_LOG_INFO, " configuration: " FFMPEG_CONFIGURATION "\n");
507 print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
508 print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
511 int opt_version(const char *opt, const char *arg) {
512 av_log_set_callback(log_callback_help);
513 printf("%s " FFMPEG_VERSION "\n", program_name);
514 print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
518 int opt_license(const char *opt, const char *arg)
522 "This version of %s has nonfree parts compiled in.\n"
523 "Therefore it is not legally redistributable.\n",
526 "%s is free software; you can redistribute it and/or modify\n"
527 "it under the terms of the GNU General Public License as published by\n"
528 "the Free Software Foundation; either version 3 of the License, or\n"
529 "(at your option) any later version.\n"
531 "%s is distributed in the hope that it will be useful,\n"
532 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
533 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
534 "GNU General Public License for more details.\n"
536 "You should have received a copy of the GNU General Public License\n"
537 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
538 program_name, program_name, program_name
540 "%s is free software; you can redistribute it and/or modify\n"
541 "it under the terms of the GNU General Public License as published by\n"
542 "the Free Software Foundation; either version 2 of the License, or\n"
543 "(at your option) any later version.\n"
545 "%s is distributed in the hope that it will be useful,\n"
546 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
547 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
548 "GNU General Public License for more details.\n"
550 "You should have received a copy of the GNU General Public License\n"
551 "along with %s; if not, write to the Free Software\n"
552 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
553 program_name, program_name, program_name
555 "%s is free software; you can redistribute it and/or modify\n"
556 "it under the terms of the GNU Lesser General Public License as published by\n"
557 "the Free Software Foundation; either version 3 of the License, or\n"
558 "(at your option) any later version.\n"
560 "%s is distributed in the hope that it will be useful,\n"
561 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
562 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
563 "GNU Lesser General Public License for more details.\n"
565 "You should have received a copy of the GNU Lesser General Public License\n"
566 "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
567 program_name, program_name, program_name
569 "%s is free software; you can redistribute it and/or\n"
570 "modify it under the terms of the GNU Lesser General Public\n"
571 "License as published by the Free Software Foundation; either\n"
572 "version 2.1 of the License, or (at your option) any later version.\n"
574 "%s is distributed in the hope that it will be useful,\n"
575 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
576 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
577 "Lesser General Public License for more details.\n"
579 "You should have received a copy of the GNU Lesser General Public\n"
580 "License along with %s; if not, write to the Free Software\n"
581 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
582 program_name, program_name, program_name
588 int opt_formats(const char *opt, const char *arg)
590 AVInputFormat *ifmt=NULL;
591 AVOutputFormat *ofmt=NULL;
592 const char *last_name;
596 " D. = Demuxing supported\n"
597 " .E = Muxing supported\n"
603 const char *name=NULL;
604 const char *long_name=NULL;
606 while((ofmt= av_oformat_next(ofmt))) {
607 if((name == NULL || strcmp(ofmt->name, name)<0) &&
608 strcmp(ofmt->name, last_name)>0){
610 long_name= ofmt->long_name;
614 while((ifmt= av_iformat_next(ifmt))) {
615 if((name == NULL || strcmp(ifmt->name, name)<0) &&
616 strcmp(ifmt->name, last_name)>0){
618 long_name= ifmt->long_name;
621 if(name && strcmp(ifmt->name, name)==0)
633 long_name ? long_name:" ");
638 int opt_codecs(const char *opt, const char *arg)
640 AVCodec *p=NULL, *p2;
641 const char *last_name;
644 " D..... = Decoding supported\n"
645 " .E.... = Encoding supported\n"
646 " ..V... = Video codec\n"
647 " ..A... = Audio codec\n"
648 " ..S... = Subtitle codec\n"
649 " ...S.. = Supports draw_horiz_band\n"
650 " ....D. = Supports direct rendering method 1\n"
651 " .....T = Supports weird frame truncation\n"
658 const char *type_str;
661 while((p= av_codec_next(p))) {
662 if((p2==NULL || strcmp(p->name, p2->name)<0) &&
663 strcmp(p->name, last_name)>0){
665 decode= encode= cap=0;
667 if(p2 && strcmp(p->name, p2->name)==0){
668 if(p->decode) decode=1;
669 if(p->encode) encode=1;
670 cap |= p->capabilities;
678 case AVMEDIA_TYPE_VIDEO:
681 case AVMEDIA_TYPE_AUDIO:
684 case AVMEDIA_TYPE_SUBTITLE:
692 " %s%s%s%s%s%s %-15s %s",
693 decode ? "D": (/*p2->decoder ? "d":*/" "),
696 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
697 cap & CODEC_CAP_DR1 ? "D":" ",
698 cap & CODEC_CAP_TRUNCATED ? "T":" ",
700 p2->long_name ? p2->long_name : "");
701 /* if(p2->decoder && decode==0)
702 printf(" use %s for decoding", p2->decoder->name);*/
707 "Note, the names of encoders and decoders do not always match, so there are\n"
708 "several cases where the above table shows encoder only or decoder only entries\n"
709 "even though both encoding and decoding are supported. For example, the h263\n"
710 "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
715 int opt_bsfs(const char *opt, const char *arg)
717 AVBitStreamFilter *bsf=NULL;
719 printf("Bitstream filters:\n");
720 while((bsf = av_bitstream_filter_next(bsf)))
721 printf("%s\n", bsf->name);
726 int opt_protocols(const char *opt, const char *arg)
728 URLProtocol *up=NULL;
730 printf("Supported file protocols:\n"
731 "I.. = Input supported\n"
732 ".O. = Output supported\n"
733 "..S = Seek supported\n"
736 while((up = av_protocol_next(up)))
737 printf("%c%c%c %s\n",
738 up->url_read ? 'I' : '.',
739 up->url_write ? 'O' : '.',
740 up->url_seek ? 'S' : '.',
745 int opt_filters(const char *opt, const char *arg)
747 AVFilter av_unused(**filter) = NULL;
749 printf("Filters:\n");
751 while ((filter = av_filter_next(filter)) && *filter)
752 printf("%-16s %s\n", (*filter)->name, (*filter)->description);
757 int opt_pix_fmts(const char *opt, const char *arg)
759 enum PixelFormat pix_fmt;
763 "I.... = Supported Input format for conversion\n"
764 ".O... = Supported Output format for conversion\n"
765 "..H.. = Hardware accelerated format\n"
766 "...P. = Paletted format\n"
767 "....B = Bitstream format\n"
768 "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
772 # define sws_isSupportedInput(x) 0
773 # define sws_isSupportedOutput(x) 0
776 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
777 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
780 printf("%c%c%c%c%c %-16s %d %2d\n",
781 sws_isSupportedInput (pix_fmt) ? 'I' : '.',
782 sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
783 pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
784 pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
785 pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
787 pix_desc->nb_components,
788 av_get_bits_per_pixel(pix_desc));
793 int show_sample_fmts(const char *opt, const char *arg)
797 for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
798 printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
805 int yesno = (toupper(c) == 'Y');
807 while (c != '\n' && c != EOF)
813 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
816 FILE *f = fopen(filename, "rb");
819 av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
820 return AVERROR(errno);
822 fseek(f, 0, SEEK_END);
824 fseek(f, 0, SEEK_SET);
825 *bufptr = av_malloc(*size + 1);
827 av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
829 return AVERROR(ENOMEM);
831 ret = fread(*bufptr, 1, *size, f);
835 av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
836 filename, strerror(errno));
837 ret = AVERROR(errno);
842 (*bufptr)[*size++] = '\0';
849 FILE *get_preset_file(char *filename, size_t filename_size,
850 const char *preset_name, int is_path, const char *codec_name)
854 const char *base[3]= { getenv("FFMPEG_DATADIR"),
860 av_strlcpy(filename, preset_name, filename_size);
861 f = fopen(filename, "r");
864 char datadir[MAX_PATH], *ls;
867 if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
869 for (ls = datadir; ls < datadir + strlen(datadir); ls++)
870 if (*ls == '\\') *ls = '/';
872 if (ls = strrchr(datadir, '/'))
875 strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
880 for (i = 0; i < 3 && !f; i++) {
883 snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
884 f = fopen(filename, "r");
885 if (!f && codec_name) {
886 snprintf(filename, filename_size,
887 "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
888 f = fopen(filename, "r");
896 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
898 if (*spec <= '9' && *spec >= '0') /* opt:index */
899 return strtol(spec, NULL, 0) == st->index;
900 else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || *spec == 't') { /* opt:[vasdt] */
901 enum AVMediaType type;
904 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
905 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
906 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
907 case 'd': type = AVMEDIA_TYPE_DATA; break;
908 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
909 default: abort(); // never reached, silence warning
911 if (type != st->codec->codec_type)
913 if (*spec++ == ':') { /* possibly followed by :index */
914 int i, index = strtol(spec, NULL, 0);
915 for (i = 0; i < s->nb_streams; i++)
916 if (s->streams[i]->codec->codec_type == type && index-- == 0)
917 return i == st->index;
921 } else if (*spec == 'p' && *(spec + 1) == ':') {
925 prog_id = strtol(spec, &endptr, 0);
926 for (i = 0; i < s->nb_programs; i++) {
927 if (s->programs[i]->id != prog_id)
930 if (*endptr++ == ':') {
931 int stream_idx = strtol(endptr, NULL, 0);
932 return (stream_idx >= 0 && stream_idx < s->programs[i]->nb_stream_indexes &&
933 st->index == s->programs[i]->stream_index[stream_idx]);
936 for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
937 if (st->index == s->programs[i]->stream_index[j])
941 } else if (!*spec) /* empty specifier, matches everything */
944 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
945 return AVERROR(EINVAL);
948 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec, AVFormatContext *s, AVStream *st)
950 AVDictionary *ret = NULL;
951 AVDictionaryEntry *t = NULL;
952 int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
954 const AVClass *cc = avcodec_get_class();
959 switch (codec->type) {
960 case AVMEDIA_TYPE_VIDEO: prefix = 'v'; flags |= AV_OPT_FLAG_VIDEO_PARAM; break;
961 case AVMEDIA_TYPE_AUDIO: prefix = 'a'; flags |= AV_OPT_FLAG_AUDIO_PARAM; break;
962 case AVMEDIA_TYPE_SUBTITLE: prefix = 's'; flags |= AV_OPT_FLAG_SUBTITLE_PARAM; break;
965 while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
966 char *p = strchr(t->key, ':');
968 /* check stream specification in opt name */
970 switch (check_stream_specifier(s, st, p + 1)) {
971 case 1: *p = 0; break;
973 default: return NULL;
976 if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
977 (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ)))
978 av_dict_set(&ret, t->key, t->value, 0);
979 else if (t->key[0] == prefix && av_opt_find(&cc, t->key+1, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ))
980 av_dict_set(&ret, t->key+1, t->value, 0);
988 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
995 opts = av_mallocz(s->nb_streams * sizeof(*opts));
997 av_log(NULL, AV_LOG_ERROR, "Could not alloc memory for stream options.\n");
1000 for (i = 0; i < s->nb_streams; i++)
1001 opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id), s, s->streams[i]);
1005 void *grow_array(void *array, int elem_size, int *size, int new_size)
1007 if (new_size >= INT_MAX / elem_size) {
1008 av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1011 if (*size < new_size) {
1012 uint8_t *tmp = av_realloc(array, new_size*elem_size);
1014 av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1017 memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);