MobileFFmpeg IOS API  3.1
fftools_cmdutils.c
Go to the documentation of this file.
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 /*
23  * CHANGES 08.2018
24  * --------------------------------------------------------
25  * - fftools_ prefix added to file name and parent header
26  *
27  * CHANGES 07.2018
28  * --------------------------------------------------------
29  * - Unused headers removed
30  * - Parentheses placed around assignments in condition to prevent -Wparentheses warning
31  * - exit_program updated with longjmp, disabling exit
32  * - longjmp_value added to store exit code
33  * - (optindex < argc) validation added before accessing argv[optindex] inside split_commandline()
34  * and parse_options()
35  * - All av_log_set_callback invocations updated to set mobileffmpeg_log_callback_function from Config.m. Unused
36  * log_callback_help and log_callback_help methods removed
37  * - (idx + 1 < argc) validation added in parse_loglevel()
38  */
39 
40 #include <string.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <math.h>
45 
46 #include "mobileffmpeg_exception.h"
47 
48 /* Include only the enabled headers since some compilers (namely, Sun
49  Studio) will not omit unused inline functions and create undefined
50  references to libraries that are not being built. */
51 
52 #include "config.h"
53 #include "libavformat/avformat.h"
54 #include "libavfilter/avfilter.h"
55 #include "libavdevice/avdevice.h"
56 #include "libswscale/swscale.h"
57 #include "libswresample/swresample.h"
58 #include "libavutil/attributes.h"
59 #include "libavutil/avassert.h"
60 #include "libavutil/avstring.h"
61 #include "libavutil/bprint.h"
62 #include "libavutil/display.h"
63 #include "libavutil/mathematics.h"
64 #include "libavutil/imgutils.h"
65 #include "libavutil/libm.h"
66 #include "libavutil/parseutils.h"
67 #include "libavutil/pixdesc.h"
68 #include "libavutil/eval.h"
69 #include "libavutil/dict.h"
70 #include "libavutil/opt.h"
71 #include "libavutil/cpu.h"
72 #include "libavutil/ffversion.h"
73 #include "libavutil/version.h"
74 #include "fftools_cmdutils.h"
75 #if CONFIG_NETWORK
76 #include "libavformat/network.h"
77 #endif
78 #if HAVE_SYS_RESOURCE_H
79 #include <sys/time.h>
80 #include <sys/resource.h>
81 #endif
82 #ifdef _WIN32
83 #include <windows.h>
84 #endif
85 
86 static int init_report(const char *env);
87 extern void mobileffmpeg_log_callback_function(void *ptr, int level, const char* format, va_list vargs);
88 
89 AVDictionary *sws_dict;
90 AVDictionary *swr_opts;
92 
93 static FILE *report_file;
94 static int report_file_level = AV_LOG_DEBUG;
95 int hide_banner = 0;
96 int longjmp_value = 0;
97 
102 };
103 
104 void init_opts(void)
105 {
106  av_dict_set(&sws_dict, "flags", "bicubic", 0);
107 }
108 
109 void uninit_opts(void)
110 {
111  av_dict_free(&swr_opts);
112  av_dict_free(&sws_dict);
113  av_dict_free(&format_opts);
114  av_dict_free(&codec_opts);
115  av_dict_free(&resample_opts);
116 }
117 
118 void init_dynload(void)
119 {
120 #ifdef _WIN32
121  /* Calling SetDllDirectory with the empty string (but not NULL) removes the
122  * current working directory from the DLL search path as a security pre-caution. */
123  SetDllDirectory("");
124 #endif
125 }
126 
127 static void (*program_exit)(int ret);
128 
129 void register_exit(void (*cb)(int ret))
130 {
131  program_exit = cb;
132 }
133 
134 void exit_program(int ret)
135 {
136  if (program_exit)
137  program_exit(ret);
138 
139  // exit disabled and replaced with longjmp, exit value stored in longjmp_value
140  // exit(ret);
141  longjmp_value = ret;
142  longjmp(ex_buf__, ret);
143 }
144 
145 double parse_number_or_die(const char *context, const char *numstr, int type,
146  double min, double max)
147 {
148  char *tail;
149  const char *error;
150  double d = av_strtod(numstr, &tail);
151  if (*tail)
152  error = "Expected number for %s but found: %s\n";
153  else if (d < min || d > max)
154  error = "The value for %s was %s which is not within %f - %f\n";
155  else if (type == OPT_INT64 && (int64_t)d != d)
156  error = "Expected int64 for %s but found %s\n";
157  else if (type == OPT_INT && (int)d != d)
158  error = "Expected int for %s but found %s\n";
159  else
160  return d;
161  av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
162  exit_program(1);
163  return 0;
164 }
165 
166 int64_t parse_time_or_die(const char *context, const char *timestr,
167  int is_duration)
168 {
169  int64_t us;
170  if (av_parse_time(&us, timestr, is_duration) < 0) {
171  av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
172  is_duration ? "duration" : "date", context, timestr);
173  exit_program(1);
174  }
175  return us;
176 }
177 
178 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
179  int rej_flags, int alt_flags)
180 {
181  const OptionDef *po;
182  int first;
183 
184  first = 1;
185  for (po = options; po->name; po++) {
186  char buf[64];
187 
188  if (((po->flags & req_flags) != req_flags) ||
189  (alt_flags && !(po->flags & alt_flags)) ||
190  (po->flags & rej_flags))
191  continue;
192 
193  if (first) {
194  printf("%s\n", msg);
195  first = 0;
196  }
197  av_strlcpy(buf, po->name, sizeof(buf));
198  if (po->argname) {
199  av_strlcat(buf, " ", sizeof(buf));
200  av_strlcat(buf, po->argname, sizeof(buf));
201  }
202  printf("-%-17s %s\n", buf, po->help);
203  }
204  printf("\n");
205 }
206 
207 void show_help_children(const AVClass *class, int flags)
208 {
209  const AVClass *child = NULL;
210  if (class->option) {
211  av_opt_show2(&class, NULL, flags, 0);
212  printf("\n");
213  }
214 
215  while ((child = av_opt_child_class_next(class, child)))
216  show_help_children(child, flags);
217 }
218 
219 static const OptionDef *find_option(const OptionDef *po, const char *name)
220 {
221  const char *p = strchr(name, ':');
222  int len = p ? p - name : strlen(name);
223 
224  while (po->name) {
225  if (!strncmp(name, po->name, len) && strlen(po->name) == len)
226  break;
227  po++;
228  }
229  return po;
230 }
231 
232 /* _WIN32 means using the windows libc - cygwin doesn't define that
233  * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
234  * it doesn't provide the actual command line via GetCommandLineW(). */
235 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
236 #include <shellapi.h>
237 /* Will be leaked on exit */
238 static char** win32_argv_utf8 = NULL;
239 static int win32_argc = 0;
240 
248 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
249 {
250  char *argstr_flat;
251  wchar_t **argv_w;
252  int i, buffsize = 0, offset = 0;
253 
254  if (win32_argv_utf8) {
255  *argc_ptr = win32_argc;
256  *argv_ptr = win32_argv_utf8;
257  return;
258  }
259 
260  win32_argc = 0;
261  argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
262  if (win32_argc <= 0 || !argv_w)
263  return;
264 
265  /* determine the UTF-8 buffer size (including NULL-termination symbols) */
266  for (i = 0; i < win32_argc; i++)
267  buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
268  NULL, 0, NULL, NULL);
269 
270  win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
271  argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
272  if (!win32_argv_utf8) {
273  LocalFree(argv_w);
274  return;
275  }
276 
277  for (i = 0; i < win32_argc; i++) {
278  win32_argv_utf8[i] = &argstr_flat[offset];
279  offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
280  &argstr_flat[offset],
281  buffsize - offset, NULL, NULL);
282  }
283  win32_argv_utf8[i] = NULL;
284  LocalFree(argv_w);
285 
286  *argc_ptr = win32_argc;
287  *argv_ptr = win32_argv_utf8;
288 }
289 #else
290 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
291 {
292  /* nothing to do */
293 }
294 #endif /* HAVE_COMMANDLINETOARGVW */
295 
296 static int write_option(void *optctx, const OptionDef *po, const char *opt,
297  const char *arg)
298 {
299  /* new-style options contain an offset into optctx, old-style address of
300  * a global var*/
301  void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
302  (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
303  int *dstcount;
304 
305  if (po->flags & OPT_SPEC) {
306  SpecifierOpt **so = dst;
307  char *p = strchr(opt, ':');
308  char *str;
309 
310  dstcount = (int *)(so + 1);
311  *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
312  str = av_strdup(p ? p + 1 : "");
313  if (!str)
314  return AVERROR(ENOMEM);
315  (*so)[*dstcount - 1].specifier = str;
316  dst = &(*so)[*dstcount - 1].u;
317  }
318 
319  if (po->flags & OPT_STRING) {
320  char *str;
321  str = av_strdup(arg);
322  av_freep(dst);
323  if (!str)
324  return AVERROR(ENOMEM);
325  *(char **)dst = str;
326  } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
327  *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
328  } else if (po->flags & OPT_INT64) {
329  *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
330  } else if (po->flags & OPT_TIME) {
331  *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
332  } else if (po->flags & OPT_FLOAT) {
333  *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
334  } else if (po->flags & OPT_DOUBLE) {
335  *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
336  } else if (po->u.func_arg) {
337  int ret = po->u.func_arg(optctx, opt, arg);
338  if (ret < 0) {
339  av_log(NULL, AV_LOG_ERROR,
340  "Failed to set value '%s' for option '%s': %s\n",
341  arg, opt, av_err2str(ret));
342  return ret;
343  }
344  }
345  if (po->flags & OPT_EXIT)
346  exit_program(0);
347 
348  return 0;
349 }
350 
351 int parse_option(void *optctx, const char *opt, const char *arg,
352  const OptionDef *options)
353 {
354  const OptionDef *po;
355  int ret;
356 
357  po = find_option(options, opt);
358  if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
359  /* handle 'no' bool option */
360  po = find_option(options, opt + 2);
361  if ((po->name && (po->flags & OPT_BOOL)))
362  arg = "0";
363  } else if (po->flags & OPT_BOOL)
364  arg = "1";
365 
366  if (!po->name)
367  po = find_option(options, "default");
368  if (!po->name) {
369  av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
370  return AVERROR(EINVAL);
371  }
372  if (po->flags & HAS_ARG && !arg) {
373  av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
374  return AVERROR(EINVAL);
375  }
376 
377  ret = write_option(optctx, po, opt, arg);
378  if (ret < 0)
379  return ret;
380 
381  return !!(po->flags & HAS_ARG);
382 }
383 
384 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
385  void (*parse_arg_function)(void *, const char*))
386 {
387  const char *opt;
388  int optindex, handleoptions = 1, ret;
389 
390  /* perform system-dependent conversions for arguments list */
391  prepare_app_arguments(&argc, &argv);
392 
393  /* parse options */
394  optindex = 1;
395  while (optindex < argc) {
396  opt = argv[optindex++];
397 
398  if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
399  if (opt[1] == '-' && opt[2] == '\0') {
400  handleoptions = 0;
401  continue;
402  }
403  opt++;
404 
405  if (optindex < argc) {
406  if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
407  exit_program(1);
408  optindex += ret;
409  }
410  } else {
411  if (parse_arg_function)
412  parse_arg_function(optctx, opt);
413  }
414  }
415 }
416 
417 int parse_optgroup(void *optctx, OptionGroup *g)
418 {
419  int i, ret;
420 
421  av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
422  g->group_def->name, g->arg);
423 
424  for (i = 0; i < g->nb_opts; i++) {
425  Option *o = &g->opts[i];
426 
427  if (g->group_def->flags &&
428  !(g->group_def->flags & o->opt->flags)) {
429  av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
430  "%s %s -- you are trying to apply an input option to an "
431  "output file or vice versa. Move this option before the "
432  "file it belongs to.\n", o->key, o->opt->help,
433  g->group_def->name, g->arg);
434  return AVERROR(EINVAL);
435  }
436 
437  av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
438  o->key, o->opt->help, o->val);
439 
440  ret = write_option(optctx, o->opt, o->key, o->val);
441  if (ret < 0)
442  return ret;
443  }
444 
445  av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
446 
447  return 0;
448 }
449 
450 int locate_option(int argc, char **argv, const OptionDef *options,
451  const char *optname)
452 {
453  const OptionDef *po;
454  int i;
455 
456  for (i = 1; i < argc; i++) {
457  const char *cur_opt = argv[i];
458 
459  if (*cur_opt++ != '-')
460  continue;
461 
462  po = find_option(options, cur_opt);
463  if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
464  po = find_option(options, cur_opt + 2);
465 
466  if ((!po->name && !strcmp(cur_opt, optname)) ||
467  (po->name && !strcmp(optname, po->name)))
468  return i;
469 
470  if (!po->name || po->flags & HAS_ARG)
471  i++;
472  }
473  return 0;
474 }
475 
476 static void dump_argument(const char *a)
477 {
478  const unsigned char *p;
479 
480  for (p = a; *p; p++)
481  if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
482  *p == '_' || (*p >= 'a' && *p <= 'z')))
483  break;
484  if (!*p) {
485  fputs(a, report_file);
486  return;
487  }
488  fputc('"', report_file);
489  for (p = a; *p; p++) {
490  if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
491  fprintf(report_file, "\\%c", *p);
492  else if (*p < ' ' || *p > '~')
493  fprintf(report_file, "\\x%02x", *p);
494  else
495  fputc(*p, report_file);
496  }
497  fputc('"', report_file);
498 }
499 
500 static void check_options(const OptionDef *po)
501 {
502  while (po->name) {
503  if (po->flags & OPT_PERFILE)
504  av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
505  po++;
506  }
507 }
508 
509 void parse_loglevel(int argc, char **argv, const OptionDef *options)
510 {
511  int idx = locate_option(argc, argv, options, "loglevel");
512  const char *env;
513 
515 
516  if (!idx)
517  idx = locate_option(argc, argv, options, "v");
518  if (idx && (idx + 1 < argc) && argv[idx + 1])
519  opt_loglevel(NULL, "loglevel", argv[idx + 1]);
520  idx = locate_option(argc, argv, options, "report");
521  if ((env = getenv("FFREPORT")) || idx) {
522  init_report(env);
523  if (report_file) {
524  int i;
525  fprintf(report_file, "Command line:\n");
526  for (i = 0; i < argc; i++) {
527  dump_argument(argv[i]);
528  fputc(i < argc - 1 ? ' ' : '\n', report_file);
529  }
530  fflush(report_file);
531  }
532  }
533  idx = locate_option(argc, argv, options, "hide_banner");
534  if (idx)
535  hide_banner = 1;
536 }
537 
538 static const AVOption *opt_find(void *obj, const char *name, const char *unit,
539  int opt_flags, int search_flags)
540 {
541  const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
542  if(o && !o->flags)
543  return NULL;
544  return o;
545 }
546 
547 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
548 int opt_default(void *optctx, const char *opt, const char *arg)
549 {
550  const AVOption *o;
551  int consumed = 0;
552  char opt_stripped[128];
553  const char *p;
554  const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
555 #if CONFIG_AVRESAMPLE
556  const AVClass *rc = avresample_get_class();
557 #endif
558 #if CONFIG_SWSCALE
559  const AVClass *sc = sws_get_class();
560 #endif
561 #if CONFIG_SWRESAMPLE
562  const AVClass *swr_class = swr_get_class();
563 #endif
564 
565  if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
566  av_log_set_level(AV_LOG_DEBUG);
567 
568  if (!(p = strchr(opt, ':')))
569  p = opt + strlen(opt);
570  av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
571 
572  if ((o = opt_find(&cc, opt_stripped, NULL, 0,
573  AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
574  ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
575  (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
576  av_dict_set(&codec_opts, opt, arg, FLAGS);
577  consumed = 1;
578  }
579  if ((o = opt_find(&fc, opt, NULL, 0,
580  AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
581  av_dict_set(&format_opts, opt, arg, FLAGS);
582  if (consumed)
583  av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
584  consumed = 1;
585  }
586 #if CONFIG_SWSCALE
587  if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
588  AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
589  struct SwsContext *sws = sws_alloc_context();
590  int ret = av_opt_set(sws, opt, arg, 0);
591  sws_freeContext(sws);
592  if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
593  !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
594  !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
595  av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
596  return AVERROR(EINVAL);
597  }
598  if (ret < 0) {
599  av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
600  return ret;
601  }
602 
603  av_dict_set(&sws_dict, opt, arg, FLAGS);
604 
605  consumed = 1;
606  }
607 #else
608  if (!consumed && !strcmp(opt, "sws_flags")) {
609  av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
610  consumed = 1;
611  }
612 #endif
613 #if CONFIG_SWRESAMPLE
614  if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
615  AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
616  struct SwrContext *swr = swr_alloc();
617  int ret = av_opt_set(swr, opt, arg, 0);
618  swr_free(&swr);
619  if (ret < 0) {
620  av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
621  return ret;
622  }
623  av_dict_set(&swr_opts, opt, arg, FLAGS);
624  consumed = 1;
625  }
626 #endif
627 #if CONFIG_AVRESAMPLE
628  if ((o=opt_find(&rc, opt, NULL, 0,
629  AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
630  av_dict_set(&resample_opts, opt, arg, FLAGS);
631  consumed = 1;
632  }
633 #endif
634 
635  if (consumed)
636  return 0;
637  return AVERROR_OPTION_NOT_FOUND;
638 }
639 
640 /*
641  * Check whether given option is a group separator.
642  *
643  * @return index of the group definition that matched or -1 if none
644  */
645 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
646  const char *opt)
647 {
648  int i;
649 
650  for (i = 0; i < nb_groups; i++) {
651  const OptionGroupDef *p = &groups[i];
652  if (p->sep && !strcmp(p->sep, opt))
653  return i;
654  }
655 
656  return -1;
657 }
658 
659 /*
660  * Finish parsing an option group.
661  *
662  * @param group_idx which group definition should this group belong to
663  * @param arg argument of the group delimiting option
664  */
665 static void finish_group(OptionParseContext *octx, int group_idx,
666  const char *arg)
667 {
668  OptionGroupList *l = &octx->groups[group_idx];
669  OptionGroup *g;
670 
671  GROW_ARRAY(l->groups, l->nb_groups);
672  g = &l->groups[l->nb_groups - 1];
673 
674  *g = octx->cur_group;
675  g->arg = arg;
676  g->group_def = l->group_def;
677  g->sws_dict = sws_dict;
678  g->swr_opts = swr_opts;
679  g->codec_opts = codec_opts;
682 
683  codec_opts = NULL;
684  format_opts = NULL;
685  resample_opts = NULL;
686  sws_dict = NULL;
687  swr_opts = NULL;
688  init_opts();
689 
690  memset(&octx->cur_group, 0, sizeof(octx->cur_group));
691 }
692 
693 /*
694  * Add an option instance to currently parsed group.
695  */
696 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
697  const char *key, const char *val)
698 {
699  int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
700  OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
701 
702  GROW_ARRAY(g->opts, g->nb_opts);
703  g->opts[g->nb_opts - 1].opt = opt;
704  g->opts[g->nb_opts - 1].key = key;
705  g->opts[g->nb_opts - 1].val = val;
706 }
707 
709  const OptionGroupDef *groups, int nb_groups)
710 {
711  static const OptionGroupDef global_group = { "global" };
712  int i;
713 
714  memset(octx, 0, sizeof(*octx));
715 
716  octx->nb_groups = nb_groups;
717  octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
718  if (!octx->groups)
719  exit_program(1);
720 
721  for (i = 0; i < octx->nb_groups; i++)
722  octx->groups[i].group_def = &groups[i];
723 
724  octx->global_opts.group_def = &global_group;
725  octx->global_opts.arg = "";
726 
727  init_opts();
728 }
729 
731 {
732  int i, j;
733 
734  for (i = 0; i < octx->nb_groups; i++) {
735  OptionGroupList *l = &octx->groups[i];
736 
737  for (j = 0; j < l->nb_groups; j++) {
738  av_freep(&l->groups[j].opts);
739  av_dict_free(&l->groups[j].codec_opts);
740  av_dict_free(&l->groups[j].format_opts);
741  av_dict_free(&l->groups[j].resample_opts);
742 
743  av_dict_free(&l->groups[j].sws_dict);
744  av_dict_free(&l->groups[j].swr_opts);
745  }
746  av_freep(&l->groups);
747  }
748  av_freep(&octx->groups);
749 
750  av_freep(&octx->cur_group.opts);
751  av_freep(&octx->global_opts.opts);
752 
753  uninit_opts();
754 }
755 
756 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
757  const OptionDef *options,
758  const OptionGroupDef *groups, int nb_groups)
759 {
760  int optindex = 1;
761  int dashdash = -2;
762 
763  /* perform system-dependent conversions for arguments list */
764  prepare_app_arguments(&argc, &argv);
765 
766  init_parse_context(octx, groups, nb_groups);
767  av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
768 
769  while (optindex < argc) {
770  const char *opt = argv[optindex++], *arg;
771  const OptionDef *po;
772  int ret;
773 
774  av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
775 
776  if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
777  dashdash = optindex;
778  continue;
779  }
780  /* unnamed group separators, e.g. output filename */
781  if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
782  finish_group(octx, 0, opt);
783  av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
784  continue;
785  }
786  opt++;
787 
788 #define GET_ARG(arg) \
789 do { \
790  if (optindex < argc) { \
791  arg = argv[optindex++]; \
792  } else { \
793  av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
794  return AVERROR(EINVAL); \
795  } \
796 } while (0)
797 
798  /* named group separators, e.g. -i */
799  if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
800  GET_ARG(arg);
801  finish_group(octx, ret, arg);
802  av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
803  groups[ret].name, arg);
804  continue;
805  }
806 
807  /* normal options */
808  po = find_option(options, opt);
809  if (po->name) {
810  if (po->flags & OPT_EXIT) {
811  /* optional argument, e.g. -h */
812  if (optindex < argc) {
813  arg = argv[optindex++];
814  } else {
815  arg = "";
816  }
817  } else if (po->flags & HAS_ARG) {
818  GET_ARG(arg);
819  } else {
820  arg = "1";
821  }
822 
823  add_opt(octx, po, opt, arg);
824  av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
825  "argument '%s'.\n", po->name, po->help, arg);
826  continue;
827  }
828 
829  /* AVOptions */
830  if ((optindex < argc) && argv[optindex]) {
831  ret = opt_default(NULL, opt, argv[optindex]);
832  if (ret >= 0) {
833  av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
834  "argument '%s'.\n", opt, argv[optindex]);
835  optindex++;
836  continue;
837  } else if (ret != AVERROR_OPTION_NOT_FOUND) {
838  av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
839  "with argument '%s'.\n", opt, argv[optindex]);
840  return ret;
841  }
842  }
843 
844  /* boolean -nofoo options */
845  if (opt[0] == 'n' && opt[1] == 'o' &&
846  (po = find_option(options, opt + 2)) &&
847  po->name && po->flags & OPT_BOOL) {
848  add_opt(octx, po, opt, "0");
849  av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
850  "argument 0.\n", po->name, po->help);
851  continue;
852  }
853 
854  av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
855  return AVERROR_OPTION_NOT_FOUND;
856  }
857 
859  av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
860  "commandline.\n");
861 
862  av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
863 
864  return 0;
865 }
866 
867 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
868 {
869  int ret;
870  unsigned flags = av_get_cpu_flags();
871 
872  if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
873  return ret;
874 
875  av_force_cpu_flags(flags);
876  return 0;
877 }
878 
879 int opt_loglevel(void *optctx, const char *opt, const char *arg)
880 {
881  const struct { const char *name; int level; } log_levels[] = {
882  { "quiet" , AV_LOG_QUIET },
883  { "panic" , AV_LOG_PANIC },
884  { "fatal" , AV_LOG_FATAL },
885  { "error" , AV_LOG_ERROR },
886  { "warning", AV_LOG_WARNING },
887  { "info" , AV_LOG_INFO },
888  { "verbose", AV_LOG_VERBOSE },
889  { "debug" , AV_LOG_DEBUG },
890  { "trace" , AV_LOG_TRACE },
891  };
892  const char *token;
893  char *tail;
894  int flags = av_log_get_flags();
895  int level = av_log_get_level();
896  int cmd, i = 0;
897 
898  av_assert0(arg);
899  while (*arg) {
900  token = arg;
901  if (*token == '+' || *token == '-') {
902  cmd = *token++;
903  } else {
904  cmd = 0;
905  }
906  if (!i && !cmd) {
907  flags = 0; /* missing relative prefix, build absolute value */
908  }
909  if (!strncmp(token, "repeat", 6)) {
910  if (cmd == '-') {
911  flags |= AV_LOG_SKIP_REPEATED;
912  } else {
913  flags &= ~AV_LOG_SKIP_REPEATED;
914  }
915  arg = token + 6;
916  } else if (!strncmp(token, "level", 5)) {
917  if (cmd == '-') {
918  flags &= ~AV_LOG_PRINT_LEVEL;
919  } else {
920  flags |= AV_LOG_PRINT_LEVEL;
921  }
922  arg = token + 5;
923  } else {
924  break;
925  }
926  i++;
927  }
928  if (!*arg) {
929  goto end;
930  } else if (*arg == '+') {
931  arg++;
932  } else if (!i) {
933  flags = av_log_get_flags(); /* level value without prefix, reset flags */
934  }
935 
936  for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
937  if (!strcmp(log_levels[i].name, arg)) {
938  level = log_levels[i].level;
939  goto end;
940  }
941  }
942 
943  level = strtol(arg, &tail, 10);
944  if (*tail) {
945  av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
946  "Possible levels are numbers or:\n", arg);
947  for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
948  av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
949  exit_program(1);
950  }
951 
952 end:
953  av_log_set_flags(flags);
954  av_log_set_level(level);
955  return 0;
956 }
957 
958 static void expand_filename_template(AVBPrint *bp, const char *template,
959  struct tm *tm)
960 {
961  int c;
962 
963  while ((c = *(template++))) {
964  if (c == '%') {
965  if (!(c = *(template++)))
966  break;
967  switch (c) {
968  case 'p':
969  av_bprintf(bp, "%s", program_name);
970  break;
971  case 't':
972  av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
973  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
974  tm->tm_hour, tm->tm_min, tm->tm_sec);
975  break;
976  case '%':
977  av_bprint_chars(bp, c, 1);
978  break;
979  }
980  } else {
981  av_bprint_chars(bp, c, 1);
982  }
983  }
984 }
985 
986 static int init_report(const char *env)
987 {
988  char *filename_template = NULL;
989  char *key, *val;
990  int ret, count = 0;
991  time_t now;
992  struct tm *tm;
993  AVBPrint filename;
994 
995  if (report_file) /* already opened */
996  return 0;
997  time(&now);
998  tm = localtime(&now);
999 
1000  while (env && *env) {
1001  if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
1002  if (count)
1003  av_log(NULL, AV_LOG_ERROR,
1004  "Failed to parse FFREPORT environment variable: %s\n",
1005  av_err2str(ret));
1006  break;
1007  }
1008  if (*env)
1009  env++;
1010  count++;
1011  if (!strcmp(key, "file")) {
1012  av_free(filename_template);
1013  filename_template = val;
1014  val = NULL;
1015  } else if (!strcmp(key, "level")) {
1016  char *tail;
1017  report_file_level = strtol(val, &tail, 10);
1018  if (*tail) {
1019  av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
1020  exit_program(1);
1021  }
1022  } else {
1023  av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
1024  }
1025  av_free(val);
1026  av_free(key);
1027  }
1028 
1029  av_bprint_init(&filename, 0, AV_BPRINT_SIZE_AUTOMATIC);
1030  expand_filename_template(&filename,
1031  av_x_if_null(filename_template, "%p-%t.log"), tm);
1032  av_free(filename_template);
1033  if (!av_bprint_is_complete(&filename)) {
1034  av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
1035  return AVERROR(ENOMEM);
1036  }
1037 
1038  report_file = fopen(filename.str, "w");
1039  if (!report_file) {
1040  int ret = AVERROR(errno);
1041  av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
1042  filename.str, strerror(errno));
1043  return ret;
1044  }
1045  av_log_set_callback(mobileffmpeg_log_callback_function);
1046  av_log(NULL, AV_LOG_INFO,
1047  "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
1048  "Report written to \"%s\"\n",
1049  program_name,
1050  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1051  tm->tm_hour, tm->tm_min, tm->tm_sec,
1052  filename.str);
1053  av_bprint_finalize(&filename, NULL);
1054  return 0;
1055 }
1056 
1057 int opt_report(const char *opt)
1058 {
1059  return init_report(NULL);
1060 }
1061 
1062 int opt_max_alloc(void *optctx, const char *opt, const char *arg)
1063 {
1064  char *tail;
1065  size_t max;
1066 
1067  max = strtol(arg, &tail, 10);
1068  if (*tail) {
1069  av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
1070  exit_program(1);
1071  }
1072  av_max_alloc(max);
1073  return 0;
1074 }
1075 
1076 int opt_timelimit(void *optctx, const char *opt, const char *arg)
1077 {
1078 #if HAVE_SETRLIMIT
1079  int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
1080  struct rlimit rl = { lim, lim + 1 };
1081  if (setrlimit(RLIMIT_CPU, &rl))
1082  perror("setrlimit");
1083 #else
1084  av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
1085 #endif
1086  return 0;
1087 }
1088 
1089 void print_error(const char *filename, int err)
1090 {
1091  char errbuf[128];
1092  const char *errbuf_ptr = errbuf;
1093 
1094  if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1095  errbuf_ptr = strerror(AVUNERROR(err));
1096  av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
1097 }
1098 
1099 static int warned_cfg = 0;
1100 
1101 #define INDENT 1
1102 #define SHOW_VERSION 2
1103 #define SHOW_CONFIG 4
1104 #define SHOW_COPYRIGHT 8
1105 
1106 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
1107  if (CONFIG_##LIBNAME) { \
1108  const char *indent = flags & INDENT? " " : ""; \
1109  if (flags & SHOW_VERSION) { \
1110  unsigned int version = libname##_version(); \
1111  av_log(NULL, level, \
1112  "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
1113  indent, #libname, \
1114  LIB##LIBNAME##_VERSION_MAJOR, \
1115  LIB##LIBNAME##_VERSION_MINOR, \
1116  LIB##LIBNAME##_VERSION_MICRO, \
1117  AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
1118  AV_VERSION_MICRO(version)); \
1119  } \
1120  if (flags & SHOW_CONFIG) { \
1121  const char *cfg = libname##_configuration(); \
1122  if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
1123  if (!warned_cfg) { \
1124  av_log(NULL, level, \
1125  "%sWARNING: library configuration mismatch\n", \
1126  indent); \
1127  warned_cfg = 1; \
1128  } \
1129  av_log(NULL, level, "%s%-11s configuration: %s\n", \
1130  indent, #libname, cfg); \
1131  } \
1132  } \
1133  } \
1134 
1135 static void print_all_libs_info(int flags, int level)
1136 {
1137  PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
1138  PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
1139  PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
1140  PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
1141  PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
1142  PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
1143  PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
1144 }
1145 
1146 static void print_program_info(int flags, int level)
1147 {
1148  const char *indent = flags & INDENT? " " : "";
1149 
1150  av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
1151  if (flags & SHOW_COPYRIGHT)
1152  av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
1153  program_birth_year, CONFIG_THIS_YEAR);
1154  av_log(NULL, level, "\n");
1155  av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
1156 
1157  av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
1158 }
1159 
1160 static void print_buildconf(int flags, int level)
1161 {
1162  const char *indent = flags & INDENT ? " " : "";
1163  char str[] = { FFMPEG_CONFIGURATION };
1164  char *conflist, *remove_tilde, *splitconf;
1165 
1166  // Change all the ' --' strings to '~--' so that
1167  // they can be identified as tokens.
1168  while ((conflist = strstr(str, " --")) != NULL) {
1169  strncpy(conflist, "~--", 3);
1170  }
1171 
1172  // Compensate for the weirdness this would cause
1173  // when passing 'pkg-config --static'.
1174  while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
1175  strncpy(remove_tilde, "pkg-config ", 11);
1176  }
1177 
1178  splitconf = strtok(str, "~");
1179  av_log(NULL, level, "\n%sconfiguration:\n", indent);
1180  while (splitconf != NULL) {
1181  av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
1182  splitconf = strtok(NULL, "~");
1183  }
1184 }
1185 
1186 void show_banner(int argc, char **argv, const OptionDef *options)
1187 {
1188  int idx = locate_option(argc, argv, options, "version");
1189  if (hide_banner || idx)
1190  return;
1191 
1192  print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
1193  print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
1194  print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
1195 }
1196 
1197 int show_version(void *optctx, const char *opt, const char *arg)
1198 {
1199  av_log_set_callback(mobileffmpeg_log_callback_function);
1200  print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
1201  print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
1202 
1203  return 0;
1204 }
1205 
1206 int show_buildconf(void *optctx, const char *opt, const char *arg)
1207 {
1208  av_log_set_callback(mobileffmpeg_log_callback_function);
1209  print_buildconf (INDENT|0, AV_LOG_INFO);
1210 
1211  return 0;
1212 }
1213 
1214 int show_license(void *optctx, const char *opt, const char *arg)
1215 {
1216 #if CONFIG_NONFREE
1217  printf(
1218  "This version of %s has nonfree parts compiled in.\n"
1219  "Therefore it is not legally redistributable.\n",
1220  program_name );
1221 #elif CONFIG_GPLV3
1222  printf(
1223  "%s is free software; you can redistribute it and/or modify\n"
1224  "it under the terms of the GNU General Public License as published by\n"
1225  "the Free Software Foundation; either version 3 of the License, or\n"
1226  "(at your option) any later version.\n"
1227  "\n"
1228  "%s is distributed in the hope that it will be useful,\n"
1229  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1230  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1231  "GNU General Public License for more details.\n"
1232  "\n"
1233  "You should have received a copy of the GNU General Public License\n"
1234  "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1236 #elif CONFIG_GPL
1237  printf(
1238  "%s is free software; you can redistribute it and/or modify\n"
1239  "it under the terms of the GNU General Public License as published by\n"
1240  "the Free Software Foundation; either version 2 of the License, or\n"
1241  "(at your option) any later version.\n"
1242  "\n"
1243  "%s is distributed in the hope that it will be useful,\n"
1244  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1245  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1246  "GNU General Public License for more details.\n"
1247  "\n"
1248  "You should have received a copy of the GNU General Public License\n"
1249  "along with %s; if not, write to the Free Software\n"
1250  "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1252 #elif CONFIG_LGPLV3
1253  printf(
1254  "%s is free software; you can redistribute it and/or modify\n"
1255  "it under the terms of the GNU Lesser General Public License as published by\n"
1256  "the Free Software Foundation; either version 3 of the License, or\n"
1257  "(at your option) any later version.\n"
1258  "\n"
1259  "%s is distributed in the hope that it will be useful,\n"
1260  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1261  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1262  "GNU Lesser General Public License for more details.\n"
1263  "\n"
1264  "You should have received a copy of the GNU Lesser General Public License\n"
1265  "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
1267 #else
1268  printf(
1269  "%s is free software; you can redistribute it and/or\n"
1270  "modify it under the terms of the GNU Lesser General Public\n"
1271  "License as published by the Free Software Foundation; either\n"
1272  "version 2.1 of the License, or (at your option) any later version.\n"
1273  "\n"
1274  "%s is distributed in the hope that it will be useful,\n"
1275  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1276  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1277  "Lesser General Public License for more details.\n"
1278  "\n"
1279  "You should have received a copy of the GNU Lesser General Public\n"
1280  "License along with %s; if not, write to the Free Software\n"
1281  "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
1283 #endif
1284 
1285  return 0;
1286 }
1287 
1288 static int is_device(const AVClass *avclass)
1289 {
1290  if (!avclass)
1291  return 0;
1292  return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
1293 }
1294 
1295 static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
1296 {
1297  void *ifmt_opaque = NULL;
1298  const AVInputFormat *ifmt = NULL;
1299  void *ofmt_opaque = NULL;
1300  const AVOutputFormat *ofmt = NULL;
1301  const char *last_name;
1302  int is_dev;
1303 
1304  printf("%s\n"
1305  " D. = Demuxing supported\n"
1306  " .E = Muxing supported\n"
1307  " --\n", device_only ? "Devices:" : "File formats:");
1308  last_name = "000";
1309  for (;;) {
1310  int decode = 0;
1311  int encode = 0;
1312  const char *name = NULL;
1313  const char *long_name = NULL;
1314 
1315  if (muxdemuxers !=SHOW_DEMUXERS) {
1316  ofmt_opaque = NULL;
1317  while ((ofmt = av_muxer_iterate(&ofmt_opaque))) {
1318  is_dev = is_device(ofmt->priv_class);
1319  if (!is_dev && device_only)
1320  continue;
1321  if ((!name || strcmp(ofmt->name, name) < 0) &&
1322  strcmp(ofmt->name, last_name) > 0) {
1323  name = ofmt->name;
1324  long_name = ofmt->long_name;
1325  encode = 1;
1326  }
1327  }
1328  }
1329  if (muxdemuxers != SHOW_MUXERS) {
1330  ifmt_opaque = NULL;
1331  while ((ifmt = av_demuxer_iterate(&ifmt_opaque))) {
1332  is_dev = is_device(ifmt->priv_class);
1333  if (!is_dev && device_only)
1334  continue;
1335  if ((!name || strcmp(ifmt->name, name) < 0) &&
1336  strcmp(ifmt->name, last_name) > 0) {
1337  name = ifmt->name;
1338  long_name = ifmt->long_name;
1339  encode = 0;
1340  }
1341  if (name && strcmp(ifmt->name, name) == 0)
1342  decode = 1;
1343  }
1344  }
1345  if (!name)
1346  break;
1347  last_name = name;
1348 
1349  printf(" %s%s %-15s %s\n",
1350  decode ? "D" : " ",
1351  encode ? "E" : " ",
1352  name,
1353  long_name ? long_name:" ");
1354  }
1355  return 0;
1356 }
1357 
1358 int show_formats(void *optctx, const char *opt, const char *arg)
1359 {
1360  return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
1361 }
1362 
1363 int show_muxers(void *optctx, const char *opt, const char *arg)
1364 {
1365  return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
1366 }
1367 
1368 int show_demuxers(void *optctx, const char *opt, const char *arg)
1369 {
1370  return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
1371 }
1372 
1373 int show_devices(void *optctx, const char *opt, const char *arg)
1374 {
1375  return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
1376 }
1377 
1378 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
1379  if (codec->field) { \
1380  const type *p = codec->field; \
1381  \
1382  printf(" Supported " list_name ":"); \
1383  while (*p != term) { \
1384  get_name(*p); \
1385  printf(" %s", name); \
1386  p++; \
1387  } \
1388  printf("\n"); \
1389  } \
1390 
1391 static void print_codec(const AVCodec *c)
1392 {
1393  int encoder = av_codec_is_encoder(c);
1394 
1395  printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
1396  c->long_name ? c->long_name : "");
1397 
1398  printf(" General capabilities: ");
1399  if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
1400  printf("horizband ");
1401  if (c->capabilities & AV_CODEC_CAP_DR1)
1402  printf("dr1 ");
1403  if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
1404  printf("trunc ");
1405  if (c->capabilities & AV_CODEC_CAP_DELAY)
1406  printf("delay ");
1407  if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
1408  printf("small ");
1409  if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
1410  printf("subframes ");
1411  if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
1412  printf("exp ");
1413  if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
1414  printf("chconf ");
1415  if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
1416  printf("paramchange ");
1417  if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
1418  printf("variable ");
1419  if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1420  AV_CODEC_CAP_SLICE_THREADS |
1421  AV_CODEC_CAP_AUTO_THREADS))
1422  printf("threads ");
1423  if (c->capabilities & AV_CODEC_CAP_AVOID_PROBING)
1424  printf("avoidprobe ");
1425  if (c->capabilities & AV_CODEC_CAP_INTRA_ONLY)
1426  printf("intraonly ");
1427  if (c->capabilities & AV_CODEC_CAP_LOSSLESS)
1428  printf("lossless ");
1429  if (c->capabilities & AV_CODEC_CAP_HARDWARE)
1430  printf("hardware ");
1431  if (c->capabilities & AV_CODEC_CAP_HYBRID)
1432  printf("hybrid ");
1433  if (!c->capabilities)
1434  printf("none");
1435  printf("\n");
1436 
1437  if (c->type == AVMEDIA_TYPE_VIDEO ||
1438  c->type == AVMEDIA_TYPE_AUDIO) {
1439  printf(" Threading capabilities: ");
1440  switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
1441  AV_CODEC_CAP_SLICE_THREADS |
1442  AV_CODEC_CAP_AUTO_THREADS)) {
1443  case AV_CODEC_CAP_FRAME_THREADS |
1444  AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
1445  case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
1446  case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
1447  case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
1448  default: printf("none"); break;
1449  }
1450  printf("\n");
1451  }
1452 
1453  if (avcodec_get_hw_config(c, 0)) {
1454  printf(" Supported hardware devices: ");
1455  for (int i = 0;; i++) {
1456  const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
1457  if (!config)
1458  break;
1459  printf("%s ", av_hwdevice_get_type_name(config->device_type));
1460  }
1461  printf("\n");
1462  }
1463 
1464  if (c->supported_framerates) {
1465  const AVRational *fps = c->supported_framerates;
1466 
1467  printf(" Supported framerates:");
1468  while (fps->num) {
1469  printf(" %d/%d", fps->num, fps->den);
1470  fps++;
1471  }
1472  printf("\n");
1473  }
1474  PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1475  AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
1476  PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1478  PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1479  AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
1480  PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1481  0, GET_CH_LAYOUT_DESC);
1482 
1483  if (c->priv_class) {
1484  show_help_children(c->priv_class,
1485  AV_OPT_FLAG_ENCODING_PARAM |
1486  AV_OPT_FLAG_DECODING_PARAM);
1487  }
1488 }
1489 
1490 static char get_media_type_char(enum AVMediaType type)
1491 {
1492  switch (type) {
1493  case AVMEDIA_TYPE_VIDEO: return 'V';
1494  case AVMEDIA_TYPE_AUDIO: return 'A';
1495  case AVMEDIA_TYPE_DATA: return 'D';
1496  case AVMEDIA_TYPE_SUBTITLE: return 'S';
1497  case AVMEDIA_TYPE_ATTACHMENT:return 'T';
1498  default: return '?';
1499  }
1500 }
1501 
1502 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1503  int encoder)
1504 {
1505  while ((prev = av_codec_next(prev))) {
1506  if (prev->id == id &&
1507  (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1508  return prev;
1509  }
1510  return NULL;
1511 }
1512 
1513 static int compare_codec_desc(const void *a, const void *b)
1514 {
1515  const AVCodecDescriptor * const *da = a;
1516  const AVCodecDescriptor * const *db = b;
1517 
1518  return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
1519  strcmp((*da)->name, (*db)->name);
1520 }
1521 
1522 static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
1523 {
1524  const AVCodecDescriptor *desc = NULL;
1525  const AVCodecDescriptor **codecs;
1526  unsigned nb_codecs = 0, i = 0;
1527 
1528  while ((desc = avcodec_descriptor_next(desc)))
1529  nb_codecs++;
1530  if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
1531  av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
1532  exit_program(1);
1533  }
1534  desc = NULL;
1535  while ((desc = avcodec_descriptor_next(desc)))
1536  codecs[i++] = desc;
1537  av_assert0(i == nb_codecs);
1538  qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
1539  *rcodecs = codecs;
1540  return nb_codecs;
1541 }
1542 
1543 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1544 {
1545  const AVCodec *codec = NULL;
1546 
1547  printf(" (%s: ", encoder ? "encoders" : "decoders");
1548 
1549  while ((codec = next_codec_for_id(id, codec, encoder)))
1550  printf("%s ", codec->name);
1551 
1552  printf(")");
1553 }
1554 
1555 int show_codecs(void *optctx, const char *opt, const char *arg)
1556 {
1557  const AVCodecDescriptor **codecs;
1558  unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1559 
1560  printf("Codecs:\n"
1561  " D..... = Decoding supported\n"
1562  " .E.... = Encoding supported\n"
1563  " ..V... = Video codec\n"
1564  " ..A... = Audio codec\n"
1565  " ..S... = Subtitle codec\n"
1566  " ...I.. = Intra frame-only codec\n"
1567  " ....L. = Lossy compression\n"
1568  " .....S = Lossless compression\n"
1569  " -------\n");
1570  for (i = 0; i < nb_codecs; i++) {
1571  const AVCodecDescriptor *desc = codecs[i];
1572  const AVCodec *codec = NULL;
1573 
1574  if (strstr(desc->name, "_deprecated"))
1575  continue;
1576 
1577  printf(" ");
1578  printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1579  printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1580 
1581  printf("%c", get_media_type_char(desc->type));
1582  printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1583  printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1584  printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1585 
1586  printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1587 
1588  /* print decoders/encoders when there's more than one or their
1589  * names are different from codec name */
1590  while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1591  if (strcmp(codec->name, desc->name)) {
1592  print_codecs_for_id(desc->id, 0);
1593  break;
1594  }
1595  }
1596  codec = NULL;
1597  while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1598  if (strcmp(codec->name, desc->name)) {
1599  print_codecs_for_id(desc->id, 1);
1600  break;
1601  }
1602  }
1603 
1604  printf("\n");
1605  }
1606  av_free(codecs);
1607  return 0;
1608 }
1609 
1610 static void print_codecs(int encoder)
1611 {
1612  const AVCodecDescriptor **codecs;
1613  unsigned i, nb_codecs = get_codecs_sorted(&codecs);
1614 
1615  printf("%s:\n"
1616  " V..... = Video\n"
1617  " A..... = Audio\n"
1618  " S..... = Subtitle\n"
1619  " .F.... = Frame-level multithreading\n"
1620  " ..S... = Slice-level multithreading\n"
1621  " ...X.. = Codec is experimental\n"
1622  " ....B. = Supports draw_horiz_band\n"
1623  " .....D = Supports direct rendering method 1\n"
1624  " ------\n",
1625  encoder ? "Encoders" : "Decoders");
1626  for (i = 0; i < nb_codecs; i++) {
1627  const AVCodecDescriptor *desc = codecs[i];
1628  const AVCodec *codec = NULL;
1629 
1630  while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1631  printf(" %c", get_media_type_char(desc->type));
1632  printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1633  printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1634  printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1635  printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
1636  printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
1637 
1638  printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1639  if (strcmp(codec->name, desc->name))
1640  printf(" (codec %s)", desc->name);
1641 
1642  printf("\n");
1643  }
1644  }
1645  av_free(codecs);
1646 }
1647 
1648 int show_decoders(void *optctx, const char *opt, const char *arg)
1649 {
1650  print_codecs(0);
1651  return 0;
1652 }
1653 
1654 int show_encoders(void *optctx, const char *opt, const char *arg)
1655 {
1656  print_codecs(1);
1657  return 0;
1658 }
1659 
1660 int show_bsfs(void *optctx, const char *opt, const char *arg)
1661 {
1662  const AVBitStreamFilter *bsf = NULL;
1663  void *opaque = NULL;
1664 
1665  printf("Bitstream filters:\n");
1666  while ((bsf = av_bsf_iterate(&opaque)))
1667  printf("%s\n", bsf->name);
1668  printf("\n");
1669  return 0;
1670 }
1671 
1672 int show_protocols(void *optctx, const char *opt, const char *arg)
1673 {
1674  void *opaque = NULL;
1675  const char *name;
1676 
1677  printf("Supported file protocols:\n"
1678  "Input:\n");
1679  while ((name = avio_enum_protocols(&opaque, 0)))
1680  printf(" %s\n", name);
1681  printf("Output:\n");
1682  while ((name = avio_enum_protocols(&opaque, 1)))
1683  printf(" %s\n", name);
1684  return 0;
1685 }
1686 
1687 int show_filters(void *optctx, const char *opt, const char *arg)
1688 {
1689 #if CONFIG_AVFILTER
1690  const AVFilter *filter = NULL;
1691  char descr[64], *descr_cur;
1692  void *opaque = NULL;
1693  int i, j;
1694  const AVFilterPad *pad;
1695 
1696  printf("Filters:\n"
1697  " T.. = Timeline support\n"
1698  " .S. = Slice threading\n"
1699  " ..C = Command support\n"
1700  " A = Audio input/output\n"
1701  " V = Video input/output\n"
1702  " N = Dynamic number and/or type of input/output\n"
1703  " | = Source or sink filter\n");
1704  while ((filter = av_filter_iterate(&opaque))) {
1705  descr_cur = descr;
1706  for (i = 0; i < 2; i++) {
1707  if (i) {
1708  *(descr_cur++) = '-';
1709  *(descr_cur++) = '>';
1710  }
1711  pad = i ? filter->outputs : filter->inputs;
1712  for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
1713  if (descr_cur >= descr + sizeof(descr) - 4)
1714  break;
1715  *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
1716  }
1717  if (!j)
1718  *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
1719  ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
1720  }
1721  *descr_cur = 0;
1722  printf(" %c%c%c %-17s %-10s %s\n",
1723  filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
1724  filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
1725  filter->process_command ? 'C' : '.',
1726  filter->name, descr, filter->description);
1727  }
1728 #else
1729  printf("No filters available: libavfilter disabled\n");
1730 #endif
1731  return 0;
1732 }
1733 
1734 int show_colors(void *optctx, const char *opt, const char *arg)
1735 {
1736  const char *name;
1737  const uint8_t *rgb;
1738  int i;
1739 
1740  printf("%-32s #RRGGBB\n", "name");
1741 
1742  for (i = 0; (name = av_get_known_color_name(i, &rgb)); i++)
1743  printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
1744 
1745  return 0;
1746 }
1747 
1748 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1749 {
1750  const AVPixFmtDescriptor *pix_desc = NULL;
1751 
1752  printf("Pixel formats:\n"
1753  "I.... = Supported Input format for conversion\n"
1754  ".O... = Supported Output format for conversion\n"
1755  "..H.. = Hardware accelerated format\n"
1756  "...P. = Paletted format\n"
1757  "....B = Bitstream format\n"
1758  "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1759  "-----\n");
1760 
1761 #if !CONFIG_SWSCALE
1762 # define sws_isSupportedInput(x) 0
1763 # define sws_isSupportedOutput(x) 0
1764 #endif
1765 
1766  while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1767  enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1768  printf("%c%c%c%c%c %-16s %d %2d\n",
1769  sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1770  sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1771  pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1772  pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1773  pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1774  pix_desc->name,
1775  pix_desc->nb_components,
1776  av_get_bits_per_pixel(pix_desc));
1777  }
1778  return 0;
1779 }
1780 
1781 int show_layouts(void *optctx, const char *opt, const char *arg)
1782 {
1783  int i = 0;
1784  uint64_t layout, j;
1785  const char *name, *descr;
1786 
1787  printf("Individual channels:\n"
1788  "NAME DESCRIPTION\n");
1789  for (i = 0; i < 63; i++) {
1790  name = av_get_channel_name((uint64_t)1 << i);
1791  if (!name)
1792  continue;
1793  descr = av_get_channel_description((uint64_t)1 << i);
1794  printf("%-14s %s\n", name, descr);
1795  }
1796  printf("\nStandard channel layouts:\n"
1797  "NAME DECOMPOSITION\n");
1798  for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
1799  if (name) {
1800  printf("%-14s ", name);
1801  for (j = 1; j; j <<= 1)
1802  if ((layout & j))
1803  printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
1804  printf("\n");
1805  }
1806  }
1807  return 0;
1808 }
1809 
1810 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1811 {
1812  int i;
1813  char fmt_str[128];
1814  for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1815  printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1816  return 0;
1817 }
1818 
1819 static void show_help_codec(const char *name, int encoder)
1820 {
1821  const AVCodecDescriptor *desc;
1822  const AVCodec *codec;
1823 
1824  if (!name) {
1825  av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1826  return;
1827  }
1828 
1829  codec = encoder ? avcodec_find_encoder_by_name(name) :
1830  avcodec_find_decoder_by_name(name);
1831 
1832  if (codec)
1833  print_codec(codec);
1834  else if ((desc = avcodec_descriptor_get_by_name(name))) {
1835  int printed = 0;
1836 
1837  while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1838  printed = 1;
1839  print_codec(codec);
1840  }
1841 
1842  if (!printed) {
1843  av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
1844  "but no %s for it are available. FFmpeg might need to be "
1845  "recompiled with additional external libraries.\n",
1846  name, encoder ? "encoders" : "decoders");
1847  }
1848  } else {
1849  av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
1850  name);
1851  }
1852 }
1853 
1854 static void show_help_demuxer(const char *name)
1855 {
1856  const AVInputFormat *fmt = av_find_input_format(name);
1857 
1858  if (!fmt) {
1859  av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1860  return;
1861  }
1862 
1863  printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1864 
1865  if (fmt->extensions)
1866  printf(" Common extensions: %s.\n", fmt->extensions);
1867 
1868  if (fmt->priv_class)
1869  show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
1870 }
1871 
1872 static void show_help_muxer(const char *name)
1873 {
1874  const AVCodecDescriptor *desc;
1875  const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1876 
1877  if (!fmt) {
1878  av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1879  return;
1880  }
1881 
1882  printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1883 
1884  if (fmt->extensions)
1885  printf(" Common extensions: %s.\n", fmt->extensions);
1886  if (fmt->mime_type)
1887  printf(" Mime type: %s.\n", fmt->mime_type);
1888  if (fmt->video_codec != AV_CODEC_ID_NONE &&
1889  (desc = avcodec_descriptor_get(fmt->video_codec))) {
1890  printf(" Default video codec: %s.\n", desc->name);
1891  }
1892  if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1893  (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1894  printf(" Default audio codec: %s.\n", desc->name);
1895  }
1896  if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1897  (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1898  printf(" Default subtitle codec: %s.\n", desc->name);
1899  }
1900 
1901  if (fmt->priv_class)
1902  show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
1903 }
1904 
1905 #if CONFIG_AVFILTER
1906 static void show_help_filter(const char *name)
1907 {
1908 #if CONFIG_AVFILTER
1909  const AVFilter *f = avfilter_get_by_name(name);
1910  int i, count;
1911 
1912  if (!name) {
1913  av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1914  return;
1915  } else if (!f) {
1916  av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1917  return;
1918  }
1919 
1920  printf("Filter %s\n", f->name);
1921  if (f->description)
1922  printf(" %s\n", f->description);
1923 
1924  if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
1925  printf(" slice threading supported\n");
1926 
1927  printf(" Inputs:\n");
1928  count = avfilter_pad_count(f->inputs);
1929  for (i = 0; i < count; i++) {
1930  printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1931  media_type_string(avfilter_pad_get_type(f->inputs, i)));
1932  }
1933  if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
1934  printf(" dynamic (depending on the options)\n");
1935  else if (!count)
1936  printf(" none (source filter)\n");
1937 
1938  printf(" Outputs:\n");
1939  count = avfilter_pad_count(f->outputs);
1940  for (i = 0; i < count; i++) {
1941  printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1942  media_type_string(avfilter_pad_get_type(f->outputs, i)));
1943  }
1944  if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
1945  printf(" dynamic (depending on the options)\n");
1946  else if (!count)
1947  printf(" none (sink filter)\n");
1948 
1949  if (f->priv_class)
1950  show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
1951  AV_OPT_FLAG_AUDIO_PARAM);
1952  if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
1953  printf("This filter has support for timeline through the 'enable' option.\n");
1954 #else
1955  av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
1956  "can not to satisfy request\n");
1957 #endif
1958 }
1959 #endif
1960 
1961 static void show_help_bsf(const char *name)
1962 {
1963  const AVBitStreamFilter *bsf = av_bsf_get_by_name(name);
1964 
1965  if (!name) {
1966  av_log(NULL, AV_LOG_ERROR, "No bitstream filter name specified.\n");
1967  return;
1968  } else if (!bsf) {
1969  av_log(NULL, AV_LOG_ERROR, "Unknown bit stream filter '%s'.\n", name);
1970  return;
1971  }
1972 
1973  printf("Bit stream filter %s\n", bsf->name);
1974  PRINT_CODEC_SUPPORTED(bsf, codec_ids, enum AVCodecID, "codecs",
1975  AV_CODEC_ID_NONE, GET_CODEC_NAME);
1976  if (bsf->priv_class)
1977  show_help_children(bsf->priv_class, AV_OPT_FLAG_BSF_PARAM);
1978 }
1979 
1980 int show_help(void *optctx, const char *opt, const char *arg)
1981 {
1982  char *topic, *par;
1983  av_log_set_callback(mobileffmpeg_log_callback_function);
1984 
1985  topic = av_strdup(arg ? arg : "");
1986  if (!topic)
1987  return AVERROR(ENOMEM);
1988  par = strchr(topic, '=');
1989  if (par)
1990  *par++ = 0;
1991 
1992  if (!*topic) {
1993  show_help_default(topic, par);
1994  } else if (!strcmp(topic, "decoder")) {
1995  show_help_codec(par, 0);
1996  } else if (!strcmp(topic, "encoder")) {
1997  show_help_codec(par, 1);
1998  } else if (!strcmp(topic, "demuxer")) {
1999  show_help_demuxer(par);
2000  } else if (!strcmp(topic, "muxer")) {
2001  show_help_muxer(par);
2002 #if CONFIG_AVFILTER
2003  } else if (!strcmp(topic, "filter")) {
2004  show_help_filter(par);
2005 #endif
2006  } else if (!strcmp(topic, "bsf")) {
2007  show_help_bsf(par);
2008  } else {
2009  show_help_default(topic, par);
2010  }
2011 
2012  av_freep(&topic);
2013  return 0;
2014 }
2015 
2016 int read_yesno(void)
2017 {
2018  int c = getchar();
2019  int yesno = (av_toupper(c) == 'Y');
2020 
2021  while (c != '\n' && c != EOF)
2022  c = getchar();
2023 
2024  return yesno;
2025 }
2026 
2027 FILE *get_preset_file(char *filename, size_t filename_size,
2028  const char *preset_name, int is_path,
2029  const char *codec_name)
2030 {
2031  FILE *f = NULL;
2032  int i;
2033  const char *base[3] = { getenv("FFMPEG_DATADIR"),
2034  getenv("HOME"),
2035  FFMPEG_DATADIR, };
2036 
2037  if (is_path) {
2038  av_strlcpy(filename, preset_name, filename_size);
2039  f = fopen(filename, "r");
2040  } else {
2041 #ifdef _WIN32
2042  char datadir[MAX_PATH], *ls;
2043  base[2] = NULL;
2044 
2045  if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
2046  {
2047  for (ls = datadir; ls < datadir + strlen(datadir); ls++)
2048  if (*ls == '\\') *ls = '/';
2049 
2050  if (ls = strrchr(datadir, '/'))
2051  {
2052  *ls = 0;
2053  strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
2054  base[2] = datadir;
2055  }
2056  }
2057 #endif
2058  for (i = 0; i < 3 && !f; i++) {
2059  if (!base[i])
2060  continue;
2061  snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
2062  i != 1 ? "" : "/.ffmpeg", preset_name);
2063  f = fopen(filename, "r");
2064  if (!f && codec_name) {
2065  snprintf(filename, filename_size,
2066  "%s%s/%s-%s.ffpreset",
2067  base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
2068  preset_name);
2069  f = fopen(filename, "r");
2070  }
2071  }
2072  }
2073 
2074  return f;
2075 }
2076 
2077 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
2078 {
2079  int ret = avformat_match_stream_specifier(s, st, spec);
2080  if (ret < 0)
2081  av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
2082  return ret;
2083 }
2084 
2085 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
2086  AVFormatContext *s, AVStream *st, AVCodec *codec)
2087 {
2088  AVDictionary *ret = NULL;
2089  AVDictionaryEntry *t = NULL;
2090  int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
2091  : AV_OPT_FLAG_DECODING_PARAM;
2092  char prefix = 0;
2093  const AVClass *cc = avcodec_get_class();
2094 
2095  if (!codec)
2096  codec = s->oformat ? avcodec_find_encoder(codec_id)
2097  : avcodec_find_decoder(codec_id);
2098 
2099  switch (st->codecpar->codec_type) {
2100  case AVMEDIA_TYPE_VIDEO:
2101  prefix = 'v';
2102  flags |= AV_OPT_FLAG_VIDEO_PARAM;
2103  break;
2104  case AVMEDIA_TYPE_AUDIO:
2105  prefix = 'a';
2106  flags |= AV_OPT_FLAG_AUDIO_PARAM;
2107  break;
2108  case AVMEDIA_TYPE_SUBTITLE:
2109  prefix = 's';
2110  flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
2111  break;
2112  }
2113 
2114  while ((t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX))) {
2115  char *p = strchr(t->key, ':');
2116 
2117  /* check stream specification in opt name */
2118  if (p)
2119  switch (check_stream_specifier(s, st, p + 1)) {
2120  case 1: *p = 0; break;
2121  case 0: continue;
2122  default: exit_program(1);
2123  }
2124 
2125  if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
2126  !codec ||
2127  (codec->priv_class &&
2128  av_opt_find(&codec->priv_class, t->key, NULL, flags,
2129  AV_OPT_SEARCH_FAKE_OBJ)))
2130  av_dict_set(&ret, t->key, t->value, 0);
2131  else if (t->key[0] == prefix &&
2132  av_opt_find(&cc, t->key + 1, NULL, flags,
2133  AV_OPT_SEARCH_FAKE_OBJ))
2134  av_dict_set(&ret, t->key + 1, t->value, 0);
2135 
2136  if (p)
2137  *p = ':';
2138  }
2139  return ret;
2140 }
2141 
2142 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
2143  AVDictionary *codec_opts)
2144 {
2145  int i;
2146  AVDictionary **opts;
2147 
2148  if (!s->nb_streams)
2149  return NULL;
2150  opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
2151  if (!opts) {
2152  av_log(NULL, AV_LOG_ERROR,
2153  "Could not alloc memory for stream options.\n");
2154  return NULL;
2155  }
2156  for (i = 0; i < s->nb_streams; i++)
2157  opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
2158  s, s->streams[i], NULL);
2159  return opts;
2160 }
2161 
2162 void *grow_array(void *array, int elem_size, int *size, int new_size)
2163 {
2164  if (new_size >= INT_MAX / elem_size) {
2165  av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
2166  exit_program(1);
2167  }
2168  if (*size < new_size) {
2169  uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
2170  if (!tmp) {
2171  av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
2172  exit_program(1);
2173  }
2174  memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
2175  *size = new_size;
2176  return tmp;
2177  }
2178  return array;
2179 }
2180 
2181 double get_rotation(AVStream *st)
2182 {
2183  uint8_t* displaymatrix = av_stream_get_side_data(st,
2184  AV_PKT_DATA_DISPLAYMATRIX, NULL);
2185  double theta = 0;
2186  if (displaymatrix)
2187  theta = -av_display_rotation_get((int32_t*) displaymatrix);
2188 
2189  theta -= 360*floor(theta/360 + 0.9/360);
2190 
2191  if (fabs(theta - 90*round(theta/90)) > 2)
2192  av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
2193  "If you want to help, upload a sample "
2194  "of this file to ftp://upload.ffmpeg.org/incoming/ "
2195  "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
2196 
2197  return theta;
2198 }
2199 
2200 #if CONFIG_AVDEVICE
2201 static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
2202 {
2203  int ret, i;
2204  AVDeviceInfoList *device_list = NULL;
2205 
2206  if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
2207  return AVERROR(EINVAL);
2208 
2209  printf("Auto-detected sources for %s:\n", fmt->name);
2210  if (!fmt->get_device_list) {
2211  ret = AVERROR(ENOSYS);
2212  printf("Cannot list sources. Not implemented.\n");
2213  goto fail;
2214  }
2215 
2216  if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
2217  printf("Cannot list sources.\n");
2218  goto fail;
2219  }
2220 
2221  for (i = 0; i < device_list->nb_devices; i++) {
2222  printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
2223  device_list->devices[i]->device_name, device_list->devices[i]->device_description);
2224  }
2225 
2226  fail:
2227  avdevice_free_list_devices(&device_list);
2228  return ret;
2229 }
2230 
2231 static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
2232 {
2233  int ret, i;
2234  AVDeviceInfoList *device_list = NULL;
2235 
2236  if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
2237  return AVERROR(EINVAL);
2238 
2239  printf("Auto-detected sinks for %s:\n", fmt->name);
2240  if (!fmt->get_device_list) {
2241  ret = AVERROR(ENOSYS);
2242  printf("Cannot list sinks. Not implemented.\n");
2243  goto fail;
2244  }
2245 
2246  if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
2247  printf("Cannot list sinks.\n");
2248  goto fail;
2249  }
2250 
2251  for (i = 0; i < device_list->nb_devices; i++) {
2252  printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
2253  device_list->devices[i]->device_name, device_list->devices[i]->device_description);
2254  }
2255 
2256  fail:
2257  avdevice_free_list_devices(&device_list);
2258  return ret;
2259 }
2260 
2261 static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
2262 {
2263  int ret;
2264  if (arg) {
2265  char *opts_str = NULL;
2266  av_assert0(dev && opts);
2267  *dev = av_strdup(arg);
2268  if (!*dev)
2269  return AVERROR(ENOMEM);
2270  if ((opts_str = strchr(*dev, ','))) {
2271  *(opts_str++) = '\0';
2272  if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
2273  av_freep(dev);
2274  return ret;
2275  }
2276  }
2277  } else
2278  printf("\nDevice name is not provided.\n"
2279  "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
2280  return 0;
2281 }
2282 
2283 int show_sources(void *optctx, const char *opt, const char *arg)
2284 {
2285  AVInputFormat *fmt = NULL;
2286  char *dev = NULL;
2287  AVDictionary *opts = NULL;
2288  int ret = 0;
2289  int error_level = av_log_get_level();
2290 
2291  av_log_set_level(AV_LOG_ERROR);
2292 
2293  if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
2294  goto fail;
2295 
2296  do {
2297  fmt = av_input_audio_device_next(fmt);
2298  if (fmt) {
2299  if (!strcmp(fmt->name, "lavfi"))
2300  continue; //it's pointless to probe lavfi
2301  if (dev && !av_match_name(dev, fmt->name))
2302  continue;
2303  print_device_sources(fmt, opts);
2304  }
2305  } while (fmt);
2306  do {
2307  fmt = av_input_video_device_next(fmt);
2308  if (fmt) {
2309  if (dev && !av_match_name(dev, fmt->name))
2310  continue;
2311  print_device_sources(fmt, opts);
2312  }
2313  } while (fmt);
2314  fail:
2315  av_dict_free(&opts);
2316  av_free(dev);
2317  av_log_set_level(error_level);
2318  return ret;
2319 }
2320 
2321 int show_sinks(void *optctx, const char *opt, const char *arg)
2322 {
2323  AVOutputFormat *fmt = NULL;
2324  char *dev = NULL;
2325  AVDictionary *opts = NULL;
2326  int ret = 0;
2327  int error_level = av_log_get_level();
2328 
2329  av_log_set_level(AV_LOG_ERROR);
2330 
2331  if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
2332  goto fail;
2333 
2334  do {
2335  fmt = av_output_audio_device_next(fmt);
2336  if (fmt) {
2337  if (dev && !av_match_name(dev, fmt->name))
2338  continue;
2339  print_device_sinks(fmt, opts);
2340  }
2341  } while (fmt);
2342  do {
2343  fmt = av_output_video_device_next(fmt);
2344  if (fmt) {
2345  if (dev && !av_match_name(dev, fmt->name))
2346  continue;
2347  print_device_sinks(fmt, opts);
2348  }
2349  } while (fmt);
2350  fail:
2351  av_dict_free(&opts);
2352  av_free(dev);
2353  av_log_set_level(error_level);
2354  return ret;
2355 }
2356 
2357 #endif
#define SHOW_CONFIG
#define OPT_PERFILE
static void dump_argument(const char *a)
int opt_default(void *optctx, const char *opt, const char *arg)
int show_layouts(void *optctx, const char *opt, const char *arg)
void init_dynload(void)
static int match_group_separator(const OptionGroupDef *groups, int nb_groups, const char *opt)
int show_formats(void *optctx, const char *opt, const char *arg)
int show_encoders(void *optctx, const char *opt, const char *arg)
#define OPT_EXIT
int show_muxers(void *optctx, const char *opt, const char *arg)
#define PRINT_LIB_INFO(libname, LIBNAME, flags, level)
void mobileffmpeg_log_callback_function(void *ptr, int level, const char *format, va_list vargs)
#define GET_ARG(arg)
static const AVCodec * next_codec_for_id(enum AVCodecID id, const AVCodec *prev, int encoder)
static void print_program_info(int flags, int level)
AVDictionary * sws_dict
int opt_report(const char *opt)
static FILE * report_file
static void print_all_libs_info(int flags, int level)
int show_colors(void *optctx, const char *opt, const char *arg)
static const OptionDef * find_option(const OptionDef *po, const char *name)
#define OPT_SPEC
#define sws_isSupportedOutput(x)
static int report_file_level
FILE * get_preset_file(char *filename, size_t filename_size, const char *preset_name, int is_path, const char *codec_name)
static int init_report(const char *env)
void * grow_array(void *array, int elem_size, int *size, int new_size)
int show_help(void *optctx, const char *opt, const char *arg)
void uninit_opts(void)
const OptionGroupDef * group_def
static void(* program_exit)(int ret)
static const AVOption * opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
int show_demuxers(void *optctx, const char *opt, const char *arg)
int show_buildconf(void *optctx, const char *opt, const char *arg)
OptionGroupList * groups
#define GROW_ARRAY(array, nb_elems)
#define GET_CODEC_NAME(id)
static void show_help_codec(const char *name, int encoder)
int show_pix_fmts(void *optctx, const char *opt, const char *arg)
AVDictionary * format_opts
const int program_birth_year
const OptionDef * opt
AVDictionary * resample_opts
static void show_help_muxer(const char *name)
#define sws_isSupportedInput(x)
static void print_codecs(int encoder)
const char * name
#define OPT_FLOAT
const char * help
#define GET_PIX_FMT_NAME(pix_fmt)
int time
Definition: Statistics.m:27
AVDictionary * format_opts
union OptionDef::@1 u
void register_exit(void(*cb)(int ret))
static char get_media_type_char(enum AVMediaType type)
const OptionDef options[]
AVDictionary * resample_opts
const OptionGroupDef * group_def
jmp_buf ex_buf__
static void expand_filename_template(AVBPrint *bp, const char *template, struct tm *tm)
#define OPT_OFFSET
int hide_banner
void exit_program(int ret)
#define OPT_INT64
static void print_codecs_for_id(enum AVCodecID id, int encoder)
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *, const char *))
NSString * format
static void show_help_bsf(const char *name)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
#define SHOW_COPYRIGHT
int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
void print_error(const char *filename, int err)
show_muxdemuxers
static void show_help_demuxer(const char *name)
int show_version(void *optctx, const char *opt, const char *arg)
int opt_loglevel(void *optctx, const char *opt, const char *arg)
#define OPT_INPUT
#define OPT_TIME
#define GET_SAMPLE_RATE_NAME(rate)
const char * key
void uninit_parse_context(OptionParseContext *octx)
NSString * type
const char * val
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
int show_codecs(void *optctx, const char *opt, const char *arg)
#define GET_SAMPLE_FMT_NAME(sample_fmt)
int longjmp_value
NSString * codec
const char program_name[]
void init_opts(void)
#define OPT_INT
AVDictionary * codec_opts
int parse_optgroup(void *optctx, OptionGroup *g)
#define GET_CH_LAYOUT_DESC(ch_layout)
int show_bsfs(void *optctx, const char *opt, const char *arg)
const char * argname
static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
void parse_loglevel(int argc, char **argv, const OptionDef *options)
AVDictionary * sws_dict
const char * sep
int opt_cpuflags(void *optctx, const char *opt, const char *arg)
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, AVCodec *codec)
#define OPT_BOOL
void show_help_children(const AVClass *class, int flags)
#define INDENT
AVDictionary * codec_opts
int show_license(void *optctx, const char *opt, const char *arg)
static void print_buildconf(int flags, int level)
#define OPT_STRING
int show_filters(void *optctx, const char *opt, const char *arg)
int opt_max_alloc(void *optctx, const char *opt, const char *arg)
static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
static int warned_cfg
double get_rotation(AVStream *st)
#define FLAGS
void show_help_default(const char *opt, const char *arg)
int show_devices(void *optctx, const char *opt, const char *arg)
int show_sample_fmts(void *optctx, const char *opt, const char *arg)
const char * arg
AVDictionary * swr_opts
#define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name)
static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
#define media_type_string
int opt_timelimit(void *optctx, const char *opt, const char *arg)
static int compare_codec_desc(const void *a, const void *b)
#define SHOW_VERSION
static void check_options(const OptionDef *po)
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
int show_protocols(void *optctx, const char *opt, const char *arg)
int show_decoders(void *optctx, const char *opt, const char *arg)
static void print_codec(const AVCodec *c)
int(* func_arg)(void *, const char *, const char *)
#define HAS_ARG
static void init_parse_context(OptionParseContext *octx, const OptionGroupDef *groups, int nb_groups)
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
int split_commandline(OptionParseContext *octx, int argc, char *argv[], const OptionDef *options, const OptionGroupDef *groups, int nb_groups)
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
static void finish_group(OptionParseContext *octx, int group_idx, const char *arg)
void show_banner(int argc, char **argv, const OptionDef *options)
static int write_option(void *optctx, const OptionDef *po, const char *opt, const char *arg)
static void add_opt(OptionParseContext *octx, const OptionDef *opt, const char *key, const char *val)
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
#define OPT_DOUBLE
OptionGroup * groups
int read_yesno(void)
static int is_device(const AVClass *avclass)
#define OPT_OUTPUT
AVDictionary * swr_opts
const char * name
long size
Definition: Statistics.m:26