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.

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