swscale.c 43 KB

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