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
27 #include "libavutil/avassert.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/bswap.h"
30 #include "libavutil/cpu.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, dither_8x8_128)[8][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, },
50 DECLARE_ALIGNED(8, const uint8_t, ff_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;
261 if (!c->hyscale_fast) {
262 c->hyScale(c, dst, dstWidth, src, hLumFilter,
263 hLumFilterPos, hLumFilterSize);
264 } else { // fast bilinear upscale / crap downscale
265 c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
269 convertRange(dst, dstWidth);
272 static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
273 int dstWidth, const uint8_t *src1,
274 const uint8_t *src2, int srcW, int xInc)
277 unsigned int xpos = 0;
278 for (i = 0; i < dstWidth; i++) {
279 register unsigned int xx = xpos >> 16;
280 register unsigned int xalpha = (xpos & 0xFFFF) >> 9;
281 dst1[i] = (src1[xx] * (xalpha ^ 127) + src1[xx + 1] * xalpha);
282 dst2[i] = (src2[xx] * (xalpha ^ 127) + src2[xx + 1] * xalpha);
285 for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
286 dst1[i] = src1[srcW-1]*128;
287 dst2[i] = src2[srcW-1]*128;
291 static av_always_inline void hcscale(SwsContext *c, int16_t *dst1,
292 int16_t *dst2, int dstWidth,
293 const uint8_t *src_in[4],
295 const int16_t *hChrFilter,
296 const int32_t *hChrFilterPos,
298 uint8_t *formatConvBuffer, uint32_t *pal)
300 const uint8_t *src1 = src_in[1], *src2 = src_in[2];
302 uint8_t *buf2 = formatConvBuffer +
303 FFALIGN(srcW*2+78, 16);
304 c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
305 src1= formatConvBuffer;
307 } else if (c->readChrPlanar) {
308 uint8_t *buf2 = formatConvBuffer +
309 FFALIGN(srcW*2+78, 16);
310 c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
311 src1 = formatConvBuffer;
315 if (!c->hcscale_fast) {
316 c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
317 c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
318 } else { // fast bilinear upscale / crap downscale
319 c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
322 if (c->chrConvertRange)
323 c->chrConvertRange(dst1, dst2, dstWidth);
326 #define DEBUG_SWSCALE_BUFFERS 0
327 #define DEBUG_BUFFERS(...) \
328 if (DEBUG_SWSCALE_BUFFERS) \
329 av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
331 static int swScale(SwsContext *c, const uint8_t *src[],
332 int srcStride[], int srcSliceY,
333 int srcSliceH, uint8_t *dst[], int dstStride[])
335 /* load a few things into local vars to make the code more readable?
337 const int srcW = c->srcW;
338 const int dstW = c->dstW;
339 const int dstH = c->dstH;
340 const int chrDstW = c->chrDstW;
341 const int chrSrcW = c->chrSrcW;
342 const int lumXInc = c->lumXInc;
343 const int chrXInc = c->chrXInc;
344 const enum AVPixelFormat dstFormat = c->dstFormat;
345 const int flags = c->flags;
346 int32_t *vLumFilterPos = c->vLumFilterPos;
347 int32_t *vChrFilterPos = c->vChrFilterPos;
348 int32_t *hLumFilterPos = c->hLumFilterPos;
349 int32_t *hChrFilterPos = c->hChrFilterPos;
350 int16_t *hLumFilter = c->hLumFilter;
351 int16_t *hChrFilter = c->hChrFilter;
352 int32_t *lumMmxFilter = c->lumMmxFilter;
353 int32_t *chrMmxFilter = c->chrMmxFilter;
354 const int vLumFilterSize = c->vLumFilterSize;
355 const int vChrFilterSize = c->vChrFilterSize;
356 const int hLumFilterSize = c->hLumFilterSize;
357 const int hChrFilterSize = c->hChrFilterSize;
358 int16_t **lumPixBuf = c->lumPixBuf;
359 int16_t **chrUPixBuf = c->chrUPixBuf;
360 int16_t **chrVPixBuf = c->chrVPixBuf;
361 int16_t **alpPixBuf = c->alpPixBuf;
362 const int vLumBufSize = c->vLumBufSize;
363 const int vChrBufSize = c->vChrBufSize;
364 uint8_t *formatConvBuffer = c->formatConvBuffer;
365 uint32_t *pal = c->pal_yuv;
366 yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
367 yuv2planarX_fn yuv2planeX = c->yuv2planeX;
368 yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
369 yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
370 yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
371 yuv2packedX_fn yuv2packedX = c->yuv2packedX;
372 yuv2anyX_fn yuv2anyX = c->yuv2anyX;
373 const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
374 const int chrSrcSliceH = -((-srcSliceH) >> c->chrSrcVSubSample);
375 int should_dither = is9_OR_10BPS(c->srcFormat) ||
376 is16BPS(c->srcFormat);
379 /* vars which will change and which we need to store back in the context */
381 int lumBufIndex = c->lumBufIndex;
382 int chrBufIndex = c->chrBufIndex;
383 int lastInLumBuf = c->lastInLumBuf;
384 int lastInChrBuf = c->lastInChrBuf;
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]%16 !=0 || dstStride[1]%16 !=0 ||
414 dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
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]%16 || (uintptr_t)dst[1]%16 || (uintptr_t)dst[2]%16
425 || (uintptr_t)src[0]%16 || (uintptr_t)src[1]%16 || (uintptr_t)src[2]%16
426 || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
427 || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
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 = ff_sws_pb_64;
453 for (; dstY < dstH; dstY++) {
454 const int chrDstY = dstY >> c->chrDstVSubSample;
456 dst[0] + dstStride[0] * dstY,
457 dst[1] + dstStride[1] * chrDstY,
458 dst[2] + dstStride[2] * chrDstY,
459 (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
461 int use_mmx_vfilter= c->use_mmx_vfilter;
463 // First line needed as input
464 const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
465 const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), dstH - 1)]);
466 // First line needed as input
467 const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
469 // Last line needed as input
470 int lastLumSrcY = FFMIN(c->srcH, firstLumSrcY + vLumFilterSize) - 1;
471 int lastLumSrcY2 = FFMIN(c->srcH, firstLumSrcY2 + vLumFilterSize) - 1;
472 int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
475 // handle holes (FAST_BILINEAR & weird filters)
476 if (firstLumSrcY > lastInLumBuf)
477 lastInLumBuf = firstLumSrcY - 1;
478 if (firstChrSrcY > lastInChrBuf)
479 lastInChrBuf = firstChrSrcY - 1;
480 av_assert0(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
481 av_assert0(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
483 DEBUG_BUFFERS("dstY: %d\n", dstY);
484 DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
485 firstLumSrcY, lastLumSrcY, lastInLumBuf);
486 DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
487 firstChrSrcY, lastChrSrcY, lastInChrBuf);
489 // Do we have enough lines in this slice to output the dstY line
490 enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
491 lastChrSrcY < -((-srcSliceY - srcSliceH) >> c->chrSrcVSubSample);
494 lastLumSrcY = srcSliceY + srcSliceH - 1;
495 lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
496 DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
497 lastLumSrcY, lastChrSrcY);
500 // Do horizontal scaling
501 while (lastInLumBuf < lastLumSrcY) {
502 const uint8_t *src1[4] = {
503 src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
504 src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
505 src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
506 src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
509 av_assert0(lumBufIndex < 2 * vLumBufSize);
510 av_assert0(lastInLumBuf + 1 - srcSliceY < srcSliceH);
511 av_assert0(lastInLumBuf + 1 - srcSliceY >= 0);
512 hyscale(c, lumPixBuf[lumBufIndex], dstW, src1, srcW, lumXInc,
513 hLumFilter, hLumFilterPos, hLumFilterSize,
514 formatConvBuffer, pal, 0);
515 if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
516 hyscale(c, alpPixBuf[lumBufIndex], dstW, src1, srcW,
517 lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
518 formatConvBuffer, pal, 1);
520 DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
521 lumBufIndex, lastInLumBuf);
523 while (lastInChrBuf < lastChrSrcY) {
524 const uint8_t *src1[4] = {
525 src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
526 src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
527 src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
528 src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
531 av_assert0(chrBufIndex < 2 * vChrBufSize);
532 av_assert0(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
533 av_assert0(lastInChrBuf + 1 - chrSrcSliceY >= 0);
534 // FIXME replace parameters through context struct (some at least)
536 if (c->needs_hcscale)
537 hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
538 chrDstW, src1, chrSrcW, chrXInc,
539 hChrFilter, hChrFilterPos, hChrFilterSize,
540 formatConvBuffer, pal);
542 DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
543 chrBufIndex, lastInChrBuf);
545 // wrap buf index around to stay inside the ring buffer
546 if (lumBufIndex >= vLumBufSize)
547 lumBufIndex -= vLumBufSize;
548 if (chrBufIndex >= vChrBufSize)
549 chrBufIndex -= vChrBufSize;
551 break; // we can't output a dstY line so let's try with the next slice
554 updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex,
555 lastInLumBuf, lastInChrBuf);
558 c->chrDither8 = dither_8x8_128[chrDstY & 7];
559 c->lumDither8 = dither_8x8_128[dstY & 7];
561 if (dstY >= dstH - 2) {
562 /* hmm looks like we can't use MMX here without overwriting
563 * this array's tail */
564 ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
565 &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
570 const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
571 const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
572 const int16_t **chrVSrcPtr = (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
573 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && alpPixBuf) ?
574 (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
575 int16_t *vLumFilter = c->vLumFilter;
576 int16_t *vChrFilter = c->vChrFilter;
578 if (isPlanarYUV(dstFormat) ||
579 (isGray(dstFormat) && !isALPHA(dstFormat))) { // YV12 like
580 const int chrSkipMask = (1 << c->chrDstVSubSample) - 1;
582 vLumFilter += dstY * vLumFilterSize;
583 vChrFilter += chrDstY * vChrFilterSize;
585 // av_assert0(use_mmx_vfilter != (
586 // yuv2planeX == yuv2planeX_10BE_c
587 // || yuv2planeX == yuv2planeX_10LE_c
588 // || yuv2planeX == yuv2planeX_9BE_c
589 // || yuv2planeX == yuv2planeX_9LE_c
590 // || yuv2planeX == yuv2planeX_16BE_c
591 // || yuv2planeX == yuv2planeX_16LE_c
592 // || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
595 vLumFilter= (int16_t *)c->lumMmxFilter;
596 vChrFilter= (int16_t *)c->chrMmxFilter;
599 if (vLumFilterSize == 1) {
600 yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
602 yuv2planeX(vLumFilter, vLumFilterSize,
604 dstW, c->lumDither8, 0);
607 if (!((dstY & chrSkipMask) || isGray(dstFormat))) {
609 yuv2nv12cX(c, vChrFilter,
610 vChrFilterSize, chrUSrcPtr, chrVSrcPtr,
612 } else if (vChrFilterSize == 1) {
613 yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
614 yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
616 yuv2planeX(vChrFilter,
617 vChrFilterSize, chrUSrcPtr, dest[1],
618 chrDstW, c->chrDither8, 0);
619 yuv2planeX(vChrFilter,
620 vChrFilterSize, chrVSrcPtr, dest[2],
621 chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
625 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
627 vLumFilter= (int16_t *)c->alpMmxFilter;
629 if (vLumFilterSize == 1) {
630 yuv2plane1(alpSrcPtr[0], dest[3], dstW,
633 yuv2planeX(vLumFilter,
634 vLumFilterSize, alpSrcPtr, dest[3],
635 dstW, c->lumDither8, 0);
638 } else if (yuv2packedX) {
639 av_assert1(lumSrcPtr + vLumFilterSize - 1 < (const int16_t **)lumPixBuf + vLumBufSize * 2);
640 av_assert1(chrUSrcPtr + vChrFilterSize - 1 < (const int16_t **)chrUPixBuf + vChrBufSize * 2);
641 if (c->yuv2packed1 && vLumFilterSize == 1 &&
642 vChrFilterSize <= 2) { // unscaled RGB
643 int chrAlpha = vChrFilterSize == 1 ? 0 : vChrFilter[2 * dstY + 1];
644 yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
645 alpPixBuf ? *alpSrcPtr : NULL,
646 dest[0], dstW, chrAlpha, dstY);
647 } else if (c->yuv2packed2 && vLumFilterSize == 2 &&
648 vChrFilterSize == 2) { // bilinear upscale RGB
649 int lumAlpha = vLumFilter[2 * dstY + 1];
650 int chrAlpha = vChrFilter[2 * dstY + 1];
652 lumMmxFilter[3] = vLumFilter[2 * dstY] * 0x10001;
654 chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
655 yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
656 alpPixBuf ? alpSrcPtr : NULL,
657 dest[0], dstW, lumAlpha, chrAlpha, dstY);
658 } else { // general RGB
659 yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
660 lumSrcPtr, vLumFilterSize,
661 vChrFilter + dstY * vChrFilterSize,
662 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
663 alpSrcPtr, dest[0], dstW, dstY);
666 av_assert1(!yuv2packed1 && !yuv2packed2);
667 yuv2anyX(c, vLumFilter + dstY * vLumFilterSize,
668 lumSrcPtr, vLumFilterSize,
669 vChrFilter + dstY * vChrFilterSize,
670 chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
671 alpSrcPtr, dest, dstW, dstY);
675 if (isPlanar(dstFormat) && isALPHA(dstFormat) && !alpPixBuf) {
677 int height = dstY - lastDstY;
679 if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
680 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
681 fillPlane16(dst[3], dstStride[3], length, height, lastDstY,
682 1, desc->comp[3].depth_minus1,
685 fillPlane(dst[3], dstStride[3], length, height, lastDstY, 255);
688 #if HAVE_MMXEXT_INLINE
689 if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
690 __asm__ volatile ("sfence" ::: "memory");
694 /* store changed local vars back in the context */
696 c->lumBufIndex = lumBufIndex;
697 c->chrBufIndex = chrBufIndex;
698 c->lastInLumBuf = lastInLumBuf;
699 c->lastInChrBuf = lastInChrBuf;
701 return dstY - lastDstY;
704 static av_cold void sws_init_swScale_c(SwsContext *c)
706 enum AVPixelFormat srcFormat = c->srcFormat;
708 ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
709 &c->yuv2nv12cX, &c->yuv2packed1,
710 &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
712 ff_sws_init_input_funcs(c);
715 if (c->srcBpc == 8) {
716 if (c->dstBpc <= 14) {
717 c->hyScale = c->hcScale = hScale8To15_c;
718 if (c->flags & SWS_FAST_BILINEAR) {
719 c->hyscale_fast = hyscale_fast_c;
720 c->hcscale_fast = hcscale_fast_c;
723 c->hyScale = c->hcScale = hScale8To19_c;
726 c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
730 if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
731 if (c->dstBpc <= 14) {
733 c->lumConvertRange = lumRangeFromJpeg_c;
734 c->chrConvertRange = chrRangeFromJpeg_c;
736 c->lumConvertRange = lumRangeToJpeg_c;
737 c->chrConvertRange = chrRangeToJpeg_c;
741 c->lumConvertRange = lumRangeFromJpeg16_c;
742 c->chrConvertRange = chrRangeFromJpeg16_c;
744 c->lumConvertRange = lumRangeToJpeg16_c;
745 c->chrConvertRange = chrRangeToJpeg16_c;
750 if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
751 srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
752 c->needs_hcscale = 1;
755 SwsFunc ff_getSwsFunc(SwsContext *c)
757 sws_init_swScale_c(c);
760 ff_sws_init_swScale_mmx(c);
762 ff_sws_init_swScale_altivec(c);
767 static void reset_ptr(const uint8_t *src[], int format)
769 if (!isALPHA(format))
771 if (!isPlanar(format)) {
772 src[3] = src[2] = NULL;
779 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
780 const int linesizes[4])
782 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
785 for (i = 0; i < 4; i++) {
786 int plane = desc->comp[i].plane;
787 if (!data[plane] || !linesizes[plane])
794 static void xyz12Torgb48(struct SwsContext *c, uint16_t *dst,
795 const uint16_t *src, int stride, int h)
798 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
800 for (yp=0; yp<h; yp++) {
801 for (xp=0; xp+2<stride; xp+=3) {
802 int x, y, z, r, g, b;
804 if (desc->flags & PIX_FMT_BE) {
805 x = AV_RB16(src + xp + 0);
806 y = AV_RB16(src + xp + 1);
807 z = AV_RB16(src + xp + 2);
809 x = AV_RL16(src + xp + 0);
810 y = AV_RL16(src + xp + 1);
811 z = AV_RL16(src + xp + 2);
814 x = c->xyzgamma[x>>4];
815 y = c->xyzgamma[y>>4];
816 z = c->xyzgamma[z>>4];
818 // convert from XYZlinear to sRGBlinear
819 r = c->xyz2rgb_matrix[0][0] * x +
820 c->xyz2rgb_matrix[0][1] * y +
821 c->xyz2rgb_matrix[0][2] * z >> 12;
822 g = c->xyz2rgb_matrix[1][0] * x +
823 c->xyz2rgb_matrix[1][1] * y +
824 c->xyz2rgb_matrix[1][2] * z >> 12;
825 b = c->xyz2rgb_matrix[2][0] * x +
826 c->xyz2rgb_matrix[2][1] * y +
827 c->xyz2rgb_matrix[2][2] * z >> 12;
829 // limit values to 12-bit depth
830 r = av_clip_c(r,0,4095);
831 g = av_clip_c(g,0,4095);
832 b = av_clip_c(b,0,4095);
834 // convert from sRGBlinear to RGB and scale from 12bit to 16bit
835 if (desc->flags & PIX_FMT_BE) {
836 AV_WB16(dst + xp + 0, c->rgbgamma[r] << 4);
837 AV_WB16(dst + xp + 1, c->rgbgamma[g] << 4);
838 AV_WB16(dst + xp + 2, c->rgbgamma[b] << 4);
840 AV_WL16(dst + xp + 0, c->rgbgamma[r] << 4);
841 AV_WL16(dst + xp + 1, c->rgbgamma[g] << 4);
842 AV_WL16(dst + xp + 2, c->rgbgamma[b] << 4);
851 * swscale wrapper, so we don't need to export the SwsContext.
852 * Assumes planar YUV to be in YUV order instead of YVU.
854 int attribute_align_arg sws_scale(struct SwsContext *c,
855 const uint8_t * const srcSlice[],
856 const int srcStride[], int srcSliceY,
857 int srcSliceH, uint8_t *const dst[],
858 const int dstStride[])
861 const uint8_t *src2[4];
863 uint8_t *rgb0_tmp = NULL;
865 if (!srcSlice || !dstStride || !dst || !srcSlice) {
866 av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
869 memcpy(src2, srcSlice, sizeof(src2));
870 memcpy(dst2, dst, sizeof(dst2));
872 // do not mess up sliceDir if we have a "trailing" 0-size slice
876 if (!check_image_pointers(srcSlice, c->srcFormat, srcStride)) {
877 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
880 if (!check_image_pointers((const uint8_t* const*)dst, c->dstFormat, dstStride)) {
881 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
885 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
886 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
889 if (c->sliceDir == 0) {
890 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
893 if (usePal(c->srcFormat)) {
894 for (i = 0; i < 256; i++) {
895 int p, r, g, b, y, u, v, a = 0xff;
896 if (c->srcFormat == AV_PIX_FMT_PAL8) {
897 p = ((const uint32_t *)(srcSlice[1]))[i];
898 a = (p >> 24) & 0xFF;
899 r = (p >> 16) & 0xFF;
902 } else if (c->srcFormat == AV_PIX_FMT_RGB8) {
904 g = ((i >> 2) & 7) * 36;
906 } else if (c->srcFormat == AV_PIX_FMT_BGR8) {
908 g = ((i >> 3) & 7) * 36;
910 } else if (c->srcFormat == AV_PIX_FMT_RGB4_BYTE) {
911 r = ( i >> 3 ) * 255;
912 g = ((i >> 1) & 3) * 85;
914 } else if (c->srcFormat == AV_PIX_FMT_GRAY8 || c->srcFormat == AV_PIX_FMT_GRAY8A) {
917 av_assert1(c->srcFormat == AV_PIX_FMT_BGR4_BYTE);
918 b = ( i >> 3 ) * 255;
919 g = ((i >> 1) & 3) * 85;
922 #define RGB2YUV_SHIFT 15
923 #define BY ( (int) (0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
924 #define BV (-(int) (0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
925 #define BU ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
926 #define GY ( (int) (0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
927 #define GV (-(int) (0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
928 #define GU (-(int) (0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
929 #define RY ( (int) (0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
930 #define RV ( (int) (0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
931 #define RU (-(int) (0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
933 y = av_clip_uint8((RY * r + GY * g + BY * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
934 u = av_clip_uint8((RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
935 v = av_clip_uint8((RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
936 c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
938 switch (c->dstFormat) {
939 case AV_PIX_FMT_BGR32:
941 case AV_PIX_FMT_RGB24:
943 c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
945 case AV_PIX_FMT_BGR32_1:
947 case AV_PIX_FMT_BGR24:
949 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
951 case AV_PIX_FMT_RGB32_1:
953 case AV_PIX_FMT_RGB24:
955 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
957 case AV_PIX_FMT_RGB32:
959 case AV_PIX_FMT_BGR24:
962 c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
967 if (c->src0Alpha && !c->dst0Alpha && isALPHA(c->dstFormat)) {
970 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
972 return AVERROR(ENOMEM);
974 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
975 for (y=0; y<srcSliceH; y++){
976 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*c->srcW);
977 for (x=c->src0Alpha-1; x<4*c->srcW; x+=4) {
978 base[ srcStride[0]*y + x] = 0xFF;
984 if (c->srcXYZ && !(c->dstXYZ && c->srcW==c->dstW && c->srcH==c->dstH)) {
986 rgb0_tmp = av_malloc(FFABS(srcStride[0]) * srcSliceH + 32);
988 return AVERROR(ENOMEM);
990 base = srcStride[0] < 0 ? rgb0_tmp - srcStride[0] * (srcSliceH-1) : rgb0_tmp;
992 xyz12Torgb48(c, base, src2[0], srcStride[0]/2, srcSliceH);
996 if (!srcSliceY && (c->flags & SWS_BITEXACT) && (c->flags & SWS_ERROR_DIFFUSION) && c->dither_error[0])
997 for (i = 0; i < 4; i++)
998 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (c->dstW+2));
1001 // copy strides, so they can safely be modified
1002 if (c->sliceDir == 1) {
1003 // slices go from top to bottom
1004 int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],
1006 int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],
1009 reset_ptr(src2, c->srcFormat);
1010 reset_ptr((void*)dst2, c->dstFormat);
1012 /* reset slice direction at end of frame */
1013 if (srcSliceY + srcSliceH == c->srcH)
1016 ret = c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,
1019 // slices go from bottom to top => we flip the image internally
1020 int srcStride2[4] = { -srcStride[0], -srcStride[1], -srcStride[2],
1022 int dstStride2[4] = { -dstStride[0], -dstStride[1], -dstStride[2],
1025 src2[0] += (srcSliceH - 1) * srcStride[0];
1026 if (!usePal(c->srcFormat))
1027 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1028 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1029 src2[3] += (srcSliceH - 1) * srcStride[3];
1030 dst2[0] += ( c->dstH - 1) * dstStride[0];
1031 dst2[1] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[1];
1032 dst2[2] += ((c->dstH >> c->chrDstVSubSample) - 1) * dstStride[2];
1033 dst2[3] += ( c->dstH - 1) * dstStride[3];
1035 reset_ptr(src2, c->srcFormat);
1036 reset_ptr((void*)dst2, c->dstFormat);
1038 /* reset slice direction at end of frame */
1042 ret = c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH,
1043 srcSliceH, dst2, dstStride2);