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
31 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
32 #define MAP_ANONYMOUS MAP_ANON
36 #define WIN32_LEAN_AND_MEAN
40 #include "libavutil/attributes.h"
41 #include "libavutil/avassert.h"
42 #include "libavutil/avutil.h"
43 #include "libavutil/bswap.h"
44 #include "libavutil/cpu.h"
45 #include "libavutil/imgutils.h"
46 #include "libavutil/intreadwrite.h"
47 #include "libavutil/mathematics.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/pixdesc.h"
50 #include "libavutil/ppc/cpu.h"
51 #include "libavutil/x86/asm.h"
52 #include "libavutil/x86/cpu.h"
55 #include "swscale_internal.h"
57 static void handle_formats(SwsContext *c);
59 unsigned swscale_version(void)
61 av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
62 return LIBSWSCALE_VERSION_INT;
65 const char *swscale_configuration(void)
67 return FFMPEG_CONFIGURATION;
70 const char *swscale_license(void)
72 #define LICENSE_PREFIX "libswscale license: "
73 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
76 typedef struct FormatEntry {
77 uint8_t is_supported_in :1;
78 uint8_t is_supported_out :1;
79 uint8_t is_supported_endianness :1;
82 static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
83 [AV_PIX_FMT_YUV420P] = { 1, 1 },
84 [AV_PIX_FMT_YUYV422] = { 1, 1 },
85 [AV_PIX_FMT_RGB24] = { 1, 1 },
86 [AV_PIX_FMT_BGR24] = { 1, 1 },
87 [AV_PIX_FMT_YUV422P] = { 1, 1 },
88 [AV_PIX_FMT_YUV444P] = { 1, 1 },
89 [AV_PIX_FMT_YUV410P] = { 1, 1 },
90 [AV_PIX_FMT_YUV411P] = { 1, 1 },
91 [AV_PIX_FMT_GRAY8] = { 1, 1 },
92 [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
93 [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
94 [AV_PIX_FMT_PAL8] = { 1, 0 },
95 [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
96 [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
97 [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
98 [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
99 [AV_PIX_FMT_YVYU422] = { 1, 1 },
100 [AV_PIX_FMT_UYVY422] = { 1, 1 },
101 [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
102 [AV_PIX_FMT_BGR8] = { 1, 1 },
103 [AV_PIX_FMT_BGR4] = { 0, 1 },
104 [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
105 [AV_PIX_FMT_RGB8] = { 1, 1 },
106 [AV_PIX_FMT_RGB4] = { 0, 1 },
107 [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
108 [AV_PIX_FMT_NV12] = { 1, 1 },
109 [AV_PIX_FMT_NV21] = { 1, 1 },
110 [AV_PIX_FMT_ARGB] = { 1, 1 },
111 [AV_PIX_FMT_RGBA] = { 1, 1 },
112 [AV_PIX_FMT_ABGR] = { 1, 1 },
113 [AV_PIX_FMT_BGRA] = { 1, 1 },
114 [AV_PIX_FMT_0RGB] = { 1, 1 },
115 [AV_PIX_FMT_RGB0] = { 1, 1 },
116 [AV_PIX_FMT_0BGR] = { 1, 1 },
117 [AV_PIX_FMT_BGR0] = { 1, 1 },
118 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
119 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
120 [AV_PIX_FMT_YUV440P] = { 1, 1 },
121 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
122 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
123 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
124 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
125 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
126 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
127 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
128 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
129 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
130 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
131 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
132 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
133 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
134 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
135 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
136 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
137 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
138 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
139 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
140 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
141 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
142 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
143 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
144 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
145 [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
146 [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
147 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
148 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
149 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
150 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
151 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
152 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
153 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
154 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
155 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
156 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
157 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
158 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
159 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
160 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
161 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
162 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
163 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
164 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
165 [AV_PIX_FMT_YA8] = { 1, 0 },
166 [AV_PIX_FMT_YA16BE] = { 1, 0 },
167 [AV_PIX_FMT_YA16LE] = { 1, 0 },
168 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
169 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
170 [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
171 [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
172 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
173 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
174 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
175 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
176 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
177 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
178 [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
179 [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
180 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
181 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
182 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
183 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
184 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
185 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
186 [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
187 [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
188 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
189 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
190 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
191 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
192 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
193 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
194 [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
195 [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
196 [AV_PIX_FMT_GBRP] = { 1, 1 },
197 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
198 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
199 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
200 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
201 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
202 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
203 [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
204 [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
205 [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
206 [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
207 [AV_PIX_FMT_GBRAP] = { 1, 1 },
208 [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
209 [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
210 [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
211 [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
212 [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
213 [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
214 [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
215 [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
216 [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
217 [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
218 [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
219 [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
220 [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
221 [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
222 [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
223 [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
226 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
228 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
229 format_entries[pix_fmt].is_supported_in : 0;
232 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
234 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
235 format_entries[pix_fmt].is_supported_out : 0;
238 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
240 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
241 format_entries[pix_fmt].is_supported_endianness : 0;
244 static double getSplineCoeff(double a, double b, double c, double d,
248 return ((d * dist + c) * dist + b) * dist + a;
250 return getSplineCoeff(0.0,
251 b + 2.0 * c + 3.0 * d,
253 -b - 3.0 * c - 6.0 * d,
257 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
259 if (pos == -1 || pos <= -513) {
260 pos = (128 << chr_subsample) - 128;
262 pos += 128; // relative to ideal left edge
263 return pos >> chr_subsample;
267 int flag; ///< flag associated to the algorithm
268 const char *description; ///< human-readable description
269 int size_factor; ///< size factor used when initing the filters
272 static const ScaleAlgorithm scale_algorithms[] = {
273 { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
274 { SWS_BICUBIC, "bicubic", 4 },
275 { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
276 { SWS_BILINEAR, "bilinear", 2 },
277 { SWS_FAST_BILINEAR, "fast bilinear", -1 },
278 { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
279 { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
280 { SWS_POINT, "nearest neighbor / point", -1 },
281 { SWS_SINC, "sinc", 20 /* infinite ;) */ },
282 { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
283 { SWS_X, "experimental", 8 },
286 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
287 int *outFilterSize, int xInc, int srcW,
288 int dstW, int filterAlign, int one,
289 int flags, int cpu_flags,
290 SwsVector *srcFilter, SwsVector *dstFilter,
291 double param[2], int srcPos, int dstPos)
297 int64_t *filter = NULL;
298 int64_t *filter2 = NULL;
299 const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
302 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
304 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
305 FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
307 if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
310 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
311 dstW, sizeof(*filter) * filterSize, fail);
313 for (i = 0; i < dstW; i++) {
314 filter[i * filterSize] = fone;
317 } else if (flags & SWS_POINT) { // lame looking point sampling mode
321 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
322 dstW, sizeof(*filter) * filterSize, fail);
324 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
325 for (i = 0; i < dstW; i++) {
326 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
328 (*filterPos)[i] = xx;
332 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
333 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
337 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
338 dstW, sizeof(*filter) * filterSize, fail);
340 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
341 for (i = 0; i < dstW; i++) {
342 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
345 (*filterPos)[i] = xx;
346 // bilinear upscale / linear interpolate / area averaging
347 for (j = 0; j < filterSize; j++) {
348 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
351 filter[i * filterSize + j] = coeff;
360 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
361 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
362 sizeFactor = scale_algorithms[i].size_factor;
366 if (flags & SWS_LANCZOS)
367 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
368 av_assert0(sizeFactor > 0);
371 filterSize = 1 + sizeFactor; // upscale
373 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
375 filterSize = FFMIN(filterSize, srcW - 2);
376 filterSize = FFMAX(filterSize, 1);
378 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
379 dstW, sizeof(*filter) * filterSize, fail);
381 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
382 for (i = 0; i < dstW; i++) {
383 int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
385 (*filterPos)[i] = xx;
386 for (j = 0; j < filterSize; j++) {
387 int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
393 floatd = d * (1.0 / (1 << 30));
395 if (flags & SWS_BICUBIC) {
396 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
397 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
399 if (d >= 1LL << 31) {
402 int64_t dd = (d * d) >> 30;
403 int64_t ddd = (dd * d) >> 30;
406 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
407 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
408 (6 * (1 << 24) - 2 * B) * (1 << 30);
410 coeff = (-B - 6 * C) * ddd +
411 (6 * B + 30 * C) * dd +
412 (-12 * B - 48 * C) * d +
413 (8 * B + 24 * C) * (1 << 30);
415 coeff /= (1LL<<54)/fone;
418 else if (flags & SWS_X) {
419 double p = param ? param * 0.01 : 0.3;
420 coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
421 coeff *= pow(2.0, -p * d * d);
424 else if (flags & SWS_X) {
425 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
429 c = cos(floatd * M_PI);
436 coeff = (c * 0.5 + 0.5) * fone;
437 } else if (flags & SWS_AREA) {
438 int64_t d2 = d - (1 << 29);
439 if (d2 * xInc < -(1LL << (29 + 16)))
440 coeff = 1.0 * (1LL << (30 + 16));
441 else if (d2 * xInc < (1LL << (29 + 16)))
442 coeff = -d2 * xInc + (1LL << (29 + 16));
445 coeff *= fone >> (30 + 16);
446 } else if (flags & SWS_GAUSS) {
447 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
448 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
449 } else if (flags & SWS_SINC) {
450 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
451 } else if (flags & SWS_LANCZOS) {
452 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
453 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
454 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
457 } else if (flags & SWS_BILINEAR) {
458 coeff = (1 << 30) - d;
462 } else if (flags & SWS_SPLINE) {
463 double p = -2.196152422706632;
464 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
469 filter[i * filterSize + j] = coeff;
472 xDstInSrc += 2 * xInc;
476 /* apply src & dst Filter to filter -> filter2
479 av_assert0(filterSize > 0);
480 filter2Size = filterSize;
482 filter2Size += srcFilter->length - 1;
484 filter2Size += dstFilter->length - 1;
485 av_assert0(filter2Size > 0);
486 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
488 for (i = 0; i < dstW; i++) {
492 for (k = 0; k < srcFilter->length; k++) {
493 for (j = 0; j < filterSize; j++)
494 filter2[i * filter2Size + k + j] +=
495 srcFilter->coeff[k] * filter[i * filterSize + j];
498 for (j = 0; j < filterSize; j++)
499 filter2[i * filter2Size + j] = filter[i * filterSize + j];
503 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
507 /* try to reduce the filter-size (step1 find size and shift left) */
508 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
510 for (i = dstW - 1; i >= 0; i--) {
511 int min = filter2Size;
513 int64_t cutOff = 0.0;
515 /* get rid of near zero elements on the left by shifting left */
516 for (j = 0; j < filter2Size; j++) {
518 cutOff += FFABS(filter2[i * filter2Size]);
520 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
523 /* preserve monotonicity because the core can't handle the
524 * filter otherwise */
525 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
528 // move filter coefficients left
529 for (k = 1; k < filter2Size; k++)
530 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
531 filter2[i * filter2Size + k - 1] = 0;
536 /* count near zeros on the right */
537 for (j = filter2Size - 1; j > 0; j--) {
538 cutOff += FFABS(filter2[i * filter2Size + j]);
540 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
545 if (min > minFilterSize)
549 if (PPC_ALTIVEC(cpu_flags)) {
550 // we can handle the special case 4, so we don't want to go the full 8
551 if (minFilterSize < 5)
554 /* We really don't want to waste our time doing useless computation, so
555 * fall back on the scalar C code for very small filters.
556 * Vectorizing is worth it only if you have a decent-sized vector. */
557 if (minFilterSize < 3)
561 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
562 // special case for unscaled vertical filtering
563 if (minFilterSize == 1 && filterAlign == 2)
567 av_assert0(minFilterSize > 0);
568 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
569 av_assert0(filterSize > 0);
570 filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
573 if (filterSize >= MAX_FILTER_SIZE * 16 /
574 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
575 ret = RETCODE_USE_CASCADE;
578 *outFilterSize = filterSize;
580 if (flags & SWS_PRINT_INFO)
581 av_log(NULL, AV_LOG_VERBOSE,
582 "SwScaler: reducing / aligning filtersize %d -> %d\n",
583 filter2Size, filterSize);
584 /* try to reduce the filter-size (step2 reduce it) */
585 for (i = 0; i < dstW; i++) {
588 for (j = 0; j < filterSize; j++) {
589 if (j >= filter2Size)
590 filter[i * filterSize + j] = 0;
592 filter[i * filterSize + j] = filter2[i * filter2Size + j];
593 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
594 filter[i * filterSize + j] = 0;
598 // FIXME try to align filterPos if possible
601 for (i = 0; i < dstW; i++) {
603 if ((*filterPos)[i] < 0) {
604 // move filter coefficients left to compensate for filterPos
605 for (j = 1; j < filterSize; j++) {
606 int left = FFMAX(j + (*filterPos)[i], 0);
607 filter[i * filterSize + left] += filter[i * filterSize + j];
608 filter[i * filterSize + j] = 0;
613 if ((*filterPos)[i] + filterSize > srcW) {
614 int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
616 // move filter coefficients right to compensate for filterPos
617 for (j = filterSize - 2; j >= 0; j--) {
618 int right = FFMIN(j + shift, filterSize - 1);
619 filter[i * filterSize + right] += filter[i * filterSize + j];
620 filter[i * filterSize + j] = 0;
622 (*filterPos)[i]-= shift;
626 // Note the +1 is for the MMX scaler which reads over the end
627 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
628 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
629 (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
631 /* normalize & store in outFilter */
632 for (i = 0; i < dstW; i++) {
637 for (j = 0; j < filterSize; j++) {
638 sum += filter[i * filterSize + j];
640 sum = (sum + one / 2) / one;
642 av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
645 for (j = 0; j < *outFilterSize; j++) {
646 int64_t v = filter[i * filterSize + j] + error;
647 int intV = ROUNDED_DIV(v, sum);
648 (*outFilter)[i * (*outFilterSize) + j] = intV;
649 error = v - intV * sum;
653 (*filterPos)[dstW + 0] =
654 (*filterPos)[dstW + 1] =
655 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
656 * read over the end */
657 for (i = 0; i < *outFilterSize; i++) {
658 int k = (dstW - 1) * (*outFilterSize) + i;
659 (*outFilter)[k + 1 * (*outFilterSize)] =
660 (*outFilter)[k + 2 * (*outFilterSize)] =
661 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
668 av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
674 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
676 int64_t W, V, Z, Cy, Cu, Cv;
677 int64_t vr = table[0];
678 int64_t ub = table[1];
679 int64_t ug = -table[2];
680 int64_t vg = -table[3];
683 uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
685 static const int8_t map[] = {
686 BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
687 RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
688 RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
689 BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
690 BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
691 RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
692 RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
693 BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
694 BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
695 RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
696 RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
697 BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
698 RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
699 BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
700 GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
701 -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
702 RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
703 BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
704 GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
705 -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
706 RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
707 BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
708 GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
709 -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
710 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
711 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
712 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
713 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
714 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
715 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
716 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
717 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
718 BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
719 BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
720 BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
723 dstRange = 0; //FIXME range = 1 is handled elsewhere
733 W = ROUNDED_DIV(ONE*ONE*ug, ub);
734 V = ROUNDED_DIV(ONE*ONE*vg, vr);
737 Cy = ROUNDED_DIV(cy*Z, ONE);
738 Cu = ROUNDED_DIV(ub*Z, ONE);
739 Cv = ROUNDED_DIV(vr*Z, ONE);
741 c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
742 c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
743 c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
745 c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
746 c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
747 c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
749 c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
750 c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
751 c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
753 if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
754 c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
755 c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
756 c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
757 c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
758 c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
759 c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
760 c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
761 c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
762 c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
764 for(i=0; i<FF_ARRAY_ELEMS(map); i++)
765 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
768 static void fill_xyztables(struct SwsContext *c)
771 double xyzgamma = XYZ_GAMMA;
772 double rgbgamma = 1.0 / RGB_GAMMA;
773 double xyzgammainv = 1.0 / XYZ_GAMMA;
774 double rgbgammainv = RGB_GAMMA;
775 static const int16_t xyz2rgb_matrix[3][4] = {
776 {13270, -6295, -2041},
778 { 228, -835, 4329} };
779 static const int16_t rgb2xyz_matrix[3][4] = {
783 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
785 memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
786 memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
787 c->xyzgamma = xyzgamma_tab;
788 c->rgbgamma = rgbgamma_tab;
789 c->xyzgammainv = xyzgammainv_tab;
790 c->rgbgammainv = rgbgammainv_tab;
792 if (rgbgamma_tab[4095])
795 /* set gamma vectors */
796 for (i = 0; i < 4096; i++) {
797 xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
798 rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
799 xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
800 rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
804 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
805 int srcRange, const int table[4], int dstRange,
806 int brightness, int contrast, int saturation)
808 const AVPixFmtDescriptor *desc_dst;
809 const AVPixFmtDescriptor *desc_src;
811 memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
812 memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
815 desc_dst = av_pix_fmt_desc_get(c->dstFormat);
816 desc_src = av_pix_fmt_desc_get(c->srcFormat);
818 if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
820 if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
823 c->brightness = brightness;
824 c->contrast = contrast;
825 c->saturation = saturation;
826 if (c->srcRange != srcRange || c->dstRange != dstRange)
828 c->srcRange = srcRange;
829 c->dstRange = dstRange;
831 //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
832 //and what we have in ticket 2939 looks better with this check
833 if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
834 ff_sws_init_range_convert(c);
836 if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
839 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
840 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
842 if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
843 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
844 contrast, saturation);
848 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
849 contrast, saturation);
852 fill_rgb2yuv_table(c, table, dstRange);
857 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
858 int *srcRange, int **table, int *dstRange,
859 int *brightness, int *contrast, int *saturation)
864 *inv_table = c->srcColorspaceTable;
865 *table = c->dstColorspaceTable;
866 *srcRange = c->srcRange;
867 *dstRange = c->dstRange;
868 *brightness = c->brightness;
869 *contrast = c->contrast;
870 *saturation = c->saturation;
875 static int handle_jpeg(enum AVPixelFormat *format)
878 case AV_PIX_FMT_YUVJ420P:
879 *format = AV_PIX_FMT_YUV420P;
881 case AV_PIX_FMT_YUVJ411P:
882 *format = AV_PIX_FMT_YUV411P;
884 case AV_PIX_FMT_YUVJ422P:
885 *format = AV_PIX_FMT_YUV422P;
887 case AV_PIX_FMT_YUVJ444P:
888 *format = AV_PIX_FMT_YUV444P;
890 case AV_PIX_FMT_YUVJ440P:
891 *format = AV_PIX_FMT_YUV440P;
893 case AV_PIX_FMT_GRAY8:
894 case AV_PIX_FMT_GRAY16LE:
895 case AV_PIX_FMT_GRAY16BE:
902 static int handle_0alpha(enum AVPixelFormat *format)
905 case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
906 case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
907 case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
908 case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
913 static int handle_xyz(enum AVPixelFormat *format)
916 case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
917 case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
922 static void handle_formats(SwsContext *c)
924 c->src0Alpha |= handle_0alpha(&c->srcFormat);
925 c->dst0Alpha |= handle_0alpha(&c->dstFormat);
926 c->srcXYZ |= handle_xyz(&c->srcFormat);
927 c->dstXYZ |= handle_xyz(&c->dstFormat);
928 if (c->srcXYZ || c->dstXYZ)
932 SwsContext *sws_alloc_context(void)
934 SwsContext *c = av_mallocz(sizeof(SwsContext));
936 av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
939 c->av_class = &sws_context_class;
940 av_opt_set_defaults(c);
946 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
947 SwsFilter *dstFilter)
950 int usesVFilter, usesHFilter;
952 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
957 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
958 int flags, cpu_flags;
959 enum AVPixelFormat srcFormat = c->srcFormat;
960 enum AVPixelFormat dstFormat = c->dstFormat;
961 const AVPixFmtDescriptor *desc_src;
962 const AVPixFmtDescriptor *desc_dst;
965 cpu_flags = av_get_cpu_flags();
971 unscaled = (srcW == dstW && srcH == dstH);
973 c->srcRange |= handle_jpeg(&c->srcFormat);
974 c->dstRange |= handle_jpeg(&c->dstFormat);
976 if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
977 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
979 if (!c->contrast && !c->saturation && !c->dstFormatBpp)
980 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
981 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
982 c->dstRange, 0, 1 << 16, 1 << 16);
985 srcFormat = c->srcFormat;
986 dstFormat = c->dstFormat;
987 desc_src = av_pix_fmt_desc_get(srcFormat);
988 desc_dst = av_pix_fmt_desc_get(dstFormat);
990 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
991 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
992 if (!sws_isSupportedInput(srcFormat)) {
993 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
994 av_get_pix_fmt_name(srcFormat));
995 return AVERROR(EINVAL);
997 if (!sws_isSupportedOutput(dstFormat)) {
998 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
999 av_get_pix_fmt_name(dstFormat));
1000 return AVERROR(EINVAL);
1004 i = flags & (SWS_POINT |
1016 /* provide a default scaler if not set by caller */
1018 if (dstW < srcW && dstH < srcH)
1019 flags |= SWS_BICUBIC;
1020 else if (dstW > srcW && dstH > srcH)
1021 flags |= SWS_BICUBIC;
1023 flags |= SWS_BICUBIC;
1025 } else if (i & (i - 1)) {
1026 av_log(c, AV_LOG_ERROR,
1027 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1028 return AVERROR(EINVAL);
1031 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1032 /* FIXME check if these are enough and try to lower them after
1033 * fixing the relevant parts of the code */
1034 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1035 srcW, srcH, dstW, dstH);
1036 return AVERROR(EINVAL);
1040 dstFilter = &dummyFilter;
1042 srcFilter = &dummyFilter;
1044 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1045 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1046 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1047 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1048 c->vRounder = 4 * 0x0001000100010001ULL;
1050 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1051 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1052 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1053 (dstFilter->chrV && dstFilter->chrV->length > 1);
1054 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1055 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1056 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1057 (dstFilter->chrH && dstFilter->chrH->length > 1);
1059 av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1060 av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1062 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1064 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1065 flags |= SWS_FULL_CHR_H_INT;
1069 if ( c->chrSrcHSubSample == 0
1070 && c->chrSrcVSubSample == 0
1071 && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1072 && !(c->flags & SWS_FAST_BILINEAR)
1074 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1075 flags |= SWS_FULL_CHR_H_INT;
1080 if (c->dither == SWS_DITHER_AUTO) {
1081 if (flags & SWS_ERROR_DIFFUSION)
1082 c->dither = SWS_DITHER_ED;
1085 if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1086 dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1087 dstFormat == AV_PIX_FMT_BGR8 ||
1088 dstFormat == AV_PIX_FMT_RGB8) {
1089 if (c->dither == SWS_DITHER_AUTO)
1090 c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1091 if (!(flags & SWS_FULL_CHR_H_INT)) {
1092 if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
1093 av_log(c, AV_LOG_DEBUG,
1094 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1095 av_get_pix_fmt_name(dstFormat));
1096 flags |= SWS_FULL_CHR_H_INT;
1100 if (flags & SWS_FULL_CHR_H_INT) {
1101 if (c->dither == SWS_DITHER_BAYER) {
1102 av_log(c, AV_LOG_DEBUG,
1103 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1104 av_get_pix_fmt_name(dstFormat));
1105 c->dither = SWS_DITHER_ED;
1109 if (isPlanarRGB(dstFormat)) {
1110 if (!(flags & SWS_FULL_CHR_H_INT)) {
1111 av_log(c, AV_LOG_DEBUG,
1112 "%s output is not supported with half chroma resolution, switching to full\n",
1113 av_get_pix_fmt_name(dstFormat));
1114 flags |= SWS_FULL_CHR_H_INT;
1119 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1120 * chroma interpolation */
1121 if (flags & SWS_FULL_CHR_H_INT &&
1122 isAnyRGB(dstFormat) &&
1123 !isPlanarRGB(dstFormat) &&
1124 dstFormat != AV_PIX_FMT_RGBA &&
1125 dstFormat != AV_PIX_FMT_ARGB &&
1126 dstFormat != AV_PIX_FMT_BGRA &&
1127 dstFormat != AV_PIX_FMT_ABGR &&
1128 dstFormat != AV_PIX_FMT_RGB24 &&
1129 dstFormat != AV_PIX_FMT_BGR24 &&
1130 dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1131 dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1132 dstFormat != AV_PIX_FMT_BGR8 &&
1133 dstFormat != AV_PIX_FMT_RGB8
1135 av_log(c, AV_LOG_WARNING,
1136 "full chroma interpolation for destination format '%s' not yet implemented\n",
1137 av_get_pix_fmt_name(dstFormat));
1138 flags &= ~SWS_FULL_CHR_H_INT;
1141 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1142 c->chrDstHSubSample = 1;
1144 // drop some chroma lines if the user wants it
1145 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1146 SWS_SRC_V_CHR_DROP_SHIFT;
1147 c->chrSrcVSubSample += c->vChrDrop;
1149 /* drop every other pixel for chroma calculation unless user
1150 * wants full chroma */
1151 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1152 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1153 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1154 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1155 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1156 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1157 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1158 srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1159 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1160 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1161 (flags & SWS_FAST_BILINEAR)))
1162 c->chrSrcHSubSample = 1;
1164 // Note the FF_CEIL_RSHIFT is so that we always round toward +inf.
1165 c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1166 c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1167 c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1168 c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1170 FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1172 c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
1175 c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
1178 if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1180 if (c->dstBpc == 16)
1183 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1184 c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1185 c->chrDstW >= c->chrSrcW &&
1187 if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1189 && (flags & SWS_FAST_BILINEAR)) {
1190 if (flags & SWS_PRINT_INFO)
1191 av_log(c, AV_LOG_INFO,
1192 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1194 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1195 c->canMMXEXTBeUsed = 0;
1197 c->canMMXEXTBeUsed = 0;
1199 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1200 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1202 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1203 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1205 * n-2 is the last chrominance sample available.
1206 * This is not perfect, but no one should notice the difference, the more
1207 * correct variant would be like the vertical one, but that would require
1208 * some special code for the first and last pixel */
1209 if (flags & SWS_FAST_BILINEAR) {
1210 if (c->canMMXEXTBeUsed) {
1214 // we don't use the x86 asm scaler if MMX is available
1215 else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1216 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1217 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1221 if (isBayer(srcFormat)) {
1223 (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
1224 enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
1226 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1227 srcW, srcH, tmpFormat, 64);
1231 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1232 srcW, srcH, tmpFormat,
1233 flags, srcFilter, NULL, c->param);
1234 if (!c->cascaded_context[0])
1237 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
1238 dstW, dstH, dstFormat,
1239 flags, NULL, dstFilter, c->param);
1240 if (!c->cascaded_context[1])
1246 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1248 /* precalculate horizontal scaler filter coefficients */
1250 #if HAVE_MMXEXT_INLINE
1251 // can't downscale !!!
1252 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1253 c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1255 c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1256 NULL, NULL, NULL, 4);
1259 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1260 PROT_READ | PROT_WRITE,
1261 MAP_PRIVATE | MAP_ANONYMOUS,
1263 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1264 PROT_READ | PROT_WRITE,
1265 MAP_PRIVATE | MAP_ANONYMOUS,
1267 #elif HAVE_VIRTUALALLOC
1268 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1269 c->lumMmxextFilterCodeSize,
1271 PAGE_EXECUTE_READWRITE);
1272 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1273 c->chrMmxextFilterCodeSize,
1275 PAGE_EXECUTE_READWRITE);
1277 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1278 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1281 #ifdef MAP_ANONYMOUS
1282 if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1284 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1287 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1288 return AVERROR(ENOMEM);
1291 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1292 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1293 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1294 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1296 ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1297 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1298 ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1299 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1302 if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1303 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1304 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1309 #endif /* HAVE_MMXEXT_INLINE */
1311 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1312 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1314 if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
1315 &c->hLumFilterSize, c->lumXInc,
1316 srcW, dstW, filterAlign, 1 << 14,
1317 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1318 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1320 get_local_pos(c, 0, 0, 0),
1321 get_local_pos(c, 0, 0, 0))) < 0)
1323 if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
1324 &c->hChrFilterSize, c->chrXInc,
1325 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1326 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1327 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1329 get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1330 get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
1333 } // initialize horizontal stuff
1335 /* precalculate vertical scaler filter coefficients */
1337 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1338 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1340 if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1341 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1342 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1343 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1345 get_local_pos(c, 0, 0, 1),
1346 get_local_pos(c, 0, 0, 1))) < 0)
1348 if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1349 c->chrYInc, c->chrSrcH, c->chrDstH,
1350 filterAlign, (1 << 12),
1351 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1352 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1354 get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1355 get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
1360 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1361 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1363 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1365 short *p = (short *)&c->vYCoeffsBank[i];
1366 for (j = 0; j < 8; j++)
1367 p[j] = c->vLumFilter[i];
1370 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1372 short *p = (short *)&c->vCCoeffsBank[i];
1373 for (j = 0; j < 8; j++)
1374 p[j] = c->vChrFilter[i];
1379 // calculate buffer sizes so that they won't run out while handling these damn slices
1380 c->vLumBufSize = c->vLumFilterSize;
1381 c->vChrBufSize = c->vChrFilterSize;
1382 for (i = 0; i < dstH; i++) {
1383 int chrI = (int64_t)i * c->chrDstH / dstH;
1384 int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1385 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1386 << c->chrSrcVSubSample));
1388 nextSlice >>= c->chrSrcVSubSample;
1389 nextSlice <<= c->chrSrcVSubSample;
1390 if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1391 c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1392 if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1393 (nextSlice >> c->chrSrcVSubSample))
1394 c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1395 c->vChrFilterPos[chrI];
1398 for (i = 0; i < 4; i++)
1399 FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1401 /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1402 * need to allocate several megabytes to handle all possible cases) */
1403 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1404 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1405 FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1406 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1407 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1408 /* Note we need at least one pixel more at the end because of the MMX code
1409 * (just in case someone wants to replace the 4000/8000). */
1410 /* align at 16 bytes for AltiVec */
1411 for (i = 0; i < c->vLumBufSize; i++) {
1412 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1413 dst_stride + 16, fail);
1414 c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1416 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1417 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1418 c->uv_offx2 = dst_stride + 16;
1419 for (i = 0; i < c->vChrBufSize; i++) {
1420 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1421 dst_stride * 2 + 32, fail);
1422 c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1423 c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1424 = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1426 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1427 for (i = 0; i < c->vLumBufSize; i++) {
1428 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1429 dst_stride + 16, fail);
1430 c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1433 // try to avoid drawing green stuff between the right end and the stride end
1434 for (i = 0; i < c->vChrBufSize; i++)
1435 if(desc_dst->comp[0].depth_minus1 == 15){
1436 av_assert0(c->dstBpc > 14);
1437 for(j=0; j<dst_stride/2+1; j++)
1438 ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1440 for(j=0; j<dst_stride+1; j++)
1441 ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1443 av_assert0(c->chrDstH <= dstH);
1445 if (flags & SWS_PRINT_INFO) {
1446 const char *scaler = NULL, *cpucaps;
1448 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1449 if (flags & scale_algorithms[i].flag) {
1450 scaler = scale_algorithms[i].description;
1455 scaler = "ehh flags invalid?!";
1456 av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1458 av_get_pix_fmt_name(srcFormat),
1460 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1461 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1462 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1467 av_get_pix_fmt_name(dstFormat));
1469 if (INLINE_MMXEXT(cpu_flags))
1471 else if (INLINE_AMD3DNOW(cpu_flags))
1473 else if (INLINE_MMX(cpu_flags))
1475 else if (PPC_ALTIVEC(cpu_flags))
1476 cpucaps = "AltiVec";
1480 av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1482 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1483 av_log(c, AV_LOG_DEBUG,
1484 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1485 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1486 av_log(c, AV_LOG_DEBUG,
1487 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1488 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1489 c->chrXInc, c->chrYInc);
1492 /* unscaled special cases */
1493 if (unscaled && !usesHFilter && !usesVFilter &&
1494 (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1495 ff_get_unscaled_swscale(c);
1498 if (flags & SWS_PRINT_INFO)
1499 av_log(c, AV_LOG_INFO,
1500 "using unscaled %s -> %s special converter\n",
1501 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1506 c->swscale = ff_getSwsFunc(c);
1508 fail: // FIXME replace things by appropriate error codes
1509 if (ret == RETCODE_USE_CASCADE) {
1510 int tmpW = sqrt(srcW * (int64_t)dstW);
1511 int tmpH = sqrt(srcH * (int64_t)dstH);
1512 enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
1514 if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
1515 return AVERROR(EINVAL);
1517 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1518 tmpW, tmpH, tmpFormat, 64);
1522 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1523 tmpW, tmpH, tmpFormat,
1524 flags, srcFilter, NULL, c->param);
1525 if (!c->cascaded_context[0])
1528 c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
1529 dstW, dstH, dstFormat,
1530 flags, NULL, dstFilter, c->param);
1531 if (!c->cascaded_context[1])
1538 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1539 int dstW, int dstH, enum AVPixelFormat dstFormat,
1540 int flags, SwsFilter *srcFilter,
1541 SwsFilter *dstFilter, const double *param)
1545 if (!(c = sws_alloc_context()))
1553 c->srcFormat = srcFormat;
1554 c->dstFormat = dstFormat;
1557 c->param[0] = param[0];
1558 c->param[1] = param[1];
1561 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1569 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1570 float lumaSharpen, float chromaSharpen,
1571 float chromaHShift, float chromaVShift,
1574 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1578 if (lumaGBlur != 0.0) {
1579 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1580 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1582 filter->lumH = sws_getIdentityVec();
1583 filter->lumV = sws_getIdentityVec();
1586 if (chromaGBlur != 0.0) {
1587 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1588 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1590 filter->chrH = sws_getIdentityVec();
1591 filter->chrV = sws_getIdentityVec();
1594 if (chromaSharpen != 0.0) {
1595 SwsVector *id = sws_getIdentityVec();
1596 sws_scaleVec(filter->chrH, -chromaSharpen);
1597 sws_scaleVec(filter->chrV, -chromaSharpen);
1598 sws_addVec(filter->chrH, id);
1599 sws_addVec(filter->chrV, id);
1603 if (lumaSharpen != 0.0) {
1604 SwsVector *id = sws_getIdentityVec();
1605 sws_scaleVec(filter->lumH, -lumaSharpen);
1606 sws_scaleVec(filter->lumV, -lumaSharpen);
1607 sws_addVec(filter->lumH, id);
1608 sws_addVec(filter->lumV, id);
1612 if (chromaHShift != 0.0)
1613 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1615 if (chromaVShift != 0.0)
1616 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1618 sws_normalizeVec(filter->chrH, 1.0);
1619 sws_normalizeVec(filter->chrV, 1.0);
1620 sws_normalizeVec(filter->lumH, 1.0);
1621 sws_normalizeVec(filter->lumV, 1.0);
1624 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1626 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1631 SwsVector *sws_allocVec(int length)
1635 if(length <= 0 || length > INT_MAX/ sizeof(double))
1638 vec = av_malloc(sizeof(SwsVector));
1641 vec->length = length;
1642 vec->coeff = av_malloc(sizeof(double) * length);
1648 SwsVector *sws_getGaussianVec(double variance, double quality)
1650 const int length = (int)(variance * quality + 0.5) | 1;
1652 double middle = (length - 1) * 0.5;
1655 if(variance < 0 || quality < 0)
1658 vec = sws_allocVec(length);
1663 for (i = 0; i < length; i++) {
1664 double dist = i - middle;
1665 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1666 sqrt(2 * variance * M_PI);
1669 sws_normalizeVec(vec, 1.0);
1674 SwsVector *sws_getConstVec(double c, int length)
1677 SwsVector *vec = sws_allocVec(length);
1682 for (i = 0; i < length; i++)
1688 SwsVector *sws_getIdentityVec(void)
1690 return sws_getConstVec(1.0, 1);
1693 static double sws_dcVec(SwsVector *a)
1698 for (i = 0; i < a->length; i++)
1704 void sws_scaleVec(SwsVector *a, double scalar)
1708 for (i = 0; i < a->length; i++)
1709 a->coeff[i] *= scalar;
1712 void sws_normalizeVec(SwsVector *a, double height)
1714 sws_scaleVec(a, height / sws_dcVec(a));
1717 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1719 int length = a->length + b->length - 1;
1721 SwsVector *vec = sws_getConstVec(0.0, length);
1726 for (i = 0; i < a->length; i++) {
1727 for (j = 0; j < b->length; j++) {
1728 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1735 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1737 int length = FFMAX(a->length, b->length);
1739 SwsVector *vec = sws_getConstVec(0.0, length);
1744 for (i = 0; i < a->length; i++)
1745 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1746 for (i = 0; i < b->length; i++)
1747 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1752 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1754 int length = FFMAX(a->length, b->length);
1756 SwsVector *vec = sws_getConstVec(0.0, length);
1761 for (i = 0; i < a->length; i++)
1762 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1763 for (i = 0; i < b->length; i++)
1764 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1769 /* shift left / or right if "shift" is negative */
1770 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1772 int length = a->length + FFABS(shift) * 2;
1774 SwsVector *vec = sws_getConstVec(0.0, length);
1779 for (i = 0; i < a->length; i++) {
1780 vec->coeff[i + (length - 1) / 2 -
1781 (a->length - 1) / 2 - shift] = a->coeff[i];
1787 void sws_shiftVec(SwsVector *a, int shift)
1789 SwsVector *shifted = sws_getShiftedVec(a, shift);
1791 a->coeff = shifted->coeff;
1792 a->length = shifted->length;
1796 void sws_addVec(SwsVector *a, SwsVector *b)
1798 SwsVector *sum = sws_sumVec(a, b);
1800 a->coeff = sum->coeff;
1801 a->length = sum->length;
1805 void sws_subVec(SwsVector *a, SwsVector *b)
1807 SwsVector *diff = sws_diffVec(a, b);
1809 a->coeff = diff->coeff;
1810 a->length = diff->length;
1814 void sws_convVec(SwsVector *a, SwsVector *b)
1816 SwsVector *conv = sws_getConvVec(a, b);
1818 a->coeff = conv->coeff;
1819 a->length = conv->length;
1823 SwsVector *sws_cloneVec(SwsVector *a)
1825 SwsVector *vec = sws_allocVec(a->length);
1830 memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
1835 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1842 for (i = 0; i < a->length; i++)
1843 if (a->coeff[i] > max)
1846 for (i = 0; i < a->length; i++)
1847 if (a->coeff[i] < min)
1852 for (i = 0; i < a->length; i++) {
1853 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1854 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1856 av_log(log_ctx, log_level, " ");
1857 av_log(log_ctx, log_level, "|\n");
1861 void sws_freeVec(SwsVector *a)
1865 av_freep(&a->coeff);
1870 void sws_freeFilter(SwsFilter *filter)
1875 sws_freeVec(filter->lumH);
1876 sws_freeVec(filter->lumV);
1877 sws_freeVec(filter->chrH);
1878 sws_freeVec(filter->chrV);
1882 void sws_freeContext(SwsContext *c)
1889 for (i = 0; i < c->vLumBufSize; i++)
1890 av_freep(&c->lumPixBuf[i]);
1891 av_freep(&c->lumPixBuf);
1894 if (c->chrUPixBuf) {
1895 for (i = 0; i < c->vChrBufSize; i++)
1896 av_freep(&c->chrUPixBuf[i]);
1897 av_freep(&c->chrUPixBuf);
1898 av_freep(&c->chrVPixBuf);
1901 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1902 for (i = 0; i < c->vLumBufSize; i++)
1903 av_freep(&c->alpPixBuf[i]);
1904 av_freep(&c->alpPixBuf);
1907 for (i = 0; i < 4; i++)
1908 av_freep(&c->dither_error[i]);
1910 av_freep(&c->vLumFilter);
1911 av_freep(&c->vChrFilter);
1912 av_freep(&c->hLumFilter);
1913 av_freep(&c->hChrFilter);
1915 av_freep(&c->vYCoeffsBank);
1916 av_freep(&c->vCCoeffsBank);
1919 av_freep(&c->vLumFilterPos);
1920 av_freep(&c->vChrFilterPos);
1921 av_freep(&c->hLumFilterPos);
1922 av_freep(&c->hChrFilterPos);
1926 if (c->lumMmxextFilterCode)
1927 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
1928 if (c->chrMmxextFilterCode)
1929 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
1930 #elif HAVE_VIRTUALALLOC
1931 if (c->lumMmxextFilterCode)
1932 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
1933 if (c->chrMmxextFilterCode)
1934 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
1936 av_free(c->lumMmxextFilterCode);
1937 av_free(c->chrMmxextFilterCode);
1939 c->lumMmxextFilterCode = NULL;
1940 c->chrMmxextFilterCode = NULL;
1941 #endif /* HAVE_MMX_INLINE */
1943 av_freep(&c->yuvTable);
1944 av_freep(&c->formatConvBuffer);
1946 sws_freeContext(c->cascaded_context[0]);
1947 sws_freeContext(c->cascaded_context[1]);
1948 memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
1949 av_freep(&c->cascaded_tmp[0]);
1954 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
1955 int srcH, enum AVPixelFormat srcFormat,
1957 enum AVPixelFormat dstFormat, int flags,
1958 SwsFilter *srcFilter,
1959 SwsFilter *dstFilter,
1960 const double *param)
1962 static const double default_param[2] = { SWS_PARAM_DEFAULT,
1963 SWS_PARAM_DEFAULT };
1964 int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
1965 src_v_chr_pos = -513, dst_v_chr_pos = -513;
1968 param = default_param;
1971 (context->srcW != srcW ||
1972 context->srcH != srcH ||
1973 context->srcFormat != srcFormat ||
1974 context->dstW != dstW ||
1975 context->dstH != dstH ||
1976 context->dstFormat != dstFormat ||
1977 context->flags != flags ||
1978 context->param[0] != param[0] ||
1979 context->param[1] != param[1])) {
1981 av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
1982 av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
1983 av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
1984 av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
1985 sws_freeContext(context);
1990 if (!(context = sws_alloc_context()))
1992 context->srcW = srcW;
1993 context->srcH = srcH;
1994 context->srcFormat = srcFormat;
1995 context->dstW = dstW;
1996 context->dstH = dstH;
1997 context->dstFormat = dstFormat;
1998 context->flags = flags;
1999 context->param[0] = param[0];
2000 context->param[1] = param[1];
2002 av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
2003 av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
2004 av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
2005 av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
2007 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2008 sws_freeContext(context);