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.

1703 lines
54KB

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