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.

1155 lines
44KB

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