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.

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