]> git.sesse.net Git - ffmpeg/blob - cmdutils.c
cosmetics: Fix "dont" "wont" "doesnt" typos
[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 "compat/va_copy.h"
33 #include "libavformat/avformat.h"
34 #include "libavfilter/avfilter.h"
35 #include "libavdevice/avdevice.h"
36 #include "libavresample/avresample.h"
37 #include "libswscale/swscale.h"
38 #include "libswresample/swresample.h"
39 #include "libpostproc/postprocess.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/bprint.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/imgutils.h"
45 #include "libavutil/parseutils.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/eval.h"
48 #include "libavutil/dict.h"
49 #include "libavutil/opt.h"
50 #include "cmdutils.h"
51 #include "version.h"
52 #if CONFIG_NETWORK
53 #include "libavformat/network.h"
54 #endif
55 #if HAVE_SYS_RESOURCE_H
56 #include <sys/time.h>
57 #include <sys/resource.h>
58 #endif
59 #if CONFIG_OPENCL
60 #include "libavutil/opencl.h"
61 #endif
62
63
64 static int init_report(const char *env);
65
66 struct SwsContext *sws_opts;
67 AVDictionary *swr_opts;
68 AVDictionary *format_opts, *codec_opts, *resample_opts;
69
70 const int this_year = 2013;
71
72 static FILE *report_file;
73
74 void init_opts(void)
75 {
76
77     if(CONFIG_SWSCALE)
78         sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
79                               NULL, NULL, NULL);
80 }
81
82 void uninit_opts(void)
83 {
84 #if CONFIG_SWSCALE
85     sws_freeContext(sws_opts);
86     sws_opts = NULL;
87 #endif
88
89     av_dict_free(&swr_opts);
90     av_dict_free(&format_opts);
91     av_dict_free(&codec_opts);
92     av_dict_free(&resample_opts);
93 }
94
95 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
96 {
97     vfprintf(stdout, fmt, vl);
98 }
99
100 static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
101 {
102     va_list vl2;
103     char line[1024];
104     static int print_prefix = 1;
105
106     va_copy(vl2, vl);
107     av_log_default_callback(ptr, level, fmt, vl);
108     av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
109     va_end(vl2);
110     fputs(line, report_file);
111     fflush(report_file);
112 }
113
114 double parse_number_or_die(const char *context, const char *numstr, int type,
115                            double min, double max)
116 {
117     char *tail;
118     const char *error;
119     double d = av_strtod(numstr, &tail);
120     if (*tail)
121         error = "Expected number for %s but found: %s\n";
122     else if (d < min || d > max)
123         error = "The value for %s was %s which is not within %f - %f\n";
124     else if (type == OPT_INT64 && (int64_t)d != d)
125         error = "Expected int64 for %s but found %s\n";
126     else if (type == OPT_INT && (int)d != d)
127         error = "Expected int for %s but found %s\n";
128     else
129         return d;
130     av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
131     exit(1);
132     return 0;
133 }
134
135 int64_t parse_time_or_die(const char *context, const char *timestr,
136                           int is_duration)
137 {
138     int64_t us;
139     if (av_parse_time(&us, timestr, is_duration) < 0) {
140         av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
141                is_duration ? "duration" : "date", context, timestr);
142         exit(1);
143     }
144     return us;
145 }
146
147 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
148                        int rej_flags, int alt_flags)
149 {
150     const OptionDef *po;
151     int first;
152
153     first = 1;
154     for (po = options; po->name != NULL; po++) {
155         char buf[64];
156
157         if (((po->flags & req_flags) != req_flags) ||
158             (alt_flags && !(po->flags & alt_flags)) ||
159             (po->flags & rej_flags))
160             continue;
161
162         if (first) {
163             printf("%s\n", msg);
164             first = 0;
165         }
166         av_strlcpy(buf, po->name, sizeof(buf));
167         if (po->argname) {
168             av_strlcat(buf, " ", sizeof(buf));
169             av_strlcat(buf, po->argname, sizeof(buf));
170         }
171         printf("-%-17s  %s\n", buf, po->help);
172     }
173     printf("\n");
174 }
175
176 void show_help_children(const AVClass *class, int flags)
177 {
178     const AVClass *child = NULL;
179     if (class->option) {
180         av_opt_show2(&class, NULL, flags, 0);
181         printf("\n");
182     }
183
184     while (child = av_opt_child_class_next(class, child))
185         show_help_children(child, flags);
186 }
187
188 static const OptionDef *find_option(const OptionDef *po, const char *name)
189 {
190     const char *p = strchr(name, ':');
191     int len = p ? p - name : strlen(name);
192
193     while (po->name != NULL) {
194         if (!strncmp(name, po->name, len) && strlen(po->name) == len)
195             break;
196         po++;
197     }
198     return po;
199 }
200
201 #if HAVE_COMMANDLINETOARGVW
202 #include <windows.h>
203 #include <shellapi.h>
204 /* Will be leaked on exit */
205 static char** win32_argv_utf8 = NULL;
206 static int win32_argc = 0;
207
208 /**
209  * Prepare command line arguments for executable.
210  * For Windows - perform wide-char to UTF-8 conversion.
211  * Input arguments should be main() function arguments.
212  * @param argc_ptr Arguments number (including executable)
213  * @param argv_ptr Arguments list.
214  */
215 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
216 {
217     char *argstr_flat;
218     wchar_t **argv_w;
219     int i, buffsize = 0, offset = 0;
220
221     if (win32_argv_utf8) {
222         *argc_ptr = win32_argc;
223         *argv_ptr = win32_argv_utf8;
224         return;
225     }
226
227     win32_argc = 0;
228     argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
229     if (win32_argc <= 0 || !argv_w)
230         return;
231
232     /* determine the UTF-8 buffer size (including NULL-termination symbols) */
233     for (i = 0; i < win32_argc; i++)
234         buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
235                                         NULL, 0, NULL, NULL);
236
237     win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
238     argstr_flat     = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
239     if (win32_argv_utf8 == NULL) {
240         LocalFree(argv_w);
241         return;
242     }
243
244     for (i = 0; i < win32_argc; i++) {
245         win32_argv_utf8[i] = &argstr_flat[offset];
246         offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
247                                       &argstr_flat[offset],
248                                       buffsize - offset, NULL, NULL);
249     }
250     win32_argv_utf8[i] = NULL;
251     LocalFree(argv_w);
252
253     *argc_ptr = win32_argc;
254     *argv_ptr = win32_argv_utf8;
255 }
256 #else
257 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
258 {
259     /* nothing to do */
260 }
261 #endif /* HAVE_COMMANDLINETOARGVW */
262
263 static int write_option(void *optctx, const OptionDef *po, const char *opt,
264                         const char *arg)
265 {
266     /* new-style options contain an offset into optctx, old-style address of
267      * a global var*/
268     void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
269                 (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
270     int *dstcount;
271
272     if (po->flags & OPT_SPEC) {
273         SpecifierOpt **so = dst;
274         char *p = strchr(opt, ':');
275
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;
280     }
281
282     if (po->flags & OPT_STRING) {
283         char *str;
284         str = av_strdup(arg);
285         av_freep(dst);
286         *(char **)dst = str;
287     } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
288         *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
289     } else if (po->flags & OPT_INT64) {
290         *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
291     } else if (po->flags & OPT_TIME) {
292         *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
293     } else if (po->flags & OPT_FLOAT) {
294         *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
295     } else if (po->flags & OPT_DOUBLE) {
296         *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
297     } else if (po->u.func_arg) {
298         int ret = po->u.func_arg(optctx, opt, arg);
299         if (ret < 0) {
300             av_log(NULL, AV_LOG_ERROR,
301                    "Failed to set value '%s' for option '%s': %s\n",
302                    arg, opt, av_err2str(ret));
303             return ret;
304         }
305     }
306     if (po->flags & OPT_EXIT)
307         exit(0);
308
309     return 0;
310 }
311
312 int parse_option(void *optctx, const char *opt, const char *arg,
313                  const OptionDef *options)
314 {
315     const OptionDef *po;
316     int ret;
317
318     po = find_option(options, opt);
319     if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
320         /* handle 'no' bool option */
321         po = find_option(options, opt + 2);
322         if ((po->name && (po->flags & OPT_BOOL)))
323             arg = "0";
324     } else if (po->flags & OPT_BOOL)
325         arg = "1";
326
327     if (!po->name)
328         po = find_option(options, "default");
329     if (!po->name) {
330         av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
331         return AVERROR(EINVAL);
332     }
333     if (po->flags & HAS_ARG && !arg) {
334         av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
335         return AVERROR(EINVAL);
336     }
337
338     ret = write_option(optctx, po, opt, arg);
339     if (ret < 0)
340         return ret;
341
342     return !!(po->flags & HAS_ARG);
343 }
344
345 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
346                    void (*parse_arg_function)(void *, const char*))
347 {
348     const char *opt;
349     int optindex, handleoptions = 1, ret;
350
351     /* perform system-dependent conversions for arguments list */
352     prepare_app_arguments(&argc, &argv);
353
354     /* parse options */
355     optindex = 1;
356     while (optindex < argc) {
357         opt = argv[optindex++];
358
359         if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
360             if (opt[1] == '-' && opt[2] == '\0') {
361                 handleoptions = 0;
362                 continue;
363             }
364             opt++;
365
366             if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
367                 exit(1);
368             optindex += ret;
369         } else {
370             if (parse_arg_function)
371                 parse_arg_function(optctx, opt);
372         }
373     }
374 }
375
376 int parse_optgroup(void *optctx, OptionGroup *g)
377 {
378     int i, ret;
379
380     av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
381            g->group_def->name, g->arg);
382
383     for (i = 0; i < g->nb_opts; i++) {
384         Option *o = &g->opts[i];
385
386         if (g->group_def->flags &&
387             !(g->group_def->flags & o->opt->flags)) {
388             av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
389                    "%s %s -- you are trying to apply an input option to an "
390                    "output file or vice versa. Move this option before the "
391                    "file it belongs to.\n", o->key, o->opt->help,
392                    g->group_def->name, g->arg);
393             return AVERROR(EINVAL);
394         }
395
396         av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
397                o->key, o->opt->help, o->val);
398
399         ret = write_option(optctx, o->opt, o->key, o->val);
400         if (ret < 0)
401             return ret;
402     }
403
404     av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
405
406     return 0;
407 }
408
409 int locate_option(int argc, char **argv, const OptionDef *options,
410                   const char *optname)
411 {
412     const OptionDef *po;
413     int i;
414
415     for (i = 1; i < argc; i++) {
416         const char *cur_opt = argv[i];
417
418         if (*cur_opt++ != '-')
419             continue;
420
421         po = find_option(options, cur_opt);
422         if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
423             po = find_option(options, cur_opt + 2);
424
425         if ((!po->name && !strcmp(cur_opt, optname)) ||
426              (po->name && !strcmp(optname, po->name)))
427             return i;
428
429         if (po->flags & HAS_ARG)
430             i++;
431     }
432     return 0;
433 }
434
435 static void dump_argument(const char *a)
436 {
437     const unsigned char *p;
438
439     for (p = a; *p; p++)
440         if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
441               *p == '_' || (*p >= 'a' && *p <= 'z')))
442             break;
443     if (!*p) {
444         fputs(a, report_file);
445         return;
446     }
447     fputc('"', report_file);
448     for (p = a; *p; p++) {
449         if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
450             fprintf(report_file, "\\%c", *p);
451         else if (*p < ' ' || *p > '~')
452             fprintf(report_file, "\\x%02x", *p);
453         else
454             fputc(*p, report_file);
455     }
456     fputc('"', report_file);
457 }
458
459 void parse_loglevel(int argc, char **argv, const OptionDef *options)
460 {
461     int idx = locate_option(argc, argv, options, "loglevel");
462     const char *env;
463     if (!idx)
464         idx = locate_option(argc, argv, options, "v");
465     if (idx && argv[idx + 1])
466         opt_loglevel(NULL, "loglevel", argv[idx + 1]);
467     idx = locate_option(argc, argv, options, "report");
468     if ((env = getenv("FFREPORT")) || idx) {
469         init_report(env);
470         if (report_file) {
471             int i;
472             fprintf(report_file, "Command line:\n");
473             for (i = 0; i < argc; i++) {
474                 dump_argument(argv[i]);
475                 fputc(i < argc - 1 ? ' ' : '\n', report_file);
476             }
477             fflush(report_file);
478         }
479     }
480 }
481
482 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
483 int opt_default(void *optctx, const char *opt, const char *arg)
484 {
485     const AVOption *o;
486     int consumed = 0;
487     char opt_stripped[128];
488     const char *p;
489     const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
490 #if CONFIG_AVRESAMPLE
491     const AVClass *rc = avresample_get_class();
492 #endif
493     const AVClass *sc, *swr_class;
494
495     if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
496         av_log_set_level(AV_LOG_DEBUG);
497
498     if (!(p = strchr(opt, ':')))
499         p = opt + strlen(opt);
500     av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
501
502     if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
503                          AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
504         ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
505          (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
506         av_dict_set(&codec_opts, opt, arg, FLAGS);
507         consumed = 1;
508     }
509     if ((o = av_opt_find(&fc, opt, NULL, 0,
510                          AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
511         av_dict_set(&format_opts, opt, arg, FLAGS);
512         if (consumed)
513             av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
514         consumed = 1;
515     }
516 #if CONFIG_SWSCALE
517     sc = sws_get_class();
518     if (!consumed && av_opt_find(&sc, opt, NULL, 0,
519                          AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
520         // XXX we only support sws_flags, not arbitrary sws options
521         int ret = av_opt_set(sws_opts, opt, arg, 0);
522         if (ret < 0) {
523             av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
524             return ret;
525         }
526         consumed = 1;
527     }
528 #endif
529 #if CONFIG_SWRESAMPLE
530     swr_class = swr_get_class();
531     if (!consumed && (o=av_opt_find(&swr_class, opt, NULL, 0,
532                                     AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
533         struct SwrContext *swr = swr_alloc();
534         int ret = av_opt_set(swr, opt, arg, 0);
535         swr_free(&swr);
536         if (ret < 0) {
537             av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
538             return ret;
539         }
540         av_dict_set(&swr_opts, opt, arg, FLAGS);
541         consumed = 1;
542     }
543 #endif
544 #if CONFIG_AVRESAMPLE
545     if ((o=av_opt_find(&rc, opt, NULL, 0,
546                        AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
547         av_dict_set(&resample_opts, opt, arg, FLAGS);
548         consumed = 1;
549     }
550 #endif
551
552     if (consumed)
553         return 0;
554     return AVERROR_OPTION_NOT_FOUND;
555 }
556
557 /*
558  * Check whether given option is a group separator.
559  *
560  * @return index of the group definition that matched or -1 if none
561  */
562 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
563                                  const char *opt)
564 {
565     int i;
566
567     for (i = 0; i < nb_groups; i++) {
568         const OptionGroupDef *p = &groups[i];
569         if (p->sep && !strcmp(p->sep, opt))
570             return i;
571     }
572
573     return -1;
574 }
575
576 /*
577  * Finish parsing an option group.
578  *
579  * @param group_idx which group definition should this group belong to
580  * @param arg argument of the group delimiting option
581  */
582 static void finish_group(OptionParseContext *octx, int group_idx,
583                          const char *arg)
584 {
585     OptionGroupList *l = &octx->groups[group_idx];
586     OptionGroup *g;
587
588     GROW_ARRAY(l->groups, l->nb_groups);
589     g = &l->groups[l->nb_groups - 1];
590
591     *g             = octx->cur_group;
592     g->arg         = arg;
593     g->group_def   = l->group_def;
594 #if CONFIG_SWSCALE
595     g->sws_opts    = sws_opts;
596 #endif
597     g->swr_opts    = swr_opts;
598     g->codec_opts  = codec_opts;
599     g->format_opts = format_opts;
600     g->resample_opts = resample_opts;
601
602     codec_opts  = NULL;
603     format_opts = NULL;
604     resample_opts = NULL;
605 #if CONFIG_SWSCALE
606     sws_opts    = NULL;
607 #endif
608     swr_opts    = NULL;
609     init_opts();
610
611     memset(&octx->cur_group, 0, sizeof(octx->cur_group));
612 }
613
614 /*
615  * Add an option instance to currently parsed group.
616  */
617 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
618                     const char *key, const char *val)
619 {
620     int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
621     OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
622
623     GROW_ARRAY(g->opts, g->nb_opts);
624     g->opts[g->nb_opts - 1].opt = opt;
625     g->opts[g->nb_opts - 1].key = key;
626     g->opts[g->nb_opts - 1].val = val;
627 }
628
629 static void init_parse_context(OptionParseContext *octx,
630                                const OptionGroupDef *groups, int nb_groups)
631 {
632     static const OptionGroupDef global_group = { "global" };
633     int i;
634
635     memset(octx, 0, sizeof(*octx));
636
637     octx->nb_groups = nb_groups;
638     octx->groups    = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
639     if (!octx->groups)
640         exit(1);
641
642     for (i = 0; i < octx->nb_groups; i++)
643         octx->groups[i].group_def = &groups[i];
644
645     octx->global_opts.group_def = &global_group;
646     octx->global_opts.arg       = "";
647
648     init_opts();
649 }
650
651 void uninit_parse_context(OptionParseContext *octx)
652 {
653     int i, j;
654
655     for (i = 0; i < octx->nb_groups; i++) {
656         OptionGroupList *l = &octx->groups[i];
657
658         for (j = 0; j < l->nb_groups; j++) {
659             av_freep(&l->groups[j].opts);
660             av_dict_free(&l->groups[j].codec_opts);
661             av_dict_free(&l->groups[j].format_opts);
662             av_dict_free(&l->groups[j].resample_opts);
663 #if CONFIG_SWSCALE
664             sws_freeContext(l->groups[j].sws_opts);
665 #endif
666             av_dict_free(&l->groups[j].swr_opts);
667         }
668         av_freep(&l->groups);
669     }
670     av_freep(&octx->groups);
671
672     av_freep(&octx->cur_group.opts);
673     av_freep(&octx->global_opts.opts);
674
675     uninit_opts();
676 }
677
678 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
679                       const OptionDef *options,
680                       const OptionGroupDef *groups, int nb_groups)
681 {
682     int optindex = 1;
683     int dashdash = -2;
684
685     /* perform system-dependent conversions for arguments list */
686     prepare_app_arguments(&argc, &argv);
687
688     init_parse_context(octx, groups, nb_groups);
689     av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
690
691     while (optindex < argc) {
692         const char *opt = argv[optindex++], *arg;
693         const OptionDef *po;
694         int ret;
695
696         av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
697
698         if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
699             dashdash = optindex;
700             continue;
701         }
702         /* unnamed group separators, e.g. output filename */
703         if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
704             finish_group(octx, 0, opt);
705             av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
706             continue;
707         }
708         opt++;
709
710 #define GET_ARG(arg)                                                           \
711 do {                                                                           \
712     arg = argv[optindex++];                                                    \
713     if (!arg) {                                                                \
714         av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
715         return AVERROR(EINVAL);                                                \
716     }                                                                          \
717 } while (0)
718
719         /* named group separators, e.g. -i */
720         if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
721             GET_ARG(arg);
722             finish_group(octx, ret, arg);
723             av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
724                    groups[ret].name, arg);
725             continue;
726         }
727
728         /* normal options */
729         po = find_option(options, opt);
730         if (po->name) {
731             if (po->flags & OPT_EXIT) {
732                 /* optional argument, e.g. -h */
733                 arg = argv[optindex++];
734             } else if (po->flags & HAS_ARG) {
735                 GET_ARG(arg);
736             } else {
737                 arg = "1";
738             }
739
740             add_opt(octx, po, opt, arg);
741             av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
742                    "argument '%s'.\n", po->name, po->help, arg);
743             continue;
744         }
745
746         /* AVOptions */
747         if (argv[optindex]) {
748             ret = opt_default(NULL, opt, argv[optindex]);
749             if (ret >= 0) {
750                 av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
751                        "argument '%s'.\n", opt, argv[optindex]);
752                 optindex++;
753                 continue;
754             } else if (ret != AVERROR_OPTION_NOT_FOUND) {
755                 av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
756                        "with argument '%s'.\n", opt, argv[optindex]);
757                 return ret;
758             }
759         }
760
761         /* boolean -nofoo options */
762         if (opt[0] == 'n' && opt[1] == 'o' &&
763             (po = find_option(options, opt + 2)) &&
764             po->name && po->flags & OPT_BOOL) {
765             add_opt(octx, po, opt, "0");
766             av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
767                    "argument 0.\n", po->name, po->help);
768             continue;
769         }
770
771         av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
772         return AVERROR_OPTION_NOT_FOUND;
773     }
774
775     if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
776         av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
777                "commandline.\n");
778
779     av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
780
781     return 0;
782 }
783
784 int opt_loglevel(void *optctx, const char *opt, const char *arg)
785 {
786     const struct { const char *name; int level; } log_levels[] = {
787         { "quiet"  , AV_LOG_QUIET   },
788         { "panic"  , AV_LOG_PANIC   },
789         { "fatal"  , AV_LOG_FATAL   },
790         { "error"  , AV_LOG_ERROR   },
791         { "warning", AV_LOG_WARNING },
792         { "info"   , AV_LOG_INFO    },
793         { "verbose", AV_LOG_VERBOSE },
794         { "debug"  , AV_LOG_DEBUG   },
795     };
796     char *tail;
797     int level;
798     int i;
799
800     tail = strstr(arg, "repeat");
801     av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED);
802     if (tail == arg)
803         arg += 6 + (arg[6]=='+');
804     if(tail && !*arg)
805         return 0;
806
807     for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
808         if (!strcmp(log_levels[i].name, arg)) {
809             av_log_set_level(log_levels[i].level);
810             return 0;
811         }
812     }
813
814     level = strtol(arg, &tail, 10);
815     if (*tail) {
816         av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
817                "Possible levels are numbers or:\n", arg);
818         for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
819             av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
820         exit(1);
821     }
822     av_log_set_level(level);
823     return 0;
824 }
825
826 static void expand_filename_template(AVBPrint *bp, const char *template,
827                                      struct tm *tm)
828 {
829     int c;
830
831     while ((c = *(template++))) {
832         if (c == '%') {
833             if (!(c = *(template++)))
834                 break;
835             switch (c) {
836             case 'p':
837                 av_bprintf(bp, "%s", program_name);
838                 break;
839             case 't':
840                 av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
841                            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
842                            tm->tm_hour, tm->tm_min, tm->tm_sec);
843                 break;
844             case '%':
845                 av_bprint_chars(bp, c, 1);
846                 break;
847             }
848         } else {
849             av_bprint_chars(bp, c, 1);
850         }
851     }
852 }
853
854 static int init_report(const char *env)
855 {
856     char *filename_template = NULL;
857     char *key, *val;
858     int ret, count = 0;
859     time_t now;
860     struct tm *tm;
861     AVBPrint filename;
862
863     if (report_file) /* already opened */
864         return 0;
865     time(&now);
866     tm = localtime(&now);
867
868     while (env && *env) {
869         if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
870             if (count)
871                 av_log(NULL, AV_LOG_ERROR,
872                        "Failed to parse FFREPORT environment variable: %s\n",
873                        av_err2str(ret));
874             break;
875         }
876         if (*env)
877             env++;
878         count++;
879         if (!strcmp(key, "file")) {
880             av_free(filename_template);
881             filename_template = val;
882             val = NULL;
883         } else {
884             av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
885         }
886         av_free(val);
887         av_free(key);
888     }
889
890     av_bprint_init(&filename, 0, 1);
891     expand_filename_template(&filename,
892                              av_x_if_null(filename_template, "%p-%t.log"), tm);
893     av_free(filename_template);
894     if (!av_bprint_is_complete(&filename)) {
895         av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
896         return AVERROR(ENOMEM);
897     }
898
899     report_file = fopen(filename.str, "w");
900     if (!report_file) {
901         av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
902                filename.str, strerror(errno));
903         return AVERROR(errno);
904     }
905     av_log_set_callback(log_callback_report);
906     av_log(NULL, AV_LOG_INFO,
907            "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
908            "Report written to \"%s\"\n",
909            program_name,
910            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
911            tm->tm_hour, tm->tm_min, tm->tm_sec,
912            filename.str);
913     av_bprint_finalize(&filename, NULL);
914     return 0;
915 }
916
917 int opt_report(const char *opt)
918 {
919     return init_report(NULL);
920 }
921
922 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
923 {
924     char *tail;
925     size_t max;
926
927     max = strtol(arg, &tail, 10);
928     if (*tail) {
929         av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
930         exit(1);
931     }
932     av_max_alloc(max);
933     return 0;
934 }
935
936 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
937 {
938     int ret;
939     unsigned flags = av_get_cpu_flags();
940
941     if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
942         return ret;
943
944     av_force_cpu_flags(flags);
945     return 0;
946 }
947
948 int opt_timelimit(void *optctx, const char *opt, const char *arg)
949 {
950 #if HAVE_SETRLIMIT
951     int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
952     struct rlimit rl = { lim, lim + 1 };
953     if (setrlimit(RLIMIT_CPU, &rl))
954         perror("setrlimit");
955 #else
956     av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
957 #endif
958     return 0;
959 }
960
961 #if CONFIG_OPENCL
962 int opt_opencl(void *optctx, const char *opt, const char *arg)
963 {
964     char *key, *value;
965     const char *opts = arg;
966     int ret = 0;
967     while (*opts) {
968         ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
969         if (ret < 0)
970             return ret;
971         ret = av_opencl_set_option(key, value);
972         if (ret < 0)
973             return ret;
974         if (*opts)
975             opts++;
976     }
977     return ret;
978 }
979 #endif
980
981 void print_error(const char *filename, int err)
982 {
983     char errbuf[128];
984     const char *errbuf_ptr = errbuf;
985
986     if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
987         errbuf_ptr = strerror(AVUNERROR(err));
988     av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
989 }
990
991 static int warned_cfg = 0;
992
993 #define INDENT        1
994 #define SHOW_VERSION  2
995 #define SHOW_CONFIG   4
996 #define SHOW_COPYRIGHT 8
997
998 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level)                  \
999     if (CONFIG_##LIBNAME) {                                             \
1000         const char *indent = flags & INDENT? "  " : "";                 \
1001         if (flags & SHOW_VERSION) {                                     \
1002             unsigned int version = libname##_version();                 \
1003             av_log(NULL, level,                                         \
1004                    "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",            \
1005                    indent, #libname,                                    \
1006                    LIB##LIBNAME##_VERSION_MAJOR,                        \
1007                    LIB##LIBNAME##_VERSION_MINOR,                        \
1008                    LIB##LIBNAME##_VERSION_MICRO,                        \
1009                    version >> 16, version >> 8 & 0xff, version & 0xff); \
1010         }                                                               \
1011         if (flags & SHOW_CONFIG) {                                      \
1012             const char *cfg = libname##_configuration();                \
1013             if (strcmp(FFMPEG_CONFIGURATION, cfg)) {                    \
1014                 if (!warned_cfg) {                                      \
1015                     av_log(NULL, level,                                 \
1016                             "%sWARNING: library configuration mismatch\n", \
1017                             indent);                                    \
1018                     warned_cfg = 1;                                     \
1019                 }                                                       \
1020                 av_log(NULL, level, "%s%-11s configuration: %s\n",      \
1021                         indent, #libname, cfg);                         \
1022             }                                                           \
1023         }                                                               \
1024     }                                                                   \
1025
1026 static void print_all_libs_info(int flags, int level)
1027 {
1028     PRINT_LIB_INFO(avutil,   AVUTIL,   flags, level);
1029     PRINT_LIB_INFO(avcodec,  AVCODEC,  flags, level);
1030     PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1031     PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1032     PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1033     PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1034     PRINT_LIB_INFO(swscale,  SWSCALE,  flags, level);
1035     PRINT_LIB_INFO(swresample,SWRESAMPLE,  flags, level);
1036     PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1037 }
1038
1039 static void print_program_info(int flags, int level)
1040 {
1041     const char *indent = flags & INDENT? "  " : "";
1042
1043     av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1044     if (flags & SHOW_COPYRIGHT)
1045         av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1046                program_birth_year, this_year);
1047     av_log(NULL, level, "\n");
1048     av_log(NULL, level, "%sbuilt on %s %s with %s\n",
1049            indent, __DATE__, __TIME__, CC_IDENT);
1050
1051     av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1052 }
1053
1054 void show_banner(int argc, char **argv, const OptionDef *options)
1055 {
1056     int idx = locate_option(argc, argv, options, "version");
1057     if (idx)
1058         return;
1059
1060     print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1061     print_all_libs_info(INDENT|SHOW_CONFIG,  AV_LOG_INFO);
1062     print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1063 }
1064
1065 int show_version(void *optctx, const char *opt, const char *arg)
1066 {
1067     av_log_set_callback(log_callback_help);
1068     print_program_info (0           , AV_LOG_INFO);
1069     print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1070
1071     return 0;
1072 }
1073
1074 int show_license(void *optctx, const char *opt, const char *arg)
1075 {
1076 #if CONFIG_NONFREE
1077     printf(
1078     "This version of %s has nonfree parts compiled in.\n"
1079     "Therefore it is not legally redistributable.\n",
1080     program_name );
1081 #elif CONFIG_GPLV3
1082     printf(
1083     "%s is free software; you can redistribute it and/or modify\n"
1084     "it under the terms of the GNU General Public License as published by\n"
1085     "the Free Software Foundation; either version 3 of the License, or\n"
1086     "(at your option) any later version.\n"
1087     "\n"
1088     "%s is distributed in the hope that it will be useful,\n"
1089     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1090     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1091     "GNU General Public License for more details.\n"
1092     "\n"
1093     "You should have received a copy of the GNU General Public License\n"
1094     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
1095     program_name, program_name, program_name );
1096 #elif CONFIG_GPL
1097     printf(
1098     "%s is free software; you can redistribute it and/or modify\n"
1099     "it under the terms of the GNU General Public License as published by\n"
1100     "the Free Software Foundation; either version 2 of the License, or\n"
1101     "(at your option) any later version.\n"
1102     "\n"
1103     "%s is distributed in the hope that it will be useful,\n"
1104     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1105     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1106     "GNU General Public License for more details.\n"
1107     "\n"
1108     "You should have received a copy of the GNU General Public License\n"
1109     "along with %s; if not, write to the Free Software\n"
1110     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1111     program_name, program_name, program_name );
1112 #elif CONFIG_LGPLV3
1113     printf(
1114     "%s is free software; you can redistribute it and/or modify\n"
1115     "it under the terms of the GNU Lesser General Public License as published by\n"
1116     "the Free Software Foundation; either version 3 of the License, or\n"
1117     "(at your option) any later version.\n"
1118     "\n"
1119     "%s is distributed in the hope that it will be useful,\n"
1120     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1121     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1122     "GNU Lesser General Public License for more details.\n"
1123     "\n"
1124     "You should have received a copy of the GNU Lesser General Public License\n"
1125     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
1126     program_name, program_name, program_name );
1127 #else
1128     printf(
1129     "%s is free software; you can redistribute it and/or\n"
1130     "modify it under the terms of the GNU Lesser General Public\n"
1131     "License as published by the Free Software Foundation; either\n"
1132     "version 2.1 of the License, or (at your option) any later version.\n"
1133     "\n"
1134     "%s is distributed in the hope that it will be useful,\n"
1135     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1136     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
1137     "Lesser General Public License for more details.\n"
1138     "\n"
1139     "You should have received a copy of the GNU Lesser General Public\n"
1140     "License along with %s; if not, write to the Free Software\n"
1141     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1142     program_name, program_name, program_name );
1143 #endif
1144
1145     return 0;
1146 }
1147
1148 int show_formats(void *optctx, const char *opt, const char *arg)
1149 {
1150     AVInputFormat *ifmt  = NULL;
1151     AVOutputFormat *ofmt = NULL;
1152     const char *last_name;
1153
1154     printf("File formats:\n"
1155            " D. = Demuxing supported\n"
1156            " .E = Muxing supported\n"
1157            " --\n");
1158     last_name = "000";
1159     for (;;) {
1160         int decode = 0;
1161         int encode = 0;
1162         const char *name      = NULL;
1163         const char *long_name = NULL;
1164
1165         while ((ofmt = av_oformat_next(ofmt))) {
1166             if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
1167                 strcmp(ofmt->name, last_name) > 0) {
1168                 name      = ofmt->name;
1169                 long_name = ofmt->long_name;
1170                 encode    = 1;
1171             }
1172         }
1173         while ((ifmt = av_iformat_next(ifmt))) {
1174             if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
1175                 strcmp(ifmt->name, last_name) > 0) {
1176                 name      = ifmt->name;
1177                 long_name = ifmt->long_name;
1178                 encode    = 0;
1179             }
1180             if (name && strcmp(ifmt->name, name) == 0)
1181                 decode = 1;
1182         }
1183         if (name == NULL)
1184             break;
1185         last_name = name;
1186
1187         printf(" %s%s %-15s %s\n",
1188                decode ? "D" : " ",
1189                encode ? "E" : " ",
1190                name,
1191             long_name ? long_name:" ");
1192     }
1193     return 0;
1194 }
1195
1196 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1197     if (codec->field) {                                                      \
1198         const type *p = codec->field;                                        \
1199                                                                              \
1200         printf("    Supported " list_name ":");                              \
1201         while (*p != term) {                                                 \
1202             get_name(*p);                                                    \
1203             printf(" %s", name);                                             \
1204             p++;                                                             \
1205         }                                                                    \
1206         printf("\n");                                                        \
1207     }                                                                        \
1208
1209 static void print_codec(const AVCodec *c)
1210 {
1211     int encoder = av_codec_is_encoder(c);
1212
1213     printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1214            c->long_name ? c->long_name : "");
1215
1216     if (c->type == AVMEDIA_TYPE_VIDEO ||
1217         c->type == AVMEDIA_TYPE_AUDIO) {
1218         printf("    Threading capabilities: ");
1219         switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
1220                                    CODEC_CAP_SLICE_THREADS)) {
1221         case CODEC_CAP_FRAME_THREADS |
1222              CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1223         case CODEC_CAP_FRAME_THREADS: printf("frame");           break;
1224         case CODEC_CAP_SLICE_THREADS: printf("slice");           break;
1225         default:                      printf("no");              break;
1226         }
1227         printf("\n");
1228     }
1229
1230     if (c->supported_framerates) {
1231         const AVRational *fps = c->supported_framerates;
1232
1233         printf("    Supported framerates:");
1234         while (fps->num) {
1235             printf(" %d/%d", fps->num, fps->den);
1236             fps++;
1237         }
1238         printf("\n");
1239     }
1240     PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1241                           AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1242     PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1243                           GET_SAMPLE_RATE_NAME);
1244     PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1245                           AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1246     PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1247                           0, GET_CH_LAYOUT_DESC);
1248
1249     if (c->priv_class) {
1250         show_help_children(c->priv_class,
1251                            AV_OPT_FLAG_ENCODING_PARAM |
1252                            AV_OPT_FLAG_DECODING_PARAM);
1253     }
1254 }
1255
1256 static char get_media_type_char(enum AVMediaType type)
1257 {
1258     switch (type) {
1259         case AVMEDIA_TYPE_VIDEO:    return 'V';
1260         case AVMEDIA_TYPE_AUDIO:    return 'A';
1261         case AVMEDIA_TYPE_DATA:     return 'D';
1262         case AVMEDIA_TYPE_SUBTITLE: return 'S';
1263         case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1264         default:                    return '?';
1265     }
1266 }
1267
1268 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1269                                         int encoder)
1270 {
1271     while ((prev = av_codec_next(prev))) {
1272         if (prev->id == id &&
1273             (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1274             return prev;
1275     }
1276     return NULL;
1277 }
1278
1279 static int compare_codec_desc(const void *a, const void *b)
1280 {
1281     const AVCodecDescriptor * const *da = a;
1282     const AVCodecDescriptor * const *db = b;
1283
1284     return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
1285            strcmp((*da)->name, (*db)->name);
1286 }
1287
1288 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1289 {
1290     const AVCodecDescriptor *desc = NULL;
1291     const AVCodecDescriptor **codecs;
1292     unsigned nb_codecs = 0, i = 0;
1293
1294     while ((desc = avcodec_descriptor_next(desc)))
1295         nb_codecs++;
1296     if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1297         av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1298         exit(1);
1299     }
1300     desc = NULL;
1301     while ((desc = avcodec_descriptor_next(desc)))
1302         codecs[i++] = desc;
1303     av_assert0(i == nb_codecs);
1304     qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1305     *rcodecs = codecs;
1306     return nb_codecs;
1307 }
1308
1309 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1310 {
1311     const AVCodec *codec = NULL;
1312
1313     printf(" (%s: ", encoder ? "encoders" : "decoders");
1314
1315     while ((codec = next_codec_for_id(id, codec, encoder)))
1316         printf("%s ", codec->name);
1317
1318     printf(")");
1319 }
1320
1321 int show_codecs(void *optctx, const char *opt, const char *arg)
1322 {
1323     const AVCodecDescriptor **codecs;
1324     unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1325
1326     printf("Codecs:\n"
1327            " D..... = Decoding supported\n"
1328            " .E.... = Encoding supported\n"
1329            " ..V... = Video codec\n"
1330            " ..A... = Audio codec\n"
1331            " ..S... = Subtitle codec\n"
1332            " ...I.. = Intra frame-only codec\n"
1333            " ....L. = Lossy compression\n"
1334            " .....S = Lossless compression\n"
1335            " -------\n");
1336     for (i = 0; i < nb_codecs; i++) {
1337         const AVCodecDescriptor *desc = codecs[i];
1338         const AVCodec *codec = NULL;
1339
1340         printf(" ");
1341         printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1342         printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1343
1344         printf("%c", get_media_type_char(desc->type));
1345         printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1346         printf((desc->props & AV_CODEC_PROP_LOSSY)      ? "L" : ".");
1347         printf((desc->props & AV_CODEC_PROP_LOSSLESS)   ? "S" : ".");
1348
1349         printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1350
1351         /* print decoders/encoders when there's more than one or their
1352          * names are different from codec name */
1353         while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1354             if (strcmp(codec->name, desc->name)) {
1355                 print_codecs_for_id(desc->id, 0);
1356                 break;
1357             }
1358         }
1359         codec = NULL;
1360         while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1361             if (strcmp(codec->name, desc->name)) {
1362                 print_codecs_for_id(desc->id, 1);
1363                 break;
1364             }
1365         }
1366
1367         printf("\n");
1368     }
1369     av_free(codecs);
1370     return 0;
1371 }
1372
1373 static void print_codecs(int encoder)
1374 {
1375     const AVCodecDescriptor **codecs;
1376     unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1377
1378     printf("%s:\n"
1379            " V..... = Video\n"
1380            " A..... = Audio\n"
1381            " S..... = Subtitle\n"
1382            " .F.... = Frame-level multithreading\n"
1383            " ..S... = Slice-level multithreading\n"
1384            " ...X.. = Codec is experimental\n"
1385            " ....B. = Supports draw_horiz_band\n"
1386            " .....D = Supports direct rendering method 1\n"
1387            " ------\n",
1388            encoder ? "Encoders" : "Decoders");
1389     for (i = 0; i < nb_codecs; i++) {
1390         const AVCodecDescriptor *desc = codecs[i];
1391         const AVCodec *codec = NULL;
1392
1393         while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1394             printf(" %c", get_media_type_char(desc->type));
1395             printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1396             printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1397             printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL)  ? "X" : ".");
1398             printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1399             printf((codec->capabilities & CODEC_CAP_DR1)           ? "D" : ".");
1400
1401             printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1402             if (strcmp(codec->name, desc->name))
1403                 printf(" (codec %s)", desc->name);
1404
1405             printf("\n");
1406         }
1407     }
1408     av_free(codecs);
1409 }
1410
1411 int show_decoders(void *optctx, const char *opt, const char *arg)
1412 {
1413     print_codecs(0);
1414     return 0;
1415 }
1416
1417 int show_encoders(void *optctx, const char *opt, const char *arg)
1418 {
1419     print_codecs(1);
1420     return 0;
1421 }
1422
1423 int show_bsfs(void *optctx, const char *opt, const char *arg)
1424 {
1425     AVBitStreamFilter *bsf = NULL;
1426
1427     printf("Bitstream filters:\n");
1428     while ((bsf = av_bitstream_filter_next(bsf)))
1429         printf("%s\n", bsf->name);
1430     printf("\n");
1431     return 0;
1432 }
1433
1434 int show_protocols(void *optctx, const char *opt, const char *arg)
1435 {
1436     void *opaque = NULL;
1437     const char *name;
1438
1439     printf("Supported file protocols:\n"
1440            "Input:\n");
1441     while ((name = avio_enum_protocols(&opaque, 0)))
1442         printf("%s\n", name);
1443     printf("Output:\n");
1444     while ((name = avio_enum_protocols(&opaque, 1)))
1445         printf("%s\n", name);
1446     return 0;
1447 }
1448
1449 int show_filters(void *optctx, const char *opt, const char *arg)
1450 {
1451     const AVFilter av_unused(*filter) = NULL;
1452     char descr[64], *descr_cur;
1453     int i, j;
1454     const AVFilterPad *pad;
1455
1456     printf("Filters:\n"
1457            "  T. = Timeline support\n"
1458            "  .S = Slice threading\n"
1459            "  A = Audio input/output\n"
1460            "  V = Video input/output\n"
1461            "  N = Dynamic number and/or type of input/output\n"
1462            "  | = Source or sink filter\n");
1463 #if CONFIG_AVFILTER
1464     while ((filter = avfilter_next(filter))) {
1465         descr_cur = descr;
1466         for (i = 0; i < 2; i++) {
1467             if (i) {
1468                 *(descr_cur++) = '-';
1469                 *(descr_cur++) = '>';
1470             }
1471             pad = i ? filter->outputs : filter->inputs;
1472             for (j = 0; pad && pad[j].name; j++) {
1473                 if (descr_cur >= descr + sizeof(descr) - 4)
1474                     break;
1475                 *(descr_cur++) = get_media_type_char(pad[j].type);
1476             }
1477             if (!j)
1478                 *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1479                                   ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1480         }
1481         *descr_cur = 0;
1482         printf(" %c%c %-16s %-10s %s\n",
1483                filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1484                filter->flags & AVFILTER_FLAG_SLICE_THREADS    ? 'S' : '.',
1485                filter->name, descr, filter->description);
1486     }
1487 #endif
1488     return 0;
1489 }
1490
1491 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1492 {
1493     const AVPixFmtDescriptor *pix_desc = NULL;
1494
1495     printf("Pixel formats:\n"
1496            "I.... = Supported Input  format for conversion\n"
1497            ".O... = Supported Output format for conversion\n"
1498            "..H.. = Hardware accelerated format\n"
1499            "...P. = Paletted format\n"
1500            "....B = Bitstream format\n"
1501            "FLAGS NAME            NB_COMPONENTS BITS_PER_PIXEL\n"
1502            "-----\n");
1503
1504 #if !CONFIG_SWSCALE
1505 #   define sws_isSupportedInput(x)  0
1506 #   define sws_isSupportedOutput(x) 0
1507 #endif
1508
1509     while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1510         enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1511         printf("%c%c%c%c%c %-16s       %d            %2d\n",
1512                sws_isSupportedInput (pix_fmt)              ? 'I' : '.',
1513                sws_isSupportedOutput(pix_fmt)              ? 'O' : '.',
1514                pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL   ? 'H' : '.',
1515                pix_desc->flags & AV_PIX_FMT_FLAG_PAL       ? 'P' : '.',
1516                pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1517                pix_desc->name,
1518                pix_desc->nb_components,
1519                av_get_bits_per_pixel(pix_desc));
1520     }
1521     return 0;
1522 }
1523
1524 int show_layouts(void *optctx, const char *opt, const char *arg)
1525 {
1526     int i = 0;
1527     uint64_t layout, j;
1528     const char *name, *descr;
1529
1530     printf("Individual channels:\n"
1531            "NAME        DESCRIPTION\n");
1532     for (i = 0; i < 63; i++) {
1533         name = av_get_channel_name((uint64_t)1 << i);
1534         if (!name)
1535             continue;
1536         descr = av_get_channel_description((uint64_t)1 << i);
1537         printf("%-12s%s\n", name, descr);
1538     }
1539     printf("\nStandard channel layouts:\n"
1540            "NAME        DECOMPOSITION\n");
1541     for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1542         if (name) {
1543             printf("%-12s", name);
1544             for (j = 1; j; j <<= 1)
1545                 if ((layout & j))
1546                     printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1547             printf("\n");
1548         }
1549     }
1550     return 0;
1551 }
1552
1553 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1554 {
1555     int i;
1556     char fmt_str[128];
1557     for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1558         printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1559     return 0;
1560 }
1561
1562 static void show_help_codec(const char *name, int encoder)
1563 {
1564     const AVCodecDescriptor *desc;
1565     const AVCodec *codec;
1566
1567     if (!name) {
1568         av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1569         return;
1570     }
1571
1572     codec = encoder ? avcodec_find_encoder_by_name(name) :
1573                       avcodec_find_decoder_by_name(name);
1574
1575     if (codec)
1576         print_codec(codec);
1577     else if ((desc = avcodec_descriptor_get_by_name(name))) {
1578         int printed = 0;
1579
1580         while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1581             printed = 1;
1582             print_codec(codec);
1583         }
1584
1585         if (!printed) {
1586             av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1587                    "but no %s for it are available. FFmpeg might need to be "
1588                    "recompiled with additional external libraries.\n",
1589                    name, encoder ? "encoders" : "decoders");
1590         }
1591     } else {
1592         av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1593                name);
1594     }
1595 }
1596
1597 static void show_help_demuxer(const char *name)
1598 {
1599     const AVInputFormat *fmt = av_find_input_format(name);
1600
1601     if (!fmt) {
1602         av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1603         return;
1604     }
1605
1606     printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1607
1608     if (fmt->extensions)
1609         printf("    Common extensions: %s.\n", fmt->extensions);
1610
1611     if (fmt->priv_class)
1612         show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1613 }
1614
1615 static void show_help_muxer(const char *name)
1616 {
1617     const AVCodecDescriptor *desc;
1618     const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1619
1620     if (!fmt) {
1621         av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1622         return;
1623     }
1624
1625     printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1626
1627     if (fmt->extensions)
1628         printf("    Common extensions: %s.\n", fmt->extensions);
1629     if (fmt->mime_type)
1630         printf("    Mime type: %s.\n", fmt->mime_type);
1631     if (fmt->video_codec != AV_CODEC_ID_NONE &&
1632         (desc = avcodec_descriptor_get(fmt->video_codec))) {
1633         printf("    Default video codec: %s.\n", desc->name);
1634     }
1635     if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1636         (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1637         printf("    Default audio codec: %s.\n", desc->name);
1638     }
1639     if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1640         (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1641         printf("    Default subtitle codec: %s.\n", desc->name);
1642     }
1643
1644     if (fmt->priv_class)
1645         show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1646 }
1647
1648 #if CONFIG_AVFILTER
1649 static void show_help_filter(const char *name)
1650 {
1651 #if CONFIG_AVFILTER
1652     const AVFilter *f = avfilter_get_by_name(name);
1653     int i, count;
1654
1655     if (!name) {
1656         av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1657         return;
1658     } else if (!f) {
1659         av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1660         return;
1661     }
1662
1663     printf("Filter %s\n", f->name);
1664     if (f->description)
1665         printf("  %s\n", f->description);
1666
1667     if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1668         printf("    slice threading supported\n");
1669
1670     printf("    Inputs:\n");
1671     count = avfilter_pad_count(f->inputs);
1672     for (i = 0; i < count; i++) {
1673         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1674                media_type_string(avfilter_pad_get_type(f->inputs, i)));
1675     }
1676     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1677         printf("        dynamic (depending on the options)\n");
1678     else if (!count)
1679         printf("        none (source filter)\n");
1680
1681     printf("    Outputs:\n");
1682     count = avfilter_pad_count(f->outputs);
1683     for (i = 0; i < count; i++) {
1684         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1685                media_type_string(avfilter_pad_get_type(f->outputs, i)));
1686     }
1687     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1688         printf("        dynamic (depending on the options)\n");
1689     else if (!count)
1690         printf("        none (sink filter)\n");
1691
1692     if (f->priv_class)
1693         show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1694                                           AV_OPT_FLAG_AUDIO_PARAM);
1695     if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1696         printf("This filter has support for timeline through the 'enable' option.\n");
1697 #else
1698     av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1699            "can not to satisfy request\n");
1700 #endif
1701 }
1702 #endif
1703
1704 int show_help(void *optctx, const char *opt, const char *arg)
1705 {
1706     char *topic, *par;
1707     av_log_set_callback(log_callback_help);
1708
1709     topic = av_strdup(arg ? arg : "");
1710     par = strchr(topic, '=');
1711     if (par)
1712         *par++ = 0;
1713
1714     if (!*topic) {
1715         show_help_default(topic, par);
1716     } else if (!strcmp(topic, "decoder")) {
1717         show_help_codec(par, 0);
1718     } else if (!strcmp(topic, "encoder")) {
1719         show_help_codec(par, 1);
1720     } else if (!strcmp(topic, "demuxer")) {
1721         show_help_demuxer(par);
1722     } else if (!strcmp(topic, "muxer")) {
1723         show_help_muxer(par);
1724 #if CONFIG_AVFILTER
1725     } else if (!strcmp(topic, "filter")) {
1726         show_help_filter(par);
1727 #endif
1728     } else {
1729         show_help_default(topic, par);
1730     }
1731
1732     av_freep(&topic);
1733     return 0;
1734 }
1735
1736 int read_yesno(void)
1737 {
1738     int c = getchar();
1739     int yesno = (av_toupper(c) == 'Y');
1740
1741     while (c != '\n' && c != EOF)
1742         c = getchar();
1743
1744     return yesno;
1745 }
1746
1747 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1748 {
1749     int ret;
1750     FILE *f = fopen(filename, "rb");
1751
1752     if (!f) {
1753         av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1754                strerror(errno));
1755         return AVERROR(errno);
1756     }
1757     fseek(f, 0, SEEK_END);
1758     *size = ftell(f);
1759     fseek(f, 0, SEEK_SET);
1760     if (*size == (size_t)-1) {
1761         av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1762         fclose(f);
1763         return AVERROR(errno);
1764     }
1765     *bufptr = av_malloc(*size + 1);
1766     if (!*bufptr) {
1767         av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1768         fclose(f);
1769         return AVERROR(ENOMEM);
1770     }
1771     ret = fread(*bufptr, 1, *size, f);
1772     if (ret < *size) {
1773         av_free(*bufptr);
1774         if (ferror(f)) {
1775             av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1776                    filename, strerror(errno));
1777             ret = AVERROR(errno);
1778         } else
1779             ret = AVERROR_EOF;
1780     } else {
1781         ret = 0;
1782         (*bufptr)[(*size)++] = '\0';
1783     }
1784
1785     fclose(f);
1786     return ret;
1787 }
1788
1789 FILE *get_preset_file(char *filename, size_t filename_size,
1790                       const char *preset_name, int is_path,
1791                       const char *codec_name)
1792 {
1793     FILE *f = NULL;
1794     int i;
1795     const char *base[3] = { getenv("FFMPEG_DATADIR"),
1796                             getenv("HOME"),
1797                             FFMPEG_DATADIR, };
1798
1799     if (is_path) {
1800         av_strlcpy(filename, preset_name, filename_size);
1801         f = fopen(filename, "r");
1802     } else {
1803 #ifdef _WIN32
1804         char datadir[MAX_PATH], *ls;
1805         base[2] = NULL;
1806
1807         if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1808         {
1809             for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1810                 if (*ls == '\\') *ls = '/';
1811
1812             if (ls = strrchr(datadir, '/'))
1813             {
1814                 *ls = 0;
1815                 strncat(datadir, "/ffpresets",  sizeof(datadir) - 1 - strlen(datadir));
1816                 base[2] = datadir;
1817             }
1818         }
1819 #endif
1820         for (i = 0; i < 3 && !f; i++) {
1821             if (!base[i])
1822                 continue;
1823             snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1824                      i != 1 ? "" : "/.ffmpeg", preset_name);
1825             f = fopen(filename, "r");
1826             if (!f && codec_name) {
1827                 snprintf(filename, filename_size,
1828                          "%s%s/%s-%s.ffpreset",
1829                          base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1830                          preset_name);
1831                 f = fopen(filename, "r");
1832             }
1833         }
1834     }
1835
1836     return f;
1837 }
1838
1839 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1840 {
1841     int ret = avformat_match_stream_specifier(s, st, spec);
1842     if (ret < 0)
1843         av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1844     return ret;
1845 }
1846
1847 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1848                                 AVFormatContext *s, AVStream *st, AVCodec *codec)
1849 {
1850     AVDictionary    *ret = NULL;
1851     AVDictionaryEntry *t = NULL;
1852     int            flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1853                                       : AV_OPT_FLAG_DECODING_PARAM;
1854     char          prefix = 0;
1855     const AVClass    *cc = avcodec_get_class();
1856
1857     if (!codec)
1858         codec            = s->oformat ? avcodec_find_encoder(codec_id)
1859                                       : avcodec_find_decoder(codec_id);
1860
1861     switch (st->codec->codec_type) {
1862     case AVMEDIA_TYPE_VIDEO:
1863         prefix  = 'v';
1864         flags  |= AV_OPT_FLAG_VIDEO_PARAM;
1865         break;
1866     case AVMEDIA_TYPE_AUDIO:
1867         prefix  = 'a';
1868         flags  |= AV_OPT_FLAG_AUDIO_PARAM;
1869         break;
1870     case AVMEDIA_TYPE_SUBTITLE:
1871         prefix  = 's';
1872         flags  |= AV_OPT_FLAG_SUBTITLE_PARAM;
1873         break;
1874     }
1875
1876     while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1877         char *p = strchr(t->key, ':');
1878
1879         /* check stream specification in opt name */
1880         if (p)
1881             switch (check_stream_specifier(s, st, p + 1)) {
1882             case  1: *p = 0; break;
1883             case  0:         continue;
1884             default:         return NULL;
1885             }
1886
1887         if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1888             (codec && codec->priv_class &&
1889              av_opt_find(&codec->priv_class, t->key, NULL, flags,
1890                          AV_OPT_SEARCH_FAKE_OBJ)))
1891             av_dict_set(&ret, t->key, t->value, 0);
1892         else if (t->key[0] == prefix &&
1893                  av_opt_find(&cc, t->key + 1, NULL, flags,
1894                              AV_OPT_SEARCH_FAKE_OBJ))
1895             av_dict_set(&ret, t->key + 1, t->value, 0);
1896
1897         if (p)
1898             *p = ':';
1899     }
1900     return ret;
1901 }
1902
1903 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1904                                            AVDictionary *codec_opts)
1905 {
1906     int i;
1907     AVDictionary **opts;
1908
1909     if (!s->nb_streams)
1910         return NULL;
1911     opts = av_mallocz(s->nb_streams * sizeof(*opts));
1912     if (!opts) {
1913         av_log(NULL, AV_LOG_ERROR,
1914                "Could not alloc memory for stream options.\n");
1915         return NULL;
1916     }
1917     for (i = 0; i < s->nb_streams; i++)
1918         opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1919                                     s, s->streams[i], NULL);
1920     return opts;
1921 }
1922
1923 void *grow_array(void *array, int elem_size, int *size, int new_size)
1924 {
1925     if (new_size >= INT_MAX / elem_size) {
1926         av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1927         exit(1);
1928     }
1929     if (*size < new_size) {
1930         uint8_t *tmp = av_realloc(array, new_size*elem_size);
1931         if (!tmp) {
1932             av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1933             exit(1);
1934         }
1935         memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1936         *size = new_size;
1937         return tmp;
1938     }
1939     return array;
1940 }