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.

1652 lines
53KB

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