swscale.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. int i;
  64. int32_t *dst = (int32_t *) _dst;
  65. const uint16_t *src = (const uint16_t *) _src;
  66. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  67. int sh = bits - 4;
  68. if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  69. sh= 9;
  70. for (i = 0; i < dstW; i++) {
  71. int j;
  72. int srcPos = filterPos[i];
  73. int val = 0;
  74. for (j = 0; j < filterSize; j++) {
  75. val += src[srcPos + j] * filter[filterSize * i + j];
  76. }
  77. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  78. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  79. }
  80. }
  81. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
  82. const uint8_t *_src, const int16_t *filter,
  83. const int32_t *filterPos, int filterSize)
  84. {
  85. int i;
  86. const uint16_t *src = (const uint16_t *) _src;
  87. int sh = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  88. if(sh<15)
  89. sh= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  90. for (i = 0; i < dstW; i++) {
  91. int j;
  92. int srcPos = filterPos[i];
  93. int val = 0;
  94. for (j = 0; j < filterSize; j++) {
  95. val += src[srcPos + j] * filter[filterSize * i + j];
  96. }
  97. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  98. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  99. }
  100. }
  101. // bilinear / bicubic scaling
  102. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
  103. const uint8_t *src, const int16_t *filter,
  104. const int32_t *filterPos, int filterSize)
  105. {
  106. int i;
  107. for (i = 0; i < dstW; i++) {
  108. int j;
  109. int srcPos = filterPos[i];
  110. int val = 0;
  111. for (j = 0; j < filterSize; j++) {
  112. val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
  113. }
  114. dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
  115. }
  116. }
  117. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
  118. const uint8_t *src, const int16_t *filter,
  119. const int32_t *filterPos, int filterSize)
  120. {
  121. int i;
  122. int32_t *dst = (int32_t *) _dst;
  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 >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
  131. }
  132. }
  133. // FIXME all pal and rgb srcFormats could do this convertion as well
  134. // FIXME all scalers more complex than bilinear could do half of this transform
  135. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  136. {
  137. int i;
  138. for (i = 0; i < width; i++) {
  139. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  140. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  141. }
  142. }
  143. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  144. {
  145. int i;
  146. for (i = 0; i < width; i++) {
  147. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  148. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  149. }
  150. }
  151. static void lumRangeToJpeg_c(int16_t *dst, int width)
  152. {
  153. int i;
  154. for (i = 0; i < width; i++)
  155. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  156. }
  157. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  158. {
  159. int i;
  160. for (i = 0; i < width; i++)
  161. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  162. }
  163. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  164. {
  165. int i;
  166. int32_t *dstU = (int32_t *) _dstU;
  167. int32_t *dstV = (int32_t *) _dstV;
  168. for (i = 0; i < width; i++) {
  169. dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  170. dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
  171. }
  172. }
  173. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  174. {
  175. int i;
  176. int32_t *dstU = (int32_t *) _dstU;
  177. int32_t *dstV = (int32_t *) _dstV;
  178. for (i = 0; i < width; i++) {
  179. dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  180. dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
  181. }
  182. }
  183. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  184. {
  185. int i;
  186. int32_t *dst = (int32_t *) _dst;
  187. for (i = 0; i < width; i++)
  188. dst[i] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
  189. }
  190. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  191. {
  192. int i;
  193. int32_t *dst = (int32_t *) _dst;
  194. for (i = 0; i < width; i++)
  195. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  196. }
  197. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  198. const uint8_t *src, int srcW, int xInc)
  199. {
  200. int i;
  201. unsigned int xpos = 0;
  202. for (i = 0; i < dstWidth; i++) {
  203. register unsigned int xx = xpos >> 16;
  204. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  205. dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
  206. xpos += xInc;
  207. }
  208. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  209. dst[i] = src[srcW-1]*128;
  210. }
  211. // *** horizontal scale Y line to temp buffer
  212. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  213. const uint8_t *src_in[4],
  214. int srcW, int xInc,
  215. const int16_t *hLumFilter,
  216. const int32_t *hLumFilterPos,
  217. int hLumFilterSize,
  218. uint8_t *formatConvBuffer,
  219. uint32_t *pal, int isAlpha)
  220. {
  221. void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
  222. isAlpha ? c->alpToYV12 : c->lumToYV12;
  223. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  224. const uint8_t *src = src_in[isAlpha ? 3 : 0];
  225. if (toYV12) {
  226. toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
  227. src = formatConvBuffer;
  228. } else if (c->readLumPlanar && !isAlpha) {
  229. c->readLumPlanar(formatConvBuffer, src_in, srcW);
  230. src = formatConvBuffer;
  231. }
  232. if (!c->hyscale_fast) {
  233. c->hyScale(c, dst, dstWidth, src, hLumFilter,
  234. hLumFilterPos, hLumFilterSize);
  235. } else { // fast bilinear upscale / crap downscale
  236. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  237. }
  238. if (convertRange)
  239. convertRange(dst, dstWidth);
  240. }
  241. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  242. int dstWidth, const uint8_t *src1,
  243. const uint8_t *src2, int srcW, int xInc)
  244. {
  245. int i;
  246. unsigned int xpos = 0;
  247. for (i = 0; i < dstWidth; i++) {
  248. register unsigned int xx = xpos >> 16;
  249. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  250. dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
  251. dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
  252. xpos += xInc;
  253. }
  254. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  255. dst1[i] = src1[srcW-1]*128;
  256. dst2[i] = src2[srcW-1]*128;
  257. }
  258. }
  259. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
  260. int16_t *dst2, int dstWidth,
  261. const uint8_t *src_in[4],
  262. int srcW, int xInc,
  263. const int16_t *hChrFilter,
  264. const int32_t *hChrFilterPos,
  265. int hChrFilterSize,
  266. uint8_t *formatConvBuffer, uint32_t *pal)
  267. {
  268. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  269. if (c->chrToYV12) {
  270. uint8_t *buf2 = formatConvBuffer +
  271. FFALIGN(srcW*2+78, 16);
  272. c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
  273. src1= formatConvBuffer;
  274. src2= buf2;
  275. } else if (c->readChrPlanar) {
  276. uint8_t *buf2 = formatConvBuffer +
  277. FFALIGN(srcW*2+78, 16);
  278. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
  279. src1 = formatConvBuffer;
  280. src2 = buf2;
  281. }
  282. if (!c->hcscale_fast) {
  283. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  284. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  285. } else { // fast bilinear upscale / crap downscale
  286. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  287. }
  288. if (c->chrConvertRange)
  289. c->chrConvertRange(dst1, dst2, dstWidth);
  290. }
  291. #define DEBUG_SWSCALE_BUFFERS 0
  292. #define DEBUG_BUFFERS(...) \
  293. if (DEBUG_SWSCALE_BUFFERS) \
  294. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  295. static int swScale(SwsContext *c, const uint8_t *src[],
  296. int srcStride[], int srcSliceY,
  297. int srcSliceH, uint8_t *dst[], int dstStride[])
  298. {
  299. /* load a few things into local vars to make the code more readable?
  300. * and faster */
  301. const int srcW = c->srcW;
  302. const int dstW = c->dstW;
  303. const int dstH = c->dstH;
  304. const int chrDstW = c->chrDstW;
  305. const int chrSrcW = c->chrSrcW;
  306. const int lumXInc = c->lumXInc;
  307. const int chrXInc = c->chrXInc;
  308. const enum PixelFormat dstFormat = c->dstFormat;
  309. const int flags = c->flags;
  310. int32_t *vLumFilterPos = c->vLumFilterPos;
  311. int32_t *vChrFilterPos = c->vChrFilterPos;
  312. int32_t *hLumFilterPos = c->hLumFilterPos;
  313. int32_t *hChrFilterPos = c->hChrFilterPos;
  314. int16_t *vLumFilter = c->vLumFilter;
  315. int16_t *vChrFilter = c->vChrFilter;
  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_MMX2 && (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. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  430. assert(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. assert(lumBufIndex < 2 * vLumBufSize);
  455. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  456. assert(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. assert(chrBufIndex < 2 * vChrBufSize);
  477. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  478. assert(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
  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. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize * 2);
  575. assert(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_MMX2
  606. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  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 PixelFormat 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 <= 10) {
  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 > 10 ? hScale16To19_c
  637. : hScale16To15_c;
  638. }
  639. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  640. if (c->dstBpc <= 10) {
  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 == PIX_FMT_MONOBLACK || srcFormat == 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. }