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 _DEFAULT_SOURCE
24 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
25 #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/attributes.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/avutil.h"
44 #include "libavutil/bswap.h"
45 #include "libavutil/cpu.h"
46 #include "libavutil/imgutils.h"
47 #include "libavutil/intreadwrite.h"
48 #include "libavutil/libm.h"
49 #include "libavutil/mathematics.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/pixdesc.h"
52 #include "libavutil/aarch64/cpu.h"
53 #include "libavutil/ppc/cpu.h"
54 #include "libavutil/x86/asm.h"
55 #include "libavutil/x86/cpu.h"
57 // We have to implement deprecated functions until they are removed, this is the
58 // simplest way to prevent warnings
59 #undef attribute_deprecated
60 #define attribute_deprecated
64 #include "swscale_internal.h"
66 #if !FF_API_SWS_VECTOR
67 static SwsVector *sws_getIdentityVec(void);
68 static void sws_addVec(SwsVector *a, SwsVector *b);
69 static void sws_shiftVec(SwsVector *a, int shift);
70 static void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level);
73 static void handle_formats(SwsContext *c);
75 unsigned swscale_version(void)
77 av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
78 return LIBSWSCALE_VERSION_INT;
81 const char *swscale_configuration(void)
83 return FFMPEG_CONFIGURATION;
86 const char *swscale_license(void)
88 #define LICENSE_PREFIX "libswscale license: "
89 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
92 typedef struct FormatEntry {
93 uint8_t is_supported_in :1;
94 uint8_t is_supported_out :1;
95 uint8_t is_supported_endianness :1;
98 static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
99 [AV_PIX_FMT_YUV420P] = { 1, 1 },
100 [AV_PIX_FMT_YUYV422] = { 1, 1 },
101 [AV_PIX_FMT_RGB24] = { 1, 1 },
102 [AV_PIX_FMT_BGR24] = { 1, 1 },
103 [AV_PIX_FMT_YUV422P] = { 1, 1 },
104 [AV_PIX_FMT_YUV444P] = { 1, 1 },
105 [AV_PIX_FMT_YUV410P] = { 1, 1 },
106 [AV_PIX_FMT_YUV411P] = { 1, 1 },
107 [AV_PIX_FMT_GRAY8] = { 1, 1 },
108 [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
109 [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
110 [AV_PIX_FMT_PAL8] = { 1, 0 },
111 [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
112 [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
113 [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
114 [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
115 [AV_PIX_FMT_YVYU422] = { 1, 1 },
116 [AV_PIX_FMT_UYVY422] = { 1, 1 },
117 [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
118 [AV_PIX_FMT_BGR8] = { 1, 1 },
119 [AV_PIX_FMT_BGR4] = { 0, 1 },
120 [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
121 [AV_PIX_FMT_RGB8] = { 1, 1 },
122 [AV_PIX_FMT_RGB4] = { 0, 1 },
123 [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
124 [AV_PIX_FMT_NV12] = { 1, 1 },
125 [AV_PIX_FMT_NV21] = { 1, 1 },
126 [AV_PIX_FMT_ARGB] = { 1, 1 },
127 [AV_PIX_FMT_RGBA] = { 1, 1 },
128 [AV_PIX_FMT_ABGR] = { 1, 1 },
129 [AV_PIX_FMT_BGRA] = { 1, 1 },
130 [AV_PIX_FMT_0RGB] = { 1, 1 },
131 [AV_PIX_FMT_RGB0] = { 1, 1 },
132 [AV_PIX_FMT_0BGR] = { 1, 1 },
133 [AV_PIX_FMT_BGR0] = { 1, 1 },
134 [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
135 [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
136 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
137 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
138 [AV_PIX_FMT_YUV440P] = { 1, 1 },
139 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
140 [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
141 [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
142 [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
143 [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
144 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
145 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
146 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
147 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
148 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
149 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
150 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
151 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
152 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
153 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
154 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
155 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
156 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
157 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
158 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
159 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
160 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
161 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
162 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
163 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
164 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
165 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
166 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
167 [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
168 [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
169 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
170 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
171 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
172 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
173 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
174 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
175 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
176 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
177 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
178 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
179 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
180 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
181 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
182 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
183 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
184 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
185 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
186 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
187 [AV_PIX_FMT_YA8] = { 1, 1 },
188 [AV_PIX_FMT_YA16BE] = { 1, 0 },
189 [AV_PIX_FMT_YA16LE] = { 1, 0 },
190 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
191 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
192 [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
193 [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
194 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
195 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
196 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
197 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
198 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
199 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
200 [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
201 [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
202 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
203 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
204 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
205 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
206 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
207 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
208 [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
209 [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
210 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
211 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
212 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
213 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
214 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
215 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
216 [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
217 [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
218 [AV_PIX_FMT_GBRP] = { 1, 1 },
219 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
220 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
221 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
222 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
223 [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
224 [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
225 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
226 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
227 [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
228 [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
229 [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
230 [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
231 [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
232 [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
233 [AV_PIX_FMT_GBRAP] = { 1, 1 },
234 [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
235 [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
236 [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
237 [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
238 [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
239 [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
240 [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
241 [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
242 [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
243 [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
244 [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
245 [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
246 [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
247 [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
248 [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
249 [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
250 [AV_PIX_FMT_AYUV64LE] = { 1, 1},
251 [AV_PIX_FMT_P010LE] = { 1, 1 },
252 [AV_PIX_FMT_P010BE] = { 1, 1 },
255 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
257 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
258 format_entries[pix_fmt].is_supported_in : 0;
261 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
263 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
264 format_entries[pix_fmt].is_supported_out : 0;
267 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
269 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
270 format_entries[pix_fmt].is_supported_endianness : 0;
273 static double getSplineCoeff(double a, double b, double c, double d,
277 return ((d * dist + c) * dist + b) * dist + a;
279 return getSplineCoeff(0.0,
280 b + 2.0 * c + 3.0 * d,
282 -b - 3.0 * c - 6.0 * d,
286 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
288 if (pos == -1 || pos <= -513) {
289 pos = (128 << chr_subsample) - 128;
291 pos += 128; // relative to ideal left edge
292 return pos >> chr_subsample;
296 int flag; ///< flag associated to the algorithm
297 const char *description; ///< human-readable description
298 int size_factor; ///< size factor used when initing the filters
301 static const ScaleAlgorithm scale_algorithms[] = {
302 { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
303 { SWS_BICUBIC, "bicubic", 4 },
304 { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
305 { SWS_BILINEAR, "bilinear", 2 },
306 { SWS_FAST_BILINEAR, "fast bilinear", -1 },
307 { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
308 { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
309 { SWS_POINT, "nearest neighbor / point", -1 },
310 { SWS_SINC, "sinc", 20 /* infinite ;) */ },
311 { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
312 { SWS_X, "experimental", 8 },
315 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
316 int *outFilterSize, int xInc, int srcW,
317 int dstW, int filterAlign, int one,
318 int flags, int cpu_flags,
319 SwsVector *srcFilter, SwsVector *dstFilter,
320 double param[2], int srcPos, int dstPos)
326 int64_t *filter = NULL;
327 int64_t *filter2 = NULL;
328 const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
331 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
333 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
334 FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
336 if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
339 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
340 dstW, sizeof(*filter) * filterSize, fail);
342 for (i = 0; i < dstW; i++) {
343 filter[i * filterSize] = fone;
346 } else if (flags & SWS_POINT) { // lame looking point sampling mode
350 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
351 dstW, sizeof(*filter) * filterSize, fail);
353 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
354 for (i = 0; i < dstW; i++) {
355 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
357 (*filterPos)[i] = xx;
361 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
362 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
366 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
367 dstW, sizeof(*filter) * filterSize, fail);
369 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
370 for (i = 0; i < dstW; i++) {
371 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
374 (*filterPos)[i] = xx;
375 // bilinear upscale / linear interpolate / area averaging
376 for (j = 0; j < filterSize; j++) {
377 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
380 filter[i * filterSize + j] = coeff;
389 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
390 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
391 sizeFactor = scale_algorithms[i].size_factor;
395 if (flags & SWS_LANCZOS)
396 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
397 av_assert0(sizeFactor > 0);
400 filterSize = 1 + sizeFactor; // upscale
402 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
404 filterSize = FFMIN(filterSize, srcW - 2);
405 filterSize = FFMAX(filterSize, 1);
407 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
408 dstW, sizeof(*filter) * filterSize, fail);
410 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
411 for (i = 0; i < dstW; i++) {
412 int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
414 (*filterPos)[i] = xx;
415 for (j = 0; j < filterSize; j++) {
416 int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
422 floatd = d * (1.0 / (1 << 30));
424 if (flags & SWS_BICUBIC) {
425 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
426 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
428 if (d >= 1LL << 31) {
431 int64_t dd = (d * d) >> 30;
432 int64_t ddd = (dd * d) >> 30;
435 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
436 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
437 (6 * (1 << 24) - 2 * B) * (1 << 30);
439 coeff = (-B - 6 * C) * ddd +
440 (6 * B + 30 * C) * dd +
441 (-12 * B - 48 * C) * d +
442 (8 * B + 24 * C) * (1 << 30);
444 coeff /= (1LL<<54)/fone;
447 else if (flags & SWS_X) {
448 double p = param ? param * 0.01 : 0.3;
449 coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
450 coeff *= pow(2.0, -p * d * d);
453 else if (flags & SWS_X) {
454 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
458 c = cos(floatd * M_PI);
465 coeff = (c * 0.5 + 0.5) * fone;
466 } else if (flags & SWS_AREA) {
467 int64_t d2 = d - (1 << 29);
468 if (d2 * xInc < -(1LL << (29 + 16)))
469 coeff = 1.0 * (1LL << (30 + 16));
470 else if (d2 * xInc < (1LL << (29 + 16)))
471 coeff = -d2 * xInc + (1LL << (29 + 16));
474 coeff *= fone >> (30 + 16);
475 } else if (flags & SWS_GAUSS) {
476 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
477 coeff = exp2(-p * floatd * floatd) * fone;
478 } else if (flags & SWS_SINC) {
479 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
480 } else if (flags & SWS_LANCZOS) {
481 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
482 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
483 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
486 } else if (flags & SWS_BILINEAR) {
487 coeff = (1 << 30) - d;
491 } else if (flags & SWS_SPLINE) {
492 double p = -2.196152422706632;
493 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
498 filter[i * filterSize + j] = coeff;
501 xDstInSrc += 2 * xInc;
505 /* apply src & dst Filter to filter -> filter2
508 av_assert0(filterSize > 0);
509 filter2Size = filterSize;
511 filter2Size += srcFilter->length - 1;
513 filter2Size += dstFilter->length - 1;
514 av_assert0(filter2Size > 0);
515 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
517 for (i = 0; i < dstW; i++) {
521 for (k = 0; k < srcFilter->length; k++) {
522 for (j = 0; j < filterSize; j++)
523 filter2[i * filter2Size + k + j] +=
524 srcFilter->coeff[k] * filter[i * filterSize + j];
527 for (j = 0; j < filterSize; j++)
528 filter2[i * filter2Size + j] = filter[i * filterSize + j];
532 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
536 /* try to reduce the filter-size (step1 find size and shift left) */
537 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
539 for (i = dstW - 1; i >= 0; i--) {
540 int min = filter2Size;
542 int64_t cutOff = 0.0;
544 /* get rid of near zero elements on the left by shifting left */
545 for (j = 0; j < filter2Size; j++) {
547 cutOff += FFABS(filter2[i * filter2Size]);
549 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
552 /* preserve monotonicity because the core can't handle the
553 * filter otherwise */
554 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
557 // move filter coefficients left
558 for (k = 1; k < filter2Size; k++)
559 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
560 filter2[i * filter2Size + k - 1] = 0;
565 /* count near zeros on the right */
566 for (j = filter2Size - 1; j > 0; j--) {
567 cutOff += FFABS(filter2[i * filter2Size + j]);
569 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
574 if (min > minFilterSize)
578 if (PPC_ALTIVEC(cpu_flags)) {
579 // we can handle the special case 4, so we don't want to go the full 8
580 if (minFilterSize < 5)
583 /* We really don't want to waste our time doing useless computation, so
584 * fall back on the scalar C code for very small filters.
585 * Vectorizing is worth it only if you have a decent-sized vector. */
586 if (minFilterSize < 3)
590 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
591 // special case for unscaled vertical filtering
592 if (minFilterSize == 1 && filterAlign == 2)
596 av_assert0(minFilterSize > 0);
597 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
598 av_assert0(filterSize > 0);
599 filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
602 if (filterSize >= MAX_FILTER_SIZE * 16 /
603 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
604 ret = RETCODE_USE_CASCADE;
607 *outFilterSize = filterSize;
609 if (flags & SWS_PRINT_INFO)
610 av_log(NULL, AV_LOG_VERBOSE,
611 "SwScaler: reducing / aligning filtersize %d -> %d\n",
612 filter2Size, filterSize);
613 /* try to reduce the filter-size (step2 reduce it) */
614 for (i = 0; i < dstW; i++) {
617 for (j = 0; j < filterSize; j++) {
618 if (j >= filter2Size)
619 filter[i * filterSize + j] = 0;
621 filter[i * filterSize + j] = filter2[i * filter2Size + j];
622 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
623 filter[i * filterSize + j] = 0;
627 // FIXME try to align filterPos if possible
630 for (i = 0; i < dstW; i++) {
632 if ((*filterPos)[i] < 0) {
633 // move filter coefficients left to compensate for filterPos
634 for (j = 1; j < filterSize; j++) {
635 int left = FFMAX(j + (*filterPos)[i], 0);
636 filter[i * filterSize + left] += filter[i * filterSize + j];
637 filter[i * filterSize + j] = 0;
642 if ((*filterPos)[i] + filterSize > srcW) {
643 int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
646 for (j = filterSize - 1; j >= 0; j--) {
647 if ((*filterPos)[i] + j >= srcW) {
648 acc += filter[i * filterSize + j];
649 filter[i * filterSize + j] = 0;
652 for (j = filterSize - 1; j >= 0; j--) {
654 filter[i * filterSize + j] = 0;
656 filter[i * filterSize + j] = filter[i * filterSize + j - shift];
660 (*filterPos)[i]-= shift;
661 filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
663 av_assert0((*filterPos)[i] >= 0);
664 av_assert0((*filterPos)[i] < srcW);
665 if ((*filterPos)[i] + filterSize > srcW) {
666 for (j = 0; j < filterSize; j++) {
667 av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
672 // Note the +1 is for the MMX scaler which reads over the end
673 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
674 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
675 (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
677 /* normalize & store in outFilter */
678 for (i = 0; i < dstW; i++) {
683 for (j = 0; j < filterSize; j++) {
684 sum += filter[i * filterSize + j];
686 sum = (sum + one / 2) / one;
688 av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
691 for (j = 0; j < *outFilterSize; j++) {
692 int64_t v = filter[i * filterSize + j] + error;
693 int intV = ROUNDED_DIV(v, sum);
694 (*outFilter)[i * (*outFilterSize) + j] = intV;
695 error = v - intV * sum;
699 (*filterPos)[dstW + 0] =
700 (*filterPos)[dstW + 1] =
701 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
702 * read over the end */
703 for (i = 0; i < *outFilterSize; i++) {
704 int k = (dstW - 1) * (*outFilterSize) + i;
705 (*outFilter)[k + 1 * (*outFilterSize)] =
706 (*outFilter)[k + 2 * (*outFilterSize)] =
707 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
714 av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
720 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
722 int64_t W, V, Z, Cy, Cu, Cv;
723 int64_t vr = table[0];
724 int64_t ub = table[1];
725 int64_t ug = -table[2];
726 int64_t vg = -table[3];
729 uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
731 static const int8_t map[] = {
732 BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
733 RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
734 RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
735 BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
736 BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
737 RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
738 RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
739 BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
740 BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
741 RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
742 RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
743 BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
744 RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
745 BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
746 GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
747 -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
748 RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
749 BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
750 GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
751 -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
752 RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
753 BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
754 GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
755 -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
756 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
757 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
758 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
759 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
760 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
761 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
762 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
763 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
764 BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
765 BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
766 BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
769 dstRange = 0; //FIXME range = 1 is handled elsewhere
779 W = ROUNDED_DIV(ONE*ONE*ug, ub);
780 V = ROUNDED_DIV(ONE*ONE*vg, vr);
783 Cy = ROUNDED_DIV(cy*Z, ONE);
784 Cu = ROUNDED_DIV(ub*Z, ONE);
785 Cv = ROUNDED_DIV(vr*Z, ONE);
787 c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
788 c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
789 c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
791 c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
792 c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
793 c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
795 c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
796 c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
797 c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
799 if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
800 c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
801 c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
802 c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
803 c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
804 c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
805 c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
806 c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
807 c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
808 c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
810 for(i=0; i<FF_ARRAY_ELEMS(map); i++)
811 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
814 static void fill_xyztables(struct SwsContext *c)
817 double xyzgamma = XYZ_GAMMA;
818 double rgbgamma = 1.0 / RGB_GAMMA;
819 double xyzgammainv = 1.0 / XYZ_GAMMA;
820 double rgbgammainv = RGB_GAMMA;
821 static const int16_t xyz2rgb_matrix[3][4] = {
822 {13270, -6295, -2041},
824 { 228, -835, 4329} };
825 static const int16_t rgb2xyz_matrix[3][4] = {
829 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
831 memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
832 memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
833 c->xyzgamma = xyzgamma_tab;
834 c->rgbgamma = rgbgamma_tab;
835 c->xyzgammainv = xyzgammainv_tab;
836 c->rgbgammainv = rgbgammainv_tab;
838 if (rgbgamma_tab[4095])
841 /* set gamma vectors */
842 for (i = 0; i < 4096; i++) {
843 xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
844 rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
845 xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
846 rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
850 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
851 int srcRange, const int table[4], int dstRange,
852 int brightness, int contrast, int saturation)
854 const AVPixFmtDescriptor *desc_dst;
855 const AVPixFmtDescriptor *desc_src;
859 desc_dst = av_pix_fmt_desc_get(c->dstFormat);
860 desc_src = av_pix_fmt_desc_get(c->srcFormat);
862 if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
864 if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
867 if (c->srcRange != srcRange ||
868 c->dstRange != dstRange ||
869 c->brightness != brightness ||
870 c->contrast != contrast ||
871 c->saturation != saturation ||
872 memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
873 memcmp(c->dstColorspaceTable, table, sizeof(int) * 4)
877 memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
878 memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
882 c->brightness = brightness;
883 c->contrast = contrast;
884 c->saturation = saturation;
885 c->srcRange = srcRange;
886 c->dstRange = dstRange;
888 //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
889 //and what we have in ticket 2939 looks better with this check
890 if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
891 ff_sws_init_range_convert(c);
893 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
894 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
896 if (c->cascaded_context[c->cascaded_mainindex])
897 return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness, contrast, saturation);
902 if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
903 if (!c->cascaded_context[0] &&
904 memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
905 c->srcW && c->srcH && c->dstW && c->dstH) {
906 enum AVPixelFormat tmp_format;
907 int tmp_width, tmp_height;
913 av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
915 if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
916 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
917 tmp_format = AV_PIX_FMT_BGRA64;
919 tmp_format = AV_PIX_FMT_BGR48;
922 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
923 tmp_format = AV_PIX_FMT_BGRA;
925 tmp_format = AV_PIX_FMT_BGR24;
929 if (srcW*srcH > dstW*dstH) {
937 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
938 tmp_width, tmp_height, tmp_format, 64);
942 c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
943 tmp_width, tmp_height, tmp_format,
945 if (!c->cascaded_context[0])
948 c->cascaded_context[0]->alphablend = c->alphablend;
949 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
952 //we set both src and dst depending on that the RGB side will be ignored
953 sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
954 srcRange, table, dstRange,
955 brightness, contrast, saturation);
957 c->cascaded_context[1] = sws_getContext(tmp_width, tmp_height, tmp_format,
958 dstW, dstH, c->dstFormat,
959 c->flags, NULL, NULL, c->param);
960 if (!c->cascaded_context[1])
962 sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
963 srcRange, table, dstRange,
964 0, 1 << 16, 1 << 16);
970 if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
971 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
972 contrast, saturation);
976 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
977 contrast, saturation);
980 fill_rgb2yuv_table(c, table, dstRange);
985 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
986 int *srcRange, int **table, int *dstRange,
987 int *brightness, int *contrast, int *saturation)
992 *inv_table = c->srcColorspaceTable;
993 *table = c->dstColorspaceTable;
994 *srcRange = c->srcRange;
995 *dstRange = c->dstRange;
996 *brightness = c->brightness;
997 *contrast = c->contrast;
998 *saturation = c->saturation;
1003 static int handle_jpeg(enum AVPixelFormat *format)
1006 case AV_PIX_FMT_YUVJ420P:
1007 *format = AV_PIX_FMT_YUV420P;
1009 case AV_PIX_FMT_YUVJ411P:
1010 *format = AV_PIX_FMT_YUV411P;
1012 case AV_PIX_FMT_YUVJ422P:
1013 *format = AV_PIX_FMT_YUV422P;
1015 case AV_PIX_FMT_YUVJ444P:
1016 *format = AV_PIX_FMT_YUV444P;
1018 case AV_PIX_FMT_YUVJ440P:
1019 *format = AV_PIX_FMT_YUV440P;
1021 case AV_PIX_FMT_GRAY8:
1022 case AV_PIX_FMT_YA8:
1023 case AV_PIX_FMT_GRAY16LE:
1024 case AV_PIX_FMT_GRAY16BE:
1025 case AV_PIX_FMT_YA16BE:
1026 case AV_PIX_FMT_YA16LE:
1033 static int handle_0alpha(enum AVPixelFormat *format)
1036 case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
1037 case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
1038 case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
1039 case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
1044 static int handle_xyz(enum AVPixelFormat *format)
1047 case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1048 case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1053 static void handle_formats(SwsContext *c)
1055 c->src0Alpha |= handle_0alpha(&c->srcFormat);
1056 c->dst0Alpha |= handle_0alpha(&c->dstFormat);
1057 c->srcXYZ |= handle_xyz(&c->srcFormat);
1058 c->dstXYZ |= handle_xyz(&c->dstFormat);
1059 if (c->srcXYZ || c->dstXYZ)
1063 SwsContext *sws_alloc_context(void)
1065 SwsContext *c = av_mallocz(sizeof(SwsContext));
1067 av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
1070 c->av_class = &ff_sws_context_class;
1071 av_opt_set_defaults(c);
1077 static uint16_t * alloc_gamma_tbl(double e)
1081 tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
1085 for (i = 0; i < 65536; ++i) {
1086 tbl[i] = pow(i / 65535.0, e) * 65535.0;
1091 static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
1094 case AV_PIX_FMT_ARGB: return AV_PIX_FMT_RGB24;
1095 case AV_PIX_FMT_RGBA: return AV_PIX_FMT_RGB24;
1096 case AV_PIX_FMT_ABGR: return AV_PIX_FMT_BGR24;
1097 case AV_PIX_FMT_BGRA: return AV_PIX_FMT_BGR24;
1098 case AV_PIX_FMT_YA8: return AV_PIX_FMT_GRAY8;
1100 case AV_PIX_FMT_YUVA420P: return AV_PIX_FMT_YUV420P;
1101 case AV_PIX_FMT_YUVA422P: return AV_PIX_FMT_YUV422P;
1102 case AV_PIX_FMT_YUVA444P: return AV_PIX_FMT_YUV444P;
1104 case AV_PIX_FMT_GBRAP: return AV_PIX_FMT_GBRP;
1106 case AV_PIX_FMT_GBRAP10LE: return AV_PIX_FMT_GBRP10;
1107 case AV_PIX_FMT_GBRAP10BE: return AV_PIX_FMT_GBRP10;
1109 case AV_PIX_FMT_GBRAP12LE: return AV_PIX_FMT_GBRP12;
1110 case AV_PIX_FMT_GBRAP12BE: return AV_PIX_FMT_GBRP12;
1112 case AV_PIX_FMT_GBRAP16LE: return AV_PIX_FMT_GBRP16;
1113 case AV_PIX_FMT_GBRAP16BE: return AV_PIX_FMT_GBRP16;
1115 case AV_PIX_FMT_RGBA64LE: return AV_PIX_FMT_RGB48;
1116 case AV_PIX_FMT_RGBA64BE: return AV_PIX_FMT_RGB48;
1117 case AV_PIX_FMT_BGRA64LE: return AV_PIX_FMT_BGR48;
1118 case AV_PIX_FMT_BGRA64BE: return AV_PIX_FMT_BGR48;
1120 case AV_PIX_FMT_YA16BE: return AV_PIX_FMT_GRAY16;
1121 case AV_PIX_FMT_YA16LE: return AV_PIX_FMT_GRAY16;
1123 case AV_PIX_FMT_YUVA420P9BE: return AV_PIX_FMT_YUV420P9;
1124 case AV_PIX_FMT_YUVA422P9BE: return AV_PIX_FMT_YUV422P9;
1125 case AV_PIX_FMT_YUVA444P9BE: return AV_PIX_FMT_YUV444P9;
1126 case AV_PIX_FMT_YUVA420P9LE: return AV_PIX_FMT_YUV420P9;
1127 case AV_PIX_FMT_YUVA422P9LE: return AV_PIX_FMT_YUV422P9;
1128 case AV_PIX_FMT_YUVA444P9LE: return AV_PIX_FMT_YUV444P9;
1129 case AV_PIX_FMT_YUVA420P10BE: return AV_PIX_FMT_YUV420P10;
1130 case AV_PIX_FMT_YUVA422P10BE: return AV_PIX_FMT_YUV422P10;
1131 case AV_PIX_FMT_YUVA444P10BE: return AV_PIX_FMT_YUV444P10;
1132 case AV_PIX_FMT_YUVA420P10LE: return AV_PIX_FMT_YUV420P10;
1133 case AV_PIX_FMT_YUVA422P10LE: return AV_PIX_FMT_YUV422P10;
1134 case AV_PIX_FMT_YUVA444P10LE: return AV_PIX_FMT_YUV444P10;
1135 case AV_PIX_FMT_YUVA420P16BE: return AV_PIX_FMT_YUV420P16;
1136 case AV_PIX_FMT_YUVA422P16BE: return AV_PIX_FMT_YUV422P16;
1137 case AV_PIX_FMT_YUVA444P16BE: return AV_PIX_FMT_YUV444P16;
1138 case AV_PIX_FMT_YUVA420P16LE: return AV_PIX_FMT_YUV420P16;
1139 case AV_PIX_FMT_YUVA422P16LE: return AV_PIX_FMT_YUV422P16;
1140 case AV_PIX_FMT_YUVA444P16LE: return AV_PIX_FMT_YUV444P16;
1142 // case AV_PIX_FMT_AYUV64LE:
1143 // case AV_PIX_FMT_AYUV64BE:
1144 // case AV_PIX_FMT_PAL8:
1145 default: return AV_PIX_FMT_NONE;
1149 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
1150 SwsFilter *dstFilter)
1153 int usesVFilter, usesHFilter;
1155 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1160 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1161 int flags, cpu_flags;
1162 enum AVPixelFormat srcFormat = c->srcFormat;
1163 enum AVPixelFormat dstFormat = c->dstFormat;
1164 const AVPixFmtDescriptor *desc_src;
1165 const AVPixFmtDescriptor *desc_dst;
1167 enum AVPixelFormat tmpFmt;
1169 cpu_flags = av_get_cpu_flags();
1173 ff_sws_rgb2rgb_init();
1175 unscaled = (srcW == dstW && srcH == dstH);
1177 c->srcRange |= handle_jpeg(&c->srcFormat);
1178 c->dstRange |= handle_jpeg(&c->dstFormat);
1180 if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
1181 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
1183 if (!c->contrast && !c->saturation && !c->dstFormatBpp)
1184 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1185 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1186 c->dstRange, 0, 1 << 16, 1 << 16);
1189 srcFormat = c->srcFormat;
1190 dstFormat = c->dstFormat;
1191 desc_src = av_pix_fmt_desc_get(srcFormat);
1192 desc_dst = av_pix_fmt_desc_get(dstFormat);
1194 // If the source has no alpha then disable alpha blendaway
1196 c->alphablend = SWS_ALPHA_BLEND_NONE;
1198 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1199 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1200 if (!sws_isSupportedInput(srcFormat)) {
1201 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1202 av_get_pix_fmt_name(srcFormat));
1203 return AVERROR(EINVAL);
1205 if (!sws_isSupportedOutput(dstFormat)) {
1206 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1207 av_get_pix_fmt_name(dstFormat));
1208 return AVERROR(EINVAL);
1211 av_assert2(desc_src && desc_dst);
1213 i = flags & (SWS_POINT |
1225 /* provide a default scaler if not set by caller */
1227 if (dstW < srcW && dstH < srcH)
1228 flags |= SWS_BICUBIC;
1229 else if (dstW > srcW && dstH > srcH)
1230 flags |= SWS_BICUBIC;
1232 flags |= SWS_BICUBIC;
1234 } else if (i & (i - 1)) {
1235 av_log(c, AV_LOG_ERROR,
1236 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1237 return AVERROR(EINVAL);
1240 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1241 /* FIXME check if these are enough and try to lower them after
1242 * fixing the relevant parts of the code */
1243 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1244 srcW, srcH, dstW, dstH);
1245 return AVERROR(EINVAL);
1247 if (flags & SWS_FAST_BILINEAR) {
1248 if (srcW < 8 || dstW < 8) {
1249 flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
1255 dstFilter = &dummyFilter;
1257 srcFilter = &dummyFilter;
1259 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1260 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1261 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1262 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1263 c->vRounder = 4 * 0x0001000100010001ULL;
1265 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1266 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1267 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1268 (dstFilter->chrV && dstFilter->chrV->length > 1);
1269 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1270 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1271 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1272 (dstFilter->chrH && dstFilter->chrH->length > 1);
1274 av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1275 av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1277 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1279 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1280 flags |= SWS_FULL_CHR_H_INT;
1284 if ( c->chrSrcHSubSample == 0
1285 && c->chrSrcVSubSample == 0
1286 && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1287 && !(c->flags & SWS_FAST_BILINEAR)
1289 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1290 flags |= SWS_FULL_CHR_H_INT;
1295 if (c->dither == SWS_DITHER_AUTO) {
1296 if (flags & SWS_ERROR_DIFFUSION)
1297 c->dither = SWS_DITHER_ED;
1300 if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1301 dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1302 dstFormat == AV_PIX_FMT_BGR8 ||
1303 dstFormat == AV_PIX_FMT_RGB8) {
1304 if (c->dither == SWS_DITHER_AUTO)
1305 c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1306 if (!(flags & SWS_FULL_CHR_H_INT)) {
1307 if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
1308 av_log(c, AV_LOG_DEBUG,
1309 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1310 av_get_pix_fmt_name(dstFormat));
1311 flags |= SWS_FULL_CHR_H_INT;
1315 if (flags & SWS_FULL_CHR_H_INT) {
1316 if (c->dither == SWS_DITHER_BAYER) {
1317 av_log(c, AV_LOG_DEBUG,
1318 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1319 av_get_pix_fmt_name(dstFormat));
1320 c->dither = SWS_DITHER_ED;
1324 if (isPlanarRGB(dstFormat)) {
1325 if (!(flags & SWS_FULL_CHR_H_INT)) {
1326 av_log(c, AV_LOG_DEBUG,
1327 "%s output is not supported with half chroma resolution, switching to full\n",
1328 av_get_pix_fmt_name(dstFormat));
1329 flags |= SWS_FULL_CHR_H_INT;
1334 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1335 * chroma interpolation */
1336 if (flags & SWS_FULL_CHR_H_INT &&
1337 isAnyRGB(dstFormat) &&
1338 !isPlanarRGB(dstFormat) &&
1339 dstFormat != AV_PIX_FMT_RGBA64LE &&
1340 dstFormat != AV_PIX_FMT_RGBA64BE &&
1341 dstFormat != AV_PIX_FMT_BGRA64LE &&
1342 dstFormat != AV_PIX_FMT_BGRA64BE &&
1343 dstFormat != AV_PIX_FMT_RGB48LE &&
1344 dstFormat != AV_PIX_FMT_RGB48BE &&
1345 dstFormat != AV_PIX_FMT_BGR48LE &&
1346 dstFormat != AV_PIX_FMT_BGR48BE &&
1347 dstFormat != AV_PIX_FMT_RGBA &&
1348 dstFormat != AV_PIX_FMT_ARGB &&
1349 dstFormat != AV_PIX_FMT_BGRA &&
1350 dstFormat != AV_PIX_FMT_ABGR &&
1351 dstFormat != AV_PIX_FMT_RGB24 &&
1352 dstFormat != AV_PIX_FMT_BGR24 &&
1353 dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1354 dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1355 dstFormat != AV_PIX_FMT_BGR8 &&
1356 dstFormat != AV_PIX_FMT_RGB8
1358 av_log(c, AV_LOG_WARNING,
1359 "full chroma interpolation for destination format '%s' not yet implemented\n",
1360 av_get_pix_fmt_name(dstFormat));
1361 flags &= ~SWS_FULL_CHR_H_INT;
1364 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1365 c->chrDstHSubSample = 1;
1367 // drop some chroma lines if the user wants it
1368 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1369 SWS_SRC_V_CHR_DROP_SHIFT;
1370 c->chrSrcVSubSample += c->vChrDrop;
1372 /* drop every other pixel for chroma calculation unless user
1373 * wants full chroma */
1374 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1375 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1376 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1377 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1378 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1379 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1380 srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
1381 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1382 srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
1383 srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1384 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1385 srcFormat != AV_PIX_FMT_GBRAP16BE && srcFormat != AV_PIX_FMT_GBRAP16LE &&
1386 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1387 (flags & SWS_FAST_BILINEAR)))
1388 c->chrSrcHSubSample = 1;
1390 // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
1391 c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1392 c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1393 c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1394 c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1396 FF_ALLOCZ_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1398 c->srcBpc = desc_src->comp[0].depth;
1401 c->dstBpc = desc_dst->comp[0].depth;
1404 if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1406 if (c->dstBpc == 16)
1409 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1410 c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1411 c->chrDstW >= c->chrSrcW &&
1413 if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1415 && (flags & SWS_FAST_BILINEAR)) {
1416 if (flags & SWS_PRINT_INFO)
1417 av_log(c, AV_LOG_INFO,
1418 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1420 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1421 c->canMMXEXTBeUsed = 0;
1423 c->canMMXEXTBeUsed = 0;
1425 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1426 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1428 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1429 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1431 * n-2 is the last chrominance sample available.
1432 * This is not perfect, but no one should notice the difference, the more
1433 * correct variant would be like the vertical one, but that would require
1434 * some special code for the first and last pixel */
1435 if (flags & SWS_FAST_BILINEAR) {
1436 if (c->canMMXEXTBeUsed) {
1440 // we don't use the x86 asm scaler if MMX is available
1441 else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1442 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1443 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1447 // hardcoded for now
1448 c->gamma_value = 2.2;
1449 tmpFmt = AV_PIX_FMT_RGBA64LE;
1452 if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
1454 c->cascaded_context[0] = NULL;
1456 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1457 srcW, srcH, tmpFmt, 64);
1461 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1463 flags, NULL, NULL, c->param);
1464 if (!c->cascaded_context[0]) {
1468 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
1470 flags, srcFilter, dstFilter, c->param);
1472 if (!c->cascaded_context[1])
1475 c2 = c->cascaded_context[1];
1476 c2->is_internal_gamma = 1;
1477 c2->gamma = alloc_gamma_tbl( c->gamma_value);
1478 c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
1479 if (!c2->gamma || !c2->inv_gamma)
1480 return AVERROR(ENOMEM);
1482 // is_internal_flag is set after creating the context
1483 // to properly create the gamma convert FilterDescriptor
1484 // we have to re-initialize it
1485 ff_free_filters(c2);
1486 if (ff_init_filters(c2) < 0) {
1487 sws_freeContext(c2);
1491 c->cascaded_context[2] = NULL;
1492 if (dstFormat != tmpFmt) {
1493 ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
1494 dstW, dstH, tmpFmt, 64);
1498 c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
1499 dstW, dstH, dstFormat,
1500 flags, NULL, NULL, c->param);
1501 if (!c->cascaded_context[2])
1507 if (isBayer(srcFormat)) {
1509 (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
1510 enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
1512 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1513 srcW, srcH, tmpFormat, 64);
1517 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1518 srcW, srcH, tmpFormat,
1519 flags, srcFilter, NULL, c->param);
1520 if (!c->cascaded_context[0])
1523 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
1524 dstW, dstH, dstFormat,
1525 flags, NULL, dstFilter, c->param);
1526 if (!c->cascaded_context[1])
1532 if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
1533 enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
1535 if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE)
1537 dstFormat != tmpFormat ||
1538 usesHFilter || usesVFilter ||
1539 c->srcRange != c->dstRange
1541 c->cascaded_mainindex = 1;
1542 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1543 srcW, srcH, tmpFormat, 64);
1547 c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
1548 srcW, srcH, tmpFormat,
1550 if (!c->cascaded_context[0])
1552 c->cascaded_context[0]->alphablend = c->alphablend;
1553 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
1557 c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
1558 dstW, dstH, dstFormat,
1560 if (!c->cascaded_context[1])
1563 c->cascaded_context[1]->srcRange = c->srcRange;
1564 c->cascaded_context[1]->dstRange = c->dstRange;
1565 ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
1573 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1575 /* precalculate horizontal scaler filter coefficients */
1577 #if HAVE_MMXEXT_INLINE
1578 // can't downscale !!!
1579 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1580 c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1582 c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1583 NULL, NULL, NULL, 4);
1586 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1587 PROT_READ | PROT_WRITE,
1588 MAP_PRIVATE | MAP_ANONYMOUS,
1590 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1591 PROT_READ | PROT_WRITE,
1592 MAP_PRIVATE | MAP_ANONYMOUS,
1594 #elif HAVE_VIRTUALALLOC
1595 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1596 c->lumMmxextFilterCodeSize,
1598 PAGE_EXECUTE_READWRITE);
1599 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1600 c->chrMmxextFilterCodeSize,
1602 PAGE_EXECUTE_READWRITE);
1604 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1605 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1608 #ifdef MAP_ANONYMOUS
1609 if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1611 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1614 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1615 return AVERROR(ENOMEM);
1618 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1619 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1620 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1621 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1623 ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1624 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1625 ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1626 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1629 if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1630 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1631 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1636 #endif /* HAVE_MMXEXT_INLINE */
1638 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1639 PPC_ALTIVEC(cpu_flags) ? 8 :
1640 have_neon(cpu_flags) ? 8 : 1;
1642 if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
1643 &c->hLumFilterSize, c->lumXInc,
1644 srcW, dstW, filterAlign, 1 << 14,
1645 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1646 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1648 get_local_pos(c, 0, 0, 0),
1649 get_local_pos(c, 0, 0, 0))) < 0)
1651 if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
1652 &c->hChrFilterSize, c->chrXInc,
1653 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1654 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1655 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1657 get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1658 get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
1661 } // initialize horizontal stuff
1663 /* precalculate vertical scaler filter coefficients */
1665 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1666 PPC_ALTIVEC(cpu_flags) ? 8 :
1667 have_neon(cpu_flags) ? 2 : 1;
1669 if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1670 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1671 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1672 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1674 get_local_pos(c, 0, 0, 1),
1675 get_local_pos(c, 0, 0, 1))) < 0)
1677 if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1678 c->chrYInc, c->chrSrcH, c->chrDstH,
1679 filterAlign, (1 << 12),
1680 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1681 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1683 get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1684 get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
1689 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1690 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1692 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1694 short *p = (short *)&c->vYCoeffsBank[i];
1695 for (j = 0; j < 8; j++)
1696 p[j] = c->vLumFilter[i];
1699 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1701 short *p = (short *)&c->vCCoeffsBank[i];
1702 for (j = 0; j < 8; j++)
1703 p[j] = c->vChrFilter[i];
1708 for (i = 0; i < 4; i++)
1709 FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1711 c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
1713 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1714 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1715 c->uv_offx2 = dst_stride + 16;
1717 av_assert0(c->chrDstH <= dstH);
1719 if (flags & SWS_PRINT_INFO) {
1720 const char *scaler = NULL, *cpucaps;
1722 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1723 if (flags & scale_algorithms[i].flag) {
1724 scaler = scale_algorithms[i].description;
1729 scaler = "ehh flags invalid?!";
1730 av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1732 av_get_pix_fmt_name(srcFormat),
1734 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1735 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1736 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1741 av_get_pix_fmt_name(dstFormat));
1743 if (INLINE_MMXEXT(cpu_flags))
1745 else if (INLINE_AMD3DNOW(cpu_flags))
1747 else if (INLINE_MMX(cpu_flags))
1749 else if (PPC_ALTIVEC(cpu_flags))
1750 cpucaps = "AltiVec";
1754 av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1756 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1757 av_log(c, AV_LOG_DEBUG,
1758 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1759 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1760 av_log(c, AV_LOG_DEBUG,
1761 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1762 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1763 c->chrXInc, c->chrYInc);
1766 /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
1767 if (unscaled && !usesHFilter && !usesVFilter &&
1768 c->alphablend != SWS_ALPHA_BLEND_NONE &&
1769 isALPHA(srcFormat) &&
1770 (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
1771 alphaless_fmt(srcFormat) == dstFormat
1773 c->swscale = ff_sws_alphablendaway;
1775 if (flags & SWS_PRINT_INFO)
1776 av_log(c, AV_LOG_INFO,
1777 "using alpha blendaway %s -> %s special converter\n",
1778 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1782 /* unscaled special cases */
1783 if (unscaled && !usesHFilter && !usesVFilter &&
1784 (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1785 ff_get_unscaled_swscale(c);
1788 if (flags & SWS_PRINT_INFO)
1789 av_log(c, AV_LOG_INFO,
1790 "using unscaled %s -> %s special converter\n",
1791 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1796 c->swscale = ff_getSwsFunc(c);
1797 return ff_init_filters(c);
1798 fail: // FIXME replace things by appropriate error codes
1799 if (ret == RETCODE_USE_CASCADE) {
1800 int tmpW = sqrt(srcW * (int64_t)dstW);
1801 int tmpH = sqrt(srcH * (int64_t)dstH);
1802 enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
1804 if (isALPHA(srcFormat))
1805 tmpFormat = AV_PIX_FMT_YUVA420P;
1807 if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
1808 return AVERROR(EINVAL);
1810 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1811 tmpW, tmpH, tmpFormat, 64);
1815 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1816 tmpW, tmpH, tmpFormat,
1817 flags, srcFilter, NULL, c->param);
1818 if (!c->cascaded_context[0])
1821 c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
1822 dstW, dstH, dstFormat,
1823 flags, NULL, dstFilter, c->param);
1824 if (!c->cascaded_context[1])
1831 SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
1832 int dstW, int dstH, enum AVPixelFormat dstFormat,
1833 int flags, const double *param)
1837 if (!(c = sws_alloc_context()))
1845 c->srcFormat = srcFormat;
1846 c->dstFormat = dstFormat;
1849 c->param[0] = param[0];
1850 c->param[1] = param[1];
1856 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1857 int dstW, int dstH, enum AVPixelFormat dstFormat,
1858 int flags, SwsFilter *srcFilter,
1859 SwsFilter *dstFilter, const double *param)
1863 c = sws_alloc_set_opts(srcW, srcH, srcFormat,
1864 dstW, dstH, dstFormat,
1869 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1877 static int isnan_vec(SwsVector *a)
1880 for (i=0; i<a->length; i++)
1881 if (isnan(a->coeff[i]))
1886 static void makenan_vec(SwsVector *a)
1889 for (i=0; i<a->length; i++)
1893 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1894 float lumaSharpen, float chromaSharpen,
1895 float chromaHShift, float chromaVShift,
1898 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1902 if (lumaGBlur != 0.0) {
1903 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1904 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1906 filter->lumH = sws_getIdentityVec();
1907 filter->lumV = sws_getIdentityVec();
1910 if (chromaGBlur != 0.0) {
1911 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1912 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1914 filter->chrH = sws_getIdentityVec();
1915 filter->chrV = sws_getIdentityVec();
1918 if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
1921 if (chromaSharpen != 0.0) {
1922 SwsVector *id = sws_getIdentityVec();
1925 sws_scaleVec(filter->chrH, -chromaSharpen);
1926 sws_scaleVec(filter->chrV, -chromaSharpen);
1927 sws_addVec(filter->chrH, id);
1928 sws_addVec(filter->chrV, id);
1932 if (lumaSharpen != 0.0) {
1933 SwsVector *id = sws_getIdentityVec();
1936 sws_scaleVec(filter->lumH, -lumaSharpen);
1937 sws_scaleVec(filter->lumV, -lumaSharpen);
1938 sws_addVec(filter->lumH, id);
1939 sws_addVec(filter->lumV, id);
1943 if (chromaHShift != 0.0)
1944 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1946 if (chromaVShift != 0.0)
1947 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1949 sws_normalizeVec(filter->chrH, 1.0);
1950 sws_normalizeVec(filter->chrV, 1.0);
1951 sws_normalizeVec(filter->lumH, 1.0);
1952 sws_normalizeVec(filter->lumV, 1.0);
1954 if (isnan_vec(filter->chrH) ||
1955 isnan_vec(filter->chrV) ||
1956 isnan_vec(filter->lumH) ||
1957 isnan_vec(filter->lumV))
1961 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1963 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1968 sws_freeVec(filter->lumH);
1969 sws_freeVec(filter->lumV);
1970 sws_freeVec(filter->chrH);
1971 sws_freeVec(filter->chrV);
1976 SwsVector *sws_allocVec(int length)
1980 if(length <= 0 || length > INT_MAX/ sizeof(double))
1983 vec = av_malloc(sizeof(SwsVector));
1986 vec->length = length;
1987 vec->coeff = av_malloc(sizeof(double) * length);
1993 SwsVector *sws_getGaussianVec(double variance, double quality)
1995 const int length = (int)(variance * quality + 0.5) | 1;
1997 double middle = (length - 1) * 0.5;
2000 if(variance < 0 || quality < 0)
2003 vec = sws_allocVec(length);
2008 for (i = 0; i < length; i++) {
2009 double dist = i - middle;
2010 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
2011 sqrt(2 * variance * M_PI);
2014 sws_normalizeVec(vec, 1.0);
2020 * Allocate and return a vector with length coefficients, all
2021 * with the same value c.
2023 #if !FF_API_SWS_VECTOR
2026 SwsVector *sws_getConstVec(double c, int length)
2029 SwsVector *vec = sws_allocVec(length);
2034 for (i = 0; i < length; i++)
2041 * Allocate and return a vector with just one coefficient, with
2044 #if !FF_API_SWS_VECTOR
2047 SwsVector *sws_getIdentityVec(void)
2049 return sws_getConstVec(1.0, 1);
2052 static double sws_dcVec(SwsVector *a)
2057 for (i = 0; i < a->length; i++)
2063 void sws_scaleVec(SwsVector *a, double scalar)
2067 for (i = 0; i < a->length; i++)
2068 a->coeff[i] *= scalar;
2071 void sws_normalizeVec(SwsVector *a, double height)
2073 sws_scaleVec(a, height / sws_dcVec(a));
2076 #if FF_API_SWS_VECTOR
2077 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
2079 int length = a->length + b->length - 1;
2081 SwsVector *vec = sws_getConstVec(0.0, length);
2086 for (i = 0; i < a->length; i++) {
2087 for (j = 0; j < b->length; j++) {
2088 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
2096 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
2098 int length = FFMAX(a->length, b->length);
2100 SwsVector *vec = sws_getConstVec(0.0, length);
2105 for (i = 0; i < a->length; i++)
2106 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2107 for (i = 0; i < b->length; i++)
2108 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
2113 #if FF_API_SWS_VECTOR
2114 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
2116 int length = FFMAX(a->length, b->length);
2118 SwsVector *vec = sws_getConstVec(0.0, length);
2123 for (i = 0; i < a->length; i++)
2124 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2125 for (i = 0; i < b->length; i++)
2126 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
2132 /* shift left / or right if "shift" is negative */
2133 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
2135 int length = a->length + FFABS(shift) * 2;
2137 SwsVector *vec = sws_getConstVec(0.0, length);
2142 for (i = 0; i < a->length; i++) {
2143 vec->coeff[i + (length - 1) / 2 -
2144 (a->length - 1) / 2 - shift] = a->coeff[i];
2150 #if !FF_API_SWS_VECTOR
2153 void sws_shiftVec(SwsVector *a, int shift)
2155 SwsVector *shifted = sws_getShiftedVec(a, shift);
2161 a->coeff = shifted->coeff;
2162 a->length = shifted->length;
2166 #if !FF_API_SWS_VECTOR
2169 void sws_addVec(SwsVector *a, SwsVector *b)
2171 SwsVector *sum = sws_sumVec(a, b);
2177 a->coeff = sum->coeff;
2178 a->length = sum->length;
2182 #if FF_API_SWS_VECTOR
2183 void sws_subVec(SwsVector *a, SwsVector *b)
2185 SwsVector *diff = sws_diffVec(a, b);
2191 a->coeff = diff->coeff;
2192 a->length = diff->length;
2196 void sws_convVec(SwsVector *a, SwsVector *b)
2198 SwsVector *conv = sws_getConvVec(a, b);
2204 a->coeff = conv->coeff;
2205 a->length = conv->length;
2209 SwsVector *sws_cloneVec(SwsVector *a)
2211 SwsVector *vec = sws_allocVec(a->length);
2216 memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
2223 * Print with av_log() a textual representation of the vector a
2224 * if log_level <= av_log_level.
2226 #if !FF_API_SWS_VECTOR
2229 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
2236 for (i = 0; i < a->length; i++)
2237 if (a->coeff[i] > max)
2240 for (i = 0; i < a->length; i++)
2241 if (a->coeff[i] < min)
2246 for (i = 0; i < a->length; i++) {
2247 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
2248 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
2250 av_log(log_ctx, log_level, " ");
2251 av_log(log_ctx, log_level, "|\n");
2255 void sws_freeVec(SwsVector *a)
2259 av_freep(&a->coeff);
2264 void sws_freeFilter(SwsFilter *filter)
2269 sws_freeVec(filter->lumH);
2270 sws_freeVec(filter->lumV);
2271 sws_freeVec(filter->chrH);
2272 sws_freeVec(filter->chrV);
2276 void sws_freeContext(SwsContext *c)
2282 for (i = 0; i < 4; i++)
2283 av_freep(&c->dither_error[i]);
2285 av_freep(&c->vLumFilter);
2286 av_freep(&c->vChrFilter);
2287 av_freep(&c->hLumFilter);
2288 av_freep(&c->hChrFilter);
2290 av_freep(&c->vYCoeffsBank);
2291 av_freep(&c->vCCoeffsBank);
2294 av_freep(&c->vLumFilterPos);
2295 av_freep(&c->vChrFilterPos);
2296 av_freep(&c->hLumFilterPos);
2297 av_freep(&c->hChrFilterPos);
2301 if (c->lumMmxextFilterCode)
2302 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
2303 if (c->chrMmxextFilterCode)
2304 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
2305 #elif HAVE_VIRTUALALLOC
2306 if (c->lumMmxextFilterCode)
2307 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
2308 if (c->chrMmxextFilterCode)
2309 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
2311 av_free(c->lumMmxextFilterCode);
2312 av_free(c->chrMmxextFilterCode);
2314 c->lumMmxextFilterCode = NULL;
2315 c->chrMmxextFilterCode = NULL;
2316 #endif /* HAVE_MMX_INLINE */
2318 av_freep(&c->yuvTable);
2319 av_freep(&c->formatConvBuffer);
2321 sws_freeContext(c->cascaded_context[0]);
2322 sws_freeContext(c->cascaded_context[1]);
2323 sws_freeContext(c->cascaded_context[2]);
2324 memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
2325 av_freep(&c->cascaded_tmp[0]);
2326 av_freep(&c->cascaded1_tmp[0]);
2328 av_freep(&c->gamma);
2329 av_freep(&c->inv_gamma);
2336 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
2337 int srcH, enum AVPixelFormat srcFormat,
2339 enum AVPixelFormat dstFormat, int flags,
2340 SwsFilter *srcFilter,
2341 SwsFilter *dstFilter,
2342 const double *param)
2344 static const double default_param[2] = { SWS_PARAM_DEFAULT,
2345 SWS_PARAM_DEFAULT };
2346 int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
2347 src_v_chr_pos = -513, dst_v_chr_pos = -513;
2350 param = default_param;
2353 (context->srcW != srcW ||
2354 context->srcH != srcH ||
2355 context->srcFormat != srcFormat ||
2356 context->dstW != dstW ||
2357 context->dstH != dstH ||
2358 context->dstFormat != dstFormat ||
2359 context->flags != flags ||
2360 context->param[0] != param[0] ||
2361 context->param[1] != param[1])) {
2363 av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
2364 av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
2365 av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
2366 av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
2367 sws_freeContext(context);
2372 if (!(context = sws_alloc_context()))
2374 context->srcW = srcW;
2375 context->srcH = srcH;
2376 context->srcFormat = srcFormat;
2377 context->dstW = dstW;
2378 context->dstH = dstH;
2379 context->dstFormat = dstFormat;
2380 context->flags = flags;
2381 context->param[0] = param[0];
2382 context->param[1] = param[1];
2384 av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
2385 av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
2386 av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
2387 av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
2389 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2390 sws_freeContext(context);