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/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
35 #include "swscale_internal.h"
38 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
39 { 36, 68, 60, 92, 34, 66, 58, 90, },
40 { 100, 4, 124, 28, 98, 2, 122, 26, },
41 { 52, 84, 44, 76, 50, 82, 42, 74, },
42 { 116, 20, 108, 12, 114, 18, 106, 10, },
43 { 32, 64, 56, 88, 38, 70, 62, 94, },
44 { 96, 0, 120, 24, 102, 6, 126, 30, },
45 { 48, 80, 40, 72, 54, 86, 46, 78, },
46 { 112, 16, 104, 8, 118, 22, 110, 14, },
47 { 36, 68, 60, 92, 34, 66, 58, 90, },
50 DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
51 64, 64, 64, 64, 64, 64, 64, 64
54 static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
55 int height, int y, uint8_t val)
58 uint8_t *ptr = plane + stride * y;
59 for (i = 0; i < height; i++) {
60 memset(ptr, val, width);
65 static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
66 const uint8_t *_src, const int16_t *filter,
67 const int32_t *filterPos, int filterSize)
69 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
71 int32_t *dst = (int32_t *) _dst;
72 const uint16_t *src = (const uint16_t *) _src;
73 int bits = desc->comp[0].depth_minus1;
76 if((isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8) && desc->comp[0].depth_minus1<15)
79 for (i = 0; i < dstW; i++) {
81 int srcPos = filterPos[i];
84 for (j = 0; j < filterSize; j++) {
85 val += src[srcPos + j] * filter[filterSize * i + j];
87 // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
88 dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
92 static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW,
93 const uint8_t *_src, const int16_t *filter,
94 const int32_t *filterPos, int filterSize)
96 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
98 const uint16_t *src = (const uint16_t *) _src;
99 int sh = desc->comp[0].depth_minus1;
102 sh= isAnyRGB(c->srcFormat) || c->srcFormat==AV_PIX_FMT_PAL8 ? 13 : desc->comp[0].depth_minus1;
104 for (i = 0; i < dstW; i++) {
106 int srcPos = filterPos[i];
109 for (j = 0; j < filterSize; j++) {
110 val += src[srcPos + j] * filter[filterSize * i + j];
112 // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
113 dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
117 // bilinear / bicubic scaling
118 static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW,
119 const uint8_t *src, const int16_t *filter,
120 const int32_t *filterPos, int filterSize)
123 for (i = 0; i < dstW; i++) {
125 int srcPos = filterPos[i];
127 for (j = 0; j < filterSize; j++) {
128 val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
130 dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
134 static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW,
135 const uint8_t *src, const int16_t *filter,
136 const int32_t *filterPos, int filterSize)
139 int32_t *dst = (int32_t *) _dst;
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 >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
151 // FIXME all pal and rgb srcFormats could do this conversion as well
152 // FIXME all scalers more complex than bilinear could do half of this transform
153 static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
156 for (i = 0; i < width; i++) {
157 dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
158 dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
162 static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
165 for (i = 0; i < width; i++) {
166 dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
167 dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
171 static void lumRangeToJpeg_c(int16_t *dst, int width)
174 for (i = 0; i < width; i++)
175 dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
178 static void lumRangeFromJpeg_c(int16_t *dst, int width)
181 for (i = 0; i < width; i++)
182 dst[i] = (dst[i] * 14071 + 33561947) >> 14;
185 static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
188 int32_t *dstU = (int32_t *) _dstU;
189 int32_t *dstV = (int32_t *) _dstV;
190 for (i = 0; i < width; i++) {
191 dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
192 dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
196 static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
199 int32_t *dstU = (int32_t *) _dstU;
200 int32_t *dstV = (int32_t *) _dstV;
201 for (i = 0; i < width; i++) {
202 dstU[i] = (dstU[i] * 1799 + (4081085 << 4)) >> 11; // 1469
203 dstV[i] = (dstV[i] * 1799 + (4081085 << 4)) >> 11; // 1469
207 static void lumRangeToJpeg16_c(int16_t *_dst, int width)
210 int32_t *dst = (int32_t *) _dst;
211 for (i = 0; i < width; i++) {
212 dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
216 static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
219 int32_t *dst = (int32_t *) _dst;
220 for (i = 0; i < width; i++)
221 dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
224 static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
225 const uint8_t *src, int srcW, int xInc)
228 unsigned int xpos = 0;
229 for (i = 0; i < dstWidth; i++) {
230 register unsigned int xx = xpos >> 16;
231 register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
232 dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
235 for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
236 dst[i] = src[srcW-1]*128;
239 // *** horizontal scale Y line to temp buffer
240 static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
241 const uint8_t *src_in[4],
243 const int16_t *hLumFilter,
244 const int32_t *hLumFilterPos,
246 uint8_t *formatConvBuffer,
247 uint32_t *pal, int isAlpha)
249 void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
250 isAlpha ? c->alpToYV12 : c->lumToYV12;
251 void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
252 const uint8_t *src = src_in[isAlpha ? 3 : 0];
255 toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
256 src = formatConvBuffer;
257 } else if (c->readLumPlanar && !isAlpha) {
258 c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
259 src = formatConvBuffer;
260 } else if (c->readAlpPlanar && isAlpha) {
261 c->readAlpPlanar(formatConvBuffer, src_in, srcW, NULL);
262 src = formatConvBuffer;
265 if (!c->hyscale_fast) {
266 c->hyScale(c, dst, dstWidth, src, hLumFilter,
267 hLumFilterPos, hLumFilterSize);
268 } else { // fast bilinear upscale / crap downscale
269 c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
273 convertRange(dst, dstWidth);
276 static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
277 int dstWidth, const uint8_t *src1,
278 const uint8_t *src2, int srcW, int xInc)
281 unsigned int xpos = 0;
282 for (i = 0; i < dstWidth; i++) {
283 register unsigned int xx = xpos >> 16;
284 register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
285 dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
286 dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
289 for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
290 dst1[i] = src1[srcW-1]*128;
291 dst2[i] = src2[srcW-1]*128;
295 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
296 int16_t *dst2, int dstWidth,
297 const uint8_t *src_in[4],
299 const int16_t *hChrFilter,
300 const int32_t *hChrFilterPos,
302 uint8_t *formatConvBuffer, uint32_t *pal)
304 const uint8_t *src1 = src_in[1], *src2 = src_in[2];
306 uint8_t *buf2 = formatConvBuffer +
307 FFALIGN(srcW*2+78, 16);
308 c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
309 src1= formatConvBuffer;
311 } else if (c->readChrPlanar) {
312 uint8_t *buf2 = formatConvBuffer +
313 FFALIGN(srcW*2+78, 16);
314 c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
315 src1 = formatConvBuffer;
319 if (!c->hcscale_fast) {
320 c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
321 c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
322 } else { // fast bilinear upscale / crap downscale
323 c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
326 if (c->chrConvertRange)
327 c->chrConvertRange(dst1, dst2, dstWidth);
330 #define DEBUG_SWSCALE_BUFFERS 0
331 #define DEBUG_BUFFERS(...) \
332 if (DEBUG_SWSCALE_BUFFERS) \
333 av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
335 static int swscale(SwsContext *c, const uint8_t *src[],
336 int srcStride[], int srcSliceY,
337 int srcSliceH, uint8_t *dst[], int dstStride[])
339 /* load a few things into local vars to make the code more readable?
341 const int srcW = c->srcW;
342 const int dstW = c->dstW;
343 const int dstH = c->dstH;
344 const int chrDstW = c->chrDstW;
345 const int chrSrcW = c->chrSrcW;
346 const int lumXInc = c->lumXInc;
347 const int chrXInc = c->chrXInc;
348 const enum AVPixelFormat dstFormat = c->dstFormat;
349 const int flags = c->flags;
350 int32_t *vLumFilterPos = c->vLumFilterPos;
351 int32_t *vChrFilterPos = c->vChrFilterPos;
352 int32_t *hLumFilterPos = c->hLumFilterPos;
353 int32_t *hChrFilterPos = c->hChrFilterPos;
354 int16_t *hLumFilter = c->hLumFilter;
355 int16_t *hChrFilter = c->hChrFilter;
356 int32_t *lumMmxFilter = c->lumMmxFilter;
357 int32_t *chrMmxFilter = c->chrMmxFilter;
358 const int vLumFilterSize = c->vLumFilterSize;
359 const int vChrFilterSize = c->vChrFilterSize;
360 const int hLumFilterSize = c->hLumFilterSize;
361 const int hChrFilterSize = c->hChrFilterSize;
362 int16_t **lumPixBuf = c->lumPixBuf;
363 int16_t **chrUPixBuf = c->chrUPixBuf;
364 int16_t **chrVPixBuf = c->chrVPixBuf;
365 int16_t **alpPixBuf = c->alpPixBuf;
366 const int vLumBufSize = c->vLumBufSize;
367 const int vChrBufSize = c->vChrBufSize;
368 uint8_t *formatConvBuffer = c->formatConvBuffer;
369 uint32_t *pal = c->pal_yuv;
370 yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
371 yuv2planarX_fn yuv2planeX = c->yuv2planeX;
372 yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
373 yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
374 yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
375 yuv2packedX_fn yuv2packedX = c->yuv2packedX;
376 yuv2anyX_fn yuv2anyX = c->yuv2anyX;
377 const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
378 const int chrSrcSliceH = FF_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
379 int should_dither = is9_OR_10BPS(c->srcFormat) ||
380 is16BPS(c->srcFormat);
383 /* vars which will change and which we need to store back in the context */
385 int lumBufIndex = c->lumBufIndex;
386 int chrBufIndex = c->chrBufIndex;
387 int lastInLumBuf = c->lastInLumBuf;
388 int lastInChrBuf = c->lastInChrBuf;
390 if (!usePal(c->srcFormat)) {
391 pal = c->input_rgb2yuv_table;
394 if (isPacked(c->srcFormat)) {
402 srcStride[3] = srcStride[0];
404 srcStride[1] <<= c->vChrDrop;
405 srcStride[2] <<= c->vChrDrop;
407 DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
408 src[0], srcStride[0], src[1], srcStride[1],
409 src[2], srcStride[2], src[3], srcStride[3],
410 dst[0], dstStride[0], dst[1], dstStride[1],
411 dst[2], dstStride[2], dst[3], dstStride[3]);
412 DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
413 srcSliceY, srcSliceH, dstY, dstH);
414 DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
415 vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
417 if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
418 dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
419 static int warnedAlready = 0; // FIXME maybe move this into the context
420 if (flags & SWS_PRINT_INFO && !warnedAlready) {
421 av_log(c, AV_LOG_WARNING,
422 "Warning: dstStride is not aligned!\n"
423 " ->cannot do aligned memory accesses anymore\n");
428 if ( (uintptr_t)dst[0]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
429 || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
430 || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
431 || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
433 static int warnedAlready=0;
434 int cpu_flags = av_get_cpu_flags();
435 if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
436 av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
441 /* Note the user might start scaling the picture in the middle so this
442 * will not get executed. This is not really intended but works
443 * currently, so people might do it. */
444 if (srcSliceY == 0) {
452 if (!should_dither) {
453 c->chrDither8 = c->lumDither8 = sws_pb_64;
457 for (; dstY < dstH; dstY++) {
458 const int chrDstY = dstY >> c->chrDstVSubSample;
460 dst[0] + dstStride[0] * dstY,
461 dst[1] + dstStride[1] * chrDstY,
462 dst[2] + dstStride[2] * chrDstY,
463 (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
465 int use_mmx_vfilter= c->use_mmx_vfilter;
467 // First line needed as input
468 const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
469 const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
470 // First line needed as input
471 const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
473 // Last line needed as input
474 int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
475 int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
476 int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
479 // handle holes (FAST_BILINEAR & weird filters)
480 if (firstLumSrcY > lastInLumBuf)
481 lastInLumBuf = firstLumSrcY - 1;
482 if (firstChrSrcY > lastInChrBuf)
483 lastInChrBuf = firstChrSrcY - 1;
484 av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
485 av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
487 DEBUG_BUFFERS("dstY: %d\n", dstY);
488 DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
489 firstLumSrcY, lastLumSrcY, lastInLumBuf);
490 DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
491 firstChrSrcY, lastChrSrcY, lastInChrBuf);
493 // Do we have enough lines in this slice to output the dstY line
494 enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
495 lastChrSrcY < FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
498 lastLumSrcY = srcSliceY + srcSliceH - 1;
499 lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
500 DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
501 lastLumSrcY, lastChrSrcY);
504 // Do horizontal scaling
505 while (lastInLumBuf < lastLumSrcY) {
506 const uint8_t *src1[4] = {
507 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
508 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
509 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
510 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
513 av_assert0(lumBufIndex < 2 * vLumBufSize);
514 av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
515 av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
516 hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
517 hLumFilter, hLumFilterPos, hLumFilterSize,
518 formatConvBuffer, pal, 0);
519 if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
520 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
521 lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
522 formatConvBuffer, pal, 1);
524 DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
525 lumBufIndex, lastInLumBuf);
527 while (lastInChrBuf < lastChrSrcY) {
528 const uint8_t *src1[4] = {
529 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
530 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
531 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
532 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
535 av_assert0(chrBufIndex < 2 * vChrBufSize);
536 av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
537 av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
538 // FIXME replace parameters through context struct (some at least)
540 if (c->needs_hcscale)
541 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
542 chrDstW, src1, chrSrcW, chrXInc,
543 hChrFilter, hChrFilterPos, hChrFilterSize,
544 formatConvBuffer, pal);
546 DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
547 chrBufIndex, lastInChrBuf);
549 // wrap buf index around to stay inside the ring buffer
550 if (lumBufIndex >= vLumBufSize)
551 lumBufIndex -= vLumBufSize;
552 if (chrBufIndex >= vChrBufSize)
553 chrBufIndex -= vChrBufSize;
555 break; // we can't output a dstY line so let's try with the next slice
558 updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
559 lastInLumBuf, lastInChrBuf);
562 c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
563 c->lumDither8 = ff_dither_8x8_128[dstY & 7];
565 if (dstY >= dstH - 2) {
566 /* hmm looks like we can't use MMX here without overwriting
567 * this array's tail */
568 ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
569 &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
574 const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
575 const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
576 const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
577 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
578 (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
579 int16_t *vLumFilter = c->vLumFilter;
580 int16_t *vChrFilter = c->vChrFilter;
582 if (isPlanarYUV(dstFormat) ||
583 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
584 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
586 vLumFilter += dstY * vLumFilterSize;
587 vChrFilter += chrDstY * vChrFilterSize;
589 // av_assert0(use_mmx_vfilter != (
590 // yuv2planeX == yuv2planeX_10BE_c
591 // || yuv2planeX == yuv2planeX_10LE_c
592 // || yuv2planeX == yuv2planeX_9BE_c
593 // || yuv2planeX == yuv2planeX_9LE_c
594 // || yuv2planeX == yuv2planeX_16BE_c
595 // || yuv2planeX == yuv2planeX_16LE_c
596 // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
599 vLumFilter= (int16_t *)c->lumMmxFilter;
600 vChrFilter= (int16_t *)c->chrMmxFilter;
603 if (vLumFilterSize == 1) {
604 yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
606 yuv2planeX(vLumFilter, vLumFilterSize,
608 dstW, c->lumDither8, 0);
611 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
613 yuv2nv12cX(c, vChrFilter,
614 vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
616 } else if (vChrFilterSize == 1) {
617 yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
618 yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
620 yuv2planeX(vChrFilter,
621 vChrFilterSize, chrUSrcPtr, dest[1],
622 chrDstW, c->chrDither8, 0);
623 yuv2planeX(vChrFilter,
624 vChrFilterSize, chrVSrcPtr, dest[2],
625 chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
629 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
631 vLumFilter= (int16_t *)c->alpMmxFilter;
633 if (vLumFilterSize == 1) {
634 yuv2plane1(alpSrcPtr[0], dest[3], dstW,
637 yuv2planeX(vLumFilter,
638 vLumFilterSize, alpSrcPtr, dest[3],
639 dstW, c->lumDither8, 0);
642 } else if (yuv2packedX) {
643 av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
644 av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
645 if (c->yuv2packed1 && vLumFilterSize == 1 &&
646 vChrFilterSize <= 2) { // unscaled RGB
647 int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
648 yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
649 alpPixBuf ? *alpSrcPtr : NULL,
650 dest[0], dstW, chrAlpha, dstY);
651 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
652 vChrFilterSize == 2) { // bilinear upscale RGB
653 int lumAlpha = vLumFilter[2 * dstY + 1];
654 int chrAlpha = vChrFilter[2 * dstY + 1];
656 lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
658 chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
659 yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
660 alpPixBuf ? alpSrcPtr : NULL,
661 dest[0], dstW, lumAlpha, chrAlpha, dstY);
662 } else { // general RGB
663 yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
664 lumSrcPtr, vLumFilterSize,
665 vChrFilter + dstY * vChrFilterSize,
666 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
667 alpSrcPtr, dest[0], dstW, dstY);
670 av_assert1(!yuv2packed1 && !yuv2packed2);
671 yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
672 lumSrcPtr, vLumFilterSize,
673 vChrFilter + dstY * vChrFilterSize,
674 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
675 alpSrcPtr, dest, dstW, dstY);
679 if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
681 int height = dstY - lastDstY;
683 if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
684 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
685 fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
686 1, desc->comp[3].depth_minus1,
689 fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
692 #if HAVE_MMXEXT_INLINE
693 if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
694 __asm__ volatile ("sfence" ::: "memory");
698 /* store changed local vars back in the context */
700 c->lumBufIndex = lumBufIndex;
701 c->chrBufIndex = chrBufIndex;
702 c->lastInLumBuf = lastInLumBuf;
703 c->lastInChrBuf = lastInChrBuf;
705 return dstY - lastDstY;
708 av_cold void ff_sws_init_range_convert(SwsContext *c)
710 c->lumConvertRange = NULL;
711 c->chrConvertRange = NULL;
712 if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
713 if (c->dstBpc <= 14) {
715 c->lumConvertRange = lumRangeFromJpeg_c;
716 c->chrConvertRange = chrRangeFromJpeg_c;
718 c->lumConvertRange = lumRangeToJpeg_c;
719 c->chrConvertRange = chrRangeToJpeg_c;
723 c->lumConvertRange = lumRangeFromJpeg16_c;
724 c->chrConvertRange = chrRangeFromJpeg16_c;
726 c->lumConvertRange = lumRangeToJpeg16_c;
727 c->chrConvertRange = chrRangeToJpeg16_c;
733 static av_cold void sws_init_swscale(SwsContext *c)
735 enum AVPixelFormat srcFormat = c->srcFormat;
737 ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
738 &c->yuv2nv12cX, &c->yuv2packed1,
739 &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
741 ff_sws_init_input_funcs(c);
744 if (c->srcBpc == 8) {
745 if (c->dstBpc <= 14) {
746 c->hyScale = c->hcScale = hScale8To15_c;
747 if (c->flags & SWS_FAST_BILINEAR) {
748 c->hyscale_fast = hyscale_fast_c;
749 c->hcscale_fast = hcscale_fast_c;
752 c->hyScale = c->hcScale = hScale8To19_c;
755 c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
759 ff_sws_init_range_convert(c);
761 if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
762 srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
763 c->needs_hcscale = 1;
766 SwsFunc ff_getSwsFunc(SwsContext *c)
771 ff_sws_init_swscale_ppc(c);
773 ff_sws_init_swscale_x86(c);
778 static void reset_ptr(const uint8_t *src[], int format)
780 if (!isALPHA(format))
782 if (!isPlanar(format)) {
783 src[3] = src[2] = NULL;
790 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
791 const int linesizes[4])
793 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
796 for (i = 0; i < 4; i++) {
797 int plane = desc->comp[i].plane;
798 if (!data[plane] || !linesizes[plane])
805 static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
806 const uint16_t *src, int stride, int h)
809 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
811 for (yp=0; yp<h; yp++) {
812 for (xp=0; xp+2<stride; xp+=3) {
813 int x, y, z, r, g, b;
815 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
816 x = AV_RB16(src + xp + 0);
817 y = AV_RB16(src + xp + 1);
818 z = AV_RB16(src + xp + 2);
820 x = AV_RL16(src + xp + 0);
821 y = AV_RL16(src + xp + 1);
822 z = AV_RL16(src + xp + 2);
825 x = c->xyzgamma[x>>4];
826 y = c->xyzgamma[y>>4];
827 z = c->xyzgamma[z>>4];
829 // convert from XYZlinear to sRGBlinear
830 r = c->xyz2rgb_matrix[0][0] * x +
831 c->xyz2rgb_matrix[0][1] * y +
832 c->xyz2rgb_matrix[0][2] * z >> 12;
833 g = c->xyz2rgb_matrix[1][0] * x +
834 c->xyz2rgb_matrix[1][1] * y +
835 c->xyz2rgb_matrix[1][2] * z >> 12;
836 b = c->xyz2rgb_matrix[2][0] * x +
837 c->xyz2rgb_matrix[2][1] * y +
838 c->xyz2rgb_matrix[2][2] * z >> 12;
840 // limit values to 12-bit depth
841 r = av_clip_c(r,0,4095);
842 g = av_clip_c(g,0,4095);
843 b = av_clip_c(b,0,4095);
845 // convert from sRGBlinear to RGB and scale from 12bit to 16bit
846 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
847 AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
848 AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
849 AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
851 AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
852 AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
853 AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
861 static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
862 const uint16_t *src, int stride, int h)
865 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
867 for (yp=0; yp<h; yp++) {
868 for (xp=0; xp+2<stride; xp+=3) {
869 int x, y, z, r, g, b;
871 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
872 r = AV_RB16(src + xp + 0);
873 g = AV_RB16(src + xp + 1);
874 b = AV_RB16(src + xp + 2);
876 r = AV_RL16(src + xp + 0);
877 g = AV_RL16(src + xp + 1);
878 b = AV_RL16(src + xp + 2);
881 r = c->rgbgammainv[r>>4];
882 g = c->rgbgammainv[g>>4];
883 b = c->rgbgammainv[b>>4];
885 // convert from sRGBlinear to XYZlinear
886 x = c->rgb2xyz_matrix[0][0] * r +
887 c->rgb2xyz_matrix[0][1] * g +
888 c->rgb2xyz_matrix[0][2] * b >> 12;
889 y = c->rgb2xyz_matrix[1][0] * r +
890 c->rgb2xyz_matrix[1][1] * g +
891 c->rgb2xyz_matrix[1][2] * b >> 12;
892 z = c->rgb2xyz_matrix[2][0] * r +
893 c->rgb2xyz_matrix[2][1] * g +
894 c->rgb2xyz_matrix[2][2] * b >> 12;
896 // limit values to 12-bit depth
897 x = av_clip_c(x,0,4095);
898 y = av_clip_c(y,0,4095);
899 z = av_clip_c(z,0,4095);
901 // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
902 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
903 AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
904 AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
905 AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
907 AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
908 AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
909 AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
918 * swscale wrapper, so we don't need to export the SwsContext.
919 * Assumes planar YUV to be in YUV order instead of YVU.
921 int attribute_align_arg sws_scale(struct SwsContext *c,
922 const uint8_t * const srcSlice[],
923 const int srcStride[], int srcSliceY,
924 int srcSliceH, uint8_t *const dst[],
925 const int dstStride[])
928 const uint8_t *src2[4];
930 uint8_t *rgb0_tmp = NULL;
932 if (!srcStride || !dstStride || !dst || !srcSlice) {
933 av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
936 memcpy(src2, srcSlice, sizeof(src2));
937 memcpy(dst2, dst, sizeof(dst2));
939 // do not mess up sliceDir if we have a "trailing" 0-size slice
943 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
944 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
947 if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
948 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
952 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
953 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
956 if (c->sliceDir == 0) {
957 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
960 if (usePal(c->srcFormat)) {
961 for (i = 0; i < 256; i++) {
962 int r, g, b, y, u, v, a = 0xff;
963 if (c->srcFormat == AV_PIX_FMT_PAL8) {
964 uint32_t p = ((const uint32_t *)(srcSlice[1]))[i];
965 a = (p >> 24) & 0xFF;
966 r = (p >> 16) & 0xFF;
969 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
971 g = ((i >> 2) & 7) * 36;
973 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
975 g = ((i >> 3) & 7) * 36;
977 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
978 r = ( i >> 3 ) * 255;
979 g = ((i >> 1) & 3) * 85;
981 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
984 av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
985 b = ( i >> 3 ) * 255;
986 g = ((i >> 1) & 3) * 85;
989 #define RGB2YUV_SHIFT 15
990 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
991 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
992 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
993 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
994 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
995 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
996 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
997 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
998 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
1000 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1001 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1002 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
1003 c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
1005 switch (c->dstFormat) {
1006 case AV_PIX_FMT_BGR32:
1008 case AV_PIX_FMT_RGB24:
1010 c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
1012 case AV_PIX_FMT_BGR32_1:
1014 case AV_PIX_FMT_BGR24:
1016 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
1018 case AV_PIX_FMT_RGB32_1:
1020 case AV_PIX_FMT_RGB24:
1022 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
1024 case AV_PIX_FMT_RGB32:
1026 case AV_PIX_FMT_BGR24:
1029 c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
1034 if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
1037 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1039 return AVERROR(ENOMEM);
1041 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1042 for (y=0; y<srcSliceH; y++){
1043 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
1044 for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
1045 base[ srcStride[0]*y + x] = 0xFF;
1051 if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1053 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1055 return AVERROR(ENOMEM);
1057 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1059 xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
1063 if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
1064 for (i = 0; i < 4; i++)
1065 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
1068 // copy strides, so they can safely be modified
1069 if (c->sliceDir == 1) {
1070 // slices go from top to bottom
1071 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1073 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1076 reset_ptr(src2, c->srcFormat);
1077 reset_ptr((void*)dst2, c->dstFormat);
1079 /* reset slice direction at end of frame */
1080 if (srcSliceY + srcSliceH == c->srcH)
1083 ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1086 // slices go from bottom to top => we flip the image internally
1087 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1089 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1092 src2[0] += (srcSliceH - 1) * srcStride[0];
1093 if (!usePal(c->srcFormat))
1094 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1095 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1096 src2[3] += (srcSliceH - 1) * srcStride[3];
1097 dst2[0] += ( c->dstH - 1) * dstStride[0];
1098 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1099 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1100 dst2[3] += ( c->dstH - 1) * dstStride[3];
1102 reset_ptr(src2, c->srcFormat);
1103 reset_ptr((void*)dst2, c->dstFormat);
1105 /* reset slice direction at end of frame */
1109 ret = c->swscale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1110 srcSliceH, dst2, dstStride2);
1114 if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1115 /* replace on the same data */
1116 rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);