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.

2230 lines
70KB

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