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.

1612 lines
52KB

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