2 * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avassert.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
36 #include "swscale_internal.h"
39 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
40 { 36, 68, 60, 92, 34, 66, 58, 90, },
41 { 100, 4, 124, 28, 98, 2, 122, 26, },
42 { 52, 84, 44, 76, 50, 82, 42, 74, },
43 { 116, 20, 108, 12, 114, 18, 106, 10, },
44 { 32, 64, 56, 88, 38, 70, 62, 94, },
45 { 96, 0, 120, 24, 102, 6, 126, 30, },
46 { 48, 80, 40, 72, 54, 86, 46, 78, },
47 { 112, 16, 104, 8, 118, 22, 110, 14, },
48 { 36, 68, 60, 92, 34, 66, 58, 90, },
51 DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
52 64, 64, 64, 64, 64, 64, 64, 64
55 static void gamma_convert(uint8_t * src[], int width, uint16_t *gamma)
58 uint16_t *src1 = (uint16_t*)src[0];
60 for (i = 0; i < width; ++i) {
61 uint16_t r = AV_RL16(src1 + i*4 + 0);
62 uint16_t g = AV_RL16(src1 + i*4 + 1);
63 uint16_t b = AV_RL16(src1 + i*4 + 2);
65 AV_WL16(src1 + i*4 + 0, gamma[r]);
66 AV_WL16(src1 + i*4 + 1, gamma[g]);
67 AV_WL16(src1 + i*4 + 2, gamma[b]);
71 static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
72 int height, int y, uint8_t val)
75 uint8_t *ptr = plane + stride * y;
76 for (i = 0; i < height; i++) {
77 memset(ptr, val, width);
82 static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
83 const uint8_t *_src, const int16_t *filter,
84 const int32_t *filterPos, int filterSize)
86 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
88 int32_t *dst = (int32_t *) _dst;
89 const uint16_t *src = (const uint16_t *) _src;
90 int bits = desc->comp[0].depth_minus1;
93 if((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth_minus1<15)
96 for (i = 0; i < dstW; i++) {
98 int srcPos = filterPos[i];
101 for (j = 0; j < filterSize; j++) {
102 val += src[srcPos + j] * filter[filterSize * i + j];
104 // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
105 dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
109 static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
110 const uint8_t *_src, const int16_t *filter,
111 const int32_t *filterPos, int filterSize)
113 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
115 const uint16_t *src = (const uint16_t *) _src;
116 int sh = desc->comp[0].depth_minus1;
119 sh= isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : desc->comp[0].depth_minus1;
121 for (i = 0; i < dstW; i++) {
123 int srcPos = filterPos[i];
126 for (j = 0; j < filterSize; j++) {
127 val += src[srcPos + j] * filter[filterSize * i + j];
129 // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
130 dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
134 // bilinear / bicubic scaling
135 static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
136 const uint8_t *src, const int16_t *filter,
137 const int32_t *filterPos, int filterSize)
140 for (i = 0; i < dstW; i++) {
142 int srcPos = filterPos[i];
144 for (j = 0; j < filterSize; j++) {
145 val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
147 dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
151 static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
152 const uint8_t *src, const int16_t *filter,
153 const int32_t *filterPos, int filterSize)
156 int32_t *dst = (int32_t *) _dst;
157 for (i = 0; i < dstW; i++) {
159 int srcPos = filterPos[i];
161 for (j = 0; j < filterSize; j++) {
162 val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
164 dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
168 // FIXME all pal and rgb srcFormats could do this conversion as well
169 // FIXME all scalers more complex than bilinear could do half of this transform
170 static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
173 for (i = 0; i < width; i++) {
174 dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
175 dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
179 static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
182 for (i = 0; i < width; i++) {
183 dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
184 dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
188 static void lumRangeToJpeg_c(int16_t *dst, int width)
191 for (i = 0; i < width; i++)
192 dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
195 static void lumRangeFromJpeg_c(int16_t *dst, int width)
198 for (i = 0; i < width; i++)
199 dst[i] = (dst[i] * 14071 + 33561947) >> 14;
202 static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
205 int32_t *dstU = (int32_t *) _dstU;
206 int32_t *dstV = (int32_t *) _dstV;
207 for (i = 0; i < width; i++) {
208 dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
209 dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
213 static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
216 int32_t *dstU = (int32_t *) _dstU;
217 int32_t *dstV = (int32_t *) _dstV;
218 for (i = 0; i < width; i++) {
219 dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
220 dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
224 static void lumRangeToJpeg16_c(int16_t *_dst, int width)
227 int32_t *dst = (int32_t *) _dst;
228 for (i = 0; i < width; i++) {
229 dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
233 static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
236 int32_t *dst = (int32_t *) _dst;
237 for (i = 0; i < width; i++)
238 dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
241 // *** horizontal scale Y line to temp buffer
242 static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
243 const uint8_t *src_in[4],
245 const int16_t *hLumFilter,
246 const int32_t *hLumFilterPos,
248 uint8_t *formatConvBuffer,
249 uint32_t *pal, int isAlpha)
251 void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
252 isAlpha ? c->alpToYV12 : c->lumToYV12;
253 void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
254 const uint8_t *src = src_in[isAlpha ? 3 : 0];
257 toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
258 src = formatConvBuffer;
259 } else if (c->readLumPlanar && !isAlpha) {
260 c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
261 src = formatConvBuffer;
262 } else if (c->readAlpPlanar && isAlpha) {
263 c->readAlpPlanar(formatConvBuffer, src_in, srcW, NULL);
264 src = formatConvBuffer;
267 if (!c->hyscale_fast) {
268 c->hyScale(c, dst, dstWidth, src, hLumFilter,
269 hLumFilterPos, hLumFilterSize);
270 } else { // fast bilinear upscale / crap downscale
271 c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
275 convertRange(dst, dstWidth);
278 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
279 int16_t *dst2, int dstWidth,
280 const uint8_t *src_in[4],
282 const int16_t *hChrFilter,
283 const int32_t *hChrFilterPos,
285 uint8_t *formatConvBuffer, uint32_t *pal)
287 const uint8_t *src1 = src_in[1], *src2 = src_in[2];
289 uint8_t *buf2 = formatConvBuffer +
290 FFALIGN(srcW*2+78, 16);
291 c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
292 src1= formatConvBuffer;
294 } else if (c->readChrPlanar) {
295 uint8_t *buf2 = formatConvBuffer +
296 FFALIGN(srcW*2+78, 16);
297 c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
298 src1 = formatConvBuffer;
302 if (!c->hcscale_fast) {
303 c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
304 c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
305 } else { // fast bilinear upscale / crap downscale
306 c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
309 if (c->chrConvertRange)
310 c->chrConvertRange(dst1, dst2, dstWidth);
313 #define DEBUG_SWSCALE_BUFFERS 0
314 #define DEBUG_BUFFERS(...) \
315 if (DEBUG_SWSCALE_BUFFERS) \
316 av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
318 static int swscale(SwsContext *c, const uint8_t *src[],
319 int srcStride[], int srcSliceY,
320 int srcSliceH, uint8_t *dst[], int dstStride[])
322 /* load a few things into local vars to make the code more readable?
324 const int srcW = c->srcW;
325 const int dstW = c->dstW;
326 const int dstH = c->dstH;
327 const int chrDstW = c->chrDstW;
328 const int chrSrcW = c->chrSrcW;
329 const int lumXInc = c->lumXInc;
330 const int chrXInc = c->chrXInc;
331 const enum AVPixelFormat dstFormat = c->dstFormat;
332 const int flags = c->flags;
333 int32_t *vLumFilterPos = c->vLumFilterPos;
334 int32_t *vChrFilterPos = c->vChrFilterPos;
335 int32_t *hLumFilterPos = c->hLumFilterPos;
336 int32_t *hChrFilterPos = c->hChrFilterPos;
337 int16_t *hLumFilter = c->hLumFilter;
338 int16_t *hChrFilter = c->hChrFilter;
339 int32_t *lumMmxFilter = c->lumMmxFilter;
340 int32_t *chrMmxFilter = c->chrMmxFilter;
341 const int vLumFilterSize = c->vLumFilterSize;
342 const int vChrFilterSize = c->vChrFilterSize;
343 const int hLumFilterSize = c->hLumFilterSize;
344 const int hChrFilterSize = c->hChrFilterSize;
345 int16_t **lumPixBuf = c->lumPixBuf;
346 int16_t **chrUPixBuf = c->chrUPixBuf;
347 int16_t **chrVPixBuf = c->chrVPixBuf;
348 int16_t **alpPixBuf = c->alpPixBuf;
349 const int vLumBufSize = c->vLumBufSize;
350 const int vChrBufSize = c->vChrBufSize;
351 uint8_t *formatConvBuffer = c->formatConvBuffer;
352 uint32_t *pal = c->pal_yuv;
353 yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
354 yuv2planarX_fn yuv2planeX = c->yuv2planeX;
355 yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
356 yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
357 yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
358 yuv2packedX_fn yuv2packedX = c->yuv2packedX;
359 yuv2anyX_fn yuv2anyX = c->yuv2anyX;
360 const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
361 const int chrSrcSliceH = FF_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
362 int should_dither = is9_OR_10BPS(c->srcFormat) ||
363 is16BPS(c->srcFormat);
366 /* vars which will change and which we need to store back in the context */
368 int lumBufIndex = c->lumBufIndex;
369 int chrBufIndex = c->chrBufIndex;
370 int lastInLumBuf = c->lastInLumBuf;
371 int lastInChrBuf = c->lastInChrBuf;
372 int perform_gamma = c->is_internal_gamma;
374 int numDesc = c->numDesc;
376 int lumEnd = c->descIndex[0];
377 int chrStart = lumEnd;
378 int chrEnd = c->descIndex[1];
379 SwsSlice *src_slice = &c->slice[lumStart];
380 SwsSlice *dst_slice = &c->slice[c->numSlice-1];
381 SwsFilterDescriptor *desc = c->desc;
386 if (!usePal(c->srcFormat)) {
387 pal = c->input_rgb2yuv_table;
390 if (isPacked(c->srcFormat)) {
398 srcStride[3] = srcStride[0];
400 srcStride[1] <<= c->vChrDrop;
401 srcStride[2] <<= c->vChrDrop;
403 DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
404 src[0], srcStride[0], src[1], srcStride[1],
405 src[2], srcStride[2], src[3], srcStride[3],
406 dst[0], dstStride[0], dst[1], dstStride[1],
407 dst[2], dstStride[2], dst[3], dstStride[3]);
408 DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
409 srcSliceY, srcSliceH, dstY, dstH);
410 DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
411 vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
413 if (dstStride[0]&15 || dstStride[1]&15 ||
414 dstStride[2]&15 || dstStride[3]&15) {
415 static int warnedAlready = 0; // FIXME maybe move this into the context
416 if (flags & SWS_PRINT_INFO && !warnedAlready) {
417 av_log(c, AV_LOG_WARNING,
418 "Warning: dstStride is not aligned!\n"
419 " ->cannot do aligned memory accesses anymore\n");
424 if ( (uintptr_t)dst[0]&15 || (uintptr_t)dst[1]&15 || (uintptr_t)dst[2]&15
425 || (uintptr_t)src[0]&15 || (uintptr_t)src[1]&15 || (uintptr_t)src[2]&15
426 || dstStride[0]&15 || dstStride[1]&15 || dstStride[2]&15 || dstStride[3]&15
427 || srcStride[0]&15 || srcStride[1]&15 || srcStride[2]&15 || srcStride[3]&15
429 static int warnedAlready=0;
430 int cpu_flags = av_get_cpu_flags();
431 if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
432 av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
437 /* Note the user might start scaling the picture in the middle so this
438 * will not get executed. This is not really intended but works
439 * currently, so people might do it. */
440 if (srcSliceY == 0) {
448 if (!should_dither) {
449 c->chrDither8 = c->lumDither8 = sws_pb_64;
455 ff_init_slice_from_src(src_slice, (uint8_t**)src, srcStride, c->srcW,
456 srcSliceY, srcSliceH,
457 chrSrcSliceY, chrSrcSliceH);
459 dst_slice->plane[0].sliceY = lastInLumBuf + 1;
460 dst_slice->plane[1].sliceY = lastInChrBuf + 1;
461 dst_slice->plane[2].sliceY = lastInChrBuf + 1;
462 dst_slice->plane[3].sliceY = lastInLumBuf + 1;
464 dst_slice->plane[0].sliceH =
465 dst_slice->plane[1].sliceH =
466 dst_slice->plane[2].sliceH =
467 dst_slice->plane[3].sliceH = 0;
468 dst_slice->width = dstW;
470 for (; dstY < dstH; dstY++) {
471 const int chrDstY = dstY >> c->chrDstVSubSample;
473 dst[0] + dstStride[0] * dstY,
474 dst[1] + dstStride[1] * chrDstY,
475 dst[2] + dstStride[2] * chrDstY,
476 (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
478 int use_mmx_vfilter= c->use_mmx_vfilter;
480 // First line needed as input
481 const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
482 const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
483 // First line needed as input
484 const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
486 // Last line needed as input
487 int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
488 int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
489 int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
492 int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY;
494 // handle holes (FAST_BILINEAR & weird filters)
495 if (firstLumSrcY > lastInLumBuf) {
496 hasLumHoles = lastInLumBuf != firstLumSrcY - 1;
497 lastInLumBuf = firstLumSrcY - 1;
499 dst_slice->plane[0].sliceY = lastInLumBuf + 1;
500 dst_slice->plane[3].sliceY = lastInLumBuf + 1;
501 dst_slice->plane[0].sliceH =
502 dst_slice->plane[3].sliceH = 0;
505 if (firstChrSrcY > lastInChrBuf) {
506 hasChrHoles = lastInChrBuf != firstChrSrcY - 1;
507 lastInChrBuf = firstChrSrcY - 1;
509 dst_slice->plane[1].sliceY = lastInChrBuf + 1;
510 dst_slice->plane[2].sliceY = lastInChrBuf + 1;
511 dst_slice->plane[1].sliceH =
512 dst_slice->plane[2].sliceH = 0;
515 av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
516 av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
518 DEBUG_BUFFERS("dstY: %d\n", dstY);
519 DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
520 firstLumSrcY, lastLumSrcY, lastInLumBuf);
521 DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
522 firstChrSrcY, lastChrSrcY, lastInChrBuf);
524 // Do we have enough lines in this slice to output the dstY line
525 enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
526 lastChrSrcY < FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
529 lastLumSrcY = srcSliceY + srcSliceH - 1;
530 lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
531 DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
532 lastLumSrcY, lastChrSrcY);
536 posY = dst_slice->plane[0].sliceY + dst_slice->plane[0].sliceH;
537 if (posY <= lastLumSrcY && !hasLumHoles) {
538 firstPosY = FFMAX(firstLumSrcY, posY);
539 lastPosY = FFMIN(lastLumSrcY + MAX_LINES_AHEAD, srcSliceY + srcSliceH - 1);
541 firstPosY = lastInLumBuf + 1;
542 lastPosY = lastLumSrcY;
545 cPosY = dst_slice->plane[1].sliceY + dst_slice->plane[1].sliceH;
546 if (cPosY <= lastChrSrcY && !hasChrHoles) {
547 firstCPosY = FFMAX(firstChrSrcY, cPosY);
548 lastCPosY = FFMIN(lastChrSrcY + MAX_LINES_AHEAD, FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1);
550 firstCPosY = lastInChrBuf + 1;
551 lastCPosY = lastChrSrcY;
554 ff_rotate_slice(dst_slice, lastPosY, lastCPosY);
556 if (posY < lastLumSrcY + 1) {
557 for (i = lumStart; i < lumEnd; ++i)
558 desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1);
561 lumBufIndex += lastLumSrcY - lastInLumBuf;
562 lastInLumBuf = lastLumSrcY;
564 if (cPosY < lastChrSrcY + 1) {
565 for (i = chrStart; i < chrEnd; ++i)
566 desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1);
569 chrBufIndex += lastChrSrcY - lastInChrBuf;
570 lastInChrBuf = lastChrSrcY;
573 // Do horizontal scaling
574 while (lastInLumBuf < lastLumSrcY) {
575 const uint8_t *src1[4] = {
576 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
577 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
578 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
579 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
582 av_assert0(lumBufIndex < 2 * vLumBufSize);
583 av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
584 av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
587 // gamma_convert((uint8_t **)src1, srcW, c->inv_gamma);
589 hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
590 hLumFilter, hLumFilterPos, hLumFilterSize,
591 formatConvBuffer, pal, 0);
592 if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
593 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
594 lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
595 formatConvBuffer, pal, 1);
597 DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
598 lumBufIndex, lastInLumBuf);
600 while (lastInChrBuf < lastChrSrcY) {
601 const uint8_t *src1[4] = {
602 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
603 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
604 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
605 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
608 av_assert0(chrBufIndex < 2 * vChrBufSize);
609 av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
610 av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
611 // FIXME replace parameters through context struct (some at least)
613 if (c->needs_hcscale)
614 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
615 chrDstW, src1, chrSrcW, chrXInc,
616 hChrFilter, hChrFilterPos, hChrFilterSize,
617 formatConvBuffer, pal);
619 DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
620 chrBufIndex, lastInChrBuf);
623 // wrap buf index around to stay inside the ring buffer
624 if (lumBufIndex >= vLumBufSize)
625 lumBufIndex -= vLumBufSize;
626 if (chrBufIndex >= vChrBufSize)
627 chrBufIndex -= vChrBufSize;
629 break; // we can't output a dstY line so let's try with the next slice
632 ff_updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
633 lastInLumBuf, lastInChrBuf);
636 c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
637 c->lumDither8 = ff_dither_8x8_128[dstY & 7];
639 if (dstY >= dstH - 2) {
640 /* hmm looks like we can't use MMX here without overwriting
641 * this array's tail */
642 ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
643 &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
649 const int16_t **lumSrcPtr = (const int16_t **)(void*) dst_slice->plane[0].line + firstLumSrcY - dst_slice->plane[0].sliceY;
650 const int16_t **chrUSrcPtr = (const int16_t **)(void*) dst_slice->plane[1].line + firstChrSrcY - dst_slice->plane[1].sliceY;
651 const int16_t **chrVSrcPtr = (const int16_t **)(void*) dst_slice->plane[2].line + firstChrSrcY - dst_slice->plane[2].sliceY;
652 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
653 (const int16_t **)(void*) dst_slice->plane[3].line + firstLumSrcY - dst_slice->plane[3].sliceY : NULL;
655 const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
656 const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
657 const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
658 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
659 (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
661 int16_t *vLumFilter = c->vLumFilter;
662 int16_t *vChrFilter = c->vChrFilter;
664 if (isPlanarYUV(dstFormat) ||
665 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
666 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
668 vLumFilter += dstY * vLumFilterSize;
669 vChrFilter += chrDstY * vChrFilterSize;
671 // av_assert0(use_mmx_vfilter != (
672 // yuv2planeX == yuv2planeX_10BE_c
673 // || yuv2planeX == yuv2planeX_10LE_c
674 // || yuv2planeX == yuv2planeX_9BE_c
675 // || yuv2planeX == yuv2planeX_9LE_c
676 // || yuv2planeX == yuv2planeX_16BE_c
677 // || yuv2planeX == yuv2planeX_16LE_c
678 // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
681 vLumFilter= (int16_t *)c->lumMmxFilter;
682 vChrFilter= (int16_t *)c->chrMmxFilter;
685 if (vLumFilterSize == 1) {
686 yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
688 yuv2planeX(vLumFilter, vLumFilterSize,
690 dstW, c->lumDither8, 0);
693 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
695 yuv2nv12cX(c, vChrFilter,
696 vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
698 } else if (vChrFilterSize == 1) {
699 yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
700 yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
702 yuv2planeX(vChrFilter,
703 vChrFilterSize, chrUSrcPtr, dest[1],
704 chrDstW, c->chrDither8, 0);
705 yuv2planeX(vChrFilter,
706 vChrFilterSize, chrVSrcPtr, dest[2],
707 chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
711 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
713 vLumFilter= (int16_t *)c->alpMmxFilter;
715 if (vLumFilterSize == 1) {
716 yuv2plane1(alpSrcPtr[0], dest[3], dstW,
719 yuv2planeX(vLumFilter,
720 vLumFilterSize, alpSrcPtr, dest[3],
721 dstW, c->lumDither8, 0);
724 } else if (yuv2packedX) {
726 av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
727 av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
729 if (c->yuv2packed1 && vLumFilterSize == 1 &&
730 vChrFilterSize <= 2) { // unscaled RGB
731 int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
732 yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
733 alpPixBuf ? *alpSrcPtr : NULL,
734 dest[0], dstW, chrAlpha, dstY);
735 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
736 vChrFilterSize == 2) { // bilinear upscale RGB
737 int lumAlpha = vLumFilter[2 * dstY + 1];
738 int chrAlpha = vChrFilter[2 * dstY + 1];
740 lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
742 chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
743 yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
744 alpPixBuf ? alpSrcPtr : NULL,
745 dest[0], dstW, lumAlpha, chrAlpha, dstY);
746 } else { // general RGB
747 yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
748 lumSrcPtr, vLumFilterSize,
749 vChrFilter + dstY * vChrFilterSize,
750 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
751 alpSrcPtr, dest[0], dstW, dstY);
754 av_assert1(!yuv2packed1 && !yuv2packed2);
755 yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
756 lumSrcPtr, vLumFilterSize,
757 vChrFilter + dstY * vChrFilterSize,
758 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
759 alpSrcPtr, dest, dstW, dstY);
762 // gamma_convert(dest, dstW, c->gamma);
765 if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
767 int height = dstY - lastDstY;
769 if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
770 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
771 fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
772 1, desc->comp[3].depth_minus1,
775 fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
778 #if HAVE_MMXEXT_INLINE
779 if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
780 __asm__ volatile ("sfence" ::: "memory");
784 /* store changed local vars back in the context */
786 c->lumBufIndex = lumBufIndex;
787 c->chrBufIndex = chrBufIndex;
788 c->lastInLumBuf = lastInLumBuf;
789 c->lastInChrBuf = lastInChrBuf;
791 return dstY - lastDstY;
794 av_cold void ff_sws_init_range_convert(SwsContext *c)
796 c->lumConvertRange = NULL;
797 c->chrConvertRange = NULL;
798 if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
799 if (c->dstBpc <= 14) {
801 c->lumConvertRange = lumRangeFromJpeg_c;
802 c->chrConvertRange = chrRangeFromJpeg_c;
804 c->lumConvertRange = lumRangeToJpeg_c;
805 c->chrConvertRange = chrRangeToJpeg_c;
809 c->lumConvertRange = lumRangeFromJpeg16_c;
810 c->chrConvertRange = chrRangeFromJpeg16_c;
812 c->lumConvertRange = lumRangeToJpeg16_c;
813 c->chrConvertRange = chrRangeToJpeg16_c;
819 static av_cold void sws_init_swscale(SwsContext *c)
821 enum AVPixelFormat srcFormat = c->srcFormat;
823 ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
824 &c->yuv2nv12cX, &c->yuv2packed1,
825 &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
827 ff_sws_init_input_funcs(c);
830 if (c->srcBpc == 8) {
831 if (c->dstBpc <= 14) {
832 c->hyScale = c->hcScale = hScale8To15_c;
833 if (c->flags & SWS_FAST_BILINEAR) {
834 c->hyscale_fast = ff_hyscale_fast_c;
835 c->hcscale_fast = ff_hcscale_fast_c;
838 c->hyScale = c->hcScale = hScale8To19_c;
841 c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
845 ff_sws_init_range_convert(c);
847 if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
848 srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
849 c->needs_hcscale = 1;
852 SwsFunc ff_getSwsFunc(SwsContext *c)
857 ff_sws_init_swscale_ppc(c);
859 ff_sws_init_swscale_x86(c);
864 static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
866 if (!isALPHA(format))
868 if (!isPlanar(format)) {
869 src[3] = src[2] = NULL;
876 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
877 const int linesizes[4])
879 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
884 for (i = 0; i < 4; i++) {
885 int plane = desc->comp[i].plane;
886 if (!data[plane] || !linesizes[plane])
893 static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
894 const uint16_t *src, int stride, int h)
897 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
899 for (yp=0; yp<h; yp++) {
900 for (xp=0; xp+2<stride; xp+=3) {
901 int x, y, z, r, g, b;
903 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
904 x = AV_RB16(src + xp + 0);
905 y = AV_RB16(src + xp + 1);
906 z = AV_RB16(src + xp + 2);
908 x = AV_RL16(src + xp + 0);
909 y = AV_RL16(src + xp + 1);
910 z = AV_RL16(src + xp + 2);
913 x = c->xyzgamma[x>>4];
914 y = c->xyzgamma[y>>4];
915 z = c->xyzgamma[z>>4];
917 // convert from XYZlinear to sRGBlinear
918 r = c->xyz2rgb_matrix[0][0] * x +
919 c->xyz2rgb_matrix[0][1] * y +
920 c->xyz2rgb_matrix[0][2] * z >> 12;
921 g = c->xyz2rgb_matrix[1][0] * x +
922 c->xyz2rgb_matrix[1][1] * y +
923 c->xyz2rgb_matrix[1][2] * z >> 12;
924 b = c->xyz2rgb_matrix[2][0] * x +
925 c->xyz2rgb_matrix[2][1] * y +
926 c->xyz2rgb_matrix[2][2] * z >> 12;
928 // limit values to 12-bit depth
929 r = av_clip_uintp2(r, 12);
930 g = av_clip_uintp2(g, 12);
931 b = av_clip_uintp2(b, 12);
933 // convert from sRGBlinear to RGB and scale from 12bit to 16bit
934 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
935 AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
936 AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
937 AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
939 AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
940 AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
941 AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
949 static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
950 const uint16_t *src, int stride, int h)
953 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
955 for (yp=0; yp<h; yp++) {
956 for (xp=0; xp+2<stride; xp+=3) {
957 int x, y, z, r, g, b;
959 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
960 r = AV_RB16(src + xp + 0);
961 g = AV_RB16(src + xp + 1);
962 b = AV_RB16(src + xp + 2);
964 r = AV_RL16(src + xp + 0);
965 g = AV_RL16(src + xp + 1);
966 b = AV_RL16(src + xp + 2);
969 r = c->rgbgammainv[r>>4];
970 g = c->rgbgammainv[g>>4];
971 b = c->rgbgammainv[b>>4];
973 // convert from sRGBlinear to XYZlinear
974 x = c->rgb2xyz_matrix[0][0] * r +
975 c->rgb2xyz_matrix[0][1] * g +
976 c->rgb2xyz_matrix[0][2] * b >> 12;
977 y = c->rgb2xyz_matrix[1][0] * r +
978 c->rgb2xyz_matrix[1][1] * g +
979 c->rgb2xyz_matrix[1][2] * b >> 12;
980 z = c->rgb2xyz_matrix[2][0] * r +
981 c->rgb2xyz_matrix[2][1] * g +
982 c->rgb2xyz_matrix[2][2] * b >> 12;
984 // limit values to 12-bit depth
985 x = av_clip_uintp2(x, 12);
986 y = av_clip_uintp2(y, 12);
987 z = av_clip_uintp2(z, 12);
989 // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
990 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
991 AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
992 AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
993 AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
995 AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
996 AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
997 AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
1006 * swscale wrapper, so we don't need to export the SwsContext.
1007 * Assumes planar YUV to be in YUV order instead of YVU.
1009 int attribute_align_arg sws_scale(struct SwsContext *c,
1010 const uint8_t * const srcSlice[],
1011 const int srcStride[], int srcSliceY,
1012 int srcSliceH, uint8_t *const dst[],
1013 const int dstStride[])
1016 const uint8_t *src2[4];
1018 uint8_t *rgb0_tmp = NULL;
1020 if (!srcStride || !dstStride || !dst || !srcSlice) {
1021 av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
1025 if (c->gamma_flag && c->cascaded_context[0]) {
1028 ret = sws_scale(c->cascaded_context[0],
1029 srcSlice, srcStride, srcSliceY, srcSliceH,
1030 c->cascaded_tmp, c->cascaded_tmpStride);
1035 if (c->cascaded_context[2])
1036 ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, c->cascaded1_tmp, c->cascaded1_tmpStride);
1038 ret = sws_scale(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp, c->cascaded_tmpStride, srcSliceY, srcSliceH, dst, dstStride);
1043 if (c->cascaded_context[2]) {
1044 ret = sws_scale(c->cascaded_context[2],
1045 (const uint8_t * const *)c->cascaded1_tmp, c->cascaded1_tmpStride, c->cascaded_context[1]->dstY - ret, c->cascaded_context[1]->dstY,
1051 if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->srcH) {
1052 ret = sws_scale(c->cascaded_context[0],
1053 srcSlice, srcStride, srcSliceY, srcSliceH,
1054 c->cascaded_tmp, c->cascaded_tmpStride);
1057 ret = sws_scale(c->cascaded_context[1],
1058 (const uint8_t * const * )c->cascaded_tmp, c->cascaded_tmpStride, 0, c->cascaded_context[0]->dstH,
1063 memcpy(src2, srcSlice, sizeof(src2));
1064 memcpy(dst2, dst, sizeof(dst2));
1066 // do not mess up sliceDir if we have a "trailing" 0-size slice
1070 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
1071 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1074 if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
1075 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1079 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1080 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1083 if (c->sliceDir == 0) {
1084 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1087 if (usePal(c->srcFormat)) {
1088 for (i = 0; i < 256; i++) {
1089 int r, g, b, y, u, v, a = 0xff;
1090 if (c->srcFormat == AV_PIX_FMT_PAL8) {
1091 uint32_t p = ((const uint32_t *)(srcSlice[1]))[i];
1092 a = (p >> 24) & 0xFF;
1093 r = (p >> 16) & 0xFF;
1094 g = (p >> 8) & 0xFF;
1096 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
1097 r = ( i >> 5 ) * 36;
1098 g = ((i >> 2) & 7) * 36;
1100 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
1101 b = ( i >> 6 ) * 85;
1102 g = ((i >> 3) & 7) * 36;
1104 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
1105 r = ( i >> 3 ) * 255;
1106 g = ((i >> 1) & 3) * 85;
1108 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
1111 av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
1112 b = ( i >> 3 ) * 255;
1113 g = ((i >> 1) & 3) * 85;
1116 #define RGB2YUV_SHIFT 15
1117 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1118 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1119 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1120 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1121 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1122 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1123 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1124 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1125 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1127 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1128 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1129 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1130 c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
1132 switch (c->dstFormat) {
1133 case AV_PIX_FMT_BGR32:
1135 case AV_PIX_FMT_RGB24:
1137 c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
1139 case AV_PIX_FMT_BGR32_1:
1141 case AV_PIX_FMT_BGR24:
1143 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
1145 case AV_PIX_FMT_RGB32_1:
1147 case AV_PIX_FMT_RGB24:
1149 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
1151 case AV_PIX_FMT_RGB32:
1153 case AV_PIX_FMT_BGR24:
1156 c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
1161 if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
1164 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1166 return AVERROR(ENOMEM);
1168 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1169 for (y=0; y<srcSliceH; y++){
1170 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
1171 for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
1172 base[ srcStride[0]*y + x] = 0xFF;
1178 if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1180 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1182 return AVERROR(ENOMEM);
1184 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1186 xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
1190 if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
1191 for (i = 0; i < 4; i++)
1192 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
1195 // copy strides, so they can safely be modified
1196 if (c->sliceDir == 1) {
1197 // slices go from top to bottom
1198 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1200 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1203 reset_ptr(src2, c->srcFormat);
1204 reset_ptr((void*)dst2, c->dstFormat);
1206 /* reset slice direction at end of frame */
1207 if (srcSliceY + srcSliceH == c->srcH)
1210 ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1213 // slices go from bottom to top => we flip the image internally
1214 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1216 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1219 src2[0] += (srcSliceH - 1) * srcStride[0];
1220 if (!usePal(c->srcFormat))
1221 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1222 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1223 src2[3] += (srcSliceH - 1) * srcStride[3];
1224 dst2[0] += ( c->dstH - 1) * dstStride[0];
1225 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1226 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1227 dst2[3] += ( c->dstH - 1) * dstStride[3];
1229 reset_ptr(src2, c->srcFormat);
1230 reset_ptr((void*)dst2, c->dstFormat);
1232 /* reset slice direction at end of frame */
1236 ret = c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1237 srcSliceH, dst2, dstStride2);
1241 if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1242 /* replace on the same data */
1243 rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);