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.

2020 lines
63KB

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