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.

2224 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. 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. void init_dynload(void)
  98. {
  99. #ifdef _WIN32
  100. /* Calling SetDllDirectory with the empty string (but not NULL) removes the
  101. * current working directory from the DLL search path as a security pre-caution. */
  102. SetDllDirectory("");
  103. #endif
  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 (!strcmp(opt, "srcw") || !strcmp(opt, "srch") ||
  502. !strcmp(opt, "dstw") || !strcmp(opt, "dsth") ||
  503. !strcmp(opt, "src_format") || !strcmp(opt, "dst_format")) {
  504. av_log(NULL, AV_LOG_ERROR, "Directly using swscale dimensions/format options is not supported, please use the -s or -pix_fmt options\n");
  505. return AVERROR(EINVAL);
  506. }
  507. if (ret < 0) {
  508. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  509. return ret;
  510. }
  511. av_dict_set(&sws_dict, opt, arg, FLAGS);
  512. consumed = 1;
  513. }
  514. #else
  515. if (!consumed && !strcmp(opt, "sws_flags")) {
  516. av_log(NULL, AV_LOG_WARNING, "Ignoring %s %s, due to disabled swscale\n", opt, arg);
  517. consumed = 1;
  518. }
  519. #endif
  520. #if CONFIG_SWRESAMPLE
  521. swr_class = swr_get_class();
  522. if (!consumed && (o=opt_find(&swr_class, opt, NULL, 0,
  523. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  524. struct SwrContext *swr = swr_alloc();
  525. int ret = av_opt_set(swr, opt, arg, 0);
  526. swr_free(&swr);
  527. if (ret < 0) {
  528. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  529. return ret;
  530. }
  531. av_dict_set(&swr_opts, opt, arg, FLAGS);
  532. consumed = 1;
  533. }
  534. #endif
  535. #if CONFIG_AVRESAMPLE
  536. if ((o=opt_find(&rc, opt, NULL, 0,
  537. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  538. av_dict_set(&resample_opts, opt, arg, FLAGS);
  539. consumed = 1;
  540. }
  541. #endif
  542. if (consumed)
  543. return 0;
  544. return AVERROR_OPTION_NOT_FOUND;
  545. }
  546. /*
  547. * Check whether given option is a group separator.
  548. *
  549. * @return index of the group definition that matched or -1 if none
  550. */
  551. static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  552. const char *opt)
  553. {
  554. int i;
  555. for (i = 0; i < nb_groups; i++) {
  556. const OptionGroupDef *p = &groups[i];
  557. if (p->sep && !strcmp(p->sep, opt))
  558. return i;
  559. }
  560. return -1;
  561. }
  562. /*
  563. * Finish parsing an option group.
  564. *
  565. * @param group_idx which group definition should this group belong to
  566. * @param arg argument of the group delimiting option
  567. */
  568. static void finish_group(OptionParseContext *octx, int group_idx,
  569. const char *arg)
  570. {
  571. OptionGroupList *l = &octx->groups[group_idx];
  572. OptionGroup *g;
  573. GROW_ARRAY(l->groups, l->nb_groups);
  574. g = &l->groups[l->nb_groups - 1];
  575. *g = octx->cur_group;
  576. g->arg = arg;
  577. g->group_def = l->group_def;
  578. g->sws_dict = sws_dict;
  579. g->swr_opts = swr_opts;
  580. g->codec_opts = codec_opts;
  581. g->format_opts = format_opts;
  582. g->resample_opts = resample_opts;
  583. codec_opts = NULL;
  584. format_opts = NULL;
  585. resample_opts = NULL;
  586. sws_dict = NULL;
  587. swr_opts = NULL;
  588. init_opts();
  589. memset(&octx->cur_group, 0, sizeof(octx->cur_group));
  590. }
  591. /*
  592. * Add an option instance to currently parsed group.
  593. */
  594. static void add_opt(OptionParseContext *octx, const OptionDef *opt,
  595. const char *key, const char *val)
  596. {
  597. int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
  598. OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
  599. GROW_ARRAY(g->opts, g->nb_opts);
  600. g->opts[g->nb_opts - 1].opt = opt;
  601. g->opts[g->nb_opts - 1].key = key;
  602. g->opts[g->nb_opts - 1].val = val;
  603. }
  604. static void init_parse_context(OptionParseContext *octx,
  605. const OptionGroupDef *groups, int nb_groups)
  606. {
  607. static const OptionGroupDef global_group = { "global" };
  608. int i;
  609. memset(octx, 0, sizeof(*octx));
  610. octx->nb_groups = nb_groups;
  611. octx->groups = av_mallocz_array(octx->nb_groups, sizeof(*octx->groups));
  612. if (!octx->groups)
  613. exit_program(1);
  614. for (i = 0; i < octx->nb_groups; i++)
  615. octx->groups[i].group_def = &groups[i];
  616. octx->global_opts.group_def = &global_group;
  617. octx->global_opts.arg = "";
  618. init_opts();
  619. }
  620. void uninit_parse_context(OptionParseContext *octx)
  621. {
  622. int i, j;
  623. for (i = 0; i < octx->nb_groups; i++) {
  624. OptionGroupList *l = &octx->groups[i];
  625. for (j = 0; j < l->nb_groups; j++) {
  626. av_freep(&l->groups[j].opts);
  627. av_dict_free(&l->groups[j].codec_opts);
  628. av_dict_free(&l->groups[j].format_opts);
  629. av_dict_free(&l->groups[j].resample_opts);
  630. av_dict_free(&l->groups[j].sws_dict);
  631. av_dict_free(&l->groups[j].swr_opts);
  632. }
  633. av_freep(&l->groups);
  634. }
  635. av_freep(&octx->groups);
  636. av_freep(&octx->cur_group.opts);
  637. av_freep(&octx->global_opts.opts);
  638. uninit_opts();
  639. }
  640. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  641. const OptionDef *options,
  642. const OptionGroupDef *groups, int nb_groups)
  643. {
  644. int optindex = 1;
  645. int dashdash = -2;
  646. /* perform system-dependent conversions for arguments list */
  647. prepare_app_arguments(&argc, &argv);
  648. init_parse_context(octx, groups, nb_groups);
  649. av_log(NULL, AV_LOG_DEBUG, "Splitting the commandline.\n");
  650. while (optindex < argc) {
  651. const char *opt = argv[optindex++], *arg;
  652. const OptionDef *po;
  653. int ret;
  654. av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);
  655. if (opt[0] == '-' && opt[1] == '-' && !opt[2]) {
  656. dashdash = optindex;
  657. continue;
  658. }
  659. /* unnamed group separators, e.g. output filename */
  660. if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
  661. finish_group(octx, 0, opt);
  662. av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
  663. continue;
  664. }
  665. opt++;
  666. #define GET_ARG(arg) \
  667. do { \
  668. arg = argv[optindex++]; \
  669. if (!arg) { \
  670. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'.\n", opt);\
  671. return AVERROR(EINVAL); \
  672. } \
  673. } while (0)
  674. /* named group separators, e.g. -i */
  675. if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
  676. GET_ARG(arg);
  677. finish_group(octx, ret, arg);
  678. av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
  679. groups[ret].name, arg);
  680. continue;
  681. }
  682. /* normal options */
  683. po = find_option(options, opt);
  684. if (po->name) {
  685. if (po->flags & OPT_EXIT) {
  686. /* optional argument, e.g. -h */
  687. arg = argv[optindex++];
  688. } else if (po->flags & HAS_ARG) {
  689. GET_ARG(arg);
  690. } else {
  691. arg = "1";
  692. }
  693. add_opt(octx, po, opt, arg);
  694. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  695. "argument '%s'.\n", po->name, po->help, arg);
  696. continue;
  697. }
  698. /* AVOptions */
  699. if (argv[optindex]) {
  700. ret = opt_default(NULL, opt, argv[optindex]);
  701. if (ret >= 0) {
  702. av_log(NULL, AV_LOG_DEBUG, " matched as AVOption '%s' with "
  703. "argument '%s'.\n", opt, argv[optindex]);
  704. optindex++;
  705. continue;
  706. } else if (ret != AVERROR_OPTION_NOT_FOUND) {
  707. av_log(NULL, AV_LOG_ERROR, "Error parsing option '%s' "
  708. "with argument '%s'.\n", opt, argv[optindex]);
  709. return ret;
  710. }
  711. }
  712. /* boolean -nofoo options */
  713. if (opt[0] == 'n' && opt[1] == 'o' &&
  714. (po = find_option(options, opt + 2)) &&
  715. po->name && po->flags & OPT_BOOL) {
  716. add_opt(octx, po, opt, "0");
  717. av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
  718. "argument 0.\n", po->name, po->help);
  719. continue;
  720. }
  721. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'.\n", opt);
  722. return AVERROR_OPTION_NOT_FOUND;
  723. }
  724. if (octx->cur_group.nb_opts || codec_opts || format_opts || resample_opts)
  725. av_log(NULL, AV_LOG_WARNING, "Trailing options were found on the "
  726. "commandline.\n");
  727. av_log(NULL, AV_LOG_DEBUG, "Finished splitting the commandline.\n");
  728. return 0;
  729. }
  730. int opt_cpuflags(void *optctx, const char *opt, const char *arg)
  731. {
  732. int ret;
  733. unsigned flags = av_get_cpu_flags();
  734. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  735. return ret;
  736. av_force_cpu_flags(flags);
  737. return 0;
  738. }
  739. int opt_loglevel(void *optctx, const char *opt, const char *arg)
  740. {
  741. const struct { const char *name; int level; } log_levels[] = {
  742. { "quiet" , AV_LOG_QUIET },
  743. { "panic" , AV_LOG_PANIC },
  744. { "fatal" , AV_LOG_FATAL },
  745. { "error" , AV_LOG_ERROR },
  746. { "warning", AV_LOG_WARNING },
  747. { "info" , AV_LOG_INFO },
  748. { "verbose", AV_LOG_VERBOSE },
  749. { "debug" , AV_LOG_DEBUG },
  750. { "trace" , AV_LOG_TRACE },
  751. };
  752. char *tail;
  753. int level;
  754. int flags;
  755. int i;
  756. flags = av_log_get_flags();
  757. tail = strstr(arg, "repeat");
  758. if (tail)
  759. flags &= ~AV_LOG_SKIP_REPEATED;
  760. else
  761. flags |= AV_LOG_SKIP_REPEATED;
  762. av_log_set_flags(flags);
  763. if (tail == arg)
  764. arg += 6 + (arg[6]=='+');
  765. if(tail && !*arg)
  766. return 0;
  767. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  768. if (!strcmp(log_levels[i].name, arg)) {
  769. av_log_set_level(log_levels[i].level);
  770. return 0;
  771. }
  772. }
  773. level = strtol(arg, &tail, 10);
  774. if (*tail) {
  775. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  776. "Possible levels are numbers or:\n", arg);
  777. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  778. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  779. exit_program(1);
  780. }
  781. av_log_set_level(level);
  782. return 0;
  783. }
  784. static void expand_filename_template(AVBPrint *bp, const char *template,
  785. struct tm *tm)
  786. {
  787. int c;
  788. while ((c = *(template++))) {
  789. if (c == '%') {
  790. if (!(c = *(template++)))
  791. break;
  792. switch (c) {
  793. case 'p':
  794. av_bprintf(bp, "%s", program_name);
  795. break;
  796. case 't':
  797. av_bprintf(bp, "%04d%02d%02d-%02d%02d%02d",
  798. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  799. tm->tm_hour, tm->tm_min, tm->tm_sec);
  800. break;
  801. case '%':
  802. av_bprint_chars(bp, c, 1);
  803. break;
  804. }
  805. } else {
  806. av_bprint_chars(bp, c, 1);
  807. }
  808. }
  809. }
  810. static int init_report(const char *env)
  811. {
  812. char *filename_template = NULL;
  813. char *key, *val;
  814. int ret, count = 0;
  815. time_t now;
  816. struct tm *tm;
  817. AVBPrint filename;
  818. if (report_file) /* already opened */
  819. return 0;
  820. time(&now);
  821. tm = localtime(&now);
  822. while (env && *env) {
  823. if ((ret = av_opt_get_key_value(&env, "=", ":", 0, &key, &val)) < 0) {
  824. if (count)
  825. av_log(NULL, AV_LOG_ERROR,
  826. "Failed to parse FFREPORT environment variable: %s\n",
  827. av_err2str(ret));
  828. break;
  829. }
  830. if (*env)
  831. env++;
  832. count++;
  833. if (!strcmp(key, "file")) {
  834. av_free(filename_template);
  835. filename_template = val;
  836. val = NULL;
  837. } else if (!strcmp(key, "level")) {
  838. char *tail;
  839. report_file_level = strtol(val, &tail, 10);
  840. if (*tail) {
  841. av_log(NULL, AV_LOG_FATAL, "Invalid report file level\n");
  842. exit_program(1);
  843. }
  844. } else {
  845. av_log(NULL, AV_LOG_ERROR, "Unknown key '%s' in FFREPORT\n", key);
  846. }
  847. av_free(val);
  848. av_free(key);
  849. }
  850. av_bprint_init(&filename, 0, 1);
  851. expand_filename_template(&filename,
  852. av_x_if_null(filename_template, "%p-%t.log"), tm);
  853. av_free(filename_template);
  854. if (!av_bprint_is_complete(&filename)) {
  855. av_log(NULL, AV_LOG_ERROR, "Out of memory building report file name\n");
  856. return AVERROR(ENOMEM);
  857. }
  858. report_file = fopen(filename.str, "w");
  859. if (!report_file) {
  860. int ret = AVERROR(errno);
  861. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  862. filename.str, strerror(errno));
  863. return ret;
  864. }
  865. av_log_set_callback(log_callback_report);
  866. av_log(NULL, AV_LOG_INFO,
  867. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  868. "Report written to \"%s\"\n",
  869. program_name,
  870. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  871. tm->tm_hour, tm->tm_min, tm->tm_sec,
  872. filename.str);
  873. av_bprint_finalize(&filename, NULL);
  874. return 0;
  875. }
  876. int opt_report(const char *opt)
  877. {
  878. return init_report(NULL);
  879. }
  880. int opt_max_alloc(void *optctx, const char *opt, const char *arg)
  881. {
  882. char *tail;
  883. size_t max;
  884. max = strtol(arg, &tail, 10);
  885. if (*tail) {
  886. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  887. exit_program(1);
  888. }
  889. av_max_alloc(max);
  890. return 0;
  891. }
  892. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  893. {
  894. #if HAVE_SETRLIMIT
  895. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  896. struct rlimit rl = { lim, lim + 1 };
  897. if (setrlimit(RLIMIT_CPU, &rl))
  898. perror("setrlimit");
  899. #else
  900. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  901. #endif
  902. return 0;
  903. }
  904. void print_error(const char *filename, int err)
  905. {
  906. char errbuf[128];
  907. const char *errbuf_ptr = errbuf;
  908. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  909. errbuf_ptr = strerror(AVUNERROR(err));
  910. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  911. }
  912. static int warned_cfg = 0;
  913. #define INDENT 1
  914. #define SHOW_VERSION 2
  915. #define SHOW_CONFIG 4
  916. #define SHOW_COPYRIGHT 8
  917. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  918. if (CONFIG_##LIBNAME) { \
  919. const char *indent = flags & INDENT? " " : ""; \
  920. if (flags & SHOW_VERSION) { \
  921. unsigned int version = libname##_version(); \
  922. av_log(NULL, level, \
  923. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  924. indent, #libname, \
  925. LIB##LIBNAME##_VERSION_MAJOR, \
  926. LIB##LIBNAME##_VERSION_MINOR, \
  927. LIB##LIBNAME##_VERSION_MICRO, \
  928. version >> 16, version >> 8 & 0xff, version & 0xff); \
  929. } \
  930. if (flags & SHOW_CONFIG) { \
  931. const char *cfg = libname##_configuration(); \
  932. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  933. if (!warned_cfg) { \
  934. av_log(NULL, level, \
  935. "%sWARNING: library configuration mismatch\n", \
  936. indent); \
  937. warned_cfg = 1; \
  938. } \
  939. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  940. indent, #libname, cfg); \
  941. } \
  942. } \
  943. } \
  944. static void print_all_libs_info(int flags, int level)
  945. {
  946. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  947. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  948. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  949. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  950. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  951. PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  952. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  953. PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
  954. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  955. }
  956. static void print_program_info(int flags, int level)
  957. {
  958. const char *indent = flags & INDENT? " " : "";
  959. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  960. if (flags & SHOW_COPYRIGHT)
  961. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  962. program_birth_year, CONFIG_THIS_YEAR);
  963. av_log(NULL, level, "\n");
  964. av_log(NULL, level, "%sbuilt with %s\n", indent, CC_IDENT);
  965. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  966. }
  967. static void print_buildconf(int flags, int level)
  968. {
  969. const char *indent = flags & INDENT ? " " : "";
  970. char str[] = { FFMPEG_CONFIGURATION };
  971. char *conflist, *remove_tilde, *splitconf;
  972. // Change all the ' --' strings to '~--' so that
  973. // they can be identified as tokens.
  974. while ((conflist = strstr(str, " --")) != NULL) {
  975. strncpy(conflist, "~--", 3);
  976. }
  977. // Compensate for the weirdness this would cause
  978. // when passing 'pkg-config --static'.
  979. while ((remove_tilde = strstr(str, "pkg-config~")) != NULL) {
  980. strncpy(remove_tilde, "pkg-config ", 11);
  981. }
  982. splitconf = strtok(str, "~");
  983. av_log(NULL, level, "\n%sconfiguration:\n", indent);
  984. while (splitconf != NULL) {
  985. av_log(NULL, level, "%s%s%s\n", indent, indent, splitconf);
  986. splitconf = strtok(NULL, "~");
  987. }
  988. }
  989. void show_banner(int argc, char **argv, const OptionDef *options)
  990. {
  991. int idx = locate_option(argc, argv, options, "version");
  992. if (hide_banner || idx)
  993. return;
  994. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  995. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  996. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  997. }
  998. int show_version(void *optctx, const char *opt, const char *arg)
  999. {
  1000. av_log_set_callback(log_callback_help);
  1001. print_program_info (SHOW_COPYRIGHT, AV_LOG_INFO);
  1002. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  1003. return 0;
  1004. }
  1005. int show_buildconf(void *optctx, const char *opt, const char *arg)
  1006. {
  1007. av_log_set_callback(log_callback_help);
  1008. print_buildconf (INDENT|0, AV_LOG_INFO);
  1009. return 0;
  1010. }
  1011. int show_license(void *optctx, const char *opt, const char *arg)
  1012. {
  1013. #if CONFIG_NONFREE
  1014. printf(
  1015. "This version of %s has nonfree parts compiled in.\n"
  1016. "Therefore it is not legally redistributable.\n",
  1017. program_name );
  1018. #elif CONFIG_GPLV3
  1019. printf(
  1020. "%s is free software; you can redistribute it and/or modify\n"
  1021. "it under the terms of the GNU General Public License as published by\n"
  1022. "the Free Software Foundation; either version 3 of the License, or\n"
  1023. "(at your option) any later version.\n"
  1024. "\n"
  1025. "%s is distributed in the hope that it will be useful,\n"
  1026. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1027. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1028. "GNU General Public License for more details.\n"
  1029. "\n"
  1030. "You should have received a copy of the GNU General Public License\n"
  1031. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1032. program_name, program_name, program_name );
  1033. #elif CONFIG_GPL
  1034. printf(
  1035. "%s is free software; you can redistribute it and/or modify\n"
  1036. "it under the terms of the GNU General Public License as published by\n"
  1037. "the Free Software Foundation; either version 2 of the License, or\n"
  1038. "(at your option) any later version.\n"
  1039. "\n"
  1040. "%s is distributed in the hope that it will be useful,\n"
  1041. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1042. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1043. "GNU General Public License for more details.\n"
  1044. "\n"
  1045. "You should have received a copy of the GNU General Public License\n"
  1046. "along with %s; if not, write to the Free Software\n"
  1047. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1048. program_name, program_name, program_name );
  1049. #elif CONFIG_LGPLV3
  1050. printf(
  1051. "%s is free software; you can redistribute it and/or modify\n"
  1052. "it under the terms of the GNU Lesser General Public License as published by\n"
  1053. "the Free Software Foundation; either version 3 of the License, or\n"
  1054. "(at your option) any later version.\n"
  1055. "\n"
  1056. "%s is distributed in the hope that it will be useful,\n"
  1057. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1058. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1059. "GNU Lesser General Public License for more details.\n"
  1060. "\n"
  1061. "You should have received a copy of the GNU Lesser General Public License\n"
  1062. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  1063. program_name, program_name, program_name );
  1064. #else
  1065. printf(
  1066. "%s is free software; you can redistribute it and/or\n"
  1067. "modify it under the terms of the GNU Lesser General Public\n"
  1068. "License as published by the Free Software Foundation; either\n"
  1069. "version 2.1 of the License, or (at your option) any later version.\n"
  1070. "\n"
  1071. "%s is distributed in the hope that it will be useful,\n"
  1072. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1073. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1074. "Lesser General Public License for more details.\n"
  1075. "\n"
  1076. "You should have received a copy of the GNU Lesser General Public\n"
  1077. "License along with %s; if not, write to the Free Software\n"
  1078. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  1079. program_name, program_name, program_name );
  1080. #endif
  1081. return 0;
  1082. }
  1083. static int is_device(const AVClass *avclass)
  1084. {
  1085. if (!avclass)
  1086. return 0;
  1087. return AV_IS_INPUT_DEVICE(avclass->category) || AV_IS_OUTPUT_DEVICE(avclass->category);
  1088. }
  1089. static int show_formats_devices(void *optctx, const char *opt, const char *arg, int device_only)
  1090. {
  1091. AVInputFormat *ifmt = NULL;
  1092. AVOutputFormat *ofmt = NULL;
  1093. const char *last_name;
  1094. int is_dev;
  1095. printf("%s\n"
  1096. " D. = Demuxing supported\n"
  1097. " .E = Muxing supported\n"
  1098. " --\n", device_only ? "Devices:" : "File formats:");
  1099. last_name = "000";
  1100. for (;;) {
  1101. int decode = 0;
  1102. int encode = 0;
  1103. const char *name = NULL;
  1104. const char *long_name = NULL;
  1105. while ((ofmt = av_oformat_next(ofmt))) {
  1106. is_dev = is_device(ofmt->priv_class);
  1107. if (!is_dev && device_only)
  1108. continue;
  1109. if ((!name || strcmp(ofmt->name, name) < 0) &&
  1110. strcmp(ofmt->name, last_name) > 0) {
  1111. name = ofmt->name;
  1112. long_name = ofmt->long_name;
  1113. encode = 1;
  1114. }
  1115. }
  1116. while ((ifmt = av_iformat_next(ifmt))) {
  1117. is_dev = is_device(ifmt->priv_class);
  1118. if (!is_dev && device_only)
  1119. continue;
  1120. if ((!name || strcmp(ifmt->name, name) < 0) &&
  1121. strcmp(ifmt->name, last_name) > 0) {
  1122. name = ifmt->name;
  1123. long_name = ifmt->long_name;
  1124. encode = 0;
  1125. }
  1126. if (name && strcmp(ifmt->name, name) == 0)
  1127. decode = 1;
  1128. }
  1129. if (!name)
  1130. break;
  1131. last_name = name;
  1132. printf(" %s%s %-15s %s\n",
  1133. decode ? "D" : " ",
  1134. encode ? "E" : " ",
  1135. name,
  1136. long_name ? long_name:" ");
  1137. }
  1138. return 0;
  1139. }
  1140. int show_formats(void *optctx, const char *opt, const char *arg)
  1141. {
  1142. return show_formats_devices(optctx, opt, arg, 0);
  1143. }
  1144. int show_devices(void *optctx, const char *opt, const char *arg)
  1145. {
  1146. return show_formats_devices(optctx, opt, arg, 1);
  1147. }
  1148. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  1149. if (codec->field) { \
  1150. const type *p = codec->field; \
  1151. \
  1152. printf(" Supported " list_name ":"); \
  1153. while (*p != term) { \
  1154. get_name(*p); \
  1155. printf(" %s", name); \
  1156. p++; \
  1157. } \
  1158. printf("\n"); \
  1159. } \
  1160. static void print_codec(const AVCodec *c)
  1161. {
  1162. int encoder = av_codec_is_encoder(c);
  1163. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  1164. c->long_name ? c->long_name : "");
  1165. if (c->type == AVMEDIA_TYPE_VIDEO ||
  1166. c->type == AVMEDIA_TYPE_AUDIO) {
  1167. printf(" Threading capabilities: ");
  1168. switch (c->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
  1169. AV_CODEC_CAP_SLICE_THREADS)) {
  1170. case AV_CODEC_CAP_FRAME_THREADS |
  1171. AV_CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  1172. case AV_CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  1173. case AV_CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  1174. default: printf("no"); break;
  1175. }
  1176. printf("\n");
  1177. }
  1178. if (c->supported_framerates) {
  1179. const AVRational *fps = c->supported_framerates;
  1180. printf(" Supported framerates:");
  1181. while (fps->num) {
  1182. printf(" %d/%d", fps->num, fps->den);
  1183. fps++;
  1184. }
  1185. printf("\n");
  1186. }
  1187. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum AVPixelFormat, "pixel formats",
  1188. AV_PIX_FMT_NONE, GET_PIX_FMT_NAME);
  1189. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  1190. GET_SAMPLE_RATE_NAME);
  1191. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  1192. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  1193. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  1194. 0, GET_CH_LAYOUT_DESC);
  1195. if (c->priv_class) {
  1196. show_help_children(c->priv_class,
  1197. AV_OPT_FLAG_ENCODING_PARAM |
  1198. AV_OPT_FLAG_DECODING_PARAM);
  1199. }
  1200. }
  1201. static char get_media_type_char(enum AVMediaType type)
  1202. {
  1203. switch (type) {
  1204. case AVMEDIA_TYPE_VIDEO: return 'V';
  1205. case AVMEDIA_TYPE_AUDIO: return 'A';
  1206. case AVMEDIA_TYPE_DATA: return 'D';
  1207. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  1208. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  1209. default: return '?';
  1210. }
  1211. }
  1212. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  1213. int encoder)
  1214. {
  1215. while ((prev = av_codec_next(prev))) {
  1216. if (prev->id == id &&
  1217. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  1218. return prev;
  1219. }
  1220. return NULL;
  1221. }
  1222. static int compare_codec_desc(const void *a, const void *b)
  1223. {
  1224. const AVCodecDescriptor * const *da = a;
  1225. const AVCodecDescriptor * const *db = b;
  1226. return (*da)->type != (*db)->type ? (*da)->type - (*db)->type :
  1227. strcmp((*da)->name, (*db)->name);
  1228. }
  1229. static unsigned get_codecs_sorted(const AVCodecDescriptor ***rcodecs)
  1230. {
  1231. const AVCodecDescriptor *desc = NULL;
  1232. const AVCodecDescriptor **codecs;
  1233. unsigned nb_codecs = 0, i = 0;
  1234. while ((desc = avcodec_descriptor_next(desc)))
  1235. nb_codecs++;
  1236. if (!(codecs = av_calloc(nb_codecs, sizeof(*codecs)))) {
  1237. av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
  1238. exit_program(1);
  1239. }
  1240. desc = NULL;
  1241. while ((desc = avcodec_descriptor_next(desc)))
  1242. codecs[i++] = desc;
  1243. av_assert0(i == nb_codecs);
  1244. qsort(codecs, nb_codecs, sizeof(*codecs), compare_codec_desc);
  1245. *rcodecs = codecs;
  1246. return nb_codecs;
  1247. }
  1248. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  1249. {
  1250. const AVCodec *codec = NULL;
  1251. printf(" (%s: ", encoder ? "encoders" : "decoders");
  1252. while ((codec = next_codec_for_id(id, codec, encoder)))
  1253. printf("%s ", codec->name);
  1254. printf(")");
  1255. }
  1256. int show_codecs(void *optctx, const char *opt, const char *arg)
  1257. {
  1258. const AVCodecDescriptor **codecs;
  1259. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1260. printf("Codecs:\n"
  1261. " D..... = Decoding supported\n"
  1262. " .E.... = Encoding supported\n"
  1263. " ..V... = Video codec\n"
  1264. " ..A... = Audio codec\n"
  1265. " ..S... = Subtitle codec\n"
  1266. " ...I.. = Intra frame-only codec\n"
  1267. " ....L. = Lossy compression\n"
  1268. " .....S = Lossless compression\n"
  1269. " -------\n");
  1270. for (i = 0; i < nb_codecs; i++) {
  1271. const AVCodecDescriptor *desc = codecs[i];
  1272. const AVCodec *codec = NULL;
  1273. if (strstr(desc->name, "_deprecated"))
  1274. continue;
  1275. printf(" ");
  1276. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  1277. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  1278. printf("%c", get_media_type_char(desc->type));
  1279. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  1280. printf((desc->props & AV_CODEC_PROP_LOSSY) ? "L" : ".");
  1281. printf((desc->props & AV_CODEC_PROP_LOSSLESS) ? "S" : ".");
  1282. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  1283. /* print decoders/encoders when there's more than one or their
  1284. * names are different from codec name */
  1285. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  1286. if (strcmp(codec->name, desc->name)) {
  1287. print_codecs_for_id(desc->id, 0);
  1288. break;
  1289. }
  1290. }
  1291. codec = NULL;
  1292. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  1293. if (strcmp(codec->name, desc->name)) {
  1294. print_codecs_for_id(desc->id, 1);
  1295. break;
  1296. }
  1297. }
  1298. printf("\n");
  1299. }
  1300. av_free(codecs);
  1301. return 0;
  1302. }
  1303. static void print_codecs(int encoder)
  1304. {
  1305. const AVCodecDescriptor **codecs;
  1306. unsigned i, nb_codecs = get_codecs_sorted(&codecs);
  1307. printf("%s:\n"
  1308. " V..... = Video\n"
  1309. " A..... = Audio\n"
  1310. " S..... = Subtitle\n"
  1311. " .F.... = Frame-level multithreading\n"
  1312. " ..S... = Slice-level multithreading\n"
  1313. " ...X.. = Codec is experimental\n"
  1314. " ....B. = Supports draw_horiz_band\n"
  1315. " .....D = Supports direct rendering method 1\n"
  1316. " ------\n",
  1317. encoder ? "Encoders" : "Decoders");
  1318. for (i = 0; i < nb_codecs; i++) {
  1319. const AVCodecDescriptor *desc = codecs[i];
  1320. const AVCodec *codec = NULL;
  1321. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1322. printf(" %c", get_media_type_char(desc->type));
  1323. printf((codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  1324. printf((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  1325. printf((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  1326. printf((codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  1327. printf((codec->capabilities & AV_CODEC_CAP_DR1) ? "D" : ".");
  1328. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  1329. if (strcmp(codec->name, desc->name))
  1330. printf(" (codec %s)", desc->name);
  1331. printf("\n");
  1332. }
  1333. }
  1334. av_free(codecs);
  1335. }
  1336. int show_decoders(void *optctx, const char *opt, const char *arg)
  1337. {
  1338. print_codecs(0);
  1339. return 0;
  1340. }
  1341. int show_encoders(void *optctx, const char *opt, const char *arg)
  1342. {
  1343. print_codecs(1);
  1344. return 0;
  1345. }
  1346. int show_bsfs(void *optctx, const char *opt, const char *arg)
  1347. {
  1348. AVBitStreamFilter *bsf = NULL;
  1349. printf("Bitstream filters:\n");
  1350. while ((bsf = av_bitstream_filter_next(bsf)))
  1351. printf("%s\n", bsf->name);
  1352. printf("\n");
  1353. return 0;
  1354. }
  1355. int show_protocols(void *optctx, const char *opt, const char *arg)
  1356. {
  1357. void *opaque = NULL;
  1358. const char *name;
  1359. printf("Supported file protocols:\n"
  1360. "Input:\n");
  1361. while ((name = avio_enum_protocols(&opaque, 0)))
  1362. printf(" %s\n", name);
  1363. printf("Output:\n");
  1364. while ((name = avio_enum_protocols(&opaque, 1)))
  1365. printf(" %s\n", name);
  1366. return 0;
  1367. }
  1368. int show_filters(void *optctx, const char *opt, const char *arg)
  1369. {
  1370. #if CONFIG_AVFILTER
  1371. const AVFilter *filter = NULL;
  1372. char descr[64], *descr_cur;
  1373. int i, j;
  1374. const AVFilterPad *pad;
  1375. printf("Filters:\n"
  1376. " T.. = Timeline support\n"
  1377. " .S. = Slice threading\n"
  1378. " ..C = Command support\n"
  1379. " A = Audio input/output\n"
  1380. " V = Video input/output\n"
  1381. " N = Dynamic number and/or type of input/output\n"
  1382. " | = Source or sink filter\n");
  1383. while ((filter = avfilter_next(filter))) {
  1384. descr_cur = descr;
  1385. for (i = 0; i < 2; i++) {
  1386. if (i) {
  1387. *(descr_cur++) = '-';
  1388. *(descr_cur++) = '>';
  1389. }
  1390. pad = i ? filter->outputs : filter->inputs;
  1391. for (j = 0; pad && avfilter_pad_get_name(pad, j); j++) {
  1392. if (descr_cur >= descr + sizeof(descr) - 4)
  1393. break;
  1394. *(descr_cur++) = get_media_type_char(avfilter_pad_get_type(pad, j));
  1395. }
  1396. if (!j)
  1397. *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
  1398. ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
  1399. }
  1400. *descr_cur = 0;
  1401. printf(" %c%c%c %-16s %-10s %s\n",
  1402. filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE ? 'T' : '.',
  1403. filter->flags & AVFILTER_FLAG_SLICE_THREADS ? 'S' : '.',
  1404. filter->process_command ? 'C' : '.',
  1405. filter->name, descr, filter->description);
  1406. }
  1407. #else
  1408. printf("No filters available: libavfilter disabled\n");
  1409. #endif
  1410. return 0;
  1411. }
  1412. int show_colors(void *optctx, const char *opt, const char *arg)
  1413. {
  1414. const char *name;
  1415. const uint8_t *rgb;
  1416. int i;
  1417. printf("%-32s #RRGGBB\n", "name");
  1418. for (i = 0; name = av_get_known_color_name(i, &rgb); i++)
  1419. printf("%-32s #%02x%02x%02x\n", name, rgb[0], rgb[1], rgb[2]);
  1420. return 0;
  1421. }
  1422. int show_pix_fmts(void *optctx, const char *opt, const char *arg)
  1423. {
  1424. const AVPixFmtDescriptor *pix_desc = NULL;
  1425. printf("Pixel formats:\n"
  1426. "I.... = Supported Input format for conversion\n"
  1427. ".O... = Supported Output format for conversion\n"
  1428. "..H.. = Hardware accelerated format\n"
  1429. "...P. = Paletted format\n"
  1430. "....B = Bitstream format\n"
  1431. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  1432. "-----\n");
  1433. #if !CONFIG_SWSCALE
  1434. # define sws_isSupportedInput(x) 0
  1435. # define sws_isSupportedOutput(x) 0
  1436. #endif
  1437. while ((pix_desc = av_pix_fmt_desc_next(pix_desc))) {
  1438. enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(pix_desc);
  1439. printf("%c%c%c%c%c %-16s %d %2d\n",
  1440. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  1441. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  1442. pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? 'H' : '.',
  1443. pix_desc->flags & AV_PIX_FMT_FLAG_PAL ? 'P' : '.',
  1444. pix_desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ? 'B' : '.',
  1445. pix_desc->name,
  1446. pix_desc->nb_components,
  1447. av_get_bits_per_pixel(pix_desc));
  1448. }
  1449. return 0;
  1450. }
  1451. int show_layouts(void *optctx, const char *opt, const char *arg)
  1452. {
  1453. int i = 0;
  1454. uint64_t layout, j;
  1455. const char *name, *descr;
  1456. printf("Individual channels:\n"
  1457. "NAME DESCRIPTION\n");
  1458. for (i = 0; i < 63; i++) {
  1459. name = av_get_channel_name((uint64_t)1 << i);
  1460. if (!name)
  1461. continue;
  1462. descr = av_get_channel_description((uint64_t)1 << i);
  1463. printf("%-14s %s\n", name, descr);
  1464. }
  1465. printf("\nStandard channel layouts:\n"
  1466. "NAME DECOMPOSITION\n");
  1467. for (i = 0; !av_get_standard_channel_layout(i, &layout, &name); i++) {
  1468. if (name) {
  1469. printf("%-14s ", name);
  1470. for (j = 1; j; j <<= 1)
  1471. if ((layout & j))
  1472. printf("%s%s", (layout & (j - 1)) ? "+" : "", av_get_channel_name(j));
  1473. printf("\n");
  1474. }
  1475. }
  1476. return 0;
  1477. }
  1478. int show_sample_fmts(void *optctx, const char *opt, const char *arg)
  1479. {
  1480. int i;
  1481. char fmt_str[128];
  1482. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  1483. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  1484. return 0;
  1485. }
  1486. static void show_help_codec(const char *name, int encoder)
  1487. {
  1488. const AVCodecDescriptor *desc;
  1489. const AVCodec *codec;
  1490. if (!name) {
  1491. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  1492. return;
  1493. }
  1494. codec = encoder ? avcodec_find_encoder_by_name(name) :
  1495. avcodec_find_decoder_by_name(name);
  1496. if (codec)
  1497. print_codec(codec);
  1498. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  1499. int printed = 0;
  1500. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  1501. printed = 1;
  1502. print_codec(codec);
  1503. }
  1504. if (!printed) {
  1505. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  1506. "but no %s for it are available. FFmpeg might need to be "
  1507. "recompiled with additional external libraries.\n",
  1508. name, encoder ? "encoders" : "decoders");
  1509. }
  1510. } else {
  1511. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  1512. name);
  1513. }
  1514. }
  1515. static void show_help_demuxer(const char *name)
  1516. {
  1517. const AVInputFormat *fmt = av_find_input_format(name);
  1518. if (!fmt) {
  1519. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1520. return;
  1521. }
  1522. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1523. if (fmt->extensions)
  1524. printf(" Common extensions: %s.\n", fmt->extensions);
  1525. if (fmt->priv_class)
  1526. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1527. }
  1528. static void show_help_muxer(const char *name)
  1529. {
  1530. const AVCodecDescriptor *desc;
  1531. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1532. if (!fmt) {
  1533. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1534. return;
  1535. }
  1536. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1537. if (fmt->extensions)
  1538. printf(" Common extensions: %s.\n", fmt->extensions);
  1539. if (fmt->mime_type)
  1540. printf(" Mime type: %s.\n", fmt->mime_type);
  1541. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1542. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1543. printf(" Default video codec: %s.\n", desc->name);
  1544. }
  1545. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1546. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1547. printf(" Default audio codec: %s.\n", desc->name);
  1548. }
  1549. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1550. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1551. printf(" Default subtitle codec: %s.\n", desc->name);
  1552. }
  1553. if (fmt->priv_class)
  1554. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1555. }
  1556. #if CONFIG_AVFILTER
  1557. static void show_help_filter(const char *name)
  1558. {
  1559. #if CONFIG_AVFILTER
  1560. const AVFilter *f = avfilter_get_by_name(name);
  1561. int i, count;
  1562. if (!name) {
  1563. av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
  1564. return;
  1565. } else if (!f) {
  1566. av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
  1567. return;
  1568. }
  1569. printf("Filter %s\n", f->name);
  1570. if (f->description)
  1571. printf(" %s\n", f->description);
  1572. if (f->flags & AVFILTER_FLAG_SLICE_THREADS)
  1573. printf(" slice threading supported\n");
  1574. printf(" Inputs:\n");
  1575. count = avfilter_pad_count(f->inputs);
  1576. for (i = 0; i < count; i++) {
  1577. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
  1578. media_type_string(avfilter_pad_get_type(f->inputs, i)));
  1579. }
  1580. if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
  1581. printf(" dynamic (depending on the options)\n");
  1582. else if (!count)
  1583. printf(" none (source filter)\n");
  1584. printf(" Outputs:\n");
  1585. count = avfilter_pad_count(f->outputs);
  1586. for (i = 0; i < count; i++) {
  1587. printf(" #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
  1588. media_type_string(avfilter_pad_get_type(f->outputs, i)));
  1589. }
  1590. if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
  1591. printf(" dynamic (depending on the options)\n");
  1592. else if (!count)
  1593. printf(" none (sink filter)\n");
  1594. if (f->priv_class)
  1595. show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
  1596. AV_OPT_FLAG_AUDIO_PARAM);
  1597. if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
  1598. printf("This filter has support for timeline through the 'enable' option.\n");
  1599. #else
  1600. av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
  1601. "can not to satisfy request\n");
  1602. #endif
  1603. }
  1604. #endif
  1605. int show_help(void *optctx, const char *opt, const char *arg)
  1606. {
  1607. char *topic, *par;
  1608. av_log_set_callback(log_callback_help);
  1609. topic = av_strdup(arg ? arg : "");
  1610. if (!topic)
  1611. return AVERROR(ENOMEM);
  1612. par = strchr(topic, '=');
  1613. if (par)
  1614. *par++ = 0;
  1615. if (!*topic) {
  1616. show_help_default(topic, par);
  1617. } else if (!strcmp(topic, "decoder")) {
  1618. show_help_codec(par, 0);
  1619. } else if (!strcmp(topic, "encoder")) {
  1620. show_help_codec(par, 1);
  1621. } else if (!strcmp(topic, "demuxer")) {
  1622. show_help_demuxer(par);
  1623. } else if (!strcmp(topic, "muxer")) {
  1624. show_help_muxer(par);
  1625. #if CONFIG_AVFILTER
  1626. } else if (!strcmp(topic, "filter")) {
  1627. show_help_filter(par);
  1628. #endif
  1629. } else {
  1630. show_help_default(topic, par);
  1631. }
  1632. av_freep(&topic);
  1633. return 0;
  1634. }
  1635. int read_yesno(void)
  1636. {
  1637. int c = getchar();
  1638. int yesno = (av_toupper(c) == 'Y');
  1639. while (c != '\n' && c != EOF)
  1640. c = getchar();
  1641. return yesno;
  1642. }
  1643. FILE *get_preset_file(char *filename, size_t filename_size,
  1644. const char *preset_name, int is_path,
  1645. const char *codec_name)
  1646. {
  1647. FILE *f = NULL;
  1648. int i;
  1649. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1650. getenv("HOME"),
  1651. FFMPEG_DATADIR, };
  1652. if (is_path) {
  1653. av_strlcpy(filename, preset_name, filename_size);
  1654. f = fopen(filename, "r");
  1655. } else {
  1656. #ifdef _WIN32
  1657. char datadir[MAX_PATH], *ls;
  1658. base[2] = NULL;
  1659. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1660. {
  1661. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1662. if (*ls == '\\') *ls = '/';
  1663. if (ls = strrchr(datadir, '/'))
  1664. {
  1665. *ls = 0;
  1666. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1667. base[2] = datadir;
  1668. }
  1669. }
  1670. #endif
  1671. for (i = 0; i < 3 && !f; i++) {
  1672. if (!base[i])
  1673. continue;
  1674. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1675. i != 1 ? "" : "/.ffmpeg", preset_name);
  1676. f = fopen(filename, "r");
  1677. if (!f && codec_name) {
  1678. snprintf(filename, filename_size,
  1679. "%s%s/%s-%s.ffpreset",
  1680. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1681. preset_name);
  1682. f = fopen(filename, "r");
  1683. }
  1684. }
  1685. }
  1686. return f;
  1687. }
  1688. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1689. {
  1690. int ret = avformat_match_stream_specifier(s, st, spec);
  1691. if (ret < 0)
  1692. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1693. return ret;
  1694. }
  1695. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1696. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1697. {
  1698. AVDictionary *ret = NULL;
  1699. AVDictionaryEntry *t = NULL;
  1700. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1701. : AV_OPT_FLAG_DECODING_PARAM;
  1702. char prefix = 0;
  1703. const AVClass *cc = avcodec_get_class();
  1704. if (!codec)
  1705. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1706. : avcodec_find_decoder(codec_id);
  1707. switch (st->codec->codec_type) {
  1708. case AVMEDIA_TYPE_VIDEO:
  1709. prefix = 'v';
  1710. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1711. break;
  1712. case AVMEDIA_TYPE_AUDIO:
  1713. prefix = 'a';
  1714. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1715. break;
  1716. case AVMEDIA_TYPE_SUBTITLE:
  1717. prefix = 's';
  1718. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1719. break;
  1720. }
  1721. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1722. char *p = strchr(t->key, ':');
  1723. /* check stream specification in opt name */
  1724. if (p)
  1725. switch (check_stream_specifier(s, st, p + 1)) {
  1726. case 1: *p = 0; break;
  1727. case 0: continue;
  1728. default: exit_program(1);
  1729. }
  1730. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1731. !codec ||
  1732. (codec->priv_class &&
  1733. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1734. AV_OPT_SEARCH_FAKE_OBJ)))
  1735. av_dict_set(&ret, t->key, t->value, 0);
  1736. else if (t->key[0] == prefix &&
  1737. av_opt_find(&cc, t->key + 1, NULL, flags,
  1738. AV_OPT_SEARCH_FAKE_OBJ))
  1739. av_dict_set(&ret, t->key + 1, t->value, 0);
  1740. if (p)
  1741. *p = ':';
  1742. }
  1743. return ret;
  1744. }
  1745. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1746. AVDictionary *codec_opts)
  1747. {
  1748. int i;
  1749. AVDictionary **opts;
  1750. if (!s->nb_streams)
  1751. return NULL;
  1752. opts = av_mallocz_array(s->nb_streams, sizeof(*opts));
  1753. if (!opts) {
  1754. av_log(NULL, AV_LOG_ERROR,
  1755. "Could not alloc memory for stream options.\n");
  1756. return NULL;
  1757. }
  1758. for (i = 0; i < s->nb_streams; i++)
  1759. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1760. s, s->streams[i], NULL);
  1761. return opts;
  1762. }
  1763. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1764. {
  1765. if (new_size >= INT_MAX / elem_size) {
  1766. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1767. exit_program(1);
  1768. }
  1769. if (*size < new_size) {
  1770. uint8_t *tmp = av_realloc_array(array, new_size, elem_size);
  1771. if (!tmp) {
  1772. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1773. exit_program(1);
  1774. }
  1775. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1776. *size = new_size;
  1777. return tmp;
  1778. }
  1779. return array;
  1780. }
  1781. double get_rotation(AVStream *st)
  1782. {
  1783. AVDictionaryEntry *rotate_tag = av_dict_get(st->metadata, "rotate", NULL, 0);
  1784. uint8_t* displaymatrix = av_stream_get_side_data(st,
  1785. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  1786. double theta = 0;
  1787. if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
  1788. char *tail;
  1789. theta = av_strtod(rotate_tag->value, &tail);
  1790. if (*tail)
  1791. theta = 0;
  1792. }
  1793. if (displaymatrix && !theta)
  1794. theta = -av_display_rotation_get((int32_t*) displaymatrix);
  1795. theta -= 360*floor(theta/360 + 0.9/360);
  1796. if (fabs(theta - 90*round(theta/90)) > 2)
  1797. av_log(NULL, AV_LOG_WARNING, "Odd rotation angle.\n"
  1798. "If you want to help, upload a sample "
  1799. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  1800. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)");
  1801. return theta;
  1802. }
  1803. #if CONFIG_AVDEVICE
  1804. static int print_device_sources(AVInputFormat *fmt, AVDictionary *opts)
  1805. {
  1806. int ret, i;
  1807. AVDeviceInfoList *device_list = NULL;
  1808. if (!fmt || !fmt->priv_class || !AV_IS_INPUT_DEVICE(fmt->priv_class->category))
  1809. return AVERROR(EINVAL);
  1810. printf("Audo-detected sources for %s:\n", fmt->name);
  1811. if (!fmt->get_device_list) {
  1812. ret = AVERROR(ENOSYS);
  1813. printf("Cannot list sources. Not implemented.\n");
  1814. goto fail;
  1815. }
  1816. if ((ret = avdevice_list_input_sources(fmt, NULL, opts, &device_list)) < 0) {
  1817. printf("Cannot list sources.\n");
  1818. goto fail;
  1819. }
  1820. for (i = 0; i < device_list->nb_devices; i++) {
  1821. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1822. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1823. }
  1824. fail:
  1825. avdevice_free_list_devices(&device_list);
  1826. return ret;
  1827. }
  1828. static int print_device_sinks(AVOutputFormat *fmt, AVDictionary *opts)
  1829. {
  1830. int ret, i;
  1831. AVDeviceInfoList *device_list = NULL;
  1832. if (!fmt || !fmt->priv_class || !AV_IS_OUTPUT_DEVICE(fmt->priv_class->category))
  1833. return AVERROR(EINVAL);
  1834. printf("Audo-detected sinks for %s:\n", fmt->name);
  1835. if (!fmt->get_device_list) {
  1836. ret = AVERROR(ENOSYS);
  1837. printf("Cannot list sinks. Not implemented.\n");
  1838. goto fail;
  1839. }
  1840. if ((ret = avdevice_list_output_sinks(fmt, NULL, opts, &device_list)) < 0) {
  1841. printf("Cannot list sinks.\n");
  1842. goto fail;
  1843. }
  1844. for (i = 0; i < device_list->nb_devices; i++) {
  1845. printf("%s %s [%s]\n", device_list->default_device == i ? "*" : " ",
  1846. device_list->devices[i]->device_name, device_list->devices[i]->device_description);
  1847. }
  1848. fail:
  1849. avdevice_free_list_devices(&device_list);
  1850. return ret;
  1851. }
  1852. static int show_sinks_sources_parse_arg(const char *arg, char **dev, AVDictionary **opts)
  1853. {
  1854. int ret;
  1855. if (arg) {
  1856. char *opts_str = NULL;
  1857. av_assert0(dev && opts);
  1858. *dev = av_strdup(arg);
  1859. if (!*dev)
  1860. return AVERROR(ENOMEM);
  1861. if ((opts_str = strchr(*dev, ','))) {
  1862. *(opts_str++) = '\0';
  1863. if (opts_str[0] && ((ret = av_dict_parse_string(opts, opts_str, "=", ":", 0)) < 0)) {
  1864. av_freep(dev);
  1865. return ret;
  1866. }
  1867. }
  1868. } else
  1869. printf("\nDevice name is not provided.\n"
  1870. "You can pass devicename[,opt1=val1[,opt2=val2...]] as an argument.\n\n");
  1871. return 0;
  1872. }
  1873. int show_sources(void *optctx, const char *opt, const char *arg)
  1874. {
  1875. AVInputFormat *fmt = NULL;
  1876. char *dev = NULL;
  1877. AVDictionary *opts = NULL;
  1878. int ret = 0;
  1879. int error_level = av_log_get_level();
  1880. av_log_set_level(AV_LOG_ERROR);
  1881. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1882. goto fail;
  1883. do {
  1884. fmt = av_input_audio_device_next(fmt);
  1885. if (fmt) {
  1886. if (!strcmp(fmt->name, "lavfi"))
  1887. continue; //it's pointless to probe lavfi
  1888. if (dev && !av_match_name(dev, fmt->name))
  1889. continue;
  1890. print_device_sources(fmt, opts);
  1891. }
  1892. } while (fmt);
  1893. do {
  1894. fmt = av_input_video_device_next(fmt);
  1895. if (fmt) {
  1896. if (dev && !av_match_name(dev, fmt->name))
  1897. continue;
  1898. print_device_sources(fmt, opts);
  1899. }
  1900. } while (fmt);
  1901. fail:
  1902. av_dict_free(&opts);
  1903. av_free(dev);
  1904. av_log_set_level(error_level);
  1905. return ret;
  1906. }
  1907. int show_sinks(void *optctx, const char *opt, const char *arg)
  1908. {
  1909. AVOutputFormat *fmt = NULL;
  1910. char *dev = NULL;
  1911. AVDictionary *opts = NULL;
  1912. int ret = 0;
  1913. int error_level = av_log_get_level();
  1914. av_log_set_level(AV_LOG_ERROR);
  1915. if ((ret = show_sinks_sources_parse_arg(arg, &dev, &opts)) < 0)
  1916. goto fail;
  1917. do {
  1918. fmt = av_output_audio_device_next(fmt);
  1919. if (fmt) {
  1920. if (dev && !av_match_name(dev, fmt->name))
  1921. continue;
  1922. print_device_sinks(fmt, opts);
  1923. }
  1924. } while (fmt);
  1925. do {
  1926. fmt = av_output_video_device_next(fmt);
  1927. if (fmt) {
  1928. if (dev && !av_match_name(dev, fmt->name))
  1929. continue;
  1930. print_device_sinks(fmt, opts);
  1931. }
  1932. } while (fmt);
  1933. fail:
  1934. av_dict_free(&opts);
  1935. av_free(dev);
  1936. av_log_set_level(error_level);
  1937. return ret;
  1938. }
  1939. #endif