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.

775 lines
30KB

  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 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_minus1;
  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_minus1;
  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. }
  247. if (!c->hyscale_fast) {
  248. c->hyScale(c, dst, dstWidth, src, hLumFilter,
  249. hLumFilterPos, hLumFilterSize);
  250. } else { // fast bilinear upscale / crap downscale
  251. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  252. }
  253. if (convertRange)
  254. convertRange(dst, dstWidth);
  255. }
  256. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  257. int dstWidth, const uint8_t *src1,
  258. const uint8_t *src2, int srcW, int xInc)
  259. {
  260. int i;
  261. unsigned int xpos = 0;
  262. for (i = 0; i < dstWidth; i++) {
  263. register unsigned int xx = xpos >> 16;
  264. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  265. dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
  266. dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
  267. xpos += xInc;
  268. }
  269. }
  270. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
  271. int16_t *dst2, int dstWidth,
  272. const uint8_t *src_in[4],
  273. int srcW, int xInc,
  274. const int16_t *hChrFilter,
  275. const int32_t *hChrFilterPos,
  276. int hChrFilterSize,
  277. uint8_t *formatConvBuffer, uint32_t *pal)
  278. {
  279. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  280. if (c->chrToYV12) {
  281. uint8_t *buf2 = formatConvBuffer +
  282. FFALIGN(srcW * FFALIGN(c->srcBpc, 8) >> 3, 16);
  283. c->chrToYV12(formatConvBuffer, buf2, src1, src2, srcW, pal);
  284. src1 = formatConvBuffer;
  285. src2 = buf2;
  286. } else if (c->readChrPlanar) {
  287. uint8_t *buf2 = formatConvBuffer +
  288. FFALIGN(srcW * FFALIGN(c->srcBpc, 8) >> 3, 16);
  289. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
  290. src1 = formatConvBuffer;
  291. src2 = buf2;
  292. }
  293. if (!c->hcscale_fast) {
  294. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  295. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  296. } else { // fast bilinear upscale / crap downscale
  297. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  298. }
  299. if (c->chrConvertRange)
  300. c->chrConvertRange(dst1, dst2, dstWidth);
  301. }
  302. #define DEBUG_SWSCALE_BUFFERS 0
  303. #define DEBUG_BUFFERS(...) \
  304. if (DEBUG_SWSCALE_BUFFERS) \
  305. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  306. static int swScale(SwsContext *c, const uint8_t *src[],
  307. int srcStride[], int srcSliceY,
  308. int srcSliceH, uint8_t *dst[], int dstStride[])
  309. {
  310. /* load a few things into local vars to make the code more readable?
  311. * and faster */
  312. const int srcW = c->srcW;
  313. const int dstW = c->dstW;
  314. const int dstH = c->dstH;
  315. const int chrDstW = c->chrDstW;
  316. const int chrSrcW = c->chrSrcW;
  317. const int lumXInc = c->lumXInc;
  318. const int chrXInc = c->chrXInc;
  319. const enum AVPixelFormat dstFormat = c->dstFormat;
  320. const int flags = c->flags;
  321. int32_t *vLumFilterPos = c->vLumFilterPos;
  322. int32_t *vChrFilterPos = c->vChrFilterPos;
  323. int32_t *hLumFilterPos = c->hLumFilterPos;
  324. int32_t *hChrFilterPos = c->hChrFilterPos;
  325. int16_t *vLumFilter = c->vLumFilter;
  326. int16_t *vChrFilter = c->vChrFilter;
  327. int16_t *hLumFilter = c->hLumFilter;
  328. int16_t *hChrFilter = c->hChrFilter;
  329. int32_t *lumMmxFilter = c->lumMmxFilter;
  330. int32_t *chrMmxFilter = c->chrMmxFilter;
  331. const int vLumFilterSize = c->vLumFilterSize;
  332. const int vChrFilterSize = c->vChrFilterSize;
  333. const int hLumFilterSize = c->hLumFilterSize;
  334. const int hChrFilterSize = c->hChrFilterSize;
  335. int16_t **lumPixBuf = c->lumPixBuf;
  336. int16_t **chrUPixBuf = c->chrUPixBuf;
  337. int16_t **chrVPixBuf = c->chrVPixBuf;
  338. int16_t **alpPixBuf = c->alpPixBuf;
  339. const int vLumBufSize = c->vLumBufSize;
  340. const int vChrBufSize = c->vChrBufSize;
  341. uint8_t *formatConvBuffer = c->formatConvBuffer;
  342. uint32_t *pal = c->pal_yuv;
  343. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  344. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  345. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  346. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  347. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  348. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  349. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  350. const int chrSrcSliceH = -((-srcSliceH) >> c->chrSrcVSubSample);
  351. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  352. is16BPS(c->srcFormat);
  353. int lastDstY;
  354. /* vars which will change and which we need to store back in the context */
  355. int dstY = c->dstY;
  356. int lumBufIndex = c->lumBufIndex;
  357. int chrBufIndex = c->chrBufIndex;
  358. int lastInLumBuf = c->lastInLumBuf;
  359. int lastInChrBuf = c->lastInChrBuf;
  360. if (isPacked(c->srcFormat)) {
  361. src[0] =
  362. src[1] =
  363. src[2] =
  364. src[3] = src[0];
  365. srcStride[0] =
  366. srcStride[1] =
  367. srcStride[2] =
  368. srcStride[3] = srcStride[0];
  369. }
  370. srcStride[1] <<= c->vChrDrop;
  371. srcStride[2] <<= c->vChrDrop;
  372. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  373. src[0], srcStride[0], src[1], srcStride[1],
  374. src[2], srcStride[2], src[3], srcStride[3],
  375. dst[0], dstStride[0], dst[1], dstStride[1],
  376. dst[2], dstStride[2], dst[3], dstStride[3]);
  377. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  378. srcSliceY, srcSliceH, dstY, dstH);
  379. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  380. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  381. if (dstStride[0] % 8 != 0 || dstStride[1] % 8 != 0 ||
  382. dstStride[2] % 8 != 0 || dstStride[3] % 8 != 0) {
  383. static int warnedAlready = 0; // FIXME maybe move this into the context
  384. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  385. av_log(c, AV_LOG_WARNING,
  386. "Warning: dstStride is not aligned!\n"
  387. " ->cannot do aligned memory accesses anymore\n");
  388. warnedAlready = 1;
  389. }
  390. }
  391. /* Note the user might start scaling the picture in the middle so this
  392. * will not get executed. This is not really intended but works
  393. * currently, so people might do it. */
  394. if (srcSliceY == 0) {
  395. lumBufIndex = -1;
  396. chrBufIndex = -1;
  397. dstY = 0;
  398. lastInLumBuf = -1;
  399. lastInChrBuf = -1;
  400. }
  401. if (!should_dither) {
  402. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  403. }
  404. lastDstY = dstY;
  405. for (; dstY < dstH; dstY++) {
  406. const int chrDstY = dstY >> c->chrDstVSubSample;
  407. uint8_t *dest[4] = {
  408. dst[0] + dstStride[0] * dstY,
  409. dst[1] + dstStride[1] * chrDstY,
  410. dst[2] + dstStride[2] * chrDstY,
  411. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  412. };
  413. // First line needed as input
  414. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  415. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  416. // First line needed as input
  417. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  418. // Last line needed as input
  419. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  420. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  421. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  422. int enough_lines;
  423. // handle holes (FAST_BILINEAR & weird filters)
  424. if (firstLumSrcY > lastInLumBuf)
  425. lastInLumBuf = firstLumSrcY - 1;
  426. if (firstChrSrcY > lastInChrBuf)
  427. lastInChrBuf = firstChrSrcY - 1;
  428. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  429. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  430. DEBUG_BUFFERS("dstY: %d\n", dstY);
  431. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  432. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  433. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  434. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  435. // Do we have enough lines in this slice to output the dstY line
  436. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  437. lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
  438. if (!enough_lines) {
  439. lastLumSrcY = srcSliceY + srcSliceH - 1;
  440. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  441. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  442. lastLumSrcY, lastChrSrcY);
  443. }
  444. // Do horizontal scaling
  445. while (lastInLumBuf < lastLumSrcY) {
  446. const uint8_t *src1[4] = {
  447. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  448. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  449. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  450. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  451. };
  452. lumBufIndex++;
  453. assert(lumBufIndex < 2 * vLumBufSize);
  454. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  455. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  456. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  457. hLumFilter, hLumFilterPos, hLumFilterSize,
  458. formatConvBuffer, pal, 0);
  459. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  460. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  461. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  462. formatConvBuffer, pal, 1);
  463. lastInLumBuf++;
  464. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  465. lumBufIndex, lastInLumBuf);
  466. }
  467. while (lastInChrBuf < lastChrSrcY) {
  468. const uint8_t *src1[4] = {
  469. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  470. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  471. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  472. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  473. };
  474. chrBufIndex++;
  475. assert(chrBufIndex < 2 * vChrBufSize);
  476. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  477. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  478. // FIXME replace parameters through context struct (some at least)
  479. if (c->needs_hcscale)
  480. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  481. chrDstW, src1, chrSrcW, chrXInc,
  482. hChrFilter, hChrFilterPos, hChrFilterSize,
  483. formatConvBuffer, pal);
  484. lastInChrBuf++;
  485. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  486. chrBufIndex, lastInChrBuf);
  487. }
  488. // wrap buf index around to stay inside the ring buffer
  489. if (lumBufIndex >= vLumBufSize)
  490. lumBufIndex -= vLumBufSize;
  491. if (chrBufIndex >= vChrBufSize)
  492. chrBufIndex -= vChrBufSize;
  493. if (!enough_lines)
  494. break; // we can't output a dstY line so let's try with the next slice
  495. #if HAVE_MMX_INLINE
  496. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  497. lastInLumBuf, lastInChrBuf);
  498. #endif
  499. if (should_dither) {
  500. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  501. c->lumDither8 = dither_8x8_128[dstY & 7];
  502. }
  503. if (dstY >= dstH - 2) {
  504. /* hmm looks like we can't use MMX here without overwriting
  505. * this array's tail */
  506. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  507. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  508. }
  509. {
  510. const int16_t **lumSrcPtr = (const int16_t **)lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  511. const int16_t **chrUSrcPtr = (const int16_t **)chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  512. const int16_t **chrVSrcPtr = (const int16_t **)chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  513. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  514. (const int16_t **)alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  515. if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
  516. const int16_t **tmpY = (const int16_t **)lumPixBuf +
  517. 2 * vLumBufSize;
  518. int neg = -firstLumSrcY, i;
  519. int end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
  520. for (i = 0; i < neg; i++)
  521. tmpY[i] = lumSrcPtr[neg];
  522. for (; i < end; i++)
  523. tmpY[i] = lumSrcPtr[i];
  524. for (; i < vLumFilterSize; i++)
  525. tmpY[i] = tmpY[i - 1];
  526. lumSrcPtr = tmpY;
  527. if (alpSrcPtr) {
  528. const int16_t **tmpA = (const int16_t **)alpPixBuf +
  529. 2 * vLumBufSize;
  530. for (i = 0; i < neg; i++)
  531. tmpA[i] = alpSrcPtr[neg];
  532. for (; i < end; i++)
  533. tmpA[i] = alpSrcPtr[i];
  534. for (; i < vLumFilterSize; i++)
  535. tmpA[i] = tmpA[i - 1];
  536. alpSrcPtr = tmpA;
  537. }
  538. }
  539. if (firstChrSrcY < 0 ||
  540. firstChrSrcY + vChrFilterSize > c->chrSrcH) {
  541. const int16_t **tmpU = (const int16_t **)chrUPixBuf + 2 * vChrBufSize,
  542. **tmpV = (const int16_t **)chrVPixBuf + 2 * vChrBufSize;
  543. int neg = -firstChrSrcY, i;
  544. int end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
  545. for (i = 0; i < neg; i++) {
  546. tmpU[i] = chrUSrcPtr[neg];
  547. tmpV[i] = chrVSrcPtr[neg];
  548. }
  549. for (; i < end; i++) {
  550. tmpU[i] = chrUSrcPtr[i];
  551. tmpV[i] = chrVSrcPtr[i];
  552. }
  553. for (; i < vChrFilterSize; i++) {
  554. tmpU[i] = tmpU[i - 1];
  555. tmpV[i] = tmpV[i - 1];
  556. }
  557. chrUSrcPtr = tmpU;
  558. chrVSrcPtr = tmpV;
  559. }
  560. if (isPlanarYUV(dstFormat) ||
  561. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  562. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  563. if (vLumFilterSize == 1) {
  564. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  565. } else {
  566. yuv2planeX(vLumFilter + dstY * vLumFilterSize,
  567. vLumFilterSize, lumSrcPtr, dest[0],
  568. dstW, c->lumDither8, 0);
  569. }
  570. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  571. if (yuv2nv12cX) {
  572. yuv2nv12cX(c, vChrFilter + chrDstY * vChrFilterSize,
  573. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  574. dest[1], chrDstW);
  575. } else if (vChrFilterSize == 1) {
  576. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  577. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  578. } else {
  579. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize,
  580. vChrFilterSize, chrUSrcPtr, dest[1],
  581. chrDstW, c->chrDither8, 0);
  582. yuv2planeX(vChrFilter + chrDstY * vChrFilterSize,
  583. vChrFilterSize, chrVSrcPtr, dest[2],
  584. chrDstW, c->chrDither8, 3);
  585. }
  586. }
  587. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  588. if (vLumFilterSize == 1) {
  589. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  590. c->lumDither8, 0);
  591. } else {
  592. yuv2planeX(vLumFilter + dstY * vLumFilterSize,
  593. vLumFilterSize, alpSrcPtr, dest[3],
  594. dstW, c->lumDither8, 0);
  595. }
  596. }
  597. } else {
  598. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  599. vChrFilterSize <= 2) { // unscaled RGB
  600. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  601. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  602. alpPixBuf ? *alpSrcPtr : NULL,
  603. dest[0], dstW, chrAlpha, dstY);
  604. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  605. vChrFilterSize == 2) { // bilinear upscale RGB
  606. int lumAlpha = vLumFilter[2 * dstY + 1];
  607. int chrAlpha = vChrFilter[2 * dstY + 1];
  608. lumMmxFilter[2] =
  609. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  610. chrMmxFilter[2] =
  611. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  612. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  613. alpPixBuf ? alpSrcPtr : NULL,
  614. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  615. } else { // general RGB
  616. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  617. lumSrcPtr, vLumFilterSize,
  618. vChrFilter + dstY * vChrFilterSize,
  619. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  620. alpSrcPtr, dest[0], dstW, dstY);
  621. }
  622. }
  623. }
  624. }
  625. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
  626. int length = dstW;
  627. int height = dstY - lastDstY;
  628. if (is16BPS(c->dstFormat))
  629. length *= 2;
  630. if (is9_OR_10BPS(dstFormat)) {
  631. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  632. fill_plane9or10(dst[3], dstStride[3], length, height, lastDstY,
  633. 255, desc->comp[3].depth_minus1 + 1,
  634. isBE(dstFormat));
  635. } else
  636. fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
  637. }
  638. #if HAVE_MMXEXT_INLINE
  639. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  640. __asm__ volatile ("sfence" ::: "memory");
  641. #endif
  642. emms_c();
  643. /* store changed local vars back in the context */
  644. c->dstY = dstY;
  645. c->lumBufIndex = lumBufIndex;
  646. c->chrBufIndex = chrBufIndex;
  647. c->lastInLumBuf = lastInLumBuf;
  648. c->lastInChrBuf = lastInChrBuf;
  649. return dstY - lastDstY;
  650. }
  651. static av_cold void sws_init_swScale_c(SwsContext *c)
  652. {
  653. enum AVPixelFormat srcFormat = c->srcFormat;
  654. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  655. &c->yuv2nv12cX, &c->yuv2packed1,
  656. &c->yuv2packed2, &c->yuv2packedX);
  657. ff_sws_init_input_funcs(c);
  658. if (c->srcBpc == 8) {
  659. if (c->dstBpc <= 10) {
  660. c->hyScale = c->hcScale = hScale8To15_c;
  661. if (c->flags & SWS_FAST_BILINEAR) {
  662. c->hyscale_fast = hyscale_fast_c;
  663. c->hcscale_fast = hcscale_fast_c;
  664. }
  665. } else {
  666. c->hyScale = c->hcScale = hScale8To19_c;
  667. }
  668. } else {
  669. c->hyScale = c->hcScale = c->dstBpc > 10 ? hScale16To19_c
  670. : hScale16To15_c;
  671. }
  672. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  673. if (c->dstBpc <= 10) {
  674. if (c->srcRange) {
  675. c->lumConvertRange = lumRangeFromJpeg_c;
  676. c->chrConvertRange = chrRangeFromJpeg_c;
  677. } else {
  678. c->lumConvertRange = lumRangeToJpeg_c;
  679. c->chrConvertRange = chrRangeToJpeg_c;
  680. }
  681. } else {
  682. if (c->srcRange) {
  683. c->lumConvertRange = lumRangeFromJpeg16_c;
  684. c->chrConvertRange = chrRangeFromJpeg16_c;
  685. } else {
  686. c->lumConvertRange = lumRangeToJpeg16_c;
  687. c->chrConvertRange = chrRangeToJpeg16_c;
  688. }
  689. }
  690. }
  691. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  692. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  693. c->needs_hcscale = 1;
  694. }
  695. SwsFunc ff_getSwsFunc(SwsContext *c)
  696. {
  697. sws_init_swScale_c(c);
  698. if (HAVE_MMX)
  699. ff_sws_init_swScale_mmx(c);
  700. if (HAVE_ALTIVEC)
  701. ff_sws_init_swScale_altivec(c);
  702. return swScale;
  703. }