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.

1115 lines
43KB

  1. /*
  2. * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <inttypes.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.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)[9][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. { 36, 68, 60, 92, 34, 66, 58, 90, },
  45. };
  46. DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
  47. 64, 64, 64, 64, 64, 64, 64, 64
  48. };
  49. static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
  50. int height, int y, uint8_t val)
  51. {
  52. int i;
  53. uint8_t *ptr = plane + stride * y;
  54. for (i = 0; i < height; i++) {
  55. memset(ptr, val, width);
  56. ptr += stride;
  57. }
  58. }
  59. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
  60. const uint8_t *_src, const int16_t *filter,
  61. const int32_t *filterPos, int filterSize)
  62. {
  63. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  64. int i;
  65. int32_t *dst = (int32_t *) _dst;
  66. const uint16_t *src = (const uint16_t *) _src;
  67. int bits = desc->comp[0].depth_minus1;
  68. int sh = bits - 4;
  69. if((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth_minus1<15)
  70. sh= 9;
  71. for (i = 0; i < dstW; i++) {
  72. int j;
  73. int srcPos = filterPos[i];
  74. int val = 0;
  75. for (j = 0; j < filterSize; j++) {
  76. val += src[srcPos + j] * filter[filterSize * i + j];
  77. }
  78. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  79. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  80. }
  81. }
  82. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
  83. const uint8_t *_src, const int16_t *filter,
  84. const int32_t *filterPos, int filterSize)
  85. {
  86. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  87. int i;
  88. const uint16_t *src = (const uint16_t *) _src;
  89. int sh = desc->comp[0].depth_minus1;
  90. if(sh<15)
  91. sh= isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : desc->comp[0].depth_minus1;
  92. for (i = 0; i < dstW; i++) {
  93. int j;
  94. int srcPos = filterPos[i];
  95. int val = 0;
  96. for (j = 0; j < filterSize; j++) {
  97. val += src[srcPos + j] * filter[filterSize * i + j];
  98. }
  99. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  100. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  101. }
  102. }
  103. // bilinear / bicubic scaling
  104. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
  105. const uint8_t *src, const int16_t *filter,
  106. const int32_t *filterPos, int filterSize)
  107. {
  108. int i;
  109. for (i = 0; i < dstW; i++) {
  110. int j;
  111. int srcPos = filterPos[i];
  112. int val = 0;
  113. for (j = 0; j < filterSize; j++) {
  114. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  115. }
  116. dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
  117. }
  118. }
  119. static void hScale8To19_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. int32_t *dst = (int32_t *) _dst;
  125. for (i = 0; i < dstW; i++) {
  126. int j;
  127. int srcPos = filterPos[i];
  128. int val = 0;
  129. for (j = 0; j < filterSize; j++) {
  130. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  131. }
  132. dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
  133. }
  134. }
  135. // FIXME all pal and rgb srcFormats could do this conversion as well
  136. // FIXME all scalers more complex than bilinear could do half of this transform
  137. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  138. {
  139. int i;
  140. for (i = 0; i < width; i++) {
  141. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  142. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  143. }
  144. }
  145. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  146. {
  147. int i;
  148. for (i = 0; i < width; i++) {
  149. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  150. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  151. }
  152. }
  153. static void lumRangeToJpeg_c(int16_t *dst, int width)
  154. {
  155. int i;
  156. for (i = 0; i < width; i++)
  157. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  158. }
  159. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  160. {
  161. int i;
  162. for (i = 0; i < width; i++)
  163. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  164. }
  165. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  166. {
  167. int i;
  168. int32_t *dstU = (int32_t *) _dstU;
  169. int32_t *dstV = (int32_t *) _dstV;
  170. for (i = 0; i < width; i++) {
  171. dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  172. dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  173. }
  174. }
  175. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  176. {
  177. int i;
  178. int32_t *dstU = (int32_t *) _dstU;
  179. int32_t *dstV = (int32_t *) _dstV;
  180. for (i = 0; i < width; i++) {
  181. dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  182. dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  183. }
  184. }
  185. static void lumRangeToJpeg16_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] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
  191. }
  192. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  193. {
  194. int i;
  195. int32_t *dst = (int32_t *) _dst;
  196. for (i = 0; i < width; i++)
  197. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  198. }
  199. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  200. const uint8_t *src, int srcW, int xInc)
  201. {
  202. int i;
  203. unsigned int xpos = 0;
  204. for (i = 0; i < dstWidth; i++) {
  205. register unsigned int xx = xpos >> 16;
  206. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  207. dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
  208. xpos += xInc;
  209. }
  210. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  211. dst[i] = src[srcW-1]*128;
  212. }
  213. // *** horizontal scale Y line to temp buffer
  214. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  215. const uint8_t *src_in[4],
  216. int srcW, int xInc,
  217. const int16_t *hLumFilter,
  218. const int32_t *hLumFilterPos,
  219. int hLumFilterSize,
  220. uint8_t *formatConvBuffer,
  221. uint32_t *pal, int isAlpha)
  222. {
  223. void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
  224. isAlpha ? c->alpToYV12 : c->lumToYV12;
  225. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  226. const uint8_t *src = src_in[isAlpha ? 3 : 0];
  227. if (toYV12) {
  228. toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
  229. src = formatConvBuffer;
  230. } else if (c->readLumPlanar && !isAlpha) {
  231. c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
  232. src = formatConvBuffer;
  233. } else if (c->readAlpPlanar && isAlpha) {
  234. c->readAlpPlanar(formatConvBuffer, src_in, srcW, NULL);
  235. src = formatConvBuffer;
  236. }
  237. if (!c->hyscale_fast) {
  238. c->hyScale(c, dst, dstWidth, src, hLumFilter,
  239. hLumFilterPos, hLumFilterSize);
  240. } else { // fast bilinear upscale / crap downscale
  241. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  242. }
  243. if (convertRange)
  244. convertRange(dst, dstWidth);
  245. }
  246. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  247. int dstWidth, const uint8_t *src1,
  248. const uint8_t *src2, int srcW, int xInc)
  249. {
  250. int i;
  251. unsigned int xpos = 0;
  252. for (i = 0; i < dstWidth; i++) {
  253. register unsigned int xx = xpos >> 16;
  254. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  255. dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
  256. dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
  257. xpos += xInc;
  258. }
  259. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  260. dst1[i] = src1[srcW-1]*128;
  261. dst2[i] = src2[srcW-1]*128;
  262. }
  263. }
  264. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
  265. int16_t *dst2, int dstWidth,
  266. const uint8_t *src_in[4],
  267. int srcW, int xInc,
  268. const int16_t *hChrFilter,
  269. const int32_t *hChrFilterPos,
  270. int hChrFilterSize,
  271. uint8_t *formatConvBuffer, uint32_t *pal)
  272. {
  273. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  274. if (c->chrToYV12) {
  275. uint8_t *buf2 = formatConvBuffer +
  276. FFALIGN(srcW*2+78, 16);
  277. c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
  278. src1= formatConvBuffer;
  279. src2= buf2;
  280. } else if (c->readChrPlanar) {
  281. uint8_t *buf2 = formatConvBuffer +
  282. FFALIGN(srcW*2+78, 16);
  283. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
  284. src1 = formatConvBuffer;
  285. src2 = buf2;
  286. }
  287. if (!c->hcscale_fast) {
  288. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  289. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  290. } else { // fast bilinear upscale / crap downscale
  291. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  292. }
  293. if (c->chrConvertRange)
  294. c->chrConvertRange(dst1, dst2, dstWidth);
  295. }
  296. #define DEBUG_SWSCALE_BUFFERS 0
  297. #define DEBUG_BUFFERS(...) \
  298. if (DEBUG_SWSCALE_BUFFERS) \
  299. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  300. static int swscale(SwsContext *c, const uint8_t *src[],
  301. int srcStride[], int srcSliceY,
  302. int srcSliceH, uint8_t *dst[], int dstStride[])
  303. {
  304. /* load a few things into local vars to make the code more readable?
  305. * and faster */
  306. const int srcW = c->srcW;
  307. const int dstW = c->dstW;
  308. const int dstH = c->dstH;
  309. const int chrDstW = c->chrDstW;
  310. const int chrSrcW = c->chrSrcW;
  311. const int lumXInc = c->lumXInc;
  312. const int chrXInc = c->chrXInc;
  313. const enum AVPixelFormat dstFormat = c->dstFormat;
  314. const int flags = c->flags;
  315. int32_t *vLumFilterPos = c->vLumFilterPos;
  316. int32_t *vChrFilterPos = c->vChrFilterPos;
  317. int32_t *hLumFilterPos = c->hLumFilterPos;
  318. int32_t *hChrFilterPos = c->hChrFilterPos;
  319. int16_t *hLumFilter = c->hLumFilter;
  320. int16_t *hChrFilter = c->hChrFilter;
  321. int32_t *lumMmxFilter = c->lumMmxFilter;
  322. int32_t *chrMmxFilter = c->chrMmxFilter;
  323. const int vLumFilterSize = c->vLumFilterSize;
  324. const int vChrFilterSize = c->vChrFilterSize;
  325. const int hLumFilterSize = c->hLumFilterSize;
  326. const int hChrFilterSize = c->hChrFilterSize;
  327. int16_t **lumPixBuf = c->lumPixBuf;
  328. int16_t **chrUPixBuf = c->chrUPixBuf;
  329. int16_t **chrVPixBuf = c->chrVPixBuf;
  330. int16_t **alpPixBuf = c->alpPixBuf;
  331. const int vLumBufSize = c->vLumBufSize;
  332. const int vChrBufSize = c->vChrBufSize;
  333. uint8_t *formatConvBuffer = c->formatConvBuffer;
  334. uint32_t *pal = c->pal_yuv;
  335. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  336. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  337. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  338. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  339. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  340. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  341. yuv2anyX_fn yuv2anyX = c->yuv2anyX;
  342. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  343. const int chrSrcSliceH = FF_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
  344. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  345. is16BPS(c->srcFormat);
  346. int lastDstY;
  347. /* vars which will change and which we need to store back in the context */
  348. int dstY = c->dstY;
  349. int lumBufIndex = c->lumBufIndex;
  350. int chrBufIndex = c->chrBufIndex;
  351. int lastInLumBuf = c->lastInLumBuf;
  352. int lastInChrBuf = c->lastInChrBuf;
  353. if (!usePal(c->srcFormat)) {
  354. pal = c->input_rgb2yuv_table;
  355. }
  356. if (isPacked(c->srcFormat)) {
  357. src[0] =
  358. src[1] =
  359. src[2] =
  360. src[3] = src[0];
  361. srcStride[0] =
  362. srcStride[1] =
  363. srcStride[2] =
  364. srcStride[3] = srcStride[0];
  365. }
  366. srcStride[1] <<= c->vChrDrop;
  367. srcStride[2] <<= c->vChrDrop;
  368. DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  369. src[0], srcStride[0], src[1], srcStride[1],
  370. src[2], srcStride[2], src[3], srcStride[3],
  371. dst[0], dstStride[0], dst[1], dstStride[1],
  372. dst[2], dstStride[2], dst[3], dstStride[3]);
  373. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  374. srcSliceY, srcSliceH, dstY, dstH);
  375. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  376. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  377. if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
  378. dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
  379. static int warnedAlready = 0; // FIXME maybe move this into the context
  380. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  381. av_log(c, AV_LOG_WARNING,
  382. "Warning: dstStride is not aligned!\n"
  383. " ->cannot do aligned memory accesses anymore\n");
  384. warnedAlready = 1;
  385. }
  386. }
  387. if ( (uintptr_t)dst[0]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
  388. || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
  389. || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
  390. || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
  391. ) {
  392. static int warnedAlready=0;
  393. int cpu_flags = av_get_cpu_flags();
  394. if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  395. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  396. warnedAlready=1;
  397. }
  398. }
  399. /* Note the user might start scaling the picture in the middle so this
  400. * will not get executed. This is not really intended but works
  401. * currently, so people might do it. */
  402. if (srcSliceY == 0) {
  403. lumBufIndex = -1;
  404. chrBufIndex = -1;
  405. dstY = 0;
  406. lastInLumBuf = -1;
  407. lastInChrBuf = -1;
  408. }
  409. if (!should_dither) {
  410. c->chrDither8 = c->lumDither8 = sws_pb_64;
  411. }
  412. lastDstY = dstY;
  413. for (; dstY < dstH; dstY++) {
  414. const int chrDstY = dstY >> c->chrDstVSubSample;
  415. uint8_t *dest[4] = {
  416. dst[0] + dstStride[0] * dstY,
  417. dst[1] + dstStride[1] * chrDstY,
  418. dst[2] + dstStride[2] * chrDstY,
  419. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  420. };
  421. int use_mmx_vfilter= c->use_mmx_vfilter;
  422. // First line needed as input
  423. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  424. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  425. // First line needed as input
  426. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  427. // Last line needed as input
  428. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  429. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  430. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  431. int enough_lines;
  432. // handle holes (FAST_BILINEAR & weird filters)
  433. if (firstLumSrcY > lastInLumBuf)
  434. lastInLumBuf = firstLumSrcY - 1;
  435. if (firstChrSrcY > lastInChrBuf)
  436. lastInChrBuf = firstChrSrcY - 1;
  437. av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  438. av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  439. DEBUG_BUFFERS("dstY: %d\n", dstY);
  440. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  441. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  442. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  443. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  444. // Do we have enough lines in this slice to output the dstY line
  445. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  446. lastChrSrcY < FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
  447. if (!enough_lines) {
  448. lastLumSrcY = srcSliceY + srcSliceH - 1;
  449. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  450. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  451. lastLumSrcY, lastChrSrcY);
  452. }
  453. // Do horizontal scaling
  454. while (lastInLumBuf < lastLumSrcY) {
  455. const uint8_t *src1[4] = {
  456. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  457. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  458. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  459. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  460. };
  461. lumBufIndex++;
  462. av_assert0(lumBufIndex < 2 * vLumBufSize);
  463. av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  464. av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
  465. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  466. hLumFilter, hLumFilterPos, hLumFilterSize,
  467. formatConvBuffer, pal, 0);
  468. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  469. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  470. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  471. formatConvBuffer, pal, 1);
  472. lastInLumBuf++;
  473. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  474. lumBufIndex, lastInLumBuf);
  475. }
  476. while (lastInChrBuf < lastChrSrcY) {
  477. const uint8_t *src1[4] = {
  478. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  479. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  480. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  481. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  482. };
  483. chrBufIndex++;
  484. av_assert0(chrBufIndex < 2 * vChrBufSize);
  485. av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  486. av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  487. // FIXME replace parameters through context struct (some at least)
  488. if (c->needs_hcscale)
  489. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  490. chrDstW, src1, chrSrcW, chrXInc,
  491. hChrFilter, hChrFilterPos, hChrFilterSize,
  492. formatConvBuffer, pal);
  493. lastInChrBuf++;
  494. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  495. chrBufIndex, lastInChrBuf);
  496. }
  497. // wrap buf index around to stay inside the ring buffer
  498. if (lumBufIndex >= vLumBufSize)
  499. lumBufIndex -= vLumBufSize;
  500. if (chrBufIndex >= vChrBufSize)
  501. chrBufIndex -= vChrBufSize;
  502. if (!enough_lines)
  503. break; // we can't output a dstY line so let's try with the next slice
  504. #if HAVE_MMX_INLINE
  505. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  506. lastInLumBuf, lastInChrBuf);
  507. #endif
  508. if (should_dither) {
  509. c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
  510. c->lumDither8 = ff_dither_8x8_128[dstY & 7];
  511. }
  512. if (dstY >= dstH - 2) {
  513. /* hmm looks like we can't use MMX here without overwriting
  514. * this array's tail */
  515. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  516. &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
  517. use_mmx_vfilter= 0;
  518. }
  519. {
  520. const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  521. const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  522. const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  523. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  524. (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  525. int16_t *vLumFilter = c->vLumFilter;
  526. int16_t *vChrFilter = c->vChrFilter;
  527. if (isPlanarYUV(dstFormat) ||
  528. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  529. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  530. vLumFilter += dstY * vLumFilterSize;
  531. vChrFilter += chrDstY * vChrFilterSize;
  532. // av_assert0(use_mmx_vfilter != (
  533. // yuv2planeX == yuv2planeX_10BE_c
  534. // || yuv2planeX == yuv2planeX_10LE_c
  535. // || yuv2planeX == yuv2planeX_9BE_c
  536. // || yuv2planeX == yuv2planeX_9LE_c
  537. // || yuv2planeX == yuv2planeX_16BE_c
  538. // || yuv2planeX == yuv2planeX_16LE_c
  539. // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
  540. if(use_mmx_vfilter){
  541. vLumFilter= (int16_t *)c->lumMmxFilter;
  542. vChrFilter= (int16_t *)c->chrMmxFilter;
  543. }
  544. if (vLumFilterSize == 1) {
  545. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  546. } else {
  547. yuv2planeX(vLumFilter, vLumFilterSize,
  548. lumSrcPtr, dest[0],
  549. dstW, c->lumDither8, 0);
  550. }
  551. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  552. if (yuv2nv12cX) {
  553. yuv2nv12cX(c, vChrFilter,
  554. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  555. dest[1], chrDstW);
  556. } else if (vChrFilterSize == 1) {
  557. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  558. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  559. } else {
  560. yuv2planeX(vChrFilter,
  561. vChrFilterSize, chrUSrcPtr, dest[1],
  562. chrDstW, c->chrDither8, 0);
  563. yuv2planeX(vChrFilter,
  564. vChrFilterSize, chrVSrcPtr, dest[2],
  565. chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
  566. }
  567. }
  568. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  569. if(use_mmx_vfilter){
  570. vLumFilter= (int16_t *)c->alpMmxFilter;
  571. }
  572. if (vLumFilterSize == 1) {
  573. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  574. c->lumDither8, 0);
  575. } else {
  576. yuv2planeX(vLumFilter,
  577. vLumFilterSize, alpSrcPtr, dest[3],
  578. dstW, c->lumDither8, 0);
  579. }
  580. }
  581. } else if (yuv2packedX) {
  582. av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
  583. av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
  584. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  585. vChrFilterSize <= 2) { // unscaled RGB
  586. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  587. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  588. alpPixBuf ? *alpSrcPtr : NULL,
  589. dest[0], dstW, chrAlpha, dstY);
  590. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  591. vChrFilterSize == 2) { // bilinear upscale RGB
  592. int lumAlpha = vLumFilter[2 * dstY + 1];
  593. int chrAlpha = vChrFilter[2 * dstY + 1];
  594. lumMmxFilter[2] =
  595. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  596. chrMmxFilter[2] =
  597. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  598. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  599. alpPixBuf ? alpSrcPtr : NULL,
  600. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  601. } else { // general RGB
  602. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  603. lumSrcPtr, vLumFilterSize,
  604. vChrFilter + dstY * vChrFilterSize,
  605. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  606. alpSrcPtr, dest[0], dstW, dstY);
  607. }
  608. } else {
  609. av_assert1(!yuv2packed1 && !yuv2packed2);
  610. yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
  611. lumSrcPtr, vLumFilterSize,
  612. vChrFilter + dstY * vChrFilterSize,
  613. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  614. alpSrcPtr, dest, dstW, dstY);
  615. }
  616. }
  617. }
  618. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
  619. int length = dstW;
  620. int height = dstY - lastDstY;
  621. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  622. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  623. fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
  624. 1, desc->comp[3].depth_minus1,
  625. isBE(dstFormat));
  626. } else
  627. fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
  628. }
  629. #if HAVE_MMXEXT_INLINE
  630. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  631. __asm__ volatile ("sfence" ::: "memory");
  632. #endif
  633. emms_c();
  634. /* store changed local vars back in the context */
  635. c->dstY = dstY;
  636. c->lumBufIndex = lumBufIndex;
  637. c->chrBufIndex = chrBufIndex;
  638. c->lastInLumBuf = lastInLumBuf;
  639. c->lastInChrBuf = lastInChrBuf;
  640. return dstY - lastDstY;
  641. }
  642. static av_cold void sws_init_swscale(SwsContext *c)
  643. {
  644. enum AVPixelFormat srcFormat = c->srcFormat;
  645. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  646. &c->yuv2nv12cX, &c->yuv2packed1,
  647. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  648. ff_sws_init_input_funcs(c);
  649. if (c->srcBpc == 8) {
  650. if (c->dstBpc <= 14) {
  651. c->hyScale = c->hcScale = hScale8To15_c;
  652. if (c->flags & SWS_FAST_BILINEAR) {
  653. c->hyscale_fast = hyscale_fast_c;
  654. c->hcscale_fast = hcscale_fast_c;
  655. }
  656. } else {
  657. c->hyScale = c->hcScale = hScale8To19_c;
  658. }
  659. } else {
  660. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  661. : hScale16To15_c;
  662. }
  663. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  664. if (c->dstBpc <= 14) {
  665. if (c->srcRange) {
  666. c->lumConvertRange = lumRangeFromJpeg_c;
  667. c->chrConvertRange = chrRangeFromJpeg_c;
  668. } else {
  669. c->lumConvertRange = lumRangeToJpeg_c;
  670. c->chrConvertRange = chrRangeToJpeg_c;
  671. }
  672. } else {
  673. if (c->srcRange) {
  674. c->lumConvertRange = lumRangeFromJpeg16_c;
  675. c->chrConvertRange = chrRangeFromJpeg16_c;
  676. } else {
  677. c->lumConvertRange = lumRangeToJpeg16_c;
  678. c->chrConvertRange = chrRangeToJpeg16_c;
  679. }
  680. }
  681. }
  682. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  683. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  684. c->needs_hcscale = 1;
  685. }
  686. SwsFunc ff_getSwsFunc(SwsContext *c)
  687. {
  688. sws_init_swscale(c);
  689. if (ARCH_PPC)
  690. ff_sws_init_swscale_ppc(c);
  691. if (ARCH_X86)
  692. ff_sws_init_swscale_x86(c);
  693. return swscale;
  694. }
  695. static void reset_ptr(const uint8_t *src[], int format)
  696. {
  697. if (!isALPHA(format))
  698. src[3] = NULL;
  699. if (!isPlanar(format)) {
  700. src[3] = src[2] = NULL;
  701. if (!usePal(format))
  702. src[1] = NULL;
  703. }
  704. }
  705. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  706. const int linesizes[4])
  707. {
  708. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  709. int i;
  710. for (i = 0; i < 4; i++) {
  711. int plane = desc->comp[i].plane;
  712. if (!data[plane] || !linesizes[plane])
  713. return 0;
  714. }
  715. return 1;
  716. }
  717. static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
  718. const uint16_t *src, int stride, int h)
  719. {
  720. int xp,yp;
  721. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
  722. for (yp=0; yp<h; yp++) {
  723. for (xp=0; xp+2<stride; xp+=3) {
  724. int x, y, z, r, g, b;
  725. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  726. x = AV_RB16(src + xp + 0);
  727. y = AV_RB16(src + xp + 1);
  728. z = AV_RB16(src + xp + 2);
  729. } else {
  730. x = AV_RL16(src + xp + 0);
  731. y = AV_RL16(src + xp + 1);
  732. z = AV_RL16(src + xp + 2);
  733. }
  734. x = c->xyzgamma[x>>4];
  735. y = c->xyzgamma[y>>4];
  736. z = c->xyzgamma[z>>4];
  737. // convert from XYZlinear to sRGBlinear
  738. r = c->xyz2rgb_matrix[0][0] * x +
  739. c->xyz2rgb_matrix[0][1] * y +
  740. c->xyz2rgb_matrix[0][2] * z >> 12;
  741. g = c->xyz2rgb_matrix[1][0] * x +
  742. c->xyz2rgb_matrix[1][1] * y +
  743. c->xyz2rgb_matrix[1][2] * z >> 12;
  744. b = c->xyz2rgb_matrix[2][0] * x +
  745. c->xyz2rgb_matrix[2][1] * y +
  746. c->xyz2rgb_matrix[2][2] * z >> 12;
  747. // limit values to 12-bit depth
  748. r = av_clip_c(r,0,4095);
  749. g = av_clip_c(g,0,4095);
  750. b = av_clip_c(b,0,4095);
  751. // convert from sRGBlinear to RGB and scale from 12bit to 16bit
  752. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  753. AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
  754. AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
  755. AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
  756. } else {
  757. AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
  758. AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
  759. AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
  760. }
  761. }
  762. src += stride;
  763. dst += stride;
  764. }
  765. }
  766. static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
  767. const uint16_t *src, int stride, int h)
  768. {
  769. int xp,yp;
  770. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  771. for (yp=0; yp<h; yp++) {
  772. for (xp=0; xp+2<stride; xp+=3) {
  773. int x, y, z, r, g, b;
  774. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  775. r = AV_RB16(src + xp + 0);
  776. g = AV_RB16(src + xp + 1);
  777. b = AV_RB16(src + xp + 2);
  778. } else {
  779. r = AV_RL16(src + xp + 0);
  780. g = AV_RL16(src + xp + 1);
  781. b = AV_RL16(src + xp + 2);
  782. }
  783. r = c->rgbgammainv[r>>4];
  784. g = c->rgbgammainv[g>>4];
  785. b = c->rgbgammainv[b>>4];
  786. // convert from sRGBlinear to XYZlinear
  787. x = c->rgb2xyz_matrix[0][0] * r +
  788. c->rgb2xyz_matrix[0][1] * g +
  789. c->rgb2xyz_matrix[0][2] * b >> 12;
  790. y = c->rgb2xyz_matrix[1][0] * r +
  791. c->rgb2xyz_matrix[1][1] * g +
  792. c->rgb2xyz_matrix[1][2] * b >> 12;
  793. z = c->rgb2xyz_matrix[2][0] * r +
  794. c->rgb2xyz_matrix[2][1] * g +
  795. c->rgb2xyz_matrix[2][2] * b >> 12;
  796. // limit values to 12-bit depth
  797. x = av_clip_c(x,0,4095);
  798. y = av_clip_c(y,0,4095);
  799. z = av_clip_c(z,0,4095);
  800. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  801. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  802. AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
  803. AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
  804. AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
  805. } else {
  806. AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
  807. AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
  808. AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
  809. }
  810. }
  811. src += stride;
  812. dst += stride;
  813. }
  814. }
  815. /**
  816. * swscale wrapper, so we don't need to export the SwsContext.
  817. * Assumes planar YUV to be in YUV order instead of YVU.
  818. */
  819. int attribute_align_arg sws_scale(struct SwsContext *c,
  820. const uint8_t * const srcSlice[],
  821. const int srcStride[], int srcSliceY,
  822. int srcSliceH, uint8_t *const dst[],
  823. const int dstStride[])
  824. {
  825. int i, ret;
  826. const uint8_t *src2[4];
  827. uint8_t *dst2[4];
  828. uint8_t *rgb0_tmp = NULL;
  829. if (!srcSlice || !dstStride || !dst || !srcSlice) {
  830. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  831. return 0;
  832. }
  833. memcpy(src2, srcSlice, sizeof(src2));
  834. memcpy(dst2, dst, sizeof(dst2));
  835. // do not mess up sliceDir if we have a "trailing" 0-size slice
  836. if (srcSliceH == 0)
  837. return 0;
  838. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  839. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  840. return 0;
  841. }
  842. if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
  843. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  844. return 0;
  845. }
  846. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  847. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  848. return 0;
  849. }
  850. if (c->sliceDir == 0) {
  851. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  852. }
  853. if (usePal(c->srcFormat)) {
  854. for (i = 0; i < 256; i++) {
  855. int p, r, g, b, y, u, v, a = 0xff;
  856. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  857. p = ((const uint32_t *)(srcSlice[1]))[i];
  858. a = (p >> 24) & 0xFF;
  859. r = (p >> 16) & 0xFF;
  860. g = (p >> 8) & 0xFF;
  861. b = p & 0xFF;
  862. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  863. r = ( i >> 5 ) * 36;
  864. g = ((i >> 2) & 7) * 36;
  865. b = ( i & 3) * 85;
  866. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  867. b = ( i >> 6 ) * 85;
  868. g = ((i >> 3) & 7) * 36;
  869. r = ( i & 7) * 36;
  870. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  871. r = ( i >> 3 ) * 255;
  872. g = ((i >> 1) & 3) * 85;
  873. b = ( i & 1) * 255;
  874. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  875. r = g = b = i;
  876. } else {
  877. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  878. b = ( i >> 3 ) * 255;
  879. g = ((i >> 1) & 3) * 85;
  880. r = ( i & 1) * 255;
  881. }
  882. #define RGB2YUV_SHIFT 15
  883. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  884. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  885. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  886. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  887. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  888. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  889. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  890. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  891. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  892. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  893. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  894. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  895. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  896. switch (c->dstFormat) {
  897. case AV_PIX_FMT_BGR32:
  898. #if !HAVE_BIGENDIAN
  899. case AV_PIX_FMT_RGB24:
  900. #endif
  901. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  902. break;
  903. case AV_PIX_FMT_BGR32_1:
  904. #if HAVE_BIGENDIAN
  905. case AV_PIX_FMT_BGR24:
  906. #endif
  907. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  908. break;
  909. case AV_PIX_FMT_RGB32_1:
  910. #if HAVE_BIGENDIAN
  911. case AV_PIX_FMT_RGB24:
  912. #endif
  913. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  914. break;
  915. case AV_PIX_FMT_RGB32:
  916. #if !HAVE_BIGENDIAN
  917. case AV_PIX_FMT_BGR24:
  918. #endif
  919. default:
  920. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  921. }
  922. }
  923. }
  924. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  925. uint8_t *base;
  926. int x,y;
  927. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  928. if (!rgb0_tmp)
  929. return AVERROR(ENOMEM);
  930. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  931. for (y=0; y<srcSliceH; y++){
  932. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  933. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  934. base[ srcStride[0]*y + x] = 0xFF;
  935. }
  936. }
  937. src2[0] = base;
  938. }
  939. if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  940. uint8_t *base;
  941. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  942. if (!rgb0_tmp)
  943. return AVERROR(ENOMEM);
  944. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  945. xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
  946. src2[0] = base;
  947. }
  948. if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
  949. for (i = 0; i < 4; i++)
  950. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  951. // copy strides, so they can safely be modified
  952. if (c->sliceDir == 1) {
  953. // slices go from top to bottom
  954. int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
  955. srcStride[3] };
  956. int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
  957. dstStride[3] };
  958. reset_ptr(src2, c->srcFormat);
  959. reset_ptr((void*)dst2, c->dstFormat);
  960. /* reset slice direction at end of frame */
  961. if (srcSliceY + srcSliceH == c->srcH)
  962. c->sliceDir = 0;
  963. ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
  964. dstStride2);
  965. } else {
  966. // slices go from bottom to top => we flip the image internally
  967. int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
  968. -srcStride[3] };
  969. int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
  970. -dstStride[3] };
  971. src2[0] += (srcSliceH - 1) * srcStride[0];
  972. if (!usePal(c->srcFormat))
  973. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  974. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  975. src2[3] += (srcSliceH - 1) * srcStride[3];
  976. dst2[0] += ( c->dstH - 1) * dstStride[0];
  977. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  978. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  979. dst2[3] += ( c->dstH - 1) * dstStride[3];
  980. reset_ptr(src2, c->srcFormat);
  981. reset_ptr((void*)dst2, c->dstFormat);
  982. /* reset slice direction at end of frame */
  983. if (!srcSliceY)
  984. c->sliceDir = 0;
  985. ret = c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
  986. srcSliceH, dst2, dstStride2);
  987. }
  988. if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  989. /* replace on the same data */
  990. rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);
  991. }
  992. av_free(rgb0_tmp);
  993. return ret;
  994. }