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/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 "libavutil/x86/cpu.h"
  47. #include "rgb2rgb.h"
  48. #include "swscale.h"
  49. #include "swscale_internal.h"
  50. unsigned swscale_version(void)
  51. {
  52. return LIBSWSCALE_VERSION_INT;
  53. }
  54. const char *swscale_configuration(void)
  55. {
  56. return LIBAV_CONFIGURATION;
  57. }
  58. const char *swscale_license(void)
  59. {
  60. #define LICENSE_PREFIX "libswscale license: "
  61. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  62. }
  63. #define RET 0xC3 // near return opcode for x86
  64. typedef struct FormatEntry {
  65. int is_supported_in, is_supported_out;
  66. } FormatEntry;
  67. static const FormatEntry format_entries[PIX_FMT_NB] = {
  68. [PIX_FMT_YUV420P] = { 1, 1 },
  69. [PIX_FMT_YUYV422] = { 1, 1 },
  70. [PIX_FMT_RGB24] = { 1, 1 },
  71. [PIX_FMT_BGR24] = { 1, 1 },
  72. [PIX_FMT_YUV422P] = { 1, 1 },
  73. [PIX_FMT_YUV444P] = { 1, 1 },
  74. [PIX_FMT_YUV410P] = { 1, 1 },
  75. [PIX_FMT_YUV411P] = { 1, 1 },
  76. [PIX_FMT_GRAY8] = { 1, 1 },
  77. [PIX_FMT_MONOWHITE] = { 1, 1 },
  78. [PIX_FMT_MONOBLACK] = { 1, 1 },
  79. [PIX_FMT_PAL8] = { 1, 0 },
  80. [PIX_FMT_YUVJ420P] = { 1, 1 },
  81. [PIX_FMT_YUVJ422P] = { 1, 1 },
  82. [PIX_FMT_YUVJ444P] = { 1, 1 },
  83. [PIX_FMT_UYVY422] = { 1, 1 },
  84. [PIX_FMT_UYYVYY411] = { 0, 0 },
  85. [PIX_FMT_BGR8] = { 1, 1 },
  86. [PIX_FMT_BGR4] = { 0, 1 },
  87. [PIX_FMT_BGR4_BYTE] = { 1, 1 },
  88. [PIX_FMT_RGB8] = { 1, 1 },
  89. [PIX_FMT_RGB4] = { 0, 1 },
  90. [PIX_FMT_RGB4_BYTE] = { 1, 1 },
  91. [PIX_FMT_NV12] = { 1, 1 },
  92. [PIX_FMT_NV21] = { 1, 1 },
  93. [PIX_FMT_ARGB] = { 1, 1 },
  94. [PIX_FMT_RGBA] = { 1, 1 },
  95. [PIX_FMT_ABGR] = { 1, 1 },
  96. [PIX_FMT_BGRA] = { 1, 1 },
  97. [PIX_FMT_GRAY16BE] = { 1, 1 },
  98. [PIX_FMT_GRAY16LE] = { 1, 1 },
  99. [PIX_FMT_YUV440P] = { 1, 1 },
  100. [PIX_FMT_YUVJ440P] = { 1, 1 },
  101. [PIX_FMT_YUVA420P] = { 1, 1 },
  102. [PIX_FMT_RGB48BE] = { 1, 1 },
  103. [PIX_FMT_RGB48LE] = { 1, 1 },
  104. [PIX_FMT_RGB565BE] = { 1, 1 },
  105. [PIX_FMT_RGB565LE] = { 1, 1 },
  106. [PIX_FMT_RGB555BE] = { 1, 1 },
  107. [PIX_FMT_RGB555LE] = { 1, 1 },
  108. [PIX_FMT_BGR565BE] = { 1, 1 },
  109. [PIX_FMT_BGR565LE] = { 1, 1 },
  110. [PIX_FMT_BGR555BE] = { 1, 1 },
  111. [PIX_FMT_BGR555LE] = { 1, 1 },
  112. [PIX_FMT_YUV420P16LE] = { 1, 1 },
  113. [PIX_FMT_YUV420P16BE] = { 1, 1 },
  114. [PIX_FMT_YUV422P16LE] = { 1, 1 },
  115. [PIX_FMT_YUV422P16BE] = { 1, 1 },
  116. [PIX_FMT_YUV444P16LE] = { 1, 1 },
  117. [PIX_FMT_YUV444P16BE] = { 1, 1 },
  118. [PIX_FMT_RGB444LE] = { 1, 1 },
  119. [PIX_FMT_RGB444BE] = { 1, 1 },
  120. [PIX_FMT_BGR444LE] = { 1, 1 },
  121. [PIX_FMT_BGR444BE] = { 1, 1 },
  122. [PIX_FMT_Y400A] = { 1, 0 },
  123. [PIX_FMT_BGR48BE] = { 1, 1 },
  124. [PIX_FMT_BGR48LE] = { 1, 1 },
  125. [PIX_FMT_YUV420P9BE] = { 1, 1 },
  126. [PIX_FMT_YUV420P9LE] = { 1, 1 },
  127. [PIX_FMT_YUV420P10BE] = { 1, 1 },
  128. [PIX_FMT_YUV420P10LE] = { 1, 1 },
  129. [PIX_FMT_YUV422P9BE] = { 1, 1 },
  130. [PIX_FMT_YUV422P9LE] = { 1, 1 },
  131. [PIX_FMT_YUV422P10BE] = { 1, 1 },
  132. [PIX_FMT_YUV422P10LE] = { 1, 1 },
  133. [PIX_FMT_YUV444P9BE] = { 1, 1 },
  134. [PIX_FMT_YUV444P9LE] = { 1, 1 },
  135. [PIX_FMT_YUV444P10BE] = { 1, 1 },
  136. [PIX_FMT_YUV444P10LE] = { 1, 1 },
  137. [PIX_FMT_GBRP] = { 1, 0 },
  138. [PIX_FMT_GBRP9LE] = { 1, 0 },
  139. [PIX_FMT_GBRP9BE] = { 1, 0 },
  140. [PIX_FMT_GBRP10LE] = { 1, 0 },
  141. [PIX_FMT_GBRP10BE] = { 1, 0 },
  142. [PIX_FMT_GBRP16LE] = { 1, 0 },
  143. [PIX_FMT_GBRP16BE] = { 1, 0 },
  144. };
  145. int sws_isSupportedInput(enum PixelFormat pix_fmt)
  146. {
  147. return (unsigned)pix_fmt < PIX_FMT_NB ?
  148. format_entries[pix_fmt].is_supported_in : 0;
  149. }
  150. int sws_isSupportedOutput(enum PixelFormat pix_fmt)
  151. {
  152. return (unsigned)pix_fmt < PIX_FMT_NB ?
  153. format_entries[pix_fmt].is_supported_out : 0;
  154. }
  155. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  156. const char *sws_format_name(enum PixelFormat format)
  157. {
  158. if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
  159. return av_pix_fmt_descriptors[format].name;
  160. else
  161. return "Unknown format";
  162. }
  163. static double getSplineCoeff(double a, double b, double c, double d,
  164. double dist)
  165. {
  166. if (dist <= 1.0)
  167. return ((d * dist + c) * dist + b) * dist + a;
  168. else
  169. return getSplineCoeff(0.0,
  170. b + 2.0 * c + 3.0 * d,
  171. c + 3.0 * d,
  172. -b - 3.0 * c - 6.0 * d,
  173. dist - 1.0);
  174. }
  175. static int initFilter(int16_t **outFilter, int32_t **filterPos,
  176. int *outFilterSize, int xInc, int srcW, int dstW,
  177. int filterAlign, int one, int flags, int cpu_flags,
  178. SwsVector *srcFilter, SwsVector *dstFilter,
  179. double param[2], int is_horizontal)
  180. {
  181. int i;
  182. int filterSize;
  183. int filter2Size;
  184. int minFilterSize;
  185. int64_t *filter = NULL;
  186. int64_t *filter2 = NULL;
  187. const int64_t fone = 1LL << 54;
  188. int ret = -1;
  189. emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
  190. // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
  191. FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
  192. if (FFABS(xInc - 0x10000) < 10) { // unscaled
  193. int i;
  194. filterSize = 1;
  195. FF_ALLOCZ_OR_GOTO(NULL, filter,
  196. dstW * sizeof(*filter) * filterSize, fail);
  197. for (i = 0; i < dstW; i++) {
  198. filter[i * filterSize] = fone;
  199. (*filterPos)[i] = i;
  200. }
  201. } else if (flags & SWS_POINT) { // lame looking point sampling mode
  202. int i;
  203. int xDstInSrc;
  204. filterSize = 1;
  205. FF_ALLOC_OR_GOTO(NULL, filter,
  206. dstW * sizeof(*filter) * filterSize, fail);
  207. xDstInSrc = xInc / 2 - 0x8000;
  208. for (i = 0; i < dstW; i++) {
  209. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  210. (*filterPos)[i] = xx;
  211. filter[i] = fone;
  212. xDstInSrc += xInc;
  213. }
  214. } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
  215. (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
  216. int i;
  217. int xDstInSrc;
  218. filterSize = 2;
  219. FF_ALLOC_OR_GOTO(NULL, filter,
  220. dstW * sizeof(*filter) * filterSize, fail);
  221. xDstInSrc = xInc / 2 - 0x8000;
  222. for (i = 0; i < dstW; i++) {
  223. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  224. int j;
  225. (*filterPos)[i] = xx;
  226. // bilinear upscale / linear interpolate / area averaging
  227. for (j = 0; j < filterSize; j++) {
  228. int64_t coeff = fone - FFABS((xx << 16) - xDstInSrc) *
  229. (fone >> 16);
  230. if (coeff < 0)
  231. coeff = 0;
  232. filter[i * filterSize + j] = coeff;
  233. xx++;
  234. }
  235. xDstInSrc += xInc;
  236. }
  237. } else {
  238. int64_t xDstInSrc;
  239. int sizeFactor;
  240. if (flags & SWS_BICUBIC)
  241. sizeFactor = 4;
  242. else if (flags & SWS_X)
  243. sizeFactor = 8;
  244. else if (flags & SWS_AREA)
  245. sizeFactor = 1; // downscale only, for upscale it is bilinear
  246. else if (flags & SWS_GAUSS)
  247. sizeFactor = 8; // infinite ;)
  248. else if (flags & SWS_LANCZOS)
  249. sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
  250. else if (flags & SWS_SINC)
  251. sizeFactor = 20; // infinite ;)
  252. else if (flags & SWS_SPLINE)
  253. sizeFactor = 20; // infinite ;)
  254. else if (flags & SWS_BILINEAR)
  255. sizeFactor = 2;
  256. else {
  257. sizeFactor = 0; // GCC warning killer
  258. assert(0);
  259. }
  260. if (xInc <= 1 << 16)
  261. filterSize = 1 + sizeFactor; // upscale
  262. else
  263. filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
  264. filterSize = FFMIN(filterSize, srcW - 2);
  265. filterSize = FFMAX(filterSize, 1);
  266. FF_ALLOC_OR_GOTO(NULL, filter,
  267. dstW * sizeof(*filter) * filterSize, fail);
  268. xDstInSrc = xInc - 0x10000;
  269. for (i = 0; i < dstW; i++) {
  270. int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
  271. int j;
  272. (*filterPos)[i] = xx;
  273. for (j = 0; j < filterSize; j++) {
  274. int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
  275. double floatd;
  276. int64_t coeff;
  277. if (xInc > 1 << 16)
  278. d = d * dstW / srcW;
  279. floatd = d * (1.0 / (1 << 30));
  280. if (flags & SWS_BICUBIC) {
  281. int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
  282. int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
  283. if (d >= 1LL << 31) {
  284. coeff = 0.0;
  285. } else {
  286. int64_t dd = (d * d) >> 30;
  287. int64_t ddd = (dd * d) >> 30;
  288. if (d < 1LL << 30)
  289. coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
  290. (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
  291. (6 * (1 << 24) - 2 * B) * (1 << 30);
  292. else
  293. coeff = (-B - 6 * C) * ddd +
  294. (6 * B + 30 * C) * dd +
  295. (-12 * B - 48 * C) * d +
  296. (8 * B + 24 * C) * (1 << 30);
  297. }
  298. coeff *= fone >> (30 + 24);
  299. }
  300. #if 0
  301. else if (flags & SWS_X) {
  302. double p = param ? param * 0.01 : 0.3;
  303. coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
  304. coeff *= pow(2.0, -p * d * d);
  305. }
  306. #endif
  307. else if (flags & SWS_X) {
  308. double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  309. double c;
  310. if (floatd < 1.0)
  311. c = cos(floatd * M_PI);
  312. else
  313. c = -1.0;
  314. if (c < 0.0)
  315. c = -pow(-c, A);
  316. else
  317. c = pow(c, A);
  318. coeff = (c * 0.5 + 0.5) * fone;
  319. } else if (flags & SWS_AREA) {
  320. int64_t d2 = d - (1 << 29);
  321. if (d2 * xInc < -(1LL << (29 + 16)))
  322. coeff = 1.0 * (1LL << (30 + 16));
  323. else if (d2 * xInc < (1LL << (29 + 16)))
  324. coeff = -d2 * xInc + (1LL << (29 + 16));
  325. else
  326. coeff = 0.0;
  327. coeff *= fone >> (30 + 16);
  328. } else if (flags & SWS_GAUSS) {
  329. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  330. coeff = (pow(2.0, -p * floatd * floatd)) * fone;
  331. } else if (flags & SWS_SINC) {
  332. coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
  333. } else if (flags & SWS_LANCZOS) {
  334. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  335. coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
  336. (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
  337. if (floatd > p)
  338. coeff = 0;
  339. } else if (flags & SWS_BILINEAR) {
  340. coeff = (1 << 30) - d;
  341. if (coeff < 0)
  342. coeff = 0;
  343. coeff *= fone >> 30;
  344. } else if (flags & SWS_SPLINE) {
  345. double p = -2.196152422706632;
  346. coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
  347. } else {
  348. coeff = 0.0; // GCC warning killer
  349. assert(0);
  350. }
  351. filter[i * filterSize + j] = coeff;
  352. xx++;
  353. }
  354. xDstInSrc += 2 * xInc;
  355. }
  356. }
  357. /* apply src & dst Filter to filter -> filter2
  358. * av_free(filter);
  359. */
  360. assert(filterSize > 0);
  361. filter2Size = filterSize;
  362. if (srcFilter)
  363. filter2Size += srcFilter->length - 1;
  364. if (dstFilter)
  365. filter2Size += dstFilter->length - 1;
  366. assert(filter2Size > 0);
  367. FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
  368. for (i = 0; i < dstW; i++) {
  369. int j, k;
  370. if (srcFilter) {
  371. for (k = 0; k < srcFilter->length; k++) {
  372. for (j = 0; j < filterSize; j++)
  373. filter2[i * filter2Size + k + j] +=
  374. srcFilter->coeff[k] * filter[i * filterSize + j];
  375. }
  376. } else {
  377. for (j = 0; j < filterSize; j++)
  378. filter2[i * filter2Size + j] = filter[i * filterSize + j];
  379. }
  380. // FIXME dstFilter
  381. (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
  382. }
  383. av_freep(&filter);
  384. /* try to reduce the filter-size (step1 find size and shift left) */
  385. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  386. minFilterSize = 0;
  387. for (i = dstW - 1; i >= 0; i--) {
  388. int min = filter2Size;
  389. int j;
  390. int64_t cutOff = 0.0;
  391. /* get rid of near zero elements on the left by shifting left */
  392. for (j = 0; j < filter2Size; j++) {
  393. int k;
  394. cutOff += FFABS(filter2[i * filter2Size]);
  395. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  396. break;
  397. /* preserve monotonicity because the core can't handle the
  398. * filter otherwise */
  399. if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
  400. break;
  401. // move filter coefficients left
  402. for (k = 1; k < filter2Size; k++)
  403. filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
  404. filter2[i * filter2Size + k - 1] = 0;
  405. (*filterPos)[i]++;
  406. }
  407. cutOff = 0;
  408. /* count near zeros on the right */
  409. for (j = filter2Size - 1; j > 0; j--) {
  410. cutOff += FFABS(filter2[i * filter2Size + j]);
  411. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  412. break;
  413. min--;
  414. }
  415. if (min > minFilterSize)
  416. minFilterSize = min;
  417. }
  418. if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
  419. // we can handle the special case 4, so we don't want to go the full 8
  420. if (minFilterSize < 5)
  421. filterAlign = 4;
  422. /* We really don't want to waste our time doing useless computation, so
  423. * fall back on the scalar C code for very small filters.
  424. * Vectorizing is worth it only if you have a decent-sized vector. */
  425. if (minFilterSize < 3)
  426. filterAlign = 1;
  427. }
  428. if (INLINE_MMX(cpu_flags)) {
  429. // special case for unscaled vertical filtering
  430. if (minFilterSize == 1 && filterAlign == 2)
  431. filterAlign = 1;
  432. }
  433. assert(minFilterSize > 0);
  434. filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
  435. assert(filterSize > 0);
  436. filter = av_malloc(filterSize * dstW * sizeof(*filter));
  437. if (filterSize >= MAX_FILTER_SIZE * 16 /
  438. ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
  439. goto fail;
  440. *outFilterSize = filterSize;
  441. if (flags & SWS_PRINT_INFO)
  442. av_log(NULL, AV_LOG_VERBOSE,
  443. "SwScaler: reducing / aligning filtersize %d -> %d\n",
  444. filter2Size, filterSize);
  445. /* try to reduce the filter-size (step2 reduce it) */
  446. for (i = 0; i < dstW; i++) {
  447. int j;
  448. for (j = 0; j < filterSize; j++) {
  449. if (j >= filter2Size)
  450. filter[i * filterSize + j] = 0;
  451. else
  452. filter[i * filterSize + j] = filter2[i * filter2Size + j];
  453. if ((flags & SWS_BITEXACT) && j >= minFilterSize)
  454. filter[i * filterSize + j] = 0;
  455. }
  456. }
  457. // FIXME try to align filterPos if possible
  458. // fix borders
  459. if (is_horizontal) {
  460. for (i = 0; i < dstW; i++) {
  461. int j;
  462. if ((*filterPos)[i] < 0) {
  463. // move filter coefficients left to compensate for filterPos
  464. for (j = 1; j < filterSize; j++) {
  465. int left = FFMAX(j + (*filterPos)[i], 0);
  466. filter[i * filterSize + left] += filter[i * filterSize + j];
  467. filter[i * filterSize + j] = 0;
  468. }
  469. (*filterPos)[i] = 0;
  470. }
  471. if ((*filterPos)[i] + filterSize > srcW) {
  472. int shift = (*filterPos)[i] + filterSize - srcW;
  473. // move filter coefficients right to compensate for filterPos
  474. for (j = filterSize - 2; j >= 0; j--) {
  475. int right = FFMIN(j + shift, filterSize - 1);
  476. filter[i * filterSize + right] += filter[i * filterSize + j];
  477. filter[i * filterSize + j] = 0;
  478. }
  479. (*filterPos)[i] = srcW - filterSize;
  480. }
  481. }
  482. }
  483. // Note the +1 is for the MMX scaler which reads over the end
  484. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  485. FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
  486. *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
  487. /* normalize & store in outFilter */
  488. for (i = 0; i < dstW; i++) {
  489. int j;
  490. int64_t error = 0;
  491. int64_t sum = 0;
  492. for (j = 0; j < filterSize; j++) {
  493. sum += filter[i * filterSize + j];
  494. }
  495. sum = (sum + one / 2) / one;
  496. for (j = 0; j < *outFilterSize; j++) {
  497. int64_t v = filter[i * filterSize + j] + error;
  498. int intV = ROUNDED_DIV(v, sum);
  499. (*outFilter)[i * (*outFilterSize) + j] = intV;
  500. error = v - intV * sum;
  501. }
  502. }
  503. (*filterPos)[dstW + 0] =
  504. (*filterPos)[dstW + 1] =
  505. (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
  506. * read over the end */
  507. for (i = 0; i < *outFilterSize; i++) {
  508. int k = (dstW - 1) * (*outFilterSize) + i;
  509. (*outFilter)[k + 1 * (*outFilterSize)] =
  510. (*outFilter)[k + 2 * (*outFilterSize)] =
  511. (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
  512. }
  513. ret = 0;
  514. fail:
  515. av_free(filter);
  516. av_free(filter2);
  517. return ret;
  518. }
  519. #if HAVE_MMXEXT_INLINE
  520. static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode,
  521. int16_t *filter, int32_t *filterPos, int numSplits)
  522. {
  523. uint8_t *fragmentA;
  524. x86_reg imm8OfPShufW1A;
  525. x86_reg imm8OfPShufW2A;
  526. x86_reg fragmentLengthA;
  527. uint8_t *fragmentB;
  528. x86_reg imm8OfPShufW1B;
  529. x86_reg imm8OfPShufW2B;
  530. x86_reg fragmentLengthB;
  531. int fragmentPos;
  532. int xpos, i;
  533. // create an optimized horizontal scaling routine
  534. /* This scaler is made of runtime-generated MMX2 code using specially tuned
  535. * pshufw instructions. For every four output pixels, if four input pixels
  536. * are enough for the fast bilinear scaling, then a chunk of fragmentB is
  537. * used. If five input pixels are needed, then a chunk of fragmentA is used.
  538. */
  539. // code fragment
  540. __asm__ volatile (
  541. "jmp 9f \n\t"
  542. // Begin
  543. "0: \n\t"
  544. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  545. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  546. "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
  547. "punpcklbw %%mm7, %%mm1 \n\t"
  548. "punpcklbw %%mm7, %%mm0 \n\t"
  549. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  550. "1: \n\t"
  551. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  552. "2: \n\t"
  553. "psubw %%mm1, %%mm0 \n\t"
  554. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  555. "pmullw %%mm3, %%mm0 \n\t"
  556. "psllw $7, %%mm1 \n\t"
  557. "paddw %%mm1, %%mm0 \n\t"
  558. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  559. "add $8, %%"REG_a" \n\t"
  560. // End
  561. "9: \n\t"
  562. // "int $3 \n\t"
  563. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  564. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  565. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  566. "dec %1 \n\t"
  567. "dec %2 \n\t"
  568. "sub %0, %1 \n\t"
  569. "sub %0, %2 \n\t"
  570. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  571. "sub %0, %3 \n\t"
  572. : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  573. "=r" (fragmentLengthA)
  574. );
  575. __asm__ volatile (
  576. "jmp 9f \n\t"
  577. // Begin
  578. "0: \n\t"
  579. "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
  580. "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
  581. "punpcklbw %%mm7, %%mm0 \n\t"
  582. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  583. "1: \n\t"
  584. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  585. "2: \n\t"
  586. "psubw %%mm1, %%mm0 \n\t"
  587. "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
  588. "pmullw %%mm3, %%mm0 \n\t"
  589. "psllw $7, %%mm1 \n\t"
  590. "paddw %%mm1, %%mm0 \n\t"
  591. "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
  592. "add $8, %%"REG_a" \n\t"
  593. // End
  594. "9: \n\t"
  595. // "int $3 \n\t"
  596. "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
  597. "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
  598. "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
  599. "dec %1 \n\t"
  600. "dec %2 \n\t"
  601. "sub %0, %1 \n\t"
  602. "sub %0, %2 \n\t"
  603. "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
  604. "sub %0, %3 \n\t"
  605. : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  606. "=r" (fragmentLengthB)
  607. );
  608. xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
  609. fragmentPos = 0;
  610. for (i = 0; i < dstW / numSplits; i++) {
  611. int xx = xpos >> 16;
  612. if ((i & 3) == 0) {
  613. int a = 0;
  614. int b = ((xpos + xInc) >> 16) - xx;
  615. int c = ((xpos + xInc * 2) >> 16) - xx;
  616. int d = ((xpos + xInc * 3) >> 16) - xx;
  617. int inc = (d + 1 < 4);
  618. uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
  619. x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
  620. x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
  621. x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
  622. int maxShift = 3 - (d + inc);
  623. int shift = 0;
  624. if (filterCode) {
  625. filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
  626. filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
  627. filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
  628. filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
  629. filterPos[i / 2] = xx;
  630. memcpy(filterCode + fragmentPos, fragment, fragmentLength);
  631. filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
  632. ((b + inc) << 2) |
  633. ((c + inc) << 4) |
  634. ((d + inc) << 6);
  635. filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
  636. (c << 4) |
  637. (d << 6);
  638. if (i + 4 - inc >= dstW)
  639. shift = maxShift; // avoid overread
  640. else if ((filterPos[i / 2] & 3) <= maxShift)
  641. shift = filterPos[i / 2] & 3; // align
  642. if (shift && i >= shift) {
  643. filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
  644. filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
  645. filterPos[i / 2] -= shift;
  646. }
  647. }
  648. fragmentPos += fragmentLength;
  649. if (filterCode)
  650. filterCode[fragmentPos] = RET;
  651. }
  652. xpos += xInc;
  653. }
  654. if (filterCode)
  655. filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
  656. return fragmentPos + 1;
  657. }
  658. #endif /* HAVE_MMXEXT_INLINE */
  659. static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
  660. {
  661. *h = av_pix_fmt_descriptors[format].log2_chroma_w;
  662. *v = av_pix_fmt_descriptors[format].log2_chroma_h;
  663. }
  664. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  665. int srcRange, const int table[4], int dstRange,
  666. int brightness, int contrast, int saturation)
  667. {
  668. memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
  669. memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
  670. c->brightness = brightness;
  671. c->contrast = contrast;
  672. c->saturation = saturation;
  673. c->srcRange = srcRange;
  674. c->dstRange = dstRange;
  675. if (isYUV(c->dstFormat) || isGray(c->dstFormat))
  676. return -1;
  677. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
  678. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
  679. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
  680. contrast, saturation);
  681. // FIXME factorize
  682. if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
  683. ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
  684. contrast, saturation);
  685. return 0;
  686. }
  687. int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
  688. int *srcRange, int **table, int *dstRange,
  689. int *brightness, int *contrast, int *saturation)
  690. {
  691. if (isYUV(c->dstFormat) || isGray(c->dstFormat))
  692. return -1;
  693. *inv_table = c->srcColorspaceTable;
  694. *table = c->dstColorspaceTable;
  695. *srcRange = c->srcRange;
  696. *dstRange = c->dstRange;
  697. *brightness = c->brightness;
  698. *contrast = c->contrast;
  699. *saturation = c->saturation;
  700. return 0;
  701. }
  702. static int handle_jpeg(enum PixelFormat *format)
  703. {
  704. switch (*format) {
  705. case PIX_FMT_YUVJ420P:
  706. *format = PIX_FMT_YUV420P;
  707. return 1;
  708. case PIX_FMT_YUVJ422P:
  709. *format = PIX_FMT_YUV422P;
  710. return 1;
  711. case PIX_FMT_YUVJ444P:
  712. *format = PIX_FMT_YUV444P;
  713. return 1;
  714. case PIX_FMT_YUVJ440P:
  715. *format = PIX_FMT_YUV440P;
  716. return 1;
  717. default:
  718. return 0;
  719. }
  720. }
  721. SwsContext *sws_alloc_context(void)
  722. {
  723. SwsContext *c = av_mallocz(sizeof(SwsContext));
  724. c->av_class = &sws_context_class;
  725. av_opt_set_defaults(c);
  726. return c;
  727. }
  728. av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
  729. SwsFilter *dstFilter)
  730. {
  731. int i;
  732. int usesVFilter, usesHFilter;
  733. int unscaled;
  734. SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
  735. int srcW = c->srcW;
  736. int srcH = c->srcH;
  737. int dstW = c->dstW;
  738. int dstH = c->dstH;
  739. int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16);
  740. int dst_stride_px = dst_stride >> 1;
  741. int flags, cpu_flags;
  742. enum PixelFormat srcFormat = c->srcFormat;
  743. enum PixelFormat dstFormat = c->dstFormat;
  744. cpu_flags = av_get_cpu_flags();
  745. flags = c->flags;
  746. emms_c();
  747. if (!rgb15to16)
  748. sws_rgb2rgb_init();
  749. unscaled = (srcW == dstW && srcH == dstH);
  750. if (!sws_isSupportedInput(srcFormat)) {
  751. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
  752. sws_format_name(srcFormat));
  753. return AVERROR(EINVAL);
  754. }
  755. if (!sws_isSupportedOutput(dstFormat)) {
  756. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
  757. sws_format_name(dstFormat));
  758. return AVERROR(EINVAL);
  759. }
  760. i = flags & (SWS_POINT |
  761. SWS_AREA |
  762. SWS_BILINEAR |
  763. SWS_FAST_BILINEAR |
  764. SWS_BICUBIC |
  765. SWS_X |
  766. SWS_GAUSS |
  767. SWS_LANCZOS |
  768. SWS_SINC |
  769. SWS_SPLINE |
  770. SWS_BICUBLIN);
  771. if (!i || (i & (i - 1))) {
  772. av_log(c, AV_LOG_ERROR,
  773. "Exactly one scaler algorithm must be chosen\n");
  774. return AVERROR(EINVAL);
  775. }
  776. /* sanity check */
  777. if (srcW < 4 || srcH < 1 || dstW < 8 || dstH < 1) {
  778. /* FIXME check if these are enough and try to lower them after
  779. * fixing the relevant parts of the code */
  780. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  781. srcW, srcH, dstW, dstH);
  782. return AVERROR(EINVAL);
  783. }
  784. if (!dstFilter)
  785. dstFilter = &dummyFilter;
  786. if (!srcFilter)
  787. srcFilter = &dummyFilter;
  788. c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
  789. c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
  790. c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
  791. c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
  792. c->vRounder = 4 * 0x0001000100010001ULL;
  793. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
  794. (srcFilter->chrV && srcFilter->chrV->length > 1) ||
  795. (dstFilter->lumV && dstFilter->lumV->length > 1) ||
  796. (dstFilter->chrV && dstFilter->chrV->length > 1);
  797. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
  798. (srcFilter->chrH && srcFilter->chrH->length > 1) ||
  799. (dstFilter->lumH && dstFilter->lumH->length > 1) ||
  800. (dstFilter->chrH && dstFilter->chrH->length > 1);
  801. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  802. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  803. /* reuse chroma for 2 pixels RGB/BGR unless user wants full
  804. * chroma interpolation */
  805. if (flags & SWS_FULL_CHR_H_INT &&
  806. isAnyRGB(dstFormat) &&
  807. dstFormat != PIX_FMT_RGBA &&
  808. dstFormat != PIX_FMT_ARGB &&
  809. dstFormat != PIX_FMT_BGRA &&
  810. dstFormat != PIX_FMT_ABGR &&
  811. dstFormat != PIX_FMT_RGB24 &&
  812. dstFormat != PIX_FMT_BGR24) {
  813. av_log(c, AV_LOG_ERROR,
  814. "full chroma interpolation for destination format '%s' not yet implemented\n",
  815. sws_format_name(dstFormat));
  816. flags &= ~SWS_FULL_CHR_H_INT;
  817. c->flags = flags;
  818. }
  819. if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
  820. c->chrDstHSubSample = 1;
  821. // drop some chroma lines if the user wants it
  822. c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
  823. SWS_SRC_V_CHR_DROP_SHIFT;
  824. c->chrSrcVSubSample += c->vChrDrop;
  825. /* drop every other pixel for chroma calculation unless user
  826. * wants full chroma */
  827. if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
  828. srcFormat != PIX_FMT_RGB8 && srcFormat != PIX_FMT_BGR8 &&
  829. srcFormat != PIX_FMT_RGB4 && srcFormat != PIX_FMT_BGR4 &&
  830. srcFormat != PIX_FMT_RGB4_BYTE && srcFormat != PIX_FMT_BGR4_BYTE &&
  831. ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
  832. (flags & SWS_FAST_BILINEAR)))
  833. c->chrSrcHSubSample = 1;
  834. // Note the -((-x)>>y) is so that we always round toward +inf.
  835. c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample);
  836. c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample);
  837. c->chrDstW = -((-dstW) >> c->chrDstHSubSample);
  838. c->chrDstH = -((-dstH) >> c->chrDstVSubSample);
  839. /* unscaled special cases */
  840. if (unscaled && !usesHFilter && !usesVFilter &&
  841. (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  842. ff_get_unscaled_swscale(c);
  843. if (c->swScale) {
  844. if (flags & SWS_PRINT_INFO)
  845. av_log(c, AV_LOG_INFO,
  846. "using unscaled %s -> %s special converter\n",
  847. sws_format_name(srcFormat), sws_format_name(dstFormat));
  848. return 0;
  849. }
  850. }
  851. c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
  852. if (c->srcBpc < 8)
  853. c->srcBpc = 8;
  854. c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
  855. if (c->dstBpc < 8)
  856. c->dstBpc = 8;
  857. if (c->dstBpc == 16)
  858. dst_stride <<= 1;
  859. FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
  860. (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
  861. fail);
  862. if (INLINE_MMXEXT(cpu_flags) && 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 (INLINE_MMX(cpu_flags)) {
  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 = INLINE_MMX(cpu_flags) ? 4 :
  932. (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
  933. 1;
  934. if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
  935. &c->hLumFilterSize, c->lumXInc,
  936. srcW, dstW, filterAlign, 1 << 14,
  937. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  938. cpu_flags, srcFilter->lumH, dstFilter->lumH,
  939. c->param, 1) < 0)
  940. goto fail;
  941. if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
  942. &c->hChrFilterSize, c->chrXInc,
  943. c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
  944. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  945. cpu_flags, srcFilter->chrH, dstFilter->chrH,
  946. c->param, 1) < 0)
  947. goto fail;
  948. }
  949. } // initialize horizontal stuff
  950. /* precalculate vertical scaler filter coefficients */
  951. {
  952. const int filterAlign = INLINE_MMX(cpu_flags) ? 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 (INLINE_MMXEXT(cpu_flags))
  1074. av_log(c, AV_LOG_INFO, "using MMX2\n");
  1075. else if (INLINE_AMD3DNOW(cpu_flags))
  1076. av_log(c, AV_LOG_INFO, "using 3DNOW\n");
  1077. else if (INLINE_MMX(cpu_flags))
  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_INLINE
  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_INLINE */
  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. }