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.

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