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.

1700 lines
59KB

  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #define _SVID_SOURCE // needed for MAP_ANONYMOUS
  22. #include <assert.h>
  23. #include <inttypes.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #if HAVE_SYS_MMAN_H
  28. #include <sys/mman.h>
  29. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  30. #define MAP_ANONYMOUS MAP_ANON
  31. #endif
  32. #endif
  33. #if HAVE_VIRTUALALLOC
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #endif
  37. #include "libavutil/avutil.h"
  38. #include "libavutil/bswap.h"
  39. #include "libavutil/cpu.h"
  40. #include "libavutil/intreadwrite.h"
  41. #include "libavutil/mathematics.h"
  42. #include "libavutil/opt.h"
  43. #include "libavutil/pixdesc.h"
  44. #include "libavutil/x86_cpu.h"
  45. #include "rgb2rgb.h"
  46. #include "swscale.h"
  47. #include "swscale_internal.h"
  48. unsigned swscale_version(void)
  49. {
  50. return LIBSWSCALE_VERSION_INT;
  51. }
  52. const char *swscale_configuration(void)
  53. {
  54. return LIBAV_CONFIGURATION;
  55. }
  56. const char *swscale_license(void)
  57. {
  58. #define LICENSE_PREFIX "libswscale license: "
  59. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  60. }
  61. #define RET 0xC3 // near return opcode for x86
  62. typedef struct FormatEntry {
  63. int is_supported_in, is_supported_out;
  64. } FormatEntry;
  65. static const FormatEntry format_entries[PIX_FMT_NB] = {
  66. [PIX_FMT_YUV420P] = { 1, 1 },
  67. [PIX_FMT_YUYV422] = { 1, 1 },
  68. [PIX_FMT_RGB24] = { 1, 1 },
  69. [PIX_FMT_BGR24] = { 1, 1 },
  70. [PIX_FMT_YUV422P] = { 1, 1 },
  71. [PIX_FMT_YUV444P] = { 1, 1 },
  72. [PIX_FMT_YUV410P] = { 1, 1 },
  73. [PIX_FMT_YUV411P] = { 1, 1 },
  74. [PIX_FMT_GRAY8] = { 1, 1 },
  75. [PIX_FMT_MONOWHITE] = { 1, 1 },
  76. [PIX_FMT_MONOBLACK] = { 1, 1 },
  77. [PIX_FMT_PAL8] = { 1, 0 },
  78. [PIX_FMT_YUVJ420P] = { 1, 1 },
  79. [PIX_FMT_YUVJ422P] = { 1, 1 },
  80. [PIX_FMT_YUVJ444P] = { 1, 1 },
  81. [PIX_FMT_UYVY422] = { 1, 1 },
  82. [PIX_FMT_UYYVYY411] = { 0, 0 },
  83. [PIX_FMT_BGR8] = { 1, 1 },
  84. [PIX_FMT_BGR4] = { 0, 1 },
  85. [PIX_FMT_BGR4_BYTE] = { 1, 1 },
  86. [PIX_FMT_RGB8] = { 1, 1 },
  87. [PIX_FMT_RGB4] = { 0, 1 },
  88. [PIX_FMT_RGB4_BYTE] = { 1, 1 },
  89. [PIX_FMT_NV12] = { 1, 1 },
  90. [PIX_FMT_NV21] = { 1, 1 },
  91. [PIX_FMT_ARGB] = { 1, 1 },
  92. [PIX_FMT_RGBA] = { 1, 1 },
  93. [PIX_FMT_ABGR] = { 1, 1 },
  94. [PIX_FMT_BGRA] = { 1, 1 },
  95. [PIX_FMT_GRAY16BE] = { 1, 1 },
  96. [PIX_FMT_GRAY16LE] = { 1, 1 },
  97. [PIX_FMT_YUV440P] = { 1, 1 },
  98. [PIX_FMT_YUVJ440P] = { 1, 1 },
  99. [PIX_FMT_YUVA420P] = { 1, 1 },
  100. [PIX_FMT_RGB48BE] = { 1, 1 },
  101. [PIX_FMT_RGB48LE] = { 1, 1 },
  102. [PIX_FMT_RGB565BE] = { 1, 1 },
  103. [PIX_FMT_RGB565LE] = { 1, 1 },
  104. [PIX_FMT_RGB555BE] = { 1, 1 },
  105. [PIX_FMT_RGB555LE] = { 1, 1 },
  106. [PIX_FMT_BGR565BE] = { 1, 1 },
  107. [PIX_FMT_BGR565LE] = { 1, 1 },
  108. [PIX_FMT_BGR555BE] = { 1, 1 },
  109. [PIX_FMT_BGR555LE] = { 1, 1 },
  110. [PIX_FMT_YUV420P16LE] = { 1, 1 },
  111. [PIX_FMT_YUV420P16BE] = { 1, 1 },
  112. [PIX_FMT_YUV422P16LE] = { 1, 1 },
  113. [PIX_FMT_YUV422P16BE] = { 1, 1 },
  114. [PIX_FMT_YUV444P16LE] = { 1, 1 },
  115. [PIX_FMT_YUV444P16BE] = { 1, 1 },
  116. [PIX_FMT_RGB444LE] = { 1, 1 },
  117. [PIX_FMT_RGB444BE] = { 1, 1 },
  118. [PIX_FMT_BGR444LE] = { 1, 1 },
  119. [PIX_FMT_BGR444BE] = { 1, 1 },
  120. [PIX_FMT_Y400A] = { 1, 0 },
  121. [PIX_FMT_BGR48BE] = { 1, 1 },
  122. [PIX_FMT_BGR48LE] = { 1, 1 },
  123. [PIX_FMT_YUV420P9BE] = { 1, 1 },
  124. [PIX_FMT_YUV420P9LE] = { 1, 1 },
  125. [PIX_FMT_YUV420P10BE] = { 1, 1 },
  126. [PIX_FMT_YUV420P10LE] = { 1, 1 },
  127. [PIX_FMT_YUV422P9BE] = { 1, 1 },
  128. [PIX_FMT_YUV422P9LE] = { 1, 1 },
  129. [PIX_FMT_YUV422P10BE] = { 1, 1 },
  130. [PIX_FMT_YUV422P10LE] = { 1, 1 },
  131. [PIX_FMT_YUV444P9BE] = { 1, 1 },
  132. [PIX_FMT_YUV444P9LE] = { 1, 1 },
  133. [PIX_FMT_YUV444P10BE] = { 1, 1 },
  134. [PIX_FMT_YUV444P10LE] = { 1, 1 },
  135. [PIX_FMT_GBRP] = { 1, 0 },
  136. [PIX_FMT_GBRP9LE] = { 1, 0 },
  137. [PIX_FMT_GBRP9BE] = { 1, 0 },
  138. [PIX_FMT_GBRP10LE] = { 1, 0 },
  139. [PIX_FMT_GBRP10BE] = { 1, 0 },
  140. [PIX_FMT_GBRP16LE] = { 1, 0 },
  141. [PIX_FMT_GBRP16BE] = { 1, 0 },
  142. };
  143. int sws_isSupportedInput(enum PixelFormat pix_fmt)
  144. {
  145. return (unsigned)pix_fmt < PIX_FMT_NB ?
  146. format_entries[pix_fmt].is_supported_in : 0;
  147. }
  148. int sws_isSupportedOutput(enum PixelFormat pix_fmt)
  149. {
  150. return (unsigned)pix_fmt < PIX_FMT_NB ?
  151. format_entries[pix_fmt].is_supported_out : 0;
  152. }
  153. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  154. const char *sws_format_name(enum PixelFormat format)
  155. {
  156. if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
  157. return av_pix_fmt_descriptors[format].name;
  158. else
  159. return "Unknown format";
  160. }
  161. static double getSplineCoeff(double a, double b, double c, double d,
  162. double dist)
  163. {
  164. if (dist <= 1.0)
  165. return ((d * dist + c) * dist + b) * dist + a;
  166. else
  167. return getSplineCoeff(0.0,
  168. b + 2.0 * c + 3.0 * d,
  169. c + 3.0 * d,
  170. -b - 3.0 * c - 6.0 * d,
  171. dist - 1.0);
  172. }
  173. static int initFilter(int16_t **outFilter, int32_t **filterPos,
  174. int *outFilterSize, int xInc, int srcW, int dstW,
  175. int filterAlign, int one, int flags, int cpu_flags,
  176. SwsVector *srcFilter, SwsVector *dstFilter,
  177. double param[2], int is_horizontal)
  178. {
  179. int i;
  180. int filterSize;
  181. int filter2Size;
  182. int minFilterSize;
  183. int64_t *filter = NULL;
  184. int64_t *filter2 = NULL;
  185. const int64_t fone = 1LL << 54;
  186. int ret = -1;
  187. emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
  188. // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
  189. FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
  190. if (FFABS(xInc - 0x10000) < 10) { // unscaled
  191. int i;
  192. filterSize = 1;
  193. FF_ALLOCZ_OR_GOTO(NULL, filter,
  194. dstW * sizeof(*filter) * filterSize, fail);
  195. for (i = 0; i < dstW; i++) {
  196. filter[i * filterSize] = fone;
  197. (*filterPos)[i] = i;
  198. }
  199. } else if (flags & SWS_POINT) { // lame looking point sampling mode
  200. int i;
  201. int xDstInSrc;
  202. filterSize = 1;
  203. FF_ALLOC_OR_GOTO(NULL, filter,
  204. dstW * sizeof(*filter) * filterSize, fail);
  205. xDstInSrc = xInc / 2 - 0x8000;
  206. for (i = 0; i < dstW; i++) {
  207. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  208. (*filterPos)[i] = xx;
  209. filter[i] = fone;
  210. xDstInSrc += xInc;
  211. }
  212. } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
  213. (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
  214. int i;
  215. int xDstInSrc;
  216. filterSize = 2;
  217. FF_ALLOC_OR_GOTO(NULL, filter,
  218. dstW * sizeof(*filter) * filterSize, fail);
  219. xDstInSrc = xInc / 2 - 0x8000;
  220. for (i = 0; i < dstW; i++) {
  221. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  222. int j;
  223. (*filterPos)[i] = xx;
  224. // bilinear upscale / linear interpolate / area averaging
  225. for (j = 0; j < filterSize; j++) {
  226. int64_t coeff = fone - FFABS((xx << 16) - xDstInSrc) *
  227. (fone >> 16);
  228. if (coeff < 0)
  229. coeff = 0;
  230. filter[i * filterSize + j] = coeff;
  231. xx++;
  232. }
  233. xDstInSrc += xInc;
  234. }
  235. } else {
  236. int64_t xDstInSrc;
  237. int sizeFactor;
  238. if (flags & SWS_BICUBIC)
  239. sizeFactor = 4;
  240. else if (flags & SWS_X)
  241. sizeFactor = 8;
  242. else if (flags & SWS_AREA)
  243. sizeFactor = 1; // downscale only, for upscale it is bilinear
  244. else if (flags & SWS_GAUSS)
  245. sizeFactor = 8; // infinite ;)
  246. else if (flags & SWS_LANCZOS)
  247. sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
  248. else if (flags & SWS_SINC)
  249. sizeFactor = 20; // infinite ;)
  250. else if (flags & SWS_SPLINE)
  251. sizeFactor = 20; // infinite ;)
  252. else if (flags & SWS_BILINEAR)
  253. sizeFactor = 2;
  254. else {
  255. sizeFactor = 0; // GCC warning killer
  256. assert(0);
  257. }
  258. if (xInc <= 1 << 16)
  259. filterSize = 1 + sizeFactor; // upscale
  260. else
  261. filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
  262. filterSize = FFMIN(filterSize, srcW - 2);
  263. filterSize = FFMAX(filterSize, 1);
  264. FF_ALLOC_OR_GOTO(NULL, filter,
  265. dstW * sizeof(*filter) * filterSize, fail);
  266. xDstInSrc = xInc - 0x10000;
  267. for (i = 0; i < dstW; i++) {
  268. int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
  269. int j;
  270. (*filterPos)[i] = xx;
  271. for (j = 0; j < filterSize; j++) {
  272. int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
  273. double floatd;
  274. int64_t coeff;
  275. if (xInc > 1 << 16)
  276. d = d * dstW / srcW;
  277. floatd = d * (1.0 / (1 << 30));
  278. if (flags & SWS_BICUBIC) {
  279. int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
  280. int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
  281. if (d >= 1LL << 31) {
  282. coeff = 0.0;
  283. } else {
  284. int64_t dd = (d * d) >> 30;
  285. int64_t ddd = (dd * d) >> 30;
  286. if (d < 1LL << 30)
  287. coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
  288. (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
  289. (6 * (1 << 24) - 2 * B) * (1 << 30);
  290. else
  291. coeff = (-B - 6 * C) * ddd +
  292. (6 * B + 30 * C) * dd +
  293. (-12 * B - 48 * C) * d +
  294. (8 * B + 24 * C) * (1 << 30);
  295. }
  296. coeff *= fone >> (30 + 24);
  297. }
  298. #if 0
  299. else if (flags & SWS_X) {
  300. double p = param ? param * 0.01 : 0.3;
  301. coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
  302. coeff *= pow(2.0, -p * d * d);
  303. }
  304. #endif
  305. else if (flags & SWS_X) {
  306. double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  307. double c;
  308. if (floatd < 1.0)
  309. c = cos(floatd * M_PI);
  310. else
  311. c = -1.0;
  312. if (c < 0.0)
  313. c = -pow(-c, A);
  314. else
  315. c = pow(c, A);
  316. coeff = (c * 0.5 + 0.5) * fone;
  317. } else if (flags & SWS_AREA) {
  318. int64_t d2 = d - (1 << 29);
  319. if (d2 * xInc < -(1LL << (29 + 16)))
  320. coeff = 1.0 * (1LL << (30 + 16));
  321. else if (d2 * xInc < (1LL << (29 + 16)))
  322. coeff = -d2 * xInc + (1LL << (29 + 16));
  323. else
  324. coeff = 0.0;
  325. coeff *= fone >> (30 + 16);
  326. } else if (flags & SWS_GAUSS) {
  327. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  328. coeff = (pow(2.0, -p * floatd * floatd)) * fone;
  329. } else if (flags & SWS_SINC) {
  330. coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
  331. } else if (flags & SWS_LANCZOS) {
  332. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  333. coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
  334. (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
  335. if (floatd > p)
  336. coeff = 0;
  337. } else if (flags & SWS_BILINEAR) {
  338. coeff = (1 << 30) - d;
  339. if (coeff < 0)
  340. coeff = 0;
  341. coeff *= fone >> 30;
  342. } else if (flags & SWS_SPLINE) {
  343. double p = -2.196152422706632;
  344. coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
  345. } else {
  346. coeff = 0.0; // GCC warning killer
  347. assert(0);
  348. }
  349. filter[i * filterSize + j] = coeff;
  350. xx++;
  351. }
  352. xDstInSrc += 2 * xInc;
  353. }
  354. }
  355. /* apply src & dst Filter to filter -> filter2
  356. * av_free(filter);
  357. */
  358. assert(filterSize > 0);
  359. filter2Size = filterSize;
  360. if (srcFilter)
  361. filter2Size += srcFilter->length - 1;
  362. if (dstFilter)
  363. filter2Size += dstFilter->length - 1;
  364. assert(filter2Size > 0);
  365. FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
  366. for (i = 0; i < dstW; i++) {
  367. int j, k;
  368. if (srcFilter) {
  369. for (k = 0; k < srcFilter->length; k++) {
  370. for (j = 0; j < filterSize; j++)
  371. filter2[i * filter2Size + k + j] +=
  372. srcFilter->coeff[k] * filter[i * filterSize + j];
  373. }
  374. } else {
  375. for (j = 0; j < filterSize; j++)
  376. filter2[i * filter2Size + j] = filter[i * filterSize + j];
  377. }
  378. // FIXME dstFilter
  379. (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
  380. }
  381. av_freep(&filter);
  382. /* try to reduce the filter-size (step1 find size and shift left) */
  383. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  384. minFilterSize = 0;
  385. for (i = dstW - 1; i >= 0; i--) {
  386. int min = filter2Size;
  387. int j;
  388. int64_t cutOff = 0.0;
  389. /* get rid of near zero elements on the left by shifting left */
  390. for (j = 0; j < filter2Size; j++) {
  391. int k;
  392. cutOff += FFABS(filter2[i * filter2Size]);
  393. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  394. break;
  395. /* preserve monotonicity because the core can't handle the
  396. * filter otherwise */
  397. if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
  398. break;
  399. // move filter coefficients left
  400. for (k = 1; k < filter2Size; k++)
  401. filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
  402. filter2[i * filter2Size + k - 1] = 0;
  403. (*filterPos)[i]++;
  404. }
  405. cutOff = 0;
  406. /* count near zeros on the right */
  407. for (j = filter2Size - 1; j > 0; j--) {
  408. cutOff += FFABS(filter2[i * filter2Size + j]);
  409. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  410. break;
  411. min--;
  412. }
  413. if (min > minFilterSize)
  414. minFilterSize = min;
  415. }
  416. if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
  417. // we can handle the special case 4, so we don't want to go the full 8
  418. if (minFilterSize < 5)
  419. filterAlign = 4;
  420. /* We really don't want to waste our time doing useless computation, so
  421. * fall back on the scalar C code for very small filters.
  422. * Vectorizing is worth it only if you have a decent-sized vector. */
  423. if (minFilterSize < 3)
  424. filterAlign = 1;
  425. }
  426. if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  427. // special case for unscaled vertical filtering
  428. if (minFilterSize == 1 && filterAlign == 2)
  429. filterAlign = 1;
  430. }
  431. assert(minFilterSize > 0);
  432. filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
  433. assert(filterSize > 0);
  434. filter = av_malloc(filterSize * dstW * sizeof(*filter));
  435. if (filterSize >= MAX_FILTER_SIZE * 16 /
  436. ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
  437. goto fail;
  438. *outFilterSize = filterSize;
  439. if (flags & SWS_PRINT_INFO)
  440. av_log(NULL, AV_LOG_VERBOSE,
  441. "SwScaler: reducing / aligning filtersize %d -> %d\n",
  442. filter2Size, filterSize);
  443. /* try to reduce the filter-size (step2 reduce it) */
  444. for (i = 0; i < dstW; i++) {
  445. int j;
  446. for (j = 0; j < filterSize; j++) {
  447. if (j >= filter2Size)
  448. filter[i * filterSize + j] = 0;
  449. else
  450. filter[i * filterSize + j] = filter2[i * filter2Size + j];
  451. if ((flags & SWS_BITEXACT) && j >= minFilterSize)
  452. filter[i * filterSize + j] = 0;
  453. }
  454. }
  455. // FIXME try to align filterPos if possible
  456. // fix borders
  457. if (is_horizontal) {
  458. for (i = 0; i < dstW; i++) {
  459. int j;
  460. if ((*filterPos)[i] < 0) {
  461. // move filter coefficients left to compensate for filterPos
  462. for (j = 1; j < filterSize; j++) {
  463. int left = FFMAX(j + (*filterPos)[i], 0);
  464. filter[i * filterSize + left] += filter[i * filterSize + j];
  465. filter[i * filterSize + j] = 0;
  466. }
  467. (*filterPos)[i] = 0;
  468. }
  469. if ((*filterPos)[i] + filterSize > srcW) {
  470. int shift = (*filterPos)[i] + filterSize - srcW;
  471. // move filter coefficients right to compensate for filterPos
  472. for (j = filterSize - 2; j >= 0; j--) {
  473. int right = FFMIN(j + shift, filterSize - 1);
  474. filter[i * filterSize + right] += filter[i * filterSize + j];
  475. filter[i * filterSize + j] = 0;
  476. }
  477. (*filterPos)[i] = srcW - filterSize;
  478. }
  479. }
  480. }
  481. // Note the +1 is for the MMX scaler which reads over the end
  482. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  483. FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
  484. *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
  485. /* normalize & store in outFilter */
  486. for (i = 0; i < dstW; i++) {
  487. int j;
  488. int64_t error = 0;
  489. int64_t sum = 0;
  490. for (j = 0; j < filterSize; j++) {
  491. sum += filter[i * filterSize + j];
  492. }
  493. sum = (sum + one / 2) / one;
  494. for (j = 0; j < *outFilterSize; j++) {
  495. int64_t v = filter[i * filterSize + j] + error;
  496. int intV = ROUNDED_DIV(v, sum);
  497. (*outFilter)[i * (*outFilterSize) + j] = intV;
  498. error = v - intV * sum;
  499. }
  500. }
  501. (*filterPos)[dstW + 0] =
  502. (*filterPos)[dstW + 1] =
  503. (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
  504. * read over the end */
  505. for (i = 0; i < *outFilterSize; i++) {
  506. int k = (dstW - 1) * (*outFilterSize) + i;
  507. (*outFilter)[k + 1 * (*outFilterSize)] =
  508. (*outFilter)[k + 2 * (*outFilterSize)] =
  509. (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
  510. }
  511. ret = 0;
  512. fail:
  513. av_free(filter);
  514. av_free(filter2);
  515. return ret;
  516. }
  517. #if HAVE_MMX2
  518. static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode,
  519. int16_t *filter, int32_t *filterPos, int numSplits)
  520. {
  521. uint8_t *fragmentA;
  522. x86_reg imm8OfPShufW1A;
  523. x86_reg imm8OfPShufW2A;
  524. x86_reg fragmentLengthA;
  525. uint8_t *fragmentB;
  526. x86_reg imm8OfPShufW1B;
  527. x86_reg imm8OfPShufW2B;
  528. x86_reg fragmentLengthB;
  529. int fragmentPos;
  530. int xpos, i;
  531. // create an optimized horizontal scaling routine
  532. /* This scaler is made of runtime-generated MMX2 code using specially tuned
  533. * pshufw instructions. For every four output pixels, if four input pixels
  534. * are enough for the fast bilinear scaling, then a chunk of fragmentB is
  535. * used. If five input pixels are needed, then a chunk of fragmentA is used.
  536. */
  537. // code fragment
  538. __asm__ volatile (
  539. "jmp 9f \n\t"
  540. // Begin
  541. "0: \n\t"
  542. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  543. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  544. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  545. "punpcklbw %%mm7, %%mm1 \n\t"
  546. "punpcklbw %%mm7, %%mm0 \n\t"
  547. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  548. "1: \n\t"
  549. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  550. "2: \n\t"
  551. "psubw %%mm1, %%mm0 \n\t"
  552. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  553. "pmullw %%mm3, %%mm0 \n\t"
  554. "psllw $7, %%mm1 \n\t"
  555. "paddw %%mm1, %%mm0 \n\t"
  556. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  557. "add $8, %%"REG_a" \n\t"
  558. // End
  559. "9: \n\t"
  560. // "int $3 \n\t"
  561. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  562. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  563. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  564. "dec %1 \n\t"
  565. "dec %2 \n\t"
  566. "sub %0, %1 \n\t"
  567. "sub %0, %2 \n\t"
  568. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  569. "sub %0, %3 \n\t"
  570. : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  571. "=r" (fragmentLengthA)
  572. );
  573. __asm__ volatile (
  574. "jmp 9f \n\t"
  575. // Begin
  576. "0: \n\t"
  577. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  578. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  579. "punpcklbw %%mm7, %%mm0 \n\t"
  580. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  581. "1: \n\t"
  582. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  583. "2: \n\t"
  584. "psubw %%mm1, %%mm0 \n\t"
  585. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  586. "pmullw %%mm3, %%mm0 \n\t"
  587. "psllw $7, %%mm1 \n\t"
  588. "paddw %%mm1, %%mm0 \n\t"
  589. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  590. "add $8, %%"REG_a" \n\t"
  591. // End
  592. "9: \n\t"
  593. // "int $3 \n\t"
  594. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  595. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  596. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  597. "dec %1 \n\t"
  598. "dec %2 \n\t"
  599. "sub %0, %1 \n\t"
  600. "sub %0, %2 \n\t"
  601. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  602. "sub %0, %3 \n\t"
  603. : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  604. "=r" (fragmentLengthB)
  605. );
  606. xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
  607. fragmentPos = 0;
  608. for (i = 0; i < dstW / numSplits; i++) {
  609. int xx = xpos >> 16;
  610. if ((i & 3) == 0) {
  611. int a = 0;
  612. int b = ((xpos + xInc) >> 16) - xx;
  613. int c = ((xpos + xInc * 2) >> 16) - xx;
  614. int d = ((xpos + xInc * 3) >> 16) - xx;
  615. int inc = (d + 1 < 4);
  616. uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
  617. x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
  618. x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
  619. x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
  620. int maxShift = 3 - (d + inc);
  621. int shift = 0;
  622. if (filterCode) {
  623. filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
  624. filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
  625. filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
  626. filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
  627. filterPos[i / 2] = xx;
  628. memcpy(filterCode + fragmentPos, fragment, fragmentLength);
  629. filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
  630. ((b + inc) << 2) |
  631. ((c + inc) << 4) |
  632. ((d + inc) << 6);
  633. filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
  634. (c << 4) |
  635. (d << 6);
  636. if (i + 4 - inc >= dstW)
  637. shift = maxShift; // avoid overread
  638. else if ((filterPos[i / 2] & 3) <= maxShift)
  639. shift = filterPos[i / 2] & 3; // align
  640. if (shift && i >= shift) {
  641. filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
  642. filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
  643. filterPos[i / 2] -= shift;
  644. }
  645. }
  646. fragmentPos += fragmentLength;
  647. if (filterCode)
  648. filterCode[fragmentPos] = RET;
  649. }
  650. xpos += xInc;
  651. }
  652. if (filterCode)
  653. filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
  654. return fragmentPos + 1;
  655. }
  656. #endif /* HAVE_MMX2 */
  657. static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
  658. {
  659. *h = av_pix_fmt_descriptors[format].log2_chroma_w;
  660. *v = av_pix_fmt_descriptors[format].log2_chroma_h;
  661. }
  662. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  663. int srcRange, const int table[4], int dstRange,
  664. int brightness, int contrast, int saturation)
  665. {
  666. memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
  667. memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
  668. c->brightness = brightness;
  669. c->contrast = contrast;
  670. c->saturation = saturation;
  671. c->srcRange = srcRange;
  672. c->dstRange = dstRange;
  673. if (isYUV(c->dstFormat) || isGray(c->dstFormat))
  674. return -1;
  675. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
  676. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
  677. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
  678. contrast, saturation);
  679. // FIXME factorize
  680. if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
  681. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
  682. contrast, saturation);
  683. return 0;
  684. }
  685. int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
  686. int *srcRange, int **table, int *dstRange,
  687. int *brightness, int *contrast, int *saturation)
  688. {
  689. if (isYUV(c->dstFormat) || isGray(c->dstFormat))
  690. return -1;
  691. *inv_table = c->srcColorspaceTable;
  692. *table = c->dstColorspaceTable;
  693. *srcRange = c->srcRange;
  694. *dstRange = c->dstRange;
  695. *brightness = c->brightness;
  696. *contrast = c->contrast;
  697. *saturation = c->saturation;
  698. return 0;
  699. }
  700. static int handle_jpeg(enum PixelFormat *format)
  701. {
  702. switch (*format) {
  703. case PIX_FMT_YUVJ420P:
  704. *format = PIX_FMT_YUV420P;
  705. return 1;
  706. case PIX_FMT_YUVJ422P:
  707. *format = PIX_FMT_YUV422P;
  708. return 1;
  709. case PIX_FMT_YUVJ444P:
  710. *format = PIX_FMT_YUV444P;
  711. return 1;
  712. case PIX_FMT_YUVJ440P:
  713. *format = PIX_FMT_YUV440P;
  714. return 1;
  715. default:
  716. return 0;
  717. }
  718. }
  719. SwsContext *sws_alloc_context(void)
  720. {
  721. SwsContext *c = av_mallocz(sizeof(SwsContext));
  722. c->av_class = &sws_context_class;
  723. av_opt_set_defaults(c);
  724. return c;
  725. }
  726. int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
  727. {
  728. int i;
  729. int usesVFilter, usesHFilter;
  730. int unscaled;
  731. SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
  732. int srcW = c->srcW;
  733. int srcH = c->srcH;
  734. int dstW = c->dstW;
  735. int dstH = c->dstH;
  736. int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16);
  737. int dst_stride_px = dst_stride >> 1;
  738. int flags, cpu_flags;
  739. enum PixelFormat srcFormat = c->srcFormat;
  740. enum PixelFormat dstFormat = c->dstFormat;
  741. cpu_flags = av_get_cpu_flags();
  742. flags = c->flags;
  743. emms_c();
  744. if (!rgb15to16)
  745. sws_rgb2rgb_init();
  746. unscaled = (srcW == dstW && srcH == dstH);
  747. if (!sws_isSupportedInput(srcFormat)) {
  748. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
  749. sws_format_name(srcFormat));
  750. return AVERROR(EINVAL);
  751. }
  752. if (!sws_isSupportedOutput(dstFormat)) {
  753. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
  754. sws_format_name(dstFormat));
  755. return AVERROR(EINVAL);
  756. }
  757. i = flags & (SWS_POINT |
  758. SWS_AREA |
  759. SWS_BILINEAR |
  760. SWS_FAST_BILINEAR |
  761. SWS_BICUBIC |
  762. SWS_X |
  763. SWS_GAUSS |
  764. SWS_LANCZOS |
  765. SWS_SINC |
  766. SWS_SPLINE |
  767. SWS_BICUBLIN);
  768. if (!i || (i & (i - 1))) {
  769. av_log(c, AV_LOG_ERROR,
  770. "Exactly one scaler algorithm must be chosen\n");
  771. return AVERROR(EINVAL);
  772. }
  773. /* sanity check */
  774. if (srcW < 4 || srcH < 1 || dstW < 8 || dstH < 1) {
  775. /* FIXME check if these are enough and try to lower them after
  776. * fixing the relevant parts of the code */
  777. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  778. srcW, srcH, dstW, dstH);
  779. return AVERROR(EINVAL);
  780. }
  781. if (!dstFilter)
  782. dstFilter = &dummyFilter;
  783. if (!srcFilter)
  784. srcFilter = &dummyFilter;
  785. c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
  786. c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
  787. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  788. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  789. c->vRounder = 4 * 0x0001000100010001ULL;
  790. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
  791. (srcFilter->chrV && srcFilter->chrV->length > 1) ||
  792. (dstFilter->lumV && dstFilter->lumV->length > 1) ||
  793. (dstFilter->chrV && dstFilter->chrV->length > 1);
  794. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
  795. (srcFilter->chrH && srcFilter->chrH->length > 1) ||
  796. (dstFilter->lumH && dstFilter->lumH->length > 1) ||
  797. (dstFilter->chrH && dstFilter->chrH->length > 1);
  798. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  799. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  800. /* reuse chroma for 2 pixels RGB/BGR unless user wants full
  801. * chroma interpolation */
  802. if (flags & SWS_FULL_CHR_H_INT &&
  803. isAnyRGB(dstFormat) &&
  804. dstFormat != PIX_FMT_RGBA &&
  805. dstFormat != PIX_FMT_ARGB &&
  806. dstFormat != PIX_FMT_BGRA &&
  807. dstFormat != PIX_FMT_ABGR &&
  808. dstFormat != PIX_FMT_RGB24 &&
  809. dstFormat != PIX_FMT_BGR24) {
  810. av_log(c, AV_LOG_ERROR,
  811. "full chroma interpolation for destination format '%s' not yet implemented\n",
  812. sws_format_name(dstFormat));
  813. flags &= ~SWS_FULL_CHR_H_INT;
  814. c->flags = flags;
  815. }
  816. if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
  817. c->chrDstHSubSample = 1;
  818. // drop some chroma lines if the user wants it
  819. c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
  820. SWS_SRC_V_CHR_DROP_SHIFT;
  821. c->chrSrcVSubSample += c->vChrDrop;
  822. /* drop every other pixel for chroma calculation unless user
  823. * wants full chroma */
  824. if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
  825. srcFormat != PIX_FMT_RGB8 && srcFormat != PIX_FMT_BGR8 &&
  826. srcFormat != PIX_FMT_RGB4 && srcFormat != PIX_FMT_BGR4 &&
  827. srcFormat != PIX_FMT_RGB4_BYTE && srcFormat != PIX_FMT_BGR4_BYTE &&
  828. ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
  829. (flags & SWS_FAST_BILINEAR)))
  830. c->chrSrcHSubSample = 1;
  831. // Note the -((-x)>>y) is so that we always round toward +inf.
  832. c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample);
  833. c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample);
  834. c->chrDstW = -((-dstW) >> c->chrDstHSubSample);
  835. c->chrDstH = -((-dstH) >> c->chrDstVSubSample);
  836. /* unscaled special cases */
  837. if (unscaled && !usesHFilter && !usesVFilter &&
  838. (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  839. ff_get_unscaled_swscale(c);
  840. if (c->swScale) {
  841. if (flags & SWS_PRINT_INFO)
  842. av_log(c, AV_LOG_INFO,
  843. "using unscaled %s -> %s special converter\n",
  844. sws_format_name(srcFormat), sws_format_name(dstFormat));
  845. return 0;
  846. }
  847. }
  848. c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
  849. if (c->srcBpc < 8)
  850. c->srcBpc = 8;
  851. c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
  852. if (c->dstBpc < 8)
  853. c->dstBpc = 8;
  854. if (c->dstBpc == 16)
  855. dst_stride <<= 1;
  856. FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
  857. (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
  858. fail);
  859. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 &&
  860. c->srcBpc == 8 && c->dstBpc <= 10) {
  861. c->canMMX2BeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
  862. (srcW & 15) == 0) ? 1 : 0;
  863. if (!c->canMMX2BeUsed && dstW >= srcW && (srcW & 15) == 0
  864. && (flags & SWS_FAST_BILINEAR)) {
  865. if (flags & SWS_PRINT_INFO)
  866. av_log(c, AV_LOG_INFO,
  867. "output width is not a multiple of 32 -> no MMX2 scaler\n");
  868. }
  869. if (usesHFilter)
  870. c->canMMX2BeUsed = 0;
  871. } else
  872. c->canMMX2BeUsed = 0;
  873. c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
  874. c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
  875. /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
  876. * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
  877. * correct scaling.
  878. * n-2 is the last chrominance sample available.
  879. * This is not perfect, but no one should notice the difference, the more
  880. * correct variant would be like the vertical one, but that would require
  881. * some special code for the first and last pixel */
  882. if (flags & SWS_FAST_BILINEAR) {
  883. if (c->canMMX2BeUsed) {
  884. c->lumXInc += 20;
  885. c->chrXInc += 20;
  886. }
  887. // we don't use the x86 asm scaler if MMX is available
  888. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  889. c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
  890. c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
  891. }
  892. }
  893. /* precalculate horizontal scaler filter coefficients */
  894. {
  895. #if HAVE_MMX2
  896. // can't downscale !!!
  897. if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
  898. c->lumMmx2FilterCodeSize = initMMX2HScaler(dstW, c->lumXInc, NULL,
  899. NULL, NULL, 8);
  900. c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc,
  901. NULL, NULL, NULL, 4);
  902. #ifdef MAP_ANONYMOUS
  903. c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  904. c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  905. #elif HAVE_VIRTUALALLOC
  906. c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  907. c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  908. #else
  909. c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
  910. c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
  911. #endif
  912. if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
  913. return AVERROR(ENOMEM);
  914. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
  915. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
  916. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
  917. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
  918. initMMX2HScaler(dstW, c->lumXInc, c->lumMmx2FilterCode,
  919. c->hLumFilter, c->hLumFilterPos, 8);
  920. initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode,
  921. c->hChrFilter, c->hChrFilterPos, 4);
  922. #ifdef MAP_ANONYMOUS
  923. mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  924. mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
  925. #endif
  926. } else
  927. #endif /* HAVE_MMX2 */
  928. {
  929. const int filterAlign =
  930. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
  931. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  932. 1;
  933. if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
  934. &c->hLumFilterSize, c->lumXInc,
  935. srcW, dstW, filterAlign, 1 << 14,
  936. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  937. cpu_flags, srcFilter->lumH, dstFilter->lumH,
  938. c->param, 1) < 0)
  939. goto fail;
  940. if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
  941. &c->hChrFilterSize, c->chrXInc,
  942. c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
  943. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  944. cpu_flags, srcFilter->chrH, dstFilter->chrH,
  945. c->param, 1) < 0)
  946. goto fail;
  947. }
  948. } // initialize horizontal stuff
  949. /* precalculate vertical scaler filter coefficients */
  950. {
  951. const int filterAlign =
  952. (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
  953. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  954. 1;
  955. if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
  956. c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
  957. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  958. cpu_flags, srcFilter->lumV, dstFilter->lumV,
  959. c->param, 0) < 0)
  960. goto fail;
  961. if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
  962. c->chrYInc, c->chrSrcH, c->chrDstH,
  963. filterAlign, (1 << 12),
  964. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  965. cpu_flags, srcFilter->chrV, dstFilter->chrV,
  966. c->param, 0) < 0)
  967. goto fail;
  968. #if HAVE_ALTIVEC
  969. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
  970. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
  971. for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
  972. int j;
  973. short *p = (short *)&c->vYCoeffsBank[i];
  974. for (j = 0; j < 8; j++)
  975. p[j] = c->vLumFilter[i];
  976. }
  977. for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
  978. int j;
  979. short *p = (short *)&c->vCCoeffsBank[i];
  980. for (j = 0; j < 8; j++)
  981. p[j] = c->vChrFilter[i];
  982. }
  983. #endif
  984. }
  985. // calculate buffer sizes so that they won't run out while handling these damn slices
  986. c->vLumBufSize = c->vLumFilterSize;
  987. c->vChrBufSize = c->vChrFilterSize;
  988. for (i = 0; i < dstH; i++) {
  989. int chrI = (int64_t)i * c->chrDstH / dstH;
  990. int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
  991. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
  992. << c->chrSrcVSubSample));
  993. nextSlice >>= c->chrSrcVSubSample;
  994. nextSlice <<= c->chrSrcVSubSample;
  995. if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
  996. c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
  997. if (c->vChrFilterPos[chrI] + c->vChrBufSize <
  998. (nextSlice >> c->chrSrcVSubSample))
  999. c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
  1000. c->vChrFilterPos[chrI];
  1001. }
  1002. /* Allocate pixbufs (we use dynamic allocation because otherwise we would
  1003. * need to allocate several megabytes to handle all possible cases) */
  1004. FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
  1005. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
  1006. FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
  1007. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  1008. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
  1009. /* Note we need at least one pixel more at the end because of the MMX code
  1010. * (just in case someone wants to replace the 4000/8000). */
  1011. /* align at 16 bytes for AltiVec */
  1012. for (i = 0; i < c->vLumBufSize; i++) {
  1013. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
  1014. dst_stride + 16, fail);
  1015. c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
  1016. }
  1017. // 64 / (c->dstBpc & ~7) is the same as 16 / sizeof(scaling_intermediate)
  1018. c->uv_off_px = dst_stride_px + 64 / (c->dstBpc & ~7);
  1019. c->uv_off_byte = dst_stride + 16;
  1020. for (i = 0; i < c->vChrBufSize; i++) {
  1021. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
  1022. dst_stride * 2 + 32, fail);
  1023. c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
  1024. c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
  1025. = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
  1026. }
  1027. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  1028. for (i = 0; i < c->vLumBufSize; i++) {
  1029. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
  1030. dst_stride + 16, fail);
  1031. c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
  1032. }
  1033. // try to avoid drawing green stuff between the right end and the stride end
  1034. for (i = 0; i < c->vChrBufSize; i++)
  1035. memset(c->chrUPixBuf[i], 64, dst_stride * 2 + 1);
  1036. assert(c->chrDstH <= dstH);
  1037. if (flags & SWS_PRINT_INFO) {
  1038. if (flags & SWS_FAST_BILINEAR)
  1039. av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
  1040. else if (flags & SWS_BILINEAR)
  1041. av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
  1042. else if (flags & SWS_BICUBIC)
  1043. av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
  1044. else if (flags & SWS_X)
  1045. av_log(c, AV_LOG_INFO, "Experimental scaler, ");
  1046. else if (flags & SWS_POINT)
  1047. av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
  1048. else if (flags & SWS_AREA)
  1049. av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
  1050. else if (flags & SWS_BICUBLIN)
  1051. av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
  1052. else if (flags & SWS_GAUSS)
  1053. av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
  1054. else if (flags & SWS_SINC)
  1055. av_log(c, AV_LOG_INFO, "Sinc scaler, ");
  1056. else if (flags & SWS_LANCZOS)
  1057. av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
  1058. else if (flags & SWS_SPLINE)
  1059. av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
  1060. else
  1061. av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
  1062. av_log(c, AV_LOG_INFO, "from %s to %s%s ",
  1063. sws_format_name(srcFormat),
  1064. #ifdef DITHER1XBPP
  1065. dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
  1066. dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
  1067. dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ?
  1068. "dithered " : "",
  1069. #else
  1070. "",
  1071. #endif
  1072. sws_format_name(dstFormat));
  1073. if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2)
  1074. av_log(c, AV_LOG_INFO, "using MMX2\n");
  1075. else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW)
  1076. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  1077. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  1078. av_log(c, AV_LOG_INFO, "using MMX\n");
  1079. else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC)
  1080. av_log(c, AV_LOG_INFO, "using AltiVec\n");
  1081. else
  1082. av_log(c, AV_LOG_INFO, "using C\n");
  1083. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1084. av_log(c, AV_LOG_DEBUG,
  1085. "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1086. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1087. av_log(c, AV_LOG_DEBUG,
  1088. "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1089. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
  1090. c->chrXInc, c->chrYInc);
  1091. }
  1092. c->swScale = ff_getSwsFunc(c);
  1093. return 0;
  1094. fail: // FIXME replace things by appropriate error codes
  1095. return -1;
  1096. }
  1097. #if FF_API_SWS_GETCONTEXT
  1098. SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
  1099. int dstW, int dstH, enum PixelFormat dstFormat,
  1100. int flags, SwsFilter *srcFilter,
  1101. SwsFilter *dstFilter, const double *param)
  1102. {
  1103. SwsContext *c;
  1104. if (!(c = sws_alloc_context()))
  1105. return NULL;
  1106. c->flags = flags;
  1107. c->srcW = srcW;
  1108. c->srcH = srcH;
  1109. c->dstW = dstW;
  1110. c->dstH = dstH;
  1111. c->srcRange = handle_jpeg(&srcFormat);
  1112. c->dstRange = handle_jpeg(&dstFormat);
  1113. c->srcFormat = srcFormat;
  1114. c->dstFormat = dstFormat;
  1115. if (param) {
  1116. c->param[0] = param[0];
  1117. c->param[1] = param[1];
  1118. }
  1119. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
  1120. ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
  1121. c->dstRange, 0, 1 << 16, 1 << 16);
  1122. if (sws_init_context(c, srcFilter, dstFilter) < 0) {
  1123. sws_freeContext(c);
  1124. return NULL;
  1125. }
  1126. return c;
  1127. }
  1128. #endif
  1129. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1130. float lumaSharpen, float chromaSharpen,
  1131. float chromaHShift, float chromaVShift,
  1132. int verbose)
  1133. {
  1134. SwsFilter *filter = av_malloc(sizeof(SwsFilter));
  1135. if (!filter)
  1136. return NULL;
  1137. if (lumaGBlur != 0.0) {
  1138. filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
  1139. filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
  1140. } else {
  1141. filter->lumH = sws_getIdentityVec();
  1142. filter->lumV = sws_getIdentityVec();
  1143. }
  1144. if (chromaGBlur != 0.0) {
  1145. filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
  1146. filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
  1147. } else {
  1148. filter->chrH = sws_getIdentityVec();
  1149. filter->chrV = sws_getIdentityVec();
  1150. }
  1151. if (chromaSharpen != 0.0) {
  1152. SwsVector *id = sws_getIdentityVec();
  1153. sws_scaleVec(filter->chrH, -chromaSharpen);
  1154. sws_scaleVec(filter->chrV, -chromaSharpen);
  1155. sws_addVec(filter->chrH, id);
  1156. sws_addVec(filter->chrV, id);
  1157. sws_freeVec(id);
  1158. }
  1159. if (lumaSharpen != 0.0) {
  1160. SwsVector *id = sws_getIdentityVec();
  1161. sws_scaleVec(filter->lumH, -lumaSharpen);
  1162. sws_scaleVec(filter->lumV, -lumaSharpen);
  1163. sws_addVec(filter->lumH, id);
  1164. sws_addVec(filter->lumV, id);
  1165. sws_freeVec(id);
  1166. }
  1167. if (chromaHShift != 0.0)
  1168. sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
  1169. if (chromaVShift != 0.0)
  1170. sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
  1171. sws_normalizeVec(filter->chrH, 1.0);
  1172. sws_normalizeVec(filter->chrV, 1.0);
  1173. sws_normalizeVec(filter->lumH, 1.0);
  1174. sws_normalizeVec(filter->lumV, 1.0);
  1175. if (verbose)
  1176. sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1177. if (verbose)
  1178. sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1179. return filter;
  1180. }
  1181. SwsVector *sws_allocVec(int length)
  1182. {
  1183. SwsVector *vec = av_malloc(sizeof(SwsVector));
  1184. if (!vec)
  1185. return NULL;
  1186. vec->length = length;
  1187. vec->coeff = av_malloc(sizeof(double) * length);
  1188. if (!vec->coeff)
  1189. av_freep(&vec);
  1190. return vec;
  1191. }
  1192. SwsVector *sws_getGaussianVec(double variance, double quality)
  1193. {
  1194. const int length = (int)(variance * quality + 0.5) | 1;
  1195. int i;
  1196. double middle = (length - 1) * 0.5;
  1197. SwsVector *vec = sws_allocVec(length);
  1198. if (!vec)
  1199. return NULL;
  1200. for (i = 0; i < length; i++) {
  1201. double dist = i - middle;
  1202. vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
  1203. sqrt(2 * variance * M_PI);
  1204. }
  1205. sws_normalizeVec(vec, 1.0);
  1206. return vec;
  1207. }
  1208. SwsVector *sws_getConstVec(double c, int length)
  1209. {
  1210. int i;
  1211. SwsVector *vec = sws_allocVec(length);
  1212. if (!vec)
  1213. return NULL;
  1214. for (i = 0; i < length; i++)
  1215. vec->coeff[i] = c;
  1216. return vec;
  1217. }
  1218. SwsVector *sws_getIdentityVec(void)
  1219. {
  1220. return sws_getConstVec(1.0, 1);
  1221. }
  1222. static double sws_dcVec(SwsVector *a)
  1223. {
  1224. int i;
  1225. double sum = 0;
  1226. for (i = 0; i < a->length; i++)
  1227. sum += a->coeff[i];
  1228. return sum;
  1229. }
  1230. void sws_scaleVec(SwsVector *a, double scalar)
  1231. {
  1232. int i;
  1233. for (i = 0; i < a->length; i++)
  1234. a->coeff[i] *= scalar;
  1235. }
  1236. void sws_normalizeVec(SwsVector *a, double height)
  1237. {
  1238. sws_scaleVec(a, height / sws_dcVec(a));
  1239. }
  1240. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
  1241. {
  1242. int length = a->length + b->length - 1;
  1243. int i, j;
  1244. SwsVector *vec = sws_getConstVec(0.0, length);
  1245. if (!vec)
  1246. return NULL;
  1247. for (i = 0; i < a->length; i++) {
  1248. for (j = 0; j < b->length; j++) {
  1249. vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
  1250. }
  1251. }
  1252. return vec;
  1253. }
  1254. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1255. {
  1256. int length = FFMAX(a->length, b->length);
  1257. int i;
  1258. SwsVector *vec = sws_getConstVec(0.0, length);
  1259. if (!vec)
  1260. return NULL;
  1261. for (i = 0; i < a->length; i++)
  1262. vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
  1263. for (i = 0; i < b->length; i++)
  1264. vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
  1265. return vec;
  1266. }
  1267. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
  1268. {
  1269. int length = FFMAX(a->length, b->length);
  1270. int i;
  1271. SwsVector *vec = sws_getConstVec(0.0, length);
  1272. if (!vec)
  1273. return NULL;
  1274. for (i = 0; i < a->length; i++)
  1275. vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
  1276. for (i = 0; i < b->length; i++)
  1277. vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
  1278. return vec;
  1279. }
  1280. /* shift left / or right if "shift" is negative */
  1281. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  1282. {
  1283. int length = a->length + FFABS(shift) * 2;
  1284. int i;
  1285. SwsVector *vec = sws_getConstVec(0.0, length);
  1286. if (!vec)
  1287. return NULL;
  1288. for (i = 0; i < a->length; i++) {
  1289. vec->coeff[i + (length - 1) / 2 -
  1290. (a->length - 1) / 2 - shift] = a->coeff[i];
  1291. }
  1292. return vec;
  1293. }
  1294. void sws_shiftVec(SwsVector *a, int shift)
  1295. {
  1296. SwsVector *shifted = sws_getShiftedVec(a, shift);
  1297. av_free(a->coeff);
  1298. a->coeff = shifted->coeff;
  1299. a->length = shifted->length;
  1300. av_free(shifted);
  1301. }
  1302. void sws_addVec(SwsVector *a, SwsVector *b)
  1303. {
  1304. SwsVector *sum = sws_sumVec(a, b);
  1305. av_free(a->coeff);
  1306. a->coeff = sum->coeff;
  1307. a->length = sum->length;
  1308. av_free(sum);
  1309. }
  1310. void sws_subVec(SwsVector *a, SwsVector *b)
  1311. {
  1312. SwsVector *diff = sws_diffVec(a, b);
  1313. av_free(a->coeff);
  1314. a->coeff = diff->coeff;
  1315. a->length = diff->length;
  1316. av_free(diff);
  1317. }
  1318. void sws_convVec(SwsVector *a, SwsVector *b)
  1319. {
  1320. SwsVector *conv = sws_getConvVec(a, b);
  1321. av_free(a->coeff);
  1322. a->coeff = conv->coeff;
  1323. a->length = conv->length;
  1324. av_free(conv);
  1325. }
  1326. SwsVector *sws_cloneVec(SwsVector *a)
  1327. {
  1328. int i;
  1329. SwsVector *vec = sws_allocVec(a->length);
  1330. if (!vec)
  1331. return NULL;
  1332. for (i = 0; i < a->length; i++)
  1333. vec->coeff[i] = a->coeff[i];
  1334. return vec;
  1335. }
  1336. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  1337. {
  1338. int i;
  1339. double max = 0;
  1340. double min = 0;
  1341. double range;
  1342. for (i = 0; i < a->length; i++)
  1343. if (a->coeff[i] > max)
  1344. max = a->coeff[i];
  1345. for (i = 0; i < a->length; i++)
  1346. if (a->coeff[i] < min)
  1347. min = a->coeff[i];
  1348. range = max - min;
  1349. for (i = 0; i < a->length; i++) {
  1350. int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
  1351. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  1352. for (; x > 0; x--)
  1353. av_log(log_ctx, log_level, " ");
  1354. av_log(log_ctx, log_level, "|\n");
  1355. }
  1356. }
  1357. void sws_freeVec(SwsVector *a)
  1358. {
  1359. if (!a)
  1360. return;
  1361. av_freep(&a->coeff);
  1362. a->length = 0;
  1363. av_free(a);
  1364. }
  1365. void sws_freeFilter(SwsFilter *filter)
  1366. {
  1367. if (!filter)
  1368. return;
  1369. if (filter->lumH)
  1370. sws_freeVec(filter->lumH);
  1371. if (filter->lumV)
  1372. sws_freeVec(filter->lumV);
  1373. if (filter->chrH)
  1374. sws_freeVec(filter->chrH);
  1375. if (filter->chrV)
  1376. sws_freeVec(filter->chrV);
  1377. av_free(filter);
  1378. }
  1379. void sws_freeContext(SwsContext *c)
  1380. {
  1381. int i;
  1382. if (!c)
  1383. return;
  1384. if (c->lumPixBuf) {
  1385. for (i = 0; i < c->vLumBufSize; i++)
  1386. av_freep(&c->lumPixBuf[i]);
  1387. av_freep(&c->lumPixBuf);
  1388. }
  1389. if (c->chrUPixBuf) {
  1390. for (i = 0; i < c->vChrBufSize; i++)
  1391. av_freep(&c->chrUPixBuf[i]);
  1392. av_freep(&c->chrUPixBuf);
  1393. av_freep(&c->chrVPixBuf);
  1394. }
  1395. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  1396. for (i = 0; i < c->vLumBufSize; i++)
  1397. av_freep(&c->alpPixBuf[i]);
  1398. av_freep(&c->alpPixBuf);
  1399. }
  1400. av_freep(&c->vLumFilter);
  1401. av_freep(&c->vChrFilter);
  1402. av_freep(&c->hLumFilter);
  1403. av_freep(&c->hChrFilter);
  1404. #if HAVE_ALTIVEC
  1405. av_freep(&c->vYCoeffsBank);
  1406. av_freep(&c->vCCoeffsBank);
  1407. #endif
  1408. av_freep(&c->vLumFilterPos);
  1409. av_freep(&c->vChrFilterPos);
  1410. av_freep(&c->hLumFilterPos);
  1411. av_freep(&c->hChrFilterPos);
  1412. #if HAVE_MMX
  1413. #ifdef MAP_ANONYMOUS
  1414. if (c->lumMmx2FilterCode)
  1415. munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
  1416. if (c->chrMmx2FilterCode)
  1417. munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
  1418. #elif HAVE_VIRTUALALLOC
  1419. if (c->lumMmx2FilterCode)
  1420. VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
  1421. if (c->chrMmx2FilterCode)
  1422. VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
  1423. #else
  1424. av_free(c->lumMmx2FilterCode);
  1425. av_free(c->chrMmx2FilterCode);
  1426. #endif
  1427. c->lumMmx2FilterCode = NULL;
  1428. c->chrMmx2FilterCode = NULL;
  1429. #endif /* HAVE_MMX */
  1430. av_freep(&c->yuvTable);
  1431. av_free(c->formatConvBuffer);
  1432. av_free(c);
  1433. }
  1434. struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
  1435. int srcH, enum PixelFormat srcFormat,
  1436. int dstW, int dstH,
  1437. enum PixelFormat dstFormat, int flags,
  1438. SwsFilter *srcFilter,
  1439. SwsFilter *dstFilter,
  1440. const double *param)
  1441. {
  1442. static const double default_param[2] = { SWS_PARAM_DEFAULT,
  1443. SWS_PARAM_DEFAULT };
  1444. if (!param)
  1445. param = default_param;
  1446. if (context &&
  1447. (context->srcW != srcW ||
  1448. context->srcH != srcH ||
  1449. context->srcFormat != srcFormat ||
  1450. context->dstW != dstW ||
  1451. context->dstH != dstH ||
  1452. context->dstFormat != dstFormat ||
  1453. context->flags != flags ||
  1454. context->param[0] != param[0] ||
  1455. context->param[1] != param[1])) {
  1456. sws_freeContext(context);
  1457. context = NULL;
  1458. }
  1459. if (!context) {
  1460. if (!(context = sws_alloc_context()))
  1461. return NULL;
  1462. context->srcW = srcW;
  1463. context->srcH = srcH;
  1464. context->srcRange = handle_jpeg(&srcFormat);
  1465. context->srcFormat = srcFormat;
  1466. context->dstW = dstW;
  1467. context->dstH = dstH;
  1468. context->dstRange = handle_jpeg(&dstFormat);
  1469. context->dstFormat = dstFormat;
  1470. context->flags = flags;
  1471. context->param[0] = param[0];
  1472. context->param[1] = param[1];
  1473. sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
  1474. context->srcRange,
  1475. ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
  1476. context->dstRange, 0, 1 << 16, 1 << 16);
  1477. if (sws_init_context(context, srcFilter, dstFilter) < 0) {
  1478. sws_freeContext(context);
  1479. return NULL;
  1480. }
  1481. }
  1482. return context;
  1483. }