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] = (FFMIN(dst[i], 30189 << 4) * 4769 - (39057361 << 2)) >> 12;
215 static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
218 int32_t *dst = (int32_t *) _dst;
219 for (i = 0; i < width; i++)
220 dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
223 static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
224 const uint8_t *src, int srcW, int xInc)
227 unsigned int xpos = 0;
228 for (i = 0; i < dstWidth; i++) {
229 register unsigned int xx = xpos >> 16;
230 register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
231 dst[i] = (src[xx] << 7) + (src[xx + 1] - src[xx]) * xalpha;
234 for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
235 dst[i] = src[srcW-1]*128;
238 // *** horizontal scale Y line to temp buffer
239 static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
240 const uint8_t *src_in[4],
242 const int16_t *hLumFilter,
243 const int32_t *hLumFilterPos,
245 uint8_t *formatConvBuffer,
246 uint32_t *pal, int isAlpha)
248 void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) =
249 isAlpha ? c->alpToYV12 : c->lumToYV12;
250 void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
251 const uint8_t *src = src_in[isAlpha ? 3 : 0];
254 toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
255 src = formatConvBuffer;
256 } else if (c->readLumPlanar && !isAlpha) {
257 c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
258 src = formatConvBuffer;
259 } else if (c->readAlpPlanar && isAlpha) {
260 c->readAlpPlanar(formatConvBuffer, src_in, srcW, NULL);
261 src = formatConvBuffer;
264 if (!c->hyscale_fast) {
265 c->hyScale(c, dst, dstWidth, src, hLumFilter,
266 hLumFilterPos, hLumFilterSize);
267 } else { // fast bilinear upscale / crap downscale
268 c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
272 convertRange(dst, dstWidth);
275 static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
276 int dstWidth, const uint8_t *src1,
277 const uint8_t *src2, int srcW, int xInc)
280 unsigned int xpos = 0;
281 for (i = 0; i < dstWidth; i++) {
282 register unsigned int xx = xpos >> 16;
283 register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
284 dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
285 dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
288 for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
289 dst1[i] = src1[srcW-1]*128;
290 dst2[i] = src2[srcW-1]*128;
294 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
295 int16_t *dst2, int dstWidth,
296 const uint8_t *src_in[4],
298 const int16_t *hChrFilter,
299 const int32_t *hChrFilterPos,
301 uint8_t *formatConvBuffer, uint32_t *pal)
303 const uint8_t *src1 = src_in[1], *src2 = src_in[2];
305 uint8_t *buf2 = formatConvBuffer +
306 FFALIGN(srcW*2+78, 16);
307 c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
308 src1= formatConvBuffer;
310 } else if (c->readChrPlanar) {
311 uint8_t *buf2 = formatConvBuffer +
312 FFALIGN(srcW*2+78, 16);
313 c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
314 src1 = formatConvBuffer;
318 if (!c->hcscale_fast) {
319 c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
320 c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
321 } else { // fast bilinear upscale / crap downscale
322 c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
325 if (c->chrConvertRange)
326 c->chrConvertRange(dst1, dst2, dstWidth);
329 #define DEBUG_SWSCALE_BUFFERS 0
330 #define DEBUG_BUFFERS(...) \
331 if (DEBUG_SWSCALE_BUFFERS) \
332 av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
334 static int swScale(SwsContext *c, const uint8_t *src[],
335 int srcStride[], int srcSliceY,
336 int srcSliceH, uint8_t *dst[], int dstStride[])
338 /* load a few things into local vars to make the code more readable?
340 const int srcW = c->srcW;
341 const int dstW = c->dstW;
342 const int dstH = c->dstH;
343 const int chrDstW = c->chrDstW;
344 const int chrSrcW = c->chrSrcW;
345 const int lumXInc = c->lumXInc;
346 const int chrXInc = c->chrXInc;
347 const enum AVPixelFormat dstFormat = c->dstFormat;
348 const int flags = c->flags;
349 int32_t *vLumFilterPos = c->vLumFilterPos;
350 int32_t *vChrFilterPos = c->vChrFilterPos;
351 int32_t *hLumFilterPos = c->hLumFilterPos;
352 int32_t *hChrFilterPos = c->hChrFilterPos;
353 int16_t *hLumFilter = c->hLumFilter;
354 int16_t *hChrFilter = c->hChrFilter;
355 int32_t *lumMmxFilter = c->lumMmxFilter;
356 int32_t *chrMmxFilter = c->chrMmxFilter;
357 const int vLumFilterSize = c->vLumFilterSize;
358 const int vChrFilterSize = c->vChrFilterSize;
359 const int hLumFilterSize = c->hLumFilterSize;
360 const int hChrFilterSize = c->hChrFilterSize;
361 int16_t **lumPixBuf = c->lumPixBuf;
362 int16_t **chrUPixBuf = c->chrUPixBuf;
363 int16_t **chrVPixBuf = c->chrVPixBuf;
364 int16_t **alpPixBuf = c->alpPixBuf;
365 const int vLumBufSize = c->vLumBufSize;
366 const int vChrBufSize = c->vChrBufSize;
367 uint8_t *formatConvBuffer = c->formatConvBuffer;
368 uint32_t *pal = c->pal_yuv;
369 yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
370 yuv2planarX_fn yuv2planeX = c->yuv2planeX;
371 yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
372 yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
373 yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
374 yuv2packedX_fn yuv2packedX = c->yuv2packedX;
375 yuv2anyX_fn yuv2anyX = c->yuv2anyX;
376 const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
377 const int chrSrcSliceH = FF_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
378 int should_dither = is9_OR_10BPS(c->srcFormat) ||
379 is16BPS(c->srcFormat);
382 /* vars which will change and which we need to store back in the context */
384 int lumBufIndex = c->lumBufIndex;
385 int chrBufIndex = c->chrBufIndex;
386 int lastInLumBuf = c->lastInLumBuf;
387 int lastInChrBuf = c->lastInChrBuf;
389 if (!usePal(c->srcFormat)) {
390 pal = c->input_rgb2yuv_table;
393 if (isPacked(c->srcFormat)) {
401 srcStride[3] = srcStride[0];
403 srcStride[1] <<= c->vChrDrop;
404 srcStride[2] <<= c->vChrDrop;
406 DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
407 src[0], srcStride[0], src[1], srcStride[1],
408 src[2], srcStride[2], src[3], srcStride[3],
409 dst[0], dstStride[0], dst[1], dstStride[1],
410 dst[2], dstStride[2], dst[3], dstStride[3]);
411 DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
412 srcSliceY, srcSliceH, dstY, dstH);
413 DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
414 vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
416 if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 ||
417 dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
418 static int warnedAlready = 0; // FIXME maybe move this into the context
419 if (flags & SWS_PRINT_INFO && !warnedAlready) {
420 av_log(c, AV_LOG_WARNING,
421 "Warning: dstStride is not aligned!\n"
422 " ->cannot do aligned memory accesses anymore\n");
427 if ( (uintptr_t)dst[0]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
428 || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
429 || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
430 || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
432 static int warnedAlready=0;
433 int cpu_flags = av_get_cpu_flags();
434 if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
435 av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
440 /* Note the user might start scaling the picture in the middle so this
441 * will not get executed. This is not really intended but works
442 * currently, so people might do it. */
443 if (srcSliceY == 0) {
451 if (!should_dither) {
452 c->chrDither8 = c->lumDither8 = sws_pb_64;
456 for (; dstY < dstH; dstY++) {
457 const int chrDstY = dstY >> c->chrDstVSubSample;
459 dst[0] + dstStride[0] * dstY,
460 dst[1] + dstStride[1] * chrDstY,
461 dst[2] + dstStride[2] * chrDstY,
462 (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
464 int use_mmx_vfilter= c->use_mmx_vfilter;
466 // First line needed as input
467 const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
468 const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
469 // First line needed as input
470 const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
472 // Last line needed as input
473 int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
474 int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
475 int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
478 // handle holes (FAST_BILINEAR & weird filters)
479 if (firstLumSrcY > lastInLumBuf)
480 lastInLumBuf = firstLumSrcY - 1;
481 if (firstChrSrcY > lastInChrBuf)
482 lastInChrBuf = firstChrSrcY - 1;
483 av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
484 av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
486 DEBUG_BUFFERS("dstY: %d\n", dstY);
487 DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
488 firstLumSrcY, lastLumSrcY, lastInLumBuf);
489 DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
490 firstChrSrcY, lastChrSrcY, lastInChrBuf);
492 // Do we have enough lines in this slice to output the dstY line
493 enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
494 lastChrSrcY < FF_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
497 lastLumSrcY = srcSliceY + srcSliceH - 1;
498 lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
499 DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
500 lastLumSrcY, lastChrSrcY);
503 // Do horizontal scaling
504 while (lastInLumBuf < lastLumSrcY) {
505 const uint8_t *src1[4] = {
506 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
507 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
508 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
509 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
512 av_assert0(lumBufIndex < 2 * vLumBufSize);
513 av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
514 av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
515 hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
516 hLumFilter, hLumFilterPos, hLumFilterSize,
517 formatConvBuffer, pal, 0);
518 if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
519 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
520 lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
521 formatConvBuffer, pal, 1);
523 DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
524 lumBufIndex, lastInLumBuf);
526 while (lastInChrBuf < lastChrSrcY) {
527 const uint8_t *src1[4] = {
528 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
529 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
530 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
531 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
534 av_assert0(chrBufIndex < 2 * vChrBufSize);
535 av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
536 av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
537 // FIXME replace parameters through context struct (some at least)
539 if (c->needs_hcscale)
540 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
541 chrDstW, src1, chrSrcW, chrXInc,
542 hChrFilter, hChrFilterPos, hChrFilterSize,
543 formatConvBuffer, pal);
545 DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
546 chrBufIndex, lastInChrBuf);
548 // wrap buf index around to stay inside the ring buffer
549 if (lumBufIndex >= vLumBufSize)
550 lumBufIndex -= vLumBufSize;
551 if (chrBufIndex >= vChrBufSize)
552 chrBufIndex -= vChrBufSize;
554 break; // we can't output a dstY line so let's try with the next slice
557 updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
558 lastInLumBuf, lastInChrBuf);
561 c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
562 c->lumDither8 = ff_dither_8x8_128[dstY & 7];
564 if (dstY >= dstH - 2) {
565 /* hmm looks like we can't use MMX here without overwriting
566 * this array's tail */
567 ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
568 &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
573 const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
574 const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
575 const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
576 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
577 (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
578 int16_t *vLumFilter = c->vLumFilter;
579 int16_t *vChrFilter = c->vChrFilter;
581 if (isPlanarYUV(dstFormat) ||
582 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
583 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
585 vLumFilter += dstY * vLumFilterSize;
586 vChrFilter += chrDstY * vChrFilterSize;
588 // av_assert0(use_mmx_vfilter != (
589 // yuv2planeX == yuv2planeX_10BE_c
590 // || yuv2planeX == yuv2planeX_10LE_c
591 // || yuv2planeX == yuv2planeX_9BE_c
592 // || yuv2planeX == yuv2planeX_9LE_c
593 // || yuv2planeX == yuv2planeX_16BE_c
594 // || yuv2planeX == yuv2planeX_16LE_c
595 // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
598 vLumFilter= (int16_t *)c->lumMmxFilter;
599 vChrFilter= (int16_t *)c->chrMmxFilter;
602 if (vLumFilterSize == 1) {
603 yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
605 yuv2planeX(vLumFilter, vLumFilterSize,
607 dstW, c->lumDither8, 0);
610 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
612 yuv2nv12cX(c, vChrFilter,
613 vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
615 } else if (vChrFilterSize == 1) {
616 yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
617 yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
619 yuv2planeX(vChrFilter,
620 vChrFilterSize, chrUSrcPtr, dest[1],
621 chrDstW, c->chrDither8, 0);
622 yuv2planeX(vChrFilter,
623 vChrFilterSize, chrVSrcPtr, dest[2],
624 chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
628 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
630 vLumFilter= (int16_t *)c->alpMmxFilter;
632 if (vLumFilterSize == 1) {
633 yuv2plane1(alpSrcPtr[0], dest[3], dstW,
636 yuv2planeX(vLumFilter,
637 vLumFilterSize, alpSrcPtr, dest[3],
638 dstW, c->lumDither8, 0);
641 } else if (yuv2packedX) {
642 av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
643 av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
644 if (c->yuv2packed1 && vLumFilterSize == 1 &&
645 vChrFilterSize <= 2) { // unscaled RGB
646 int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
647 yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
648 alpPixBuf ? *alpSrcPtr : NULL,
649 dest[0], dstW, chrAlpha, dstY);
650 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
651 vChrFilterSize == 2) { // bilinear upscale RGB
652 int lumAlpha = vLumFilter[2 * dstY + 1];
653 int chrAlpha = vChrFilter[2 * dstY + 1];
655 lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
657 chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
658 yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
659 alpPixBuf ? alpSrcPtr : NULL,
660 dest[0], dstW, lumAlpha, chrAlpha, dstY);
661 } else { // general RGB
662 yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
663 lumSrcPtr, vLumFilterSize,
664 vChrFilter + dstY * vChrFilterSize,
665 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
666 alpSrcPtr, dest[0], dstW, dstY);
669 av_assert1(!yuv2packed1 && !yuv2packed2);
670 yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
671 lumSrcPtr, vLumFilterSize,
672 vChrFilter + dstY * vChrFilterSize,
673 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
674 alpSrcPtr, dest, dstW, dstY);
678 if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
680 int height = dstY - lastDstY;
682 if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
683 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
684 fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
685 1, desc->comp[3].depth_minus1,
688 fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
691 #if HAVE_MMXEXT_INLINE
692 if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
693 __asm__ volatile ("sfence" ::: "memory");
697 /* store changed local vars back in the context */
699 c->lumBufIndex = lumBufIndex;
700 c->chrBufIndex = chrBufIndex;
701 c->lastInLumBuf = lastInLumBuf;
702 c->lastInChrBuf = lastInChrBuf;
704 return dstY - lastDstY;
707 static av_cold void sws_init_swScale_c(SwsContext *c)
709 enum AVPixelFormat srcFormat = c->srcFormat;
711 ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
712 &c->yuv2nv12cX, &c->yuv2packed1,
713 &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
715 ff_sws_init_input_funcs(c);
718 if (c->srcBpc == 8) {
719 if (c->dstBpc <= 14) {
720 c->hyScale = c->hcScale = hScale8To15_c;
721 if (c->flags & SWS_FAST_BILINEAR) {
722 c->hyscale_fast = hyscale_fast_c;
723 c->hcscale_fast = hcscale_fast_c;
726 c->hyScale = c->hcScale = hScale8To19_c;
729 c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
733 if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
734 if (c->dstBpc <= 14) {
736 c->lumConvertRange = lumRangeFromJpeg_c;
737 c->chrConvertRange = chrRangeFromJpeg_c;
739 c->lumConvertRange = lumRangeToJpeg_c;
740 c->chrConvertRange = chrRangeToJpeg_c;
744 c->lumConvertRange = lumRangeFromJpeg16_c;
745 c->chrConvertRange = chrRangeFromJpeg16_c;
747 c->lumConvertRange = lumRangeToJpeg16_c;
748 c->chrConvertRange = chrRangeToJpeg16_c;
753 if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
754 srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
755 c->needs_hcscale = 1;
758 SwsFunc ff_getSwsFunc(SwsContext *c)
760 sws_init_swScale_c(c);
763 ff_sws_init_swScale_mmx(c);
765 ff_sws_init_swScale_altivec(c);
770 static void reset_ptr(const uint8_t *src[], int format)
772 if (!isALPHA(format))
774 if (!isPlanar(format)) {
775 src[3] = src[2] = NULL;
782 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
783 const int linesizes[4])
785 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
788 for (i = 0; i < 4; i++) {
789 int plane = desc->comp[i].plane;
790 if (!data[plane] || !linesizes[plane])
797 static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
798 const uint16_t *src, int stride, int h)
801 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
803 for (yp=0; yp<h; yp++) {
804 for (xp=0; xp+2<stride; xp+=3) {
805 int x, y, z, r, g, b;
807 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
808 x = AV_RB16(src + xp + 0);
809 y = AV_RB16(src + xp + 1);
810 z = AV_RB16(src + xp + 2);
812 x = AV_RL16(src + xp + 0);
813 y = AV_RL16(src + xp + 1);
814 z = AV_RL16(src + xp + 2);
817 x = c->xyzgamma[x>>4];
818 y = c->xyzgamma[y>>4];
819 z = c->xyzgamma[z>>4];
821 // convert from XYZlinear to sRGBlinear
822 r = c->xyz2rgb_matrix[0][0] * x +
823 c->xyz2rgb_matrix[0][1] * y +
824 c->xyz2rgb_matrix[0][2] * z >> 12;
825 g = c->xyz2rgb_matrix[1][0] * x +
826 c->xyz2rgb_matrix[1][1] * y +
827 c->xyz2rgb_matrix[1][2] * z >> 12;
828 b = c->xyz2rgb_matrix[2][0] * x +
829 c->xyz2rgb_matrix[2][1] * y +
830 c->xyz2rgb_matrix[2][2] * z >> 12;
832 // limit values to 12-bit depth
833 r = av_clip_c(r,0,4095);
834 g = av_clip_c(g,0,4095);
835 b = av_clip_c(b,0,4095);
837 // convert from sRGBlinear to RGB and scale from 12bit to 16bit
838 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
839 AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
840 AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
841 AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
843 AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
844 AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
845 AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
853 static void rgb48Toxyz12(struct SwsContext *c, uint16_t *dst,
854 const uint16_t *src, int stride, int h)
857 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
859 for (yp=0; yp<h; yp++) {
860 for (xp=0; xp+2<stride; xp+=3) {
861 int x, y, z, r, g, b;
863 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
864 r = AV_RB16(src + xp + 0);
865 g = AV_RB16(src + xp + 1);
866 b = AV_RB16(src + xp + 2);
868 r = AV_RL16(src + xp + 0);
869 g = AV_RL16(src + xp + 1);
870 b = AV_RL16(src + xp + 2);
873 r = c->rgbgammainv[r>>4];
874 g = c->rgbgammainv[g>>4];
875 b = c->rgbgammainv[b>>4];
877 // convert from sRGBlinear to XYZlinear
878 x = c->rgb2xyz_matrix[0][0] * r +
879 c->rgb2xyz_matrix[0][1] * g +
880 c->rgb2xyz_matrix[0][2] * b >> 12;
881 y = c->rgb2xyz_matrix[1][0] * r +
882 c->rgb2xyz_matrix[1][1] * g +
883 c->rgb2xyz_matrix[1][2] * b >> 12;
884 z = c->rgb2xyz_matrix[2][0] * r +
885 c->rgb2xyz_matrix[2][1] * g +
886 c->rgb2xyz_matrix[2][2] * b >> 12;
888 // limit values to 12-bit depth
889 x = av_clip_c(x,0,4095);
890 y = av_clip_c(y,0,4095);
891 z = av_clip_c(z,0,4095);
893 // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
894 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
895 AV_WB16(dst + xp + 0, c->xyzgammainv[x] << 4);
896 AV_WB16(dst + xp + 1, c->xyzgammainv[y] << 4);
897 AV_WB16(dst + xp + 2, c->xyzgammainv[z] << 4);
899 AV_WL16(dst + xp + 0, c->xyzgammainv[x] << 4);
900 AV_WL16(dst + xp + 1, c->xyzgammainv[y] << 4);
901 AV_WL16(dst + xp + 2, c->xyzgammainv[z] << 4);
910 * swscale wrapper, so we don't need to export the SwsContext.
911 * Assumes planar YUV to be in YUV order instead of YVU.
913 int attribute_align_arg sws_scale(struct SwsContext *c,
914 const uint8_t * const srcSlice[],
915 const int srcStride[], int srcSliceY,
916 int srcSliceH, uint8_t *const dst[],
917 const int dstStride[])
920 const uint8_t *src2[4];
922 uint8_t *rgb0_tmp = NULL;
924 if (!srcSlice || !dstStride || !dst || !srcSlice) {
925 av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
928 memcpy(src2, srcSlice, sizeof(src2));
929 memcpy(dst2, dst, sizeof(dst2));
931 // do not mess up sliceDir if we have a "trailing" 0-size slice
935 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
936 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
939 if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
940 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
944 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
945 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
948 if (c->sliceDir == 0) {
949 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
952 if (usePal(c->srcFormat)) {
953 for (i = 0; i < 256; i++) {
954 int p, r, g, b, y, u, v, a = 0xff;
955 if (c->srcFormat == AV_PIX_FMT_PAL8) {
956 p = ((const uint32_t *)(srcSlice[1]))[i];
957 a = (p >> 24) & 0xFF;
958 r = (p >> 16) & 0xFF;
961 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
963 g = ((i >> 2) & 7) * 36;
965 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
967 g = ((i >> 3) & 7) * 36;
969 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
970 r = ( i >> 3 ) * 255;
971 g = ((i >> 1) & 3) * 85;
973 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
976 av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
977 b = ( i >> 3 ) * 255;
978 g = ((i >> 1) & 3) * 85;
981 #define RGB2YUV_SHIFT 15
982 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
983 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
984 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
985 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
986 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
987 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
988 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
989 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
990 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
992 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
993 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
994 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
995 c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
997 switch (c->dstFormat) {
998 case AV_PIX_FMT_BGR32:
1000 case AV_PIX_FMT_RGB24:
1002 c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
1004 case AV_PIX_FMT_BGR32_1:
1006 case AV_PIX_FMT_BGR24:
1008 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
1010 case AV_PIX_FMT_RGB32_1:
1012 case AV_PIX_FMT_RGB24:
1014 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
1016 case AV_PIX_FMT_RGB32:
1018 case AV_PIX_FMT_BGR24:
1021 c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
1026 if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
1029 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1031 return AVERROR(ENOMEM);
1033 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1034 for (y=0; y<srcSliceH; y++){
1035 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
1036 for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
1037 base[ srcStride[0]*y + x] = 0xFF;
1043 if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1045 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
1047 return AVERROR(ENOMEM);
1049 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
1051 xyz12Torgb48(c, (uint16_t*)base, (const uint16_t*)src2[0], srcStride[0]/2, srcSliceH);
1055 if (!srcSliceY && (c->flags & SWS_BITEXACT) && c->dither == SWS_DITHER_ED && c->dither_error[0])
1056 for (i = 0; i < 4; i++)
1057 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
1060 // copy strides, so they can safely be modified
1061 if (c->sliceDir == 1) {
1062 // slices go from top to bottom
1063 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1065 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1068 reset_ptr(src2, c->srcFormat);
1069 reset_ptr((void*)dst2, c->dstFormat);
1071 /* reset slice direction at end of frame */
1072 if (srcSliceY + srcSliceH == c->srcH)
1075 ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1078 // slices go from bottom to top => we flip the image internally
1079 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1081 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1084 src2[0] += (srcSliceH - 1) * srcStride[0];
1085 if (!usePal(c->srcFormat))
1086 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1087 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1088 src2[3] += (srcSliceH - 1) * srcStride[3];
1089 dst2[0] += ( c->dstH - 1) * dstStride[0];
1090 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1091 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1092 dst2[3] += ( c->dstH - 1) * dstStride[3];
1094 reset_ptr(src2, c->srcFormat);
1095 reset_ptr((void*)dst2, c->dstFormat);
1097 /* reset slice direction at end of frame */
1101 ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1102 srcSliceH, dst2, dstStride2);
1106 if (c->dstXYZ && !(c->srcXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
1107 /* replace on the same data */
1108 rgb48Toxyz12(c, (uint16_t*)dst2[0], (const uint16_t*)dst2[0], dstStride[0]/2, ret);