You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1704 lines
55KB

  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. #include <string.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. /* Include only the enabled headers since some compilers (namely, Sun
  27. Studio) will not omit unused inline functions and create undefined
  28. references to libraries that are not being built. */
  29. #include "config.h"
  30. #include "libavformat/avformat.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavdevice/avdevice.h"
  33. #include "libavresample/avresample.h"
  34. #include "libswscale/swscale.h"
  35. #include "libavutil/attributes.h"
  36. #include "libavutil/avassert.h"
  37. #include "libavutil/avstring.h"
  38. #include "libavutil/mathematics.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/parseutils.h"
  41. #include "libavutil/pixdesc.h"
  42. #include "libavutil/eval.h"
  43. #include "libavutil/dict.h"
  44. #include "libavutil/opt.h"
  45. #include "libavutil/cpu.h"
  46. #include "avversion.h"
  47. #include "cmdutils.h"
  48. #if CONFIG_NETWORK
  49. #include "libavformat/network.h"
  50. #endif
  51. #if HAVE_SYS_RESOURCE_H
  52. #include <sys/time.h>
  53. #include <sys/resource.h>
  54. #endif
  55. struct SwsContext *sws_opts;
  56. AVDictionary *format_opts, *codec_opts, *resample_opts;
  57. static const int this_year = 2017;
  58. void init_opts(void)
  59. {
  60. #if CONFIG_SWSCALE
  61. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
  62. NULL, NULL, NULL);
  63. #endif
  64. }
  65. void uninit_opts(void)
  66. {
  67. #if CONFIG_SWSCALE
  68. sws_freeContext(sws_opts);
  69. sws_opts = NULL;
  70. #endif
  71. av_dict_free(&format_opts);
  72. av_dict_free(&codec_opts);
  73. av_dict_free(&resample_opts);
  74. }
  75. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  76. {
  77. vfprintf(stdout, fmt, vl);
  78. }
  79. static void (*program_exit)(int ret);
  80. void register_exit(void (*cb)(int ret))
  81. {
  82. program_exit = cb;
  83. }
  84. void exit_program(int ret)
  85. {
  86. if (program_exit)
  87. program_exit(ret);
  88. exit(ret);
  89. }
  90. double parse_number_or_die(const char *context, const char *numstr, int type,
  91. double min, double max)
  92. {
  93. char *tail;
  94. const char *error;
  95. double d = av_strtod(numstr, &tail);
  96. if (*tail)
  97. error = "Expected number for %s but found: %s\n";
  98. else if (d < min || d > max)
  99. error = "The value for %s was %s which is not within %f - %f\n";
  100. else if (type == OPT_INT64 && (int64_t)d != d)
  101. error = "Expected int64 for %s but found %s\n";
  102. else if (type == OPT_INT && (int)d != d)
  103. error = "Expected int for %s but found %s\n";
  104. else
  105. return d;
  106. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  107. exit_program(1);
  108. return 0;
  109. }
  110. int64_t parse_time_or_die(const char *context, const char *timestr,
  111. int is_duration)
  112. {
  113. int64_t us;
  114. if (av_parse_time(&us, timestr, is_duration) < 0) {
  115. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  116. is_duration ? "duration" : "date", context, timestr);
  117. exit_program(1);
  118. }
  119. return us;
  120. }
  121. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  122. int rej_flags, int alt_flags)
  123. {
  124. const OptionDef *po;
  125. int first;
  126. first = 1;
  127. for (po = options; po->name != NULL; po++) {
  128. char buf[64];
  129. if (((po->flags & req_flags) != req_flags) ||
  130. (alt_flags && !(po->flags & alt_flags)) ||
  131. (po->flags & rej_flags))
  132. continue;
  133. if (first) {
  134. printf("%s\n", msg);
  135. first = 0;
  136. }
  137. av_strlcpy(buf, po->name, sizeof(buf));
  138. if (po->argname) {
  139. av_strlcat(buf, " ", sizeof(buf));
  140. av_strlcat(buf, po->argname, sizeof(buf));
  141. }
  142. printf("-%-17s %s\n", buf, po->help);
  143. }
  144. printf("\n");
  145. }
  146. void show_help_children(const AVClass *class, int flags)
  147. {
  148. const AVClass *child = NULL;
  149. av_opt_show2(&class, NULL, flags, 0);
  150. printf("\n");
  151. while (child = av_opt_child_class_next(class, child))
  152. show_help_children(child, flags);
  153. }
  154. static const OptionDef *find_option(const OptionDef *po, const char *name)
  155. {
  156. const char *p = strchr(name, ':');
  157. int len = p ? p - name : strlen(name);
  158. while (po->name) {
  159. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  160. break;
  161. po++;
  162. }
  163. return po;
  164. }
  165. /* _WIN32 means using the windows libc - cygwin doesn't define that
  166. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  167. * it doesn't provide the actual command line via GetCommandLineW(). */
  168. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  169. #include <windows.h>
  170. #include <shellapi.h>
  171. /* Will be leaked on exit */
  172. static char** win32_argv_utf8 = NULL;
  173. static int win32_argc = 0;
  174. /**
  175. * Prepare command line arguments for executable.
  176. * For Windows - perform wide-char to UTF-8 conversion.
  177. * Input arguments should be main() function arguments.
  178. * @param argc_ptr Arguments number (including executable)
  179. * @param argv_ptr Arguments list.
  180. */
  181. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  182. {
  183. char *argstr_flat;
  184. wchar_t **argv_w;
  185. int i, buffsize = 0, offset = 0;
  186. if (win32_argv_utf8) {
  187. *argc_ptr = win32_argc;
  188. *argv_ptr = win32_argv_utf8;
  189. return;
  190. }
  191. win32_argc = 0;
  192. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  193. if (win32_argc <= 0 || !argv_w)
  194. return;
  195. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  196. for (i = 0; i < win32_argc; i++)
  197. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  198. NULL, 0, NULL, NULL);
  199. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  200. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  201. if (!win32_argv_utf8) {
  202. LocalFree(argv_w);
  203. return;
  204. }
  205. for (i = 0; i < win32_argc; i++) {
  206. win32_argv_utf8[i] = &argstr_flat[offset];
  207. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  208. &argstr_flat[offset],
  209. buffsize - offset, NULL, NULL);
  210. }
  211. win32_argv_utf8[i] = NULL;
  212. LocalFree(argv_w);
  213. *argc_ptr = win32_argc;
  214. *argv_ptr = win32_argv_utf8;
  215. }
  216. #else
  217. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  218. {
  219. /* nothing to do */
  220. }
  221. #endif /* HAVE_COMMANDLINETOARGVW */
  222. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  223. const char *arg)
  224. {
  225. /* new-style options contain an offset into optctx, old-style address of
  226. * a global var*/
  227. void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
  228. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  229. int *dstcount;
  230. if (po->flags & OPT_SPEC) {
  231. SpecifierOpt **so = dst;
  232. char *p = strchr(opt, ':');
  233. char *str;
  234. dstcount = (int *)(so + 1);
  235. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  236. str = av_strdup(p ? p + 1 : "");
  237. if (!str)
  238. return AVERROR(ENOMEM);
  239. (*so)[*dstcount - 1].specifier = str;
  240. dst = &(*so)[*dstcount - 1].u;
  241. }
  242. if (po->flags & OPT_STRING) {
  243. char *str;
  244. str = av_strdup(arg);
  245. av_freep(dst);
  246. if (!str)
  247. return AVERROR(ENOMEM);
  248. *(char **)dst = str;
  249. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  250. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  251. } else if (po->flags & OPT_INT64) {
  252. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  253. } else if (po->flags & OPT_TIME) {
  254. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  255. } else if (po->flags & OPT_FLOAT) {
  256. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  257. } else if (po->flags & OPT_DOUBLE) {
  258. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  259. } else if (po->u.func_arg) {
  260. int ret = po->u.func_arg(optctx, opt, arg);
  261. if (ret < 0) {
  262. av_log(NULL, AV_LOG_ERROR,
  263. "Failed to set value '%s' for option '%s'\n", arg, opt);
  264. return ret;
  265. }
  266. }
  267. if (po->flags & OPT_EXIT)
  268. exit_program(0);
  269. return 0;
  270. }
  271. int parse_option(void *optctx, const char *opt, const char *arg,
  272. const OptionDef *options)
  273. {
  274. const OptionDef *po;
  275. int ret;
  276. po = find_option(options, opt);
  277. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  278. /* handle 'no' bool option */
  279. po = find_option(options, opt + 2);
  280. if ((po->name && (po->flags & OPT_BOOL)))
  281. arg = "0";
  282. } else if (po->flags & OPT_BOOL)
  283. arg = "1";
  284. if (!po->name)
  285. po = find_option(options, "default");
  286. if (!po->name) {
  287. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  288. return AVERROR(EINVAL);
  289. }
  290. if (po->flags & HAS_ARG && !arg) {
  291. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  292. return AVERROR(EINVAL);
  293. }
  294. ret = write_option(optctx, po, opt, arg);
  295. if (ret < 0)
  296. return ret;
  297. return !!(po->flags & HAS_ARG);
  298. }
  299. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  300. void (*parse_arg_function)(void *, const char*))
  301. {
  302. const char *opt;
  303. int optindex, handleoptions = 1, ret;
  304. /* perform system-dependent conversions for arguments list */
  305. prepare_app_arguments(&argc, &argv);
  306. /* parse options */
  307. optindex = 1;
  308. while (optindex < argc) {
  309. opt = argv[optindex++];
  310. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  311. if (opt[1] == '-' && opt[2] == '\0') {
  312. handleoptions = 0;
  313. continue;
  314. }
  315. opt++;
  316. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  317. exit_program(1);
  318. optindex += ret;
  319. } else {
  320. if (parse_arg_function)
  321. parse_arg_function(optctx, opt);
  322. }
  323. }
  324. }
  325. int parse_optgroup(void *optctx, OptionGroup *g)
  326. {
  327. int i, ret;
  328. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  329. g->group_def->name, g->arg);
  330. for (i = 0; i < g->nb_opts; i++) {
  331. Option *o = &g->opts[i];
  332. if (g->group_def->flags &&
  333. !(g->group_def->flags & o->opt->flags)) {
  334. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  335. "%s %s -- you are trying to apply an input option to an "
  336. "output file or vice versa. Move this option before the "
  337. "file it belongs to.\n", o->key, o->opt->help,
  338. g->group_def->name, g->arg);
  339. return AVERROR(EINVAL);
  340. }
  341. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  342. o->key, o->opt->help, o->val);
  343. ret = write_option(optctx, o->opt, o->key, o->val);
  344. if (ret < 0)
  345. return ret;
  346. }
  347. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  348. return 0;
  349. }
  350. int locate_option(int argc, char **argv, const OptionDef *options,
  351. const char *optname)
  352. {
  353. const OptionDef *po;
  354. int i;
  355. for (i = 1; i < argc; i++) {
  356. const char *cur_opt = argv[i];
  357. if (*cur_opt++ != '-')
  358. continue;
  359. po = find_option(options, cur_opt);
  360. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  361. po = find_option(options, cur_opt + 2);
  362. if ((!po->name && !strcmp(cur_opt, optname)) ||
  363. (po->name && !strcmp(optname, po->name)))
  364. return i;
  365. if (!po->name || po->flags & HAS_ARG)
  366. i++;
  367. }
  368. return 0;
  369. }
  370. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  371. {
  372. int idx = locate_option(argc, argv, options, "loglevel");
  373. if (!idx)
  374. idx = locate_option(argc, argv, options, "v");
  375. if (idx && argv[idx + 1])
  376. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  377. }
  378. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  379. int opt_default(void *optctx, const char *opt, const char *arg)
  380. {
  381. const AVOption *o;
  382. char opt_stripped[128];
  383. const char *p;
  384. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  385. #if CONFIG_AVRESAMPLE
  386. const AVClass *rc = avresample_get_class();
  387. #endif
  388. #if CONFIG_SWSCALE
  389. const AVClass *sc = sws_get_class();
  390. #endif
  391. if (!(p = strchr(opt, ':')))
  392. p = opt + strlen(opt);
  393. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  394. if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
  395. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  396. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  397. (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
  398. av_dict_set(&codec_opts, opt, arg, FLAGS);
  399. else if ((o = av_opt_find(&fc, opt, NULL, 0,
  400. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  401. av_dict_set(&format_opts, opt, arg, FLAGS);
  402. #if CONFIG_AVRESAMPLE
  403. else if ((o = av_opt_find(&rc, opt, NULL, 0,
  404. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  405. av_dict_set(&resample_opts, opt, arg, FLAGS);
  406. #endif
  407. #if CONFIG_SWSCALE
  408. else if ((o = av_opt_find(&sc, opt, NULL, 0,
  409. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  410. // XXX we only support sws_flags, not arbitrary sws options
  411. int ret = av_opt_set(sws_opts, opt, arg, 0);
  412. if (ret < 0) {
  413. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  414. return ret;
  415. }
  416. }
  417. #endif
  418. if (o)
  419. return 0;
  420. return AVERROR_OPTION_NOT_FOUND;
  421. }
  422. /*
  423. * Check whether given option is a group separator.
  424. *
  425. * @return index of the group definition that matched or -1 if none
  426. */
  427. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  428. const char *opt)
  429. {
  430. int i;
  431. for (i = 0; i < nb_groups; i++) {
  432. const OptionGroupDef *p = &groups[i];
  433. if (p->sep && !strcmp(p->sep, opt))
  434. return i;
  435. }
  436. return -1;
  437. }
  438. /*
  439. * Finish parsing an option group.
  440. *
  441. * @param group_idx which group definition should this group belong to
  442. * @param arg argument of the group delimiting option
  443. */
  444. static void finish_group(OptionParseContext *octx, int group_idx,
  445. const char *arg)
  446. {
  447. OptionGroupList *l = &octx->groups[group_idx];
  448. OptionGroup *g;
  449. GROW_ARRAY(l->groups, l->nb_groups);
  450. g = &l->groups[l->nb_groups - 1];
  451. *g = octx->cur_group;
  452. g->arg = arg;
  453. g->group_def = l->group_def;
  454. #if CONFIG_SWSCALE
  455. g->sws_opts = sws_opts;
  456. #endif
  457. g->codec_opts = codec_opts;
  458. g->format_opts = format_opts;
  459. g->resample_opts = resample_opts;
  460. codec_opts = NULL;
  461. format_opts = NULL;
  462. resample_opts = NULL;
  463. #if CONFIG_SWSCALE
  464. sws_opts = NULL;
  465. #endif
  466. init_opts();
  467. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  468. }
  469. /*
  470. * Add an option instance to currently parsed group.
  471. */
  472. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  473. const char *key, const char *val)
  474. {
  475. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  476. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  477. GROW_ARRAY(g->opts, g->nb_opts);
  478. g->opts[g->nb_opts - 1].opt = opt;
  479. g->opts[g->nb_opts - 1].key = key;
  480. g->opts[g->nb_opts - 1].val = val;
  481. }
  482. static void init_parse_context(OptionParseContext *octx,
  483. const OptionGroupDef *groups, int nb_groups)
  484. {
  485. static const OptionGroupDef global_group = { "global" };
  486. int i;
  487. memset(octx, 0, sizeof(*octx));
  488. octx->nb_groups = nb_groups;
  489. octx->groups = av_mallocz(sizeof(*octx->groups) * octx->nb_groups);
  490. if (!octx->groups)
  491. exit_program(1);
  492. for (i = 0; i < octx->nb_groups; i++)
  493. octx->groups[i].group_def = &groups[i];
  494. octx->global_opts.group_def = &global_group;
  495. octx->global_opts.arg = "";
  496. init_opts();
  497. }
  498. void uninit_parse_context(OptionParseContext *octx)
  499. {
  500. int i, j;
  501. for (i = 0; i < octx->nb_groups; i++) {
  502. OptionGroupList *l = &octx->groups[i];
  503. for (j = 0; j < l->nb_groups; j++) {
  504. av_freep(&l->groups[j].opts);
  505. av_dict_free(&l->groups[j].codec_opts);
  506. av_dict_free(&l->groups[j].format_opts);
  507. av_dict_free(&l->groups[j].resample_opts);
  508. #if CONFIG_SWSCALE
  509. sws_freeContext(l->groups[j].sws_opts);
  510. #endif
  511. }
  512. av_freep(&l->groups);
  513. }
  514. av_freep(&octx->groups);
  515. av_freep(&octx->cur_group.opts);
  516. av_freep(&octx->global_opts.opts);
  517. uninit_opts();
  518. }
  519. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  520. const OptionDef *options,
  521. const OptionGroupDef *groups, int nb_groups)
  522. {
  523. int optindex = 1;
  524. /* perform system-dependent conversions for arguments list */
  525. prepare_app_arguments(&argc, &argv);
  526. init_parse_context(octx, groups, nb_groups);
  527. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  528. while (optindex < argc) {
  529. const char *opt = argv[optindex++], *arg;
  530. const OptionDef *po;
  531. int ret;
  532. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  533. /* unnamed group separators, e.g. output filename */
  534. if (opt[0] != '-' || !opt[1]) {
  535. finish_group(octx, 0, opt);
  536. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  537. continue;
  538. }
  539. opt++;
  540. #define GET_ARG(arg) \
  541. do { \
  542. arg = argv[optindex++]; \
  543. if (!arg) { \
  544. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  545. return AVERROR(EINVAL); \
  546. } \
  547. } while (0)
  548. /* named group separators, e.g. -i */
  549. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  550. GET_ARG(arg);
  551. finish_group(octx, ret, arg);
  552. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  553. groups[ret].name, arg);
  554. continue;
  555. }
  556. /* normal options */
  557. po = find_option(options, opt);
  558. if (po->name) {
  559. if (po->flags & OPT_EXIT) {
  560. /* optional argument, e.g. -h */
  561. arg = argv[optindex++];
  562. } else if (po->flags & HAS_ARG) {
  563. GET_ARG(arg);
  564. } else {
  565. arg = "1";
  566. }
  567. add_opt(octx, po, opt, arg);
  568. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  569. "argument '%s'.\n", po->name, po->help, arg);
  570. continue;
  571. }
  572. /* AVOptions */
  573. if (argv[optindex]) {
  574. ret = opt_default(NULL, opt, argv[optindex]);
  575. if (ret >= 0) {
  576. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  577. "argument '%s'.\n", opt, argv[optindex]);
  578. optindex++;
  579. continue;
  580. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  581. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  582. "with argument '%s'.\n", opt, argv[optindex]);
  583. return ret;
  584. }
  585. }
  586. /* boolean -nofoo options */
  587. if (opt[0] == 'n' && opt[1] == 'o' &&
  588. (po = find_option(options, opt + 2)) &&
  589. po->name && po->flags & OPT_BOOL) {
  590. add_opt(octx, po, opt, "0");
  591. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  592. "argument 0.\n", po->name, po->help);
  593. continue;
  594. }
  595. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  596. return AVERROR_OPTION_NOT_FOUND;
  597. }
  598. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  599. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  600. "commandline.\n");
  601. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  602. return 0;
  603. }
  604. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  605. {
  606. int flags = av_parse_cpu_flags(arg);
  607. if (flags < 0)
  608. return flags;
  609. av_set_cpu_flags_mask(flags);
  610. return 0;
  611. }
  612. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  613. {
  614. const struct { const char *name; int level; } log_levels[] = {
  615. { "quiet" , AV_LOG_QUIET },
  616. { "panic" , AV_LOG_PANIC },
  617. { "fatal" , AV_LOG_FATAL },
  618. { "error" , AV_LOG_ERROR },
  619. { "warning", AV_LOG_WARNING },
  620. { "info" , AV_LOG_INFO },
  621. { "verbose", AV_LOG_VERBOSE },
  622. { "debug" , AV_LOG_DEBUG },
  623. { "trace" , AV_LOG_TRACE },
  624. };
  625. char *tail;
  626. int level;
  627. int i;
  628. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  629. if (!strcmp(log_levels[i].name, arg)) {
  630. av_log_set_level(log_levels[i].level);
  631. return 0;
  632. }
  633. }
  634. level = strtol(arg, &tail, 10);
  635. if (*tail) {
  636. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  637. "Possible levels are numbers or:\n", arg);
  638. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  639. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  640. exit_program(1);
  641. }
  642. av_log_set_level(level);
  643. return 0;
  644. }
  645. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  646. {
  647. #if HAVE_SETRLIMIT
  648. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  649. struct rlimit rl = { lim, lim + 1 };
  650. if (setrlimit(RLIMIT_CPU, &rl))
  651. perror("setrlimit");
  652. #else
  653. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  654. #endif
  655. return 0;
  656. }
  657. void print_error(const char *filename, int err)
  658. {
  659. char errbuf[128];
  660. const char *errbuf_ptr = errbuf;
  661. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  662. errbuf_ptr = strerror(AVUNERROR(err));
  663. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  664. }
  665. static int warned_cfg = 0;
  666. #define INDENT 1
  667. #define SHOW_VERSION 2
  668. #define SHOW_CONFIG 4
  669. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  670. if (CONFIG_##LIBNAME) { \
  671. const char *indent = flags & INDENT? " " : ""; \
  672. if (flags & SHOW_VERSION) { \
  673. unsigned int version = libname##_version(); \
  674. av_log(NULL, level, \
  675. "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  676. indent, #libname, \
  677. LIB##LIBNAME##_VERSION_MAJOR, \
  678. LIB##LIBNAME##_VERSION_MINOR, \
  679. LIB##LIBNAME##_VERSION_MICRO, \
  680. version >> 16, version >> 8 & 0xff, version & 0xff); \
  681. } \
  682. if (flags & SHOW_CONFIG) { \
  683. const char *cfg = libname##_configuration(); \
  684. if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
  685. if (!warned_cfg) { \
  686. av_log(NULL, level, \
  687. "%sWARNING: library configuration mismatch\n", \
  688. indent); \
  689. warned_cfg = 1; \
  690. } \
  691. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  692. indent, #libname, cfg); \
  693. } \
  694. } \
  695. } \
  696. static void print_all_libs_info(int flags, int level)
  697. {
  698. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  699. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  700. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  701. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  702. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  703. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  704. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  705. }
  706. void show_banner(void)
  707. {
  708. av_log(NULL, AV_LOG_INFO,
  709. "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
  710. program_name, program_birth_year, this_year);
  711. av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
  712. __DATE__, __TIME__, CC_IDENT);
  713. av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
  714. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
  715. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
  716. }
  717. int show_version(void *optctx, const char *opt, const char *arg)
  718. {
  719. av_log_set_callback(log_callback_help);
  720. printf("%s " LIBAV_VERSION "\n", program_name);
  721. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  722. return 0;
  723. }
  724. int show_license(void *optctx, const char *opt, const char *arg)
  725. {
  726. printf(
  727. #if CONFIG_NONFREE
  728. "This version of %s has nonfree parts compiled in.\n"
  729. "Therefore it is not legally redistributable.\n",
  730. program_name
  731. #elif CONFIG_GPLV3
  732. "%s is free software; you can redistribute it and/or modify\n"
  733. "it under the terms of the GNU General Public License as published by\n"
  734. "the Free Software Foundation; either version 3 of the License, or\n"
  735. "(at your option) any later version.\n"
  736. "\n"
  737. "%s is distributed in the hope that it will be useful,\n"
  738. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  739. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  740. "GNU General Public License for more details.\n"
  741. "\n"
  742. "You should have received a copy of the GNU General Public License\n"
  743. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  744. program_name, program_name, program_name
  745. #elif CONFIG_GPL
  746. "%s is free software; you can redistribute it and/or modify\n"
  747. "it under the terms of the GNU General Public License as published by\n"
  748. "the Free Software Foundation; either version 2 of the License, or\n"
  749. "(at your option) any later version.\n"
  750. "\n"
  751. "%s is distributed in the hope that it will be useful,\n"
  752. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  753. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  754. "GNU General Public License for more details.\n"
  755. "\n"
  756. "You should have received a copy of the GNU General Public License\n"
  757. "along with %s; if not, write to the Free Software\n"
  758. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  759. program_name, program_name, program_name
  760. #elif CONFIG_LGPLV3
  761. "%s is free software; you can redistribute it and/or modify\n"
  762. "it under the terms of the GNU Lesser General Public License as published by\n"
  763. "the Free Software Foundation; either version 3 of the License, or\n"
  764. "(at your option) any later version.\n"
  765. "\n"
  766. "%s is distributed in the hope that it will be useful,\n"
  767. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  768. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  769. "GNU Lesser General Public License for more details.\n"
  770. "\n"
  771. "You should have received a copy of the GNU Lesser General Public License\n"
  772. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  773. program_name, program_name, program_name
  774. #else
  775. "%s is free software; you can redistribute it and/or\n"
  776. "modify it under the terms of the GNU Lesser General Public\n"
  777. "License as published by the Free Software Foundation; either\n"
  778. "version 2.1 of the License, or (at your option) any later version.\n"
  779. "\n"
  780. "%s is distributed in the hope that it will be useful,\n"
  781. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  782. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  783. "Lesser General Public License for more details.\n"
  784. "\n"
  785. "You should have received a copy of the GNU Lesser General Public\n"
  786. "License along with %s; if not, write to the Free Software\n"
  787. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  788. program_name, program_name, program_name
  789. #endif
  790. );
  791. return 0;
  792. }
  793. int show_formats(void *optctx, const char *opt, const char *arg)
  794. {
  795. AVInputFormat *ifmt = NULL;
  796. AVOutputFormat *ofmt = NULL;
  797. const char *last_name;
  798. printf("File formats:\n"
  799. " D. = Demuxing supported\n"
  800. " .E = Muxing supported\n"
  801. " --\n");
  802. last_name = "000";
  803. for (;;) {
  804. int decode = 0;
  805. int encode = 0;
  806. const char *name = NULL;
  807. const char *long_name = NULL;
  808. while ((ofmt = av_oformat_next(ofmt))) {
  809. if ((!name || strcmp(ofmt->name, name) < 0) &&
  810. strcmp(ofmt->name, last_name) > 0) {
  811. name = ofmt->name;
  812. long_name = ofmt->long_name;
  813. encode = 1;
  814. }
  815. }
  816. while ((ifmt = av_iformat_next(ifmt))) {
  817. if ((!name || strcmp(ifmt->name, name) < 0) &&
  818. strcmp(ifmt->name, last_name) > 0) {
  819. name = ifmt->name;
  820. long_name = ifmt->long_name;
  821. encode = 0;
  822. }
  823. if (name && strcmp(ifmt->name, name) == 0)
  824. decode = 1;
  825. }
  826. if (!name)
  827. break;
  828. last_name = name;
  829. printf(" %s%s %-15s %s\n",
  830. decode ? "D" : " ",
  831. encode ? "E" : " ",
  832. name,
  833. long_name ? long_name:" ");
  834. }
  835. return 0;
  836. }
  837. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  838. if (codec->field) { \
  839. const type *p = c->field; \
  840. \
  841. printf(" Supported " list_name ":"); \
  842. while (*p != term) { \
  843. get_name(*p); \
  844. printf(" %s", name); \
  845. p++; \
  846. } \
  847. printf("\n"); \
  848. } \
  849. static void print_codec(const AVCodec *c)
  850. {
  851. int encoder = av_codec_is_encoder(c);
  852. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  853. c->long_name ? c->long_name : "");
  854. printf(" General capabilities: ");
  855. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  856. printf("horizband ");
  857. if (c->capabilities & AV_CODEC_CAP_DR1)
  858. printf("dr1 ");
  859. if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
  860. printf("trunc ");
  861. if (c->capabilities & AV_CODEC_CAP_DELAY)
  862. printf("delay ");
  863. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  864. printf("small ");
  865. if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
  866. printf("subframes ");
  867. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  868. printf("exp ");
  869. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  870. printf("chconf ");
  871. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  872. printf("paramchange ");
  873. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  874. printf("variable ");
  875. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  876. AV_CODEC_CAP_SLICE_THREADS |
  877. AV_CODEC_CAP_AUTO_THREADS))
  878. printf("threads ");
  879. if (!c->capabilities)
  880. printf("none");
  881. printf("\n");
  882. if (c->type == AVMEDIA_TYPE_VIDEO) {
  883. printf(" Threading capabilities: ");
  884. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  885. AV_CODEC_CAP_SLICE_THREADS |
  886. AV_CODEC_CAP_AUTO_THREADS)) {
  887. case AV_CODEC_CAP_FRAME_THREADS |
  888. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  889. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  890. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  891. case AV_CODEC_CAP_AUTO_THREADS : printf("auto"); break;
  892. default: printf("none"); break;
  893. }
  894. printf("\n");
  895. }
  896. if (c->supported_framerates) {
  897. const AVRational *fps = c->supported_framerates;
  898. printf(" Supported framerates:");
  899. while (fps->num) {
  900. printf(" %d/%d", fps->num, fps->den);
  901. fps++;
  902. }
  903. printf("\n");
  904. }
  905. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  906. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  907. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  908. GET_SAMPLE_RATE_NAME);
  909. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  910. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  911. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  912. 0, GET_CH_LAYOUT_DESC);
  913. if (c->priv_class) {
  914. show_help_children(c->priv_class,
  915. AV_OPT_FLAG_ENCODING_PARAM |
  916. AV_OPT_FLAG_DECODING_PARAM);
  917. }
  918. }
  919. static char get_media_type_char(enum AVMediaType type)
  920. {
  921. switch (type) {
  922. case AVMEDIA_TYPE_VIDEO: return 'V';
  923. case AVMEDIA_TYPE_AUDIO: return 'A';
  924. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  925. default: return '?';
  926. }
  927. }
  928. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  929. int encoder)
  930. {
  931. while ((prev = av_codec_next(prev))) {
  932. if (prev->id == id &&
  933. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  934. return prev;
  935. }
  936. return NULL;
  937. }
  938. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  939. {
  940. const AVCodec *codec = NULL;
  941. printf(" (%s: ", encoder ? "encoders" : "decoders");
  942. while ((codec = next_codec_for_id(id, codec, encoder)))
  943. printf("%s ", codec->name);
  944. printf(")");
  945. }
  946. int show_codecs(void *optctx, const char *opt, const char *arg)
  947. {
  948. const AVCodecDescriptor *desc = NULL;
  949. printf("Codecs:\n"
  950. " D..... = Decoding supported\n"
  951. " .E.... = Encoding supported\n"
  952. " ..V... = Video codec\n"
  953. " ..A... = Audio codec\n"
  954. " ..S... = Subtitle codec\n"
  955. " ...I.. = Intra frame-only codec\n"
  956. " ....L. = Lossy compression\n"
  957. " .....S = Lossless compression\n"
  958. " -------\n");
  959. while ((desc = avcodec_descriptor_next(desc))) {
  960. const AVCodec *codec = NULL;
  961. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  962. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  963. printf("%c", get_media_type_char(desc->type));
  964. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  965. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  966. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  967. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  968. /* print decoders/encoders when there's more than one or their
  969. * names are different from codec name */
  970. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  971. if (strcmp(codec->name, desc->name)) {
  972. print_codecs_for_id(desc->id, 0);
  973. break;
  974. }
  975. }
  976. codec = NULL;
  977. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  978. if (strcmp(codec->name, desc->name)) {
  979. print_codecs_for_id(desc->id, 1);
  980. break;
  981. }
  982. }
  983. printf("\n");
  984. }
  985. return 0;
  986. }
  987. static void print_codecs(int encoder)
  988. {
  989. const AVCodecDescriptor *desc = NULL;
  990. printf("%s:\n"
  991. " V... = Video\n"
  992. " A... = Audio\n"
  993. " S... = Subtitle\n"
  994. " .F.. = Frame-level multithreading\n"
  995. " ..S. = Slice-level multithreading\n"
  996. " ...X = Codec is experimental\n"
  997. " ---\n",
  998. encoder ? "Encoders" : "Decoders");
  999. while ((desc = avcodec_descriptor_next(desc))) {
  1000. const AVCodec *codec = NULL;
  1001. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1002. printf("%c", get_media_type_char(desc->type));
  1003. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1004. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1005. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1006. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1007. if (strcmp(codec->name, desc->name))
  1008. printf(" (codec %s)", desc->name);
  1009. printf("\n");
  1010. }
  1011. }
  1012. }
  1013. int show_decoders(void *optctx, const char *opt, const char *arg)
  1014. {
  1015. print_codecs(0);
  1016. return 0;
  1017. }
  1018. int show_encoders(void *optctx, const char *opt, const char *arg)
  1019. {
  1020. print_codecs(1);
  1021. return 0;
  1022. }
  1023. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1024. {
  1025. const AVBitStreamFilter *bsf = NULL;
  1026. void *opaque = NULL;
  1027. printf("Bitstream filters:\n");
  1028. while ((bsf = av_bsf_next(&opaque)))
  1029. printf("%s\n", bsf->name);
  1030. printf("\n");
  1031. return 0;
  1032. }
  1033. int show_protocols(void *optctx, const char *opt, const char *arg)
  1034. {
  1035. void *opaque = NULL;
  1036. const char *name;
  1037. printf("Supported file protocols:\n"
  1038. "Input:\n");
  1039. while ((name = avio_enum_protocols(&opaque, 0)))
  1040. printf("%s\n", name);
  1041. printf("Output:\n");
  1042. while ((name = avio_enum_protocols(&opaque, 1)))
  1043. printf("%s\n", name);
  1044. return 0;
  1045. }
  1046. int show_filters(void *optctx, const char *opt, const char *arg)
  1047. {
  1048. #if CONFIG_AVFILTER
  1049. const AVFilter *filter = NULL;
  1050. printf("Filters:\n");
  1051. while ((filter = avfilter_next(filter)))
  1052. printf("%-16s %s\n", filter->name, filter->description);
  1053. #else
  1054. printf("No filters available: libavfilter disabled\n");
  1055. #endif
  1056. return 0;
  1057. }
  1058. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1059. {
  1060. const AVPixFmtDescriptor *pix_desc = NULL;
  1061. printf("Pixel formats:\n"
  1062. "I.... = Supported Input format for conversion\n"
  1063. ".O... = Supported Output format for conversion\n"
  1064. "..H.. = Hardware accelerated format\n"
  1065. "...P. = Paletted format\n"
  1066. "....B = Bitstream format\n"
  1067. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1068. "-----\n");
  1069. #if !CONFIG_SWSCALE
  1070. # define sws_isSupportedInput(x) 0
  1071. # define sws_isSupportedOutput(x) 0
  1072. #endif
  1073. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1074. enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1075. printf("%c%c%c%c%c %-16s %d %2d\n",
  1076. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1077. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1078. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1079. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1080. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1081. pix_desc->name,
  1082. pix_desc->nb_components,
  1083. av_get_bits_per_pixel(pix_desc));
  1084. }
  1085. return 0;
  1086. }
  1087. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1088. {
  1089. int i;
  1090. char fmt_str[128];
  1091. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1092. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1093. return 0;
  1094. }
  1095. static void show_help_codec(const char *name, int encoder)
  1096. {
  1097. const AVCodecDescriptor *desc;
  1098. const AVCodec *codec;
  1099. if (!name) {
  1100. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1101. return;
  1102. }
  1103. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1104. avcodec_find_decoder_by_name(name);
  1105. if (codec)
  1106. print_codec(codec);
  1107. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1108. int printed = 0;
  1109. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1110. printed = 1;
  1111. print_codec(codec);
  1112. }
  1113. if (!printed) {
  1114. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
  1115. "but no %s for it are available. Libav might need to be "
  1116. "recompiled with additional external libraries.\n",
  1117. name, encoder ? "encoders" : "decoders");
  1118. }
  1119. } else {
  1120. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
  1121. name);
  1122. }
  1123. }
  1124. static void show_help_demuxer(const char *name)
  1125. {
  1126. const AVInputFormat *fmt = av_find_input_format(name);
  1127. if (!fmt) {
  1128. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1129. return;
  1130. }
  1131. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1132. if (fmt->extensions)
  1133. printf(" Common extensions: %s.\n", fmt->extensions);
  1134. if (fmt->priv_class)
  1135. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1136. }
  1137. static void show_help_muxer(const char *name)
  1138. {
  1139. const AVCodecDescriptor *desc;
  1140. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1141. if (!fmt) {
  1142. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1143. return;
  1144. }
  1145. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1146. if (fmt->extensions)
  1147. printf(" Common extensions: %s.\n", fmt->extensions);
  1148. if (fmt->mime_type)
  1149. printf(" Mime type: %s.\n", fmt->mime_type);
  1150. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1151. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1152. printf(" Default video codec: %s.\n", desc->name);
  1153. }
  1154. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1155. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1156. printf(" Default audio codec: %s.\n", desc->name);
  1157. }
  1158. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1159. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1160. printf(" Default subtitle codec: %s.\n", desc->name);
  1161. }
  1162. if (fmt->priv_class)
  1163. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1164. }
  1165. #if CONFIG_AVFILTER
  1166. static void show_help_filter(const char *name)
  1167. {
  1168. const AVFilter *f = avfilter_get_by_name(name);
  1169. int i, count;
  1170. if (!name) {
  1171. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1172. return;
  1173. } else if (!f) {
  1174. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1175. return;
  1176. }
  1177. printf("Filter %s [%s]:\n", f->name, f->description);
  1178. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1179. printf(" slice threading supported\n");
  1180. printf(" Inputs:\n");
  1181. count = avfilter_pad_count(f->inputs);
  1182. for (i = 0; i < count; i++) {
  1183. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1184. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1185. }
  1186. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1187. printf(" dynamic (depending on the options)\n");
  1188. printf(" Outputs:\n");
  1189. count = avfilter_pad_count(f->outputs);
  1190. for (i = 0; i < count; i++) {
  1191. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1192. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1193. }
  1194. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1195. printf(" dynamic (depending on the options)\n");
  1196. if (f->priv_class)
  1197. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
  1198. AV_OPT_FLAG_AUDIO_PARAM);
  1199. }
  1200. #endif
  1201. int show_help(void *optctx, const char *opt, const char *arg)
  1202. {
  1203. char *topic, *par;
  1204. av_log_set_callback(log_callback_help);
  1205. topic = av_strdup(arg ? arg : "");
  1206. if (!topic)
  1207. return AVERROR(ENOMEM);
  1208. par = strchr(topic, '=');
  1209. if (par)
  1210. *par++ = 0;
  1211. if (!*topic) {
  1212. show_help_default(topic, par);
  1213. } else if (!strcmp(topic, "decoder")) {
  1214. show_help_codec(par, 0);
  1215. } else if (!strcmp(topic, "encoder")) {
  1216. show_help_codec(par, 1);
  1217. } else if (!strcmp(topic, "demuxer")) {
  1218. show_help_demuxer(par);
  1219. } else if (!strcmp(topic, "muxer")) {
  1220. show_help_muxer(par);
  1221. #if CONFIG_AVFILTER
  1222. } else if (!strcmp(topic, "filter")) {
  1223. show_help_filter(par);
  1224. #endif
  1225. } else {
  1226. show_help_default(topic, par);
  1227. }
  1228. av_freep(&topic);
  1229. return 0;
  1230. }
  1231. int read_yesno(void)
  1232. {
  1233. int c = getchar();
  1234. int yesno = (av_toupper(c) == 'Y');
  1235. while (c != '\n' && c != EOF)
  1236. c = getchar();
  1237. return yesno;
  1238. }
  1239. void init_pts_correction(PtsCorrectionContext *ctx)
  1240. {
  1241. ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
  1242. ctx->last_pts = ctx->last_dts = INT64_MIN;
  1243. }
  1244. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
  1245. int64_t dts)
  1246. {
  1247. int64_t pts = AV_NOPTS_VALUE;
  1248. if (dts != AV_NOPTS_VALUE) {
  1249. ctx->num_faulty_dts += dts <= ctx->last_dts;
  1250. ctx->last_dts = dts;
  1251. }
  1252. if (reordered_pts != AV_NOPTS_VALUE) {
  1253. ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
  1254. ctx->last_pts = reordered_pts;
  1255. }
  1256. if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
  1257. && reordered_pts != AV_NOPTS_VALUE)
  1258. pts = reordered_pts;
  1259. else
  1260. pts = dts;
  1261. return pts;
  1262. }
  1263. FILE *get_preset_file(char *filename, size_t filename_size,
  1264. const char *preset_name, int is_path,
  1265. const char *codec_name)
  1266. {
  1267. FILE *f = NULL;
  1268. int i;
  1269. const char *base[3] = { getenv("AVCONV_DATADIR"),
  1270. getenv("HOME"),
  1271. AVCONV_DATADIR, };
  1272. if (is_path) {
  1273. av_strlcpy(filename, preset_name, filename_size);
  1274. f = fopen(filename, "r");
  1275. } else {
  1276. for (i = 0; i < 3 && !f; i++) {
  1277. if (!base[i])
  1278. continue;
  1279. snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
  1280. i != 1 ? "" : "/.avconv", preset_name);
  1281. f = fopen(filename, "r");
  1282. if (!f && codec_name) {
  1283. snprintf(filename, filename_size,
  1284. "%s%s/%s-%s.avpreset",
  1285. base[i], i != 1 ? "" : "/.avconv", codec_name,
  1286. preset_name);
  1287. f = fopen(filename, "r");
  1288. }
  1289. }
  1290. }
  1291. return f;
  1292. }
  1293. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1294. {
  1295. if (*spec <= '9' && *spec >= '0') /* opt:index */
  1296. return strtol(spec, NULL, 0) == st->index;
  1297. else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
  1298. *spec == 't') { /* opt:[vasdt] */
  1299. enum AVMediaType type;
  1300. switch (*spec++) {
  1301. case 'v': type = AVMEDIA_TYPE_VIDEO; break;
  1302. case 'a': type = AVMEDIA_TYPE_AUDIO; break;
  1303. case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
  1304. case 'd': type = AVMEDIA_TYPE_DATA; break;
  1305. case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  1306. default: av_assert0(0);
  1307. }
  1308. if (type != st->codecpar->codec_type)
  1309. return 0;
  1310. if (*spec++ == ':') { /* possibly followed by :index */
  1311. int i, index = strtol(spec, NULL, 0);
  1312. for (i = 0; i < s->nb_streams; i++)
  1313. if (s->streams[i]->codecpar->codec_type == type && index-- == 0)
  1314. return i == st->index;
  1315. return 0;
  1316. }
  1317. return 1;
  1318. } else if (*spec == 'p' && *(spec + 1) == ':') {
  1319. int prog_id, i, j;
  1320. char *endptr;
  1321. spec += 2;
  1322. prog_id = strtol(spec, &endptr, 0);
  1323. for (i = 0; i < s->nb_programs; i++) {
  1324. if (s->programs[i]->id != prog_id)
  1325. continue;
  1326. if (*endptr++ == ':') {
  1327. int stream_idx = strtol(endptr, NULL, 0);
  1328. return stream_idx >= 0 &&
  1329. stream_idx < s->programs[i]->nb_stream_indexes &&
  1330. st->index == s->programs[i]->stream_index[stream_idx];
  1331. }
  1332. for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
  1333. if (st->index == s->programs[i]->stream_index[j])
  1334. return 1;
  1335. }
  1336. return 0;
  1337. } else if (*spec == 'i' && *(spec + 1) == ':') {
  1338. int stream_id;
  1339. char *endptr;
  1340. spec += 2;
  1341. stream_id = strtol(spec, &endptr, 0);
  1342. return stream_id == st->id;
  1343. } else if (*spec == 'm' && *(spec + 1) == ':') {
  1344. AVDictionaryEntry *tag;
  1345. char *key, *val;
  1346. int ret;
  1347. spec += 2;
  1348. val = strchr(spec, ':');
  1349. key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
  1350. if (!key)
  1351. return AVERROR(ENOMEM);
  1352. tag = av_dict_get(st->metadata, key, NULL, 0);
  1353. if (tag) {
  1354. if (!val || !strcmp(tag->value, val + 1))
  1355. ret = 1;
  1356. else
  1357. ret = 0;
  1358. } else
  1359. ret = 0;
  1360. av_freep(&key);
  1361. return ret;
  1362. } else if (*spec == 'u') {
  1363. AVCodecParameters *par = st->codecpar;
  1364. int val;
  1365. switch (par->codec_type) {
  1366. case AVMEDIA_TYPE_AUDIO:
  1367. val = par->sample_rate && par->channels;
  1368. if (par->format == AV_SAMPLE_FMT_NONE)
  1369. return 0;
  1370. break;
  1371. case AVMEDIA_TYPE_VIDEO:
  1372. val = par->width && par->height;
  1373. if (par->format == AV_PIX_FMT_NONE)
  1374. return 0;
  1375. break;
  1376. case AVMEDIA_TYPE_UNKNOWN:
  1377. val = 0;
  1378. break;
  1379. default:
  1380. val = 1;
  1381. break;
  1382. }
  1383. return par->codec_id != AV_CODEC_ID_NONE && val != 0;
  1384. } else if (!*spec) /* empty specifier, matches everything */
  1385. return 1;
  1386. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1387. return AVERROR(EINVAL);
  1388. }
  1389. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1390. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1391. {
  1392. AVDictionary *ret = NULL;
  1393. AVDictionaryEntry *t = NULL;
  1394. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1395. : AV_OPT_FLAG_DECODING_PARAM;
  1396. char prefix = 0;
  1397. const AVClass *cc = avcodec_get_class();
  1398. if (!codec)
  1399. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1400. : avcodec_find_decoder(codec_id);
  1401. switch (st->codecpar->codec_type) {
  1402. case AVMEDIA_TYPE_VIDEO:
  1403. prefix = 'v';
  1404. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1405. break;
  1406. case AVMEDIA_TYPE_AUDIO:
  1407. prefix = 'a';
  1408. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1409. break;
  1410. case AVMEDIA_TYPE_SUBTITLE:
  1411. prefix = 's';
  1412. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1413. break;
  1414. }
  1415. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1416. char *p = strchr(t->key, ':');
  1417. /* check stream specification in opt name */
  1418. if (p)
  1419. switch (check_stream_specifier(s, st, p + 1)) {
  1420. case 1: *p = 0; break;
  1421. case 0: continue;
  1422. default: return NULL;
  1423. }
  1424. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1425. (codec && codec->priv_class &&
  1426. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1427. AV_OPT_SEARCH_FAKE_OBJ)))
  1428. av_dict_set(&ret, t->key, t->value, 0);
  1429. else if (t->key[0] == prefix &&
  1430. av_opt_find(&cc, t->key + 1, NULL, flags,
  1431. AV_OPT_SEARCH_FAKE_OBJ))
  1432. av_dict_set(&ret, t->key + 1, t->value, 0);
  1433. if (p)
  1434. *p = ':';
  1435. }
  1436. return ret;
  1437. }
  1438. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1439. AVDictionary *codec_opts)
  1440. {
  1441. int i;
  1442. AVDictionary **opts;
  1443. if (!s->nb_streams)
  1444. return NULL;
  1445. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1446. if (!opts) {
  1447. av_log(NULL, AV_LOG_ERROR,
  1448. "Could not alloc memory for stream options.\n");
  1449. return NULL;
  1450. }
  1451. for (i = 0; i < s->nb_streams; i++)
  1452. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  1453. s, s->streams[i], NULL);
  1454. return opts;
  1455. }
  1456. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1457. {
  1458. if (new_size >= INT_MAX / elem_size) {
  1459. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1460. exit_program(1);
  1461. }
  1462. if (*size < new_size) {
  1463. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1464. if (!tmp) {
  1465. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1466. exit_program(1);
  1467. }
  1468. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1469. *size = new_size;
  1470. return tmp;
  1471. }
  1472. return array;
  1473. }
  1474. const char *media_type_string(enum AVMediaType media_type)
  1475. {
  1476. switch (media_type) {
  1477. case AVMEDIA_TYPE_VIDEO: return "video";
  1478. case AVMEDIA_TYPE_AUDIO: return "audio";
  1479. case AVMEDIA_TYPE_DATA: return "data";
  1480. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  1481. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  1482. default: return "unknown";
  1483. }
  1484. }