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.

967 lines
38KB

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