swscale.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 *hLumFilter = c->hLumFilter;
  315. int16_t *hChrFilter = c->hChrFilter;
  316. int32_t *lumMmxFilter = c->lumMmxFilter;
  317. int32_t *chrMmxFilter = c->chrMmxFilter;
  318. const int vLumFilterSize = c->vLumFilterSize;
  319. const int vChrFilterSize = c->vChrFilterSize;
  320. const int hLumFilterSize = c->hLumFilterSize;
  321. const int hChrFilterSize = c->hChrFilterSize;
  322. int16_t **lumPixBuf = c->lumPixBuf;
  323. int16_t **chrUPixBuf = c->chrUPixBuf;
  324. int16_t **chrVPixBuf = c->chrVPixBuf;
  325. int16_t **alpPixBuf = c->alpPixBuf;
  326. const int vLumBufSize = c->vLumBufSize;
  327. const int vChrBufSize = c->vChrBufSize;
  328. uint8_t *formatConvBuffer = c->formatConvBuffer;
  329. uint32_t *pal = c->pal_yuv;
  330. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  331. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  332. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  333. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  334. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  335. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  336. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  337. const int chrSrcSliceH = -((-srcSliceH) >> c->chrSrcVSubSample);
  338. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  339. is16BPS(c->srcFormat);
  340. int lastDstY;
  341. /* vars which will change and which we need to store back in the context */
  342. int dstY = c->dstY;
  343. int lumBufIndex = c->lumBufIndex;
  344. int chrBufIndex = c->chrBufIndex;
  345. int lastInLumBuf = c->lastInLumBuf;
  346. int lastInChrBuf = c->lastInChrBuf;
  347. if (isPacked(c->srcFormat)) {
  348. src[0] =
  349. src[1] =
  350. src[2] =
  351. src[3] = src[0];
  352. srcStride[0] =
  353. srcStride[1] =
  354. srcStride[2] =
  355. srcStride[3] = srcStride[0];
  356. }
  357. srcStride[1] <<= c->vChrDrop;
  358. srcStride[2] <<= c->vChrDrop;
  359. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  360. src[0], srcStride[0], src[1], srcStride[1],
  361. src[2], srcStride[2], src[3], srcStride[3],
  362. dst[0], dstStride[0], dst[1], dstStride[1],
  363. dst[2], dstStride[2], dst[3], dstStride[3]);
  364. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  365. srcSliceY, srcSliceH, dstY, dstH);
  366. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  367. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  368. if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
  369. dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
  370. static int warnedAlready = 0; // FIXME maybe move this into the context
  371. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  372. av_log(c, AV_LOG_WARNING,
  373. "Warning: dstStride is not aligned!\n"
  374. " ->cannot do aligned memory accesses anymore\n");
  375. warnedAlready = 1;
  376. }
  377. }
  378. 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
  379. || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
  380. || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
  381. ) {
  382. static int warnedAlready=0;
  383. int cpu_flags = av_get_cpu_flags();
  384. if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  385. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  386. warnedAlready=1;
  387. }
  388. }
  389. /* Note the user might start scaling the picture in the middle so this
  390. * will not get executed. This is not really intended but works
  391. * currently, so people might do it. */
  392. if (srcSliceY == 0) {
  393. lumBufIndex = -1;
  394. chrBufIndex = -1;
  395. dstY = 0;
  396. lastInLumBuf = -1;
  397. lastInChrBuf = -1;
  398. }
  399. if (!should_dither) {
  400. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  401. }
  402. lastDstY = dstY;
  403. for (; dstY < dstH; dstY++) {
  404. const int chrDstY = dstY >> c->chrDstVSubSample;
  405. uint8_t *dest[4] = {
  406. dst[0] + dstStride[0] * dstY,
  407. dst[1] + dstStride[1] * chrDstY,
  408. dst[2] + dstStride[2] * chrDstY,
  409. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  410. };
  411. int use_mmx_vfilter= c->use_mmx_vfilter;
  412. // First line needed as input
  413. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  414. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  415. // First line needed as input
  416. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  417. // Last line needed as input
  418. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  419. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  420. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  421. int enough_lines;
  422. // handle holes (FAST_BILINEAR & weird filters)
  423. if (firstLumSrcY > lastInLumBuf)
  424. lastInLumBuf = firstLumSrcY - 1;
  425. if (firstChrSrcY > lastInChrBuf)
  426. lastInChrBuf = firstChrSrcY - 1;
  427. av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  428. av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  429. DEBUG_BUFFERS("dstY: %d\n", dstY);
  430. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  431. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  432. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  433. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  434. // Do we have enough lines in this slice to output the dstY line
  435. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  436. lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
  437. if (!enough_lines) {
  438. lastLumSrcY = srcSliceY + srcSliceH - 1;
  439. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  440. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  441. lastLumSrcY, lastChrSrcY);
  442. }
  443. // Do horizontal scaling
  444. while (lastInLumBuf < lastLumSrcY) {
  445. const uint8_t *src1[4] = {
  446. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  447. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  448. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  449. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  450. };
  451. lumBufIndex++;
  452. av_assert0(lumBufIndex < 2 * vLumBufSize);
  453. av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  454. av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
  455. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  456. hLumFilter, hLumFilterPos, hLumFilterSize,
  457. formatConvBuffer, pal, 0);
  458. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  459. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  460. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  461. formatConvBuffer, pal, 1);
  462. lastInLumBuf++;
  463. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  464. lumBufIndex, lastInLumBuf);
  465. }
  466. while (lastInChrBuf < lastChrSrcY) {
  467. const uint8_t *src1[4] = {
  468. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  469. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  470. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  471. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  472. };
  473. chrBufIndex++;
  474. av_assert0(chrBufIndex < 2 * vChrBufSize);
  475. av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  476. av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  477. // FIXME replace parameters through context struct (some at least)
  478. if (c->needs_hcscale)
  479. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  480. chrDstW, src1, chrSrcW, chrXInc,
  481. hChrFilter, hChrFilterPos, hChrFilterSize,
  482. formatConvBuffer, pal);
  483. lastInChrBuf++;
  484. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  485. chrBufIndex, lastInChrBuf);
  486. }
  487. // wrap buf index around to stay inside the ring buffer
  488. if (lumBufIndex >= vLumBufSize)
  489. lumBufIndex -= vLumBufSize;
  490. if (chrBufIndex >= vChrBufSize)
  491. chrBufIndex -= vChrBufSize;
  492. if (!enough_lines)
  493. break; // we can't output a dstY line so let's try with the next slice
  494. #if HAVE_MMX_INLINE
  495. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  496. lastInLumBuf, lastInChrBuf);
  497. #endif
  498. if (should_dither) {
  499. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  500. c->lumDither8 = dither_8x8_128[dstY & 7];
  501. }
  502. if (dstY >= dstH - 2) {
  503. /* hmm looks like we can't use MMX here without overwriting
  504. * this array's tail */
  505. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  506. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  507. use_mmx_vfilter= 0;
  508. }
  509. {
  510. const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  511. const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  512. const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  513. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  514. (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  515. int16_t *vLumFilter = c->vLumFilter;
  516. int16_t *vChrFilter = c->vChrFilter;
  517. if (isPlanarYUV(dstFormat) ||
  518. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  519. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  520. vLumFilter += dstY * vLumFilterSize;
  521. vChrFilter += chrDstY * vChrFilterSize;
  522. // av_assert0(use_mmx_vfilter != (
  523. // yuv2planeX == yuv2planeX_10BE_c
  524. // || yuv2planeX == yuv2planeX_10LE_c
  525. // || yuv2planeX == yuv2planeX_9BE_c
  526. // || yuv2planeX == yuv2planeX_9LE_c
  527. // || yuv2planeX == yuv2planeX_16BE_c
  528. // || yuv2planeX == yuv2planeX_16LE_c
  529. // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
  530. if(use_mmx_vfilter){
  531. vLumFilter= c->lumMmxFilter;
  532. vChrFilter= c->chrMmxFilter;
  533. }
  534. if (vLumFilterSize == 1) {
  535. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  536. } else {
  537. yuv2planeX(vLumFilter, vLumFilterSize,
  538. lumSrcPtr, dest[0],
  539. dstW, c->lumDither8, 0);
  540. }
  541. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  542. if (yuv2nv12cX) {
  543. yuv2nv12cX(c, vChrFilter,
  544. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  545. dest[1], chrDstW);
  546. } else if (vChrFilterSize == 1) {
  547. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  548. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  549. } else {
  550. yuv2planeX(vChrFilter,
  551. vChrFilterSize, chrUSrcPtr, dest[1],
  552. chrDstW, c->chrDither8, 0);
  553. yuv2planeX(vChrFilter,
  554. vChrFilterSize, chrVSrcPtr, dest[2],
  555. chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
  556. }
  557. }
  558. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  559. if(use_mmx_vfilter){
  560. vLumFilter= c->alpMmxFilter;
  561. }
  562. if (vLumFilterSize == 1) {
  563. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  564. c->lumDither8, 0);
  565. } else {
  566. yuv2planeX(vLumFilter,
  567. vLumFilterSize, alpSrcPtr, dest[3],
  568. dstW, c->lumDither8, 0);
  569. }
  570. }
  571. } else {
  572. av_assert1(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize * 2);
  573. av_assert1(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize * 2);
  574. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  575. vChrFilterSize <= 2) { // unscaled RGB
  576. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  577. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  578. alpPixBuf ? *alpSrcPtr : NULL,
  579. dest[0], dstW, chrAlpha, dstY);
  580. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  581. vChrFilterSize == 2) { // bilinear upscale RGB
  582. int lumAlpha = vLumFilter[2 * dstY + 1];
  583. int chrAlpha = vChrFilter[2 * dstY + 1];
  584. lumMmxFilter[2] =
  585. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  586. chrMmxFilter[2] =
  587. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  588. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  589. alpPixBuf ? alpSrcPtr : NULL,
  590. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  591. } else { // general RGB
  592. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  593. lumSrcPtr, vLumFilterSize,
  594. vChrFilter + dstY * vChrFilterSize,
  595. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  596. alpSrcPtr, dest[0], dstW, dstY);
  597. }
  598. }
  599. }
  600. }
  601. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf)
  602. fillPlane(dst[3], dstStride[3], dstW, dstY - lastDstY, lastDstY, 255);
  603. #if HAVE_MMXEXT_INLINE
  604. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  605. __asm__ volatile ("sfence" ::: "memory");
  606. #endif
  607. emms_c();
  608. /* store changed local vars back in the context */
  609. c->dstY = dstY;
  610. c->lumBufIndex = lumBufIndex;
  611. c->chrBufIndex = chrBufIndex;
  612. c->lastInLumBuf = lastInLumBuf;
  613. c->lastInChrBuf = lastInChrBuf;
  614. return dstY - lastDstY;
  615. }
  616. static av_cold void sws_init_swScale_c(SwsContext *c)
  617. {
  618. enum PixelFormat srcFormat = c->srcFormat;
  619. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  620. &c->yuv2nv12cX, &c->yuv2packed1,
  621. &c->yuv2packed2, &c->yuv2packedX);
  622. ff_sws_init_input_funcs(c);
  623. if (c->srcBpc == 8) {
  624. if (c->dstBpc <= 14) {
  625. c->hyScale = c->hcScale = hScale8To15_c;
  626. if (c->flags & SWS_FAST_BILINEAR) {
  627. c->hyscale_fast = hyscale_fast_c;
  628. c->hcscale_fast = hcscale_fast_c;
  629. }
  630. } else {
  631. c->hyScale = c->hcScale = hScale8To19_c;
  632. }
  633. } else {
  634. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  635. : hScale16To15_c;
  636. }
  637. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  638. if (c->dstBpc <= 14) {
  639. if (c->srcRange) {
  640. c->lumConvertRange = lumRangeFromJpeg_c;
  641. c->chrConvertRange = chrRangeFromJpeg_c;
  642. } else {
  643. c->lumConvertRange = lumRangeToJpeg_c;
  644. c->chrConvertRange = chrRangeToJpeg_c;
  645. }
  646. } else {
  647. if (c->srcRange) {
  648. c->lumConvertRange = lumRangeFromJpeg16_c;
  649. c->chrConvertRange = chrRangeFromJpeg16_c;
  650. } else {
  651. c->lumConvertRange = lumRangeToJpeg16_c;
  652. c->chrConvertRange = chrRangeToJpeg16_c;
  653. }
  654. }
  655. }
  656. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  657. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  658. c->needs_hcscale = 1;
  659. }
  660. SwsFunc ff_getSwsFunc(SwsContext *c)
  661. {
  662. sws_init_swScale_c(c);
  663. if (HAVE_MMX)
  664. ff_sws_init_swScale_mmx(c);
  665. if (HAVE_ALTIVEC)
  666. ff_sws_init_swScale_altivec(c);
  667. return swScale;
  668. }