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.

1278 lines
48KB

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