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.

2048 lines
65KB

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