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.

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