2 * Copyright (C) 2001-2003 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
23 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
24 #define _DARWIN_C_SOURCE // needed for MAP_ANON
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 #define MAP_ANONYMOUS MAP_ANON
37 #define WIN32_LEAN_AND_MEAN
41 #include "libavutil/avassert.h"
42 #include "libavutil/avutil.h"
43 #include "libavutil/bswap.h"
44 #include "libavutil/cpu.h"
45 #include "libavutil/intreadwrite.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/x86_cpu.h"
52 #include "swscale_internal.h"
54 unsigned swscale_version(void)
56 av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
57 return LIBSWSCALE_VERSION_INT;
60 const char *swscale_configuration(void)
62 return FFMPEG_CONFIGURATION;
65 const char *swscale_license(void)
67 #define LICENSE_PREFIX "libswscale license: "
68 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
71 #define RET 0xC3 // near return opcode for x86
73 typedef struct FormatEntry {
74 int is_supported_in, is_supported_out;
77 static const FormatEntry format_entries[PIX_FMT_NB] = {
78 [PIX_FMT_YUV420P] = { 1, 1 },
79 [PIX_FMT_YUYV422] = { 1, 1 },
80 [PIX_FMT_RGB24] = { 1, 1 },
81 [PIX_FMT_BGR24] = { 1, 1 },
82 [PIX_FMT_YUV422P] = { 1, 1 },
83 [PIX_FMT_YUV444P] = { 1, 1 },
84 [PIX_FMT_YUV410P] = { 1, 1 },
85 [PIX_FMT_YUV411P] = { 1, 1 },
86 [PIX_FMT_GRAY8] = { 1, 1 },
87 [PIX_FMT_MONOWHITE] = { 1, 1 },
88 [PIX_FMT_MONOBLACK] = { 1, 1 },
89 [PIX_FMT_PAL8] = { 1, 0 },
90 [PIX_FMT_YUVJ420P] = { 1, 1 },
91 [PIX_FMT_YUVJ422P] = { 1, 1 },
92 [PIX_FMT_YUVJ444P] = { 1, 1 },
93 [PIX_FMT_UYVY422] = { 1, 1 },
94 [PIX_FMT_UYYVYY411] = { 0, 0 },
95 [PIX_FMT_BGR8] = { 1, 1 },
96 [PIX_FMT_BGR4] = { 0, 1 },
97 [PIX_FMT_BGR4_BYTE] = { 1, 1 },
98 [PIX_FMT_RGB8] = { 1, 1 },
99 [PIX_FMT_RGB4] = { 0, 1 },
100 [PIX_FMT_RGB4_BYTE] = { 1, 1 },
101 [PIX_FMT_NV12] = { 1, 1 },
102 [PIX_FMT_NV21] = { 1, 1 },
103 [PIX_FMT_ARGB] = { 1, 1 },
104 [PIX_FMT_RGBA] = { 1, 1 },
105 [PIX_FMT_ABGR] = { 1, 1 },
106 [PIX_FMT_BGRA] = { 1, 1 },
107 [PIX_FMT_0RGB] = { 1, 1 },
108 [PIX_FMT_RGB0] = { 1, 1 },
109 [PIX_FMT_0BGR] = { 1, 1 },
110 [PIX_FMT_BGR0] = { 1, 1 },
111 [PIX_FMT_GRAY16BE] = { 1, 1 },
112 [PIX_FMT_GRAY16LE] = { 1, 1 },
113 [PIX_FMT_YUV440P] = { 1, 1 },
114 [PIX_FMT_YUVJ440P] = { 1, 1 },
115 [PIX_FMT_YUVA420P] = { 1, 1 },
116 [PIX_FMT_YUVA444P] = { 1, 1 },
117 [PIX_FMT_RGB48BE] = { 1, 1 },
118 [PIX_FMT_RGB48LE] = { 1, 1 },
119 [PIX_FMT_RGBA64BE] = { 1, 0 },
120 [PIX_FMT_RGBA64LE] = { 1, 0 },
121 [PIX_FMT_RGB565BE] = { 1, 1 },
122 [PIX_FMT_RGB565LE] = { 1, 1 },
123 [PIX_FMT_RGB555BE] = { 1, 1 },
124 [PIX_FMT_RGB555LE] = { 1, 1 },
125 [PIX_FMT_BGR565BE] = { 1, 1 },
126 [PIX_FMT_BGR565LE] = { 1, 1 },
127 [PIX_FMT_BGR555BE] = { 1, 1 },
128 [PIX_FMT_BGR555LE] = { 1, 1 },
129 [PIX_FMT_YUV420P16LE] = { 1, 1 },
130 [PIX_FMT_YUV420P16BE] = { 1, 1 },
131 [PIX_FMT_YUV422P16LE] = { 1, 1 },
132 [PIX_FMT_YUV422P16BE] = { 1, 1 },
133 [PIX_FMT_YUV444P16LE] = { 1, 1 },
134 [PIX_FMT_YUV444P16BE] = { 1, 1 },
135 [PIX_FMT_RGB444LE] = { 1, 1 },
136 [PIX_FMT_RGB444BE] = { 1, 1 },
137 [PIX_FMT_BGR444LE] = { 1, 1 },
138 [PIX_FMT_BGR444BE] = { 1, 1 },
139 [PIX_FMT_Y400A] = { 1, 0 },
140 [PIX_FMT_BGR48BE] = { 1, 1 },
141 [PIX_FMT_BGR48LE] = { 1, 1 },
142 [PIX_FMT_BGRA64BE] = { 0, 0 },
143 [PIX_FMT_BGRA64LE] = { 0, 0 },
144 [PIX_FMT_YUV420P9BE] = { 1, 1 },
145 [PIX_FMT_YUV420P9LE] = { 1, 1 },
146 [PIX_FMT_YUV420P10BE] = { 1, 1 },
147 [PIX_FMT_YUV420P10LE] = { 1, 1 },
148 [PIX_FMT_YUV422P9BE] = { 1, 1 },
149 [PIX_FMT_YUV422P9LE] = { 1, 1 },
150 [PIX_FMT_YUV422P10BE] = { 1, 1 },
151 [PIX_FMT_YUV422P10LE] = { 1, 1 },
152 [PIX_FMT_YUV444P9BE] = { 1, 1 },
153 [PIX_FMT_YUV444P9LE] = { 1, 1 },
154 [PIX_FMT_YUV444P10BE] = { 1, 1 },
155 [PIX_FMT_YUV444P10LE] = { 1, 1 },
156 [PIX_FMT_GBRP] = { 1, 0 },
157 [PIX_FMT_GBRP9LE] = { 1, 0 },
158 [PIX_FMT_GBRP9BE] = { 1, 0 },
159 [PIX_FMT_GBRP10LE] = { 1, 0 },
160 [PIX_FMT_GBRP10BE] = { 1, 0 },
161 [PIX_FMT_GBRP16LE] = { 1, 0 },
162 [PIX_FMT_GBRP16BE] = { 1, 0 },
165 int sws_isSupportedInput(enum PixelFormat pix_fmt)
167 return (unsigned)pix_fmt < PIX_FMT_NB ?
168 format_entries[pix_fmt].is_supported_in : 0;
171 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
173 return (unsigned)pix_fmt < PIX_FMT_NB ?
174 format_entries[pix_fmt].is_supported_out : 0;
177 extern const int32_t ff_yuv2rgb_coeffs[8][4];
179 #if FF_API_SWS_FORMAT_NAME
180 const char *sws_format_name(enum PixelFormat format)
182 return av_get_pix_fmt_name(format);
186 static double getSplineCoeff(double a, double b, double c, double d,
190 return ((d * dist + c) * dist + b) * dist + a;
192 return getSplineCoeff(0.0,
193 b + 2.0 * c + 3.0 * d,
195 -b - 3.0 * c - 6.0 * d,
199 static int initFilter(int16_t **outFilter, int32_t **filterPos,
200 int *outFilterSize, int xInc, int srcW, int dstW,
201 int filterAlign, int one, int flags, int cpu_flags,
202 SwsVector *srcFilter, SwsVector *dstFilter,
209 int64_t *filter = NULL;
210 int64_t *filter2 = NULL;
211 const int64_t fone = 1LL << 54;
214 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
216 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
217 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
219 if (FFABS(xInc - 0x10000) < 10) { // unscaled
222 FF_ALLOCZ_OR_GOTO(NULL, filter,
223 dstW * sizeof(*filter) * filterSize, fail);
225 for (i = 0; i < dstW; i++) {
226 filter[i * filterSize] = fone;
229 } else if (flags & SWS_POINT) { // lame looking point sampling mode
233 FF_ALLOC_OR_GOTO(NULL, filter,
234 dstW * sizeof(*filter) * filterSize, fail);
236 xDstInSrc = xInc / 2 - 0x8000;
237 for (i = 0; i < dstW; i++) {
238 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
240 (*filterPos)[i] = xx;
244 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
245 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
249 FF_ALLOC_OR_GOTO(NULL, filter,
250 dstW * sizeof(*filter) * filterSize, fail);
252 xDstInSrc = xInc / 2 - 0x8000;
253 for (i = 0; i < dstW; i++) {
254 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
257 (*filterPos)[i] = xx;
258 // bilinear upscale / linear interpolate / area averaging
259 for (j = 0; j < filterSize; j++) {
260 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
263 filter[i * filterSize + j] = coeff;
272 if (flags & SWS_BICUBIC)
274 else if (flags & SWS_X)
276 else if (flags & SWS_AREA)
277 sizeFactor = 1; // downscale only, for upscale it is bilinear
278 else if (flags & SWS_GAUSS)
279 sizeFactor = 8; // infinite ;)
280 else if (flags & SWS_LANCZOS)
281 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
282 else if (flags & SWS_SINC)
283 sizeFactor = 20; // infinite ;)
284 else if (flags & SWS_SPLINE)
285 sizeFactor = 20; // infinite ;)
286 else if (flags & SWS_BILINEAR)
289 sizeFactor = 0; // GCC warning killer
294 filterSize = 1 + sizeFactor; // upscale
296 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
298 filterSize = FFMIN(filterSize, srcW - 2);
299 filterSize = FFMAX(filterSize, 1);
301 FF_ALLOC_OR_GOTO(NULL, filter,
302 dstW * sizeof(*filter) * filterSize, fail);
304 xDstInSrc = xInc - 0x10000;
305 for (i = 0; i < dstW; i++) {
306 int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
308 (*filterPos)[i] = xx;
309 for (j = 0; j < filterSize; j++) {
310 int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
316 floatd = d * (1.0 / (1 << 30));
318 if (flags & SWS_BICUBIC) {
319 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
320 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
322 if (d >= 1LL << 31) {
325 int64_t dd = (d * d) >> 30;
326 int64_t ddd = (dd * d) >> 30;
329 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
330 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
331 (6 * (1 << 24) - 2 * B) * (1 << 30);
333 coeff = (-B - 6 * C) * ddd +
334 (6 * B + 30 * C) * dd +
335 (-12 * B - 48 * C) * d +
336 (8 * B + 24 * C) * (1 << 30);
338 coeff *= fone >> (30 + 24);
341 else if (flags & SWS_X) {
342 double p = param ? param * 0.01 : 0.3;
343 coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
344 coeff *= pow(2.0, -p * d * d);
347 else if (flags & SWS_X) {
348 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
352 c = cos(floatd * M_PI);
359 coeff = (c * 0.5 + 0.5) * fone;
360 } else if (flags & SWS_AREA) {
361 int64_t d2 = d - (1 << 29);
362 if (d2 * xInc < -(1LL << (29 + 16)))
363 coeff = 1.0 * (1LL << (30 + 16));
364 else if (d2 * xInc < (1LL << (29 + 16)))
365 coeff = -d2 * xInc + (1LL << (29 + 16));
368 coeff *= fone >> (30 + 16);
369 } else if (flags & SWS_GAUSS) {
370 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
371 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
372 } else if (flags & SWS_SINC) {
373 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
374 } else if (flags & SWS_LANCZOS) {
375 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
376 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
377 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
380 } else if (flags & SWS_BILINEAR) {
381 coeff = (1 << 30) - d;
385 } else if (flags & SWS_SPLINE) {
386 double p = -2.196152422706632;
387 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
389 coeff = 0.0; // GCC warning killer
393 filter[i * filterSize + j] = coeff;
396 xDstInSrc += 2 * xInc;
400 /* apply src & dst Filter to filter -> filter2
403 assert(filterSize > 0);
404 filter2Size = filterSize;
406 filter2Size += srcFilter->length - 1;
408 filter2Size += dstFilter->length - 1;
409 assert(filter2Size > 0);
410 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
412 for (i = 0; i < dstW; i++) {
416 for (k = 0; k < srcFilter->length; k++) {
417 for (j = 0; j < filterSize; j++)
418 filter2[i * filter2Size + k + j] +=
419 srcFilter->coeff[k] * filter[i * filterSize + j];
422 for (j = 0; j < filterSize; j++)
423 filter2[i * filter2Size + j] = filter[i * filterSize + j];
427 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
431 /* try to reduce the filter-size (step1 find size and shift left) */
432 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
434 for (i = dstW - 1; i >= 0; i--) {
435 int min = filter2Size;
437 int64_t cutOff = 0.0;
439 /* get rid of near zero elements on the left by shifting left */
440 for (j = 0; j < filter2Size; j++) {
442 cutOff += FFABS(filter2[i * filter2Size]);
444 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
447 /* preserve monotonicity because the core can't handle the
448 * filter otherwise */
449 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
452 // move filter coefficients left
453 for (k = 1; k < filter2Size; k++)
454 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
455 filter2[i * filter2Size + k - 1] = 0;
460 /* count near zeros on the right */
461 for (j = filter2Size - 1; j > 0; j--) {
462 cutOff += FFABS(filter2[i * filter2Size + j]);
464 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
469 if (min > minFilterSize)
473 if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
474 // we can handle the special case 4, so we don't want to go the full 8
475 if (minFilterSize < 5)
478 /* We really don't want to waste our time doing useless computation, so
479 * fall back on the scalar C code for very small filters.
480 * Vectorizing is worth it only if you have a decent-sized vector. */
481 if (minFilterSize < 3)
485 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
486 // special case for unscaled vertical filtering
487 if (minFilterSize == 1 && filterAlign == 2)
491 assert(minFilterSize > 0);
492 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
493 assert(filterSize > 0);
494 filter = av_malloc(filterSize * dstW * sizeof(*filter));
495 if (filterSize >= MAX_FILTER_SIZE * 16 /
496 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
498 *outFilterSize = filterSize;
500 if (flags & SWS_PRINT_INFO)
501 av_log(NULL, AV_LOG_VERBOSE,
502 "SwScaler: reducing / aligning filtersize %d -> %d\n",
503 filter2Size, filterSize);
504 /* try to reduce the filter-size (step2 reduce it) */
505 for (i = 0; i < dstW; i++) {
508 for (j = 0; j < filterSize; j++) {
509 if (j >= filter2Size)
510 filter[i * filterSize + j] = 0;
512 filter[i * filterSize + j] = filter2[i * filter2Size + j];
513 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
514 filter[i * filterSize + j] = 0;
518 // FIXME try to align filterPos if possible
521 for (i = 0; i < dstW; i++) {
523 if ((*filterPos)[i] < 0) {
524 // move filter coefficients left to compensate for filterPos
525 for (j = 1; j < filterSize; j++) {
526 int left = FFMAX(j + (*filterPos)[i], 0);
527 filter[i * filterSize + left] += filter[i * filterSize + j];
528 filter[i * filterSize + j] = 0;
533 if ((*filterPos)[i] + filterSize > srcW) {
534 int shift = (*filterPos)[i] + filterSize - srcW;
535 // move filter coefficients right to compensate for filterPos
536 for (j = filterSize - 2; j >= 0; j--) {
537 int right = FFMIN(j + shift, filterSize - 1);
538 filter[i * filterSize + right] += filter[i * filterSize + j];
539 filter[i * filterSize + j] = 0;
541 (*filterPos)[i]= srcW - filterSize;
545 // Note the +1 is for the MMX scaler which reads over the end
546 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
547 FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
548 *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
550 /* normalize & store in outFilter */
551 for (i = 0; i < dstW; i++) {
556 for (j = 0; j < filterSize; j++) {
557 sum += filter[i * filterSize + j];
559 sum = (sum + one / 2) / one;
560 for (j = 0; j < *outFilterSize; j++) {
561 int64_t v = filter[i * filterSize + j] + error;
562 int intV = ROUNDED_DIV(v, sum);
563 (*outFilter)[i * (*outFilterSize) + j] = intV;
564 error = v - intV * sum;
568 (*filterPos)[dstW + 0] =
569 (*filterPos)[dstW + 1] =
570 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
571 * read over the end */
572 for (i = 0; i < *outFilterSize; i++) {
573 int k = (dstW - 1) * (*outFilterSize) + i;
574 (*outFilter)[k + 1 * (*outFilterSize)] =
575 (*outFilter)[k + 2 * (*outFilterSize)] =
576 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
588 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode,
589 int16_t *filter, int32_t *filterPos, int numSplits)
592 x86_reg imm8OfPShufW1A;
593 x86_reg imm8OfPShufW2A;
594 x86_reg fragmentLengthA;
596 x86_reg imm8OfPShufW1B;
597 x86_reg imm8OfPShufW2B;
598 x86_reg fragmentLengthB;
603 // create an optimized horizontal scaling routine
604 /* This scaler is made of runtime-generated MMX2 code using specially tuned
605 * pshufw instructions. For every four output pixels, if four input pixels
606 * are enough for the fast bilinear scaling, then a chunk of fragmentB is
607 * used. If five input pixels are needed, then a chunk of fragmentA is used.
616 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
617 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
618 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
619 "punpcklbw %%mm7, %%mm1 \n\t"
620 "punpcklbw %%mm7, %%mm0 \n\t"
621 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
623 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
625 "psubw %%mm1, %%mm0 \n\t"
626 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
627 "pmullw %%mm3, %%mm0 \n\t"
628 "psllw $7, %%mm1 \n\t"
629 "paddw %%mm1, %%mm0 \n\t"
631 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
633 "add $8, %%"REG_a" \n\t"
637 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
638 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
639 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
644 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
648 : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
649 "=r" (fragmentLengthA)
656 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
657 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
658 "punpcklbw %%mm7, %%mm0 \n\t"
659 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
661 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
663 "psubw %%mm1, %%mm0 \n\t"
664 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
665 "pmullw %%mm3, %%mm0 \n\t"
666 "psllw $7, %%mm1 \n\t"
667 "paddw %%mm1, %%mm0 \n\t"
669 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
671 "add $8, %%"REG_a" \n\t"
675 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
676 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
677 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
682 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
686 : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
687 "=r" (fragmentLengthB)
690 xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
693 for (i = 0; i < dstW / numSplits; i++) {
698 int b = ((xpos + xInc) >> 16) - xx;
699 int c = ((xpos + xInc * 2) >> 16) - xx;
700 int d = ((xpos + xInc * 3) >> 16) - xx;
701 int inc = (d + 1 < 4);
702 uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
703 x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
704 x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
705 x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
706 int maxShift = 3 - (d + inc);
710 filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
711 filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
712 filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
713 filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
714 filterPos[i / 2] = xx;
716 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
718 filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
722 filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
726 if (i + 4 - inc >= dstW)
727 shift = maxShift; // avoid overread
728 else if ((filterPos[i / 2] & 3) <= maxShift)
729 shift = filterPos[i / 2] & 3; // align
731 if (shift && i >= shift) {
732 filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
733 filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
734 filterPos[i / 2] -= shift;
738 fragmentPos += fragmentLength;
741 filterCode[fragmentPos] = RET;
746 filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
748 return fragmentPos + 1;
750 #endif /* HAVE_MMX2 */
752 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
754 *h = av_pix_fmt_descriptors[format].log2_chroma_w;
755 *v = av_pix_fmt_descriptors[format].log2_chroma_h;
758 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
759 int srcRange, const int table[4], int dstRange,
760 int brightness, int contrast, int saturation)
762 memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
763 memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
765 c->brightness = brightness;
766 c->contrast = contrast;
767 c->saturation = saturation;
768 c->srcRange = srcRange;
769 c->dstRange = dstRange;
770 if (isYUV(c->dstFormat) || isGray(c->dstFormat))
773 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
774 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
776 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
777 contrast, saturation);
780 if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
781 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
782 contrast, saturation);
786 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
787 int *srcRange, int **table, int *dstRange,
788 int *brightness, int *contrast, int *saturation)
790 if (!c || isYUV(c->dstFormat) || isGray(c->dstFormat))
793 *inv_table = c->srcColorspaceTable;
794 *table = c->dstColorspaceTable;
795 *srcRange = c->srcRange;
796 *dstRange = c->dstRange;
797 *brightness = c->brightness;
798 *contrast = c->contrast;
799 *saturation = c->saturation;
804 static int handle_jpeg(enum PixelFormat *format)
807 case PIX_FMT_YUVJ420P:
808 *format = PIX_FMT_YUV420P;
810 case PIX_FMT_YUVJ422P:
811 *format = PIX_FMT_YUV422P;
813 case PIX_FMT_YUVJ444P:
814 *format = PIX_FMT_YUV444P;
816 case PIX_FMT_YUVJ440P:
817 *format = PIX_FMT_YUV440P;
824 static int handle_0alpha(enum PixelFormat *format)
827 case PIX_FMT_0BGR : *format = PIX_FMT_ABGR ; return 1;
828 case PIX_FMT_BGR0 : *format = PIX_FMT_BGRA ; return 4;
829 case PIX_FMT_0RGB : *format = PIX_FMT_ARGB ; return 1;
830 case PIX_FMT_RGB0 : *format = PIX_FMT_RGBA ; return 4;
835 SwsContext *sws_alloc_context(void)
837 SwsContext *c = av_mallocz(sizeof(SwsContext));
839 c->av_class = &sws_context_class;
840 av_opt_set_defaults(c);
845 int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
848 int usesVFilter, usesHFilter;
850 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
855 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
856 int flags, cpu_flags;
857 enum PixelFormat srcFormat = c->srcFormat;
858 enum PixelFormat dstFormat = c->dstFormat;
860 cpu_flags = av_get_cpu_flags();
866 unscaled = (srcW == dstW && srcH == dstH);
868 handle_jpeg(&srcFormat);
869 handle_jpeg(&dstFormat);
870 handle_0alpha(&srcFormat);
871 handle_0alpha(&dstFormat);
873 if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat){
874 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
875 c->srcFormat= srcFormat;
876 c->dstFormat= dstFormat;
879 if (!sws_isSupportedInput(srcFormat)) {
880 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
881 av_get_pix_fmt_name(srcFormat));
882 return AVERROR(EINVAL);
884 if (!sws_isSupportedOutput(dstFormat)) {
885 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
886 av_get_pix_fmt_name(dstFormat));
887 return AVERROR(EINVAL);
890 i = flags & (SWS_POINT |
901 if (!i || (i & (i - 1))) {
902 av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen, got %X\n", i);
903 return AVERROR(EINVAL);
906 if (srcW < 4 || srcH < 1 || dstW < 8 || dstH < 1) {
907 /* FIXME check if these are enough and try to lower them after
908 * fixing the relevant parts of the code */
909 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
910 srcW, srcH, dstW, dstH);
911 return AVERROR(EINVAL);
915 dstFilter = &dummyFilter;
917 srcFilter = &dummyFilter;
919 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
920 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
921 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
922 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
923 c->vRounder = 4 * 0x0001000100010001ULL;
925 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
926 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
927 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
928 (dstFilter->chrV && dstFilter->chrV->length > 1);
929 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
930 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
931 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
932 (dstFilter->chrH && dstFilter->chrH->length > 1);
934 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
935 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
938 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
940 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
941 flags |= SWS_FULL_CHR_H_INT;
945 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
946 * chroma interpolation */
947 if (flags & SWS_FULL_CHR_H_INT &&
948 isAnyRGB(dstFormat) &&
949 dstFormat != PIX_FMT_RGBA &&
950 dstFormat != PIX_FMT_ARGB &&
951 dstFormat != PIX_FMT_BGRA &&
952 dstFormat != PIX_FMT_ABGR &&
953 dstFormat != PIX_FMT_RGB24 &&
954 dstFormat != PIX_FMT_BGR24) {
955 av_log(c, AV_LOG_WARNING,
956 "full chroma interpolation for destination format '%s' not yet implemented\n",
957 av_get_pix_fmt_name(dstFormat));
958 flags &= ~SWS_FULL_CHR_H_INT;
961 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
962 c->chrDstHSubSample = 1;
964 // drop some chroma lines if the user wants it
965 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
966 SWS_SRC_V_CHR_DROP_SHIFT;
967 c->chrSrcVSubSample += c->vChrDrop;
969 /* drop every other pixel for chroma calculation unless user
970 * wants full chroma */
971 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
972 srcFormat != PIX_FMT_RGB8 && srcFormat != PIX_FMT_BGR8 &&
973 srcFormat != PIX_FMT_RGB4 && srcFormat != PIX_FMT_BGR4 &&
974 srcFormat != PIX_FMT_RGB4_BYTE && srcFormat != PIX_FMT_BGR4_BYTE &&
975 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
976 (flags & SWS_FAST_BILINEAR)))
977 c->chrSrcHSubSample = 1;
979 // Note the -((-x)>>y) is so that we always round toward +inf.
980 c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample);
981 c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample);
982 c->chrDstW = -((-dstW) >> c->chrDstHSubSample);
983 c->chrDstH = -((-dstH) >> c->chrDstVSubSample);
985 /* unscaled special cases */
986 if (unscaled && !usesHFilter && !usesVFilter &&
987 (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
988 ff_get_unscaled_swscale(c);
991 if (flags & SWS_PRINT_INFO)
992 av_log(c, AV_LOG_INFO,
993 "using unscaled %s -> %s special converter\n",
994 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
999 c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
1002 c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
1005 if (isAnyRGB(srcFormat) || srcFormat == PIX_FMT_PAL8)
1007 if (c->dstBpc == 16)
1009 FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1010 if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 &&
1011 c->srcBpc == 8 && c->dstBpc <= 10) {
1012 c->canMMX2BeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
1013 (srcW & 15) == 0) ? 1 : 0;
1014 if (!c->canMMX2BeUsed && dstW >= srcW && (srcW & 15) == 0
1015 && (flags & SWS_FAST_BILINEAR)) {
1016 if (flags & SWS_PRINT_INFO)
1017 av_log(c, AV_LOG_INFO,
1018 "output width is not a multiple of 32 -> no MMX2 scaler\n");
1020 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1023 c->canMMX2BeUsed = 0;
1025 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1026 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1028 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1029 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1031 * n-2 is the last chrominance sample available.
1032 * This is not perfect, but no one should notice the difference, the more
1033 * correct variant would be like the vertical one, but that would require
1034 * some special code for the first and last pixel */
1035 if (flags & SWS_FAST_BILINEAR) {
1036 if (c->canMMX2BeUsed) {
1040 // we don't use the x86 asm scaler if MMX is available
1041 else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX && c->dstBpc <= 10) {
1042 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1043 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1047 /* precalculate horizontal scaler filter coefficients */
1050 // can't downscale !!!
1051 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
1052 c->lumMmx2FilterCodeSize = initMMX2HScaler(dstW, c->lumXInc, NULL,
1054 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc,
1055 NULL, NULL, NULL, 4);
1057 #ifdef MAP_ANONYMOUS
1058 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1059 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1060 #elif HAVE_VIRTUALALLOC
1061 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1062 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1064 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
1065 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
1068 #ifdef MAP_ANONYMOUS
1069 if (c->lumMmx2FilterCode == MAP_FAILED || c->chrMmx2FilterCode == MAP_FAILED)
1071 if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
1074 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1075 return AVERROR(ENOMEM);
1078 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1079 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1080 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1081 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1083 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode,
1084 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1085 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode,
1086 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1088 #ifdef MAP_ANONYMOUS
1089 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
1090 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
1093 #endif /* HAVE_MMX2 */
1095 const int filterAlign =
1096 (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
1097 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1100 if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1101 &c->hLumFilterSize, c->lumXInc,
1102 srcW, dstW, filterAlign, 1 << 14,
1103 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1104 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1107 if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1108 &c->hChrFilterSize, c->chrXInc,
1109 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1110 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1111 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1115 } // initialize horizontal stuff
1117 /* precalculate vertical scaler filter coefficients */
1119 const int filterAlign =
1120 (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
1121 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1124 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1125 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1126 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1127 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1130 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1131 c->chrYInc, c->chrSrcH, c->chrDstH,
1132 filterAlign, (1 << 12),
1133 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1134 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1139 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1140 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1142 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1144 short *p = (short *)&c->vYCoeffsBank[i];
1145 for (j = 0; j < 8; j++)
1146 p[j] = c->vLumFilter[i];
1149 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1151 short *p = (short *)&c->vCCoeffsBank[i];
1152 for (j = 0; j < 8; j++)
1153 p[j] = c->vChrFilter[i];
1158 // calculate buffer sizes so that they won't run out while handling these damn slices
1159 c->vLumBufSize = c->vLumFilterSize;
1160 c->vChrBufSize = c->vChrFilterSize;
1161 for (i = 0; i < dstH; i++) {
1162 int chrI = (int64_t)i * c->chrDstH / dstH;
1163 int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1164 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1165 << c->chrSrcVSubSample));
1167 nextSlice >>= c->chrSrcVSubSample;
1168 nextSlice <<= c->chrSrcVSubSample;
1169 if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1170 c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1171 if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1172 (nextSlice >> c->chrSrcVSubSample))
1173 c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1174 c->vChrFilterPos[chrI];
1177 /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1178 * need to allocate several megabytes to handle all possible cases) */
1179 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1180 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1181 FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1182 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1183 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1184 /* Note we need at least one pixel more at the end because of the MMX code
1185 * (just in case someone wants to replace the 4000/8000). */
1186 /* align at 16 bytes for AltiVec */
1187 for (i = 0; i < c->vLumBufSize; i++) {
1188 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1189 dst_stride + 16, fail);
1190 c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1192 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1193 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1194 c->uv_offx2 = dst_stride + 16;
1195 for (i = 0; i < c->vChrBufSize; i++) {
1196 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1197 dst_stride * 2 + 32, fail);
1198 c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1199 c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1200 = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1202 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1203 for (i = 0; i < c->vLumBufSize; i++) {
1204 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1205 dst_stride + 16, fail);
1206 c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1209 // try to avoid drawing green stuff between the right end and the stride end
1210 for (i = 0; i < c->vChrBufSize; i++)
1211 if(av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 == 15){
1212 av_assert0(c->dstBpc > 10);
1213 for(j=0; j<dst_stride/2+1; j++)
1214 ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1216 for(j=0; j<dst_stride+1; j++)
1217 ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1219 assert(c->chrDstH <= dstH);
1221 if (flags & SWS_PRINT_INFO) {
1222 if (flags & SWS_FAST_BILINEAR)
1223 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1224 else if (flags & SWS_BILINEAR)
1225 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1226 else if (flags & SWS_BICUBIC)
1227 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1228 else if (flags & SWS_X)
1229 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1230 else if (flags & SWS_POINT)
1231 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1232 else if (flags & SWS_AREA)
1233 av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1234 else if (flags & SWS_BICUBLIN)
1235 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1236 else if (flags & SWS_GAUSS)
1237 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1238 else if (flags & SWS_SINC)
1239 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1240 else if (flags & SWS_LANCZOS)
1241 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1242 else if (flags & SWS_SPLINE)
1243 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1245 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1247 av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1248 av_get_pix_fmt_name(srcFormat),
1250 dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
1251 dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
1252 dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ?
1257 av_get_pix_fmt_name(dstFormat));
1259 if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2)
1260 av_log(c, AV_LOG_INFO, "using MMX2\n");
1261 else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW)
1262 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1263 else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
1264 av_log(c, AV_LOG_INFO, "using MMX\n");
1265 else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC)
1266 av_log(c, AV_LOG_INFO, "using AltiVec\n");
1268 av_log(c, AV_LOG_INFO, "using C\n");
1270 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1271 av_log(c, AV_LOG_DEBUG,
1272 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1273 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1274 av_log(c, AV_LOG_DEBUG,
1275 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1276 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1277 c->chrXInc, c->chrYInc);
1280 c->swScale = ff_getSwsFunc(c);
1282 fail: // FIXME replace things by appropriate error codes
1286 #if FF_API_SWS_GETCONTEXT
1287 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
1288 int dstW, int dstH, enum PixelFormat dstFormat,
1289 int flags, SwsFilter *srcFilter,
1290 SwsFilter *dstFilter, const double *param)
1294 if (!(c = sws_alloc_context()))
1302 c->srcRange = handle_jpeg(&srcFormat);
1303 c->dstRange = handle_jpeg(&dstFormat);
1304 c->src0Alpha = handle_0alpha(&srcFormat);
1305 c->dst0Alpha = handle_0alpha(&dstFormat);
1306 c->srcFormat = srcFormat;
1307 c->dstFormat = dstFormat;
1310 c->param[0] = param[0];
1311 c->param[1] = param[1];
1313 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1314 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1315 c->dstRange, 0, 1 << 16, 1 << 16);
1317 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1326 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1327 float lumaSharpen, float chromaSharpen,
1328 float chromaHShift, float chromaVShift,
1331 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1335 if (lumaGBlur != 0.0) {
1336 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1337 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1339 filter->lumH = sws_getIdentityVec();
1340 filter->lumV = sws_getIdentityVec();
1343 if (chromaGBlur != 0.0) {
1344 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1345 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1347 filter->chrH = sws_getIdentityVec();
1348 filter->chrV = sws_getIdentityVec();
1351 if (chromaSharpen != 0.0) {
1352 SwsVector *id = sws_getIdentityVec();
1353 sws_scaleVec(filter->chrH, -chromaSharpen);
1354 sws_scaleVec(filter->chrV, -chromaSharpen);
1355 sws_addVec(filter->chrH, id);
1356 sws_addVec(filter->chrV, id);
1360 if (lumaSharpen != 0.0) {
1361 SwsVector *id = sws_getIdentityVec();
1362 sws_scaleVec(filter->lumH, -lumaSharpen);
1363 sws_scaleVec(filter->lumV, -lumaSharpen);
1364 sws_addVec(filter->lumH, id);
1365 sws_addVec(filter->lumV, id);
1369 if (chromaHShift != 0.0)
1370 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1372 if (chromaVShift != 0.0)
1373 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1375 sws_normalizeVec(filter->chrH, 1.0);
1376 sws_normalizeVec(filter->chrV, 1.0);
1377 sws_normalizeVec(filter->lumH, 1.0);
1378 sws_normalizeVec(filter->lumV, 1.0);
1381 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1383 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1388 SwsVector *sws_allocVec(int length)
1390 SwsVector *vec = av_malloc(sizeof(SwsVector));
1393 vec->length = length;
1394 vec->coeff = av_malloc(sizeof(double) * length);
1400 SwsVector *sws_getGaussianVec(double variance, double quality)
1402 const int length = (int)(variance * quality + 0.5) | 1;
1404 double middle = (length - 1) * 0.5;
1405 SwsVector *vec = sws_allocVec(length);
1410 for (i = 0; i < length; i++) {
1411 double dist = i - middle;
1412 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1413 sqrt(2 * variance * M_PI);
1416 sws_normalizeVec(vec, 1.0);
1421 SwsVector *sws_getConstVec(double c, int length)
1424 SwsVector *vec = sws_allocVec(length);
1429 for (i = 0; i < length; i++)
1435 SwsVector *sws_getIdentityVec(void)
1437 return sws_getConstVec(1.0, 1);
1440 static double sws_dcVec(SwsVector *a)
1445 for (i = 0; i < a->length; i++)
1451 void sws_scaleVec(SwsVector *a, double scalar)
1455 for (i = 0; i < a->length; i++)
1456 a->coeff[i] *= scalar;
1459 void sws_normalizeVec(SwsVector *a, double height)
1461 sws_scaleVec(a, height / sws_dcVec(a));
1464 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1466 int length = a->length + b->length - 1;
1468 SwsVector *vec = sws_getConstVec(0.0, length);
1473 for (i = 0; i < a->length; i++) {
1474 for (j = 0; j < b->length; j++) {
1475 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1482 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1484 int length = FFMAX(a->length, b->length);
1486 SwsVector *vec = sws_getConstVec(0.0, length);
1491 for (i = 0; i < a->length; i++)
1492 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1493 for (i = 0; i < b->length; i++)
1494 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1499 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1501 int length = FFMAX(a->length, b->length);
1503 SwsVector *vec = sws_getConstVec(0.0, length);
1508 for (i = 0; i < a->length; i++)
1509 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1510 for (i = 0; i < b->length; i++)
1511 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1516 /* shift left / or right if "shift" is negative */
1517 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1519 int length = a->length + FFABS(shift) * 2;
1521 SwsVector *vec = sws_getConstVec(0.0, length);
1526 for (i = 0; i < a->length; i++) {
1527 vec->coeff[i + (length - 1) / 2 -
1528 (a->length - 1) / 2 - shift] = a->coeff[i];
1534 void sws_shiftVec(SwsVector *a, int shift)
1536 SwsVector *shifted = sws_getShiftedVec(a, shift);
1538 a->coeff = shifted->coeff;
1539 a->length = shifted->length;
1543 void sws_addVec(SwsVector *a, SwsVector *b)
1545 SwsVector *sum = sws_sumVec(a, b);
1547 a->coeff = sum->coeff;
1548 a->length = sum->length;
1552 void sws_subVec(SwsVector *a, SwsVector *b)
1554 SwsVector *diff = sws_diffVec(a, b);
1556 a->coeff = diff->coeff;
1557 a->length = diff->length;
1561 void sws_convVec(SwsVector *a, SwsVector *b)
1563 SwsVector *conv = sws_getConvVec(a, b);
1565 a->coeff = conv->coeff;
1566 a->length = conv->length;
1570 SwsVector *sws_cloneVec(SwsVector *a)
1573 SwsVector *vec = sws_allocVec(a->length);
1578 for (i = 0; i < a->length; i++)
1579 vec->coeff[i] = a->coeff[i];
1584 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1591 for (i = 0; i < a->length; i++)
1592 if (a->coeff[i] > max)
1595 for (i = 0; i < a->length; i++)
1596 if (a->coeff[i] < min)
1601 for (i = 0; i < a->length; i++) {
1602 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1603 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1605 av_log(log_ctx, log_level, " ");
1606 av_log(log_ctx, log_level, "|\n");
1610 void sws_freeVec(SwsVector *a)
1614 av_freep(&a->coeff);
1619 void sws_freeFilter(SwsFilter *filter)
1625 sws_freeVec(filter->lumH);
1627 sws_freeVec(filter->lumV);
1629 sws_freeVec(filter->chrH);
1631 sws_freeVec(filter->chrV);
1635 void sws_freeContext(SwsContext *c)
1642 for (i = 0; i < c->vLumBufSize; i++)
1643 av_freep(&c->lumPixBuf[i]);
1644 av_freep(&c->lumPixBuf);
1647 if (c->chrUPixBuf) {
1648 for (i = 0; i < c->vChrBufSize; i++)
1649 av_freep(&c->chrUPixBuf[i]);
1650 av_freep(&c->chrUPixBuf);
1651 av_freep(&c->chrVPixBuf);
1654 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1655 for (i = 0; i < c->vLumBufSize; i++)
1656 av_freep(&c->alpPixBuf[i]);
1657 av_freep(&c->alpPixBuf);
1660 av_freep(&c->vLumFilter);
1661 av_freep(&c->vChrFilter);
1662 av_freep(&c->hLumFilter);
1663 av_freep(&c->hChrFilter);
1665 av_freep(&c->vYCoeffsBank);
1666 av_freep(&c->vCCoeffsBank);
1669 av_freep(&c->vLumFilterPos);
1670 av_freep(&c->vChrFilterPos);
1671 av_freep(&c->hLumFilterPos);
1672 av_freep(&c->hChrFilterPos);
1675 #ifdef MAP_ANONYMOUS
1676 if (c->lumMmx2FilterCode)
1677 munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
1678 if (c->chrMmx2FilterCode)
1679 munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
1680 #elif HAVE_VIRTUALALLOC
1681 if (c->lumMmx2FilterCode)
1682 VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
1683 if (c->chrMmx2FilterCode)
1684 VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
1686 av_free(c->lumMmx2FilterCode);
1687 av_free(c->chrMmx2FilterCode);
1689 c->lumMmx2FilterCode = NULL;
1690 c->chrMmx2FilterCode = NULL;
1691 #endif /* HAVE_MMX */
1693 av_freep(&c->yuvTable);
1694 av_freep(&c->formatConvBuffer);
1699 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
1700 int srcH, enum PixelFormat srcFormat,
1702 enum PixelFormat dstFormat, int flags,
1703 SwsFilter *srcFilter,
1704 SwsFilter *dstFilter,
1705 const double *param)
1707 static const double default_param[2] = { SWS_PARAM_DEFAULT,
1708 SWS_PARAM_DEFAULT };
1711 param = default_param;
1714 (context->srcW != srcW ||
1715 context->srcH != srcH ||
1716 context->srcFormat != srcFormat ||
1717 context->dstW != dstW ||
1718 context->dstH != dstH ||
1719 context->dstFormat != dstFormat ||
1720 context->flags != flags ||
1721 context->param[0] != param[0] ||
1722 context->param[1] != param[1])) {
1723 sws_freeContext(context);
1728 if (!(context = sws_alloc_context()))
1730 context->srcW = srcW;
1731 context->srcH = srcH;
1732 context->srcRange = handle_jpeg(&srcFormat);
1733 context->src0Alpha = handle_0alpha(&srcFormat);
1734 context->srcFormat = srcFormat;
1735 context->dstW = dstW;
1736 context->dstH = dstH;
1737 context->dstRange = handle_jpeg(&dstFormat);
1738 context->dst0Alpha = handle_0alpha(&dstFormat);
1739 context->dstFormat = dstFormat;
1740 context->flags = flags;
1741 context->param[0] = param[0];
1742 context->param[1] = param[1];
1743 sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1745 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1746 context->dstRange, 0, 1 << 16, 1 << 16);
1747 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1748 sws_freeContext(context);