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.

1281 lines
49KB

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