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.

1829 lines
57KB

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