]> git.sesse.net Git - ffmpeg/blob - cmdutils.c
Merge remote-tracking branch 'qatar/master'
[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_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
914     av_bprint_finalize(&filename, NULL);
915     return 0;
916 }
917
918 int opt_report(const char *opt)
919 {
920     return init_report(NULL);
921 }
922
923 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
924 {
925     char *tail;
926     size_t max;
927
928     max = strtol(arg, &tail, 10);
929     if (*tail) {
930         av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
931         exit(1);
932     }
933     av_max_alloc(max);
934     return 0;
935 }
936
937 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
938 {
939     int ret;
940     unsigned flags = av_get_cpu_flags();
941
942     if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
943         return ret;
944
945     av_force_cpu_flags(flags);
946     return 0;
947 }
948
949 int opt_timelimit(void *optctx, const char *opt, const char *arg)
950 {
951 #if HAVE_SETRLIMIT
952     int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
953     struct rlimit rl = { lim, lim + 1 };
954     if (setrlimit(RLIMIT_CPU, &rl))
955         perror("setrlimit");
956 #else
957     av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
958 #endif
959     return 0;
960 }
961
962 #if CONFIG_OPENCL
963 int opt_opencl(void *optctx, const char *opt, const char *arg)
964 {
965     char *key, *value;
966     const char *opts = arg;
967     int ret = 0;
968     while (*opts) {
969         ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
970         if (ret < 0)
971             return ret;
972         ret = av_opencl_set_option(key, value);
973         if (ret < 0)
974             return ret;
975         if (*opts)
976             opts++;
977     }
978     return ret;
979 }
980 #endif
981
982 void print_error(const char *filename, int err)
983 {
984     char errbuf[128];
985     const char *errbuf_ptr = errbuf;
986
987     if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
988         errbuf_ptr = strerror(AVUNERROR(err));
989     av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
990 }
991
992 static int warned_cfg = 0;
993
994 #define INDENT        1
995 #define SHOW_VERSION  2
996 #define SHOW_CONFIG   4
997 #define SHOW_COPYRIGHT 8
998
999 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level)                  \
1000     if (CONFIG_##LIBNAME) {                                             \
1001         const char *indent = flags & INDENT? "  " : "";                 \
1002         if (flags & SHOW_VERSION) {                                     \
1003             unsigned int version = libname##_version();                 \
1004             av_log(NULL, level,                                         \
1005                    "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n",            \
1006                    indent, #libname,                                    \
1007                    LIB##LIBNAME##_VERSION_MAJOR,                        \
1008                    LIB##LIBNAME##_VERSION_MINOR,                        \
1009                    LIB##LIBNAME##_VERSION_MICRO,                        \
1010                    version >> 16, version >> 8 & 0xff, version & 0xff); \
1011         }                                                               \
1012         if (flags & SHOW_CONFIG) {                                      \
1013             const char *cfg = libname##_configuration();                \
1014             if (strcmp(FFMPEG_CONFIGURATION, cfg)) {                    \
1015                 if (!warned_cfg) {                                      \
1016                     av_log(NULL, level,                                 \
1017                             "%sWARNING: library configuration mismatch\n", \
1018                             indent);                                    \
1019                     warned_cfg = 1;                                     \
1020                 }                                                       \
1021                 av_log(NULL, level, "%s%-11s configuration: %s\n",      \
1022                         indent, #libname, cfg);                         \
1023             }                                                           \
1024         }                                                               \
1025     }                                                                   \
1026
1027 static void print_all_libs_info(int flags, int level)
1028 {
1029     PRINT_LIB_INFO(avutil,   AVUTIL,   flags, level);
1030     PRINT_LIB_INFO(avcodec,  AVCODEC,  flags, level);
1031     PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1032     PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1033     PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1034     PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
1035     PRINT_LIB_INFO(swscale,  SWSCALE,  flags, level);
1036     PRINT_LIB_INFO(swresample,SWRESAMPLE,  flags, level);
1037     PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
1038 }
1039
1040 static void print_program_info(int flags, int level)
1041 {
1042     const char *indent = flags & INDENT? "  " : "";
1043
1044     av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1045     if (flags & SHOW_COPYRIGHT)
1046         av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1047                program_birth_year, this_year);
1048     av_log(NULL, level, "\n");
1049     av_log(NULL, level, "%sbuilt on %s %s with %s\n",
1050            indent, __DATE__, __TIME__, CC_IDENT);
1051
1052     av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1053 }
1054
1055 void show_banner(int argc, char **argv, const OptionDef *options)
1056 {
1057     int idx = locate_option(argc, argv, options, "version");
1058     if (idx)
1059         return;
1060
1061     print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1062     print_all_libs_info(INDENT|SHOW_CONFIG,  AV_LOG_INFO);
1063     print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1064 }
1065
1066 int show_version(void *optctx, const char *opt, const char *arg)
1067 {
1068     av_log_set_callback(log_callback_help);
1069     print_program_info (0           , AV_LOG_INFO);
1070     print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1071
1072     return 0;
1073 }
1074
1075 int show_license(void *optctx, const char *opt, const char *arg)
1076 {
1077 #if CONFIG_NONFREE
1078     printf(
1079     "This version of %s has nonfree parts compiled in.\n"
1080     "Therefore it is not legally redistributable.\n",
1081     program_name );
1082 #elif CONFIG_GPLV3
1083     printf(
1084     "%s is free software; you can redistribute it and/or modify\n"
1085     "it under the terms of the GNU General Public License as published by\n"
1086     "the Free Software Foundation; either version 3 of the License, or\n"
1087     "(at your option) any later version.\n"
1088     "\n"
1089     "%s is distributed in the hope that it will be useful,\n"
1090     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1091     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1092     "GNU General Public License for more details.\n"
1093     "\n"
1094     "You should have received a copy of the GNU General Public License\n"
1095     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
1096     program_name, program_name, program_name );
1097 #elif CONFIG_GPL
1098     printf(
1099     "%s is free software; you can redistribute it and/or modify\n"
1100     "it under the terms of the GNU General Public License as published by\n"
1101     "the Free Software Foundation; either version 2 of the License, or\n"
1102     "(at your option) any later version.\n"
1103     "\n"
1104     "%s is distributed in the hope that it will be useful,\n"
1105     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1106     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1107     "GNU General Public License for more details.\n"
1108     "\n"
1109     "You should have received a copy of the GNU General Public License\n"
1110     "along with %s; if not, write to the Free Software\n"
1111     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1112     program_name, program_name, program_name );
1113 #elif CONFIG_LGPLV3
1114     printf(
1115     "%s is free software; you can redistribute it and/or modify\n"
1116     "it under the terms of the GNU Lesser General Public License as published by\n"
1117     "the Free Software Foundation; either version 3 of the License, or\n"
1118     "(at your option) any later version.\n"
1119     "\n"
1120     "%s is distributed in the hope that it will be useful,\n"
1121     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1122     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1123     "GNU Lesser General Public License for more details.\n"
1124     "\n"
1125     "You should have received a copy of the GNU Lesser General Public License\n"
1126     "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
1127     program_name, program_name, program_name );
1128 #else
1129     printf(
1130     "%s is free software; you can redistribute it and/or\n"
1131     "modify it under the terms of the GNU Lesser General Public\n"
1132     "License as published by the Free Software Foundation; either\n"
1133     "version 2.1 of the License, or (at your option) any later version.\n"
1134     "\n"
1135     "%s is distributed in the hope that it will be useful,\n"
1136     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1137     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
1138     "Lesser General Public License for more details.\n"
1139     "\n"
1140     "You should have received a copy of the GNU Lesser General Public\n"
1141     "License along with %s; if not, write to the Free Software\n"
1142     "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1143     program_name, program_name, program_name );
1144 #endif
1145
1146     return 0;
1147 }
1148
1149 int show_formats(void *optctx, const char *opt, const char *arg)
1150 {
1151     AVInputFormat *ifmt  = NULL;
1152     AVOutputFormat *ofmt = NULL;
1153     const char *last_name;
1154
1155     printf("File formats:\n"
1156            " D. = Demuxing supported\n"
1157            " .E = Muxing supported\n"
1158            " --\n");
1159     last_name = "000";
1160     for (;;) {
1161         int decode = 0;
1162         int encode = 0;
1163         const char *name      = NULL;
1164         const char *long_name = NULL;
1165
1166         while ((ofmt = av_oformat_next(ofmt))) {
1167             if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
1168                 strcmp(ofmt->name, last_name) > 0) {
1169                 name      = ofmt->name;
1170                 long_name = ofmt->long_name;
1171                 encode    = 1;
1172             }
1173         }
1174         while ((ifmt = av_iformat_next(ifmt))) {
1175             if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
1176                 strcmp(ifmt->name, last_name) > 0) {
1177                 name      = ifmt->name;
1178                 long_name = ifmt->long_name;
1179                 encode    = 0;
1180             }
1181             if (name && strcmp(ifmt->name, name) == 0)
1182                 decode = 1;
1183         }
1184         if (name == NULL)
1185             break;
1186         last_name = name;
1187
1188         printf(" %s%s %-15s %s\n",
1189                decode ? "D" : " ",
1190                encode ? "E" : " ",
1191                name,
1192             long_name ? long_name:" ");
1193     }
1194     return 0;
1195 }
1196
1197 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1198     if (codec->field) {                                                      \
1199         const type *p = codec->field;                                        \
1200                                                                              \
1201         printf("    Supported " list_name ":");                              \
1202         while (*p != term) {                                                 \
1203             get_name(*p);                                                    \
1204             printf(" %s", name);                                             \
1205             p++;                                                             \
1206         }                                                                    \
1207         printf("\n");                                                        \
1208     }                                                                        \
1209
1210 static void print_codec(const AVCodec *c)
1211 {
1212     int encoder = av_codec_is_encoder(c);
1213
1214     printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1215            c->long_name ? c->long_name : "");
1216
1217     if (c->type == AVMEDIA_TYPE_VIDEO ||
1218         c->type == AVMEDIA_TYPE_AUDIO) {
1219         printf("    Threading capabilities: ");
1220         switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
1221                                    CODEC_CAP_SLICE_THREADS)) {
1222         case CODEC_CAP_FRAME_THREADS |
1223              CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1224         case CODEC_CAP_FRAME_THREADS: printf("frame");           break;
1225         case CODEC_CAP_SLICE_THREADS: printf("slice");           break;
1226         default:                      printf("no");              break;
1227         }
1228         printf("\n");
1229     }
1230
1231     if (c->supported_framerates) {
1232         const AVRational *fps = c->supported_framerates;
1233
1234         printf("    Supported framerates:");
1235         while (fps->num) {
1236             printf(" %d/%d", fps->num, fps->den);
1237             fps++;
1238         }
1239         printf("\n");
1240     }
1241     PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1242                           AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1243     PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1244                           GET_SAMPLE_RATE_NAME);
1245     PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1246                           AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1247     PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1248                           0, GET_CH_LAYOUT_DESC);
1249
1250     if (c->priv_class) {
1251         show_help_children(c->priv_class,
1252                            AV_OPT_FLAG_ENCODING_PARAM |
1253                            AV_OPT_FLAG_DECODING_PARAM);
1254     }
1255 }
1256
1257 static char get_media_type_char(enum AVMediaType type)
1258 {
1259     switch (type) {
1260         case AVMEDIA_TYPE_VIDEO:    return 'V';
1261         case AVMEDIA_TYPE_AUDIO:    return 'A';
1262         case AVMEDIA_TYPE_DATA:     return 'D';
1263         case AVMEDIA_TYPE_SUBTITLE: return 'S';
1264         case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1265         default:                    return '?';
1266     }
1267 }
1268
1269 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1270                                         int encoder)
1271 {
1272     while ((prev = av_codec_next(prev))) {
1273         if (prev->id == id &&
1274             (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1275             return prev;
1276     }
1277     return NULL;
1278 }
1279
1280 static int compare_codec_desc(const void *a, const void *b)
1281 {
1282     const AVCodecDescriptor * const *da = a;
1283     const AVCodecDescriptor * const *db = b;
1284
1285     return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
1286            strcmp((*da)->name, (*db)->name);
1287 }
1288
1289 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1290 {
1291     const AVCodecDescriptor *desc = NULL;
1292     const AVCodecDescriptor **codecs;
1293     unsigned nb_codecs = 0, i = 0;
1294
1295     while ((desc = avcodec_descriptor_next(desc)))
1296         nb_codecs++;
1297     if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1298         av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1299         exit(1);
1300     }
1301     desc = NULL;
1302     while ((desc = avcodec_descriptor_next(desc)))
1303         codecs[i++] = desc;
1304     av_assert0(i == nb_codecs);
1305     qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1306     *rcodecs = codecs;
1307     return nb_codecs;
1308 }
1309
1310 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1311 {
1312     const AVCodec *codec = NULL;
1313
1314     printf(" (%s: ", encoder ? "encoders" : "decoders");
1315
1316     while ((codec = next_codec_for_id(id, codec, encoder)))
1317         printf("%s ", codec->name);
1318
1319     printf(")");
1320 }
1321
1322 int show_codecs(void *optctx, const char *opt, const char *arg)
1323 {
1324     const AVCodecDescriptor **codecs;
1325     unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1326
1327     printf("Codecs:\n"
1328            " D..... = Decoding supported\n"
1329            " .E.... = Encoding supported\n"
1330            " ..V... = Video codec\n"
1331            " ..A... = Audio codec\n"
1332            " ..S... = Subtitle codec\n"
1333            " ...I.. = Intra frame-only codec\n"
1334            " ....L. = Lossy compression\n"
1335            " .....S = Lossless compression\n"
1336            " -------\n");
1337     for (i = 0; i < nb_codecs; i++) {
1338         const AVCodecDescriptor *desc = codecs[i];
1339         const AVCodec *codec = NULL;
1340
1341         printf(" ");
1342         printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1343         printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1344
1345         printf("%c", get_media_type_char(desc->type));
1346         printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1347         printf((desc->props & AV_CODEC_PROP_LOSSY)      ? "L" : ".");
1348         printf((desc->props & AV_CODEC_PROP_LOSSLESS)   ? "S" : ".");
1349
1350         printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1351
1352         /* print decoders/encoders when there's more than one or their
1353          * names are different from codec name */
1354         while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1355             if (strcmp(codec->name, desc->name)) {
1356                 print_codecs_for_id(desc->id, 0);
1357                 break;
1358             }
1359         }
1360         codec = NULL;
1361         while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1362             if (strcmp(codec->name, desc->name)) {
1363                 print_codecs_for_id(desc->id, 1);
1364                 break;
1365             }
1366         }
1367
1368         printf("\n");
1369     }
1370     av_free(codecs);
1371     return 0;
1372 }
1373
1374 static void print_codecs(int encoder)
1375 {
1376     const AVCodecDescriptor **codecs;
1377     unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1378
1379     printf("%s:\n"
1380            " V..... = Video\n"
1381            " A..... = Audio\n"
1382            " S..... = Subtitle\n"
1383            " .F.... = Frame-level multithreading\n"
1384            " ..S... = Slice-level multithreading\n"
1385            " ...X.. = Codec is experimental\n"
1386            " ....B. = Supports draw_horiz_band\n"
1387            " .....D = Supports direct rendering method 1\n"
1388            " ------\n",
1389            encoder ? "Encoders" : "Decoders");
1390     for (i = 0; i < nb_codecs; i++) {
1391         const AVCodecDescriptor *desc = codecs[i];
1392         const AVCodec *codec = NULL;
1393
1394         while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1395             printf(" %c", get_media_type_char(desc->type));
1396             printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1397             printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1398             printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL)  ? "X" : ".");
1399             printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1400             printf((codec->capabilities & CODEC_CAP_DR1)           ? "D" : ".");
1401
1402             printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1403             if (strcmp(codec->name, desc->name))
1404                 printf(" (codec %s)", desc->name);
1405
1406             printf("\n");
1407         }
1408     }
1409     av_free(codecs);
1410 }
1411
1412 int show_decoders(void *optctx, const char *opt, const char *arg)
1413 {
1414     print_codecs(0);
1415     return 0;
1416 }
1417
1418 int show_encoders(void *optctx, const char *opt, const char *arg)
1419 {
1420     print_codecs(1);
1421     return 0;
1422 }
1423
1424 int show_bsfs(void *optctx, const char *opt, const char *arg)
1425 {
1426     AVBitStreamFilter *bsf = NULL;
1427
1428     printf("Bitstream filters:\n");
1429     while ((bsf = av_bitstream_filter_next(bsf)))
1430         printf("%s\n", bsf->name);
1431     printf("\n");
1432     return 0;
1433 }
1434
1435 int show_protocols(void *optctx, const char *opt, const char *arg)
1436 {
1437     void *opaque = NULL;
1438     const char *name;
1439
1440     printf("Supported file protocols:\n"
1441            "Input:\n");
1442     while ((name = avio_enum_protocols(&opaque, 0)))
1443         printf("%s\n", name);
1444     printf("Output:\n");
1445     while ((name = avio_enum_protocols(&opaque, 1)))
1446         printf("%s\n", name);
1447     return 0;
1448 }
1449
1450 int show_filters(void *optctx, const char *opt, const char *arg)
1451 {
1452     const AVFilter av_unused(*filter) = NULL;
1453     char descr[64], *descr_cur;
1454     int i, j;
1455     const AVFilterPad *pad;
1456
1457     printf("Filters:\n"
1458            "  T. = Timeline support\n"
1459            "  .S = Slice threading\n"
1460            "  A = Audio input/output\n"
1461            "  V = Video input/output\n"
1462            "  N = Dynamic number and/or type of input/output\n"
1463            "  | = Source or sink filter\n");
1464 #if CONFIG_AVFILTER
1465     while ((filter = avfilter_next(filter))) {
1466         descr_cur = descr;
1467         for (i = 0; i < 2; i++) {
1468             if (i) {
1469                 *(descr_cur++) = '-';
1470                 *(descr_cur++) = '>';
1471             }
1472             pad = i ? filter->outputs : filter->inputs;
1473             for (j = 0; pad && pad[j].name; j++) {
1474                 if (descr_cur >= descr + sizeof(descr) - 4)
1475                     break;
1476                 *(descr_cur++) = get_media_type_char(pad[j].type);
1477             }
1478             if (!j)
1479                 *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1480                                   ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1481         }
1482         *descr_cur = 0;
1483         printf(" %c%c %-16s %-10s %s\n",
1484                filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1485                filter->flags & AVFILTER_FLAG_SLICE_THREADS    ? 'S' : '.',
1486                filter->name, descr, filter->description);
1487     }
1488 #endif
1489     return 0;
1490 }
1491
1492 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1493 {
1494     const AVPixFmtDescriptor *pix_desc = NULL;
1495
1496     printf("Pixel formats:\n"
1497            "I.... = Supported Input  format for conversion\n"
1498            ".O... = Supported Output format for conversion\n"
1499            "..H.. = Hardware accelerated format\n"
1500            "...P. = Paletted format\n"
1501            "....B = Bitstream format\n"
1502            "FLAGS NAME            NB_COMPONENTS BITS_PER_PIXEL\n"
1503            "-----\n");
1504
1505 #if !CONFIG_SWSCALE
1506 #   define sws_isSupportedInput(x)  0
1507 #   define sws_isSupportedOutput(x) 0
1508 #endif
1509
1510     while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1511         enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1512         printf("%c%c%c%c%c %-16s       %d            %2d\n",
1513                sws_isSupportedInput (pix_fmt)              ? 'I' : '.',
1514                sws_isSupportedOutput(pix_fmt)              ? 'O' : '.',
1515                pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL   ? 'H' : '.',
1516                pix_desc->flags & AV_PIX_FMT_FLAG_PAL       ? 'P' : '.',
1517                pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1518                pix_desc->name,
1519                pix_desc->nb_components,
1520                av_get_bits_per_pixel(pix_desc));
1521     }
1522     return 0;
1523 }
1524
1525 int show_layouts(void *optctx, const char *opt, const char *arg)
1526 {
1527     int i = 0;
1528     uint64_t layout, j;
1529     const char *name, *descr;
1530
1531     printf("Individual channels:\n"
1532            "NAME        DESCRIPTION\n");
1533     for (i = 0; i < 63; i++) {
1534         name = av_get_channel_name((uint64_t)1 << i);
1535         if (!name)
1536             continue;
1537         descr = av_get_channel_description((uint64_t)1 << i);
1538         printf("%-12s%s\n", name, descr);
1539     }
1540     printf("\nStandard channel layouts:\n"
1541            "NAME        DECOMPOSITION\n");
1542     for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1543         if (name) {
1544             printf("%-12s", name);
1545             for (j = 1; j; j <<= 1)
1546                 if ((layout & j))
1547                     printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1548             printf("\n");
1549         }
1550     }
1551     return 0;
1552 }
1553
1554 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1555 {
1556     int i;
1557     char fmt_str[128];
1558     for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1559         printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1560     return 0;
1561 }
1562
1563 static void show_help_codec(const char *name, int encoder)
1564 {
1565     const AVCodecDescriptor *desc;
1566     const AVCodec *codec;
1567
1568     if (!name) {
1569         av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1570         return;
1571     }
1572
1573     codec = encoder ? avcodec_find_encoder_by_name(name) :
1574                       avcodec_find_decoder_by_name(name);
1575
1576     if (codec)
1577         print_codec(codec);
1578     else if ((desc = avcodec_descriptor_get_by_name(name))) {
1579         int printed = 0;
1580
1581         while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1582             printed = 1;
1583             print_codec(codec);
1584         }
1585
1586         if (!printed) {
1587             av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1588                    "but no %s for it are available. FFmpeg might need to be "
1589                    "recompiled with additional external libraries.\n",
1590                    name, encoder ? "encoders" : "decoders");
1591         }
1592     } else {
1593         av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1594                name);
1595     }
1596 }
1597
1598 static void show_help_demuxer(const char *name)
1599 {
1600     const AVInputFormat *fmt = av_find_input_format(name);
1601
1602     if (!fmt) {
1603         av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1604         return;
1605     }
1606
1607     printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1608
1609     if (fmt->extensions)
1610         printf("    Common extensions: %s.\n", fmt->extensions);
1611
1612     if (fmt->priv_class)
1613         show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1614 }
1615
1616 static void show_help_muxer(const char *name)
1617 {
1618     const AVCodecDescriptor *desc;
1619     const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1620
1621     if (!fmt) {
1622         av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1623         return;
1624     }
1625
1626     printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1627
1628     if (fmt->extensions)
1629         printf("    Common extensions: %s.\n", fmt->extensions);
1630     if (fmt->mime_type)
1631         printf("    Mime type: %s.\n", fmt->mime_type);
1632     if (fmt->video_codec != AV_CODEC_ID_NONE &&
1633         (desc = avcodec_descriptor_get(fmt->video_codec))) {
1634         printf("    Default video codec: %s.\n", desc->name);
1635     }
1636     if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1637         (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1638         printf("    Default audio codec: %s.\n", desc->name);
1639     }
1640     if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1641         (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1642         printf("    Default subtitle codec: %s.\n", desc->name);
1643     }
1644
1645     if (fmt->priv_class)
1646         show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1647 }
1648
1649 #if CONFIG_AVFILTER
1650 static void show_help_filter(const char *name)
1651 {
1652 #if CONFIG_AVFILTER
1653     const AVFilter *f = avfilter_get_by_name(name);
1654     int i, count;
1655
1656     if (!name) {
1657         av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1658         return;
1659     } else if (!f) {
1660         av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1661         return;
1662     }
1663
1664     printf("Filter %s\n", f->name);
1665     if (f->description)
1666         printf("  %s\n", f->description);
1667
1668     if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1669         printf("    slice threading supported\n");
1670
1671     printf("    Inputs:\n");
1672     count = avfilter_pad_count(f->inputs);
1673     for (i = 0; i < count; i++) {
1674         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1675                media_type_string(avfilter_pad_get_type(f->inputs, i)));
1676     }
1677     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1678         printf("        dynamic (depending on the options)\n");
1679     else if (!count)
1680         printf("        none (source filter)\n");
1681
1682     printf("    Outputs:\n");
1683     count = avfilter_pad_count(f->outputs);
1684     for (i = 0; i < count; i++) {
1685         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1686                media_type_string(avfilter_pad_get_type(f->outputs, i)));
1687     }
1688     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1689         printf("        dynamic (depending on the options)\n");
1690     else if (!count)
1691         printf("        none (sink filter)\n");
1692
1693     if (f->priv_class)
1694         show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1695                                           AV_OPT_FLAG_AUDIO_PARAM);
1696     if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1697         printf("This filter has support for timeline through the 'enable' option.\n");
1698 #else
1699     av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1700            "can not to satisfy request\n");
1701 #endif
1702 }
1703 #endif
1704
1705 int show_help(void *optctx, const char *opt, const char *arg)
1706 {
1707     char *topic, *par;
1708     av_log_set_callback(log_callback_help);
1709
1710     topic = av_strdup(arg ? arg : "");
1711     par = strchr(topic, '=');
1712     if (par)
1713         *par++ = 0;
1714
1715     if (!*topic) {
1716         show_help_default(topic, par);
1717     } else if (!strcmp(topic, "decoder")) {
1718         show_help_codec(par, 0);
1719     } else if (!strcmp(topic, "encoder")) {
1720         show_help_codec(par, 1);
1721     } else if (!strcmp(topic, "demuxer")) {
1722         show_help_demuxer(par);
1723     } else if (!strcmp(topic, "muxer")) {
1724         show_help_muxer(par);
1725 #if CONFIG_AVFILTER
1726     } else if (!strcmp(topic, "filter")) {
1727         show_help_filter(par);
1728 #endif
1729     } else {
1730         show_help_default(topic, par);
1731     }
1732
1733     av_freep(&topic);
1734     return 0;
1735 }
1736
1737 int read_yesno(void)
1738 {
1739     int c = getchar();
1740     int yesno = (av_toupper(c) == 'Y');
1741
1742     while (c != '\n' && c != EOF)
1743         c = getchar();
1744
1745     return yesno;
1746 }
1747
1748 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1749 {
1750     int ret;
1751     FILE *f = fopen(filename, "rb");
1752
1753     if (!f) {
1754         av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1755                strerror(errno));
1756         return AVERROR(errno);
1757     }
1758     fseek(f, 0, SEEK_END);
1759     *size = ftell(f);
1760     fseek(f, 0, SEEK_SET);
1761     if (*size == (size_t)-1) {
1762         av_log(NULL, AV_LOG_ERROR, "IO error: %s\n", strerror(errno));
1763         fclose(f);
1764         return AVERROR(errno);
1765     }
1766     *bufptr = av_malloc(*size + 1);
1767     if (!*bufptr) {
1768         av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1769         fclose(f);
1770         return AVERROR(ENOMEM);
1771     }
1772     ret = fread(*bufptr, 1, *size, f);
1773     if (ret < *size) {
1774         av_free(*bufptr);
1775         if (ferror(f)) {
1776             av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1777                    filename, strerror(errno));
1778             ret = AVERROR(errno);
1779         } else
1780             ret = AVERROR_EOF;
1781     } else {
1782         ret = 0;
1783         (*bufptr)[(*size)++] = '\0';
1784     }
1785
1786     fclose(f);
1787     return ret;
1788 }
1789
1790 FILE *get_preset_file(char *filename, size_t filename_size,
1791                       const char *preset_name, int is_path,
1792                       const char *codec_name)
1793 {
1794     FILE *f = NULL;
1795     int i;
1796     const char *base[3] = { getenv("FFMPEG_DATADIR"),
1797                             getenv("HOME"),
1798                             FFMPEG_DATADIR, };
1799
1800     if (is_path) {
1801         av_strlcpy(filename, preset_name, filename_size);
1802         f = fopen(filename, "r");
1803     } else {
1804 #ifdef _WIN32
1805         char datadir[MAX_PATH], *ls;
1806         base[2] = NULL;
1807
1808         if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
1809         {
1810             for (ls = datadir; ls < datadir + strlen(datadir); ls++)
1811                 if (*ls == '\\') *ls = '/';
1812
1813             if (ls = strrchr(datadir, '/'))
1814             {
1815                 *ls = 0;
1816                 strncat(datadir, "/ffpresets",  sizeof(datadir) - 1 - strlen(datadir));
1817                 base[2] = datadir;
1818             }
1819         }
1820 #endif
1821         for (i = 0; i < 3 && !f; i++) {
1822             if (!base[i])
1823                 continue;
1824             snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
1825                      i != 1 ? "" : "/.ffmpeg", preset_name);
1826             f = fopen(filename, "r");
1827             if (!f && codec_name) {
1828                 snprintf(filename, filename_size,
1829                          "%s%s/%s-%s.ffpreset",
1830                          base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
1831                          preset_name);
1832                 f = fopen(filename, "r");
1833             }
1834         }
1835     }
1836
1837     return f;
1838 }
1839
1840 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1841 {
1842     int ret = avformat_match_stream_specifier(s, st, spec);
1843     if (ret < 0)
1844         av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1845     return ret;
1846 }
1847
1848 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
1849                                 AVFormatContext *s, AVStream *st, AVCodec *codec)
1850 {
1851     AVDictionary    *ret = NULL;
1852     AVDictionaryEntry *t = NULL;
1853     int            flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
1854                                       : AV_OPT_FLAG_DECODING_PARAM;
1855     char          prefix = 0;
1856     const AVClass    *cc = avcodec_get_class();
1857
1858     if (!codec)
1859         codec            = s->oformat ? avcodec_find_encoder(codec_id)
1860                                       : avcodec_find_decoder(codec_id);
1861
1862     switch (st->codec->codec_type) {
1863     case AVMEDIA_TYPE_VIDEO:
1864         prefix  = 'v';
1865         flags  |= AV_OPT_FLAG_VIDEO_PARAM;
1866         break;
1867     case AVMEDIA_TYPE_AUDIO:
1868         prefix  = 'a';
1869         flags  |= AV_OPT_FLAG_AUDIO_PARAM;
1870         break;
1871     case AVMEDIA_TYPE_SUBTITLE:
1872         prefix  = 's';
1873         flags  |= AV_OPT_FLAG_SUBTITLE_PARAM;
1874         break;
1875     }
1876
1877     while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1878         char *p = strchr(t->key, ':');
1879
1880         /* check stream specification in opt name */
1881         if (p)
1882             switch (check_stream_specifier(s, st, p + 1)) {
1883             case  1: *p = 0; break;
1884             case  0:         continue;
1885             default:         return NULL;
1886             }
1887
1888         if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1889             (codec && codec->priv_class &&
1890              av_opt_find(&codec->priv_class, t->key, NULL, flags,
1891                          AV_OPT_SEARCH_FAKE_OBJ)))
1892             av_dict_set(&ret, t->key, t->value, 0);
1893         else if (t->key[0] == prefix &&
1894                  av_opt_find(&cc, t->key + 1, NULL, flags,
1895                              AV_OPT_SEARCH_FAKE_OBJ))
1896             av_dict_set(&ret, t->key + 1, t->value, 0);
1897
1898         if (p)
1899             *p = ':';
1900     }
1901     return ret;
1902 }
1903
1904 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
1905                                            AVDictionary *codec_opts)
1906 {
1907     int i;
1908     AVDictionary **opts;
1909
1910     if (!s->nb_streams)
1911         return NULL;
1912     opts = av_mallocz(s->nb_streams * sizeof(*opts));
1913     if (!opts) {
1914         av_log(NULL, AV_LOG_ERROR,
1915                "Could not alloc memory for stream options.\n");
1916         return NULL;
1917     }
1918     for (i = 0; i < s->nb_streams; i++)
1919         opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1920                                     s, s->streams[i], NULL);
1921     return opts;
1922 }
1923
1924 void *grow_array(void *array, int elem_size, int *size, int new_size)
1925 {
1926     if (new_size >= INT_MAX / elem_size) {
1927         av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1928         exit(1);
1929     }
1930     if (*size < new_size) {
1931         uint8_t *tmp = av_realloc(array, new_size*elem_size);
1932         if (!tmp) {
1933             av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1934             exit(1);
1935         }
1936         memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1937         *size = new_size;
1938         return tmp;
1939     }
1940     return array;
1941 }