]> git.sesse.net Git - ffmpeg/blob - cmdutils.c
fb4e869f2ebcba5e13cad404f84109141445049a
[ffmpeg] / cmdutils.c
1 /*
2  * Various utilities for command line tools
3  * Copyright (c) 2000-2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <math.h>
26
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. */
30
31 #include "config.h"
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 #if CONFIG_POSTPROC
38 #include "libpostproc/postprocess.h"
39 #endif
40 #include "libavutil/avstring.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/eval.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/opt.h"
47 #include "cmdutils.h"
48 #include "version.h"
49 #if CONFIG_NETWORK
50 #include "libavformat/network.h"
51 #endif
52 #if HAVE_SYS_RESOURCE_H
53 #include <sys/resource.h>
54 #endif
55
56 struct SwsContext *sws_opts;
57 SwrContext *swr_opts;
58 AVDictionary *format_opts, *codec_opts;
59
60 const int this_year = 2012;
61
62 static FILE *report_file;
63
64 void init_opts(void)
65 {
66 #if CONFIG_SWSCALE
67     sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
68                               NULL, NULL, NULL);
69 #endif
70     swr_opts = swr_alloc();
71 }
72
73 void uninit_opts(void)
74 {
75 #if CONFIG_SWSCALE
76     sws_freeContext(sws_opts);
77     sws_opts = NULL;
78 #endif
79     swr_free(&swr_opts);
80     av_dict_free(&format_opts);
81     av_dict_free(&codec_opts);
82 }
83
84 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
85 {
86     vfprintf(stdout, fmt, vl);
87 }
88
89 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
90 {
91     va_list vl2;
92     char line[1024];
93     static int print_prefix = 1;
94
95     va_copy(vl2, vl);
96     av_log_default_callback(ptr, level, fmt, vl);
97     av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
98     va_end(vl2);
99     fputs(line, report_file);
100     fflush(report_file);
101 }
102
103 double parse_number_or_die(const char *context, const char *numstr, int type,
104                            double min, double max)
105 {
106     char *tail;
107     const char *error;
108     double d = av_strtod(numstr, &tail);
109     if (*tail)
110         error = "Expected number for %s but found: %s\n";
111     else if (d < min || d > max)
112         error = "The value for %s was %s which is not within %f - %f\n";
113     else if (type == OPT_INT64 && (int64_t)d != d)
114         error = "Expected int64 for %s but found %s\n";
115     else if (type == OPT_INT && (int)d != d)
116         error = "Expected int for %s but found %s\n";
117     else
118         return d;
119     av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
120     exit_program(1);
121     return 0;
122 }
123
124 int64_t parse_time_or_die(const char *context, const char *timestr,
125                           int is_duration)
126 {
127     int64_t us;
128     if (av_parse_time(&us, timestr, is_duration) < 0) {
129         av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
130                is_duration ? "duration" : "date", context, timestr);
131         exit_program(1);
132     }
133     return us;
134 }
135
136 void show_help_options(const OptionDef *options, const char *msg, int mask,
137                        int value)
138 {
139     const OptionDef *po;
140     int first;
141
142     first = 1;
143     for (po = options; po->name != NULL; po++) {
144         char buf[64];
145         if ((po->flags & mask) == value) {
146             if (first) {
147                 printf("%s", msg);
148                 first = 0;
149             }
150             av_strlcpy(buf, po->name, sizeof(buf));
151             if (po->flags & HAS_ARG) {
152                 av_strlcat(buf, " ", sizeof(buf));
153                 av_strlcat(buf, po->argname, sizeof(buf));
154             }
155             printf("-%-17s  %s\n", buf, po->help);
156         }
157     }
158 }
159
160 void show_help_children(const AVClass *class, int flags)
161 {
162     const AVClass *child = NULL;
163     av_opt_show2(&class, NULL, flags, 0);
164     printf("\n");
165
166     while (child = av_opt_child_class_next(class, child))
167         show_help_children(child, flags);
168 }
169
170 static const OptionDef *find_option(const OptionDef *po, const char *name)
171 {
172     const char *p = strchr(name, ':');
173     int len = p ? p - name : strlen(name);
174
175     while (po->name != NULL) {
176         if (!strncmp(name, po->name, len) && strlen(po->name) == len)
177             break;
178         po++;
179     }
180     return po;
181 }
182
183 #if defined(_WIN32) && !defined(__MINGW32CE__)
184 #include <windows.h>
185 /* Will be leaked on exit */
186 static char** win32_argv_utf8 = NULL;
187 static int win32_argc = 0;
188
189 /**
190  * Prepare command line arguments for executable.
191  * For Windows - perform wide-char to UTF-8 conversion.
192  * Input arguments should be main() function arguments.
193  * @param argc_ptr Arguments number (including executable)
194  * @param argv_ptr Arguments list.
195  */
196 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
197 {
198     char *argstr_flat;
199     wchar_t **argv_w;
200     int i, buffsize = 0, offset = 0;
201
202     if (win32_argv_utf8) {
203         *argc_ptr = win32_argc;
204         *argv_ptr = win32_argv_utf8;
205         return;
206     }
207
208     win32_argc = 0;
209     argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
210     if (win32_argc <= 0 || !argv_w)
211         return;
212
213     /* determine the UTF-8 buffer size (including NULL-termination symbols) */
214     for (i = 0; i < win32_argc; i++)
215         buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
216                                         NULL, 0, NULL, NULL);
217
218     win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
219     argstr_flat     = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
220     if (win32_argv_utf8 == NULL) {
221         LocalFree(argv_w);
222         return;
223     }
224
225     for (i = 0; i < win32_argc; i++) {
226         win32_argv_utf8[i] = &argstr_flat[offset];
227         offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
228                                       &argstr_flat[offset],
229                                       buffsize - offset, NULL, NULL);
230     }
231     win32_argv_utf8[i] = NULL;
232     LocalFree(argv_w);
233
234     *argc_ptr = win32_argc;
235     *argv_ptr = win32_argv_utf8;
236 }
237 #else
238 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
239 {
240     /* nothing to do */
241 }
242 #endif /* WIN32 && !__MINGW32CE__ */
243
244 int parse_option(void *optctx, const char *opt, const char *arg,
245                  const OptionDef *options)
246 {
247     const OptionDef *po;
248     int bool_val = 1;
249     int *dstcount;
250     void *dst;
251
252     po = find_option(options, opt);
253     if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
254         /* handle 'no' bool option */
255         po = find_option(options, opt + 2);
256         if ((po->name && (po->flags & OPT_BOOL)))
257             bool_val = 0;
258     }
259     if (!po->name)
260         po = find_option(options, "default");
261     if (!po->name) {
262         av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
263         return AVERROR(EINVAL);
264     }
265     if (po->flags & HAS_ARG && !arg) {
266         av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
267         return AVERROR(EINVAL);
268     }
269
270     /* new-style options contain an offset into optctx, old-style address of
271      * a global var*/
272     dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
273                                               : po->u.dst_ptr;
274
275     if (po->flags & OPT_SPEC) {
276         SpecifierOpt **so = dst;
277         char *p = strchr(opt, ':');
278
279         dstcount = (int *)(so + 1);
280         *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
281         (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
282         dst = &(*so)[*dstcount - 1].u;
283     }
284
285     if (po->flags & OPT_STRING) {
286         char *str;
287         str = av_strdup(arg);
288         *(char **)dst = str;
289     } else if (po->flags & OPT_BOOL) {
290         *(int *)dst = bool_val;
291     } else if (po->flags & OPT_INT) {
292         *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
293     } else if (po->flags & OPT_INT64) {
294         *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
295     } else if (po->flags & OPT_TIME) {
296         *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
297     } else if (po->flags & OPT_FLOAT) {
298         *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
299     } else if (po->flags & OPT_DOUBLE) {
300         *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
301     } else if (po->u.func_arg) {
302         int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
303                                         : po->u.func_arg(opt, arg);
304         if (ret < 0) {
305             av_log(NULL, AV_LOG_ERROR,
306                    "Failed to set value '%s' for option '%s'\n", arg, opt);
307             return ret;
308         }
309     }
310     if (po->flags & OPT_EXIT)
311         exit_program(0);
312     return !!(po->flags & HAS_ARG);
313 }
314
315 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
316                    void (*parse_arg_function)(void *, const char*))
317 {
318     const char *opt;
319     int optindex, handleoptions = 1, ret;
320
321     /* perform system-dependent conversions for arguments list */
322     prepare_app_arguments(&argc, &argv);
323
324     /* parse options */
325     optindex = 1;
326     while (optindex < argc) {
327         opt = argv[optindex++];
328
329         if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
330             if (opt[1] == '-' && opt[2] == '\0') {
331                 handleoptions = 0;
332                 continue;
333             }
334             opt++;
335
336             if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
337                 exit_program(1);
338             optindex += ret;
339         } else {
340             if (parse_arg_function)
341                 parse_arg_function(optctx, opt);
342         }
343     }
344 }
345
346 int locate_option(int argc, char **argv, const OptionDef *options,
347                   const char *optname)
348 {
349     const OptionDef *po;
350     int i;
351
352     for (i = 1; i < argc; i++) {
353         const char *cur_opt = argv[i];
354
355         if (*cur_opt++ != '-')
356             continue;
357
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);
361
362         if ((!po->name && !strcmp(cur_opt, optname)) ||
363              (po->name && !strcmp(optname, po->name)))
364             return i;
365
366         if (!po || po->flags & HAS_ARG)
367             i++;
368     }
369     return 0;
370 }
371
372 static void dump_argument(const char *a)
373 {
374     const unsigned char *p;
375
376     for (p = a; *p; p++)
377         if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
378               *p == '_' || (*p >= 'a' && *p <= 'z')))
379             break;
380     if (!*p) {
381         fputs(a, report_file);
382         return;
383     }
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);
390         else
391             fputc(*p, report_file);
392     }
393     fputc('"', report_file);
394 }
395
396 void parse_loglevel(int argc, char **argv, const OptionDef *options)
397 {
398     int idx = locate_option(argc, argv, options, "loglevel");
399     if (!idx)
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");
406         if (report_file) {
407             int i;
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);
412             }
413             fflush(report_file);
414         }
415     }
416 }
417
418 #define FLAGS(o) ((o)->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
419 int opt_default(const char *opt, const char *arg)
420 {
421     const AVOption *oc, *of, *os, *oswr = NULL;
422     char opt_stripped[128];
423     const char *p;
424     const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
425
426     if (!(p = strchr(opt, ':')))
427         p = opt + strlen(opt);
428     av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
429
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));
438 #if CONFIG_SWSCALE
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);
444         if (ret < 0) {
445             av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
446             return ret;
447         }
448     }
449 #endif
450     swr_class = swr_get_class();
451     if (!oc && !of && !os && (oswr = av_opt_find(&swr_class, opt, NULL, 0,
452                           AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
453         int ret = av_opt_set(swr_opts, opt, arg, 0);
454         if (ret < 0) {
455             av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
456             return ret;
457         }
458     }
459
460     if (oc || of || os || oswr)
461         return 0;
462     av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
463     return AVERROR_OPTION_NOT_FOUND;
464 }
465
466 int opt_loglevel(const char *opt, const char *arg)
467 {
468     const struct { const char *name; int level; } log_levels[] = {
469         { "quiet"  , AV_LOG_QUIET   },
470         { "panic"  , AV_LOG_PANIC   },
471         { "fatal"  , AV_LOG_FATAL   },
472         { "error"  , AV_LOG_ERROR   },
473         { "warning", AV_LOG_WARNING },
474         { "info"   , AV_LOG_INFO    },
475         { "verbose", AV_LOG_VERBOSE },
476         { "debug"  , AV_LOG_DEBUG   },
477     };
478     char *tail;
479     int level;
480     int i;
481
482     for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
483         if (!strcmp(log_levels[i].name, arg)) {
484             av_log_set_level(log_levels[i].level);
485             return 0;
486         }
487     }
488
489     level = strtol(arg, &tail, 10);
490     if (*tail) {
491         av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
492                "Possible levels are numbers or:\n", arg);
493         for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
494             av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
495         exit_program(1);
496     }
497     av_log_set_level(level);
498     return 0;
499 }
500
501 int opt_report(const char *opt)
502 {
503     char filename[64];
504     time_t now;
505     struct tm *tm;
506
507     if (report_file) /* already opened */
508         return 0;
509     time(&now);
510     tm = localtime(&now);
511     snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
512              program_name,
513              tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
514              tm->tm_hour, tm->tm_min, tm->tm_sec);
515     report_file = fopen(filename, "w");
516     if (!report_file) {
517         av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
518                filename, strerror(errno));
519         return AVERROR(errno);
520     }
521     av_log_set_callback(log_callback_report);
522     av_log(NULL, AV_LOG_INFO,
523            "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
524            "Report written to \"%s\"\n",
525            program_name,
526            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
527            tm->tm_hour, tm->tm_min, tm->tm_sec,
528            filename);
529     av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
530     return 0;
531 }
532
533 int opt_max_alloc(const char *opt, const char *arg)
534 {
535     char *tail;
536     size_t max;
537
538     max = strtol(arg, &tail, 10);
539     if (*tail) {
540         av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
541         exit_program(1);
542     }
543     av_max_alloc(max);
544     return 0;
545 }
546
547 int opt_cpuflags(const char *opt, const char *arg)
548 {
549     static const AVOption cpuflags_opts[] = {
550         { "flags"   , NULL, 0, AV_OPT_TYPE_FLAGS, { 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
551         { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ALTIVEC  },    .unit = "flags" },
552         { "mmx"     , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_MMX      },    .unit = "flags" },
553         { "mmx2"    , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_MMX2     },    .unit = "flags" },
554         { "sse"     , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE      },    .unit = "flags" },
555         { "sse2"    , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE2     },    .unit = "flags" },
556         { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE2SLOW },    .unit = "flags" },
557         { "sse3"    , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE3     },    .unit = "flags" },
558         { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE3SLOW },    .unit = "flags" },
559         { "ssse3"   , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSSE3    },    .unit = "flags" },
560         { "atom"    , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ATOM     },    .unit = "flags" },
561         { "sse4.1"  , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE4     },    .unit = "flags" },
562         { "sse4.2"  , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_SSE42    },    .unit = "flags" },
563         { "avx"     , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_AVX      },    .unit = "flags" },
564         { "xop"     , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_XOP      },    .unit = "flags" },
565         { "fma4"    , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_FMA4     },    .unit = "flags" },
566         { "3dnow"   , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_3DNOW    },    .unit = "flags" },
567         { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_3DNOWEXT },    .unit = "flags" },
568
569         { "armv5te",  NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV5TE  },    .unit = "flags" },
570         { "armv6",    NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6    },    .unit = "flags" },
571         { "armv6t2",  NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6T2  },    .unit = "flags" },
572         { "vfp",      NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFP      },    .unit = "flags" },
573         { "vfpv3",    NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFPV3    },    .unit = "flags" },
574         { "neon",     NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_NEON     },    .unit = "flags" },
575
576         { NULL },
577     };
578     static const AVClass class = {
579         .class_name = "cpuflags",
580         .item_name  = av_default_item_name,
581         .option     = cpuflags_opts,
582         .version    = LIBAVUTIL_VERSION_INT,
583     };
584     int flags = av_get_cpu_flags();
585     int ret;
586     const AVClass *pclass = &class;
587
588     if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], arg, &flags)) < 0)
589         return ret;
590
591     av_force_cpu_flags(flags);
592     return 0;
593 }
594
595 int opt_codec_debug(const char *opt, const char *arg)
596 {
597     av_log_set_level(AV_LOG_DEBUG);
598     return opt_default(opt, arg);
599 }
600
601 int opt_timelimit(const char *opt, const char *arg)
602 {
603 #if HAVE_SETRLIMIT
604     int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
605     struct rlimit rl = { lim, lim + 1 };
606     if (setrlimit(RLIMIT_CPU, &rl))
607         perror("setrlimit");
608 #else
609     av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
610 #endif
611     return 0;
612 }
613
614 void print_error(const char *filename, int err)
615 {
616     char errbuf[128];
617     const char *errbuf_ptr = errbuf;
618
619     if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
620         errbuf_ptr = strerror(AVUNERROR(err));
621     av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
622 }
623
624 static int warned_cfg = 0;
625
626 #define INDENT        1
627 #define SHOW_VERSION  2
628 #define SHOW_CONFIG   4
629 #define SHOW_COPYRIGHT 8
630
631 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level)                  \
632     if (CONFIG_##LIBNAME) {                                             \
633         const char *indent = flags & INDENT? "  " : "";                 \
634         if (flags & SHOW_VERSION) {                                     \
635             unsigned int version = libname##_version();                 \
636             av_log(NULL, level, "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",\
637                    indent, #libname,                                    \
638                    LIB##LIBNAME##_VERSION_MAJOR,                        \
639                    LIB##LIBNAME##_VERSION_MINOR,                        \
640                    LIB##LIBNAME##_VERSION_MICRO,                        \
641                    version >> 16, version >> 8 & 0xff, version & 0xff); \
642         }                                                               \
643         if (flags & SHOW_CONFIG) {                                      \
644             const char *cfg = libname##_configuration();                \
645             if (strcmp(FFMPEG_CONFIGURATION, cfg)) {                    \
646                 if (!warned_cfg) {                                      \
647                     av_log(NULL, level,                                 \
648                             "%sWARNING: library configuration mismatch\n", \
649                             indent);                                    \
650                     warned_cfg = 1;                                     \
651                 }                                                       \
652                 av_log(NULL, level, "%s%-11s configuration: %s\n",      \
653                         indent, #libname, cfg);                         \
654             }                                                           \
655         }                                                               \
656     }                                                                   \
657
658 static void print_all_libs_info(int flags, int level)
659 {
660     PRINT_LIB_INFO(avutil,   AVUTIL,   flags, level);
661     PRINT_LIB_INFO(avcodec,  AVCODEC,  flags, level);
662     PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
663     PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
664     PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
665     PRINT_LIB_INFO(swscale,  SWSCALE,  flags, level);
666     PRINT_LIB_INFO(swresample,SWRESAMPLE,  flags, level);
667 #if CONFIG_POSTPROC
668     PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
669 #endif
670 }
671
672 static void print_program_info(int flags, int level)
673 {
674     const char *indent = flags & INDENT? "  " : "";
675
676     av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
677     if (flags & SHOW_COPYRIGHT)
678         av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
679                program_birth_year, this_year);
680     av_log(NULL, level, "\n");
681     av_log(NULL, level, "%sbuilt on %s %s with %s %s\n",
682            indent, __DATE__, __TIME__, CC_TYPE, CC_VERSION);
683     av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
684 }
685
686 void show_banner(int argc, char **argv, const OptionDef *options)
687 {
688     int idx = locate_option(argc, argv, options, "version");
689     if (idx)
690         return;
691
692     print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
693     print_all_libs_info(INDENT|SHOW_CONFIG,  AV_LOG_INFO);
694     print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
695 }
696
697 int opt_version(const char *opt, const char *arg) {
698     av_log_set_callback(log_callback_help);
699     print_program_info (0           , AV_LOG_INFO);
700     print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
701     return 0;
702 }
703
704 int opt_license(const char *opt, const char *arg)
705 {
706     printf(
707 #if CONFIG_NONFREE
708     "This version of %s has nonfree parts compiled in.\n"
709     "Therefore it is not legally redistributable.\n",
710     program_name
711 #elif CONFIG_GPLV3
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 3 of the License, or\n"
715     "(at your option) any later version.\n"
716     "\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"
721     "\n"
722     "You should have received a copy of the GNU General Public License\n"
723     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
724     program_name, program_name, program_name
725 #elif CONFIG_GPL
726     "%s is free software; you can redistribute it and/or modify\n"
727     "it under the terms of the GNU General Public License as published by\n"
728     "the Free Software Foundation; either version 2 of the License, or\n"
729     "(at your option) any later version.\n"
730     "\n"
731     "%s is distributed in the hope that it will be useful,\n"
732     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
733     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
734     "GNU General Public License for more details.\n"
735     "\n"
736     "You should have received a copy of the GNU General Public License\n"
737     "along with %s; if not, write to the Free Software\n"
738     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
739     program_name, program_name, program_name
740 #elif CONFIG_LGPLV3
741     "%s is free software; you can redistribute it and/or modify\n"
742     "it under the terms of the GNU Lesser General Public License as published by\n"
743     "the Free Software Foundation; either version 3 of the License, or\n"
744     "(at your option) any later version.\n"
745     "\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\n"
749     "GNU Lesser General Public License for more details.\n"
750     "\n"
751     "You should have received a copy of the GNU Lesser General Public License\n"
752     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
753     program_name, program_name, program_name
754 #else
755     "%s is free software; you can redistribute it and/or\n"
756     "modify it under the terms of the GNU Lesser General Public\n"
757     "License as published by the Free Software Foundation; either\n"
758     "version 2.1 of the License, or (at your option) any later version.\n"
759     "\n"
760     "%s is distributed in the hope that it will be useful,\n"
761     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
762     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
763     "Lesser General Public License for more details.\n"
764     "\n"
765     "You should have received a copy of the GNU Lesser General Public\n"
766     "License along with %s; if not, write to the Free Software\n"
767     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
768     program_name, program_name, program_name
769 #endif
770     );
771     return 0;
772 }
773
774 int opt_formats(const char *opt, const char *arg)
775 {
776     AVInputFormat *ifmt  = NULL;
777     AVOutputFormat *ofmt = NULL;
778     const char *last_name;
779
780     printf("File formats:\n"
781            " D. = Demuxing supported\n"
782            " .E = Muxing supported\n"
783            " --\n");
784     last_name = "000";
785     for (;;) {
786         int decode = 0;
787         int encode = 0;
788         const char *name      = NULL;
789         const char *long_name = NULL;
790
791         while ((ofmt = av_oformat_next(ofmt))) {
792             if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
793                 strcmp(ofmt->name, last_name) > 0) {
794                 name      = ofmt->name;
795                 long_name = ofmt->long_name;
796                 encode    = 1;
797             }
798         }
799         while ((ifmt = av_iformat_next(ifmt))) {
800             if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
801                 strcmp(ifmt->name, last_name) > 0) {
802                 name      = ifmt->name;
803                 long_name = ifmt->long_name;
804                 encode    = 0;
805             }
806             if (name && strcmp(ifmt->name, name) == 0)
807                 decode = 1;
808         }
809         if (name == NULL)
810             break;
811         last_name = name;
812
813         printf(" %s%s %-15s %s\n",
814                decode ? "D" : " ",
815                encode ? "E" : " ",
816                name,
817             long_name ? long_name:" ");
818     }
819     return 0;
820 }
821
822 static char get_media_type_char(enum AVMediaType type)
823 {
824     static const char map[AVMEDIA_TYPE_NB] = {
825         [AVMEDIA_TYPE_VIDEO]      = 'V',
826         [AVMEDIA_TYPE_AUDIO]      = 'A',
827         [AVMEDIA_TYPE_DATA]       = 'D',
828         [AVMEDIA_TYPE_SUBTITLE]   = 'S',
829         [AVMEDIA_TYPE_ATTACHMENT] = 'T',
830     };
831     return type >= 0 && type < AVMEDIA_TYPE_NB && map[type] ? map[type] : '?';
832 }
833
834 int opt_codecs(const char *opt, const char *arg)
835 {
836     AVCodec *p = NULL, *p2;
837     const char *last_name;
838     printf("Codecs:\n"
839            " D..... = Decoding supported\n"
840            " .E.... = Encoding supported\n"
841            " ..V... = Video codec\n"
842            " ..A... = Audio codec\n"
843            " ..S... = Subtitle codec\n"
844            " ...S.. = Supports draw_horiz_band\n"
845            " ....D. = Supports direct rendering method 1\n"
846            " .....T = Supports weird frame truncation\n"
847            " ------\n");
848     last_name= "000";
849     for (;;) {
850         int decode = 0;
851         int encode = 0;
852         int cap    = 0;
853
854         p2 = NULL;
855         while ((p = av_codec_next(p))) {
856             if ((p2 == NULL || strcmp(p->name, p2->name) < 0) &&
857                 strcmp(p->name, last_name) > 0) {
858                 p2 = p;
859                 decode = encode = cap = 0;
860             }
861             if (p2 && strcmp(p->name, p2->name) == 0) {
862                 if (av_codec_is_decoder(p))
863                     decode = 1;
864                 if (av_codec_is_encoder(p))
865                     encode = 1;
866                 cap |= p->capabilities;
867             }
868         }
869         if (p2 == NULL)
870             break;
871         last_name = p2->name;
872
873         printf(" %s%s%c%s%s%s %-15s %s",
874                decode ? "D" : (/* p2->decoder ? "d" : */ " "),
875                encode ? "E" : " ",
876                get_media_type_char(p2->type),
877                cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S" : " ",
878                cap & CODEC_CAP_DR1 ? "D" : " ",
879                cap & CODEC_CAP_TRUNCATED ? "T" : " ",
880                p2->name,
881                p2->long_name ? p2->long_name : "");
882 #if 0
883             if (p2->decoder && decode == 0)
884                 printf(" use %s for decoding", p2->decoder->name);
885 #endif
886         printf("\n");
887     }
888     printf("\n");
889     printf("Note, the names of encoders and decoders do not always match, so there are\n"
890            "several cases where the above table shows encoder only or decoder only entries\n"
891            "even though both encoding and decoding are supported. For example, the h263\n"
892            "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
893            "worse.\n");
894     return 0;
895 }
896
897 int opt_bsfs(const char *opt, const char *arg)
898 {
899     AVBitStreamFilter *bsf = NULL;
900
901     printf("Bitstream filters:\n");
902     while ((bsf = av_bitstream_filter_next(bsf)))
903         printf("%s\n", bsf->name);
904     printf("\n");
905     return 0;
906 }
907
908 int opt_protocols(const char *opt, const char *arg)
909 {
910     void *opaque = NULL;
911     const char *name;
912
913     printf("Supported file protocols:\n"
914            "Input:\n");
915     while ((name = avio_enum_protocols(&opaque, 0)))
916         printf("%s\n", name);
917     printf("Output:\n");
918     while ((name = avio_enum_protocols(&opaque, 1)))
919         printf("%s\n", name);
920     return 0;
921 }
922
923 int opt_filters(const char *opt, const char *arg)
924 {
925     AVFilter av_unused(**filter) = NULL;
926     char descr[64], *descr_cur;
927     int i, j;
928     const AVFilterPad *pad;
929
930     printf("Filters:\n");
931 #if CONFIG_AVFILTER
932     while ((filter = av_filter_next(filter)) && *filter) {
933         descr_cur = descr;
934         for (i = 0; i < 2; i++) {
935             if (i) {
936                 *(descr_cur++) = '-';
937                 *(descr_cur++) = '>';
938             }
939             pad = i ? (*filter)->outputs : (*filter)->inputs;
940             for (j = 0; pad[j].name; j++) {
941                 if (descr_cur >= descr + sizeof(descr) - 4)
942                     break;
943                 *(descr_cur++) = get_media_type_char(pad[j].type);
944             }
945             if (!j)
946                 *(descr_cur++) = '|';
947         }
948         *descr_cur = 0;
949         printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
950     }
951 #endif
952     return 0;
953 }
954
955 int opt_pix_fmts(const char *opt, const char *arg)
956 {
957     enum PixelFormat pix_fmt;
958
959     printf("Pixel formats:\n"
960            "I.... = Supported Input  format for conversion\n"
961            ".O... = Supported Output format for conversion\n"
962            "..H.. = Hardware accelerated format\n"
963            "...P. = Paletted format\n"
964            "....B = Bitstream format\n"
965            "FLAGS NAME            NB_COMPONENTS BITS_PER_PIXEL\n"
966            "-----\n");
967
968 #if !CONFIG_SWSCALE
969 #   define sws_isSupportedInput(x)  0
970 #   define sws_isSupportedOutput(x) 0
971 #endif
972
973     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
974         const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
975         if(!pix_desc->name)
976             continue;
977         printf("%c%c%c%c%c %-16s       %d            %2d\n",
978                sws_isSupportedInput (pix_fmt)      ? 'I' : '.',
979                sws_isSupportedOutput(pix_fmt)      ? 'O' : '.',
980                pix_desc->flags & PIX_FMT_HWACCEL   ? 'H' : '.',
981                pix_desc->flags & PIX_FMT_PAL       ? 'P' : '.',
982                pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
983                pix_desc->name,
984                pix_desc->nb_components,
985                av_get_bits_per_pixel(pix_desc));
986     }
987     return 0;
988 }
989
990 int show_sample_fmts(const char *opt, const char *arg)
991 {
992     int i;
993     char fmt_str[128];
994     for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
995         printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
996     return 0;
997 }
998
999 int read_yesno(void)
1000 {
1001     int c = getchar();
1002     int yesno = (toupper(c) == 'Y');
1003
1004     while (c != '\n' && c != EOF)
1005         c = getchar();
1006
1007     return yesno;
1008 }
1009
1010 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1011 {
1012     int ret;
1013     FILE *f = fopen(filename, "rb");
1014
1015     if (!f) {
1016         av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1017                strerror(errno));
1018         return AVERROR(errno);
1019     }
1020     fseek(f, 0, SEEK_END);
1021     *size = ftell(f);
1022     fseek(f, 0, SEEK_SET);
1023     *bufptr = av_malloc(*size + 1);
1024     if (!*bufptr) {
1025         av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1026         fclose(f);
1027         return AVERROR(ENOMEM);
1028     }
1029     ret = fread(*bufptr, 1, *size, f);
1030     if (ret < *size) {
1031         av_free(*bufptr);
1032         if (ferror(f)) {
1033             av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1034                    filename, strerror(errno));
1035             ret = AVERROR(errno);
1036         } else
1037             ret = AVERROR_EOF;
1038     } else {
1039         ret = 0;
1040         (*bufptr)[*size++] = '\0';
1041     }
1042
1043     fclose(f);
1044     return ret;
1045 }
1046
1047 FILE *get_preset_file(char *filename, size_t filename_size,
1048                       const char *preset_name, int is_path,
1049                       const char *codec_name)
1050 {
1051     FILE *f = NULL;
1052     int i;
1053     const char *base[3] = { getenv("FFMPEG_DATADIR"),
1054                             getenv("HOME"),
1055                             FFMPEG_DATADIR, };
1056
1057     if (is_path) {
1058         av_strlcpy(filename, preset_name, filename_size);
1059         f = fopen(filename, "r");
1060     } else {
1061 #ifdef _WIN32
1062         char datadir[MAX_PATH], *ls;
1063         base[2] = NULL;
1064
1065         if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1066         {
1067             for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1068                 if (*ls == '\\') *ls = '/';
1069
1070             if (ls = strrchr(datadir, '/'))
1071             {
1072                 *ls = 0;
1073                 strncat(datadir, "/ffpresets",  sizeof(datadir) - 1 - strlen(datadir));
1074                 base[2] = datadir;
1075             }
1076         }
1077 #endif
1078         for (i = 0; i < 3 && !f; i++) {
1079             if (!base[i])
1080                 continue;
1081             snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1082                      i != 1 ? "" : "/.ffmpeg", preset_name);
1083             f = fopen(filename, "r");
1084             if (!f && codec_name) {
1085                 snprintf(filename, filename_size,
1086                          "%s%s/%s-%s.ffpreset",
1087                          base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1088                          preset_name);
1089                 f = fopen(filename, "r");
1090             }
1091         }
1092     }
1093
1094     return f;
1095 }
1096
1097 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1098 {
1099     if (*spec <= '9' && *spec >= '0') /* opt:index */
1100         return strtol(spec, NULL, 0) == st->index;
1101     else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1102              *spec == 't') { /* opt:[vasdt] */
1103         enum AVMediaType type;
1104
1105         switch (*spec++) {
1106         case 'v': type = AVMEDIA_TYPE_VIDEO;      break;
1107         case 'a': type = AVMEDIA_TYPE_AUDIO;      break;
1108         case 's': type = AVMEDIA_TYPE_SUBTITLE;   break;
1109         case 'd': type = AVMEDIA_TYPE_DATA;       break;
1110         case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1111         default: abort(); // never reached, silence warning
1112         }
1113         if (type != st->codec->codec_type)
1114             return 0;
1115         if (*spec++ == ':') { /* possibly followed by :index */
1116             int i, index = strtol(spec, NULL, 0);
1117             for (i = 0; i < s->nb_streams; i++)
1118                 if (s->streams[i]->codec->codec_type == type && index-- == 0)
1119                    return i == st->index;
1120             return 0;
1121         }
1122         return 1;
1123     } else if (*spec == 'p' && *(spec + 1) == ':') {
1124         int prog_id, i, j;
1125         char *endptr;
1126         spec += 2;
1127         prog_id = strtol(spec, &endptr, 0);
1128         for (i = 0; i < s->nb_programs; i++) {
1129             if (s->programs[i]->id != prog_id)
1130                 continue;
1131
1132             if (*endptr++ == ':') {
1133                 int stream_idx = strtol(endptr, NULL, 0);
1134                 return stream_idx >= 0 &&
1135                     stream_idx < s->programs[i]->nb_stream_indexes &&
1136                     st->index == s->programs[i]->stream_index[stream_idx];
1137             }
1138
1139             for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1140                 if (st->index == s->programs[i]->stream_index[j])
1141                     return 1;
1142         }
1143         return 0;
1144     } else if (!*spec) /* empty specifier, matches everything */
1145         return 1;
1146
1147     av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1148     return AVERROR(EINVAL);
1149 }
1150
1151 AVDictionary *filter_codec_opts(AVDictionary *opts, AVCodec *codec,
1152                                 AVFormatContext *s, AVStream *st)
1153 {
1154     AVDictionary    *ret = NULL;
1155     AVDictionaryEntry *t = NULL;
1156     int            flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1157                                       : AV_OPT_FLAG_DECODING_PARAM;
1158     char          prefix = 0;
1159     const AVClass    *cc = avcodec_get_class();
1160
1161     if (!codec)
1162         return NULL;
1163
1164     switch (codec->type) {
1165     case AVMEDIA_TYPE_VIDEO:
1166         prefix  = 'v';
1167         flags  |= AV_OPT_FLAG_VIDEO_PARAM;
1168         break;
1169     case AVMEDIA_TYPE_AUDIO:
1170         prefix  = 'a';
1171         flags  |= AV_OPT_FLAG_AUDIO_PARAM;
1172         break;
1173     case AVMEDIA_TYPE_SUBTITLE:
1174         prefix  = 's';
1175         flags  |= AV_OPT_FLAG_SUBTITLE_PARAM;
1176         break;
1177     }
1178
1179     while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1180         char *p = strchr(t->key, ':');
1181
1182         /* check stream specification in opt name */
1183         if (p)
1184             switch (check_stream_specifier(s, st, p + 1)) {
1185             case  1: *p = 0; break;
1186             case  0:         continue;
1187             default:         return NULL;
1188             }
1189
1190         if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1191             (codec && codec->priv_class &&
1192              av_opt_find(&codec->priv_class, t->key, NULL, flags,
1193                          AV_OPT_SEARCH_FAKE_OBJ)))
1194             av_dict_set(&ret, t->key, t->value, 0);
1195         else if (t->key[0] == prefix &&
1196                  av_opt_find(&cc, t->key + 1, NULL, flags,
1197                              AV_OPT_SEARCH_FAKE_OBJ))
1198             av_dict_set(&ret, t->key + 1, t->value, 0);
1199
1200         if (p)
1201             *p = ':';
1202     }
1203     return ret;
1204 }
1205
1206 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1207                                            AVDictionary *codec_opts)
1208 {
1209     int i;
1210     AVDictionary **opts;
1211
1212     if (!s->nb_streams)
1213         return NULL;
1214     opts = av_mallocz(s->nb_streams * sizeof(*opts));
1215     if (!opts) {
1216         av_log(NULL, AV_LOG_ERROR,
1217                "Could not alloc memory for stream options.\n");
1218         return NULL;
1219     }
1220     for (i = 0; i < s->nb_streams; i++)
1221         opts[i] = filter_codec_opts(codec_opts, avcodec_find_decoder(s->streams[i]->codec->codec_id),
1222                                     s, s->streams[i]);
1223     return opts;
1224 }
1225
1226 void *grow_array(void *array, int elem_size, int *size, int new_size)
1227 {
1228     if (new_size >= INT_MAX / elem_size) {
1229         av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1230         exit_program(1);
1231     }
1232     if (*size < new_size) {
1233         uint8_t *tmp = av_realloc(array, new_size*elem_size);
1234         if (!tmp) {
1235             av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1236             exit_program(1);
1237         }
1238         memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1239         *size = new_size;
1240         return tmp;
1241     }
1242     return array;
1243 }