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.

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