Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <math.h>
27 
28 /* Include only the enabled headers since some compilers (namely, Sun
29  Studio) will not omit unused inline functions and create undefined
30  references to libraries that are not being built. */
31 
32 #include "config.h"
33 #include "libavformat/avformat.h"
34 #include "libavfilter/avfilter.h"
35 #include "libavdevice/avdevice.h"
37 #include "libswscale/swscale.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/avstring.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/parseutils.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/eval.h"
45 #include "libavutil/dict.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/cpu.h"
48 #include "cmdutils.h"
49 #include "version.h"
50 #if CONFIG_NETWORK
51 #include "libavformat/network.h"
52 #endif
53 #if HAVE_SYS_RESOURCE_H
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #endif
57 
60 
61 static const int this_year = 2016;
62 
63 void init_opts(void)
64 {
65 #if CONFIG_SWSCALE
66  sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
67  NULL, NULL, NULL);
68 #endif
69 }
70 
71 void uninit_opts(void)
72 {
73 #if CONFIG_SWSCALE
74  sws_freeContext(sws_opts);
75  sws_opts = NULL;
76 #endif
77  av_dict_free(&format_opts);
78  av_dict_free(&codec_opts);
79  av_dict_free(&resample_opts);
80 }
81 
82 void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
83 {
84  vfprintf(stdout, fmt, vl);
85 }
86 
87 static void (*program_exit)(int ret);
88 
89 void register_exit(void (*cb)(int ret))
90 {
91  program_exit = cb;
92 }
93 
94 void exit_program(int ret)
95 {
96  if (program_exit)
97  program_exit(ret);
98 
99  exit(ret);
100 }
101 
102 double parse_number_or_die(const char *context, const char *numstr, int type,
103  double min, double max)
104 {
105  char *tail;
106  const char *error;
107  double d = av_strtod(numstr, &tail);
108  if (*tail)
109  error = "Expected number for %s but found: %s\n";
110  else if (d < min || d > max)
111  error = "The value for %s was %s which is not within %f - %f\n";
112  else if (type == OPT_INT64 && (int64_t)d != d)
113  error = "Expected int64 for %s but found %s\n";
114  else if (type == OPT_INT && (int)d != d)
115  error = "Expected int for %s but found %s\n";
116  else
117  return d;
118  av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
119  exit_program(1);
120  return 0;
121 }
122 
123 int64_t parse_time_or_die(const char *context, const char *timestr,
124  int is_duration)
125 {
126  int64_t us;
127  if (av_parse_time(&us, timestr, is_duration) < 0) {
128  av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
129  is_duration ? "duration" : "date", context, timestr);
130  exit_program(1);
131  }
132  return us;
133 }
134 
135 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
136  int rej_flags, int alt_flags)
137 {
138  const OptionDef *po;
139  int first;
140 
141  first = 1;
142  for (po = options; po->name != NULL; po++) {
143  char buf[64];
144 
145  if (((po->flags & req_flags) != req_flags) ||
146  (alt_flags && !(po->flags & alt_flags)) ||
147  (po->flags & rej_flags))
148  continue;
149 
150  if (first) {
151  printf("%s\n", msg);
152  first = 0;
153  }
154  av_strlcpy(buf, po->name, sizeof(buf));
155  if (po->argname) {
156  av_strlcat(buf, " ", sizeof(buf));
157  av_strlcat(buf, po->argname, sizeof(buf));
158  }
159  printf("-%-17s %s\n", buf, po->help);
160  }
161  printf("\n");
162 }
163 
164 void show_help_children(const AVClass *class, int flags)
165 {
166  const AVClass *child = NULL;
167  av_opt_show2(&class, NULL, flags, 0);
168  printf("\n");
169 
170  while (child = av_opt_child_class_next(class, child))
171  show_help_children(child, flags);
172 }
173 
174 static const OptionDef *find_option(const OptionDef *po, const char *name)
175 {
176  const char *p = strchr(name, ':');
177  int len = p ? p - name : strlen(name);
178 
179  while (po->name) {
180  if (!strncmp(name, po->name, len) && strlen(po->name) == len)
181  break;
182  po++;
183  }
184  return po;
185 }
186 
187 /* _WIN32 means using the windows libc - cygwin doesn't define that
188  * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
189  * it doesn't provide the actual command line via GetCommandLineW(). */
190 #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
191 #include <windows.h>
192 #include <shellapi.h>
193 /* Will be leaked on exit */
194 static char** win32_argv_utf8 = NULL;
195 static int win32_argc = 0;
196 
204 static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
205 {
206  char *argstr_flat;
207  wchar_t **argv_w;
208  int i, buffsize = 0, offset = 0;
209 
210  if (win32_argv_utf8) {
211  *argc_ptr = win32_argc;
212  *argv_ptr = win32_argv_utf8;
213  return;
214  }
215 
216  win32_argc = 0;
217  argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
218  if (win32_argc <= 0 || !argv_w)
219  return;
220 
221  /* determine the UTF-8 buffer size (including NULL-termination symbols) */
222  for (i = 0; i < win32_argc; i++)
223  buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
224  NULL, 0, NULL, NULL);
225 
226  win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
227  argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
228  if (!win32_argv_utf8) {
229  LocalFree(argv_w);
230  return;
231  }
232 
233  for (i = 0; i < win32_argc; i++) {
234  win32_argv_utf8[i] = &argstr_flat[offset];
235  offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
236  &argstr_flat[offset],
237  buffsize - offset, NULL, NULL);
238  }
239  win32_argv_utf8[i] = NULL;
240  LocalFree(argv_w);
241 
242  *argc_ptr = win32_argc;
243  *argv_ptr = win32_argv_utf8;
244 }
245 #else
246 static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
247 {
248  /* nothing to do */
249 }
250 #endif /* HAVE_COMMANDLINETOARGVW */
251 
252 static int write_option(void *optctx, const OptionDef *po, const char *opt,
253  const char *arg)
254 {
255  /* new-style options contain an offset into optctx, old-style address of
256  * a global var*/
257  void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
258  (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
259  int *dstcount;
260 
261  if (po->flags & OPT_SPEC) {
262  SpecifierOpt **so = dst;
263  char *p = strchr(opt, ':');
264 
265  dstcount = (int *)(so + 1);
266  *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
267  (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
268  dst = &(*so)[*dstcount - 1].u;
269  }
270 
271  if (po->flags & OPT_STRING) {
272  char *str;
273  str = av_strdup(arg);
274  av_freep(dst);
275  *(char **)dst = str;
276  } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
277  *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
278  } else if (po->flags & OPT_INT64) {
279  *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
280  } else if (po->flags & OPT_TIME) {
281  *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
282  } else if (po->flags & OPT_FLOAT) {
283  *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
284  } else if (po->flags & OPT_DOUBLE) {
285  *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
286  } else if (po->u.func_arg) {
287  int ret = po->u.func_arg(optctx, opt, arg);
288  if (ret < 0) {
290  "Failed to set value '%s' for option '%s'\n", arg, opt);
291  return ret;
292  }
293  }
294  if (po->flags & OPT_EXIT)
295  exit_program(0);
296 
297  return 0;
298 }
299 
300 int parse_option(void *optctx, const char *opt, const char *arg,
301  const OptionDef *options)
302 {
303  const OptionDef *po;
304  int ret;
305 
306  po = find_option(options, opt);
307  if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
308  /* handle 'no' bool option */
309  po = find_option(options, opt + 2);
310  if ((po->name && (po->flags & OPT_BOOL)))
311  arg = "0";
312  } else if (po->flags & OPT_BOOL)
313  arg = "1";
314 
315  if (!po->name)
316  po = find_option(options, "default");
317  if (!po->name) {
318  av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
319  return AVERROR(EINVAL);
320  }
321  if (po->flags & HAS_ARG && !arg) {
322  av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
323  return AVERROR(EINVAL);
324  }
325 
326  ret = write_option(optctx, po, opt, arg);
327  if (ret < 0)
328  return ret;
329 
330  return !!(po->flags & HAS_ARG);
331 }
332 
333 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
334  void (*parse_arg_function)(void *, const char*))
335 {
336  const char *opt;
337  int optindex, handleoptions = 1, ret;
338 
339  /* perform system-dependent conversions for arguments list */
340  prepare_app_arguments(&argc, &argv);
341 
342  /* parse options */
343  optindex = 1;
344  while (optindex < argc) {
345  opt = argv[optindex++];
346 
347  if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
348  if (opt[1] == '-' && opt[2] == '\0') {
349  handleoptions = 0;
350  continue;
351  }
352  opt++;
353 
354  if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
355  exit_program(1);
356  optindex += ret;
357  } else {
358  if (parse_arg_function)
359  parse_arg_function(optctx, opt);
360  }
361  }
362 }
363 
364 int parse_optgroup(void *optctx, OptionGroup *g)
365 {
366  int i, ret;
367 
368  av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
369  g->group_def->name, g->arg);
370 
371  for (i = 0; i < g->nb_opts; i++) {
372  Option *o = &g->opts[i];
373 
374  if (g->group_def->flags &&
375  !(g->group_def->flags & o->opt->flags)) {
376  av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
377  "%s %s -- you are trying to apply an input option to an "
378  "output file or vice versa. Move this option before the "
379  "file it belongs to.\n", o->key, o->opt->help,
380  g->group_def->name, g->arg);
381  return AVERROR(EINVAL);
382  }
383 
384  av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
385  o->key, o->opt->help, o->val);
386 
387  ret = write_option(optctx, o->opt, o->key, o->val);
388  if (ret < 0)
389  return ret;
390  }
391 
392  av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
393 
394  return 0;
395 }
396 
397 int locate_option(int argc, char **argv, const OptionDef *options,
398  const char *optname)
399 {
400  const OptionDef *po;
401  int i;
402 
403  for (i = 1; i < argc; i++) {
404  const char *cur_opt = argv[i];
405 
406  if (*cur_opt++ != '-')
407  continue;
408 
409  po = find_option(options, cur_opt);
410  if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
411  po = find_option(options, cur_opt + 2);
412 
413  if ((!po->name && !strcmp(cur_opt, optname)) ||
414  (po->name && !strcmp(optname, po->name)))
415  return i;
416 
417  if (!po->name || po->flags & HAS_ARG)
418  i++;
419  }
420  return 0;
421 }
422 
423 void parse_loglevel(int argc, char **argv, const OptionDef *options)
424 {
425  int idx = locate_option(argc, argv, options, "loglevel");
426  if (!idx)
427  idx = locate_option(argc, argv, options, "v");
428  if (idx && argv[idx + 1])
429  opt_loglevel(NULL, "loglevel", argv[idx + 1]);
430 }
431 
432 #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
433 int opt_default(void *optctx, const char *opt, const char *arg)
434 {
435  const AVOption *o;
436  char opt_stripped[128];
437  const char *p;
438  const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
439 #if CONFIG_AVRESAMPLE
440  const AVClass *rc = avresample_get_class();
441 #endif
442 #if CONFIG_SWSCALE
443  const AVClass *sc = sws_get_class();
444 #endif
445 
446  if (!(p = strchr(opt, ':')))
447  p = opt + strlen(opt);
448  av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
449 
450  if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
452  ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
453  (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
454  av_dict_set(&codec_opts, opt, arg, FLAGS);
455  else if ((o = av_opt_find(&fc, opt, NULL, 0,
457  av_dict_set(&format_opts, opt, arg, FLAGS);
458 #if CONFIG_AVRESAMPLE
459  else if ((o = av_opt_find(&rc, opt, NULL, 0,
461  av_dict_set(&resample_opts, opt, arg, FLAGS);
462 #endif
463 #if CONFIG_SWSCALE
464  else if ((o = av_opt_find(&sc, opt, NULL, 0,
466  // XXX we only support sws_flags, not arbitrary sws options
467  int ret = av_opt_set(sws_opts, opt, arg, 0);
468  if (ret < 0) {
469  av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
470  return ret;
471  }
472  }
473 #endif
474 
475  if (o)
476  return 0;
478 }
479 
480 /*
481  * Check whether given option is a group separator.
482  *
483  * @return index of the group definition that matched or -1 if none
484  */
485 static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
486  const char *opt)
487 {
488  int i;
489 
490  for (i = 0; i < nb_groups; i++) {
491  const OptionGroupDef *p = &groups[i];
492  if (p->sep && !strcmp(p->sep, opt))
493  return i;
494  }
495 
496  return -1;
497 }
498 
499 /*
500  * Finish parsing an option group.
501  *
502  * @param group_idx which group definition should this group belong to
503  * @param arg argument of the group delimiting option
504  */
505 static void finish_group(OptionParseContext *octx, int group_idx,
506  const char *arg)
507 {
508  OptionGroupList *l = &octx->groups[group_idx];
509  OptionGroup *g;
510 
511  GROW_ARRAY(l->groups, l->nb_groups);
512  g = &l->groups[l->nb_groups - 1];
513 
514  *g = octx->cur_group;
515  g->arg = arg;
516  g->group_def = l->group_def;
517 #if CONFIG_SWSCALE
518  g->sws_opts = sws_opts;
519 #endif
520  g->codec_opts = codec_opts;
523 
524  codec_opts = NULL;
525  format_opts = NULL;
526  resample_opts = NULL;
527 #if CONFIG_SWSCALE
528  sws_opts = NULL;
529 #endif
530  init_opts();
531 
532  memset(&octx->cur_group, 0, sizeof(octx->cur_group));
533 }
534 
535 /*
536  * Add an option instance to currently parsed group.
537  */
538 static void add_opt(OptionParseContext *octx, const OptionDef *opt,
539  const char *key, const char *val)
540 {
541  int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
542  OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
543 
544  GROW_ARRAY(g->opts, g->nb_opts);
545  g->opts[g->nb_opts - 1].opt = opt;
546  g->opts[g->nb_opts - 1].key = key;
547  g->opts[g->nb_opts - 1].val = val;
548 }
549 
551  const OptionGroupDef *groups, int nb_groups)
552 {
553  static const OptionGroupDef global_group = { "global" };
554  int i;
555 
556  memset(octx, 0, sizeof(*octx));
557 
558  octx->nb_groups = nb_groups;
559  octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
560  if (!octx->groups)
561  exit_program(1);
562 
563  for (i = 0; i < octx->nb_groups; i++)
564  octx->groups[i].group_def = &groups[i];
565 
566  octx->global_opts.group_def = &global_group;
567  octx->global_opts.arg = "";
568 
569  init_opts();
570 }
571 
573 {
574  int i, j;
575 
576  for (i = 0; i < octx->nb_groups; i++) {
577  OptionGroupList *l = &octx->groups[i];
578 
579  for (j = 0; j < l->nb_groups; j++) {
580  av_freep(&l->groups[j].opts);
584 #if CONFIG_SWSCALE
586 #endif
587  }
588  av_freep(&l->groups);
589  }
590  av_freep(&octx->groups);
591 
592  av_freep(&octx->cur_group.opts);
593  av_freep(&octx->global_opts.opts);
594 
595  uninit_opts();
596 }
597 
598 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
599  const OptionDef *options,
600  const OptionGroupDef *groups, int nb_groups)
601 {
602  int optindex = 1;
603 
604  /* perform system-dependent conversions for arguments list */
605  prepare_app_arguments(&argc, &argv);
606 
607  init_parse_context(octx, groups, nb_groups);
608  av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
609 
610  while (optindex < argc) {
611  const char *opt = argv[optindex++], *arg;
612  const OptionDef *po;
613  int ret;
614 
615  av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
616 
617  /* unnamed group separators, e.g. output filename */
618  if (opt[0] != '-' || !opt[1]) {
619  finish_group(octx, 0, opt);
620  av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
621  continue;
622  }
623  opt++;
624 
625 #define GET_ARG(arg) \
626 do { \
627  arg = argv[optindex++]; \
628  if (!arg) { \
629  av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
630  return AVERROR(EINVAL); \
631  } \
632 } while (0)
633 
634  /* named group separators, e.g. -i */
635  if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
636  GET_ARG(arg);
637  finish_group(octx, ret, arg);
638  av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
639  groups[ret].name, arg);
640  continue;
641  }
642 
643  /* normal options */
644  po = find_option(options, opt);
645  if (po->name) {
646  if (po->flags & OPT_EXIT) {
647  /* optional argument, e.g. -h */
648  arg = argv[optindex++];
649  } else if (po->flags & HAS_ARG) {
650  GET_ARG(arg);
651  } else {
652  arg = "1";
653  }
654 
655  add_opt(octx, po, opt, arg);
656  av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
657  "argument '%s'.\n", po->name, po->help, arg);
658  continue;
659  }
660 
661  /* AVOptions */
662  if (argv[optindex]) {
663  ret = opt_default(NULL, opt, argv[optindex]);
664  if (ret >= 0) {
665  av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
666  "argument '%s'.\n", opt, argv[optindex]);
667  optindex++;
668  continue;
669  } else if (ret != AVERROR_OPTION_NOT_FOUND) {
670  av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
671  "with argument '%s'.\n", opt, argv[optindex]);
672  return ret;
673  }
674  }
675 
676  /* boolean -nofoo options */
677  if (opt[0] == 'n' && opt[1] == 'o' &&
678  (po = find_option(options, opt + 2)) &&
679  po->name && po->flags & OPT_BOOL) {
680  add_opt(octx, po, opt, "0");
681  av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
682  "argument 0.\n", po->name, po->help);
683  continue;
684  }
685 
686  av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
688  }
689 
690  if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
691  av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
692  "commandline.\n");
693 
694  av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
695 
696  return 0;
697 }
698 
699 int opt_cpuflags(void *optctx, const char *opt, const char *arg)
700 {
701  int flags = av_parse_cpu_flags(arg);
702 
703  if (flags < 0)
704  return flags;
705 
706  av_set_cpu_flags_mask(flags);
707  return 0;
708 }
709 
710 int opt_loglevel(void *optctx, const char *opt, const char *arg)
711 {
712  const struct { const char *name; int level; } log_levels[] = {
713  { "quiet" , AV_LOG_QUIET },
714  { "panic" , AV_LOG_PANIC },
715  { "fatal" , AV_LOG_FATAL },
716  { "error" , AV_LOG_ERROR },
717  { "warning", AV_LOG_WARNING },
718  { "info" , AV_LOG_INFO },
719  { "verbose", AV_LOG_VERBOSE },
720  { "debug" , AV_LOG_DEBUG },
721  };
722  char *tail;
723  int level;
724  int i;
725 
726  for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
727  if (!strcmp(log_levels[i].name, arg)) {
728  av_log_set_level(log_levels[i].level);
729  return 0;
730  }
731  }
732 
733  level = strtol(arg, &tail, 10);
734  if (*tail) {
735  av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
736  "Possible levels are numbers or:\n", arg);
737  for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
738  av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
739  exit_program(1);
740  }
741  av_log_set_level(level);
742  return 0;
743 }
744 
745 int opt_timelimit(void *optctx, const char *opt, const char *arg)
746 {
747 #if HAVE_SETRLIMIT
748  int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
749  struct rlimit rl = { lim, lim + 1 };
750  if (setrlimit(RLIMIT_CPU, &rl))
751  perror("setrlimit");
752 #else
753  av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
754 #endif
755  return 0;
756 }
757 
758 void print_error(const char *filename, int err)
759 {
760  char errbuf[128];
761  const char *errbuf_ptr = errbuf;
762 
763  if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
764  errbuf_ptr = strerror(AVUNERROR(err));
765  av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
766 }
767 
768 // Debian/Ubuntu: see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=619530
769 // https://launchpad.net/bugs/765357
770 static int warned_cfg = 1;
771 
772 #define INDENT 1
773 #define SHOW_VERSION 2
774 #define SHOW_CONFIG 4
775 
776 #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
777  if (CONFIG_##LIBNAME) { \
778  const char *indent = flags & INDENT? " " : ""; \
779  if (flags & SHOW_VERSION) { \
780  unsigned int version = libname##_version(); \
781  av_log(NULL, level, \
782  "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
783  indent, #libname, \
784  LIB##LIBNAME##_VERSION_MAJOR, \
785  LIB##LIBNAME##_VERSION_MINOR, \
786  LIB##LIBNAME##_VERSION_MICRO, \
787  version >> 16, version >> 8 & 0xff, version & 0xff); \
788  } \
789  if (flags & SHOW_CONFIG) { \
790  const char *cfg = libname##_configuration(); \
791  if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
792  if (!warned_cfg) { \
793  av_log(NULL, level, \
794  "%sWARNING: library configuration mismatch\n", \
795  indent); \
796  warned_cfg = 1; \
797  } \
798  av_log(NULL, level, "%s%-11s configuration: %s\n", \
799  indent, #libname, cfg); \
800  } \
801  } \
802  } \
803 
804 static void print_all_libs_info(int flags, int level)
805 {
806  PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
807  PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
808  PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
809  PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
810  PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
811  PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
812  PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
813 }
814 
815 void show_banner(void)
816 {
818  "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
820  av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
821  __DATE__, __TIME__, CC_IDENT);
822  av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
825 }
826 
827 int show_version(void *optctx, const char *opt, const char *arg)
828 {
830  printf("%s " LIBAV_VERSION "\n", program_name);
832 
833  return 0;
834 }
835 
836 int show_license(void *optctx, const char *opt, const char *arg)
837 {
838  printf(
839 #if CONFIG_NONFREE
840  "This version of %s has nonfree parts compiled in.\n"
841  "Therefore it is not legally redistributable.\n",
843 #elif CONFIG_GPLV3
844  "%s is free software; you can redistribute it and/or modify\n"
845  "it under the terms of the GNU General Public License as published by\n"
846  "the Free Software Foundation; either version 3 of the License, or\n"
847  "(at your option) any later version.\n"
848  "\n"
849  "%s is distributed in the hope that it will be useful,\n"
850  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
851  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
852  "GNU General Public License for more details.\n"
853  "\n"
854  "You should have received a copy of the GNU General Public License\n"
855  "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
857 #elif CONFIG_GPL
858  "%s is free software; you can redistribute it and/or modify\n"
859  "it under the terms of the GNU General Public License as published by\n"
860  "the Free Software Foundation; either version 2 of the License, or\n"
861  "(at your option) any later version.\n"
862  "\n"
863  "%s is distributed in the hope that it will be useful,\n"
864  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
865  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
866  "GNU General Public License for more details.\n"
867  "\n"
868  "You should have received a copy of the GNU General Public License\n"
869  "along with %s; if not, write to the Free Software\n"
870  "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
872 #elif CONFIG_LGPLV3
873  "%s is free software; you can redistribute it and/or modify\n"
874  "it under the terms of the GNU Lesser General Public License as published by\n"
875  "the Free Software Foundation; either version 3 of the License, or\n"
876  "(at your option) any later version.\n"
877  "\n"
878  "%s is distributed in the hope that it will be useful,\n"
879  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
880  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
881  "GNU Lesser General Public License for more details.\n"
882  "\n"
883  "You should have received a copy of the GNU Lesser General Public License\n"
884  "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
886 #else
887  "%s is free software; you can redistribute it and/or\n"
888  "modify it under the terms of the GNU Lesser General Public\n"
889  "License as published by the Free Software Foundation; either\n"
890  "version 2.1 of the License, or (at your option) any later version.\n"
891  "\n"
892  "%s is distributed in the hope that it will be useful,\n"
893  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
894  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
895  "Lesser General Public License for more details.\n"
896  "\n"
897  "You should have received a copy of the GNU Lesser General Public\n"
898  "License along with %s; if not, write to the Free Software\n"
899  "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
901 #endif
902  );
903 
904  return 0;
905 }
906 
907 int show_formats(void *optctx, const char *opt, const char *arg)
908 {
909  AVInputFormat *ifmt = NULL;
910  AVOutputFormat *ofmt = NULL;
911  const char *last_name;
912 
913  printf("File formats:\n"
914  " D. = Demuxing supported\n"
915  " .E = Muxing supported\n"
916  " --\n");
917  last_name = "000";
918  for (;;) {
919  int decode = 0;
920  int encode = 0;
921  const char *name = NULL;
922  const char *long_name = NULL;
923 
924  while ((ofmt = av_oformat_next(ofmt))) {
925  if ((!name || strcmp(ofmt->name, name) < 0) &&
926  strcmp(ofmt->name, last_name) > 0) {
927  name = ofmt->name;
928  long_name = ofmt->long_name;
929  encode = 1;
930  }
931  }
932  while ((ifmt = av_iformat_next(ifmt))) {
933  if ((!name || strcmp(ifmt->name, name) < 0) &&
934  strcmp(ifmt->name, last_name) > 0) {
935  name = ifmt->name;
936  long_name = ifmt->long_name;
937  encode = 0;
938  }
939  if (name && strcmp(ifmt->name, name) == 0)
940  decode = 1;
941  }
942  if (!name)
943  break;
944  last_name = name;
945 
946  printf(" %s%s %-15s %s\n",
947  decode ? "D" : " ",
948  encode ? "E" : " ",
949  name,
950  long_name ? long_name:" ");
951  }
952  return 0;
953 }
954 
955 #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
956  if (codec->field) { \
957  const type *p = c->field; \
958  \
959  printf(" Supported " list_name ":"); \
960  while (*p != term) { \
961  get_name(*p); \
962  printf(" %s", name); \
963  p++; \
964  } \
965  printf("\n"); \
966  } \
967 
968 static void print_codec(const AVCodec *c)
969 {
970  int encoder = av_codec_is_encoder(c);
971 
972  printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
973  c->long_name ? c->long_name : "");
974 
975  if (c->type == AVMEDIA_TYPE_VIDEO) {
976  printf(" Threading capabilities: ");
977  switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
980  CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
981  case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
982  case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
983  default: printf("no"); break;
984  }
985  printf("\n");
986  }
987 
988  if (c->supported_framerates) {
989  const AVRational *fps = c->supported_framerates;
990 
991  printf(" Supported framerates:");
992  while (fps->num) {
993  printf(" %d/%d", fps->num, fps->den);
994  fps++;
995  }
996  printf("\n");
997  }
998  PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
1000  PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
1002  PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
1004  PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
1005  0, GET_CH_LAYOUT_DESC);
1006 
1007  if (c->priv_class) {
1011  }
1012 }
1013 
1014 static char get_media_type_char(enum AVMediaType type)
1015 {
1016  switch (type) {
1017  case AVMEDIA_TYPE_VIDEO: return 'V';
1018  case AVMEDIA_TYPE_AUDIO: return 'A';
1019  case AVMEDIA_TYPE_SUBTITLE: return 'S';
1020  default: return '?';
1021  }
1022 }
1023 
1024 static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
1025  int encoder)
1026 {
1027  while ((prev = av_codec_next(prev))) {
1028  if (prev->id == id &&
1029  (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
1030  return prev;
1031  }
1032  return NULL;
1033 }
1034 
1035 static void print_codecs_for_id(enum AVCodecID id, int encoder)
1036 {
1037  const AVCodec *codec = NULL;
1038 
1039  printf(" (%s: ", encoder ? "encoders" : "decoders");
1040 
1041  while ((codec = next_codec_for_id(id, codec, encoder)))
1042  printf("%s ", codec->name);
1043 
1044  printf(")");
1045 }
1046 
1047 int show_codecs(void *optctx, const char *opt, const char *arg)
1048 {
1049  const AVCodecDescriptor *desc = NULL;
1050 
1051  printf("Codecs:\n"
1052  " D..... = Decoding supported\n"
1053  " .E.... = Encoding supported\n"
1054  " ..V... = Video codec\n"
1055  " ..A... = Audio codec\n"
1056  " ..S... = Subtitle codec\n"
1057  " ...I.. = Intra frame-only codec\n"
1058  " ....L. = Lossy compression\n"
1059  " .....S = Lossless compression\n"
1060  " -------\n");
1061  while ((desc = avcodec_descriptor_next(desc))) {
1062  const AVCodec *codec = NULL;
1063 
1064  printf(avcodec_find_decoder(desc->id) ? "D" : ".");
1065  printf(avcodec_find_encoder(desc->id) ? "E" : ".");
1066 
1067  printf("%c", get_media_type_char(desc->type));
1068  printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
1069  printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
1070  printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
1071 
1072  printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
1073 
1074  /* print decoders/encoders when there's more than one or their
1075  * names are different from codec name */
1076  while ((codec = next_codec_for_id(desc->id, codec, 0))) {
1077  if (strcmp(codec->name, desc->name)) {
1078  print_codecs_for_id(desc->id, 0);
1079  break;
1080  }
1081  }
1082  codec = NULL;
1083  while ((codec = next_codec_for_id(desc->id, codec, 1))) {
1084  if (strcmp(codec->name, desc->name)) {
1085  print_codecs_for_id(desc->id, 1);
1086  break;
1087  }
1088  }
1089 
1090  printf("\n");
1091  }
1092  return 0;
1093 }
1094 
1095 static void print_codecs(int encoder)
1096 {
1097  const AVCodecDescriptor *desc = NULL;
1098 
1099  printf("%s:\n"
1100  " V... = Video\n"
1101  " A... = Audio\n"
1102  " S... = Subtitle\n"
1103  " .F.. = Frame-level multithreading\n"
1104  " ..S. = Slice-level multithreading\n"
1105  " ...X = Codec is experimental\n"
1106  " ---\n",
1107  encoder ? "Encoders" : "Decoders");
1108  while ((desc = avcodec_descriptor_next(desc))) {
1109  const AVCodec *codec = NULL;
1110 
1111  while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1112  printf("%c", get_media_type_char(desc->type));
1113  printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
1114  printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
1115  printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
1116 
1117  printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
1118  if (strcmp(codec->name, desc->name))
1119  printf(" (codec %s)", desc->name);
1120 
1121  printf("\n");
1122  }
1123  }
1124 }
1125 
1126 int show_decoders(void *optctx, const char *opt, const char *arg)
1127 {
1128  print_codecs(0);
1129  return 0;
1130 }
1131 
1132 int show_encoders(void *optctx, const char *opt, const char *arg)
1133 {
1134  print_codecs(1);
1135  return 0;
1136 }
1137 
1138 int show_bsfs(void *optctx, const char *opt, const char *arg)
1139 {
1140  AVBitStreamFilter *bsf = NULL;
1141 
1142  printf("Bitstream filters:\n");
1143  while ((bsf = av_bitstream_filter_next(bsf)))
1144  printf("%s\n", bsf->name);
1145  printf("\n");
1146  return 0;
1147 }
1148 
1149 int show_protocols(void *optctx, const char *opt, const char *arg)
1150 {
1151  void *opaque = NULL;
1152  const char *name;
1153 
1154  printf("Supported file protocols:\n"
1155  "Input:\n");
1156  while ((name = avio_enum_protocols(&opaque, 0)))
1157  printf("%s\n", name);
1158  printf("Output:\n");
1159  while ((name = avio_enum_protocols(&opaque, 1)))
1160  printf("%s\n", name);
1161  return 0;
1162 }
1163 
1164 int show_filters(void *optctx, const char *opt, const char *arg)
1165 {
1166  const AVFilter av_unused(*filter) = NULL;
1167 
1168  printf("Filters:\n");
1169 #if CONFIG_AVFILTER
1170  while ((filter = avfilter_next(filter)))
1171  printf("%-16s %s\n", filter->name, filter->description);
1172 #endif
1173  return 0;
1174 }
1175 
1176 int show_pix_fmts(void *optctx, const char *opt, const char *arg)
1177 {
1178  const AVPixFmtDescriptor *pix_desc = NULL;
1179 
1180  printf("Pixel formats:\n"
1181  "I.... = Supported Input format for conversion\n"
1182  ".O... = Supported Output format for conversion\n"
1183  "..H.. = Hardware accelerated format\n"
1184  "...P. = Paletted format\n"
1185  "....B = Bitstream format\n"
1186  "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
1187  "-----\n");
1188 
1189 #if !CONFIG_SWSCALE
1190 # define sws_isSupportedInput(x) 0
1191 # define sws_isSupportedOutput(x) 0
1192 #endif
1193 
1194  while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
1195  enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
1196  printf("%c%c%c%c%c %-16s %d %2d\n",
1197  sws_isSupportedInput (pix_fmt) ? 'I' : '.',
1198  sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
1199  pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
1200  pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
1201  pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
1202  pix_desc->name,
1203  pix_desc->nb_components,
1204  av_get_bits_per_pixel(pix_desc));
1205  }
1206  return 0;
1207 }
1208 
1209 int show_sample_fmts(void *optctx, const char *opt, const char *arg)
1210 {
1211  int i;
1212  char fmt_str[128];
1213  for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
1214  printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
1215  return 0;
1216 }
1217 
1218 static void show_help_codec(const char *name, int encoder)
1219 {
1220  const AVCodecDescriptor *desc;
1221  const AVCodec *codec;
1222 
1223  if (!name) {
1224  av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
1225  return;
1226  }
1227 
1228  codec = encoder ? avcodec_find_encoder_by_name(name) :
1230 
1231  if (codec)
1232  print_codec(codec);
1233  else if ((desc = avcodec_descriptor_get_by_name(name))) {
1234  int printed = 0;
1235 
1236  while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
1237  printed = 1;
1238  print_codec(codec);
1239  }
1240 
1241  if (!printed) {
1242  av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
1243  "but no %s for it are available. Libav might need to be "
1244  "recompiled with additional external libraries.\n",
1245  name, encoder ? "encoders" : "decoders");
1246  }
1247  } else {
1248  av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
1249  name);
1250  }
1251 }
1252 
1253 static void show_help_demuxer(const char *name)
1254 {
1255  const AVInputFormat *fmt = av_find_input_format(name);
1256 
1257  if (!fmt) {
1258  av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1259  return;
1260  }
1261 
1262  printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
1263 
1264  if (fmt->extensions)
1265  printf(" Common extensions: %s.\n", fmt->extensions);
1266 
1267  if (fmt->priv_class)
1269 }
1270 
1271 static void show_help_muxer(const char *name)
1272 {
1273  const AVCodecDescriptor *desc;
1274  const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
1275 
1276  if (!fmt) {
1277  av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
1278  return;
1279  }
1280 
1281  printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
1282 
1283  if (fmt->extensions)
1284  printf(" Common extensions: %s.\n", fmt->extensions);
1285  if (fmt->mime_type)
1286  printf(" Mime type: %s.\n", fmt->mime_type);
1287  if (fmt->video_codec != AV_CODEC_ID_NONE &&
1288  (desc = avcodec_descriptor_get(fmt->video_codec))) {
1289  printf(" Default video codec: %s.\n", desc->name);
1290  }
1291  if (fmt->audio_codec != AV_CODEC_ID_NONE &&
1292  (desc = avcodec_descriptor_get(fmt->audio_codec))) {
1293  printf(" Default audio codec: %s.\n", desc->name);
1294  }
1295  if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
1296  (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
1297  printf(" Default subtitle codec: %s.\n", desc->name);
1298  }
1299 
1300  if (fmt->priv_class)
1302 }
1303 
1304 #if CONFIG_AVFILTER
1305 static void show_help_filter(const char *name)
1306 {
1307  const AVFilter *f = avfilter_get_by_name(name);
1308  int i, count;
1309 
1310  if (!name) {
1311  av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
1312  return;
1313  } else if (!f) {
1314  av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
1315  return;
1316  }
1317 
1318  printf("Filter %s [%s]:\n", f->name, f->description);
1319 
1321  printf(" slice threading supported\n");
1322 
1323  printf(" Inputs:\n");
1324  count = avfilter_pad_count(f->inputs);
1325  for (i = 0; i < count; i++) {
1326  printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
1328  }
1330  printf(" dynamic (depending on the options)\n");
1331 
1332  printf(" Outputs:\n");
1333  count = avfilter_pad_count(f->outputs);
1334  for (i = 0; i < count; i++) {
1335  printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
1337  }
1339  printf(" dynamic (depending on the options)\n");
1340 
1341  if (f->priv_class)
1344 }
1345 #endif
1346 
1347 int show_help(void *optctx, const char *opt, const char *arg)
1348 {
1349  char *topic, *par;
1351 
1352  topic = av_strdup(arg ? arg : "");
1353  par = strchr(topic, '=');
1354  if (par)
1355  *par++ = 0;
1356 
1357  if (!*topic) {
1358  show_help_default(topic, par);
1359  } else if (!strcmp(topic, "decoder")) {
1360  show_help_codec(par, 0);
1361  } else if (!strcmp(topic, "encoder")) {
1362  show_help_codec(par, 1);
1363  } else if (!strcmp(topic, "demuxer")) {
1364  show_help_demuxer(par);
1365  } else if (!strcmp(topic, "muxer")) {
1366  show_help_muxer(par);
1367 #if CONFIG_AVFILTER
1368  } else if (!strcmp(topic, "filter")) {
1369  show_help_filter(par);
1370 #endif
1371  } else {
1372  show_help_default(topic, par);
1373  }
1374 
1375  av_freep(&topic);
1376  return 0;
1377 }
1378 
1379 int read_yesno(void)
1380 {
1381  int c = getchar();
1382  int yesno = (av_toupper(c) == 'Y');
1383 
1384  while (c != '\n' && c != EOF)
1385  c = getchar();
1386 
1387  return yesno;
1388 }
1389 
1390 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
1391 {
1392  int ret;
1393  FILE *f = fopen(filename, "rb");
1394 
1395  if (!f) {
1396  av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
1397  strerror(errno));
1398  return AVERROR(errno);
1399  }
1400 
1401  ret = fseek(f, 0, SEEK_END);
1402  if (ret == -1) {
1403  ret = AVERROR(errno);
1404  goto out;
1405  }
1406 
1407  ret = ftell(f);
1408  if (ret < 0) {
1409  ret = AVERROR(errno);
1410  goto out;
1411  }
1412  *size = ret;
1413 
1414  ret = fseek(f, 0, SEEK_SET);
1415  if (ret == -1) {
1416  ret = AVERROR(errno);
1417  goto out;
1418  }
1419 
1420  *bufptr = av_malloc(*size + 1);
1421  if (!*bufptr) {
1422  av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
1423  ret = AVERROR(ENOMEM);
1424  goto out;
1425  }
1426  ret = fread(*bufptr, 1, *size, f);
1427  if (ret < *size) {
1428  av_free(*bufptr);
1429  if (ferror(f)) {
1430  av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
1431  filename, strerror(errno));
1432  ret = AVERROR(errno);
1433  } else
1434  ret = AVERROR_EOF;
1435  } else {
1436  ret = 0;
1437  (*bufptr)[(*size)++] = '\0';
1438  }
1439 
1440 out:
1441  fclose(f);
1442  return ret;
1443 }
1444 
1446 {
1447  ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
1448  ctx->last_pts = ctx->last_dts = INT64_MIN;
1449 }
1450 
1451 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
1452  int64_t dts)
1453 {
1454  int64_t pts = AV_NOPTS_VALUE;
1455 
1456  if (dts != AV_NOPTS_VALUE) {
1457  ctx->num_faulty_dts += dts <= ctx->last_dts;
1458  ctx->last_dts = dts;
1459  }
1460  if (reordered_pts != AV_NOPTS_VALUE) {
1461  ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
1462  ctx->last_pts = reordered_pts;
1463  }
1464  if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
1465  && reordered_pts != AV_NOPTS_VALUE)
1466  pts = reordered_pts;
1467  else
1468  pts = dts;
1469 
1470  return pts;
1471 }
1472 
1473 FILE *get_preset_file(char *filename, size_t filename_size,
1474  const char *preset_name, int is_path,
1475  const char *codec_name)
1476 {
1477  FILE *f = NULL;
1478  int i;
1479  const char *base[3] = { getenv("AVCONV_DATADIR"),
1480  getenv("HOME"),
1481  AVCONV_DATADIR, };
1482 
1483  if (is_path) {
1484  av_strlcpy(filename, preset_name, filename_size);
1485  f = fopen(filename, "r");
1486  } else {
1487  for (i = 0; i < 3 && !f; i++) {
1488  if (!base[i])
1489  continue;
1490  snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
1491  i != 1 ? "" : "/.avconv", preset_name);
1492  f = fopen(filename, "r");
1493  if (!f && codec_name) {
1494  snprintf(filename, filename_size,
1495  "%s%s/%s-%s.avpreset",
1496  base[i], i != 1 ? "" : "/.avconv", codec_name,
1497  preset_name);
1498  f = fopen(filename, "r");
1499  }
1500  }
1501  }
1502 
1503  return f;
1504 }
1505 
1506 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1507 {
1508  if (*spec <= '9' && *spec >= '0') /* opt:index */
1509  return strtol(spec, NULL, 0) == st->index;
1510  else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1511  *spec == 't') { /* opt:[vasdt] */
1512  enum AVMediaType type;
1513 
1514  switch (*spec++) {
1515  case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1516  case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1517  case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1518  case 'd': type = AVMEDIA_TYPE_DATA; break;
1519  case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1520  default: av_assert0(0);
1521  }
1522  if (type != st->codec->codec_type)
1523  return 0;
1524  if (*spec++ == ':') { /* possibly followed by :index */
1525  int i, index = strtol(spec, NULL, 0);
1526  for (i = 0; i < s->nb_streams; i++)
1527  if (s->streams[i]->codec->codec_type == type && index-- == 0)
1528  return i == st->index;
1529  return 0;
1530  }
1531  return 1;
1532  } else if (*spec == 'p' && *(spec + 1) == ':') {
1533  int prog_id, i, j;
1534  char *endptr;
1535  spec += 2;
1536  prog_id = strtol(spec, &endptr, 0);
1537  for (i = 0; i < s->nb_programs; i++) {
1538  if (s->programs[i]->id != prog_id)
1539  continue;
1540 
1541  if (*endptr++ == ':') {
1542  int stream_idx = strtol(endptr, NULL, 0);
1543  return stream_idx >= 0 &&
1544  stream_idx < s->programs[i]->nb_stream_indexes &&
1545  st->index == s->programs[i]->stream_index[stream_idx];
1546  }
1547 
1548  for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
1549  if (st->index == s->programs[i]->stream_index[j])
1550  return 1;
1551  }
1552  return 0;
1553  } else if (*spec == 'i' && *(spec + 1) == ':') {
1554  int stream_id;
1555  char *endptr;
1556  spec += 2;
1557  stream_id = strtol(spec, &endptr, 0);
1558  return stream_id == st->id;
1559  } else if (*spec == 'm' && *(spec + 1) == ':') {
1561  char *key, *val;
1562  int ret;
1563 
1564  spec += 2;
1565  val = strchr(spec, ':');
1566 
1567  key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
1568  if (!key)
1569  return AVERROR(ENOMEM);
1570 
1571  tag = av_dict_get(st->metadata, key, NULL, 0);
1572  if (tag) {
1573  if (!val || !strcmp(tag->value, val + 1))
1574  ret = 1;
1575  else
1576  ret = 0;
1577  } else
1578  ret = 0;
1579 
1580  av_freep(&key);
1581  return ret;
1582  } else if (!*spec) /* empty specifier, matches everything */
1583  return 1;
1584 
1585  av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1586  return AVERROR(EINVAL);
1587 }
1588 
1590  AVFormatContext *s, AVStream *st, AVCodec *codec)
1591 {
1592  AVDictionary *ret = NULL;
1593  AVDictionaryEntry *t = NULL;
1596  char prefix = 0;
1597  const AVClass *cc = avcodec_get_class();
1598 
1599  if (!codec)
1600  codec = s->oformat ? avcodec_find_encoder(codec_id)
1601  : avcodec_find_decoder(codec_id);
1602 
1603  switch (st->codec->codec_type) {
1604  case AVMEDIA_TYPE_VIDEO:
1605  prefix = 'v';
1606  flags |= AV_OPT_FLAG_VIDEO_PARAM;
1607  break;
1608  case AVMEDIA_TYPE_AUDIO:
1609  prefix = 'a';
1610  flags |= AV_OPT_FLAG_AUDIO_PARAM;
1611  break;
1612  case AVMEDIA_TYPE_SUBTITLE:
1613  prefix = 's';
1614  flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
1615  break;
1616  }
1617 
1618  while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
1619  char *p = strchr(t->key, ':');
1620 
1621  /* check stream specification in opt name */
1622  if (p)
1623  switch (check_stream_specifier(s, st, p + 1)) {
1624  case 1: *p = 0; break;
1625  case 0: continue;
1626  default: return NULL;
1627  }
1628 
1629  if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
1630  (codec && codec->priv_class &&
1631  av_opt_find(&codec->priv_class, t->key, NULL, flags,
1633  av_dict_set(&ret, t->key, t->value, 0);
1634  else if (t->key[0] == prefix &&
1635  av_opt_find(&cc, t->key + 1, NULL, flags,
1637  av_dict_set(&ret, t->key + 1, t->value, 0);
1638 
1639  if (p)
1640  *p = ':';
1641  }
1642  return ret;
1643 }
1644 
1646  AVDictionary *codec_opts)
1647 {
1648  int i;
1649  AVDictionary **opts;
1650 
1651  if (!s->nb_streams)
1652  return NULL;
1653  opts = av_mallocz(s->nb_streams * sizeof(*opts));
1654  if (!opts) {
1656  "Could not alloc memory for stream options.\n");
1657  return NULL;
1658  }
1659  for (i = 0; i < s->nb_streams; i++)
1660  opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
1661  s, s->streams[i], NULL);
1662  return opts;
1663 }
1664 
1665 void *grow_array(void *array, int elem_size, int *size, int new_size)
1666 {
1667  if (new_size >= INT_MAX / elem_size) {
1668  av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
1669  exit_program(1);
1670  }
1671  if (*size < new_size) {
1672  uint8_t *tmp = av_realloc(array, new_size*elem_size);
1673  if (!tmp) {
1674  av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
1675  exit_program(1);
1676  }
1677  memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
1678  *size = new_size;
1679  return tmp;
1680  }
1681  return array;
1682 }
1683 
1684 const char *media_type_string(enum AVMediaType media_type)
1685 {
1686  switch (media_type) {
1687  case AVMEDIA_TYPE_VIDEO: return "video";
1688  case AVMEDIA_TYPE_AUDIO: return "audio";
1689  case AVMEDIA_TYPE_DATA: return "data";
1690  case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
1691  case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
1692  default: return "unknown";
1693  }
1694 }
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:112
int parse_optgroup(void *optctx, OptionGroup *g)
Parse an options group and write results into optctx.
Definition: cmdutils.c:364
int64_t num_faulty_dts
Number of incorrect PTS values so far.
Definition: cmdutils.h:477
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: avcodec.h:501
AVDictionary * resample_opts
Definition: cmdutils.h:260
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:75
int size
int show_decoders(void *optctx, const char *opt, const char *arg)
Print a listing containing all the decoders supported by the program.
Definition: cmdutils.c:1126
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:1761
const char * name
< group name
Definition: cmdutils.h:238
static void finish_group(OptionParseContext *octx, int group_idx, const char *arg)
Definition: cmdutils.c:505
#define FLAGS
Definition: cmdutils.c:432
int64_t num_faulty_pts
Definition: cmdutils.h:476
#define SWS_BICUBIC
Definition: swscale.h:59
AVOption.
Definition: opt.h:234
int show_license(void *optctx, const char *opt, const char *arg)
Print the license of the program to stdout.
Definition: cmdutils.c:836
#define AV_CODEC_PROP_LOSSY
Codec supports lossy compression.
Definition: avcodec.h:507
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:271
int(* func_arg)(void *, const char *, const char *)
Definition: cmdutils.h:166
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
char * av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt)
Generate a string corresponding to the sample format with sample_fmt, or a header if sample_fmt is ne...
Definition: samplefmt.c:82
Main libavfilter public API header.
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:482
void av_log_set_level(int level)
Set the log level.
Definition: log.c:191
static PrintContext octx
Definition: avprobe.c:110
int split_commandline(OptionParseContext *octx, int argc, char *argv[], const OptionDef *options, const OptionGroupDef *groups, int nb_groups)
Split the commandline into an intermediate form convenient for further processing.
Definition: cmdutils.c:598
void show_banner(void)
Print the program banner to stderr.
Definition: cmdutils.c:815
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:1571
const AVClass * av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
Iterate over potential AVOptions-enabled children of parent.
Definition: opt.c:753
int opt_loglevel(void *optctx, const char *opt, const char *arg)
Set the libav* libraries log level.
Definition: cmdutils.c:710
enum AVCodecID video_codec
default video codec
Definition: avformat.h:457
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the given stream matches a stream specifier.
Definition: cmdutils.c:1506
#define INDENT
Definition: cmdutils.c:772
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:404
enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
Get the type of an AVFilterPad.
Definition: avfilter.c:723
int num
numerator
Definition: rational.h:44
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:269
int index
stream index in AVFormatContext
Definition: avformat.h:700
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:69
int show_protocols(void *optctx, const char *opt, const char *arg)
Print a listing containing all the protocols supported by the program.
Definition: cmdutils.c:1149
#define CONFIG_GPL
Definition: config.h:372
const char * arg
Definition: cmdutils.h:253
const char * sep
Option to be used as group separator.
Definition: cmdutils.h:243
#define GET_CH_LAYOUT_DESC(ch_layout)
Definition: cmdutils.h:554
int64_t last_pts
Number of incorrect DTS values so far.
Definition: cmdutils.h:478
enum AVMediaType type
Definition: avcodec.h:2825
#define FF_ARRAY_ELEMS(a)
int show_formats(void *optctx, const char *opt, const char *arg)
Print a listing containing all the formats supported by the program.
Definition: cmdutils.c:907
const AVClass * sws_get_class(void)
Get the AVClass for swsContext.
Definition: options.c:71
int id
Definition: avformat.h:893
#define OPT_DOUBLE
Definition: cmdutils.h:161
#define OPT_FLOAT
Definition: cmdutils.h:149
AVCodec.
Definition: avcodec.h:2812
int show_pix_fmts(void *optctx, const char *opt, const char *arg)
Print a listing containing all the pixel formats supported by the program.
Definition: cmdutils.c:1176
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, AVCodec *codec)
Filter out options for given codec.
Definition: cmdutils.c:1589
void init_pts_correction(PtsCorrectionContext *ctx)
Reset the state of the PtsCorrectionContext.
Definition: cmdutils.c:1445
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
void uninit_parse_context(OptionParseContext *octx)
Free all allocated memory in an OptionParseContext.
Definition: cmdutils.c:572
int av_codec_is_decoder(const AVCodec *codec)
Definition: utils.c:100
const AVCodecDescriptor * avcodec_descriptor_next(const AVCodecDescriptor *prev)
Iterate over all codec descriptors known to libavcodec.
Definition: codec_desc.c:2363
Format I/O context.
Definition: avformat.h:922
const AVClass * avresample_get_class(void)
Get the AVClass for AVAudioResampleContext.
Definition: options.c:110
const AVClass * avcodec_get_class(void)
Get the AVClass for AVCodecContext.
Definition: options.c:217
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:95
unsigned int nb_stream_indexes
Definition: avformat.h:897
#define AV_LOG_QUIET
Print no output.
Definition: log.h:105
static int warned_cfg
Definition: cmdutils.c:770
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int show_codecs(void *optctx, const char *opt, const char *arg)
Print a listing containing all the codecs supported by the program.
Definition: cmdutils.c:1047
Public dictionary API.
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
#define LIBAV_CONFIGURATION
Definition: config.h:4
void register_exit(void(*cb)(int ret))
Register a program-specific cleanup routine.
Definition: cmdutils.c:89
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition: cmdutils.c:82
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:189
int opt_default(void *optctx, const char *opt, const char *arg)
Fallback for options that are not explicitly handled, these will be parsed through AVOptions...
Definition: cmdutils.c:433
AVOptions.
#define HAS_ARG
Definition: cmdutils.h:142
#define AV_LOG_PANIC
Something went really wrong and we will crash now.
Definition: log.h:110
#define AV_CODEC_PROP_LOSSLESS
Codec supports lossless compression.
Definition: avcodec.h:511
#define CONFIG_LGPLV3
Definition: config.h:425
int id
Format-specific stream ID.
Definition: avformat.h:706
int nb_opts
Definition: cmdutils.h:256
#define OPT_OFFSET
Definition: cmdutils.h:156
static void init_parse_context(OptionParseContext *octx, const OptionGroupDef *groups, int nb_groups)
Definition: cmdutils.c:550
const char * name
void init_opts(void)
Initialize the cmdutils option system, in particular allocate the *_opts contexts.
Definition: cmdutils.c:63
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
int flags
A combination of AVFILTER_FLAG_*.
Definition: avfilter.h:464
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
Definition: cpu.c:75
const char * name
Definition: avcodec.h:4313
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:550
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *, const char *))
Definition: cmdutils.c:333
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:1321
#define OPT_SPEC
Definition: cmdutils.h:157
const AVFilter * avfilter_next(const AVFilter *prev)
Iterate over all registered filters.
Definition: avfilter.c:307
char * av_strndup(const char *s, size_t len)
Duplicate a substring of the string s.
Definition: mem.c:225
static void print_all_libs_info(int flags, int level)
Definition: cmdutils.c:804
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
const AVClass * avformat_get_class(void)
Get the AVClass for AVFormatContext.
Definition: options.c:114
void parse_loglevel(int argc, char **argv, const OptionDef *options)
Find the '-loglevel' option in the command line args and apply it.
Definition: cmdutils.c:423
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:410
external api for the swscale stuff
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
Print help for all options matching specified flags.
Definition: cmdutils.c:135
static void print_codecs(int encoder)
Definition: cmdutils.c:1095
unsigned int * stream_index
Definition: avformat.h:896
int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
Return index of option opt in argv or 0 if not found.
Definition: cmdutils.c:397
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:941
const char * name
Definition: pixdesc.h:70
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
Setup AVCodecContext options for avformat_find_stream_info().
Definition: cmdutils.c:1645
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition: utils.c:1766
AVDictionary * format_opts
Definition: cmdutils.c:59
const OptionDef options[]
Definition: avconv_opt.c:2187
int show_help(void *optctx, const char *opt, const char *arg)
Generic -h handler common to all avtools.
Definition: cmdutils.c:1347
Main libavdevice API header.
int flags
Option flags that must be set on each option that is applied to this group.
Definition: cmdutils.h:248
enum AVCodecID id
Definition: avcodec.h:2826
void show_help_default(const char *opt, const char *arg)
Per-avtool specific help handler.
Definition: avconv_opt.c:2025
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
const char * long_name
Descriptive name for the format, meant to be more human-readable than name.
Definition: avformat.h:452
int show_sample_fmts(void *optctx, const char *opt, const char *arg)
Print a listing containing all the sample formats supported by the program.
Definition: cmdutils.c:1209
sample_fmts
Definition: avconv_filter.c:68
#define sws_isSupportedOutput(x)
AVCodec * av_codec_next(const AVCodec *c)
If c is NULL, returns the first registered codec, if c is non-NULL, returns the next registered codec...
Definition: utils.c:75
g
Definition: yuv2rgb.c:535
int capabilities
Codec capabilities.
Definition: avcodec.h:2831
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:415
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
unsigned int nb_programs
Definition: avformat.h:1069
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:170
const char * name
Definition: cmdutils.h:140
static void show_help_muxer(const char *name)
Definition: cmdutils.c:1271
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
Parse one given option.
Definition: cmdutils.c:300
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:120
simple assert() macros that are a bit more flexible than ISO C assert().
#define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name)
Definition: cmdutils.c:955
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
int flags
Definition: cmdutils.h:141
const char * long_name
A more descriptive name for this codec.
Definition: avcodec.h:490
const char * val
Definition: cmdutils.h:233
enum AVCodecID codec_id
Definition: mov_chan.c:432
int show_filters(void *optctx, const char *opt, const char *arg)
Print a listing containing all the filters supported by the program.
Definition: cmdutils.c:1164
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2353
static const OptionDef * find_option(const OptionDef *po, const char *name)
Definition: cmdutils.c:174
int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int64_t dts)
Attempt to guess proper monotonic timestamps for decoded video frames which might have incorrect time...
Definition: cmdutils.c:1451
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: avcodec.h:494
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:283
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition: opt.c:700
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
AVBitStreamFilter * av_bitstream_filter_next(const AVBitStreamFilter *f)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
OptionGroup * groups
Definition: cmdutils.h:271
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:1615
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:162
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
size_t off
Definition: cmdutils.h:167
const char * media_type_string(enum AVMediaType media_type)
Get a string describing a media type.
Definition: cmdutils.c:1684
external API header
#define FFMIN(a, b)
Definition: common.h:57
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:201
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:383
int show_bsfs(void *optctx, const char *opt, const char *arg)
Print a listing containing all the bit stream filters supported by the program.
Definition: cmdutils.c:1138
static void print_codecs_for_id(enum AVCodecID id, int encoder)
Definition: cmdutils.c:1035
const char * avio_enum_protocols(void **opaque, int output)
Iterate through names of available protocols.
Definition: avio.c:86
const char * name
Definition: avformat.h:446
#define GET_PIX_FMT_NAME(pix_fmt)
Definition: cmdutils.h:540
const OptionGroupDef * group_def
Definition: cmdutils.h:269
A list of option groups that all have the same group type (e.g.
Definition: cmdutils.h:268
#define OPT_EXIT
Definition: cmdutils.h:152
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:1666
#define CONFIG_GPLV3
Definition: config.h:412
AVDictionary * resample_opts
Definition: cmdutils.c:59
#define OPT_INT64
Definition: cmdutils.h:151
AVDictionary * metadata
Definition: avformat.h:771
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:104
#define sws_isSupportedInput(x)
static const OptionGroupDef groups[]
Definition: avconv_opt.c:2102
Opaque data information usually sparse.
Definition: avutil.h:191
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
int opt_timelimit(void *optctx, const char *opt, const char *arg)
Limit the execution time.
Definition: cmdutils.c:745
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:474
AVOutputFormat * av_oformat_next(const AVOutputFormat *f)
If f is NULL, returns the first registered output format, if f is non-NULL, returns the next register...
Definition: format.c:47
void * dst_ptr
Definition: cmdutils.h:165
const AVCodecDescriptor * avcodec_descriptor_get_by_name(const char *name)
Definition: codec_desc.c:2372
#define GET_SAMPLE_FMT_NAME(sample_fmt)
Definition: cmdutils.h:543
void exit_program(int ret)
Wraps exit with a program-specific cleanup routine.
Definition: cmdutils.c:94
const AVFilterPad * inputs
List of inputs, terminated by a zeroed element.
Definition: avfilter.h:441
const char * long_name
Descriptive name for the format, meant to be more human-readable than name.
Definition: avformat.h:532
#define INFINITY
Definition: math.h:27
Stream structure.
Definition: avformat.h:699
static char get_media_type_char(enum AVMediaType type)
Definition: cmdutils.c:1014
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:80
#define GET_SAMPLE_RATE_NAME(rate)
Definition: cmdutils.h:546
const char * long_name
Descriptive name for the codec, meant to be more human readable than name.
Definition: avcodec.h:2824
NULL
Definition: eval.c:55
const AVClass * priv_class
A class for the private data, used to declare filter private AVOptions.
Definition: avfilter.h:459
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
static const AVCodec * next_codec_for_id(enum AVCodecID id, const AVCodec *prev, int encoder)
Definition: cmdutils.c:1024
enum AVMediaType codec_type
Definition: avcodec.h:1058
const AVRational * supported_framerates
array of supported framerates, or NULL if any, array is terminated by {0,0}
Definition: avcodec.h:2832
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
enum AVCodecID codec_id
Definition: avcodec.h:1067
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:534
const char * help
Definition: cmdutils.h:169
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:270
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1780
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
const int program_birth_year
program birth year, defined by the program for show_banner()
Definition: avconv.c:83
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
#define SHOW_VERSION
Definition: cmdutils.c:773
FILE * get_preset_file(char *filename, size_t filename_size, const char *preset_name, int is_path, const char *codec_name)
Get a file corresponding to a preset file.
Definition: cmdutils.c:1473
const OptionGroupDef * group_def
Definition: cmdutils.h:252
#define PRINT_LIB_INFO(libname, LIBNAME, flags, level)
Definition: cmdutils.c:776
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
int index
Definition: gxfenc.c:72
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:458
rational number numerator/denominator
Definition: rational.h:43
const char program_name[]
program name, defined by the program for show_version().
Definition: avconv.c:82
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
Parse a string specifying a time and return its corresponding value as a number of microseconds...
Definition: cmdutils.c:123
void * grow_array(void *array, int elem_size, int *size, int new_size)
Realloc array to hold new_size elements of elem_size.
Definition: cmdutils.c:1665
const char * argname
Definition: cmdutils.h:170
#define OPT_STRING
Definition: cmdutils.h:145
struct SwsContext * sws_opts
Definition: cmdutils.c:58
AVMediaType
Definition: avutil.h:185
const char * name
Filter name.
Definition: avfilter.h:425
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:486
int64_t last_dts
PTS of the last frame.
Definition: cmdutils.h:479
const char * avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
Get the name of an AVFilterPad.
Definition: avfilter.c:718
static void print_codec(const AVCodec *c)
Definition: cmdutils.c:968
misc parsing utilities
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:116
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:478
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
Parse a string and return its corresponding value as a double.
Definition: cmdutils.c:102
#define OPT_TIME
Definition: cmdutils.h:160
#define CC_IDENT
Definition: config.h:7
int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
Read the file with name filename, and put its content in a newly allocated 0-terminated buffer...
Definition: cmdutils.c:1390
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition: utils.c:1785
#define LIBAV_VERSION
Definition: version.h:1
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:2840
uint8_t level
Definition: svq3.c:147
static int match_group_separator(const OptionGroupDef *groups, int nb_groups, const char *opt)
Definition: cmdutils.c:485
#define AVCONV_DATADIR
Definition: config.h:6
static int swscale(SwsContext *c, const uint8_t *src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t *dst[], int dstStride[])
Definition: swscale.c:340
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:23
static int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:172
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
enum AVMediaType type
Definition: avcodec.h:480
const char * extensions
If extensions are defined, then no probe is done.
Definition: avformat.h:546
#define OPT_BOOL
Definition: cmdutils.h:143
An option extracted from the commandline.
Definition: cmdutils.h:230
Main libavformat public API header.
void print_error(const char *filename, int err)
Print an error message to stderr, indicating filename and a human readable description of the error c...
Definition: cmdutils.c:758
static const int this_year
Definition: cmdutils.c:61
#define OPT_INT
Definition: cmdutils.h:148
AVDictionary * codec_opts
Definition: cmdutils.c:59
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:759
AVDictionary * format_opts
Definition: cmdutils.h:259
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:755
OptionGroupList * groups
Definition: cmdutils.h:278
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
static void(* program_exit)(int ret)
Definition: cmdutils.c:87
OptionGroup global_opts
Definition: cmdutils.h:276
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:741
#define AV_OPT_SEARCH_FAKE_OBJ
The obj passed to av_opt_find() is fake – only a double pointer to AVClass instead of a required po...
Definition: opt.h:392
char * key
Definition: dict.h:75
int den
denominator
Definition: rational.h:45
void uninit_opts(void)
Uninitialize the cmdutils option system, in particular free the *_opts contexts and their contents...
Definition: cmdutils.c:71
const char * key
Definition: cmdutils.h:232
union OptionDef::@2 u
#define CONFIG_NONFREE
Definition: config.h:373
#define AVUNERROR(e)
Definition: error.h:44
enum AVCodecID id
Definition: avcodec.h:479
#define GROW_ARRAY(array, nb_elems)
Definition: cmdutils.h:537
const OptionDef * opt
Definition: cmdutils.h:231
const char * description
A description of the filter.
Definition: avfilter.h:432
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:56
char * value
Definition: dict.h:76
#define SHOW_CONFIG
Definition: cmdutils.c:774
int len
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:456
static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
Definition: cmdutils.c:246
static int write_option(void *optctx, const OptionDef *po, const char *opt, const char *arg)
Definition: cmdutils.c:252
void show_help_children(const AVClass *class, int flags)
Show help for all options with given flags in class and all its children.
Definition: cmdutils.c:164
#define GET_ARG(arg)
OptionGroup cur_group
Definition: cmdutils.h:282
int avfilter_pad_count(const AVFilterPad *pads)
Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
Definition: avfilter.c:323
int opt_cpuflags(void *optctx, const char *opt, const char *arg)
Override the cpuflags mask.
Definition: cmdutils.c:699
AVDictionary * codec_opts
Definition: cmdutils.h:258
Option * opts
Definition: cmdutils.h:255
int read_yesno(void)
Return a positive value if a line read from standard input starts with [yY], otherwise return 0...
Definition: cmdutils.c:1379
const AVFilterPad * outputs
List of outputs, terminated by a zeroed element.
Definition: avfilter.h:449
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
static void show_help_demuxer(const char *name)
Definition: cmdutils.c:1253
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:117
int show_version(void *optctx, const char *opt, const char *arg)
Print the version of the program to stdout.
Definition: cmdutils.c:827
#define OPT_PERFILE
Definition: cmdutils.h:154
const char * extensions
comma-separated filename extensions
Definition: avformat.h:454
const char * mime_type
Definition: avformat.h:453
struct SwsContext * sws_opts
Definition: cmdutils.h:261
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:212
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
AVInputFormat * av_iformat_next(const AVInputFormat *f)
If f is NULL, returns the first registered input format, if f is non-NULL, returns the next registere...
Definition: format.c:39
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static void show_help_codec(const char *name, int encoder)
Definition: cmdutils.c:1218
#define av_unused
Definition: attributes.h:86
AVProgram ** programs
Definition: avformat.h:1070
int show_encoders(void *optctx, const char *opt, const char *arg)
Print a listing containing all the encoders supported by the program.
Definition: cmdutils.c:1132
simple arithmetic expression evaluator
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:1606
static void add_opt(OptionParseContext *octx, const OptionDef *opt, const char *key, const char *val)
Definition: cmdutils.c:538