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.

2240 lines
71KB

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