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.

2038 lines
64KB

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