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.

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