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.

1089 lines
42KB

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