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.

1726 lines
55KB

  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. /* Include only the enabled headers since some compilers (namely, Sun
  27. Studio) will not omit unused inline functions and create undefined
  28. references to libraries that are not being built. */
  29. #include "config.h"
  30. #include "libavformat/avformat.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavdevice/avdevice.h"
  33. #include "libavresample/avresample.h"
  34. #include "libswscale/swscale.h"
  35. #include "libavutil/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 "avversion.h"
  46. #include "cmdutils.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. { "trace" , AV_LOG_TRACE },
  623. };
  624. char *tail;
  625. int level;
  626. int i;
  627. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  628. if (!strcmp(log_levels[i].name, arg)) {
  629. av_log_set_level(log_levels[i].level);
  630. return 0;
  631. }
  632. }
  633. level = strtol(arg, &tail, 10);
  634. if (*tail) {
  635. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  636. "Possible levels are numbers or:\n", arg);
  637. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  638. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  639. exit_program(1);
  640. }
  641. av_log_set_level(level);
  642. return 0;
  643. }
  644. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  645. {
  646. #if HAVE_SETRLIMIT
  647. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  648. struct rlimit rl = { lim, lim + 1 };
  649. if (setrlimit(RLIMIT_CPU, &rl))
  650. perror("setrlimit");
  651. #else
  652. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  653. #endif
  654. return 0;
  655. }
  656. void print_error(const char *filename, int err)
  657. {
  658. char errbuf[128];
  659. const char *errbuf_ptr = errbuf;
  660. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  661. errbuf_ptr = strerror(AVUNERROR(err));
  662. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  663. }
  664. static int warned_cfg = 0;
  665. #define INDENT 1
  666. #define SHOW_VERSION 2
  667. #define SHOW_CONFIG 4
  668. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  669. if (CONFIG_##LIBNAME) { \
  670. const char *indent = flags & INDENT? " " : ""; \
  671. if (flags & SHOW_VERSION) { \
  672. unsigned int version = libname##_version(); \
  673. av_log(NULL, level, \
  674. "%slib%-10s %2d.%3d.%2d / %2d.%3d.%2d\n", \
  675. indent, #libname, \
  676. LIB##LIBNAME##_VERSION_MAJOR, \
  677. LIB##LIBNAME##_VERSION_MINOR, \
  678. LIB##LIBNAME##_VERSION_MICRO, \
  679. version >> 16, version >> 8 & 0xff, version & 0xff); \
  680. } \
  681. if (flags & SHOW_CONFIG) { \
  682. const char *cfg = libname##_configuration(); \
  683. if (strcmp(LIBAV_CONFIGURATION, cfg)) { \
  684. if (!warned_cfg) { \
  685. av_log(NULL, level, \
  686. "%sWARNING: library configuration mismatch\n", \
  687. indent); \
  688. warned_cfg = 1; \
  689. } \
  690. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  691. indent, #libname, cfg); \
  692. } \
  693. } \
  694. } \
  695. static void print_all_libs_info(int flags, int level)
  696. {
  697. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  698. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  699. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  700. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  701. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  702. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  703. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  704. }
  705. void show_banner(void)
  706. {
  707. av_log(NULL, AV_LOG_INFO,
  708. "%s version " LIBAV_VERSION ", Copyright (c) %d-%d the Libav developers\n",
  709. program_name, program_birth_year, this_year);
  710. av_log(NULL, AV_LOG_INFO, " built on %s %s with %s\n",
  711. __DATE__, __TIME__, CC_IDENT);
  712. av_log(NULL, AV_LOG_VERBOSE, " configuration: " LIBAV_CONFIGURATION "\n");
  713. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_VERBOSE);
  714. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_VERBOSE);
  715. }
  716. int show_version(void *optctx, const char *opt, const char *arg)
  717. {
  718. av_log_set_callback(log_callback_help);
  719. printf("%s " LIBAV_VERSION "\n", program_name);
  720. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  721. return 0;
  722. }
  723. int show_license(void *optctx, const char *opt, const char *arg)
  724. {
  725. printf(
  726. #if CONFIG_NONFREE
  727. "This version of %s has nonfree parts compiled in.\n"
  728. "Therefore it is not legally redistributable.\n",
  729. program_name
  730. #elif CONFIG_GPLV3
  731. "%s is free software; you can redistribute it and/or modify\n"
  732. "it under the terms of the GNU General Public License as published by\n"
  733. "the Free Software Foundation; either version 3 of the License, or\n"
  734. "(at your option) any later version.\n"
  735. "\n"
  736. "%s is distributed in the hope that it will be useful,\n"
  737. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  738. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  739. "GNU General Public License for more details.\n"
  740. "\n"
  741. "You should have received a copy of the GNU General Public License\n"
  742. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  743. program_name, program_name, program_name
  744. #elif CONFIG_GPL
  745. "%s is free software; you can redistribute it and/or modify\n"
  746. "it under the terms of the GNU General Public License as published by\n"
  747. "the Free Software Foundation; either version 2 of the License, or\n"
  748. "(at your option) any later version.\n"
  749. "\n"
  750. "%s is distributed in the hope that it will be useful,\n"
  751. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  752. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  753. "GNU General Public License for more details.\n"
  754. "\n"
  755. "You should have received a copy of the GNU General Public License\n"
  756. "along with %s; if not, write to the Free Software\n"
  757. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  758. program_name, program_name, program_name
  759. #elif CONFIG_LGPLV3
  760. "%s is free software; you can redistribute it and/or modify\n"
  761. "it under the terms of the GNU Lesser General Public License as published by\n"
  762. "the Free Software Foundation; either version 3 of the License, or\n"
  763. "(at your option) any later version.\n"
  764. "\n"
  765. "%s is distributed in the hope that it will be useful,\n"
  766. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  767. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  768. "GNU Lesser General Public License for more details.\n"
  769. "\n"
  770. "You should have received a copy of the GNU Lesser General Public License\n"
  771. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  772. program_name, program_name, program_name
  773. #else
  774. "%s is free software; you can redistribute it and/or\n"
  775. "modify it under the terms of the GNU Lesser General Public\n"
  776. "License as published by the Free Software Foundation; either\n"
  777. "version 2.1 of the License, or (at your option) any later version.\n"
  778. "\n"
  779. "%s is distributed in the hope that it will be useful,\n"
  780. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  781. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  782. "Lesser General Public License for more details.\n"
  783. "\n"
  784. "You should have received a copy of the GNU Lesser General Public\n"
  785. "License along with %s; if not, write to the Free Software\n"
  786. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  787. program_name, program_name, program_name
  788. #endif
  789. );
  790. return 0;
  791. }
  792. int show_formats(void *optctx, const char *opt, const char *arg)
  793. {
  794. AVInputFormat *ifmt = NULL;
  795. AVOutputFormat *ofmt = NULL;
  796. const char *last_name;
  797. printf("File formats:\n"
  798. " D. = Demuxing supported\n"
  799. " .E = Muxing supported\n"
  800. " --\n");
  801. last_name = "000";
  802. for (;;) {
  803. int decode = 0;
  804. int encode = 0;
  805. const char *name = NULL;
  806. const char *long_name = NULL;
  807. while ((ofmt = av_oformat_next(ofmt))) {
  808. if ((!name || strcmp(ofmt->name, name) < 0) &&
  809. strcmp(ofmt->name, last_name) > 0) {
  810. name = ofmt->name;
  811. long_name = ofmt->long_name;
  812. encode = 1;
  813. }
  814. }
  815. while ((ifmt = av_iformat_next(ifmt))) {
  816. if ((!name || strcmp(ifmt->name, name) < 0) &&
  817. strcmp(ifmt->name, last_name) > 0) {
  818. name = ifmt->name;
  819. long_name = ifmt->long_name;
  820. encode = 0;
  821. }
  822. if (name && strcmp(ifmt->name, name) == 0)
  823. decode = 1;
  824. }
  825. if (!name)
  826. break;
  827. last_name = name;
  828. printf(" %s%s %-15s %s\n",
  829. decode ? "D" : " ",
  830. encode ? "E" : " ",
  831. name,
  832. long_name ? long_name:" ");
  833. }
  834. return 0;
  835. }
  836. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  837. if (codec->field) { \
  838. const type *p = c->field; \
  839. \
  840. printf(" Supported " list_name ":"); \
  841. while (*p != term) { \
  842. get_name(*p); \
  843. printf(" %s", name); \
  844. p++; \
  845. } \
  846. printf("\n"); \
  847. } \
  848. static void print_codec(const AVCodec *c)
  849. {
  850. int encoder = av_codec_is_encoder(c);
  851. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  852. c->long_name ? c->long_name : "");
  853. if (c->type == AVMEDIA_TYPE_VIDEO) {
  854. printf(" Threading capabilities: ");
  855. switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
  856. CODEC_CAP_SLICE_THREADS)) {
  857. case CODEC_CAP_FRAME_THREADS |
  858. CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  859. case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  860. case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  861. default: printf("no"); break;
  862. }
  863. printf("\n");
  864. }
  865. if (c->supported_framerates) {
  866. const AVRational *fps = c->supported_framerates;
  867. printf(" Supported framerates:");
  868. while (fps->num) {
  869. printf(" %d/%d", fps->num, fps->den);
  870. fps++;
  871. }
  872. printf("\n");
  873. }
  874. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  875. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  876. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  877. GET_SAMPLE_RATE_NAME);
  878. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  879. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  880. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  881. 0, GET_CH_LAYOUT_DESC);
  882. if (c->priv_class) {
  883. show_help_children(c->priv_class,
  884. AV_OPT_FLAG_ENCODING_PARAM |
  885. AV_OPT_FLAG_DECODING_PARAM);
  886. }
  887. }
  888. static char get_media_type_char(enum AVMediaType type)
  889. {
  890. switch (type) {
  891. case AVMEDIA_TYPE_VIDEO: return 'V';
  892. case AVMEDIA_TYPE_AUDIO: return 'A';
  893. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  894. default: return '?';
  895. }
  896. }
  897. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  898. int encoder)
  899. {
  900. while ((prev = av_codec_next(prev))) {
  901. if (prev->id == id &&
  902. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  903. return prev;
  904. }
  905. return NULL;
  906. }
  907. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  908. {
  909. const AVCodec *codec = NULL;
  910. printf(" (%s: ", encoder ? "encoders" : "decoders");
  911. while ((codec = next_codec_for_id(id, codec, encoder)))
  912. printf("%s ", codec->name);
  913. printf(")");
  914. }
  915. int show_codecs(void *optctx, const char *opt, const char *arg)
  916. {
  917. const AVCodecDescriptor *desc = NULL;
  918. printf("Codecs:\n"
  919. " D..... = Decoding supported\n"
  920. " .E.... = Encoding supported\n"
  921. " ..V... = Video codec\n"
  922. " ..A... = Audio codec\n"
  923. " ..S... = Subtitle codec\n"
  924. " ...I.. = Intra frame-only codec\n"
  925. " ....L. = Lossy compression\n"
  926. " .....S = Lossless compression\n"
  927. " -------\n");
  928. while ((desc = avcodec_descriptor_next(desc))) {
  929. const AVCodec *codec = NULL;
  930. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  931. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  932. printf("%c", get_media_type_char(desc->type));
  933. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  934. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  935. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  936. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  937. /* print decoders/encoders when there's more than one or their
  938. * names are different from codec name */
  939. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  940. if (strcmp(codec->name, desc->name)) {
  941. print_codecs_for_id(desc->id, 0);
  942. break;
  943. }
  944. }
  945. codec = NULL;
  946. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  947. if (strcmp(codec->name, desc->name)) {
  948. print_codecs_for_id(desc->id, 1);
  949. break;
  950. }
  951. }
  952. printf("\n");
  953. }
  954. return 0;
  955. }
  956. static void print_codecs(int encoder)
  957. {
  958. const AVCodecDescriptor *desc = NULL;
  959. printf("%s:\n"
  960. " V... = Video\n"
  961. " A... = Audio\n"
  962. " S... = Subtitle\n"
  963. " .F.. = Frame-level multithreading\n"
  964. " ..S. = Slice-level multithreading\n"
  965. " ...X = Codec is experimental\n"
  966. " ---\n",
  967. encoder ? "Encoders" : "Decoders");
  968. while ((desc = avcodec_descriptor_next(desc))) {
  969. const AVCodec *codec = NULL;
  970. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  971. printf("%c", get_media_type_char(desc->type));
  972. printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  973. printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  974. printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  975. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  976. if (strcmp(codec->name, desc->name))
  977. printf(" (codec %s)", desc->name);
  978. printf("\n");
  979. }
  980. }
  981. }
  982. int show_decoders(void *optctx, const char *opt, const char *arg)
  983. {
  984. print_codecs(0);
  985. return 0;
  986. }
  987. int show_encoders(void *optctx, const char *opt, const char *arg)
  988. {
  989. print_codecs(1);
  990. return 0;
  991. }
  992. int show_bsfs(void *optctx, const char *opt, const char *arg)
  993. {
  994. AVBitStreamFilter *bsf = NULL;
  995. printf("Bitstream filters:\n");
  996. while ((bsf = av_bitstream_filter_next(bsf)))
  997. printf("%s\n", bsf->name);
  998. printf("\n");
  999. return 0;
  1000. }
  1001. int show_protocols(void *optctx, const char *opt, const char *arg)
  1002. {
  1003. void *opaque = NULL;
  1004. const char *name;
  1005. printf("Supported file protocols:\n"
  1006. "Input:\n");
  1007. while ((name = avio_enum_protocols(&opaque, 0)))
  1008. printf("%s\n", name);
  1009. printf("Output:\n");
  1010. while ((name = avio_enum_protocols(&opaque, 1)))
  1011. printf("%s\n", name);
  1012. return 0;
  1013. }
  1014. int show_filters(void *optctx, const char *opt, const char *arg)
  1015. {
  1016. #if CONFIG_AVFILTER
  1017. const AVFilter *filter = NULL;
  1018. printf("Filters:\n");
  1019. while ((filter = avfilter_next(filter)))
  1020. printf("%-16s %s\n", filter->name, filter->description);
  1021. #else
  1022. printf("No filters available: libavfilter disabled\n");
  1023. #endif
  1024. return 0;
  1025. }
  1026. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1027. {
  1028. const AVPixFmtDescriptor *pix_desc = NULL;
  1029. printf("Pixel formats:\n"
  1030. "I.... = Supported Input format for conversion\n"
  1031. ".O... = Supported Output format for conversion\n"
  1032. "..H.. = Hardware accelerated format\n"
  1033. "...P. = Paletted format\n"
  1034. "....B = Bitstream format\n"
  1035. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1036. "-----\n");
  1037. #if !CONFIG_SWSCALE
  1038. # define sws_isSupportedInput(x) 0
  1039. # define sws_isSupportedOutput(x) 0
  1040. #endif
  1041. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1042. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1043. printf("%c%c%c%c%c %-16s %d %2d\n",
  1044. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1045. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1046. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1047. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1048. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1049. pix_desc->name,
  1050. pix_desc->nb_components,
  1051. av_get_bits_per_pixel(pix_desc));
  1052. }
  1053. return 0;
  1054. }
  1055. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1056. {
  1057. int i;
  1058. char fmt_str[128];
  1059. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1060. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1061. return 0;
  1062. }
  1063. static void show_help_codec(const char *name, int encoder)
  1064. {
  1065. const AVCodecDescriptor *desc;
  1066. const AVCodec *codec;
  1067. if (!name) {
  1068. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1069. return;
  1070. }
  1071. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1072. avcodec_find_decoder_by_name(name);
  1073. if (codec)
  1074. print_codec(codec);
  1075. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1076. int printed = 0;
  1077. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1078. printed = 1;
  1079. print_codec(codec);
  1080. }
  1081. if (!printed) {
  1082. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to Libav, "
  1083. "but no %s for it are available. Libav might need to be "
  1084. "recompiled with additional external libraries.\n",
  1085. name, encoder ? "encoders" : "decoders");
  1086. }
  1087. } else {
  1088. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by Libav.\n",
  1089. name);
  1090. }
  1091. }
  1092. static void show_help_demuxer(const char *name)
  1093. {
  1094. const AVInputFormat *fmt = av_find_input_format(name);
  1095. if (!fmt) {
  1096. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1097. return;
  1098. }
  1099. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1100. if (fmt->extensions)
  1101. printf(" Common extensions: %s.\n", fmt->extensions);
  1102. if (fmt->priv_class)
  1103. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1104. }
  1105. static void show_help_muxer(const char *name)
  1106. {
  1107. const AVCodecDescriptor *desc;
  1108. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1109. if (!fmt) {
  1110. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1111. return;
  1112. }
  1113. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1114. if (fmt->extensions)
  1115. printf(" Common extensions: %s.\n", fmt->extensions);
  1116. if (fmt->mime_type)
  1117. printf(" Mime type: %s.\n", fmt->mime_type);
  1118. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1119. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1120. printf(" Default video codec: %s.\n", desc->name);
  1121. }
  1122. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1123. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1124. printf(" Default audio codec: %s.\n", desc->name);
  1125. }
  1126. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1127. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1128. printf(" Default subtitle codec: %s.\n", desc->name);
  1129. }
  1130. if (fmt->priv_class)
  1131. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1132. }
  1133. #if CONFIG_AVFILTER
  1134. static void show_help_filter(const char *name)
  1135. {
  1136. const AVFilter *f = avfilter_get_by_name(name);
  1137. int i, count;
  1138. if (!name) {
  1139. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1140. return;
  1141. } else if (!f) {
  1142. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1143. return;
  1144. }
  1145. printf("Filter %s [%s]:\n", f->name, f->description);
  1146. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1147. printf(" slice threading supported\n");
  1148. printf(" Inputs:\n");
  1149. count = avfilter_pad_count(f->inputs);
  1150. for (i = 0; i < count; i++) {
  1151. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1152. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1153. }
  1154. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1155. printf(" dynamic (depending on the options)\n");
  1156. printf(" Outputs:\n");
  1157. count = avfilter_pad_count(f->outputs);
  1158. for (i = 0; i < count; i++) {
  1159. printf(" %d %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1160. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1161. }
  1162. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1163. printf(" dynamic (depending on the options)\n");
  1164. if (f->priv_class)
  1165. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM |
  1166. AV_OPT_FLAG_AUDIO_PARAM);
  1167. }
  1168. #endif
  1169. int show_help(void *optctx, const char *opt, const char *arg)
  1170. {
  1171. char *topic, *par;
  1172. av_log_set_callback(log_callback_help);
  1173. topic = av_strdup(arg ? arg : "");
  1174. if (!topic)
  1175. return AVERROR(ENOMEM);
  1176. par = strchr(topic, '=');
  1177. if (par)
  1178. *par++ = 0;
  1179. if (!*topic) {
  1180. show_help_default(topic, par);
  1181. } else if (!strcmp(topic, "decoder")) {
  1182. show_help_codec(par, 0);
  1183. } else if (!strcmp(topic, "encoder")) {
  1184. show_help_codec(par, 1);
  1185. } else if (!strcmp(topic, "demuxer")) {
  1186. show_help_demuxer(par);
  1187. } else if (!strcmp(topic, "muxer")) {
  1188. show_help_muxer(par);
  1189. #if CONFIG_AVFILTER
  1190. } else if (!strcmp(topic, "filter")) {
  1191. show_help_filter(par);
  1192. #endif
  1193. } else {
  1194. show_help_default(topic, par);
  1195. }
  1196. av_freep(&topic);
  1197. return 0;
  1198. }
  1199. int read_yesno(void)
  1200. {
  1201. int c = getchar();
  1202. int yesno = (av_toupper(c) == 'Y');
  1203. while (c != '\n' && c != EOF)
  1204. c = getchar();
  1205. return yesno;
  1206. }
  1207. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1208. {
  1209. int ret;
  1210. FILE *f = fopen(filename, "rb");
  1211. if (!f) {
  1212. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1213. strerror(errno));
  1214. return AVERROR(errno);
  1215. }
  1216. ret = fseek(f, 0, SEEK_END);
  1217. if (ret == -1) {
  1218. ret = AVERROR(errno);
  1219. goto out;
  1220. }
  1221. ret = ftell(f);
  1222. if (ret < 0) {
  1223. ret = AVERROR(errno);
  1224. goto out;
  1225. }
  1226. *size = ret;
  1227. ret = fseek(f, 0, SEEK_SET);
  1228. if (ret == -1) {
  1229. ret = AVERROR(errno);
  1230. goto out;
  1231. }
  1232. *bufptr = av_malloc(*size + 1);
  1233. if (!*bufptr) {
  1234. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1235. ret = AVERROR(ENOMEM);
  1236. goto out;
  1237. }
  1238. ret = fread(*bufptr, 1, *size, f);
  1239. if (ret < *size) {
  1240. av_free(*bufptr);
  1241. if (ferror(f)) {
  1242. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1243. filename, strerror(errno));
  1244. ret = AVERROR(errno);
  1245. } else
  1246. ret = AVERROR_EOF;
  1247. } else {
  1248. ret = 0;
  1249. (*bufptr)[(*size)++] = '\0';
  1250. }
  1251. out:
  1252. fclose(f);
  1253. return ret;
  1254. }
  1255. void init_pts_correction(PtsCorrectionContext *ctx)
  1256. {
  1257. ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
  1258. ctx->last_pts = ctx->last_dts = INT64_MIN;
  1259. }
  1260. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts,
  1261. int64_t dts)
  1262. {
  1263. int64_t pts = AV_NOPTS_VALUE;
  1264. if (dts != AV_NOPTS_VALUE) {
  1265. ctx->num_faulty_dts += dts <= ctx->last_dts;
  1266. ctx->last_dts = dts;
  1267. }
  1268. if (reordered_pts != AV_NOPTS_VALUE) {
  1269. ctx->num_faulty_pts += reordered_pts <= ctx->last_pts;
  1270. ctx->last_pts = reordered_pts;
  1271. }
  1272. if ((ctx->num_faulty_pts<=ctx->num_faulty_dts || dts == AV_NOPTS_VALUE)
  1273. && reordered_pts != AV_NOPTS_VALUE)
  1274. pts = reordered_pts;
  1275. else
  1276. pts = dts;
  1277. return pts;
  1278. }
  1279. FILE *get_preset_file(char *filename, size_t filename_size,
  1280. const char *preset_name, int is_path,
  1281. const char *codec_name)
  1282. {
  1283. FILE *f = NULL;
  1284. int i;
  1285. const char *base[3] = { getenv("AVCONV_DATADIR"),
  1286. getenv("HOME"),
  1287. AVCONV_DATADIR, };
  1288. if (is_path) {
  1289. av_strlcpy(filename, preset_name, filename_size);
  1290. f = fopen(filename, "r");
  1291. } else {
  1292. for (i = 0; i < 3 && !f; i++) {
  1293. if (!base[i])
  1294. continue;
  1295. snprintf(filename, filename_size, "%s%s/%s.avpreset", base[i],
  1296. i != 1 ? "" : "/.avconv", preset_name);
  1297. f = fopen(filename, "r");
  1298. if (!f && codec_name) {
  1299. snprintf(filename, filename_size,
  1300. "%s%s/%s-%s.avpreset",
  1301. base[i], i != 1 ? "" : "/.avconv", codec_name,
  1302. preset_name);
  1303. f = fopen(filename, "r");
  1304. }
  1305. }
  1306. }
  1307. return f;
  1308. }
  1309. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1310. {
  1311. if (*spec <= '9' && *spec >= '0') /* opt:index */
  1312. return strtol(spec, NULL, 0) == st->index;
  1313. else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
  1314. *spec == 't') { /* opt:[vasdt] */
  1315. enum AVMediaType type;
  1316. switch (*spec++) {
  1317. case 'v': type = AVMEDIA_TYPE_VIDEO; break;
  1318. case 'a': type = AVMEDIA_TYPE_AUDIO; break;
  1319. case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
  1320. case 'd': type = AVMEDIA_TYPE_DATA; break;
  1321. case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
  1322. default: av_assert0(0);
  1323. }
  1324. if (type != st->codec->codec_type)
  1325. return 0;
  1326. if (*spec++ == ':') { /* possibly followed by :index */
  1327. int i, index = strtol(spec, NULL, 0);
  1328. for (i = 0; i < s->nb_streams; i++)
  1329. if (s->streams[i]->codec->codec_type == type && index-- == 0)
  1330. return i == st->index;
  1331. return 0;
  1332. }
  1333. return 1;
  1334. } else if (*spec == 'p' && *(spec + 1) == ':') {
  1335. int prog_id, i, j;
  1336. char *endptr;
  1337. spec += 2;
  1338. prog_id = strtol(spec, &endptr, 0);
  1339. for (i = 0; i < s->nb_programs; i++) {
  1340. if (s->programs[i]->id != prog_id)
  1341. continue;
  1342. if (*endptr++ == ':') {
  1343. int stream_idx = strtol(endptr, NULL, 0);
  1344. return stream_idx >= 0 &&
  1345. stream_idx < s->programs[i]->nb_stream_indexes &&
  1346. st->index == s->programs[i]->stream_index[stream_idx];
  1347. }
  1348. for (j = 0; j < s->programs[i]->nb_stream_indexes; j++)
  1349. if (st->index == s->programs[i]->stream_index[j])
  1350. return 1;
  1351. }
  1352. return 0;
  1353. } else if (*spec == 'i' && *(spec + 1) == ':') {
  1354. int stream_id;
  1355. char *endptr;
  1356. spec += 2;
  1357. stream_id = strtol(spec, &endptr, 0);
  1358. return stream_id == st->id;
  1359. } else if (*spec == 'm' && *(spec + 1) == ':') {
  1360. AVDictionaryEntry *tag;
  1361. char *key, *val;
  1362. int ret;
  1363. spec += 2;
  1364. val = strchr(spec, ':');
  1365. key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
  1366. if (!key)
  1367. return AVERROR(ENOMEM);
  1368. tag = av_dict_get(st->metadata, key, NULL, 0);
  1369. if (tag) {
  1370. if (!val || !strcmp(tag->value, val + 1))
  1371. ret = 1;
  1372. else
  1373. ret = 0;
  1374. } else
  1375. ret = 0;
  1376. av_freep(&key);
  1377. return ret;
  1378. } else if (*spec == 'u') {
  1379. AVCodecContext *avctx = st->codec;
  1380. int val;
  1381. switch (avctx->codec_type) {
  1382. case AVMEDIA_TYPE_AUDIO:
  1383. val = avctx->sample_rate && avctx->channels;
  1384. if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
  1385. return 0;
  1386. break;
  1387. case AVMEDIA_TYPE_VIDEO:
  1388. val = avctx->width && avctx->height;
  1389. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  1390. return 0;
  1391. break;
  1392. case AVMEDIA_TYPE_UNKNOWN:
  1393. val = 0;
  1394. break;
  1395. default:
  1396. val = 1;
  1397. break;
  1398. }
  1399. return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
  1400. } else if (!*spec) /* empty specifier, matches everything */
  1401. return 1;
  1402. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1403. return AVERROR(EINVAL);
  1404. }
  1405. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1406. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1407. {
  1408. AVDictionary *ret = NULL;
  1409. AVDictionaryEntry *t = NULL;
  1410. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1411. : AV_OPT_FLAG_DECODING_PARAM;
  1412. char prefix = 0;
  1413. const AVClass *cc = avcodec_get_class();
  1414. if (!codec)
  1415. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1416. : avcodec_find_decoder(codec_id);
  1417. switch (st->codec->codec_type) {
  1418. case AVMEDIA_TYPE_VIDEO:
  1419. prefix = 'v';
  1420. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1421. break;
  1422. case AVMEDIA_TYPE_AUDIO:
  1423. prefix = 'a';
  1424. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1425. break;
  1426. case AVMEDIA_TYPE_SUBTITLE:
  1427. prefix = 's';
  1428. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1429. break;
  1430. }
  1431. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1432. char *p = strchr(t->key, ':');
  1433. /* check stream specification in opt name */
  1434. if (p)
  1435. switch (check_stream_specifier(s, st, p + 1)) {
  1436. case 1: *p = 0; break;
  1437. case 0: continue;
  1438. default: return NULL;
  1439. }
  1440. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1441. (codec && codec->priv_class &&
  1442. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1443. AV_OPT_SEARCH_FAKE_OBJ)))
  1444. av_dict_set(&ret, t->key, t->value, 0);
  1445. else if (t->key[0] == prefix &&
  1446. av_opt_find(&cc, t->key + 1, NULL, flags,
  1447. AV_OPT_SEARCH_FAKE_OBJ))
  1448. av_dict_set(&ret, t->key + 1, t->value, 0);
  1449. if (p)
  1450. *p = ':';
  1451. }
  1452. return ret;
  1453. }
  1454. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1455. AVDictionary *codec_opts)
  1456. {
  1457. int i;
  1458. AVDictionary **opts;
  1459. if (!s->nb_streams)
  1460. return NULL;
  1461. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1462. if (!opts) {
  1463. av_log(NULL, AV_LOG_ERROR,
  1464. "Could not alloc memory for stream options.\n");
  1465. return NULL;
  1466. }
  1467. for (i = 0; i < s->nb_streams; i++)
  1468. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1469. s, s->streams[i], NULL);
  1470. return opts;
  1471. }
  1472. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1473. {
  1474. if (new_size >= INT_MAX / elem_size) {
  1475. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1476. exit_program(1);
  1477. }
  1478. if (*size < new_size) {
  1479. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1480. if (!tmp) {
  1481. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1482. exit_program(1);
  1483. }
  1484. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1485. *size = new_size;
  1486. return tmp;
  1487. }
  1488. return array;
  1489. }
  1490. const char *media_type_string(enum AVMediaType media_type)
  1491. {
  1492. switch (media_type) {
  1493. case AVMEDIA_TYPE_VIDEO: return "video";
  1494. case AVMEDIA_TYPE_AUDIO: return "audio";
  1495. case AVMEDIA_TYPE_DATA: return "data";
  1496. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  1497. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  1498. default: return "unknown";
  1499. }
  1500. }