swscale.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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);
  233. src = formatConvBuffer;
  234. }
  235. if (!c->hyscale_fast) {
  236. c->hyScale(c, dst, dstWidth, src, hLumFilter,
  237. hLumFilterPos, hLumFilterSize);
  238. } else { // fast bilinear upscale / crap downscale
  239. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  240. }
  241. if (convertRange)
  242. convertRange(dst, dstWidth);
  243. }
  244. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  245. int dstWidth, const uint8_t *src1,
  246. const uint8_t *src2, int srcW, int xInc)
  247. {
  248. int i;
  249. unsigned int xpos = 0;
  250. for (i = 0; i < dstWidth; i++) {
  251. register unsigned int xx = xpos >> 16;
  252. register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
  253. dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
  254. dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
  255. xpos += xInc;
  256. }
  257. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  258. dst1[i] = src1[srcW-1]*128;
  259. dst2[i] = src2[srcW-1]*128;
  260. }
  261. }
  262. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
  263. int16_t *dst2, int dstWidth,
  264. const uint8_t *src_in[4],
  265. int srcW, int xInc,
  266. const int16_t *hChrFilter,
  267. const int32_t *hChrFilterPos,
  268. int hChrFilterSize,
  269. uint8_t *formatConvBuffer, uint32_t *pal)
  270. {
  271. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  272. if (c->chrToYV12) {
  273. uint8_t *buf2 = formatConvBuffer +
  274. FFALIGN(srcW*2+78, 16);
  275. c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
  276. src1= formatConvBuffer;
  277. src2= buf2;
  278. } else if (c->readChrPlanar) {
  279. uint8_t *buf2 = formatConvBuffer +
  280. FFALIGN(srcW*2+78, 16);
  281. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
  282. src1 = formatConvBuffer;
  283. src2 = buf2;
  284. }
  285. if (!c->hcscale_fast) {
  286. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  287. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  288. } else { // fast bilinear upscale / crap downscale
  289. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  290. }
  291. if (c->chrConvertRange)
  292. c->chrConvertRange(dst1, dst2, dstWidth);
  293. }
  294. #define DEBUG_SWSCALE_BUFFERS 0
  295. #define DEBUG_BUFFERS(...) \
  296. if (DEBUG_SWSCALE_BUFFERS) \
  297. av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  298. static int swScale(SwsContext *c, const uint8_t *src[],
  299. int srcStride[], int srcSliceY,
  300. int srcSliceH, uint8_t *dst[], int dstStride[])
  301. {
  302. /* load a few things into local vars to make the code more readable?
  303. * and faster */
  304. const int srcW = c->srcW;
  305. const int dstW = c->dstW;
  306. const int dstH = c->dstH;
  307. const int chrDstW = c->chrDstW;
  308. const int chrSrcW = c->chrSrcW;
  309. const int lumXInc = c->lumXInc;
  310. const int chrXInc = c->chrXInc;
  311. const enum AVPixelFormat dstFormat = c->dstFormat;
  312. const int flags = c->flags;
  313. int32_t *vLumFilterPos = c->vLumFilterPos;
  314. int32_t *vChrFilterPos = c->vChrFilterPos;
  315. int32_t *hLumFilterPos = c->hLumFilterPos;
  316. int32_t *hChrFilterPos = c->hChrFilterPos;
  317. int16_t *hLumFilter = c->hLumFilter;
  318. int16_t *hChrFilter = c->hChrFilter;
  319. int32_t *lumMmxFilter = c->lumMmxFilter;
  320. int32_t *chrMmxFilter = c->chrMmxFilter;
  321. const int vLumFilterSize = c->vLumFilterSize;
  322. const int vChrFilterSize = c->vChrFilterSize;
  323. const int hLumFilterSize = c->hLumFilterSize;
  324. const int hChrFilterSize = c->hChrFilterSize;
  325. int16_t **lumPixBuf = c->lumPixBuf;
  326. int16_t **chrUPixBuf = c->chrUPixBuf;
  327. int16_t **chrVPixBuf = c->chrVPixBuf;
  328. int16_t **alpPixBuf = c->alpPixBuf;
  329. const int vLumBufSize = c->vLumBufSize;
  330. const int vChrBufSize = c->vChrBufSize;
  331. uint8_t *formatConvBuffer = c->formatConvBuffer;
  332. uint32_t *pal = c->pal_yuv;
  333. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  334. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  335. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  336. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  337. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  338. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  339. yuv2anyX_fn yuv2anyX = c->yuv2anyX;
  340. const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
  341. const int chrSrcSliceH = -((-srcSliceH) >> c->chrSrcVSubSample);
  342. int should_dither = is9_OR_10BPS(c->srcFormat) ||
  343. is16BPS(c->srcFormat);
  344. int lastDstY;
  345. /* vars which will change and which we need to store back in the context */
  346. int dstY = c->dstY;
  347. int lumBufIndex = c->lumBufIndex;
  348. int chrBufIndex = c->chrBufIndex;
  349. int lastInLumBuf = c->lastInLumBuf;
  350. int lastInChrBuf = c->lastInChrBuf;
  351. if (isPacked(c->srcFormat)) {
  352. src[0] =
  353. src[1] =
  354. src[2] =
  355. src[3] = src[0];
  356. srcStride[0] =
  357. srcStride[1] =
  358. srcStride[2] =
  359. srcStride[3] = srcStride[0];
  360. }
  361. srcStride[1] <<= c->vChrDrop;
  362. srcStride[2] <<= c->vChrDrop;
  363. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  364. src[0], srcStride[0], src[1], srcStride[1],
  365. src[2], srcStride[2], src[3], srcStride[3],
  366. dst[0], dstStride[0], dst[1], dstStride[1],
  367. dst[2], dstStride[2], dst[3], dstStride[3]);
  368. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  369. srcSliceY, srcSliceH, dstY, dstH);
  370. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  371. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  372. if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
  373. dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
  374. static int warnedAlready = 0; // FIXME maybe move this into the context
  375. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  376. av_log(c, AV_LOG_WARNING,
  377. "Warning: dstStride is not aligned!\n"
  378. " ->cannot do aligned memory accesses anymore\n");
  379. warnedAlready = 1;
  380. }
  381. }
  382. if ( (uintptr_t)dst[0]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
  383. || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
  384. || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
  385. || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
  386. ) {
  387. static int warnedAlready=0;
  388. int cpu_flags = av_get_cpu_flags();
  389. if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  390. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  391. warnedAlready=1;
  392. }
  393. }
  394. /* Note the user might start scaling the picture in the middle so this
  395. * will not get executed. This is not really intended but works
  396. * currently, so people might do it. */
  397. if (srcSliceY == 0) {
  398. lumBufIndex = -1;
  399. chrBufIndex = -1;
  400. dstY = 0;
  401. lastInLumBuf = -1;
  402. lastInChrBuf = -1;
  403. }
  404. if (!should_dither) {
  405. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  406. }
  407. lastDstY = dstY;
  408. for (; dstY < dstH; dstY++) {
  409. const int chrDstY = dstY >> c->chrDstVSubSample;
  410. uint8_t *dest[4] = {
  411. dst[0] + dstStride[0] * dstY,
  412. dst[1] + dstStride[1] * chrDstY,
  413. dst[2] + dstStride[2] * chrDstY,
  414. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  415. };
  416. int use_mmx_vfilter= c->use_mmx_vfilter;
  417. // First line needed as input
  418. const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
  419. const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
  420. // First line needed as input
  421. const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
  422. // Last line needed as input
  423. int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
  424. int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
  425. int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
  426. int enough_lines;
  427. // handle holes (FAST_BILINEAR & weird filters)
  428. if (firstLumSrcY > lastInLumBuf)
  429. lastInLumBuf = firstLumSrcY - 1;
  430. if (firstChrSrcY > lastInChrBuf)
  431. lastInChrBuf = firstChrSrcY - 1;
  432. av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  433. av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  434. DEBUG_BUFFERS("dstY: %d\n", dstY);
  435. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  436. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  437. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  438. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  439. // Do we have enough lines in this slice to output the dstY line
  440. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
  441. lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
  442. if (!enough_lines) {
  443. lastLumSrcY = srcSliceY + srcSliceH - 1;
  444. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  445. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  446. lastLumSrcY, lastChrSrcY);
  447. }
  448. // Do horizontal scaling
  449. while (lastInLumBuf < lastLumSrcY) {
  450. const uint8_t *src1[4] = {
  451. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  452. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  453. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  454. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  455. };
  456. lumBufIndex++;
  457. av_assert0(lumBufIndex < 2 * vLumBufSize);
  458. av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  459. av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
  460. hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
  461. hLumFilter, hLumFilterPos, hLumFilterSize,
  462. formatConvBuffer, pal, 0);
  463. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  464. hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
  465. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  466. formatConvBuffer, pal, 1);
  467. lastInLumBuf++;
  468. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  469. lumBufIndex, lastInLumBuf);
  470. }
  471. while (lastInChrBuf < lastChrSrcY) {
  472. const uint8_t *src1[4] = {
  473. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  474. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  475. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  476. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  477. };
  478. chrBufIndex++;
  479. av_assert0(chrBufIndex < 2 * vChrBufSize);
  480. av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  481. av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  482. // FIXME replace parameters through context struct (some at least)
  483. if (c->needs_hcscale)
  484. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  485. chrDstW, src1, chrSrcW, chrXInc,
  486. hChrFilter, hChrFilterPos, hChrFilterSize,
  487. formatConvBuffer, pal);
  488. lastInChrBuf++;
  489. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  490. chrBufIndex, lastInChrBuf);
  491. }
  492. // wrap buf index around to stay inside the ring buffer
  493. if (lumBufIndex >= vLumBufSize)
  494. lumBufIndex -= vLumBufSize;
  495. if (chrBufIndex >= vChrBufSize)
  496. chrBufIndex -= vChrBufSize;
  497. if (!enough_lines)
  498. break; // we can't output a dstY line so let's try with the next slice
  499. #if HAVE_MMX_INLINE
  500. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
  501. lastInLumBuf, lastInChrBuf);
  502. #endif
  503. if (should_dither) {
  504. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  505. c->lumDither8 = dither_8x8_128[dstY & 7];
  506. }
  507. if (dstY >= dstH - 2) {
  508. /* hmm looks like we can't use MMX here without overwriting
  509. * this array's tail */
  510. ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  511. &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
  512. use_mmx_vfilter= 0;
  513. }
  514. {
  515. const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  516. const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  517. const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  518. const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
  519. (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  520. int16_t *vLumFilter = c->vLumFilter;
  521. int16_t *vChrFilter = c->vChrFilter;
  522. if (isPlanarYUV(dstFormat) ||
  523. (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
  524. const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
  525. vLumFilter += dstY * vLumFilterSize;
  526. vChrFilter += chrDstY * vChrFilterSize;
  527. // av_assert0(use_mmx_vfilter != (
  528. // yuv2planeX == yuv2planeX_10BE_c
  529. // || yuv2planeX == yuv2planeX_10LE_c
  530. // || yuv2planeX == yuv2planeX_9BE_c
  531. // || yuv2planeX == yuv2planeX_9LE_c
  532. // || yuv2planeX == yuv2planeX_16BE_c
  533. // || yuv2planeX == yuv2planeX_16LE_c
  534. // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
  535. if(use_mmx_vfilter){
  536. vLumFilter= (int16_t *)c->lumMmxFilter;
  537. vChrFilter= (int16_t *)c->chrMmxFilter;
  538. }
  539. if (vLumFilterSize == 1) {
  540. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  541. } else {
  542. yuv2planeX(vLumFilter, vLumFilterSize,
  543. lumSrcPtr, dest[0],
  544. dstW, c->lumDither8, 0);
  545. }
  546. if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
  547. if (yuv2nv12cX) {
  548. yuv2nv12cX(c, vChrFilter,
  549. vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
  550. dest[1], chrDstW);
  551. } else if (vChrFilterSize == 1) {
  552. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  553. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  554. } else {
  555. yuv2planeX(vChrFilter,
  556. vChrFilterSize, chrUSrcPtr, dest[1],
  557. chrDstW, c->chrDither8, 0);
  558. yuv2planeX(vChrFilter,
  559. vChrFilterSize, chrVSrcPtr, dest[2],
  560. chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
  561. }
  562. }
  563. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  564. if(use_mmx_vfilter){
  565. vLumFilter= (int16_t *)c->alpMmxFilter;
  566. }
  567. if (vLumFilterSize == 1) {
  568. yuv2plane1(alpSrcPtr[0], dest[3], dstW,
  569. c->lumDither8, 0);
  570. } else {
  571. yuv2planeX(vLumFilter,
  572. vLumFilterSize, alpSrcPtr, dest[3],
  573. dstW, c->lumDither8, 0);
  574. }
  575. }
  576. } else if (yuv2packedX) {
  577. av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
  578. av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
  579. if (c->yuv2packed1 && vLumFilterSize == 1 &&
  580. vChrFilterSize <= 2) { // unscaled RGB
  581. int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
  582. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  583. alpPixBuf ? *alpSrcPtr : NULL,
  584. dest[0], dstW, chrAlpha, dstY);
  585. } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
  586. vChrFilterSize == 2) { // bilinear upscale RGB
  587. int lumAlpha = vLumFilter[2 * dstY + 1];
  588. int chrAlpha = vChrFilter[2 * dstY + 1];
  589. lumMmxFilter[2] =
  590. lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
  591. chrMmxFilter[2] =
  592. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  593. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  594. alpPixBuf ? alpSrcPtr : NULL,
  595. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  596. } else { // general RGB
  597. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  598. lumSrcPtr, vLumFilterSize,
  599. vChrFilter + dstY * vChrFilterSize,
  600. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  601. alpSrcPtr, dest[0], dstW, dstY);
  602. }
  603. } else {
  604. av_assert1(!yuv2packed1 && !yuv2packed2);
  605. yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
  606. lumSrcPtr, vLumFilterSize,
  607. vChrFilter + dstY * vChrFilterSize,
  608. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  609. alpSrcPtr, dest, dstW, dstY);
  610. }
  611. }
  612. }
  613. if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
  614. int length = dstW;
  615. int height = dstY - lastDstY;
  616. if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
  617. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  618. fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
  619. 1, desc->comp[3].depth_minus1,
  620. isBE(dstFormat));
  621. } else
  622. fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
  623. }
  624. #if HAVE_MMXEXT_INLINE
  625. if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
  626. __asm__ volatile ("sfence" ::: "memory");
  627. #endif
  628. emms_c();
  629. /* store changed local vars back in the context */
  630. c->dstY = dstY;
  631. c->lumBufIndex = lumBufIndex;
  632. c->chrBufIndex = chrBufIndex;
  633. c->lastInLumBuf = lastInLumBuf;
  634. c->lastInChrBuf = lastInChrBuf;
  635. return dstY - lastDstY;
  636. }
  637. static av_cold void sws_init_swScale_c(SwsContext *c)
  638. {
  639. enum AVPixelFormat srcFormat = c->srcFormat;
  640. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  641. &c->yuv2nv12cX, &c->yuv2packed1,
  642. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  643. ff_sws_init_input_funcs(c);
  644. if (c->srcBpc == 8) {
  645. if (c->dstBpc <= 14) {
  646. c->hyScale = c->hcScale = hScale8To15_c;
  647. if (c->flags & SWS_FAST_BILINEAR) {
  648. c->hyscale_fast = hyscale_fast_c;
  649. c->hcscale_fast = hcscale_fast_c;
  650. }
  651. } else {
  652. c->hyScale = c->hcScale = hScale8To19_c;
  653. }
  654. } else {
  655. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  656. : hScale16To15_c;
  657. }
  658. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  659. if (c->dstBpc <= 14) {
  660. if (c->srcRange) {
  661. c->lumConvertRange = lumRangeFromJpeg_c;
  662. c->chrConvertRange = chrRangeFromJpeg_c;
  663. } else {
  664. c->lumConvertRange = lumRangeToJpeg_c;
  665. c->chrConvertRange = chrRangeToJpeg_c;
  666. }
  667. } else {
  668. if (c->srcRange) {
  669. c->lumConvertRange = lumRangeFromJpeg16_c;
  670. c->chrConvertRange = chrRangeFromJpeg16_c;
  671. } else {
  672. c->lumConvertRange = lumRangeToJpeg16_c;
  673. c->chrConvertRange = chrRangeToJpeg16_c;
  674. }
  675. }
  676. }
  677. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  678. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  679. c->needs_hcscale = 1;
  680. }
  681. SwsFunc ff_getSwsFunc(SwsContext *c)
  682. {
  683. sws_init_swScale_c(c);
  684. if (HAVE_MMX)
  685. ff_sws_init_swScale_mmx(c);
  686. if (HAVE_ALTIVEC)
  687. ff_sws_init_swScale_altivec(c);
  688. return swScale;
  689. }
  690. static void reset_ptr(const uint8_t *src[], int format)
  691. {
  692. if (!isALPHA(format))
  693. src[3] = NULL;
  694. if (!isPlanar(format)) {
  695. src[3] = src[2] = NULL;
  696. if (!usePal(format))
  697. src[1] = NULL;
  698. }
  699. }
  700. static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
  701. const int linesizes[4])
  702. {
  703. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  704. int i;
  705. for (i = 0; i < 4; i++) {
  706. int plane = desc->comp[i].plane;
  707. if (!data[plane] || !linesizes[plane])
  708. return 0;
  709. }
  710. return 1;
  711. }
  712. /**
  713. * swscale wrapper, so we don't need to export the SwsContext.
  714. * Assumes planar YUV to be in YUV order instead of YVU.
  715. */
  716. int attribute_align_arg sws_scale(struct SwsContext *c,
  717. const uint8_t * const srcSlice[],
  718. const int srcStride[], int srcSliceY,
  719. int srcSliceH, uint8_t *const dst[],
  720. const int dstStride[])
  721. {
  722. int i, ret;
  723. const uint8_t *src2[4];
  724. uint8_t *dst2[4];
  725. uint8_t *rgb0_tmp = NULL;
  726. if (!srcStride || !dstStride || !dst || !srcSlice) {
  727. av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
  728. return 0;
  729. }
  730. memcpy(src2, srcSlice, sizeof(src2));
  731. memcpy(dst2, dst, sizeof(dst2));
  732. // do not mess up sliceDir if we have a "trailing" 0-size slice
  733. if (srcSliceH == 0)
  734. return 0;
  735. if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
  736. av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
  737. return 0;
  738. }
  739. if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
  740. av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
  741. return 0;
  742. }
  743. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  744. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  745. return 0;
  746. }
  747. if (c->sliceDir == 0) {
  748. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  749. }
  750. if (usePal(c->srcFormat)) {
  751. for (i = 0; i < 256; i++) {
  752. int p, r, g, b, y, u, v, a = 0xff;
  753. if (c->srcFormat == AV_PIX_FMT_PAL8) {
  754. p = ((const uint32_t *)(srcSlice[1]))[i];
  755. a = (p >> 24) & 0xFF;
  756. r = (p >> 16) & 0xFF;
  757. g = (p >> 8) & 0xFF;
  758. b = p & 0xFF;
  759. } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
  760. r = ( i >> 5 ) * 36;
  761. g = ((i >> 2) & 7) * 36;
  762. b = ( i & 3) * 85;
  763. } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
  764. b = ( i >> 6 ) * 85;
  765. g = ((i >> 3) & 7) * 36;
  766. r = ( i & 7) * 36;
  767. } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
  768. r = ( i >> 3 ) * 255;
  769. g = ((i >> 1) & 3) * 85;
  770. b = ( i & 1) * 255;
  771. } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
  772. r = g = b = i;
  773. } else {
  774. av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
  775. b = ( i >> 3 ) * 255;
  776. g = ((i >> 1) & 3) * 85;
  777. r = ( i & 1) * 255;
  778. }
  779. #define RGB2YUV_SHIFT 15
  780. #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  781. #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  782. #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  783. #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  784. #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  785. #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  786. #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  787. #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  788. #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
  789. y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  790. u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  791. v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  792. c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
  793. switch (c->dstFormat) {
  794. case AV_PIX_FMT_BGR32:
  795. #if !HAVE_BIGENDIAN
  796. case AV_PIX_FMT_RGB24:
  797. #endif
  798. c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
  799. break;
  800. case AV_PIX_FMT_BGR32_1:
  801. #if HAVE_BIGENDIAN
  802. case AV_PIX_FMT_BGR24:
  803. #endif
  804. c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
  805. break;
  806. case AV_PIX_FMT_RGB32_1:
  807. #if HAVE_BIGENDIAN
  808. case AV_PIX_FMT_RGB24:
  809. #endif
  810. c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
  811. break;
  812. case AV_PIX_FMT_RGB32:
  813. #if !HAVE_BIGENDIAN
  814. case AV_PIX_FMT_BGR24:
  815. #endif
  816. default:
  817. c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
  818. }
  819. }
  820. }
  821. if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
  822. uint8_t *base;
  823. int x,y;
  824. rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
  825. base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
  826. for (y=0; y<srcSliceH; y++){
  827. memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
  828. for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
  829. base[ srcStride[0]*y + x] = 0xFF;
  830. }
  831. }
  832. src2[0] = base;
  833. }
  834. if (!srcSliceY && (c->flags & SWS_BITEXACT) && (c->flags & SWS_ERROR_DIFFUSION) && c->dither_error[0])
  835. for (i = 0; i < 4; i++)
  836. memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
  837. // copy strides, so they can safely be modified
  838. if (c->sliceDir == 1) {
  839. // slices go from top to bottom
  840. int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
  841. srcStride[3] };
  842. int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
  843. dstStride[3] };
  844. reset_ptr(src2, c->srcFormat);
  845. reset_ptr((void*)dst2, c->dstFormat);
  846. /* reset slice direction at end of frame */
  847. if (srcSliceY + srcSliceH == c->srcH)
  848. c->sliceDir = 0;
  849. ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
  850. dstStride2);
  851. } else {
  852. // slices go from bottom to top => we flip the image internally
  853. int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
  854. -srcStride[3] };
  855. int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
  856. -dstStride[3] };
  857. src2[0] += (srcSliceH - 1) * srcStride[0];
  858. if (!usePal(c->srcFormat))
  859. src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
  860. src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
  861. src2[3] += (srcSliceH - 1) * srcStride[3];
  862. dst2[0] += ( c->dstH - 1) * dstStride[0];
  863. dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
  864. dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
  865. dst2[3] += ( c->dstH - 1) * dstStride[3];
  866. reset_ptr(src2, c->srcFormat);
  867. reset_ptr((void*)dst2, c->dstFormat);
  868. /* reset slice direction at end of frame */
  869. if (!srcSliceY)
  870. c->sliceDir = 0;
  871. ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
  872. srcSliceH, dst2, dstStride2);
  873. }
  874. av_free(rgb0_tmp);
  875. return ret;
  876. }