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.

2377 lines
75KB

  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 <stdint.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. /* Include only the enabled headers since some compilers (namely, Sun
  27. Studio) will not omit unused inline functions and create undefined
  28. references to libraries that are not being built. */
  29. #include "config.h"
  30. #include "compat/va_copy.h"
  31. #include "libavformat/avformat.h"
  32. #include "libavfilter/avfilter.h"
  33. #include "libavdevice/avdevice.h"
  34. #include "libavresample/avresample.h"
  35. #include "libswscale/swscale.h"
  36. #include "libswresample/swresample.h"
  37. #include "libpostproc/postprocess.h"
  38. #include "libavutil/attributes.h"
  39. #include "libavutil/avassert.h"
  40. #include "libavutil/avstring.h"
  41. #include "libavutil/bprint.h"
  42. #include "libavutil/display.h"
  43. #include "libavutil/mathematics.h"
  44. #include "libavutil/imgutils.h"
  45. #include "libavutil/libm.h"
  46. #include "libavutil/parseutils.h"
  47. #include "libavutil/pixdesc.h"
  48. #include "libavutil/eval.h"
  49. #include "libavutil/dict.h"
  50. #include "libavutil/opt.h"
  51. #include "libavutil/cpu.h"
  52. #include "libavutil/ffversion.h"
  53. #include "libavutil/version.h"
  54. #include "cmdutils.h"
  55. #if HAVE_SYS_RESOURCE_H
  56. #include <sys/time.h>
  57. #include <sys/resource.h>
  58. #endif
  59. #ifdef _WIN32
  60. #include <windows.h>
  61. #endif
  62. static int init_report(const char *env);
  63. AVDictionary *sws_dict;
  64. AVDictionary *swr_opts;
  65. AVDictionary *format_opts, *codec_opts, *resample_opts;
  66. static FILE *report_file;
  67. static int report_file_level = AV_LOG_DEBUG;
  68. int hide_banner = 0;
  69. enum show_muxdemuxers {
  70. SHOW_DEFAULT,
  71. SHOW_DEMUXERS,
  72. SHOW_MUXERS,
  73. };
  74. void init_opts(void)
  75. {
  76. av_dict_set(&sws_dict, "flags", "bicubic", 0);
  77. }
  78. void uninit_opts(void)
  79. {
  80. av_dict_free(&swr_opts);
  81. av_dict_free(&sws_dict);
  82. av_dict_free(&format_opts);
  83. av_dict_free(&codec_opts);
  84. av_dict_free(&resample_opts);
  85. }
  86. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  87. {
  88. vfprintf(stdout, fmt, vl);
  89. }
  90. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  91. {
  92. va_list vl2;
  93. char line[1024];
  94. static int print_prefix = 1;
  95. va_copy(vl2, vl);
  96. av_log_default_callback(ptr, level, fmt, vl);
  97. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  98. va_end(vl2);
  99. if (report_file_level >= level) {
  100. fputs(line, report_file);
  101. fflush(report_file);
  102. }
  103. }
  104. void init_dynload(void)
  105. {
  106. #if HAVE_SETDLLDIRECTORY && defined(_WIN32)
  107. /* Calling SetDllDirectory with the empty string (but not NULL) removes the
  108. * current working directory from the DLL search path as a security pre-caution. */
  109. SetDllDirectory("");
  110. #endif
  111. }
  112. static void (*program_exit)(int ret);
  113. void register_exit(void (*cb)(int ret))
  114. {
  115. program_exit = cb;
  116. }
  117. void exit_program(int ret)
  118. {
  119. if (program_exit)
  120. program_exit(ret);
  121. exit(ret);
  122. }
  123. double parse_number_or_die(const char *context, const char *numstr, int type,
  124. double min, double max)
  125. {
  126. char *tail;
  127. const char *error;
  128. double d = av_strtod(numstr, &tail);
  129. if (*tail)
  130. error = "Expected number for %s but found: %s\n";
  131. else if (d < min || d > max)
  132. error = "The value for %s was %s which is not within %f - %f\n";
  133. else if (type == OPT_INT64 && (int64_t)d != d)
  134. error = "Expected int64 for %s but found %s\n";
  135. else if (type == OPT_INT && (int)d != d)
  136. error = "Expected int for %s but found %s\n";
  137. else
  138. return d;
  139. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  140. exit_program(1);
  141. return 0;
  142. }
  143. int64_t parse_time_or_die(const char *context, const char *timestr,
  144. int is_duration)
  145. {
  146. int64_t us;
  147. if (av_parse_time(&us, timestr, is_duration) < 0) {
  148. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  149. is_duration ? "duration" : "date", context, timestr);
  150. exit_program(1);
  151. }
  152. return us;
  153. }
  154. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  155. int rej_flags, int alt_flags)
  156. {
  157. const OptionDef *po;
  158. int first;
  159. first = 1;
  160. for (po = options; po->name; po++) {
  161. char buf[128];
  162. if (((po->flags & req_flags) != req_flags) ||
  163. (alt_flags && !(po->flags & alt_flags)) ||
  164. (po->flags & rej_flags))
  165. continue;
  166. if (first) {
  167. printf("%s\n", msg);
  168. first = 0;
  169. }
  170. av_strlcpy(buf, po->name, sizeof(buf));
  171. if (po->argname) {
  172. av_strlcat(buf, " ", sizeof(buf));
  173. av_strlcat(buf, po->argname, sizeof(buf));
  174. }
  175. printf("-%-17s %s\n", buf, po->help);
  176. }
  177. printf("\n");
  178. }
  179. void show_help_children(const AVClass *class, int flags)
  180. {
  181. void *iter = NULL;
  182. const AVClass *child;
  183. if (class->option) {
  184. av_opt_show2(&class, NULL, flags, 0);
  185. printf("\n");
  186. }
  187. while (child = av_opt_child_class_iterate(class, &iter))
  188. show_help_children(child, flags);
  189. }
  190. static const OptionDef *find_option(const OptionDef *po, const char *name)
  191. {
  192. while (po->name) {
  193. const char *end;
  194. if (av_strstart(name, po->name, &end) && (!*end || *end == ':'))
  195. break;
  196. po++;
  197. }
  198. return po;
  199. }
  200. /* _WIN32 means using the windows libc - cygwin doesn't define that
  201. * by default. HAVE_COMMANDLINETOARGVW is true on cygwin, while
  202. * it doesn't provide the actual command line via GetCommandLineW(). */
  203. #if HAVE_COMMANDLINETOARGVW && defined(_WIN32)
  204. #include <shellapi.h>
  205. /* Will be leaked on exit */
  206. static char** win32_argv_utf8 = NULL;
  207. static int win32_argc = 0;
  208. /**
  209. * Prepare command line arguments for executable.
  210. * For Windows - perform wide-char to UTF-8 conversion.
  211. * Input arguments should be main() function arguments.
  212. * @param argc_ptr Arguments number (including executable)
  213. * @param argv_ptr Arguments list.
  214. */
  215. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  216. {
  217. char *argstr_flat;
  218. wchar_t **argv_w;
  219. int i, buffsize = 0, offset = 0;
  220. if (win32_argv_utf8) {
  221. *argc_ptr = win32_argc;
  222. *argv_ptr = win32_argv_utf8;
  223. return;
  224. }
  225. win32_argc = 0;
  226. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  227. if (win32_argc <= 0 || !argv_w)
  228. return;
  229. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  230. for (i = 0; i < win32_argc; i++)
  231. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  232. NULL, 0, NULL, NULL);
  233. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  234. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  235. if (!win32_argv_utf8) {
  236. LocalFree(argv_w);
  237. return;
  238. }
  239. for (i = 0; i < win32_argc; i++) {
  240. win32_argv_utf8[i] = &argstr_flat[offset];
  241. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  242. &argstr_flat[offset],
  243. buffsize - offset, NULL, NULL);
  244. }
  245. win32_argv_utf8[i] = NULL;
  246. LocalFree(argv_w);
  247. *argc_ptr = win32_argc;
  248. *argv_ptr = win32_argv_utf8;
  249. }
  250. #else
  251. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  252. {
  253. /* nothing to do */
  254. }
  255. #endif /* HAVE_COMMANDLINETOARGVW */
  256. static int write_option(void *optctx, const OptionDef *po, const char *opt,
  257. const char *arg)
  258. {
  259. /* new-style options contain an offset into optctx, old-style address of
  260. * a global var*/
  261. void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
  262. (uint8_t *)optctx + po->u.off : po->u.dst_ptr;
  263. int *dstcount;
  264. if (po->flags & OPT_SPEC) {
  265. SpecifierOpt **so = dst;
  266. char *p = strchr(opt, ':');
  267. char *str;
  268. dstcount = (int *)(so + 1);
  269. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  270. str = av_strdup(p ? p + 1 : "");
  271. if (!str)
  272. return AVERROR(ENOMEM);
  273. (*so)[*dstcount - 1].specifier = str;
  274. dst = &(*so)[*dstcount - 1].u;
  275. }
  276. if (po->flags & OPT_STRING) {
  277. char *str;
  278. str = av_strdup(arg);
  279. av_freep(dst);
  280. if (!str)
  281. return AVERROR(ENOMEM);
  282. *(char **)dst = str;
  283. } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
  284. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  285. } else if (po->flags & OPT_INT64) {
  286. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  287. } else if (po->flags & OPT_TIME) {
  288. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  289. } else if (po->flags & OPT_FLOAT) {
  290. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  291. } else if (po->flags & OPT_DOUBLE) {
  292. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  293. } else if (po->u.func_arg) {
  294. int ret = po->u.func_arg(optctx, opt, arg);
  295. if (ret < 0) {
  296. av_log(NULL, AV_LOG_ERROR,
  297. "Failed to set value '%s' for option '%s': %s\n",
  298. arg, opt, av_err2str(ret));
  299. return ret;
  300. }
  301. }
  302. if (po->flags & OPT_EXIT)
  303. exit_program(0);
  304. return 0;
  305. }
  306. int parse_option(void *optctx, const char *opt, const char *arg,
  307. const OptionDef *options)
  308. {
  309. const OptionDef *po;
  310. int ret;
  311. po = find_option(options, opt);
  312. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  313. /* handle 'no' bool option */
  314. po = find_option(options, opt + 2);
  315. if ((po->name && (po->flags & OPT_BOOL)))
  316. arg = "0";
  317. } else if (po->flags & OPT_BOOL)
  318. arg = "1";
  319. if (!po->name)
  320. po = find_option(options, "default");
  321. if (!po->name) {
  322. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  323. return AVERROR(EINVAL);
  324. }
  325. if (po->flags & HAS_ARG && !arg) {
  326. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  327. return AVERROR(EINVAL);
  328. }
  329. ret = write_option(optctx, po, opt, arg);
  330. if (ret < 0)
  331. return ret;
  332. return !!(po->flags & HAS_ARG);
  333. }
  334. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  335. void (*parse_arg_function)(void *, const char*))
  336. {
  337. const char *opt;
  338. int optindex, handleoptions = 1, ret;
  339. /* perform system-dependent conversions for arguments list */
  340. prepare_app_arguments(&argc, &argv);
  341. /* parse options */
  342. optindex = 1;
  343. while (optindex < argc) {
  344. opt = argv[optindex++];
  345. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  346. if (opt[1] == '-' && opt[2] == '\0') {
  347. handleoptions = 0;
  348. continue;
  349. }
  350. opt++;
  351. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  352. exit_program(1);
  353. optindex += ret;
  354. } else {
  355. if (parse_arg_function)
  356. parse_arg_function(optctx, opt);
  357. }
  358. }
  359. }
  360. int parse_optgroup(void *optctx, OptionGroup *g)
  361. {
  362. int i, ret;
  363. av_log(NULL, AV_LOG_DEBUG, "Parsing a group of options: %s %s.\n",
  364. g->group_def->name, g->arg);
  365. for (i = 0; i < g->nb_opts; i++) {
  366. Option *o = &g->opts[i];
  367. if (g->group_def->flags &&
  368. !(g->group_def->flags & o->opt->flags)) {
  369. av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
  370. "%s %s -- you are trying to apply an input option to an "
  371. "output file or vice versa. Move this option before the "
  372. "file it belongs to.\n", o->key, o->opt->help,
  373. g->group_def->name, g->arg);
  374. return AVERROR(EINVAL);
  375. }
  376. av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
  377. o->key, o->opt->help, o->val);
  378. ret = write_option(optctx, o->opt, o->key, o->val);
  379. if (ret < 0)
  380. return ret;
  381. }
  382. av_log(NULL, AV_LOG_DEBUG, "Successfully parsed a group of options.\n");
  383. return 0;
  384. }
  385. int locate_option(int argc, char **argv, const OptionDef *options,
  386. const char *optname)
  387. {
  388. const OptionDef *po;
  389. int i;
  390. for (i = 1; i < argc; i++) {
  391. const char *cur_opt = argv[i];
  392. if (*cur_opt++ != '-')
  393. continue;
  394. po = find_option(options, cur_opt);
  395. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  396. po = find_option(options, cur_opt + 2);
  397. if ((!po->name && !strcmp(cur_opt, optname)) ||
  398. (po->name && !strcmp(optname, po->name)))
  399. return i;
  400. if (!po->name || po->flags & HAS_ARG)
  401. i++;
  402. }
  403. return 0;
  404. }
  405. static void dump_argument(const char *a)
  406. {
  407. const unsigned char *p;
  408. for (p = a; *p; p++)
  409. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  410. *p == '_' || (*p >= 'a' && *p <= 'z')))
  411. break;
  412. if (!*p) {
  413. fputs(a, report_file);
  414. return;
  415. }
  416. fputc('"', report_file);
  417. for (p = a; *p; p++) {
  418. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  419. fprintf(report_file, "\\%c", *p);
  420. else if (*p < ' ' || *p > '~')
  421. fprintf(report_file, "\\x%02x", *p);
  422. else
  423. fputc(*p, report_file);
  424. }
  425. fputc('"', report_file);
  426. }
  427. static void check_options(const OptionDef *po)
  428. {
  429. while (po->name) {
  430. if (po->flags & OPT_PERFILE)
  431. av_assert0(po->flags & (OPT_INPUT | OPT_OUTPUT));
  432. po++;
  433. }
  434. }
  435. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  436. {
  437. int idx = locate_option(argc, argv, options, "loglevel");
  438. const char *env;
  439. check_options(options);
  440. if (!idx)
  441. idx = locate_option(argc, argv, options, "v");
  442. if (idx && argv[idx + 1])
  443. opt_loglevel(NULL, "loglevel", argv[idx + 1]);
  444. idx = locate_option(argc, argv, options, "report");
  445. if ((env = getenv("FFREPORT")) || idx) {
  446. init_report(env);
  447. if (report_file) {
  448. int i;
  449. fprintf(report_file, "Command line:\n");
  450. for (i = 0; i < argc; i++) {
  451. dump_argument(argv[i]);
  452. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  453. }
  454. fflush(report_file);
  455. }
  456. }
  457. idx = locate_option(argc, argv, options, "hide_banner");
  458. if (idx)
  459. hide_banner = 1;
  460. }
  461. static const AVOption *opt_find(void *obj, const char *name, const char *unit,
  462. int opt_flags, int search_flags)
  463. {
  464. const AVOption *o = av_opt_find(obj, name, unit, opt_flags, search_flags);
  465. if(o && !o->flags)
  466. return NULL;
  467. return o;
  468. }
  469. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS && (arg[0]=='-' || arg[0]=='+')) ? AV_DICT_APPEND : 0
  470. int opt_default(void *optctx, const char *opt, const char *arg)
  471. {
  472. const AVOption *o;
  473. int consumed = 0;
  474. char opt_stripped[128];
  475. const char *p;
  476. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class();
  477. #if CONFIG_AVRESAMPLE
  478. const AVClass *rc = avresample_get_class();
  479. #endif
  480. #if CONFIG_SWSCALE
  481. const AVClass *sc = sws_get_class();
  482. #endif
  483. #if CONFIG_SWRESAMPLE
  484. const AVClass *swr_class = swr_get_class();
  485. #endif
  486. if (!strcmp(opt, "debug") || !strcmp(opt, "fdebug"))
  487. av_log_set_level(AV_LOG_DEBUG);
  488. if (!(p = strchr(opt, ':')))
  489. p = opt + strlen(opt);
  490. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  491. if ((o = opt_find(&cc, opt_stripped, NULL, 0,
  492. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  493. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  494. (o = opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)))) {
  495. av_dict_set(&codec_opts, opt, arg, FLAGS);
  496. consumed = 1;
  497. }
  498. if ((o = opt_find(&fc, opt, NULL, 0,
  499. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  500. av_dict_set(&format_opts, opt, arg, FLAGS);
  501. if (consumed)
  502. av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
  503. consumed = 1;
  504. }
  505. #if CONFIG_SWSCALE
  506. if (!consumed && (o = opt_find(&sc, opt, NULL, 0,
  507. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  508. struct SwsContext *sws = sws_alloc_context();
  509. int ret = av_opt_set(sws, opt, arg, 0);
  510. sws_freeContext(sws);
  511. if (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  512. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  513. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  514. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  515. return AVERROR(EINVAL);
  516. }
  517. if (ret < 0) {
  518. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  519. return ret;
  520. }
  521. av_dict_set(&sws_dict, opt, arg, FLAGS);
  522. consumed = 1;
  523. }
  524. #else
  525. if (!consumed && !strcmp(opt, "sws_flags")) {
  526. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  527. consumed = 1;
  528. }
  529. #endif
  530. #if CONFIG_SWRESAMPLE
  531. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  532. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  533. struct SwrContext *swr = swr_alloc();
  534. int ret = av_opt_set(swr, opt, arg, 0);
  535. swr_free(&swr);
  536. if (ret < 0) {
  537. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  538. return ret;
  539. }
  540. av_dict_set(&swr_opts, opt, arg, FLAGS);
  541. consumed = 1;
  542. }
  543. #endif
  544. #if CONFIG_AVRESAMPLE
  545. if ((o=opt_find(&rc, opt, NULL, 0,
  546. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  547. av_dict_set(&resample_opts, opt, arg, FLAGS);
  548. consumed = 1;
  549. }
  550. #endif
  551. if (consumed)
  552. return 0;
  553. return AVERROR_OPTION_NOT_FOUND;
  554. }
  555. /*
  556. * Check whether given option is a group separator.
  557. *
  558. * @return index of the group definition that matched or -1 if none
  559. */
  560. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  561. const char *opt)
  562. {
  563. int i;
  564. for (i = 0; i < nb_groups; i++) {
  565. const OptionGroupDef *p = &groups[i];
  566. if (p->sep && !strcmp(p->sep, opt))
  567. return i;
  568. }
  569. return -1;
  570. }
  571. /*
  572. * Finish parsing an option group.
  573. *
  574. * @param group_idx which group definition should this group belong to
  575. * @param arg argument of the group delimiting option
  576. */
  577. static void finish_group(OptionParseContext *octx, int group_idx,
  578. const char *arg)
  579. {
  580. OptionGroupList *l = &octx->groups[group_idx];
  581. OptionGroup *g;
  582. GROW_ARRAY(l->groups, l->nb_groups);
  583. g = &l->groups[l->nb_groups - 1];
  584. *g = octx->cur_group;
  585. g->arg = arg;
  586. g->group_def = l->group_def;
  587. g->sws_dict = sws_dict;
  588. g->swr_opts = swr_opts;
  589. g->codec_opts = codec_opts;
  590. g->format_opts = format_opts;
  591. g->resample_opts = resample_opts;
  592. codec_opts = NULL;
  593. format_opts = NULL;
  594. resample_opts = NULL;
  595. sws_dict = NULL;
  596. swr_opts = NULL;
  597. init_opts();
  598. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  599. }
  600. /*
  601. * Add an option instance to currently parsed group.
  602. */
  603. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  604. const char *key, const char *val)
  605. {
  606. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  607. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  608. GROW_ARRAY(g->opts, g->nb_opts);
  609. g->opts[g->nb_opts - 1].opt = opt;
  610. g->opts[g->nb_opts - 1].key = key;
  611. g->opts[g->nb_opts - 1].val = val;
  612. }
  613. static void init_parse_context(OptionParseContext *octx,
  614. const OptionGroupDef *groups, int nb_groups)
  615. {
  616. static const OptionGroupDef global_group = { "global" };
  617. int i;
  618. memset(octx, 0, sizeof(*octx));
  619. octx->nb_groups = nb_groups;
  620. octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
  621. if (!octx->groups)
  622. exit_program(1);
  623. for (i = 0; i < octx->nb_groups; i++)
  624. octx->groups[i].group_def = &groups[i];
  625. octx->global_opts.group_def = &global_group;
  626. octx->global_opts.arg = "";
  627. init_opts();
  628. }
  629. void uninit_parse_context(OptionParseContext *octx)
  630. {
  631. int i, j;
  632. for (i = 0; i < octx->nb_groups; i++) {
  633. OptionGroupList *l = &octx->groups[i];
  634. for (j = 0; j < l->nb_groups; j++) {
  635. av_freep(&l->groups[j].opts);
  636. av_dict_free(&l->groups[j].codec_opts);
  637. av_dict_free(&l->groups[j].format_opts);
  638. av_dict_free(&l->groups[j].resample_opts);
  639. av_dict_free(&l->groups[j].sws_dict);
  640. av_dict_free(&l->groups[j].swr_opts);
  641. }
  642. av_freep(&l->groups);
  643. }
  644. av_freep(&octx->groups);
  645. av_freep(&octx->cur_group.opts);
  646. av_freep(&octx->global_opts.opts);
  647. uninit_opts();
  648. }
  649. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  650. const OptionDef *options,
  651. const OptionGroupDef *groups, int nb_groups)
  652. {
  653. int optindex = 1;
  654. int dashdash = -2;
  655. /* perform system-dependent conversions for arguments list */
  656. prepare_app_arguments(&argc, &argv);
  657. init_parse_context(octx, groups, nb_groups);
  658. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  659. while (optindex < argc) {
  660. const char *opt = argv[optindex++], *arg;
  661. const OptionDef *po;
  662. int ret;
  663. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  664. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  665. dashdash = optindex;
  666. continue;
  667. }
  668. /* unnamed group separators, e.g. output filename */
  669. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  670. finish_group(octx, 0, opt);
  671. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  672. continue;
  673. }
  674. opt++;
  675. #define GET_ARG(arg) \
  676. do { \
  677. arg = argv[optindex++]; \
  678. if (!arg) { \
  679. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  680. return AVERROR(EINVAL); \
  681. } \
  682. } while (0)
  683. /* named group separators, e.g. -i */
  684. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  685. GET_ARG(arg);
  686. finish_group(octx, ret, arg);
  687. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  688. groups[ret].name, arg);
  689. continue;
  690. }
  691. /* normal options */
  692. po = find_option(options, opt);
  693. if (po->name) {
  694. if (po->flags & OPT_EXIT) {
  695. /* optional argument, e.g. -h */
  696. arg = argv[optindex++];
  697. } else if (po->flags & HAS_ARG) {
  698. GET_ARG(arg);
  699. } else {
  700. arg = "1";
  701. }
  702. add_opt(octx, po, opt, arg);
  703. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  704. "argument '%s'.\n", po->name, po->help, arg);
  705. continue;
  706. }
  707. /* AVOptions */
  708. if (argv[optindex]) {
  709. ret = opt_default(NULL, opt, argv[optindex]);
  710. if (ret >= 0) {
  711. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  712. "argument '%s'.\n", opt, argv[optindex]);
  713. optindex++;
  714. continue;
  715. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  716. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  717. "with argument '%s'.\n", opt, argv[optindex]);
  718. return ret;
  719. }
  720. }
  721. /* boolean -nofoo options */
  722. if (opt[0] == 'n' && opt[1] == 'o' &&
  723. (po = find_option(options, opt + 2)) &&
  724. po->name && po->flags & OPT_BOOL) {
  725. add_opt(octx, po, opt, "0");
  726. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  727. "argument 0.\n", po->name, po->help);
  728. continue;
  729. }
  730. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  731. return AVERROR_OPTION_NOT_FOUND;
  732. }
  733. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  734. av_log(NULL, AV_LOG_WARNING, "Trailing option(s) found in the "
  735. "command: may be ignored.\n");
  736. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  737. return 0;
  738. }
  739. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  740. {
  741. int ret;
  742. unsigned flags = av_get_cpu_flags();
  743. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  744. return ret;
  745. av_force_cpu_flags(flags);
  746. return 0;
  747. }
  748. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  749. {
  750. const struct { const char *name; int level; } log_levels[] = {
  751. { "quiet" , AV_LOG_QUIET },
  752. { "panic" , AV_LOG_PANIC },
  753. { "fatal" , AV_LOG_FATAL },
  754. { "error" , AV_LOG_ERROR },
  755. { "warning", AV_LOG_WARNING },
  756. { "info" , AV_LOG_INFO },
  757. { "verbose", AV_LOG_VERBOSE },
  758. { "debug" , AV_LOG_DEBUG },
  759. { "trace" , AV_LOG_TRACE },
  760. };
  761. const char *token;
  762. char *tail;
  763. int flags = av_log_get_flags();
  764. int level = av_log_get_level();
  765. int cmd, i = 0;
  766. av_assert0(arg);
  767. while (*arg) {
  768. token = arg;
  769. if (*token == '+' || *token == '-') {
  770. cmd = *token++;
  771. } else {
  772. cmd = 0;
  773. }
  774. if (!i && !cmd) {
  775. flags = 0; /* missing relative prefix, build absolute value */
  776. }
  777. if (!strncmp(token, "repeat", 6)) {
  778. if (cmd == '-') {
  779. flags |= AV_LOG_SKIP_REPEATED;
  780. } else {
  781. flags &= ~AV_LOG_SKIP_REPEATED;
  782. }
  783. arg = token + 6;
  784. } else if (!strncmp(token, "level", 5)) {
  785. if (cmd == '-') {
  786. flags &= ~AV_LOG_PRINT_LEVEL;
  787. } else {
  788. flags |= AV_LOG_PRINT_LEVEL;
  789. }
  790. arg = token + 5;
  791. } else {
  792. break;
  793. }
  794. i++;
  795. }
  796. if (!*arg) {
  797. goto end;
  798. } else if (*arg == '+') {
  799. arg++;
  800. } else if (!i) {
  801. flags = av_log_get_flags(); /* level value without prefix, reset flags */
  802. }
  803. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  804. if (!strcmp(log_levels[i].name, arg)) {
  805. level = log_levels[i].level;
  806. goto end;
  807. }
  808. }
  809. level = strtol(arg, &tail, 10);
  810. if (*tail) {
  811. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  812. "Possible levels are numbers or:\n", arg);
  813. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  814. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  815. exit_program(1);
  816. }
  817. end:
  818. av_log_set_flags(flags);
  819. av_log_set_level(level);
  820. return 0;
  821. }
  822. static void expand_filename_template(AVBPrint *bp, const char *template,
  823. struct tm *tm)
  824. {
  825. int c;
  826. while ((c = *(template++))) {
  827. if (c == '%') {
  828. if (!(c = *(template++)))
  829. break;
  830. switch (c) {
  831. case 'p':
  832. av_bprintf(bp, "%s", program_name);
  833. break;
  834. case 't':
  835. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  836. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  837. tm->tm_hour, tm->tm_min, tm->tm_sec);
  838. break;
  839. case '%':
  840. av_bprint_chars(bp, c, 1);
  841. break;
  842. }
  843. } else {
  844. av_bprint_chars(bp, c, 1);
  845. }
  846. }
  847. }
  848. static int init_report(const char *env)
  849. {
  850. char *filename_template = NULL;
  851. char *key, *val;
  852. int ret, count = 0;
  853. int prog_loglevel, envlevel = 0;
  854. time_t now;
  855. struct tm *tm;
  856. AVBPrint filename;
  857. if (report_file) /* already opened */
  858. return 0;
  859. time(&now);
  860. tm = localtime(&now);
  861. while (env && *env) {
  862. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  863. if (count)
  864. av_log(NULL, AV_LOG_ERROR,
  865. "Failed to parse FFREPORT environment variable: %s\n",
  866. av_err2str(ret));
  867. break;
  868. }
  869. if (*env)
  870. env++;
  871. count++;
  872. if (!strcmp(key, "file")) {
  873. av_free(filename_template);
  874. filename_template = val;
  875. val = NULL;
  876. } else if (!strcmp(key, "level")) {
  877. char *tail;
  878. report_file_level = strtol(val, &tail, 10);
  879. if (*tail) {
  880. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  881. exit_program(1);
  882. }
  883. envlevel = 1;
  884. } else {
  885. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  886. }
  887. av_free(val);
  888. av_free(key);
  889. }
  890. av_bprint_init(&filename, 0, AV_BPRINT_SIZE_AUTOMATIC);
  891. expand_filename_template(&filename,
  892. av_x_if_null(filename_template, "%p-%t.log"), tm);
  893. av_free(filename_template);
  894. if (!av_bprint_is_complete(&filename)) {
  895. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  896. return AVERROR(ENOMEM);
  897. }
  898. prog_loglevel = av_log_get_level();
  899. if (!envlevel)
  900. report_file_level = FFMAX(report_file_level, prog_loglevel);
  901. report_file = fopen(filename.str, "w");
  902. if (!report_file) {
  903. int ret = AVERROR(errno);
  904. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  905. filename.str, strerror(errno));
  906. return ret;
  907. }
  908. av_log_set_callback(log_callback_report);
  909. av_log(NULL, AV_LOG_INFO,
  910. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  911. "Report written to \"%s\"\n"
  912. "Log level: %d\n",
  913. program_name,
  914. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  915. tm->tm_hour, tm->tm_min, tm->tm_sec,
  916. filename.str, report_file_level);
  917. av_bprint_finalize(&filename, NULL);
  918. return 0;
  919. }
  920. int opt_report(void *optctx, const char *opt, const char *arg)
  921. {
  922. return init_report(NULL);
  923. }
  924. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  925. {
  926. char *tail;
  927. size_t max;
  928. max = strtol(arg, &tail, 10);
  929. if (*tail) {
  930. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  931. exit_program(1);
  932. }
  933. av_max_alloc(max);
  934. return 0;
  935. }
  936. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  937. {
  938. #if HAVE_SETRLIMIT
  939. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  940. struct rlimit rl = { lim, lim + 1 };
  941. if (setrlimit(RLIMIT_CPU, &rl))
  942. perror("setrlimit");
  943. #else
  944. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  945. #endif
  946. return 0;
  947. }
  948. void print_error(const char *filename, int err)
  949. {
  950. char errbuf[128];
  951. const char *errbuf_ptr = errbuf;
  952. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  953. errbuf_ptr = strerror(AVUNERROR(err));
  954. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  955. }
  956. static int warned_cfg = 0;
  957. #define INDENT 1
  958. #define SHOW_VERSION 2
  959. #define SHOW_CONFIG 4
  960. #define SHOW_COPYRIGHT 8
  961. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  962. if (CONFIG_##LIBNAME) { \
  963. const char *indent = flags & INDENT? " " : ""; \
  964. if (flags & SHOW_VERSION) { \
  965. unsigned int version = libname##_version(); \
  966. av_log(NULL, level, \
  967. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  968. indent, #libname, \
  969. LIB##LIBNAME##_VERSION_MAJOR, \
  970. LIB##LIBNAME##_VERSION_MINOR, \
  971. LIB##LIBNAME##_VERSION_MICRO, \
  972. AV_VERSION_MAJOR(version), AV_VERSION_MINOR(version),\
  973. AV_VERSION_MICRO(version)); \
  974. } \
  975. if (flags & SHOW_CONFIG) { \
  976. const char *cfg = libname##_configuration(); \
  977. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  978. if (!warned_cfg) { \
  979. av_log(NULL, level, \
  980. "%sWARNING: library configuration mismatch\n", \
  981. indent); \
  982. warned_cfg = 1; \
  983. } \
  984. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  985. indent, #libname, cfg); \
  986. } \
  987. } \
  988. } \
  989. static void print_all_libs_info(int flags, int level)
  990. {
  991. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  992. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  993. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  994. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  995. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  996. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  997. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  998. PRINT_LIB_INFO(swresample, SWRESAMPLE, flags, level);
  999. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  1000. }
  1001. static void print_program_info(int flags, int level)
  1002. {
  1003. const char *indent = flags & INDENT? " " : "";
  1004. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  1005. if (flags & SHOW_COPYRIGHT)
  1006. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  1007. program_birth_year, CONFIG_THIS_YEAR);
  1008. av_log(NULL, level, "\n");
  1009. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  1010. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  1011. }
  1012. static void print_buildconf(int flags, int level)
  1013. {
  1014. const char *indent = flags & INDENT ? " " : "";
  1015. char str[] = { FFMPEG_CONFIGURATION };
  1016. char *conflist, *remove_tilde, *splitconf;
  1017. // Change all the ' --' strings to '~--' so that
  1018. // they can be identified as tokens.
  1019. while ((conflist = strstr(str, " --")) != NULL) {
  1020. conflist[0] = '~';
  1021. }
  1022. // Compensate for the weirdness this would cause
  1023. // when passing 'pkg-config --static'.
  1024. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  1025. remove_tilde[sizeof("pkg-config~") - 2] = ' ';
  1026. }
  1027. splitconf = strtok(str, "~");
  1028. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  1029. while (splitconf != NULL) {
  1030. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  1031. splitconf = strtok(NULL, "~");
  1032. }
  1033. }
  1034. void show_banner(int argc, char **argv, const OptionDef *options)
  1035. {
  1036. int idx = locate_option(argc, argv, options, "version");
  1037. if (hide_banner || idx)
  1038. return;
  1039. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  1040. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  1041. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  1042. }
  1043. int show_version(void *optctx, const char *opt, const char *arg)
  1044. {
  1045. av_log_set_callback(log_callback_help);
  1046. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  1047. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  1048. return 0;
  1049. }
  1050. int show_buildconf(void *optctx, const char *opt, const char *arg)
  1051. {
  1052. av_log_set_callback(log_callback_help);
  1053. print_buildconf (INDENT|0, AV_LOG_INFO);
  1054. return 0;
  1055. }
  1056. int show_license(void *optctx, const char *opt, const char *arg)
  1057. {
  1058. #if CONFIG_NONFREE
  1059. printf(
  1060. "This version of %s has nonfree parts compiled in.\n"
  1061. "Therefore it is not legally redistributable.\n",
  1062. program_name );
  1063. #elif CONFIG_GPLV3
  1064. printf(
  1065. "%s is free software; you can redistribute it and/or modify\n"
  1066. "it under the terms of the GNU General Public License as published by\n"
  1067. "the Free Software Foundation; either version 3 of the License, or\n"
  1068. "(at your option) any later version.\n"
  1069. "\n"
  1070. "%s is distributed in the hope that it will be useful,\n"
  1071. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1072. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1073. "GNU General Public License for more details.\n"
  1074. "\n"
  1075. "You should have received a copy of the GNU General Public License\n"
  1076. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1077. program_name, program_name, program_name );
  1078. #elif CONFIG_GPL
  1079. printf(
  1080. "%s is free software; you can redistribute it and/or modify\n"
  1081. "it under the terms of the GNU General Public License as published by\n"
  1082. "the Free Software Foundation; either version 2 of the License, or\n"
  1083. "(at your option) any later version.\n"
  1084. "\n"
  1085. "%s is distributed in the hope that it will be useful,\n"
  1086. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1087. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1088. "GNU General Public License for more details.\n"
  1089. "\n"
  1090. "You should have received a copy of the GNU General Public License\n"
  1091. "along with %s; if not, write to the Free Software\n"
  1092. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1093. program_name, program_name, program_name );
  1094. #elif CONFIG_LGPLV3
  1095. printf(
  1096. "%s is free software; you can redistribute it and/or modify\n"
  1097. "it under the terms of the GNU Lesser General Public License as published by\n"
  1098. "the Free Software Foundation; either version 3 of the License, or\n"
  1099. "(at your option) any later version.\n"
  1100. "\n"
  1101. "%s is distributed in the hope that it will be useful,\n"
  1102. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1103. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1104. "GNU Lesser General Public License for more details.\n"
  1105. "\n"
  1106. "You should have received a copy of the GNU Lesser General Public License\n"
  1107. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1108. program_name, program_name, program_name );
  1109. #else
  1110. printf(
  1111. "%s is free software; you can redistribute it and/or\n"
  1112. "modify it under the terms of the GNU Lesser General Public\n"
  1113. "License as published by the Free Software Foundation; either\n"
  1114. "version 2.1 of the License, or (at your option) any later version.\n"
  1115. "\n"
  1116. "%s is distributed in the hope that it will be useful,\n"
  1117. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1118. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1119. "Lesser General Public License for more details.\n"
  1120. "\n"
  1121. "You should have received a copy of the GNU Lesser General Public\n"
  1122. "License along with %s; if not, write to the Free Software\n"
  1123. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1124. program_name, program_name, program_name );
  1125. #endif
  1126. return 0;
  1127. }
  1128. static int is_device(const AVClass *avclass)
  1129. {
  1130. if (!avclass)
  1131. return 0;
  1132. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  1133. }
  1134. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only, int muxdemuxers)
  1135. {
  1136. void *ifmt_opaque = NULL;
  1137. const AVInputFormat *ifmt = NULL;
  1138. void *ofmt_opaque = NULL;
  1139. const AVOutputFormat *ofmt = NULL;
  1140. const char *last_name;
  1141. int is_dev;
  1142. printf("%s\n"
  1143. " D. = Demuxing supported\n"
  1144. " .E = Muxing supported\n"
  1145. " --\n", device_only ? "Devices:" : "File formats:");
  1146. last_name = "000";
  1147. for (;;) {
  1148. int decode = 0;
  1149. int encode = 0;
  1150. const char *name = NULL;
  1151. const char *long_name = NULL;
  1152. if (muxdemuxers !=SHOW_DEMUXERS) {
  1153. ofmt_opaque = NULL;
  1154. while ((ofmt = av_muxer_iterate(&ofmt_opaque))) {
  1155. is_dev = is_device(ofmt->priv_class);
  1156. if (!is_dev && device_only)
  1157. continue;
  1158. if ((!name || strcmp(ofmt->name, name) < 0) &&
  1159. strcmp(ofmt->name, last_name) > 0) {
  1160. name = ofmt->name;
  1161. long_name = ofmt->long_name;
  1162. encode = 1;
  1163. }
  1164. }
  1165. }
  1166. if (muxdemuxers != SHOW_MUXERS) {
  1167. ifmt_opaque = NULL;
  1168. while ((ifmt = av_demuxer_iterate(&ifmt_opaque))) {
  1169. is_dev = is_device(ifmt->priv_class);
  1170. if (!is_dev && device_only)
  1171. continue;
  1172. if ((!name || strcmp(ifmt->name, name) < 0) &&
  1173. strcmp(ifmt->name, last_name) > 0) {
  1174. name = ifmt->name;
  1175. long_name = ifmt->long_name;
  1176. encode = 0;
  1177. }
  1178. if (name && strcmp(ifmt->name, name) == 0)
  1179. decode = 1;
  1180. }
  1181. }
  1182. if (!name)
  1183. break;
  1184. last_name = name;
  1185. printf(" %s%s %-15s %s\n",
  1186. decode ? "D" : " ",
  1187. encode ? "E" : " ",
  1188. name,
  1189. long_name ? long_name:" ");
  1190. }
  1191. return 0;
  1192. }
  1193. int show_formats(void *optctx, const char *opt, const char *arg)
  1194. {
  1195. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEFAULT);
  1196. }
  1197. int show_muxers(void *optctx, const char *opt, const char *arg)
  1198. {
  1199. return show_formats_devices(optctx, opt, arg, 0, SHOW_MUXERS);
  1200. }
  1201. int show_demuxers(void *optctx, const char *opt, const char *arg)
  1202. {
  1203. return show_formats_devices(optctx, opt, arg, 0, SHOW_DEMUXERS);
  1204. }
  1205. int show_devices(void *optctx, const char *opt, const char *arg)
  1206. {
  1207. return show_formats_devices(optctx, opt, arg, 1, SHOW_DEFAULT);
  1208. }
  1209. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  1210. if (codec->field) { \
  1211. const type *p = codec->field; \
  1212. \
  1213. printf(" Supported " list_name ":"); \
  1214. while (*p != term) { \
  1215. get_name(*p); \
  1216. printf(" %s", name); \
  1217. p++; \
  1218. } \
  1219. printf("\n"); \
  1220. } \
  1221. static void print_codec(const AVCodec *c)
  1222. {
  1223. int encoder = av_codec_is_encoder(c);
  1224. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  1225. c->long_name ? c->long_name : "");
  1226. printf(" General capabilities: ");
  1227. if (c->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)
  1228. printf("horizband ");
  1229. if (c->capabilities & AV_CODEC_CAP_DR1)
  1230. printf("dr1 ");
  1231. if (c->capabilities & AV_CODEC_CAP_TRUNCATED)
  1232. printf("trunc ");
  1233. if (c->capabilities & AV_CODEC_CAP_DELAY)
  1234. printf("delay ");
  1235. if (c->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)
  1236. printf("small ");
  1237. if (c->capabilities & AV_CODEC_CAP_SUBFRAMES)
  1238. printf("subframes ");
  1239. if (c->capabilities & AV_CODEC_CAP_EXPERIMENTAL)
  1240. printf("exp ");
  1241. if (c->capabilities & AV_CODEC_CAP_CHANNEL_CONF)
  1242. printf("chconf ");
  1243. if (c->capabilities & AV_CODEC_CAP_PARAM_CHANGE)
  1244. printf("paramchange ");
  1245. if (c->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  1246. printf("variable ");
  1247. if (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1248. AV_CODEC_CAP_SLICE_THREADS |
  1249. AV_CODEC_CAP_OTHER_THREADS))
  1250. printf("threads ");
  1251. if (c->capabilities & AV_CODEC_CAP_AVOID_PROBING)
  1252. printf("avoidprobe ");
  1253. if (c->capabilities & AV_CODEC_CAP_HARDWARE)
  1254. printf("hardware ");
  1255. if (c->capabilities & AV_CODEC_CAP_HYBRID)
  1256. printf("hybrid ");
  1257. if (!c->capabilities)
  1258. printf("none");
  1259. printf("\n");
  1260. if (c->type == AVMEDIA_TYPE_VIDEO ||
  1261. c->type == AVMEDIA_TYPE_AUDIO) {
  1262. printf(" Threading capabilities: ");
  1263. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1264. AV_CODEC_CAP_SLICE_THREADS |
  1265. AV_CODEC_CAP_OTHER_THREADS)) {
  1266. case AV_CODEC_CAP_FRAME_THREADS |
  1267. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  1268. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  1269. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  1270. case AV_CODEC_CAP_OTHER_THREADS: printf("other"); break;
  1271. default: printf("none"); break;
  1272. }
  1273. printf("\n");
  1274. }
  1275. if (avcodec_get_hw_config(c, 0)) {
  1276. printf(" Supported hardware devices: ");
  1277. for (int i = 0;; i++) {
  1278. const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
  1279. if (!config)
  1280. break;
  1281. printf("%s ", av_hwdevice_get_type_name(config->device_type));
  1282. }
  1283. printf("\n");
  1284. }
  1285. if (c->supported_framerates) {
  1286. const AVRational *fps = c->supported_framerates;
  1287. printf(" Supported framerates:");
  1288. while (fps->num) {
  1289. printf(" %d/%d", fps->num, fps->den);
  1290. fps++;
  1291. }
  1292. printf("\n");
  1293. }
  1294. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  1295. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  1296. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  1297. GET_SAMPLE_RATE_NAME);
  1298. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  1299. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  1300. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  1301. 0, GET_CH_LAYOUT_DESC);
  1302. if (c->priv_class) {
  1303. show_help_children(c->priv_class,
  1304. AV_OPT_FLAG_ENCODING_PARAM |
  1305. AV_OPT_FLAG_DECODING_PARAM);
  1306. }
  1307. }
  1308. static char get_media_type_char(enum AVMediaType type)
  1309. {
  1310. switch (type) {
  1311. case AVMEDIA_TYPE_VIDEO: return 'V';
  1312. case AVMEDIA_TYPE_AUDIO: return 'A';
  1313. case AVMEDIA_TYPE_DATA: return 'D';
  1314. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  1315. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  1316. default: return '?';
  1317. }
  1318. }
  1319. static const AVCodec *next_codec_for_id(enum AVCodecID id, void **iter,
  1320. int encoder)
  1321. {
  1322. const AVCodec *c;
  1323. while ((c = av_codec_iterate(iter))) {
  1324. if (c->id == id &&
  1325. (encoder ? av_codec_is_encoder(c) : av_codec_is_decoder(c)))
  1326. return c;
  1327. }
  1328. return NULL;
  1329. }
  1330. static int compare_codec_desc(const void *a, const void *b)
  1331. {
  1332. const AVCodecDescriptor * const *da = a;
  1333. const AVCodecDescriptor * const *db = b;
  1334. return (*da)->type != (*db)->type ? FFDIFFSIGN((*da)->type, (*db)->type) :
  1335. strcmp((*da)->name, (*db)->name);
  1336. }
  1337. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  1338. {
  1339. const AVCodecDescriptor *desc = NULL;
  1340. const AVCodecDescriptor **codecs;
  1341. unsigned nb_codecs = 0, i = 0;
  1342. while ((desc = avcodec_descriptor_next(desc)))
  1343. nb_codecs++;
  1344. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  1345. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  1346. exit_program(1);
  1347. }
  1348. desc = NULL;
  1349. while ((desc = avcodec_descriptor_next(desc)))
  1350. codecs[i++] = desc;
  1351. av_assert0(i == nb_codecs);
  1352. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  1353. *rcodecs = codecs;
  1354. return nb_codecs;
  1355. }
  1356. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  1357. {
  1358. void *iter = NULL;
  1359. const AVCodec *codec;
  1360. printf(" (%s: ", encoder ? "encoders" : "decoders");
  1361. while ((codec = next_codec_for_id(id, &iter, encoder)))
  1362. printf("%s ", codec->name);
  1363. printf(")");
  1364. }
  1365. int show_codecs(void *optctx, const char *opt, const char *arg)
  1366. {
  1367. const AVCodecDescriptor **codecs;
  1368. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1369. printf("Codecs:\n"
  1370. " D..... = Decoding supported\n"
  1371. " .E.... = Encoding supported\n"
  1372. " ..V... = Video codec\n"
  1373. " ..A... = Audio codec\n"
  1374. " ..S... = Subtitle codec\n"
  1375. " ...I.. = Intra frame-only codec\n"
  1376. " ....L. = Lossy compression\n"
  1377. " .....S = Lossless compression\n"
  1378. " -------\n");
  1379. for (i = 0; i < nb_codecs; i++) {
  1380. const AVCodecDescriptor *desc = codecs[i];
  1381. const AVCodec *codec;
  1382. void *iter = NULL;
  1383. if (strstr(desc->name, "_deprecated"))
  1384. continue;
  1385. printf(" ");
  1386. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  1387. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  1388. printf("%c", get_media_type_char(desc->type));
  1389. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  1390. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  1391. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  1392. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  1393. /* print decoders/encoders when there's more than one or their
  1394. * names are different from codec name */
  1395. while ((codec = next_codec_for_id(desc->id, &iter, 0))) {
  1396. if (strcmp(codec->name, desc->name)) {
  1397. print_codecs_for_id(desc->id, 0);
  1398. break;
  1399. }
  1400. }
  1401. iter = NULL;
  1402. while ((codec = next_codec_for_id(desc->id, &iter, 1))) {
  1403. if (strcmp(codec->name, desc->name)) {
  1404. print_codecs_for_id(desc->id, 1);
  1405. break;
  1406. }
  1407. }
  1408. printf("\n");
  1409. }
  1410. av_free(codecs);
  1411. return 0;
  1412. }
  1413. static void print_codecs(int encoder)
  1414. {
  1415. const AVCodecDescriptor **codecs;
  1416. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1417. printf("%s:\n"
  1418. " V..... = Video\n"
  1419. " A..... = Audio\n"
  1420. " S..... = Subtitle\n"
  1421. " .F.... = Frame-level multithreading\n"
  1422. " ..S... = Slice-level multithreading\n"
  1423. " ...X.. = Codec is experimental\n"
  1424. " ....B. = Supports draw_horiz_band\n"
  1425. " .....D = Supports direct rendering method 1\n"
  1426. " ------\n",
  1427. encoder ? "Encoders" : "Decoders");
  1428. for (i = 0; i < nb_codecs; i++) {
  1429. const AVCodecDescriptor *desc = codecs[i];
  1430. const AVCodec *codec;
  1431. void *iter = NULL;
  1432. while ((codec = next_codec_for_id(desc->id, &iter, encoder))) {
  1433. printf(" %c", get_media_type_char(desc->type));
  1434. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1435. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1436. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1437. printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  1438. printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
  1439. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1440. if (strcmp(codec->name, desc->name))
  1441. printf(" (codec %s)", desc->name);
  1442. printf("\n");
  1443. }
  1444. }
  1445. av_free(codecs);
  1446. }
  1447. int show_decoders(void *optctx, const char *opt, const char *arg)
  1448. {
  1449. print_codecs(0);
  1450. return 0;
  1451. }
  1452. int show_encoders(void *optctx, const char *opt, const char *arg)
  1453. {
  1454. print_codecs(1);
  1455. return 0;
  1456. }
  1457. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1458. {
  1459. const AVBitStreamFilter *bsf = NULL;
  1460. void *opaque = NULL;
  1461. printf("Bitstream filters:\n");
  1462. while ((bsf = av_bsf_iterate(&opaque)))
  1463. printf("%s\n", bsf->name);
  1464. printf("\n");
  1465. return 0;
  1466. }
  1467. int show_protocols(void *optctx, const char *opt, const char *arg)
  1468. {
  1469. void *opaque = NULL;
  1470. const char *name;
  1471. printf("Supported file protocols:\n"
  1472. "Input:\n");
  1473. while ((name = avio_enum_protocols(&opaque, 0)))
  1474. printf(" %s\n", name);
  1475. printf("Output:\n");
  1476. while ((name = avio_enum_protocols(&opaque, 1)))
  1477. printf(" %s\n", name);
  1478. return 0;
  1479. }
  1480. int show_filters(void *optctx, const char *opt, const char *arg)
  1481. {
  1482. #if CONFIG_AVFILTER
  1483. const AVFilter *filter = NULL;
  1484. char descr[64], *descr_cur;
  1485. void *opaque = NULL;
  1486. int i, j;
  1487. const AVFilterPad *pad;
  1488. printf("Filters:\n"
  1489. " T.. = Timeline support\n"
  1490. " .S. = Slice threading\n"
  1491. " ..C = Command support\n"
  1492. " A = Audio input/output\n"
  1493. " V = Video input/output\n"
  1494. " N = Dynamic number and/or type of input/output\n"
  1495. " | = Source or sink filter\n");
  1496. while ((filter = av_filter_iterate(&opaque))) {
  1497. descr_cur = descr;
  1498. for (i = 0; i < 2; i++) {
  1499. if (i) {
  1500. *(descr_cur++) = '-';
  1501. *(descr_cur++) = '>';
  1502. }
  1503. pad = i ? filter->outputs : filter->inputs;
  1504. for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
  1505. if (descr_cur >= descr + sizeof(descr) - 4)
  1506. break;
  1507. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  1508. }
  1509. if (!j)
  1510. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  1511. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  1512. }
  1513. *descr_cur = 0;
  1514. printf(" %c%c%c %-17s %-10s %s\n",
  1515. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  1516. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  1517. filter->process_command ? 'C' : '.',
  1518. filter->name, descr, filter->description);
  1519. }
  1520. #else
  1521. printf("No filters available: libavfilter disabled\n");
  1522. #endif
  1523. return 0;
  1524. }
  1525. int show_colors(void *optctx, const char *opt, const char *arg)
  1526. {
  1527. const char *name;
  1528. const uint8_t *rgb;
  1529. int i;
  1530. printf("%-32s #RRGGBB\n", "name");
  1531. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  1532. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  1533. return 0;
  1534. }
  1535. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1536. {
  1537. const AVPixFmtDescriptor *pix_desc = NULL;
  1538. printf("Pixel formats:\n"
  1539. "I.... = Supported Input format for conversion\n"
  1540. ".O... = Supported Output format for conversion\n"
  1541. "..H.. = Hardware accelerated format\n"
  1542. "...P. = Paletted format\n"
  1543. "....B = Bitstream format\n"
  1544. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1545. "-----\n");
  1546. #if !CONFIG_SWSCALE
  1547. # define sws_isSupportedInput(x) 0
  1548. # define sws_isSupportedOutput(x) 0
  1549. #endif
  1550. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1551. enum AVPixelFormat av_unused pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1552. printf("%c%c%c%c%c %-16s %d %2d\n",
  1553. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1554. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1555. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1556. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1557. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1558. pix_desc->name,
  1559. pix_desc->nb_components,
  1560. av_get_bits_per_pixel(pix_desc));
  1561. }
  1562. return 0;
  1563. }
  1564. int show_layouts(void *optctx, const char *opt, const char *arg)
  1565. {
  1566. int i = 0;
  1567. uint64_t layout, j;
  1568. const char *name, *descr;
  1569. printf("Individual channels:\n"
  1570. "NAME DESCRIPTION\n");
  1571. for (i = 0; i < 63; i++) {
  1572. name = av_get_channel_name((uint64_t)1 << i);
  1573. if (!name)
  1574. continue;
  1575. descr = av_get_channel_description((uint64_t)1 << i);
  1576. printf("%-14s %s\n", name, descr);
  1577. }
  1578. printf("\nStandard channel layouts:\n"
  1579. "NAME DECOMPOSITION\n");
  1580. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1581. if (name) {
  1582. printf("%-14s ", name);
  1583. for (j = 1; j; j <<= 1)
  1584. if ((layout & j))
  1585. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1586. printf("\n");
  1587. }
  1588. }
  1589. return 0;
  1590. }
  1591. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1592. {
  1593. int i;
  1594. char fmt_str[128];
  1595. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1596. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1597. return 0;
  1598. }
  1599. static void show_help_codec(const char *name, int encoder)
  1600. {
  1601. const AVCodecDescriptor *desc;
  1602. const AVCodec *codec;
  1603. if (!name) {
  1604. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1605. return;
  1606. }
  1607. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1608. avcodec_find_decoder_by_name(name);
  1609. if (codec)
  1610. print_codec(codec);
  1611. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1612. void *iter = NULL;
  1613. int printed = 0;
  1614. while ((codec = next_codec_for_id(desc->id, &iter, encoder))) {
  1615. printed = 1;
  1616. print_codec(codec);
  1617. }
  1618. if (!printed) {
  1619. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1620. "but no %s for it are available. FFmpeg might need to be "
  1621. "recompiled with additional external libraries.\n",
  1622. name, encoder ? "encoders" : "decoders");
  1623. }
  1624. } else {
  1625. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1626. name);
  1627. }
  1628. }
  1629. static void show_help_demuxer(const char *name)
  1630. {
  1631. const AVInputFormat *fmt = av_find_input_format(name);
  1632. if (!fmt) {
  1633. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1634. return;
  1635. }
  1636. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1637. if (fmt->extensions)
  1638. printf(" Common extensions: %s.\n", fmt->extensions);
  1639. if (fmt->priv_class)
  1640. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1641. }
  1642. static void show_help_protocol(const char *name)
  1643. {
  1644. const AVClass *proto_class;
  1645. if (!name) {
  1646. av_log(NULL, AV_LOG_ERROR, "No protocol name specified.\n");
  1647. return;
  1648. }
  1649. proto_class = avio_protocol_get_class(name);
  1650. if (!proto_class) {
  1651. av_log(NULL, AV_LOG_ERROR, "Unknown protocol '%s'.\n", name);
  1652. return;
  1653. }
  1654. show_help_children(proto_class, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
  1655. }
  1656. static void show_help_muxer(const char *name)
  1657. {
  1658. const AVCodecDescriptor *desc;
  1659. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1660. if (!fmt) {
  1661. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1662. return;
  1663. }
  1664. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1665. if (fmt->extensions)
  1666. printf(" Common extensions: %s.\n", fmt->extensions);
  1667. if (fmt->mime_type)
  1668. printf(" Mime type: %s.\n", fmt->mime_type);
  1669. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1670. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1671. printf(" Default video codec: %s.\n", desc->name);
  1672. }
  1673. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1674. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1675. printf(" Default audio codec: %s.\n", desc->name);
  1676. }
  1677. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1678. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1679. printf(" Default subtitle codec: %s.\n", desc->name);
  1680. }
  1681. if (fmt->priv_class)
  1682. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1683. }
  1684. #if CONFIG_AVFILTER
  1685. static void show_help_filter(const char *name)
  1686. {
  1687. #if CONFIG_AVFILTER
  1688. const AVFilter *f = avfilter_get_by_name(name);
  1689. int i, count;
  1690. if (!name) {
  1691. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1692. return;
  1693. } else if (!f) {
  1694. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1695. return;
  1696. }
  1697. printf("Filter %s\n", f->name);
  1698. if (f->description)
  1699. printf(" %s\n", f->description);
  1700. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1701. printf(" slice threading supported\n");
  1702. printf(" Inputs:\n");
  1703. count = avfilter_pad_count(f->inputs);
  1704. for (i = 0; i < count; i++) {
  1705. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1706. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1707. }
  1708. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1709. printf(" dynamic (depending on the options)\n");
  1710. else if (!count)
  1711. printf(" none (source filter)\n");
  1712. printf(" Outputs:\n");
  1713. count = avfilter_pad_count(f->outputs);
  1714. for (i = 0; i < count; i++) {
  1715. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1716. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1717. }
  1718. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1719. printf(" dynamic (depending on the options)\n");
  1720. else if (!count)
  1721. printf(" none (sink filter)\n");
  1722. if (f->priv_class)
  1723. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  1724. AV_OPT_FLAG_AUDIO_PARAM);
  1725. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  1726. printf("This filter has support for timeline through the 'enable' option.\n");
  1727. #else
  1728. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  1729. "can not to satisfy request\n");
  1730. #endif
  1731. }
  1732. #endif
  1733. static void show_help_bsf(const char *name)
  1734. {
  1735. const AVBitStreamFilter *bsf = av_bsf_get_by_name(name);
  1736. if (!name) {
  1737. av_log(NULL, AV_LOG_ERROR, "No bitstream filter name specified.\n");
  1738. return;
  1739. } else if (!bsf) {
  1740. av_log(NULL, AV_LOG_ERROR, "Unknown bit stream filter '%s'.\n", name);
  1741. return;
  1742. }
  1743. printf("Bit stream filter %s\n", bsf->name);
  1744. PRINT_CODEC_SUPPORTED(bsf, codec_ids, enum AVCodecID, "codecs",
  1745. AV_CODEC_ID_NONE, GET_CODEC_NAME);
  1746. if (bsf->priv_class)
  1747. show_help_children(bsf->priv_class, AV_OPT_FLAG_BSF_PARAM);
  1748. }
  1749. int show_help(void *optctx, const char *opt, const char *arg)
  1750. {
  1751. char *topic, *par;
  1752. av_log_set_callback(log_callback_help);
  1753. topic = av_strdup(arg ? arg : "");
  1754. if (!topic)
  1755. return AVERROR(ENOMEM);
  1756. par = strchr(topic, '=');
  1757. if (par)
  1758. *par++ = 0;
  1759. if (!*topic) {
  1760. show_help_default(topic, par);
  1761. } else if (!strcmp(topic, "decoder")) {
  1762. show_help_codec(par, 0);
  1763. } else if (!strcmp(topic, "encoder")) {
  1764. show_help_codec(par, 1);
  1765. } else if (!strcmp(topic, "demuxer")) {
  1766. show_help_demuxer(par);
  1767. } else if (!strcmp(topic, "muxer")) {
  1768. show_help_muxer(par);
  1769. } else if (!strcmp(topic, "protocol")) {
  1770. show_help_protocol(par);
  1771. #if CONFIG_AVFILTER
  1772. } else if (!strcmp(topic, "filter")) {
  1773. show_help_filter(par);
  1774. #endif
  1775. } else if (!strcmp(topic, "bsf")) {
  1776. show_help_bsf(par);
  1777. } else {
  1778. show_help_default(topic, par);
  1779. }
  1780. av_freep(&topic);
  1781. return 0;
  1782. }
  1783. int read_yesno(void)
  1784. {
  1785. int c = getchar();
  1786. int yesno = (av_toupper(c) == 'Y');
  1787. while (c != '\n' && c != EOF)
  1788. c = getchar();
  1789. return yesno;
  1790. }
  1791. FILE *get_preset_file(char *filename, size_t filename_size,
  1792. const char *preset_name, int is_path,
  1793. const char *codec_name)
  1794. {
  1795. FILE *f = NULL;
  1796. int i;
  1797. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1798. getenv("HOME"),
  1799. FFMPEG_DATADIR, };
  1800. if (is_path) {
  1801. av_strlcpy(filename, preset_name, filename_size);
  1802. f = fopen(filename, "r");
  1803. } else {
  1804. #if HAVE_GETMODULEHANDLE && defined(_WIN32)
  1805. char datadir[MAX_PATH], *ls;
  1806. base[2] = NULL;
  1807. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1808. {
  1809. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1810. if (*ls == '\\') *ls = '/';
  1811. if (ls = strrchr(datadir, '/'))
  1812. {
  1813. *ls = 0;
  1814. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1815. base[2] = datadir;
  1816. }
  1817. }
  1818. #endif
  1819. for (i = 0; i < 3 && !f; i++) {
  1820. if (!base[i])
  1821. continue;
  1822. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1823. i != 1 ? "" : "/.ffmpeg", preset_name);
  1824. f = fopen(filename, "r");
  1825. if (!f && codec_name) {
  1826. snprintf(filename, filename_size,
  1827. "%s%s/%s-%s.ffpreset",
  1828. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1829. preset_name);
  1830. f = fopen(filename, "r");
  1831. }
  1832. }
  1833. }
  1834. return f;
  1835. }
  1836. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1837. {
  1838. int ret = avformat_match_stream_specifier(s, st, spec);
  1839. if (ret < 0)
  1840. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1841. return ret;
  1842. }
  1843. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1844. AVFormatContext *s, AVStream *st, const AVCodec *codec)
  1845. {
  1846. AVDictionary *ret = NULL;
  1847. AVDictionaryEntry *t = NULL;
  1848. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1849. : AV_OPT_FLAG_DECODING_PARAM;
  1850. char prefix = 0;
  1851. const AVClass *cc = avcodec_get_class();
  1852. if (!codec)
  1853. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1854. : avcodec_find_decoder(codec_id);
  1855. switch (st->codecpar->codec_type) {
  1856. case AVMEDIA_TYPE_VIDEO:
  1857. prefix = 'v';
  1858. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1859. break;
  1860. case AVMEDIA_TYPE_AUDIO:
  1861. prefix = 'a';
  1862. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1863. break;
  1864. case AVMEDIA_TYPE_SUBTITLE:
  1865. prefix = 's';
  1866. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1867. break;
  1868. }
  1869. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1870. const AVClass *priv_class;
  1871. char *p = strchr(t->key, ':');
  1872. /* check stream specification in opt name */
  1873. if (p)
  1874. switch (check_stream_specifier(s, st, p + 1)) {
  1875. case 1: *p = 0; break;
  1876. case 0: continue;
  1877. default: exit_program(1);
  1878. }
  1879. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1880. !codec ||
  1881. ((priv_class = codec->priv_class) &&
  1882. av_opt_find(&priv_class, t->key, NULL, flags,
  1883. AV_OPT_SEARCH_FAKE_OBJ)))
  1884. av_dict_set(&ret, t->key, t->value, 0);
  1885. else if (t->key[0] == prefix &&
  1886. av_opt_find(&cc, t->key + 1, NULL, flags,
  1887. AV_OPT_SEARCH_FAKE_OBJ))
  1888. av_dict_set(&ret, t->key + 1, t->value, 0);
  1889. if (p)
  1890. *p = ':';
  1891. }
  1892. return ret;
  1893. }
  1894. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1895. AVDictionary *codec_opts)
  1896. {
  1897. int i;
  1898. AVDictionary **opts;
  1899. if (!s->nb_streams)
  1900. return NULL;
  1901. opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
  1902. if (!opts) {
  1903. av_log(NULL, AV_LOG_ERROR,
  1904. "Could not alloc memory for stream options.\n");
  1905. return NULL;
  1906. }
  1907. for (i = 0; i < s->nb_streams; i++)
  1908. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
  1909. s, s->streams[i], NULL);
  1910. return opts;
  1911. }
  1912. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1913. {
  1914. if (new_size >= INT_MAX / elem_size) {
  1915. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1916. exit_program(1);
  1917. }
  1918. if (*size < new_size) {
  1919. uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
  1920. if (!tmp) {
  1921. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1922. exit_program(1);
  1923. }
  1924. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1925. *size = new_size;
  1926. return tmp;
  1927. }
  1928. return array;
  1929. }
  1930. double get_rotation(AVStream *st)
  1931. {
  1932. uint8_t* displaymatrix = av_stream_get_side_data(st,
  1933. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  1934. double theta = 0;
  1935. if (displaymatrix)
  1936. theta = -av_display_rotation_get((int32_t*) displaymatrix);
  1937. theta -= 360*floor(theta/360 + 0.9/360);
  1938. if (fabs(theta - 90*round(theta/90)) > 2)
  1939. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  1940. "If you want to help, upload a sample "
  1941. "of this file to https://streams.videolan.org/upload/ "
  1942. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  1943. return theta;
  1944. }
  1945. #if CONFIG_AVDEVICE
  1946. static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
  1947. {
  1948. int ret, i;
  1949. AVDeviceInfoList *device_list = NULL;
  1950. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1951. return AVERROR(EINVAL);
  1952. printf("Auto-detected sources for %s:\n", fmt->name);
  1953. if (!fmt->get_device_list) {
  1954. ret = AVERROR(ENOSYS);
  1955. printf("Cannot list sources. Not implemented.\n");
  1956. goto fail;
  1957. }
  1958. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1959. printf("Cannot list sources.\n");
  1960. goto fail;
  1961. }
  1962. for (i = 0; i < device_list->nb_devices; i++) {
  1963. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1964. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1965. }
  1966. fail:
  1967. avdevice_free_list_devices(&device_list);
  1968. return ret;
  1969. }
  1970. static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
  1971. {
  1972. int ret, i;
  1973. AVDeviceInfoList *device_list = NULL;
  1974. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1975. return AVERROR(EINVAL);
  1976. printf("Auto-detected sinks for %s:\n", fmt->name);
  1977. if (!fmt->get_device_list) {
  1978. ret = AVERROR(ENOSYS);
  1979. printf("Cannot list sinks. Not implemented.\n");
  1980. goto fail;
  1981. }
  1982. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1983. printf("Cannot list sinks.\n");
  1984. goto fail;
  1985. }
  1986. for (i = 0; i < device_list->nb_devices; i++) {
  1987. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1988. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1989. }
  1990. fail:
  1991. avdevice_free_list_devices(&device_list);
  1992. return ret;
  1993. }
  1994. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1995. {
  1996. int ret;
  1997. if (arg) {
  1998. char *opts_str = NULL;
  1999. av_assert0(dev && opts);
  2000. *dev = av_strdup(arg);
  2001. if (!*dev)
  2002. return AVERROR(ENOMEM);
  2003. if ((opts_str = strchr(*dev, ','))) {
  2004. *(opts_str++) = '\0';
  2005. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  2006. av_freep(dev);
  2007. return ret;
  2008. }
  2009. }
  2010. } else
  2011. printf("\nDevice name is not provided.\n"
  2012. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  2013. return 0;
  2014. }
  2015. int show_sources(void *optctx, const char *opt, const char *arg)
  2016. {
  2017. AVInputFormat *fmt = NULL;
  2018. char *dev = NULL;
  2019. AVDictionary *opts = NULL;
  2020. int ret = 0;
  2021. int error_level = av_log_get_level();
  2022. av_log_set_level(AV_LOG_WARNING);
  2023. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  2024. goto fail;
  2025. do {
  2026. fmt = av_input_audio_device_next(fmt);
  2027. if (fmt) {
  2028. if (!strcmp(fmt->name, "lavfi"))
  2029. continue; //it's pointless to probe lavfi
  2030. if (dev && !av_match_name(dev, fmt->name))
  2031. continue;
  2032. print_device_sources(fmt, opts);
  2033. }
  2034. } while (fmt);
  2035. do {
  2036. fmt = av_input_video_device_next(fmt);
  2037. if (fmt) {
  2038. if (dev && !av_match_name(dev, fmt->name))
  2039. continue;
  2040. print_device_sources(fmt, opts);
  2041. }
  2042. } while (fmt);
  2043. fail:
  2044. av_dict_free(&opts);
  2045. av_free(dev);
  2046. av_log_set_level(error_level);
  2047. return ret;
  2048. }
  2049. int show_sinks(void *optctx, const char *opt, const char *arg)
  2050. {
  2051. AVOutputFormat *fmt = NULL;
  2052. char *dev = NULL;
  2053. AVDictionary *opts = NULL;
  2054. int ret = 0;
  2055. int error_level = av_log_get_level();
  2056. av_log_set_level(AV_LOG_WARNING);
  2057. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  2058. goto fail;
  2059. do {
  2060. fmt = av_output_audio_device_next(fmt);
  2061. if (fmt) {
  2062. if (dev && !av_match_name(dev, fmt->name))
  2063. continue;
  2064. print_device_sinks(fmt, opts);
  2065. }
  2066. } while (fmt);
  2067. do {
  2068. fmt = av_output_video_device_next(fmt);
  2069. if (fmt) {
  2070. if (dev && !av_match_name(dev, fmt->name))
  2071. continue;
  2072. print_device_sinks(fmt, opts);
  2073. }
  2074. } while (fmt);
  2075. fail:
  2076. av_dict_free(&opts);
  2077. av_free(dev);
  2078. av_log_set_level(error_level);
  2079. return ret;
  2080. }
  2081. #endif