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.

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