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.

1994 lines
63KB

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