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.

741 lines
29KB

  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 <assert.h>
  21. #include <inttypes.h>
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/bswap.h"
  27. #include "libavutil/cpu.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "config.h"
  32. #include "rgb2rgb.h"
  33. #include "swscale_internal.h"
  34. #include "swscale.h"
  35. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128)[8][8] = {
  36. { 36, 68, 60, 92, 34, 66, 58, 90, },
  37. { 100, 4, 124, 28, 98, 2, 122, 26, },
  38. { 52, 84, 44, 76, 50, 82, 42, 74, },
  39. { 116, 20, 108, 12, 114, 18, 106, 10, },
  40. { 32, 64, 56, 88, 38, 70, 62, 94, },
  41. { 96, 0, 120, 24, 102, 6, 126, 30, },
  42. { 48, 80, 40, 72, 54, 86, 46, 78, },
  43. { 112, 16, 104, 8, 118, 22, 110, 14, },
  44. };
  45. DECLARE_ALIGNED(8, const uint8_t, ff_sws_pb_64)[8] = {
  46. 64, 64, 64, 64, 64, 64, 64, 64
  47. };
  48. static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
  49. int height, int y, uint8_t val)
  50. {
  51. int i;
  52. uint8_t *ptr = plane + stride * y;
  53. for (i = 0; i < height; i++) {
  54. memset(ptr, val, width);
  55. ptr += stride;
  56. }
  57. }
  58. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
  59. const uint8_t *_src, const int16_t *filter,
  60. const int32_t *filterPos, int filterSize)
  61. {
  62. int i;
  63. int32_t *dst = (int32_t *) _dst;
  64. const uint16_t *src = (const uint16_t *) _src;
  65. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  66. int sh = bits - 4;
  67. for (i = 0; i < dstW; i++) {
  68. int j;
  69. int srcPos = filterPos[i];
  70. int val = 0;
  71. for (j = 0; j < filterSize; j++) {
  72. val += src[srcPos + j] * filter[filterSize * i + j];
  73. }
  74. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  75. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  76. }
  77. }
  78. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
  79. const uint8_t *_src, const int16_t *filter,
  80. const int32_t *filterPos, int filterSize)
  81. {
  82. int i;
  83. const uint16_t *src = (const uint16_t *) _src;
  84. int sh = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  85. for (i = 0; i < dstW; i++) {
  86. int j;
  87. int srcPos = filterPos[i];
  88. int val = 0;
  89. for (j = 0; j < filterSize; j++) {
  90. val += src[srcPos + j] * filter[filterSize * i + j];
  91. }
  92. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  93. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  94. }
  95. }
  96. // bilinear / bicubic scaling
  97. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
  98. const uint8_t *src, const int16_t *filter,
  99. const int32_t *filterPos, int filterSize)
  100. {
  101. int i;
  102. for (i = 0; i < dstW; i++) {
  103. int j;
  104. int srcPos = filterPos[i];
  105. int val = 0;
  106. for (j = 0; j < filterSize; j++) {
  107. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  108. }
  109. dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
  110. }
  111. }
  112. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
  113. const uint8_t *src, const int16_t *filter,
  114. const int32_t *filterPos, int filterSize)
  115. {
  116. int i;
  117. int32_t *dst = (int32_t *) _dst;
  118. for (i = 0; i < dstW; i++) {
  119. int j;
  120. int srcPos = filterPos[i];
  121. int val = 0;
  122. for (j = 0; j < filterSize; j++) {
  123. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  124. }
  125. dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
  126. }
  127. }
  128. // FIXME all pal and rgb srcFormats could do this convertion as well
  129. // FIXME all scalers more complex than bilinear could do half of this transform
  130. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  131. {
  132. int i;
  133. for (i = 0; i < width; i++) {
  134. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  135. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  136. }
  137. }
  138. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  139. {
  140. int i;
  141. for (i = 0; i < width; i++) {
  142. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  143. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  144. }
  145. }
  146. static void lumRangeToJpeg_c(int16_t *dst, int width)
  147. {
  148. int i;
  149. for (i = 0; i < width; i++)
  150. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  151. }
  152. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  153. {
  154. int i;
  155. for (i = 0; i < width; i++)
  156. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  157. }
  158. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  159. {
  160. int i;
  161. int32_t *dstU = (int32_t *) _dstU;
  162. int32_t *dstV = (int32_t *) _dstV;
  163. for (i = 0; i < width; i++) {
  164. dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  165. dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  166. }
  167. }
  168. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  169. {
  170. int i;
  171. int32_t *dstU = (int32_t *) _dstU;
  172. int32_t *dstV = (int32_t *) _dstV;
  173. for (i = 0; i < width; i++) {
  174. dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  175. dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  176. }
  177. }
  178. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  179. {
  180. int i;
  181. int32_t *dst = (int32_t *) _dst;
  182. for (i = 0; i < width; i++)
  183. dst[i] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
  184. }
  185. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  186. {
  187. int i;
  188. int32_t *dst = (int32_t *) _dst;
  189. for (i = 0; i < width; i++)
  190. dst[i] = (dst[i] * 14071 + (33561947 << 4)) >> 14;
  191. }
  192. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  193. const uint8_t *src, int srcW, int xInc)
  194. {
  195. int i;
  196. unsigned int xpos = 0;
  197. for (i = 0; i < dstWidth; i++) {
  198. register unsigned int xx = xpos >> 16;
  199. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  200. dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
  201. xpos += xInc;
  202. }
  203. }
  204. // *** horizontal scale Y line to temp buffer
  205. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  206. const uint8_t *src_in[4],
  207. int srcW, int xInc,
  208. const int16_t *hLumFilter,
  209. const int32_t *hLumFilterPos,
  210. int hLumFilterSize,
  211. uint8_t *formatConvBuffer,
  212. uint32_t *pal, int isAlpha)
  213. {
  214. void (*toYV12)(uint8_t *, const uint8_t *, int, uint32_t *) =
  215. isAlpha ? c->alpToYV12 : c->lumToYV12;
  216. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  217. const uint8_t *src = src_in[isAlpha ? 3 : 0];
  218. if (toYV12) {
  219. toYV12(formatConvBuffer, src, srcW, pal);
  220. src = formatConvBuffer;
  221. } else if (c->readLumPlanar && !isAlpha) {
  222. c->readLumPlanar(formatConvBuffer, src_in, srcW);
  223. src = formatConvBuffer;
  224. }
  225. if (!c->hyscale_fast) {
  226. c->hyScale(c, dst, dstWidth, src, hLumFilter,
  227. hLumFilterPos, hLumFilterSize);
  228. } else { // fast bilinear upscale / crap downscale
  229. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  230. }
  231. if (convertRange)
  232. convertRange(dst, dstWidth);
  233. }
  234. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  235. int dstWidth, const uint8_t *src1,
  236. const uint8_t *src2, int srcW, int xInc)
  237. {
  238. int i;
  239. unsigned int xpos = 0;
  240. for (i = 0; i < dstWidth; i++) {
  241. register unsigned int xx = xpos >> 16;
  242. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  243. dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
  244. dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
  245. xpos += xInc;
  246. }
  247. }
  248. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
  249. int16_t *dst2, int dstWidth,
  250. const uint8_t *src_in[4],
  251. int srcW, int xInc,
  252. const int16_t *hChrFilter,
  253. const int32_t *hChrFilterPos,
  254. int hChrFilterSize,
  255. uint8_t *formatConvBuffer, uint32_t *pal)
  256. {
  257. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  258. if (c->chrToYV12) {
  259. uint8_t *buf2 = formatConvBuffer +
  260. FFALIGN(srcW * FFALIGN(c->srcBpc, 8) >> 3, 16);
  261. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  262. src1 = formatConvBuffer;
  263. src2 = buf2;
  264. } else if (c->readChrPlanar) {
  265. uint8_t *buf2 = formatConvBuffer +
  266. FFALIGN(srcW * FFALIGN(c->srcBpc, 8) >> 3, 16);
  267. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
  268. src1 = formatConvBuffer;
  269. src2 = buf2;
  270. }
  271. if (!c->hcscale_fast) {
  272. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  273. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  274. } else { // fast bilinear upscale / crap downscale
  275. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  276. }
  277. if (c->chrConvertRange)
  278. c->chrConvertRange(dst1, dst2, dstWidth);
  279. }
  280. #define DEBUG_SWSCALE_BUFFERS 0
  281. #define DEBUG_BUFFERS(...) \
  282. if (DEBUG_SWSCALE_BUFFERS) \
  283. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  284. static int swScale(SwsContext *c, const uint8_t *src[],
  285. int srcStride[], int srcSliceY,
  286. int srcSliceH, uint8_t *dst[], int dstStride[])
  287. {
  288. /* load a few things into local vars to make the code more readable?
  289. * and faster */
  290. const int srcW = c->srcW;
  291. const int dstW = c->dstW;
  292. const int dstH = c->dstH;
  293. const int chrDstW = c->chrDstW;
  294. const int chrSrcW = c->chrSrcW;
  295. const int lumXInc = c->lumXInc;
  296. const int chrXInc = c->chrXInc;
  297. const enum PixelFormat dstFormat = c->dstFormat;
  298. const int flags = c->flags;
  299. int32_t *vLumFilterPos = c->vLumFilterPos;
  300. int32_t *vChrFilterPos = c->vChrFilterPos;
  301. int32_t *hLumFilterPos = c->hLumFilterPos;
  302. int32_t *hChrFilterPos = c->hChrFilterPos;
  303. int16_t *vLumFilter = c->vLumFilter;
  304. int16_t *vChrFilter = c->vChrFilter;
  305. int16_t *hLumFilter = c->hLumFilter;
  306. int16_t *hChrFilter = c->hChrFilter;
  307. int32_t *lumMmxFilter = c->lumMmxFilter;
  308. int32_t *chrMmxFilter = c->chrMmxFilter;
  309. const int vLumFilterSize = c->vLumFilterSize;
  310. const int vChrFilterSize = c->vChrFilterSize;
  311. const int hLumFilterSize = c->hLumFilterSize;
  312. const int hChrFilterSize = c->hChrFilterSize;
  313. int16_t **lumPixBuf = c->lumPixBuf;
  314. int16_t **chrUPixBuf = c->chrUPixBuf;
  315. int16_t **chrVPixBuf = c->chrVPixBuf;
  316. int16_t **alpPixBuf = c->alpPixBuf;
  317. const int vLumBufSize = c->vLumBufSize;
  318. const int vChrBufSize = c->vChrBufSize;
  319. uint8_t *formatConvBuffer = c->formatConvBuffer;
  320. uint32_t *pal = c->pal_yuv;
  321. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  322. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  323. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  324. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  325. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  326. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  327. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  328. const int chrSrcSliceH = -((-srcSliceH) >> c->chrSrcVSubSample);
  329. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  330. is16BPS(c->srcFormat);
  331. int lastDstY;
  332. /* vars which will change and which we need to store back in the context */
  333. int dstY = c->dstY;
  334. int lumBufIndex = c->lumBufIndex;
  335. int chrBufIndex = c->chrBufIndex;
  336. int lastInLumBuf = c->lastInLumBuf;
  337. int lastInChrBuf = c->lastInChrBuf;
  338. if (isPacked(c->srcFormat)) {
  339. src[0] =
  340. src[1] =
  341. src[2] =
  342. src[3] = src[0];
  343. srcStride[0] =
  344. srcStride[1] =
  345. srcStride[2] =
  346. srcStride[3] = srcStride[0];
  347. }
  348. srcStride[1] <<= c->vChrDrop;
  349. srcStride[2] <<= c->vChrDrop;
  350. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  351. src[0], srcStride[0], src[1], srcStride[1],
  352. src[2], srcStride[2], src[3], srcStride[3],
  353. dst[0], dstStride[0], dst[1], dstStride[1],
  354. dst[2], dstStride[2], dst[3], dstStride[3]);
  355. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  356. srcSliceY, srcSliceH, dstY, dstH);
  357. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  358. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  359. if (dstStride[0] % 8 != 0 || dstStride[1] % 8 != 0 ||
  360. dstStride[2] % 8 != 0 || dstStride[3] % 8 != 0) {
  361. static int warnedAlready = 0; // FIXME maybe move this into the context
  362. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  363. av_log(c, AV_LOG_WARNING,
  364. "Warning: dstStride is not aligned!\n"
  365. " ->cannot do aligned memory accesses anymore\n");
  366. warnedAlready = 1;
  367. }
  368. }
  369. /* Note the user might start scaling the picture in the middle so this
  370. * will not get executed. This is not really intended but works
  371. * currently, so people might do it. */
  372. if (srcSliceY == 0) {
  373. lumBufIndex = -1;
  374. chrBufIndex = -1;
  375. dstY = 0;
  376. lastInLumBuf = -1;
  377. lastInChrBuf = -1;
  378. }
  379. if (!should_dither) {
  380. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  381. }
  382. lastDstY = dstY;
  383. for (; dstY < dstH; dstY++) {
  384. const int chrDstY = dstY >> c->chrDstVSubSample;
  385. uint8_t *dest[4] = {
  386. dst[0] + dstStride[0] * dstY,
  387. dst[1] + dstStride[1] * chrDstY,
  388. dst[2] + dstStride[2] * chrDstY,
  389. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  390. };
  391. // First line needed as input
  392. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  393. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  394. // First line needed as input
  395. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  396. // Last line needed as input
  397. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  398. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  399. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  400. int enough_lines;
  401. // handle holes (FAST_BILINEAR & weird filters)
  402. if (firstLumSrcY > lastInLumBuf)
  403. lastInLumBuf = firstLumSrcY - 1;
  404. if (firstChrSrcY > lastInChrBuf)
  405. lastInChrBuf = firstChrSrcY - 1;
  406. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  407. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  408. DEBUG_BUFFERS("dstY: %d\n", dstY);
  409. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  410. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  411. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  412. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  413. // Do we have enough lines in this slice to output the dstY line
  414. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  415. lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
  416. if (!enough_lines) {
  417. lastLumSrcY = srcSliceY + srcSliceH - 1;
  418. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  419. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  420. lastLumSrcY, lastChrSrcY);
  421. }
  422. // Do horizontal scaling
  423. while (lastInLumBuf < lastLumSrcY) {
  424. const uint8_t *src1[4] = {
  425. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  426. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  427. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  428. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  429. };
  430. lumBufIndex++;
  431. assert(lumBufIndex < 2 * vLumBufSize);
  432. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  433. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  434. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  435. hLumFilter, hLumFilterPos, hLumFilterSize,
  436. formatConvBuffer, pal, 0);
  437. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  438. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  439. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  440. formatConvBuffer, pal, 1);
  441. lastInLumBuf++;
  442. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  443. lumBufIndex, lastInLumBuf);
  444. }
  445. while (lastInChrBuf < lastChrSrcY) {
  446. const uint8_t *src1[4] = {
  447. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  448. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  449. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  450. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  451. };
  452. chrBufIndex++;
  453. assert(chrBufIndex < 2 * vChrBufSize);
  454. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  455. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  456. // FIXME replace parameters through context struct (some at least)
  457. if (c->needs_hcscale)
  458. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  459. chrDstW, src1, chrSrcW, chrXInc,
  460. hChrFilter, hChrFilterPos, hChrFilterSize,
  461. formatConvBuffer, pal);
  462. lastInChrBuf++;
  463. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  464. chrBufIndex, lastInChrBuf);
  465. }
  466. // wrap buf index around to stay inside the ring buffer
  467. if (lumBufIndex >= vLumBufSize)
  468. lumBufIndex -= vLumBufSize;
  469. if (chrBufIndex >= vChrBufSize)
  470. chrBufIndex -= vChrBufSize;
  471. if (!enough_lines)
  472. break; // we can't output a dstY line so let's try with the next slice
  473. #if HAVE_MMX
  474. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  475. lastInLumBuf, lastInChrBuf);
  476. #endif
  477. if (should_dither) {
  478. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  479. c->lumDither8 = dither_8x8_128[dstY & 7];
  480. }
  481. if (dstY >= dstH - 2) {
  482. /* hmm looks like we can't use MMX here without overwriting
  483. * this array's tail */
  484. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  485. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  486. }
  487. {
  488. const int16_t **lumSrcPtr = (const int16_t **)lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  489. const int16_t **chrUSrcPtr = (const int16_t **)chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  490. const int16_t **chrVSrcPtr = (const int16_t **)chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  491. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  492. (const int16_t **)alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  493. if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
  494. const int16_t **tmpY = (const int16_t **)lumPixBuf +
  495. 2 * vLumBufSize;
  496. int neg = -firstLumSrcY, i;
  497. int end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
  498. for (i = 0; i < neg; i++)
  499. tmpY[i] = lumSrcPtr[neg];
  500. for (; i < end; i++)
  501. tmpY[i] = lumSrcPtr[i];
  502. for (; i < vLumFilterSize; i++)
  503. tmpY[i] = tmpY[i - 1];
  504. lumSrcPtr = tmpY;
  505. if (alpSrcPtr) {
  506. const int16_t **tmpA = (const int16_t **)alpPixBuf +
  507. 2 * vLumBufSize;
  508. for (i = 0; i < neg; i++)
  509. tmpA[i] = alpSrcPtr[neg];
  510. for (; i < end; i++)
  511. tmpA[i] = alpSrcPtr[i];
  512. for (; i < vLumFilterSize; i++)
  513. tmpA[i] = tmpA[i - 1];
  514. alpSrcPtr = tmpA;
  515. }
  516. }
  517. if (firstChrSrcY < 0 ||
  518. firstChrSrcY + vChrFilterSize > c->chrSrcH) {
  519. const int16_t **tmpU = (const int16_t **)chrUPixBuf + 2 * vChrBufSize,
  520. **tmpV = (const int16_t **)chrVPixBuf + 2 * vChrBufSize;
  521. int neg = -firstChrSrcY, i;
  522. int end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
  523. for (i = 0; i < neg; i++) {
  524. tmpU[i] = chrUSrcPtr[neg];
  525. tmpV[i] = chrVSrcPtr[neg];
  526. }
  527. for (; i < end; i++) {
  528. tmpU[i] = chrUSrcPtr[i];
  529. tmpV[i] = chrVSrcPtr[i];
  530. }
  531. for (; i < vChrFilterSize; i++) {
  532. tmpU[i] = tmpU[i - 1];
  533. tmpV[i] = tmpV[i - 1];
  534. }
  535. chrUSrcPtr = tmpU;
  536. chrVSrcPtr = tmpV;
  537. }
  538. if (isPlanarYUV(dstFormat) ||
  539. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  540. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  541. if (vLumFilterSize == 1) {
  542. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  543. } else {
  544. yuv2planeX(vLumFilter + dstY * vLumFilterSize,
  545. vLumFilterSize, lumSrcPtr, dest[0],
  546. dstW, c->lumDither8, 0);
  547. }
  548. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  549. if (yuv2nv12cX) {
  550. yuv2nv12cX(c, vChrFilter + chrDstY * vChrFilterSize,
  551. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  552. dest[1], chrDstW);
  553. } else if (vChrFilterSize == 1) {
  554. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  555. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  556. } else {
  557. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize,
  558. vChrFilterSize, chrUSrcPtr, dest[1],
  559. chrDstW, c->chrDither8, 0);
  560. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize,
  561. vChrFilterSize, chrVSrcPtr, dest[2],
  562. chrDstW, c->chrDither8, 3);
  563. }
  564. }
  565. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  566. if (vLumFilterSize == 1) {
  567. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  568. c->lumDither8, 0);
  569. } else {
  570. yuv2planeX(vLumFilter + dstY * vLumFilterSize,
  571. vLumFilterSize, alpSrcPtr, dest[3],
  572. dstW, c->lumDither8, 0);
  573. }
  574. }
  575. } else {
  576. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize * 2);
  577. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize * 2);
  578. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  579. vChrFilterSize <= 2) { // unscaled RGB
  580. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  581. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  582. alpPixBuf ? *alpSrcPtr : NULL,
  583. dest[0], dstW, chrAlpha, dstY);
  584. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  585. vChrFilterSize == 2) { // bilinear upscale RGB
  586. int lumAlpha = vLumFilter[2 * dstY + 1];
  587. int chrAlpha = vChrFilter[2 * dstY + 1];
  588. lumMmxFilter[2] =
  589. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  590. chrMmxFilter[2] =
  591. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  592. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  593. alpPixBuf ? alpSrcPtr : NULL,
  594. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  595. } else { // general RGB
  596. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  597. lumSrcPtr, vLumFilterSize,
  598. vChrFilter + dstY * vChrFilterSize,
  599. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  600. alpSrcPtr, dest[0], dstW, dstY);
  601. }
  602. }
  603. }
  604. }
  605. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf)
  606. fillPlane(dst[3], dstStride[3], dstW, dstY - lastDstY, lastDstY, 255);
  607. #if HAVE_MMX2
  608. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  609. __asm__ volatile ("sfence" ::: "memory");
  610. #endif
  611. emms_c();
  612. /* store changed local vars back in the context */
  613. c->dstY = dstY;
  614. c->lumBufIndex = lumBufIndex;
  615. c->chrBufIndex = chrBufIndex;
  616. c->lastInLumBuf = lastInLumBuf;
  617. c->lastInChrBuf = lastInChrBuf;
  618. return dstY - lastDstY;
  619. }
  620. static av_cold void sws_init_swScale_c(SwsContext *c)
  621. {
  622. enum PixelFormat srcFormat = c->srcFormat;
  623. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  624. &c->yuv2nv12cX, &c->yuv2packed1,
  625. &c->yuv2packed2, &c->yuv2packedX);
  626. ff_sws_init_input_funcs(c);
  627. if (c->srcBpc == 8) {
  628. if (c->dstBpc <= 10) {
  629. c->hyScale = c->hcScale = hScale8To15_c;
  630. if (c->flags & SWS_FAST_BILINEAR) {
  631. c->hyscale_fast = hyscale_fast_c;
  632. c->hcscale_fast = hcscale_fast_c;
  633. }
  634. } else {
  635. c->hyScale = c->hcScale = hScale8To19_c;
  636. }
  637. } else {
  638. c->hyScale = c->hcScale = c->dstBpc > 10 ? hScale16To19_c
  639. : hScale16To15_c;
  640. }
  641. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  642. if (c->dstBpc <= 10) {
  643. if (c->srcRange) {
  644. c->lumConvertRange = lumRangeFromJpeg_c;
  645. c->chrConvertRange = chrRangeFromJpeg_c;
  646. } else {
  647. c->lumConvertRange = lumRangeToJpeg_c;
  648. c->chrConvertRange = chrRangeToJpeg_c;
  649. }
  650. } else {
  651. if (c->srcRange) {
  652. c->lumConvertRange = lumRangeFromJpeg16_c;
  653. c->chrConvertRange = chrRangeFromJpeg16_c;
  654. } else {
  655. c->lumConvertRange = lumRangeToJpeg16_c;
  656. c->chrConvertRange = chrRangeToJpeg16_c;
  657. }
  658. }
  659. }
  660. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  661. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  662. c->needs_hcscale = 1;
  663. }
  664. SwsFunc ff_getSwsFunc(SwsContext *c)
  665. {
  666. sws_init_swScale_c(c);
  667. if (HAVE_MMX)
  668. ff_sws_init_swScale_mmx(c);
  669. if (HAVE_ALTIVEC)
  670. ff_sws_init_swScale_altivec(c);
  671. return swScale;
  672. }