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.

945 lines
37KB

  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 convertion 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 ((int)dst[0]%16 || (int)dst[1]%16 || (int)dst[2]%16 || (int)src[0]%16 || (int)src[1]%16 || (int)src[2]%16
  381. || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
  382. || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
  383. ) {
  384. static int warnedAlready=0;
  385. int cpu_flags = av_get_cpu_flags();
  386. if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  387. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  388. warnedAlready=1;
  389. }
  390. }
  391. /* Note the user might start scaling the picture in the middle so this
  392. * will not get executed. This is not really intended but works
  393. * currently, so people might do it. */
  394. if (srcSliceY == 0) {
  395. lumBufIndex = -1;
  396. chrBufIndex = -1;
  397. dstY = 0;
  398. lastInLumBuf = -1;
  399. lastInChrBuf = -1;
  400. }
  401. if (!should_dither) {
  402. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  403. }
  404. lastDstY = dstY;
  405. for (; dstY < dstH; dstY++) {
  406. const int chrDstY = dstY >> c->chrDstVSubSample;
  407. uint8_t *dest[4] = {
  408. dst[0] + dstStride[0] * dstY,
  409. dst[1] + dstStride[1] * chrDstY,
  410. dst[2] + dstStride[2] * chrDstY,
  411. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  412. };
  413. int use_mmx_vfilter= c->use_mmx_vfilter;
  414. // First line needed as input
  415. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  416. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  417. // First line needed as input
  418. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  419. // Last line needed as input
  420. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  421. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  422. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  423. int enough_lines;
  424. // handle holes (FAST_BILINEAR & weird filters)
  425. if (firstLumSrcY > lastInLumBuf)
  426. lastInLumBuf = firstLumSrcY - 1;
  427. if (firstChrSrcY > lastInChrBuf)
  428. lastInChrBuf = firstChrSrcY - 1;
  429. av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  430. av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  431. DEBUG_BUFFERS("dstY: %d\n", dstY);
  432. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  433. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  434. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  435. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  436. // Do we have enough lines in this slice to output the dstY line
  437. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  438. lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
  439. if (!enough_lines) {
  440. lastLumSrcY = srcSliceY + srcSliceH - 1;
  441. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  442. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  443. lastLumSrcY, lastChrSrcY);
  444. }
  445. // Do horizontal scaling
  446. while (lastInLumBuf < lastLumSrcY) {
  447. const uint8_t *src1[4] = {
  448. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  449. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  450. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  451. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  452. };
  453. lumBufIndex++;
  454. av_assert0(lumBufIndex < 2 * vLumBufSize);
  455. av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  456. av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
  457. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  458. hLumFilter, hLumFilterPos, hLumFilterSize,
  459. formatConvBuffer, pal, 0);
  460. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  461. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  462. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  463. formatConvBuffer, pal, 1);
  464. lastInLumBuf++;
  465. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  466. lumBufIndex, lastInLumBuf);
  467. }
  468. while (lastInChrBuf < lastChrSrcY) {
  469. const uint8_t *src1[4] = {
  470. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  471. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  472. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  473. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  474. };
  475. chrBufIndex++;
  476. av_assert0(chrBufIndex < 2 * vChrBufSize);
  477. av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  478. av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  479. // FIXME replace parameters through context struct (some at least)
  480. if (c->needs_hcscale)
  481. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  482. chrDstW, src1, chrSrcW, chrXInc,
  483. hChrFilter, hChrFilterPos, hChrFilterSize,
  484. formatConvBuffer, pal);
  485. lastInChrBuf++;
  486. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  487. chrBufIndex, lastInChrBuf);
  488. }
  489. // wrap buf index around to stay inside the ring buffer
  490. if (lumBufIndex >= vLumBufSize)
  491. lumBufIndex -= vLumBufSize;
  492. if (chrBufIndex >= vChrBufSize)
  493. chrBufIndex -= vChrBufSize;
  494. if (!enough_lines)
  495. break; // we can't output a dstY line so let's try with the next slice
  496. #if HAVE_MMX_INLINE
  497. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  498. lastInLumBuf, lastInChrBuf);
  499. #endif
  500. if (should_dither) {
  501. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  502. c->lumDither8 = dither_8x8_128[dstY & 7];
  503. }
  504. if (dstY >= dstH - 2) {
  505. /* hmm looks like we can't use MMX here without overwriting
  506. * this array's tail */
  507. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  508. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  509. use_mmx_vfilter= 0;
  510. }
  511. {
  512. const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  513. const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  514. const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  515. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  516. (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  517. int16_t *vLumFilter = c->vLumFilter;
  518. int16_t *vChrFilter = c->vChrFilter;
  519. if (isPlanarYUV(dstFormat) ||
  520. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  521. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  522. vLumFilter += dstY * vLumFilterSize;
  523. vChrFilter += chrDstY * vChrFilterSize;
  524. // av_assert0(use_mmx_vfilter != (
  525. // yuv2planeX == yuv2planeX_10BE_c
  526. // || yuv2planeX == yuv2planeX_10LE_c
  527. // || yuv2planeX == yuv2planeX_9BE_c
  528. // || yuv2planeX == yuv2planeX_9LE_c
  529. // || yuv2planeX == yuv2planeX_16BE_c
  530. // || yuv2planeX == yuv2planeX_16LE_c
  531. // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
  532. if(use_mmx_vfilter){
  533. vLumFilter= c->lumMmxFilter;
  534. vChrFilter= c->chrMmxFilter;
  535. }
  536. if (vLumFilterSize == 1) {
  537. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  538. } else {
  539. yuv2planeX(vLumFilter, vLumFilterSize,
  540. lumSrcPtr, dest[0],
  541. dstW, c->lumDither8, 0);
  542. }
  543. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  544. if (yuv2nv12cX) {
  545. yuv2nv12cX(c, vChrFilter,
  546. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  547. dest[1], chrDstW);
  548. } else if (vChrFilterSize == 1) {
  549. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  550. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  551. } else {
  552. yuv2planeX(vChrFilter,
  553. vChrFilterSize, chrUSrcPtr, dest[1],
  554. chrDstW, c->chrDither8, 0);
  555. yuv2planeX(vChrFilter,
  556. vChrFilterSize, chrVSrcPtr, dest[2],
  557. chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
  558. }
  559. }
  560. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  561. if(use_mmx_vfilter){
  562. vLumFilter= c->alpMmxFilter;
  563. }
  564. if (vLumFilterSize == 1) {
  565. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  566. c->lumDither8, 0);
  567. } else {
  568. yuv2planeX(vLumFilter,
  569. vLumFilterSize, alpSrcPtr, dest[3],
  570. dstW, c->lumDither8, 0);
  571. }
  572. }
  573. } else {
  574. av_assert1(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize * 2);
  575. av_assert1(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize * 2);
  576. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  577. vChrFilterSize <= 2) { // unscaled RGB
  578. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  579. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  580. alpPixBuf ? *alpSrcPtr : NULL,
  581. dest[0], dstW, chrAlpha, dstY);
  582. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  583. vChrFilterSize == 2) { // bilinear upscale RGB
  584. int lumAlpha = vLumFilter[2 * dstY + 1];
  585. int chrAlpha = vChrFilter[2 * dstY + 1];
  586. lumMmxFilter[2] =
  587. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  588. chrMmxFilter[2] =
  589. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  590. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  591. alpPixBuf ? alpSrcPtr : NULL,
  592. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  593. } else { // general RGB
  594. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  595. lumSrcPtr, vLumFilterSize,
  596. vChrFilter + dstY * vChrFilterSize,
  597. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  598. alpSrcPtr, dest[0], dstW, dstY);
  599. }
  600. }
  601. }
  602. }
  603. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf)
  604. fillPlane(dst[3], dstStride[3], dstW, dstY - lastDstY, lastDstY, 255);
  605. #if HAVE_MMXEXT_INLINE
  606. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  607. __asm__ volatile ("sfence" ::: "memory");
  608. #endif
  609. emms_c();
  610. /* store changed local vars back in the context */
  611. c->dstY = dstY;
  612. c->lumBufIndex = lumBufIndex;
  613. c->chrBufIndex = chrBufIndex;
  614. c->lastInLumBuf = lastInLumBuf;
  615. c->lastInChrBuf = lastInChrBuf;
  616. return dstY - lastDstY;
  617. }
  618. static av_cold void sws_init_swScale_c(SwsContext *c)
  619. {
  620. enum AVPixelFormat srcFormat = c->srcFormat;
  621. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  622. &c->yuv2nv12cX, &c->yuv2packed1,
  623. &c->yuv2packed2, &c->yuv2packedX);
  624. ff_sws_init_input_funcs(c);
  625. if (c->srcBpc == 8) {
  626. if (c->dstBpc <= 14) {
  627. c->hyScale = c->hcScale = hScale8To15_c;
  628. if (c->flags & SWS_FAST_BILINEAR) {
  629. c->hyscale_fast = hyscale_fast_c;
  630. c->hcscale_fast = hcscale_fast_c;
  631. }
  632. } else {
  633. c->hyScale = c->hcScale = hScale8To19_c;
  634. }
  635. } else {
  636. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  637. : hScale16To15_c;
  638. }
  639. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  640. if (c->dstBpc <= 14) {
  641. if (c->srcRange) {
  642. c->lumConvertRange = lumRangeFromJpeg_c;
  643. c->chrConvertRange = chrRangeFromJpeg_c;
  644. } else {
  645. c->lumConvertRange = lumRangeToJpeg_c;
  646. c->chrConvertRange = chrRangeToJpeg_c;
  647. }
  648. } else {
  649. if (c->srcRange) {
  650. c->lumConvertRange = lumRangeFromJpeg16_c;
  651. c->chrConvertRange = chrRangeFromJpeg16_c;
  652. } else {
  653. c->lumConvertRange = lumRangeToJpeg16_c;
  654. c->chrConvertRange = chrRangeToJpeg16_c;
  655. }
  656. }
  657. }
  658. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  659. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  660. c->needs_hcscale = 1;
  661. }
  662. SwsFunc ff_getSwsFunc(SwsContext *c)
  663. {
  664. sws_init_swScale_c(c);
  665. if (HAVE_MMX)
  666. ff_sws_init_swScale_mmx(c);
  667. if (HAVE_ALTIVEC)
  668. ff_sws_init_swScale_altivec(c);
  669. return swScale;
  670. }
  671. static void reset_ptr(const uint8_t *src[], int format)
  672. {
  673. if (!isALPHA(format))
  674. src[3] = NULL;
  675. if (!isPlanar(format)) {
  676. src[3] = src[2] = NULL;
  677. if (!usePal(format))
  678. src[1] = NULL;
  679. }
  680. }
  681. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  682. const int linesizes[4])
  683. {
  684. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  685. int i;
  686. for (i = 0; i < 4; i++) {
  687. int plane = desc->comp[i].plane;
  688. if (!data[plane] || !linesizes[plane])
  689. return 0;
  690. }
  691. return 1;
  692. }
  693. /**
  694. * swscale wrapper, so we don't need to export the SwsContext.
  695. * Assumes planar YUV to be in YUV order instead of YVU.
  696. */
  697. int attribute_align_arg sws_scale(struct SwsContext *c,
  698. const uint8_t * const srcSlice[],
  699. const int srcStride[], int srcSliceY,
  700. int srcSliceH, uint8_t *const dst[],
  701. const int dstStride[])
  702. {
  703. int i, ret;
  704. const uint8_t *src2[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
  705. uint8_t *dst2[4] = { dst[0], dst[1], dst[2], dst[3] };
  706. uint8_t *rgb0_tmp = NULL;
  707. // do not mess up sliceDir if we have a "trailing" 0-size slice
  708. if (srcSliceH == 0)
  709. return 0;
  710. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  711. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  712. return 0;
  713. }
  714. if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
  715. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  716. return 0;
  717. }
  718. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  719. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  720. return 0;
  721. }
  722. if (c->sliceDir == 0) {
  723. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  724. }
  725. if (usePal(c->srcFormat)) {
  726. for (i = 0; i < 256; i++) {
  727. int p, r, g, b, y, u, v, a = 0xff;
  728. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  729. p = ((const uint32_t *)(srcSlice[1]))[i];
  730. a = (p >> 24) & 0xFF;
  731. r = (p >> 16) & 0xFF;
  732. g = (p >> 8) & 0xFF;
  733. b = p & 0xFF;
  734. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  735. r = ( i >> 5 ) * 36;
  736. g = ((i >> 2) & 7) * 36;
  737. b = ( i & 3) * 85;
  738. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  739. b = ( i >> 6 ) * 85;
  740. g = ((i >> 3) & 7) * 36;
  741. r = ( i & 7) * 36;
  742. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  743. r = ( i >> 3 ) * 255;
  744. g = ((i >> 1) & 3) * 85;
  745. b = ( i & 1) * 255;
  746. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  747. r = g = b = i;
  748. } else {
  749. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  750. b = ( i >> 3 ) * 255;
  751. g = ((i >> 1) & 3) * 85;
  752. r = ( i & 1) * 255;
  753. }
  754. #define RGB2YUV_SHIFT 15
  755. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  756. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  757. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  758. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  759. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  760. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  761. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  762. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  763. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  764. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  765. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  766. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  767. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  768. switch (c->dstFormat) {
  769. case AV_PIX_FMT_BGR32:
  770. #if !HAVE_BIGENDIAN
  771. case AV_PIX_FMT_RGB24:
  772. #endif
  773. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  774. break;
  775. case AV_PIX_FMT_BGR32_1:
  776. #if HAVE_BIGENDIAN
  777. case AV_PIX_FMT_BGR24:
  778. #endif
  779. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  780. break;
  781. case AV_PIX_FMT_RGB32_1:
  782. #if HAVE_BIGENDIAN
  783. case AV_PIX_FMT_RGB24:
  784. #endif
  785. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  786. break;
  787. case AV_PIX_FMT_RGB32:
  788. #if !HAVE_BIGENDIAN
  789. case AV_PIX_FMT_BGR24:
  790. #endif
  791. default:
  792. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  793. }
  794. }
  795. }
  796. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  797. uint8_t *base;
  798. int x,y;
  799. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  800. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  801. for (y=0; y<srcSliceH; y++){
  802. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  803. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  804. base[ srcStride[0]*y + x] = 0xFF;
  805. }
  806. }
  807. src2[0] = base;
  808. }
  809. // copy strides, so they can safely be modified
  810. if (c->sliceDir == 1) {
  811. // slices go from top to bottom
  812. int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
  813. srcStride[3] };
  814. int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
  815. dstStride[3] };
  816. reset_ptr(src2, c->srcFormat);
  817. reset_ptr((void*)dst2, c->dstFormat);
  818. /* reset slice direction at end of frame */
  819. if (srcSliceY + srcSliceH == c->srcH)
  820. c->sliceDir = 0;
  821. ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
  822. dstStride2);
  823. } else {
  824. // slices go from bottom to top => we flip the image internally
  825. int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
  826. -srcStride[3] };
  827. int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
  828. -dstStride[3] };
  829. src2[0] += (srcSliceH - 1) * srcStride[0];
  830. if (!usePal(c->srcFormat))
  831. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  832. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  833. src2[3] += (srcSliceH - 1) * srcStride[3];
  834. dst2[0] += ( c->dstH - 1) * dstStride[0];
  835. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  836. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  837. dst2[3] += ( c->dstH - 1) * dstStride[3];
  838. reset_ptr(src2, c->srcFormat);
  839. reset_ptr((void*)dst2, c->dstFormat);
  840. /* reset slice direction at end of frame */
  841. if (!srcSliceY)
  842. c->sliceDir = 0;
  843. ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
  844. srcSliceH, dst2, dstStride2);
  845. }
  846. av_free(rgb0_tmp);
  847. return ret;
  848. }