swscale.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 <inttypes.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/bswap.h"
  27. #include "libavutil/cpu.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "config.h"
  32. #include "rgb2rgb.h"
  33. #include "swscale_internal.h"
  34. #include "swscale.h"
  35. DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
  36. { 36, 68, 60, 92, 34, 66, 58, 90, },
  37. { 100, 4, 124, 28, 98, 2, 122, 26, },
  38. { 52, 84, 44, 76, 50, 82, 42, 74, },
  39. { 116, 20, 108, 12, 114, 18, 106, 10, },
  40. { 32, 64, 56, 88, 38, 70, 62, 94, },
  41. { 96, 0, 120, 24, 102, 6, 126, 30, },
  42. { 48, 80, 40, 72, 54, 86, 46, 78, },
  43. { 112, 16, 104, 8, 118, 22, 110, 14, },
  44. { 36, 68, 60, 92, 34, 66, 58, 90, },
  45. };
  46. DECLARE_ALIGNED(8, static const uint8_t, 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 = 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 = ff_dither_8x8_128[chrDstY & 7];
  511. c->lumDither8 = ff_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(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);
  690. if (ARCH_PPC)
  691. ff_sws_init_swscale_ppc(c);
  692. if (ARCH_X86)
  693. ff_sws_init_swscale_x86(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. static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
  768. const uint16_t *src, int stride, int h)
  769. {
  770. int xp,yp;
  771. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
  772. for (yp=0; yp<h; yp++) {
  773. for (xp=0; xp+2<stride; xp+=3) {
  774. int x, y, z, r, g, b;
  775. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  776. r = AV_RB16(src + xp + 0);
  777. g = AV_RB16(src + xp + 1);
  778. b = AV_RB16(src + xp + 2);
  779. } else {
  780. r = AV_RL16(src + xp + 0);
  781. g = AV_RL16(src + xp + 1);
  782. b = AV_RL16(src + xp + 2);
  783. }
  784. r = c->rgbgammainv[r>>4];
  785. g = c->rgbgammainv[g>>4];
  786. b = c->rgbgammainv[b>>4];
  787. // convert from sRGBlinear to XYZlinear
  788. x = c->rgb2xyz_matrix[0][0] * r +
  789. c->rgb2xyz_matrix[0][1] * g +
  790. c->rgb2xyz_matrix[0][2] * b >> 12;
  791. y = c->rgb2xyz_matrix[1][0] * r +
  792. c->rgb2xyz_matrix[1][1] * g +
  793. c->rgb2xyz_matrix[1][2] * b >> 12;
  794. z = c->rgb2xyz_matrix[2][0] * r +
  795. c->rgb2xyz_matrix[2][1] * g +
  796. c->rgb2xyz_matrix[2][2] * b >> 12;
  797. // limit values to 12-bit depth
  798. x = av_clip_c(x,0,4095);
  799. y = av_clip_c(y,0,4095);
  800. z = av_clip_c(z,0,4095);
  801. // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
  802. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  803. AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
  804. AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
  805. AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
  806. } else {
  807. AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
  808. AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
  809. AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
  810. }
  811. }
  812. src += stride;
  813. dst += stride;
  814. }
  815. }
  816. /**
  817. * swscale wrapper, so we don't need to export the SwsContext.
  818. * Assumes planar YUV to be in YUV order instead of YVU.
  819. */
  820. int attribute_align_arg sws_scale(struct SwsContext *c,
  821. const uint8_t * const srcSlice[],
  822. const int srcStride[], int srcSliceY,
  823. int srcSliceH, uint8_t *const dst[],
  824. const int dstStride[])
  825. {
  826. int i, ret;
  827. const uint8_t *src2[4];
  828. uint8_t *dst2[4];
  829. uint8_t *rgb0_tmp = NULL;
  830. if (!srcStride || !dstStride || !dst || !srcSlice) {
  831. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  832. return 0;
  833. }
  834. memcpy(src2, srcSlice, sizeof(src2));
  835. memcpy(dst2, dst, sizeof(dst2));
  836. // do not mess up sliceDir if we have a "trailing" 0-size slice
  837. if (srcSliceH == 0)
  838. return 0;
  839. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  840. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  841. return 0;
  842. }
  843. if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
  844. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  845. return 0;
  846. }
  847. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  848. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  849. return 0;
  850. }
  851. if (c->sliceDir == 0) {
  852. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  853. }
  854. if (usePal(c->srcFormat)) {
  855. for (i = 0; i < 256; i++) {
  856. int p, r, g, b, y, u, v, a = 0xff;
  857. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  858. p = ((const uint32_t *)(srcSlice[1]))[i];
  859. a = (p >> 24) & 0xFF;
  860. r = (p >> 16) & 0xFF;
  861. g = (p >> 8) & 0xFF;
  862. b = p & 0xFF;
  863. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  864. r = ( i >> 5 ) * 36;
  865. g = ((i >> 2) & 7) * 36;
  866. b = ( i & 3) * 85;
  867. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  868. b = ( i >> 6 ) * 85;
  869. g = ((i >> 3) & 7) * 36;
  870. r = ( i & 7) * 36;
  871. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  872. r = ( i >> 3 ) * 255;
  873. g = ((i >> 1) & 3) * 85;
  874. b = ( i & 1) * 255;
  875. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  876. r = g = b = i;
  877. } else {
  878. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  879. b = ( i >> 3 ) * 255;
  880. g = ((i >> 1) & 3) * 85;
  881. r = ( i & 1) * 255;
  882. }
  883. #define RGB2YUV_SHIFT 15
  884. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  885. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  886. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  887. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  888. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  889. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  890. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  891. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  892. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  893. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  894. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  895. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  896. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  897. switch (c->dstFormat) {
  898. case AV_PIX_FMT_BGR32:
  899. #if !HAVE_BIGENDIAN
  900. case AV_PIX_FMT_RGB24:
  901. #endif
  902. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  903. break;
  904. case AV_PIX_FMT_BGR32_1:
  905. #if HAVE_BIGENDIAN
  906. case AV_PIX_FMT_BGR24:
  907. #endif
  908. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  909. break;
  910. case AV_PIX_FMT_RGB32_1:
  911. #if HAVE_BIGENDIAN
  912. case AV_PIX_FMT_RGB24:
  913. #endif
  914. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  915. break;
  916. case AV_PIX_FMT_RGB32:
  917. #if !HAVE_BIGENDIAN
  918. case AV_PIX_FMT_BGR24:
  919. #endif
  920. default:
  921. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  922. }
  923. }
  924. }
  925. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  926. uint8_t *base;
  927. int x,y;
  928. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  929. if (!rgb0_tmp)
  930. return AVERROR(ENOMEM);
  931. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  932. for (y=0; y<srcSliceH; y++){
  933. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  934. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  935. base[ srcStride[0]*y + x] = 0xFF;
  936. }
  937. }
  938. src2[0] = base;
  939. }
  940. if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  941. uint8_t *base;
  942. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  943. if (!rgb0_tmp)
  944. return AVERROR(ENOMEM);
  945. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  946. xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
  947. src2[0] = base;
  948. }
  949. if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
  950. for (i = 0; i < 4; i++)
  951. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  952. // copy strides, so they can safely be modified
  953. if (c->sliceDir == 1) {
  954. // slices go from top to bottom
  955. int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
  956. srcStride[3] };
  957. int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
  958. dstStride[3] };
  959. reset_ptr(src2, c->srcFormat);
  960. reset_ptr((void*)dst2, c->dstFormat);
  961. /* reset slice direction at end of frame */
  962. if (srcSliceY + srcSliceH == c->srcH)
  963. c->sliceDir = 0;
  964. ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
  965. dstStride2);
  966. } else {
  967. // slices go from bottom to top => we flip the image internally
  968. int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
  969. -srcStride[3] };
  970. int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
  971. -dstStride[3] };
  972. src2[0] += (srcSliceH - 1) * srcStride[0];
  973. if (!usePal(c->srcFormat))
  974. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  975. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  976. src2[3] += (srcSliceH - 1) * srcStride[3];
  977. dst2[0] += ( c->dstH - 1) * dstStride[0];
  978. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  979. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  980. dst2[3] += ( c->dstH - 1) * dstStride[3];
  981. reset_ptr(src2, c->srcFormat);
  982. reset_ptr((void*)dst2, c->dstFormat);
  983. /* reset slice direction at end of frame */
  984. if (!srcSliceY)
  985. c->sliceDir = 0;
  986. ret = c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
  987. srcSliceH, dst2, dstStride2);
  988. }
  989. if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
  990. /* replace on the same data */
  991. rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);
  992. }
  993. av_free(rgb0_tmp);
  994. return ret;
  995. }