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.

1645 lines
53KB

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