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.

1959 lines
62KB

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