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.

1630 lines
52KB

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