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.

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