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.

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