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.

1546 lines
50KB

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