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.

784 lines
31KB

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