swscale.c 41 KB

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