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.

1932 lines
61KB

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