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.

1560 lines
49KB

  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <math.h>
  25. /* Include only the enabled headers since some compilers (namely, Sun
  26. Studio) will not omit unused inline functions and create undefined
  27. references to libraries that are not being built. */
  28. #include "config.h"
  29. #include "libavformat/avformat.h"
  30. #include "libavfilter/avfilter.h"
  31. #include "libavdevice/avdevice.h"
  32. #include "libavresample/avresample.h"
  33. #include "libswscale/swscale.h"
  34. #include "libswresample/swresample.h"
  35. #if CONFIG_POSTPROC
  36. #include "libpostproc/postprocess.h"
  37. #endif
  38. #include "libavutil/avassert.h"
  39. #include "libavutil/avstring.h"
  40. #include "libavutil/mathematics.h"
  41. #include "libavutil/imgutils.h"
  42. #include "libavutil/parseutils.h"
  43. #include "libavutil/pixdesc.h"
  44. #include "libavutil/eval.h"
  45. #include "libavutil/dict.h"
  46. #include "libavutil/opt.h"
  47. #include "cmdutils.h"
  48. #include "version.h"
  49. #if CONFIG_NETWORK
  50. #include "libavformat/network.h"
  51. #endif
  52. #if HAVE_SYS_RESOURCE_H
  53. #include <sys/resource.h>
  54. #endif
  55. struct SwsContext *sws_opts;
  56. SwrContext *swr_opts;
  57. AVDictionary *format_opts, *codec_opts;
  58. const int this_year = 2012;
  59. static FILE *report_file;
  60. void init_opts(void)
  61. {
  62. if(CONFIG_SWSCALE)
  63. sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC,
  64. NULL, NULL, NULL);
  65. if(CONFIG_SWRESAMPLE)
  66. swr_opts = swr_alloc();
  67. }
  68. void uninit_opts(void)
  69. {
  70. #if CONFIG_SWSCALE
  71. sws_freeContext(sws_opts);
  72. sws_opts = NULL;
  73. #endif
  74. if(CONFIG_SWRESAMPLE)
  75. swr_free(&swr_opts);
  76. av_dict_free(&format_opts);
  77. av_dict_free(&codec_opts);
  78. }
  79. void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
  80. {
  81. vfprintf(stdout, fmt, vl);
  82. }
  83. static void log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
  84. {
  85. va_list vl2;
  86. char line[1024];
  87. static int print_prefix = 1;
  88. va_copy(vl2, vl);
  89. av_log_default_callback(ptr, level, fmt, vl);
  90. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  91. va_end(vl2);
  92. fputs(line, report_file);
  93. fflush(report_file);
  94. }
  95. double parse_number_or_die(const char *context, const char *numstr, int type,
  96. double min, double max)
  97. {
  98. char *tail;
  99. const char *error;
  100. double d = av_strtod(numstr, &tail);
  101. if (*tail)
  102. error = "Expected number for %s but found: %s\n";
  103. else if (d < min || d > max)
  104. error = "The value for %s was %s which is not within %f - %f\n";
  105. else if (type == OPT_INT64 && (int64_t)d != d)
  106. error = "Expected int64 for %s but found %s\n";
  107. else if (type == OPT_INT && (int)d != d)
  108. error = "Expected int for %s but found %s\n";
  109. else
  110. return d;
  111. av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
  112. exit_program(1);
  113. return 0;
  114. }
  115. int64_t parse_time_or_die(const char *context, const char *timestr,
  116. int is_duration)
  117. {
  118. int64_t us;
  119. if (av_parse_time(&us, timestr, is_duration) < 0) {
  120. av_log(NULL, AV_LOG_FATAL, "Invalid %s specification for %s: %s\n",
  121. is_duration ? "duration" : "date", context, timestr);
  122. exit_program(1);
  123. }
  124. return us;
  125. }
  126. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  127. int rej_flags, int alt_flags)
  128. {
  129. const OptionDef *po;
  130. int first;
  131. first = 1;
  132. for (po = options; po->name != NULL; po++) {
  133. char buf[64];
  134. if (((po->flags & req_flags) != req_flags) ||
  135. (alt_flags && !(po->flags & alt_flags)) ||
  136. (po->flags & rej_flags))
  137. continue;
  138. if (first) {
  139. printf("%s\n", msg);
  140. first = 0;
  141. }
  142. av_strlcpy(buf, po->name, sizeof(buf));
  143. if (po->argname) {
  144. av_strlcat(buf, " ", sizeof(buf));
  145. av_strlcat(buf, po->argname, sizeof(buf));
  146. }
  147. printf("-%-17s %s\n", buf, po->help);
  148. }
  149. printf("\n");
  150. }
  151. void show_help_children(const AVClass *class, int flags)
  152. {
  153. const AVClass *child = NULL;
  154. if (class->option) {
  155. av_opt_show2(&class, NULL, flags, 0);
  156. printf("\n");
  157. }
  158. while (child = av_opt_child_class_next(class, child))
  159. show_help_children(child, flags);
  160. }
  161. static const OptionDef *find_option(const OptionDef *po, const char *name)
  162. {
  163. const char *p = strchr(name, ':');
  164. int len = p ? p - name : strlen(name);
  165. while (po->name != NULL) {
  166. if (!strncmp(name, po->name, len) && strlen(po->name) == len)
  167. break;
  168. po++;
  169. }
  170. return po;
  171. }
  172. #if defined(_WIN32) && !defined(__MINGW32CE__)
  173. #include <windows.h>
  174. #include <shellapi.h>
  175. /* Will be leaked on exit */
  176. static char** win32_argv_utf8 = NULL;
  177. static int win32_argc = 0;
  178. /**
  179. * Prepare command line arguments for executable.
  180. * For Windows - perform wide-char to UTF-8 conversion.
  181. * Input arguments should be main() function arguments.
  182. * @param argc_ptr Arguments number (including executable)
  183. * @param argv_ptr Arguments list.
  184. */
  185. static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  186. {
  187. char *argstr_flat;
  188. wchar_t **argv_w;
  189. int i, buffsize = 0, offset = 0;
  190. if (win32_argv_utf8) {
  191. *argc_ptr = win32_argc;
  192. *argv_ptr = win32_argv_utf8;
  193. return;
  194. }
  195. win32_argc = 0;
  196. argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
  197. if (win32_argc <= 0 || !argv_w)
  198. return;
  199. /* determine the UTF-8 buffer size (including NULL-termination symbols) */
  200. for (i = 0; i < win32_argc; i++)
  201. buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  202. NULL, 0, NULL, NULL);
  203. win32_argv_utf8 = av_mallocz(sizeof(char *) * (win32_argc + 1) + buffsize);
  204. argstr_flat = (char *)win32_argv_utf8 + sizeof(char *) * (win32_argc + 1);
  205. if (win32_argv_utf8 == NULL) {
  206. LocalFree(argv_w);
  207. return;
  208. }
  209. for (i = 0; i < win32_argc; i++) {
  210. win32_argv_utf8[i] = &argstr_flat[offset];
  211. offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
  212. &argstr_flat[offset],
  213. buffsize - offset, NULL, NULL);
  214. }
  215. win32_argv_utf8[i] = NULL;
  216. LocalFree(argv_w);
  217. *argc_ptr = win32_argc;
  218. *argv_ptr = win32_argv_utf8;
  219. }
  220. #else
  221. static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
  222. {
  223. /* nothing to do */
  224. }
  225. #endif /* WIN32 && !__MINGW32CE__ */
  226. int parse_option(void *optctx, const char *opt, const char *arg,
  227. const OptionDef *options)
  228. {
  229. const OptionDef *po;
  230. int bool_val = 1;
  231. int *dstcount;
  232. void *dst;
  233. po = find_option(options, opt);
  234. if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
  235. /* handle 'no' bool option */
  236. po = find_option(options, opt + 2);
  237. if ((po->name && (po->flags & OPT_BOOL)))
  238. bool_val = 0;
  239. }
  240. if (!po->name)
  241. po = find_option(options, "default");
  242. if (!po->name) {
  243. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  244. return AVERROR(EINVAL);
  245. }
  246. if (po->flags & HAS_ARG && !arg) {
  247. av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
  248. return AVERROR(EINVAL);
  249. }
  250. /* new-style options contain an offset into optctx, old-style address of
  251. * a global var*/
  252. dst = po->flags & (OPT_OFFSET | OPT_SPEC) ? (uint8_t *)optctx + po->u.off
  253. : po->u.dst_ptr;
  254. if (po->flags & OPT_SPEC) {
  255. SpecifierOpt **so = dst;
  256. char *p = strchr(opt, ':');
  257. dstcount = (int *)(so + 1);
  258. *so = grow_array(*so, sizeof(**so), dstcount, *dstcount + 1);
  259. (*so)[*dstcount - 1].specifier = av_strdup(p ? p + 1 : "");
  260. dst = &(*so)[*dstcount - 1].u;
  261. }
  262. if (po->flags & OPT_STRING) {
  263. char *str;
  264. str = av_strdup(arg);
  265. // av_freep(dst);
  266. *(char **)dst = str;
  267. } else if (po->flags & OPT_BOOL) {
  268. *(int *)dst = bool_val;
  269. } else if (po->flags & OPT_INT) {
  270. *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
  271. } else if (po->flags & OPT_INT64) {
  272. *(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
  273. } else if (po->flags & OPT_TIME) {
  274. *(int64_t *)dst = parse_time_or_die(opt, arg, 1);
  275. } else if (po->flags & OPT_FLOAT) {
  276. *(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
  277. } else if (po->flags & OPT_DOUBLE) {
  278. *(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
  279. } else if (po->u.func_arg) {
  280. int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg)
  281. : po->u.func_arg(opt, arg);
  282. if (ret < 0) {
  283. av_log(NULL, AV_LOG_ERROR,
  284. "Failed to set value '%s' for option '%s'\n", arg, opt);
  285. return ret;
  286. }
  287. }
  288. if (po->flags & OPT_EXIT)
  289. exit_program(0);
  290. return !!(po->flags & HAS_ARG);
  291. }
  292. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  293. void (*parse_arg_function)(void *, const char*))
  294. {
  295. const char *opt;
  296. int optindex, handleoptions = 1, ret;
  297. /* perform system-dependent conversions for arguments list */
  298. prepare_app_arguments(&argc, &argv);
  299. /* parse options */
  300. optindex = 1;
  301. while (optindex < argc) {
  302. opt = argv[optindex++];
  303. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  304. if (opt[1] == '-' && opt[2] == '\0') {
  305. handleoptions = 0;
  306. continue;
  307. }
  308. opt++;
  309. if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
  310. exit_program(1);
  311. optindex += ret;
  312. } else {
  313. if (parse_arg_function)
  314. parse_arg_function(optctx, opt);
  315. }
  316. }
  317. }
  318. int locate_option(int argc, char **argv, const OptionDef *options,
  319. const char *optname)
  320. {
  321. const OptionDef *po;
  322. int i;
  323. for (i = 1; i < argc; i++) {
  324. const char *cur_opt = argv[i];
  325. if (*cur_opt++ != '-')
  326. continue;
  327. po = find_option(options, cur_opt);
  328. if (!po->name && cur_opt[0] == 'n' && cur_opt[1] == 'o')
  329. po = find_option(options, cur_opt + 2);
  330. if ((!po->name && !strcmp(cur_opt, optname)) ||
  331. (po->name && !strcmp(optname, po->name)))
  332. return i;
  333. if (!po || po->flags & HAS_ARG)
  334. i++;
  335. }
  336. return 0;
  337. }
  338. static void dump_argument(const char *a)
  339. {
  340. const unsigned char *p;
  341. for (p = a; *p; p++)
  342. if (!((*p >= '+' && *p <= ':') || (*p >= '@' && *p <= 'Z') ||
  343. *p == '_' || (*p >= 'a' && *p <= 'z')))
  344. break;
  345. if (!*p) {
  346. fputs(a, report_file);
  347. return;
  348. }
  349. fputc('"', report_file);
  350. for (p = a; *p; p++) {
  351. if (*p == '\\' || *p == '"' || *p == '$' || *p == '`')
  352. fprintf(report_file, "\\%c", *p);
  353. else if (*p < ' ' || *p > '~')
  354. fprintf(report_file, "\\x%02x", *p);
  355. else
  356. fputc(*p, report_file);
  357. }
  358. fputc('"', report_file);
  359. }
  360. void parse_loglevel(int argc, char **argv, const OptionDef *options)
  361. {
  362. int idx = locate_option(argc, argv, options, "loglevel");
  363. if (!idx)
  364. idx = locate_option(argc, argv, options, "v");
  365. if (idx && argv[idx + 1])
  366. opt_loglevel("loglevel", argv[idx + 1]);
  367. idx = locate_option(argc, argv, options, "report");
  368. if (idx || getenv("FFREPORT")) {
  369. opt_report("report");
  370. if (report_file) {
  371. int i;
  372. fprintf(report_file, "Command line:\n");
  373. for (i = 0; i < argc; i++) {
  374. dump_argument(argv[i]);
  375. fputc(i < argc - 1 ? ' ' : '\n', report_file);
  376. }
  377. fflush(report_file);
  378. }
  379. }
  380. }
  381. #define FLAGS (o->type == AV_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
  382. int opt_default(const char *opt, const char *arg)
  383. {
  384. const AVOption *o;
  385. char opt_stripped[128];
  386. const char *p;
  387. const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc, *swr_class;
  388. if (!(p = strchr(opt, ':')))
  389. p = opt + strlen(opt);
  390. av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
  391. if ((o = av_opt_find(&cc, opt_stripped, NULL, 0,
  392. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) ||
  393. ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
  394. (o = av_opt_find(&cc, opt + 1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
  395. av_dict_set(&codec_opts, opt, arg, FLAGS);
  396. else if ((o = av_opt_find(&fc, opt, NULL, 0,
  397. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
  398. av_dict_set(&format_opts, opt, arg, FLAGS);
  399. #if CONFIG_SWSCALE
  400. sc = sws_get_class();
  401. if (!o && (o = av_opt_find(&sc, opt, NULL, 0,
  402. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  403. // XXX we only support sws_flags, not arbitrary sws options
  404. int ret = av_opt_set(sws_opts, opt, arg, 0);
  405. if (ret < 0) {
  406. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  407. return ret;
  408. }
  409. }
  410. #endif
  411. #if CONFIG_SWRESAMPLE
  412. swr_class = swr_get_class();
  413. if (!o && (o = av_opt_find(&swr_class, opt, NULL, 0,
  414. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
  415. int ret = av_opt_set(swr_opts, opt, arg, 0);
  416. if (ret < 0) {
  417. av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
  418. return ret;
  419. }
  420. }
  421. #endif
  422. if (o)
  423. return 0;
  424. av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
  425. return AVERROR_OPTION_NOT_FOUND;
  426. }
  427. int opt_loglevel(const char *opt, const char *arg)
  428. {
  429. const struct { const char *name; int level; } log_levels[] = {
  430. { "quiet" , AV_LOG_QUIET },
  431. { "panic" , AV_LOG_PANIC },
  432. { "fatal" , AV_LOG_FATAL },
  433. { "error" , AV_LOG_ERROR },
  434. { "warning", AV_LOG_WARNING },
  435. { "info" , AV_LOG_INFO },
  436. { "verbose", AV_LOG_VERBOSE },
  437. { "debug" , AV_LOG_DEBUG },
  438. };
  439. char *tail;
  440. int level;
  441. int i;
  442. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
  443. if (!strcmp(log_levels[i].name, arg)) {
  444. av_log_set_level(log_levels[i].level);
  445. return 0;
  446. }
  447. }
  448. level = strtol(arg, &tail, 10);
  449. if (*tail) {
  450. av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "
  451. "Possible levels are numbers or:\n", arg);
  452. for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
  453. av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);
  454. exit_program(1);
  455. }
  456. av_log_set_level(level);
  457. return 0;
  458. }
  459. int opt_report(const char *opt)
  460. {
  461. char filename[64];
  462. time_t now;
  463. struct tm *tm;
  464. if (report_file) /* already opened */
  465. return 0;
  466. time(&now);
  467. tm = localtime(&now);
  468. snprintf(filename, sizeof(filename), "%s-%04d%02d%02d-%02d%02d%02d.log",
  469. program_name,
  470. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  471. tm->tm_hour, tm->tm_min, tm->tm_sec);
  472. report_file = fopen(filename, "w");
  473. if (!report_file) {
  474. av_log(NULL, AV_LOG_ERROR, "Failed to open report \"%s\": %s\n",
  475. filename, strerror(errno));
  476. return AVERROR(errno);
  477. }
  478. av_log_set_callback(log_callback_report);
  479. av_log(NULL, AV_LOG_INFO,
  480. "%s started on %04d-%02d-%02d at %02d:%02d:%02d\n"
  481. "Report written to \"%s\"\n",
  482. program_name,
  483. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  484. tm->tm_hour, tm->tm_min, tm->tm_sec,
  485. filename);
  486. av_log_set_level(FFMAX(av_log_get_level(), AV_LOG_VERBOSE));
  487. return 0;
  488. }
  489. int opt_max_alloc(const char *opt, const char *arg)
  490. {
  491. char *tail;
  492. size_t max;
  493. max = strtol(arg, &tail, 10);
  494. if (*tail) {
  495. av_log(NULL, AV_LOG_FATAL, "Invalid max_alloc \"%s\".\n", arg);
  496. exit_program(1);
  497. }
  498. av_max_alloc(max);
  499. return 0;
  500. }
  501. int opt_cpuflags(const char *opt, const char *arg)
  502. {
  503. int ret;
  504. unsigned flags = av_get_cpu_flags();
  505. if ((ret = av_parse_cpu_caps(&flags, arg)) < 0)
  506. return ret;
  507. av_force_cpu_flags(flags);
  508. return 0;
  509. }
  510. int opt_codec_debug(const char *opt, const char *arg)
  511. {
  512. av_log_set_level(AV_LOG_DEBUG);
  513. return opt_default(opt, arg);
  514. }
  515. int opt_timelimit(const char *opt, const char *arg)
  516. {
  517. #if HAVE_SETRLIMIT
  518. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  519. struct rlimit rl = { lim, lim + 1 };
  520. if (setrlimit(RLIMIT_CPU, &rl))
  521. perror("setrlimit");
  522. #else
  523. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  524. #endif
  525. return 0;
  526. }
  527. void print_error(const char *filename, int err)
  528. {
  529. char errbuf[128];
  530. const char *errbuf_ptr = errbuf;
  531. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  532. errbuf_ptr = strerror(AVUNERROR(err));
  533. av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr);
  534. }
  535. static int warned_cfg = 0;
  536. #define INDENT 1
  537. #define SHOW_VERSION 2
  538. #define SHOW_CONFIG 4
  539. #define SHOW_COPYRIGHT 8
  540. #define PRINT_LIB_INFO(libname, LIBNAME, flags, level) \
  541. if (CONFIG_##LIBNAME) { \
  542. const char *indent = flags & INDENT? " " : ""; \
  543. if (flags & SHOW_VERSION) { \
  544. unsigned int version = libname##_version(); \
  545. av_log(NULL, level, \
  546. "%slib%-11s %2d.%3d.%3d / %2d.%3d.%3d\n", \
  547. indent, #libname, \
  548. LIB##LIBNAME##_VERSION_MAJOR, \
  549. LIB##LIBNAME##_VERSION_MINOR, \
  550. LIB##LIBNAME##_VERSION_MICRO, \
  551. version >> 16, version >> 8 & 0xff, version & 0xff); \
  552. } \
  553. if (flags & SHOW_CONFIG) { \
  554. const char *cfg = libname##_configuration(); \
  555. if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
  556. if (!warned_cfg) { \
  557. av_log(NULL, level, \
  558. "%sWARNING: library configuration mismatch\n", \
  559. indent); \
  560. warned_cfg = 1; \
  561. } \
  562. av_log(NULL, level, "%s%-11s configuration: %s\n", \
  563. indent, #libname, cfg); \
  564. } \
  565. } \
  566. } \
  567. static void print_all_libs_info(int flags, int level)
  568. {
  569. PRINT_LIB_INFO(avutil, AVUTIL, flags, level);
  570. PRINT_LIB_INFO(avcodec, AVCODEC, flags, level);
  571. PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
  572. PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
  573. PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
  574. // PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
  575. PRINT_LIB_INFO(swscale, SWSCALE, flags, level);
  576. PRINT_LIB_INFO(swresample,SWRESAMPLE, flags, level);
  577. #if CONFIG_POSTPROC
  578. PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
  579. #endif
  580. }
  581. static void print_program_info(int flags, int level)
  582. {
  583. const char *indent = flags & INDENT? " " : "";
  584. av_log(NULL, level, "%s version " FFMPEG_VERSION, program_name);
  585. if (flags & SHOW_COPYRIGHT)
  586. av_log(NULL, level, " Copyright (c) %d-%d the FFmpeg developers",
  587. program_birth_year, this_year);
  588. av_log(NULL, level, "\n");
  589. av_log(NULL, level, "%sbuilt on %s %s with %s\n",
  590. indent, __DATE__, __TIME__, CC_IDENT);
  591. av_log(NULL, level, "%sconfiguration: " FFMPEG_CONFIGURATION "\n", indent);
  592. }
  593. void show_banner(int argc, char **argv, const OptionDef *options)
  594. {
  595. int idx = locate_option(argc, argv, options, "version");
  596. if (idx)
  597. return;
  598. print_program_info (INDENT|SHOW_COPYRIGHT, AV_LOG_INFO);
  599. print_all_libs_info(INDENT|SHOW_CONFIG, AV_LOG_INFO);
  600. print_all_libs_info(INDENT|SHOW_VERSION, AV_LOG_INFO);
  601. }
  602. int show_version(const char *opt, const char *arg)
  603. {
  604. av_log_set_callback(log_callback_help);
  605. print_program_info (0 , AV_LOG_INFO);
  606. print_all_libs_info(SHOW_VERSION, AV_LOG_INFO);
  607. return 0;
  608. }
  609. int show_license(const char *opt, const char *arg)
  610. {
  611. printf(
  612. #if CONFIG_NONFREE
  613. "This version of %s has nonfree parts compiled in.\n"
  614. "Therefore it is not legally redistributable.\n",
  615. program_name
  616. #elif CONFIG_GPLV3
  617. "%s is free software; you can redistribute it and/or modify\n"
  618. "it under the terms of the GNU General Public License as published by\n"
  619. "the Free Software Foundation; either version 3 of the License, or\n"
  620. "(at your option) any later version.\n"
  621. "\n"
  622. "%s is distributed in the hope that it will be useful,\n"
  623. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  624. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  625. "GNU General Public License for more details.\n"
  626. "\n"
  627. "You should have received a copy of the GNU General Public License\n"
  628. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  629. program_name, program_name, program_name
  630. #elif CONFIG_GPL
  631. "%s is free software; you can redistribute it and/or modify\n"
  632. "it under the terms of the GNU General Public License as published by\n"
  633. "the Free Software Foundation; either version 2 of the License, or\n"
  634. "(at your option) any later version.\n"
  635. "\n"
  636. "%s is distributed in the hope that it will be useful,\n"
  637. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  638. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  639. "GNU General Public License for more details.\n"
  640. "\n"
  641. "You should have received a copy of the GNU General Public License\n"
  642. "along with %s; if not, write to the Free Software\n"
  643. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  644. program_name, program_name, program_name
  645. #elif CONFIG_LGPLV3
  646. "%s is free software; you can redistribute it and/or modify\n"
  647. "it under the terms of the GNU Lesser General Public License as published by\n"
  648. "the Free Software Foundation; either version 3 of the License, or\n"
  649. "(at your option) any later version.\n"
  650. "\n"
  651. "%s is distributed in the hope that it will be useful,\n"
  652. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  653. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  654. "GNU Lesser General Public License for more details.\n"
  655. "\n"
  656. "You should have received a copy of the GNU Lesser General Public License\n"
  657. "along with %s. If not, see <http://www.gnu.org/licenses/>.\n",
  658. program_name, program_name, program_name
  659. #else
  660. "%s is free software; you can redistribute it and/or\n"
  661. "modify it under the terms of the GNU Lesser General Public\n"
  662. "License as published by the Free Software Foundation; either\n"
  663. "version 2.1 of the License, or (at your option) any later version.\n"
  664. "\n"
  665. "%s is distributed in the hope that it will be useful,\n"
  666. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  667. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  668. "Lesser General Public License for more details.\n"
  669. "\n"
  670. "You should have received a copy of the GNU Lesser General Public\n"
  671. "License along with %s; if not, write to the Free Software\n"
  672. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
  673. program_name, program_name, program_name
  674. #endif
  675. );
  676. return 0;
  677. }
  678. int show_formats(const char *opt, const char *arg)
  679. {
  680. AVInputFormat *ifmt = NULL;
  681. AVOutputFormat *ofmt = NULL;
  682. const char *last_name;
  683. printf("File formats:\n"
  684. " D. = Demuxing supported\n"
  685. " .E = Muxing supported\n"
  686. " --\n");
  687. last_name = "000";
  688. for (;;) {
  689. int decode = 0;
  690. int encode = 0;
  691. const char *name = NULL;
  692. const char *long_name = NULL;
  693. while ((ofmt = av_oformat_next(ofmt))) {
  694. if ((name == NULL || strcmp(ofmt->name, name) < 0) &&
  695. strcmp(ofmt->name, last_name) > 0) {
  696. name = ofmt->name;
  697. long_name = ofmt->long_name;
  698. encode = 1;
  699. }
  700. }
  701. while ((ifmt = av_iformat_next(ifmt))) {
  702. if ((name == NULL || strcmp(ifmt->name, name) < 0) &&
  703. strcmp(ifmt->name, last_name) > 0) {
  704. name = ifmt->name;
  705. long_name = ifmt->long_name;
  706. encode = 0;
  707. }
  708. if (name && strcmp(ifmt->name, name) == 0)
  709. decode = 1;
  710. }
  711. if (name == NULL)
  712. break;
  713. last_name = name;
  714. printf(" %s%s %-15s %s\n",
  715. decode ? "D" : " ",
  716. encode ? "E" : " ",
  717. name,
  718. long_name ? long_name:" ");
  719. }
  720. return 0;
  721. }
  722. #define PRINT_CODEC_SUPPORTED(codec, field, type, list_name, term, get_name) \
  723. if (codec->field) { \
  724. const type *p = c->field; \
  725. \
  726. printf(" Supported " list_name ":"); \
  727. while (*p != term) { \
  728. get_name(*p); \
  729. printf(" %s", name); \
  730. p++; \
  731. } \
  732. printf("\n"); \
  733. } \
  734. static void print_codec(const AVCodec *c)
  735. {
  736. int encoder = av_codec_is_encoder(c);
  737. printf("%s %s [%s]:\n", encoder ? "Encoder" : "Decoder", c->name,
  738. c->long_name ? c->long_name : "");
  739. if (c->type == AVMEDIA_TYPE_VIDEO) {
  740. printf(" Threading capabilities: ");
  741. switch (c->capabilities & (CODEC_CAP_FRAME_THREADS |
  742. CODEC_CAP_SLICE_THREADS)) {
  743. case CODEC_CAP_FRAME_THREADS |
  744. CODEC_CAP_SLICE_THREADS: printf("frame and slice"); break;
  745. case CODEC_CAP_FRAME_THREADS: printf("frame"); break;
  746. case CODEC_CAP_SLICE_THREADS: printf("slice"); break;
  747. default: printf("no"); break;
  748. }
  749. printf("\n");
  750. }
  751. if (c->supported_framerates) {
  752. const AVRational *fps = c->supported_framerates;
  753. printf(" Supported framerates:");
  754. while (fps->num) {
  755. printf(" %d/%d", fps->num, fps->den);
  756. fps++;
  757. }
  758. printf("\n");
  759. }
  760. PRINT_CODEC_SUPPORTED(c, pix_fmts, enum PixelFormat, "pixel formats",
  761. PIX_FMT_NONE, GET_PIX_FMT_NAME);
  762. PRINT_CODEC_SUPPORTED(c, supported_samplerates, int, "sample rates", 0,
  763. GET_SAMPLE_RATE_NAME);
  764. PRINT_CODEC_SUPPORTED(c, sample_fmts, enum AVSampleFormat, "sample formats",
  765. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME);
  766. PRINT_CODEC_SUPPORTED(c, channel_layouts, uint64_t, "channel layouts",
  767. 0, GET_CH_LAYOUT_DESC);
  768. if (c->priv_class) {
  769. show_help_children(c->priv_class,
  770. AV_OPT_FLAG_ENCODING_PARAM |
  771. AV_OPT_FLAG_DECODING_PARAM);
  772. }
  773. }
  774. static char get_media_type_char(enum AVMediaType type)
  775. {
  776. switch (type) {
  777. case AVMEDIA_TYPE_VIDEO: return 'V';
  778. case AVMEDIA_TYPE_AUDIO: return 'A';
  779. case AVMEDIA_TYPE_DATA: return 'D';
  780. case AVMEDIA_TYPE_SUBTITLE: return 'S';
  781. case AVMEDIA_TYPE_ATTACHMENT:return 'T';
  782. default: return '?';
  783. }
  784. }
  785. static const AVCodec *next_codec_for_id(enum AVCodecID id, const AVCodec *prev,
  786. int encoder)
  787. {
  788. while ((prev = av_codec_next(prev))) {
  789. if (prev->id == id &&
  790. (encoder ? av_codec_is_encoder(prev) : av_codec_is_decoder(prev)))
  791. return prev;
  792. }
  793. return NULL;
  794. }
  795. static void print_codecs_for_id(enum AVCodecID id, int encoder)
  796. {
  797. const AVCodec *codec = NULL;
  798. printf(" (%s: ", encoder ? "encoders" : "decoders");
  799. while ((codec = next_codec_for_id(id, codec, encoder)))
  800. printf("%s ", codec->name);
  801. printf(")");
  802. }
  803. int show_codecs(const char *opt, const char *arg)
  804. {
  805. const AVCodecDescriptor *desc = NULL;
  806. printf("Codecs:\n"
  807. " D... = Decoding supported\n"
  808. " .E.. = Encoding supported\n"
  809. " ..V. = Video codec\n"
  810. " ..A. = Audio codec\n"
  811. " ..S. = Subtitle codec\n"
  812. " ...I = Intra frame-only codec\n"
  813. " -----\n");
  814. while ((desc = avcodec_descriptor_next(desc))) {
  815. const AVCodec *codec = NULL;
  816. printf(avcodec_find_decoder(desc->id) ? "D" : ".");
  817. printf(avcodec_find_encoder(desc->id) ? "E" : ".");
  818. printf("%c", get_media_type_char(desc->type));
  819. printf((desc->props & AV_CODEC_PROP_INTRA_ONLY) ? "I" : ".");
  820. printf(" %-20s %s", desc->name, desc->long_name ? desc->long_name : "");
  821. /* print decoders/encoders when there's more than one or their
  822. * names are different from codec name */
  823. while ((codec = next_codec_for_id(desc->id, codec, 0))) {
  824. if (strcmp(codec->name, desc->name)) {
  825. print_codecs_for_id(desc->id, 0);
  826. break;
  827. }
  828. }
  829. codec = NULL;
  830. while ((codec = next_codec_for_id(desc->id, codec, 1))) {
  831. if (strcmp(codec->name, desc->name)) {
  832. print_codecs_for_id(desc->id, 1);
  833. break;
  834. }
  835. }
  836. printf("\n");
  837. }
  838. return 0;
  839. }
  840. static void print_codecs(int encoder)
  841. {
  842. const AVCodecDescriptor *desc = NULL;
  843. printf("%s:\n"
  844. " V..... = Video\n"
  845. " A..... = Audio\n"
  846. " S..... = Subtitle\n"
  847. " .F.... = Frame-level multithreading\n"
  848. " ..S... = Slice-level multithreading\n"
  849. " ...X.. = Codec is experimental\n"
  850. " ....B. = Supports draw_horiz_band\n"
  851. " .....D = Supports direct rendering method 1\n"
  852. " ------\n",
  853. encoder ? "Encoders" : "Decoders");
  854. while ((desc = avcodec_descriptor_next(desc))) {
  855. const AVCodec *codec = NULL;
  856. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  857. printf("%c", get_media_type_char(desc->type));
  858. printf((codec->capabilities & CODEC_CAP_FRAME_THREADS) ? "F" : ".");
  859. printf((codec->capabilities & CODEC_CAP_SLICE_THREADS) ? "S" : ".");
  860. printf((codec->capabilities & CODEC_CAP_EXPERIMENTAL) ? "X" : ".");
  861. printf((codec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND)?"B" : ".");
  862. printf((codec->capabilities & CODEC_CAP_DR1) ? "D" : ".");
  863. printf(" %-20s %s", codec->name, codec->long_name ? codec->long_name : "");
  864. if (strcmp(codec->name, desc->name))
  865. printf(" (codec %s)", desc->name);
  866. printf("\n");
  867. }
  868. }
  869. }
  870. int show_decoders(const char *opt, const char *arg)
  871. {
  872. print_codecs(0);
  873. return 0;
  874. }
  875. int show_encoders(const char *opt, const char *arg)
  876. {
  877. print_codecs(1);
  878. return 0;
  879. }
  880. int show_bsfs(const char *opt, const char *arg)
  881. {
  882. AVBitStreamFilter *bsf = NULL;
  883. printf("Bitstream filters:\n");
  884. while ((bsf = av_bitstream_filter_next(bsf)))
  885. printf("%s\n", bsf->name);
  886. printf("\n");
  887. return 0;
  888. }
  889. int show_protocols(const char *opt, const char *arg)
  890. {
  891. void *opaque = NULL;
  892. const char *name;
  893. printf("Supported file protocols:\n"
  894. "Input:\n");
  895. while ((name = avio_enum_protocols(&opaque, 0)))
  896. printf("%s\n", name);
  897. printf("Output:\n");
  898. while ((name = avio_enum_protocols(&opaque, 1)))
  899. printf("%s\n", name);
  900. return 0;
  901. }
  902. int show_filters(const char *opt, const char *arg)
  903. {
  904. AVFilter av_unused(**filter) = NULL;
  905. char descr[64], *descr_cur;
  906. int i, j;
  907. const AVFilterPad *pad;
  908. printf("Filters:\n");
  909. #if CONFIG_AVFILTER
  910. while ((filter = av_filter_next(filter)) && *filter) {
  911. descr_cur = descr;
  912. for (i = 0; i < 2; i++) {
  913. if (i) {
  914. *(descr_cur++) = '-';
  915. *(descr_cur++) = '>';
  916. }
  917. pad = i ? (*filter)->outputs : (*filter)->inputs;
  918. for (j = 0; pad[j].name; j++) {
  919. if (descr_cur >= descr + sizeof(descr) - 4)
  920. break;
  921. *(descr_cur++) = get_media_type_char(pad[j].type);
  922. }
  923. if (!j)
  924. *(descr_cur++) = '|';
  925. }
  926. *descr_cur = 0;
  927. printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
  928. }
  929. #endif
  930. return 0;
  931. }
  932. int show_pix_fmts(const char *opt, const char *arg)
  933. {
  934. enum PixelFormat pix_fmt;
  935. printf("Pixel formats:\n"
  936. "I.... = Supported Input format for conversion\n"
  937. ".O... = Supported Output format for conversion\n"
  938. "..H.. = Hardware accelerated format\n"
  939. "...P. = Paletted format\n"
  940. "....B = Bitstream format\n"
  941. "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
  942. "-----\n");
  943. #if !CONFIG_SWSCALE
  944. # define sws_isSupportedInput(x) 0
  945. # define sws_isSupportedOutput(x) 0
  946. #endif
  947. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
  948. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  949. if(!pix_desc->name)
  950. continue;
  951. printf("%c%c%c%c%c %-16s %d %2d\n",
  952. sws_isSupportedInput (pix_fmt) ? 'I' : '.',
  953. sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
  954. pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
  955. pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
  956. pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
  957. pix_desc->name,
  958. pix_desc->nb_components,
  959. av_get_bits_per_pixel(pix_desc));
  960. }
  961. return 0;
  962. }
  963. int show_sample_fmts(const char *opt, const char *arg)
  964. {
  965. int i;
  966. char fmt_str[128];
  967. for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
  968. printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
  969. return 0;
  970. }
  971. static void show_help_codec(const char *name, int encoder)
  972. {
  973. const AVCodecDescriptor *desc;
  974. const AVCodec *codec;
  975. if (!name) {
  976. av_log(NULL, AV_LOG_ERROR, "No codec name specified.\n");
  977. return;
  978. }
  979. codec = encoder ? avcodec_find_encoder_by_name(name) :
  980. avcodec_find_decoder_by_name(name);
  981. if (codec)
  982. print_codec(codec);
  983. else if ((desc = avcodec_descriptor_get_by_name(name))) {
  984. int printed = 0;
  985. while ((codec = next_codec_for_id(desc->id, codec, encoder))) {
  986. printed = 1;
  987. print_codec(codec);
  988. }
  989. if (!printed) {
  990. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is known to FFmpeg, "
  991. "but no %s for it are available. FFmpeg might need to be "
  992. "recompiled with additional external libraries.\n",
  993. name, encoder ? "encoders" : "decoders");
  994. }
  995. } else {
  996. av_log(NULL, AV_LOG_ERROR, "Codec '%s' is not recognized by FFmpeg.\n",
  997. name);
  998. }
  999. }
  1000. static void show_help_demuxer(const char *name)
  1001. {
  1002. const AVInputFormat *fmt = av_find_input_format(name);
  1003. if (!fmt) {
  1004. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1005. return;
  1006. }
  1007. printf("Demuxer %s [%s]:\n", fmt->name, fmt->long_name);
  1008. if (fmt->extensions)
  1009. printf(" Common extensions: %s.\n", fmt->extensions);
  1010. if (fmt->priv_class)
  1011. show_help_children(fmt->priv_class, AV_OPT_FLAG_DECODING_PARAM);
  1012. }
  1013. static void show_help_muxer(const char *name)
  1014. {
  1015. const AVCodecDescriptor *desc;
  1016. const AVOutputFormat *fmt = av_guess_format(name, NULL, NULL);
  1017. if (!fmt) {
  1018. av_log(NULL, AV_LOG_ERROR, "Unknown format '%s'.\n", name);
  1019. return;
  1020. }
  1021. printf("Muxer %s [%s]:\n", fmt->name, fmt->long_name);
  1022. if (fmt->extensions)
  1023. printf(" Common extensions: %s.\n", fmt->extensions);
  1024. if (fmt->mime_type)
  1025. printf(" Mime type: %s.\n", fmt->mime_type);
  1026. if (fmt->video_codec != AV_CODEC_ID_NONE &&
  1027. (desc = avcodec_descriptor_get(fmt->video_codec))) {
  1028. printf(" Default video codec: %s.\n", desc->name);
  1029. }
  1030. if (fmt->audio_codec != AV_CODEC_ID_NONE &&
  1031. (desc = avcodec_descriptor_get(fmt->audio_codec))) {
  1032. printf(" Default audio codec: %s.\n", desc->name);
  1033. }
  1034. if (fmt->subtitle_codec != AV_CODEC_ID_NONE &&
  1035. (desc = avcodec_descriptor_get(fmt->subtitle_codec))) {
  1036. printf(" Default subtitle codec: %s.\n", desc->name);
  1037. }
  1038. if (fmt->priv_class)
  1039. show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
  1040. }
  1041. int show_help(const char *opt, const char *arg)
  1042. {
  1043. char *topic, *par;
  1044. av_log_set_callback(log_callback_help);
  1045. topic = av_strdup(arg ? arg : "");
  1046. par = strchr(topic, '=');
  1047. if (par)
  1048. *par++ = 0;
  1049. if (!*topic) {
  1050. show_help_default(topic, par);
  1051. } else if (!strcmp(topic, "decoder")) {
  1052. show_help_codec(par, 0);
  1053. } else if (!strcmp(topic, "encoder")) {
  1054. show_help_codec(par, 1);
  1055. } else if (!strcmp(topic, "demuxer")) {
  1056. show_help_demuxer(par);
  1057. } else if (!strcmp(topic, "muxer")) {
  1058. show_help_muxer(par);
  1059. } else {
  1060. show_help_default(topic, par);
  1061. }
  1062. av_freep(&topic);
  1063. return 0;
  1064. }
  1065. int read_yesno(void)
  1066. {
  1067. int c = getchar();
  1068. int yesno = (toupper(c) == 'Y');
  1069. while (c != '\n' && c != EOF)
  1070. c = getchar();
  1071. return yesno;
  1072. }
  1073. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
  1074. {
  1075. int ret;
  1076. FILE *f = fopen(filename, "rb");
  1077. if (!f) {
  1078. av_log(NULL, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename,
  1079. strerror(errno));
  1080. return AVERROR(errno);
  1081. }
  1082. fseek(f, 0, SEEK_END);
  1083. *size = ftell(f);
  1084. fseek(f, 0, SEEK_SET);
  1085. *bufptr = av_malloc(*size + 1);
  1086. if (!*bufptr) {
  1087. av_log(NULL, AV_LOG_ERROR, "Could not allocate file buffer\n");
  1088. fclose(f);
  1089. return AVERROR(ENOMEM);
  1090. }
  1091. ret = fread(*bufptr, 1, *size, f);
  1092. if (ret < *size) {
  1093. av_free(*bufptr);
  1094. if (ferror(f)) {
  1095. av_log(NULL, AV_LOG_ERROR, "Error while reading file '%s': %s\n",
  1096. filename, strerror(errno));
  1097. ret = AVERROR(errno);
  1098. } else
  1099. ret = AVERROR_EOF;
  1100. } else {
  1101. ret = 0;
  1102. (*bufptr)[*size++] = '\0';
  1103. }
  1104. fclose(f);
  1105. return ret;
  1106. }
  1107. FILE *get_preset_file(char *filename, size_t filename_size,
  1108. const char *preset_name, int is_path,
  1109. const char *codec_name)
  1110. {
  1111. FILE *f = NULL;
  1112. int i;
  1113. const char *base[3] = { getenv("FFMPEG_DATADIR"),
  1114. getenv("HOME"),
  1115. FFMPEG_DATADIR, };
  1116. if (is_path) {
  1117. av_strlcpy(filename, preset_name, filename_size);
  1118. f = fopen(filename, "r");
  1119. } else {
  1120. #ifdef _WIN32
  1121. char datadir[MAX_PATH], *ls;
  1122. base[2] = NULL;
  1123. if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
  1124. {
  1125. for (ls = datadir; ls < datadir + strlen(datadir); ls++)
  1126. if (*ls == '\\') *ls = '/';
  1127. if (ls = strrchr(datadir, '/'))
  1128. {
  1129. *ls = 0;
  1130. strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
  1131. base[2] = datadir;
  1132. }
  1133. }
  1134. #endif
  1135. for (i = 0; i < 3 && !f; i++) {
  1136. if (!base[i])
  1137. continue;
  1138. snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i],
  1139. i != 1 ? "" : "/.ffmpeg", preset_name);
  1140. f = fopen(filename, "r");
  1141. if (!f && codec_name) {
  1142. snprintf(filename, filename_size,
  1143. "%s%s/%s-%s.ffpreset",
  1144. base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
  1145. preset_name);
  1146. f = fopen(filename, "r");
  1147. }
  1148. }
  1149. }
  1150. return f;
  1151. }
  1152. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
  1153. {
  1154. int ret = avformat_match_stream_specifier(s, st, spec);
  1155. if (ret < 0)
  1156. av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
  1157. return ret;
  1158. }
  1159. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  1160. AVFormatContext *s, AVStream *st, AVCodec *codec)
  1161. {
  1162. AVDictionary *ret = NULL;
  1163. AVDictionaryEntry *t = NULL;
  1164. int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM
  1165. : AV_OPT_FLAG_DECODING_PARAM;
  1166. char prefix = 0;
  1167. const AVClass *cc = avcodec_get_class();
  1168. if (!codec)
  1169. codec = s->oformat ? avcodec_find_encoder(codec_id)
  1170. : avcodec_find_decoder(codec_id);
  1171. if (!codec)
  1172. return NULL;
  1173. switch (codec->type) {
  1174. case AVMEDIA_TYPE_VIDEO:
  1175. prefix = 'v';
  1176. flags |= AV_OPT_FLAG_VIDEO_PARAM;
  1177. break;
  1178. case AVMEDIA_TYPE_AUDIO:
  1179. prefix = 'a';
  1180. flags |= AV_OPT_FLAG_AUDIO_PARAM;
  1181. break;
  1182. case AVMEDIA_TYPE_SUBTITLE:
  1183. prefix = 's';
  1184. flags |= AV_OPT_FLAG_SUBTITLE_PARAM;
  1185. break;
  1186. }
  1187. while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
  1188. char *p = strchr(t->key, ':');
  1189. /* check stream specification in opt name */
  1190. if (p)
  1191. switch (check_stream_specifier(s, st, p + 1)) {
  1192. case 1: *p = 0; break;
  1193. case 0: continue;
  1194. default: return NULL;
  1195. }
  1196. if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
  1197. (codec && codec->priv_class &&
  1198. av_opt_find(&codec->priv_class, t->key, NULL, flags,
  1199. AV_OPT_SEARCH_FAKE_OBJ)))
  1200. av_dict_set(&ret, t->key, t->value, 0);
  1201. else if (t->key[0] == prefix &&
  1202. av_opt_find(&cc, t->key + 1, NULL, flags,
  1203. AV_OPT_SEARCH_FAKE_OBJ))
  1204. av_dict_set(&ret, t->key + 1, t->value, 0);
  1205. if (p)
  1206. *p = ':';
  1207. }
  1208. return ret;
  1209. }
  1210. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  1211. AVDictionary *codec_opts)
  1212. {
  1213. int i;
  1214. AVDictionary **opts;
  1215. if (!s->nb_streams)
  1216. return NULL;
  1217. opts = av_mallocz(s->nb_streams * sizeof(*opts));
  1218. if (!opts) {
  1219. av_log(NULL, AV_LOG_ERROR,
  1220. "Could not alloc memory for stream options.\n");
  1221. return NULL;
  1222. }
  1223. for (i = 0; i < s->nb_streams; i++)
  1224. opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id,
  1225. s, s->streams[i], NULL);
  1226. return opts;
  1227. }
  1228. void *grow_array(void *array, int elem_size, int *size, int new_size)
  1229. {
  1230. if (new_size >= INT_MAX / elem_size) {
  1231. av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
  1232. exit_program(1);
  1233. }
  1234. if (*size < new_size) {
  1235. uint8_t *tmp = av_realloc(array, new_size*elem_size);
  1236. if (!tmp) {
  1237. av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
  1238. exit_program(1);
  1239. }
  1240. memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
  1241. *size = new_size;
  1242. return tmp;
  1243. }
  1244. return array;
  1245. }
  1246. static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
  1247. {
  1248. FrameBuffer *buf = av_mallocz(sizeof(*buf));
  1249. int i, ret;
  1250. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  1251. int h_chroma_shift, v_chroma_shift;
  1252. int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
  1253. int w = s->width, h = s->height;
  1254. if (!buf)
  1255. return AVERROR(ENOMEM);
  1256. avcodec_align_dimensions(s, &w, &h);
  1257. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1258. w += 2*edge;
  1259. h += 2*edge;
  1260. }
  1261. if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
  1262. s->pix_fmt, 32)) < 0) {
  1263. av_freep(&buf);
  1264. av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
  1265. return ret;
  1266. }
  1267. /* XXX this shouldn't be needed, but some tests break without this line
  1268. * those decoders are buggy and need to be fixed.
  1269. * the following tests fail:
  1270. * cdgraphics, ansi, aasc, fraps-v1, qtrle-1bit
  1271. */
  1272. memset(buf->base[0], 128, ret);
  1273. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1274. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1275. const int h_shift = i==0 ? 0 : h_chroma_shift;
  1276. const int v_shift = i==0 ? 0 : v_chroma_shift;
  1277. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
  1278. buf->data[i] = buf->base[i];
  1279. else
  1280. buf->data[i] = buf->base[i] +
  1281. FFALIGN((buf->linesize[i]*edge >> v_shift) +
  1282. (pixel_size*edge >> h_shift), 32);
  1283. }
  1284. buf->w = s->width;
  1285. buf->h = s->height;
  1286. buf->pix_fmt = s->pix_fmt;
  1287. buf->pool = pool;
  1288. *pbuf = buf;
  1289. return 0;
  1290. }
  1291. int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
  1292. {
  1293. FrameBuffer **pool = s->opaque;
  1294. FrameBuffer *buf;
  1295. int ret, i;
  1296. if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
  1297. av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
  1298. return -1;
  1299. }
  1300. if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
  1301. return ret;
  1302. buf = *pool;
  1303. *pool = buf->next;
  1304. buf->next = NULL;
  1305. if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
  1306. av_freep(&buf->base[0]);
  1307. av_free(buf);
  1308. if ((ret = alloc_buffer(pool, s, &buf)) < 0)
  1309. return ret;
  1310. }
  1311. av_assert0(!buf->refcount);
  1312. buf->refcount++;
  1313. frame->opaque = buf;
  1314. frame->type = FF_BUFFER_TYPE_USER;
  1315. frame->extended_data = frame->data;
  1316. frame->pkt_pts = s->pkt ? s->pkt->pts : AV_NOPTS_VALUE;
  1317. frame->width = buf->w;
  1318. frame->height = buf->h;
  1319. frame->format = buf->pix_fmt;
  1320. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  1321. for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
  1322. frame->base[i] = buf->base[i]; // XXX h264.c uses base though it shouldn't
  1323. frame->data[i] = buf->data[i];
  1324. frame->linesize[i] = buf->linesize[i];
  1325. }
  1326. return 0;
  1327. }
  1328. static void unref_buffer(FrameBuffer *buf)
  1329. {
  1330. FrameBuffer **pool = buf->pool;
  1331. av_assert0(buf->refcount > 0);
  1332. buf->refcount--;
  1333. if (!buf->refcount) {
  1334. FrameBuffer *tmp;
  1335. for(tmp= *pool; tmp; tmp= tmp->next)
  1336. av_assert1(tmp != buf);
  1337. buf->next = *pool;
  1338. *pool = buf;
  1339. }
  1340. }
  1341. void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
  1342. {
  1343. FrameBuffer *buf = frame->opaque;
  1344. int i;
  1345. if(frame->type!=FF_BUFFER_TYPE_USER) {
  1346. avcodec_default_release_buffer(s, frame);
  1347. return;
  1348. }
  1349. for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
  1350. frame->data[i] = NULL;
  1351. unref_buffer(buf);
  1352. }
  1353. void filter_release_buffer(AVFilterBuffer *fb)
  1354. {
  1355. FrameBuffer *buf = fb->priv;
  1356. av_free(fb);
  1357. unref_buffer(buf);
  1358. }
  1359. void free_buffer_pool(FrameBuffer **pool)
  1360. {
  1361. FrameBuffer *buf = *pool;
  1362. while (buf) {
  1363. *pool = buf->next;
  1364. av_freep(&buf->base[0]);
  1365. av_free(buf);
  1366. buf = *pool;
  1367. }
  1368. }