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.

1385 lines
43KB

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