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.

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