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/intreadwrite.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/ppc/cpu.h"
50 #include "libavutil/x86/asm.h"
51 #include "libavutil/x86/cpu.h"
54 #include "swscale_internal.h"
56 static void handle_formats(SwsContext *c);
58 unsigned swscale_version(void)
60 av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
61 return LIBSWSCALE_VERSION_INT;
64 const char *swscale_configuration(void)
66 return FFMPEG_CONFIGURATION;
69 const char *swscale_license(void)
71 #define LICENSE_PREFIX "libswscale license: "
72 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
75 #define RET 0xC3 // near return opcode for x86
77 typedef struct FormatEntry {
78 uint8_t is_supported_in :1;
79 uint8_t is_supported_out :1;
80 uint8_t is_supported_endianness :1;
83 static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
84 [AV_PIX_FMT_YUV420P] = { 1, 1 },
85 [AV_PIX_FMT_YUYV422] = { 1, 1 },
86 [AV_PIX_FMT_RGB24] = { 1, 1 },
87 [AV_PIX_FMT_BGR24] = { 1, 1 },
88 [AV_PIX_FMT_YUV422P] = { 1, 1 },
89 [AV_PIX_FMT_YUV444P] = { 1, 1 },
90 [AV_PIX_FMT_YUV410P] = { 1, 1 },
91 [AV_PIX_FMT_YUV411P] = { 1, 1 },
92 [AV_PIX_FMT_GRAY8] = { 1, 1 },
93 [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
94 [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
95 [AV_PIX_FMT_PAL8] = { 1, 0 },
96 [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
97 [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
98 [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
99 [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
100 [AV_PIX_FMT_YVYU422] = { 1, 1 },
101 [AV_PIX_FMT_UYVY422] = { 1, 1 },
102 [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
103 [AV_PIX_FMT_BGR8] = { 1, 1 },
104 [AV_PIX_FMT_BGR4] = { 0, 1 },
105 [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
106 [AV_PIX_FMT_RGB8] = { 1, 1 },
107 [AV_PIX_FMT_RGB4] = { 0, 1 },
108 [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
109 [AV_PIX_FMT_NV12] = { 1, 1 },
110 [AV_PIX_FMT_NV21] = { 1, 1 },
111 [AV_PIX_FMT_ARGB] = { 1, 1 },
112 [AV_PIX_FMT_RGBA] = { 1, 1 },
113 [AV_PIX_FMT_ABGR] = { 1, 1 },
114 [AV_PIX_FMT_BGRA] = { 1, 1 },
115 [AV_PIX_FMT_0RGB] = { 1, 1 },
116 [AV_PIX_FMT_RGB0] = { 1, 1 },
117 [AV_PIX_FMT_0BGR] = { 1, 1 },
118 [AV_PIX_FMT_BGR0] = { 1, 1 },
119 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
120 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
121 [AV_PIX_FMT_YUV440P] = { 1, 1 },
122 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
123 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
124 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
125 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
126 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
127 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
128 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
129 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
130 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
131 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
132 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
133 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
134 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
135 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
136 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
137 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
138 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
139 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
140 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
141 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
142 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
143 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
144 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
145 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
146 [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
147 [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
148 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
149 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
150 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
151 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
152 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
153 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
154 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
155 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
156 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
157 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
158 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
159 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
160 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
161 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
162 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
163 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
164 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
165 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
166 [AV_PIX_FMT_Y400A] = { 1, 0 },
167 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
168 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
169 [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
170 [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
171 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
172 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
173 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
174 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
175 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
176 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
177 [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
178 [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
179 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
180 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
181 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
182 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
183 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
184 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
185 [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
186 [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
187 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
188 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
189 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
190 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
191 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
192 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
193 [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
194 [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
195 [AV_PIX_FMT_GBRP] = { 1, 1 },
196 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
197 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
198 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
199 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
200 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
201 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
202 [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
203 [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
204 [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
205 [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
206 [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
207 [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
208 [AV_PIX_FMT_GBRAP] = { 1, 1 },
209 [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
210 [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
211 [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
212 [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
213 [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
214 [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
215 [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
216 [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
217 [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
218 [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
219 [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
220 [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
221 [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
222 [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
225 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
227 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
228 format_entries[pix_fmt].is_supported_in : 0;
231 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
233 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
234 format_entries[pix_fmt].is_supported_out : 0;
237 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
239 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
240 format_entries[pix_fmt].is_supported_endianness : 0;
243 #if FF_API_SWS_FORMAT_NAME
244 const char *sws_format_name(enum AVPixelFormat format)
246 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
250 return "Unknown format";
254 static double getSplineCoeff(double a, double b, double c, double d,
258 return ((d * dist + c) * dist + b) * dist + a;
260 return getSplineCoeff(0.0,
261 b + 2.0 * c + 3.0 * d,
263 -b - 3.0 * c - 6.0 * d,
267 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
270 pos = (128 << chr_subsample) - 128;
272 pos += 128; // relative to ideal left edge
273 return pos >> chr_subsample;
277 int flag; ///< flag associated to the algorithm
278 const char *description; ///< human-readable description
279 int size_factor; ///< size factor used when initing the filters
282 static const ScaleAlgorithm scale_algorithms[] = {
283 { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
284 { SWS_BICUBIC, "bicubic", 4 },
285 { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
286 { SWS_BILINEAR, "bilinear", 2 },
287 { SWS_FAST_BILINEAR, "fast bilinear", -1 },
288 { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
289 { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
290 { SWS_POINT, "nearest neighbor / point", -1 },
291 { SWS_SINC, "sinc", 20 /* infinite ;) */ },
292 { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
293 { SWS_X, "experimental", 8 },
296 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
297 int *outFilterSize, int xInc, int srcW,
298 int dstW, int filterAlign, int one,
299 int flags, int cpu_flags,
300 SwsVector *srcFilter, SwsVector *dstFilter,
301 double param[2], int srcPos, int dstPos)
307 int64_t *filter = NULL;
308 int64_t *filter2 = NULL;
309 const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
312 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
314 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
315 FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
317 if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
320 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
321 dstW, sizeof(*filter) * filterSize, fail);
323 for (i = 0; i < dstW; i++) {
324 filter[i * filterSize] = fone;
327 } else if (flags & SWS_POINT) { // lame looking point sampling mode
331 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
332 dstW, sizeof(*filter) * filterSize, fail);
334 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
335 for (i = 0; i < dstW; i++) {
336 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
338 (*filterPos)[i] = xx;
342 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
343 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
347 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
348 dstW, sizeof(*filter) * filterSize, fail);
350 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
351 for (i = 0; i < dstW; i++) {
352 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
355 (*filterPos)[i] = xx;
356 // bilinear upscale / linear interpolate / area averaging
357 for (j = 0; j < filterSize; j++) {
358 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
361 filter[i * filterSize + j] = coeff;
370 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
371 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
372 sizeFactor = scale_algorithms[i].size_factor;
376 if (flags & SWS_LANCZOS)
377 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
378 av_assert0(sizeFactor > 0);
381 filterSize = 1 + sizeFactor; // upscale
383 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
385 filterSize = FFMIN(filterSize, srcW - 2);
386 filterSize = FFMAX(filterSize, 1);
388 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
389 dstW, sizeof(*filter) * filterSize, fail);
391 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
392 for (i = 0; i < dstW; i++) {
393 int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
395 (*filterPos)[i] = xx;
396 for (j = 0; j < filterSize; j++) {
397 int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
403 floatd = d * (1.0 / (1 << 30));
405 if (flags & SWS_BICUBIC) {
406 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
407 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
409 if (d >= 1LL << 31) {
412 int64_t dd = (d * d) >> 30;
413 int64_t ddd = (dd * d) >> 30;
416 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
417 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
418 (6 * (1 << 24) - 2 * B) * (1 << 30);
420 coeff = (-B - 6 * C) * ddd +
421 (6 * B + 30 * C) * dd +
422 (-12 * B - 48 * C) * d +
423 (8 * B + 24 * C) * (1 << 30);
425 coeff /= (1LL<<54)/fone;
428 else if (flags & SWS_X) {
429 double p = param ? param * 0.01 : 0.3;
430 coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
431 coeff *= pow(2.0, -p * d * d);
434 else if (flags & SWS_X) {
435 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
439 c = cos(floatd * M_PI);
446 coeff = (c * 0.5 + 0.5) * fone;
447 } else if (flags & SWS_AREA) {
448 int64_t d2 = d - (1 << 29);
449 if (d2 * xInc < -(1LL << (29 + 16)))
450 coeff = 1.0 * (1LL << (30 + 16));
451 else if (d2 * xInc < (1LL << (29 + 16)))
452 coeff = -d2 * xInc + (1LL << (29 + 16));
455 coeff *= fone >> (30 + 16);
456 } else if (flags & SWS_GAUSS) {
457 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
458 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
459 } else if (flags & SWS_SINC) {
460 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
461 } else if (flags & SWS_LANCZOS) {
462 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
463 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
464 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
467 } else if (flags & SWS_BILINEAR) {
468 coeff = (1 << 30) - d;
472 } else if (flags & SWS_SPLINE) {
473 double p = -2.196152422706632;
474 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
479 filter[i * filterSize + j] = coeff;
482 xDstInSrc += 2 * xInc;
486 /* apply src & dst Filter to filter -> filter2
489 av_assert0(filterSize > 0);
490 filter2Size = filterSize;
492 filter2Size += srcFilter->length - 1;
494 filter2Size += dstFilter->length - 1;
495 av_assert0(filter2Size > 0);
496 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
498 for (i = 0; i < dstW; i++) {
502 for (k = 0; k < srcFilter->length; k++) {
503 for (j = 0; j < filterSize; j++)
504 filter2[i * filter2Size + k + j] +=
505 srcFilter->coeff[k] * filter[i * filterSize + j];
508 for (j = 0; j < filterSize; j++)
509 filter2[i * filter2Size + j] = filter[i * filterSize + j];
513 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
517 /* try to reduce the filter-size (step1 find size and shift left) */
518 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
520 for (i = dstW - 1; i >= 0; i--) {
521 int min = filter2Size;
523 int64_t cutOff = 0.0;
525 /* get rid of near zero elements on the left by shifting left */
526 for (j = 0; j < filter2Size; j++) {
528 cutOff += FFABS(filter2[i * filter2Size]);
530 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
533 /* preserve monotonicity because the core can't handle the
534 * filter otherwise */
535 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
538 // move filter coefficients left
539 for (k = 1; k < filter2Size; k++)
540 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
541 filter2[i * filter2Size + k - 1] = 0;
546 /* count near zeros on the right */
547 for (j = filter2Size - 1; j > 0; j--) {
548 cutOff += FFABS(filter2[i * filter2Size + j]);
550 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
555 if (min > minFilterSize)
559 if (PPC_ALTIVEC(cpu_flags)) {
560 // we can handle the special case 4, so we don't want to go the full 8
561 if (minFilterSize < 5)
564 /* We really don't want to waste our time doing useless computation, so
565 * fall back on the scalar C code for very small filters.
566 * Vectorizing is worth it only if you have a decent-sized vector. */
567 if (minFilterSize < 3)
571 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
572 // special case for unscaled vertical filtering
573 if (minFilterSize == 1 && filterAlign == 2)
577 av_assert0(minFilterSize > 0);
578 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
579 av_assert0(filterSize > 0);
580 filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
583 if (filterSize >= MAX_FILTER_SIZE * 16 /
584 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
585 av_log(NULL, AV_LOG_ERROR, "sws: filterSize %d is too large, try less extreme scaling or increase MAX_FILTER_SIZE and recompile\n",
586 FF_CEIL_RSHIFT((filterSize+1) * ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16), 4));
589 *outFilterSize = filterSize;
591 if (flags & SWS_PRINT_INFO)
592 av_log(NULL, AV_LOG_VERBOSE,
593 "SwScaler: reducing / aligning filtersize %d -> %d\n",
594 filter2Size, filterSize);
595 /* try to reduce the filter-size (step2 reduce it) */
596 for (i = 0; i < dstW; i++) {
599 for (j = 0; j < filterSize; j++) {
600 if (j >= filter2Size)
601 filter[i * filterSize + j] = 0;
603 filter[i * filterSize + j] = filter2[i * filter2Size + j];
604 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
605 filter[i * filterSize + j] = 0;
609 // FIXME try to align filterPos if possible
612 for (i = 0; i < dstW; i++) {
614 if ((*filterPos)[i] < 0) {
615 // move filter coefficients left to compensate for filterPos
616 for (j = 1; j < filterSize; j++) {
617 int left = FFMAX(j + (*filterPos)[i], 0);
618 filter[i * filterSize + left] += filter[i * filterSize + j];
619 filter[i * filterSize + j] = 0;
624 if ((*filterPos)[i] + filterSize > srcW) {
625 int shift = (*filterPos)[i] + filterSize - srcW;
626 // move filter coefficients right to compensate for filterPos
627 for (j = filterSize - 2; j >= 0; j--) {
628 int right = FFMIN(j + shift, filterSize - 1);
629 filter[i * filterSize + right] += filter[i * filterSize + j];
630 filter[i * filterSize + j] = 0;
632 (*filterPos)[i]= srcW - filterSize;
636 // Note the +1 is for the MMX scaler which reads over the end
637 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
638 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
639 (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
641 /* normalize & store in outFilter */
642 for (i = 0; i < dstW; i++) {
647 for (j = 0; j < filterSize; j++) {
648 sum += filter[i * filterSize + j];
650 sum = (sum + one / 2) / one;
652 av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
655 for (j = 0; j < *outFilterSize; j++) {
656 int64_t v = filter[i * filterSize + j] + error;
657 int intV = ROUNDED_DIV(v, sum);
658 (*outFilter)[i * (*outFilterSize) + j] = intV;
659 error = v - intV * sum;
663 (*filterPos)[dstW + 0] =
664 (*filterPos)[dstW + 1] =
665 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
666 * read over the end */
667 for (i = 0; i < *outFilterSize; i++) {
668 int k = (dstW - 1) * (*outFilterSize) + i;
669 (*outFilter)[k + 1 * (*outFilterSize)] =
670 (*outFilter)[k + 2 * (*outFilterSize)] =
671 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
678 av_log(NULL, AV_LOG_ERROR, "sws: initFilter failed\n");
684 #if HAVE_MMXEXT_INLINE
685 static av_cold int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
686 int16_t *filter, int32_t *filterPos,
690 x86_reg imm8OfPShufW1A;
691 x86_reg imm8OfPShufW2A;
692 x86_reg fragmentLengthA;
694 x86_reg imm8OfPShufW1B;
695 x86_reg imm8OfPShufW2B;
696 x86_reg fragmentLengthB;
701 // create an optimized horizontal scaling routine
702 /* This scaler is made of runtime-generated MMXEXT code using specially tuned
703 * pshufw instructions. For every four output pixels, if four input pixels
704 * are enough for the fast bilinear scaling, then a chunk of fragmentB is
705 * used. If five input pixels are needed, then a chunk of fragmentA is used.
714 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
715 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
716 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
717 "punpcklbw %%mm7, %%mm1 \n\t"
718 "punpcklbw %%mm7, %%mm0 \n\t"
719 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
721 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
723 "psubw %%mm1, %%mm0 \n\t"
724 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
725 "pmullw %%mm3, %%mm0 \n\t"
726 "psllw $7, %%mm1 \n\t"
727 "paddw %%mm1, %%mm0 \n\t"
729 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
731 "add $8, %%"REG_a" \n\t"
735 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
736 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
737 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
742 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
746 : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
747 "=r" (fragmentLengthA)
754 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
755 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
756 "punpcklbw %%mm7, %%mm0 \n\t"
757 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
759 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
761 "psubw %%mm1, %%mm0 \n\t"
762 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
763 "pmullw %%mm3, %%mm0 \n\t"
764 "psllw $7, %%mm1 \n\t"
765 "paddw %%mm1, %%mm0 \n\t"
767 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
769 "add $8, %%"REG_a" \n\t"
773 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
774 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
775 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
780 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
784 : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
785 "=r" (fragmentLengthB)
788 xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
791 for (i = 0; i < dstW / numSplits; i++) {
796 int b = ((xpos + xInc) >> 16) - xx;
797 int c = ((xpos + xInc * 2) >> 16) - xx;
798 int d = ((xpos + xInc * 3) >> 16) - xx;
799 int inc = (d + 1 < 4);
800 uint8_t *fragment = inc ? fragmentB : fragmentA;
801 x86_reg imm8OfPShufW1 = inc ? imm8OfPShufW1B : imm8OfPShufW1A;
802 x86_reg imm8OfPShufW2 = inc ? imm8OfPShufW2B : imm8OfPShufW2A;
803 x86_reg fragmentLength = inc ? fragmentLengthB : fragmentLengthA;
804 int maxShift = 3 - (d + inc);
808 filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
809 filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
810 filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
811 filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
812 filterPos[i / 2] = xx;
814 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
816 filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
820 filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
824 if (i + 4 - inc >= dstW)
825 shift = maxShift; // avoid overread
826 else if ((filterPos[i / 2] & 3) <= maxShift)
827 shift = filterPos[i / 2] & 3; // align
829 if (shift && i >= shift) {
830 filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
831 filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
832 filterPos[i / 2] -= shift;
836 fragmentPos += fragmentLength;
839 filterCode[fragmentPos] = RET;
844 filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
846 return fragmentPos + 1;
848 #endif /* HAVE_MMXEXT_INLINE */
850 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
852 int64_t W, V, Z, Cy, Cu, Cv;
853 int64_t vr = table[0];
854 int64_t ub = table[1];
855 int64_t ug = -table[2];
856 int64_t vg = -table[3];
859 uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
861 static const int8_t map[] = {
862 BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
863 RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
864 RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
865 BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
866 BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
867 RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
868 RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
869 BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
870 BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
871 RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
872 RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
873 BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
874 RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
875 BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
876 GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
877 -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
878 RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
879 BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
880 GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
881 -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
882 RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
883 BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
884 GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
885 -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
886 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
887 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
888 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
889 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
890 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
891 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
892 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
893 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
894 BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
895 BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
896 BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
899 dstRange = 0; //FIXME range = 1 is handled elsewhere
909 W = ROUNDED_DIV(ONE*ONE*ug, ub);
910 V = ROUNDED_DIV(ONE*ONE*vg, vr);
913 Cy = ROUNDED_DIV(cy*Z, ONE);
914 Cu = ROUNDED_DIV(ub*Z, ONE);
915 Cv = ROUNDED_DIV(vr*Z, ONE);
917 c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
918 c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
919 c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
921 c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
922 c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
923 c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
925 c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
926 c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
927 c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
929 if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
930 c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
931 c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
932 c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
933 c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
934 c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
935 c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
936 c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
937 c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
938 c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
940 for(i=0; i<FF_ARRAY_ELEMS(map); i++)
941 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
944 static void fill_xyztables(struct SwsContext *c)
947 double xyzgamma = XYZ_GAMMA;
948 double rgbgamma = 1.0 / RGB_GAMMA;
949 double xyzgammainv = 1.0 / XYZ_GAMMA;
950 double rgbgammainv = RGB_GAMMA;
951 static const int16_t xyz2rgb_matrix[3][4] = {
952 {13270, -6295, -2041},
954 { 228, -835, 4329} };
955 static const int16_t rgb2xyz_matrix[3][4] = {
959 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
961 memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
962 memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
963 c->xyzgamma = xyzgamma_tab;
964 c->rgbgamma = rgbgamma_tab;
965 c->xyzgammainv = xyzgammainv_tab;
966 c->rgbgammainv = rgbgammainv_tab;
968 if (rgbgamma_tab[4095])
971 /* set gamma vectors */
972 for (i = 0; i < 4096; i++) {
973 xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
974 rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
975 xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
976 rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
980 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
981 int srcRange, const int table[4], int dstRange,
982 int brightness, int contrast, int saturation)
984 const AVPixFmtDescriptor *desc_dst;
985 const AVPixFmtDescriptor *desc_src;
987 memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
988 memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
991 desc_dst = av_pix_fmt_desc_get(c->dstFormat);
992 desc_src = av_pix_fmt_desc_get(c->srcFormat);
994 if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
996 if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
999 c->brightness = brightness;
1000 c->contrast = contrast;
1001 c->saturation = saturation;
1002 if (c->srcRange != srcRange || c->dstRange != dstRange)
1004 c->srcRange = srcRange;
1005 c->dstRange = dstRange;
1007 //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
1008 //and what we have in ticket 2939 looks better with this check
1009 if (need_reinit && c->srcBpc == 8)
1010 ff_sws_init_range_convert(c);
1012 if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
1015 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1016 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1018 if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
1019 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
1020 contrast, saturation);
1024 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
1025 contrast, saturation);
1028 fill_rgb2yuv_table(c, table, dstRange);
1033 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
1034 int *srcRange, int **table, int *dstRange,
1035 int *brightness, int *contrast, int *saturation)
1040 *inv_table = c->srcColorspaceTable;
1041 *table = c->dstColorspaceTable;
1042 *srcRange = c->srcRange;
1043 *dstRange = c->dstRange;
1044 *brightness = c->brightness;
1045 *contrast = c->contrast;
1046 *saturation = c->saturation;
1051 static int handle_jpeg(enum AVPixelFormat *format)
1054 case AV_PIX_FMT_YUVJ420P:
1055 *format = AV_PIX_FMT_YUV420P;
1057 case AV_PIX_FMT_YUVJ411P:
1058 *format = AV_PIX_FMT_YUV411P;
1060 case AV_PIX_FMT_YUVJ422P:
1061 *format = AV_PIX_FMT_YUV422P;
1063 case AV_PIX_FMT_YUVJ444P:
1064 *format = AV_PIX_FMT_YUV444P;
1066 case AV_PIX_FMT_YUVJ440P:
1067 *format = AV_PIX_FMT_YUV440P;
1069 case AV_PIX_FMT_GRAY8:
1070 case AV_PIX_FMT_GRAY16LE:
1071 case AV_PIX_FMT_GRAY16BE:
1078 static int handle_0alpha(enum AVPixelFormat *format)
1081 case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
1082 case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
1083 case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
1084 case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
1089 static int handle_xyz(enum AVPixelFormat *format)
1092 case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1093 case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1098 static void handle_formats(SwsContext *c)
1100 c->src0Alpha |= handle_0alpha(&c->srcFormat);
1101 c->dst0Alpha |= handle_0alpha(&c->dstFormat);
1102 c->srcXYZ |= handle_xyz(&c->srcFormat);
1103 c->dstXYZ |= handle_xyz(&c->dstFormat);
1104 if (c->srcXYZ || c->dstXYZ)
1108 SwsContext *sws_alloc_context(void)
1110 SwsContext *c = av_mallocz(sizeof(SwsContext));
1112 av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
1115 c->av_class = &sws_context_class;
1116 av_opt_set_defaults(c);
1122 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
1123 SwsFilter *dstFilter)
1126 int usesVFilter, usesHFilter;
1128 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1133 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1134 int flags, cpu_flags;
1135 enum AVPixelFormat srcFormat = c->srcFormat;
1136 enum AVPixelFormat dstFormat = c->dstFormat;
1137 const AVPixFmtDescriptor *desc_src;
1138 const AVPixFmtDescriptor *desc_dst;
1140 cpu_flags = av_get_cpu_flags();
1146 unscaled = (srcW == dstW && srcH == dstH);
1148 c->srcRange |= handle_jpeg(&c->srcFormat);
1149 c->dstRange |= handle_jpeg(&c->dstFormat);
1151 if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
1152 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
1154 if (!c->contrast && !c->saturation && !c->dstFormatBpp)
1155 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1156 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1157 c->dstRange, 0, 1 << 16, 1 << 16);
1160 srcFormat = c->srcFormat;
1161 dstFormat = c->dstFormat;
1162 desc_src = av_pix_fmt_desc_get(srcFormat);
1163 desc_dst = av_pix_fmt_desc_get(dstFormat);
1165 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1166 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1167 if (!sws_isSupportedInput(srcFormat)) {
1168 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1169 av_get_pix_fmt_name(srcFormat));
1170 return AVERROR(EINVAL);
1172 if (!sws_isSupportedOutput(dstFormat)) {
1173 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1174 av_get_pix_fmt_name(dstFormat));
1175 return AVERROR(EINVAL);
1179 i = flags & (SWS_POINT |
1191 /* provide a default scaler if not set by caller */
1193 if (dstW < srcW && dstH < srcH)
1194 flags |= SWS_BICUBIC;
1195 else if (dstW > srcW && dstH > srcH)
1196 flags |= SWS_BICUBIC;
1198 flags |= SWS_BICUBIC;
1200 } else if (i & (i - 1)) {
1201 av_log(c, AV_LOG_ERROR,
1202 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1203 return AVERROR(EINVAL);
1206 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1207 /* FIXME check if these are enough and try to lower them after
1208 * fixing the relevant parts of the code */
1209 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1210 srcW, srcH, dstW, dstH);
1211 return AVERROR(EINVAL);
1215 dstFilter = &dummyFilter;
1217 srcFilter = &dummyFilter;
1219 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1220 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1221 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1222 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1223 c->vRounder = 4 * 0x0001000100010001ULL;
1225 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1226 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1227 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1228 (dstFilter->chrV && dstFilter->chrV->length > 1);
1229 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1230 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1231 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1232 (dstFilter->chrH && dstFilter->chrH->length > 1);
1234 av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1235 av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1237 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1239 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1240 flags |= SWS_FULL_CHR_H_INT;
1244 if ( c->chrSrcHSubSample == 0
1245 && c->chrSrcVSubSample == 0
1246 && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1247 && !(c->flags & SWS_FAST_BILINEAR)
1249 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1250 flags |= SWS_FULL_CHR_H_INT;
1255 if (c->dither == SWS_DITHER_AUTO) {
1256 if (flags & SWS_ERROR_DIFFUSION)
1257 c->dither = SWS_DITHER_ED;
1260 if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1261 dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1262 dstFormat == AV_PIX_FMT_BGR8 ||
1263 dstFormat == AV_PIX_FMT_RGB8) {
1264 if (c->dither == SWS_DITHER_AUTO)
1265 c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1266 if (!(flags & SWS_FULL_CHR_H_INT)) {
1267 if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
1268 av_log(c, AV_LOG_DEBUG,
1269 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1270 av_get_pix_fmt_name(dstFormat));
1271 flags |= SWS_FULL_CHR_H_INT;
1275 if (flags & SWS_FULL_CHR_H_INT) {
1276 if (c->dither == SWS_DITHER_BAYER) {
1277 av_log(c, AV_LOG_DEBUG,
1278 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1279 av_get_pix_fmt_name(dstFormat));
1280 c->dither = SWS_DITHER_ED;
1284 if (isPlanarRGB(dstFormat)) {
1285 if (!(flags & SWS_FULL_CHR_H_INT)) {
1286 av_log(c, AV_LOG_DEBUG,
1287 "%s output is not supported with half chroma resolution, switching to full\n",
1288 av_get_pix_fmt_name(dstFormat));
1289 flags |= SWS_FULL_CHR_H_INT;
1294 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1295 * chroma interpolation */
1296 if (flags & SWS_FULL_CHR_H_INT &&
1297 isAnyRGB(dstFormat) &&
1298 !isPlanarRGB(dstFormat) &&
1299 dstFormat != AV_PIX_FMT_RGBA &&
1300 dstFormat != AV_PIX_FMT_ARGB &&
1301 dstFormat != AV_PIX_FMT_BGRA &&
1302 dstFormat != AV_PIX_FMT_ABGR &&
1303 dstFormat != AV_PIX_FMT_RGB24 &&
1304 dstFormat != AV_PIX_FMT_BGR24 &&
1305 dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1306 dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1307 dstFormat != AV_PIX_FMT_BGR8 &&
1308 dstFormat != AV_PIX_FMT_RGB8
1310 av_log(c, AV_LOG_WARNING,
1311 "full chroma interpolation for destination format '%s' not yet implemented\n",
1312 av_get_pix_fmt_name(dstFormat));
1313 flags &= ~SWS_FULL_CHR_H_INT;
1316 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1317 c->chrDstHSubSample = 1;
1319 // drop some chroma lines if the user wants it
1320 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1321 SWS_SRC_V_CHR_DROP_SHIFT;
1322 c->chrSrcVSubSample += c->vChrDrop;
1324 /* drop every other pixel for chroma calculation unless user
1325 * wants full chroma */
1326 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1327 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1328 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1329 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1330 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1331 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1332 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1333 srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1334 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1335 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1336 (flags & SWS_FAST_BILINEAR)))
1337 c->chrSrcHSubSample = 1;
1339 // Note the FF_CEIL_RSHIFT is so that we always round toward +inf.
1340 c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1341 c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1342 c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1343 c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1345 FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1347 c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
1350 c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
1353 if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1355 if (c->dstBpc == 16)
1358 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1359 c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1360 c->chrDstW >= c->chrSrcW &&
1362 if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1364 && (flags & SWS_FAST_BILINEAR)) {
1365 if (flags & SWS_PRINT_INFO)
1366 av_log(c, AV_LOG_INFO,
1367 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1369 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1370 c->canMMXEXTBeUsed = 0;
1372 c->canMMXEXTBeUsed = 0;
1374 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1375 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1377 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1378 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1380 * n-2 is the last chrominance sample available.
1381 * This is not perfect, but no one should notice the difference, the more
1382 * correct variant would be like the vertical one, but that would require
1383 * some special code for the first and last pixel */
1384 if (flags & SWS_FAST_BILINEAR) {
1385 if (c->canMMXEXTBeUsed) {
1389 // we don't use the x86 asm scaler if MMX is available
1390 else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1391 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1392 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1396 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1398 /* precalculate horizontal scaler filter coefficients */
1400 #if HAVE_MMXEXT_INLINE
1401 // can't downscale !!!
1402 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1403 c->lumMmxextFilterCodeSize = init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1405 c->chrMmxextFilterCodeSize = init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1406 NULL, NULL, NULL, 4);
1409 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1410 PROT_READ | PROT_WRITE,
1411 MAP_PRIVATE | MAP_ANONYMOUS,
1413 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1414 PROT_READ | PROT_WRITE,
1415 MAP_PRIVATE | MAP_ANONYMOUS,
1417 #elif HAVE_VIRTUALALLOC
1418 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1419 c->lumMmxextFilterCodeSize,
1421 PAGE_EXECUTE_READWRITE);
1422 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1423 c->chrMmxextFilterCodeSize,
1425 PAGE_EXECUTE_READWRITE);
1427 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1428 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1431 #ifdef MAP_ANONYMOUS
1432 if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1434 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1437 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1438 return AVERROR(ENOMEM);
1441 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1442 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1443 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1444 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1446 init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1447 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1448 init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1449 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1452 if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1453 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1454 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1459 #endif /* HAVE_MMXEXT_INLINE */
1461 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1462 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1464 if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1465 &c->hLumFilterSize, c->lumXInc,
1466 srcW, dstW, filterAlign, 1 << 14,
1467 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1468 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1470 get_local_pos(c, 0, 0, 0),
1471 get_local_pos(c, 0, 0, 0)) < 0)
1473 if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1474 &c->hChrFilterSize, c->chrXInc,
1475 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1476 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1477 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1479 get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1480 get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0)) < 0)
1483 } // initialize horizontal stuff
1485 /* precalculate vertical scaler filter coefficients */
1487 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1488 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1490 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1491 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1492 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1493 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1495 get_local_pos(c, 0, 0, 1),
1496 get_local_pos(c, 0, 0, 1)) < 0)
1498 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1499 c->chrYInc, c->chrSrcH, c->chrDstH,
1500 filterAlign, (1 << 12),
1501 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1502 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1504 get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1505 get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1)) < 0)
1510 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1511 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1513 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1515 short *p = (short *)&c->vYCoeffsBank[i];
1516 for (j = 0; j < 8; j++)
1517 p[j] = c->vLumFilter[i];
1520 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1522 short *p = (short *)&c->vCCoeffsBank[i];
1523 for (j = 0; j < 8; j++)
1524 p[j] = c->vChrFilter[i];
1529 // calculate buffer sizes so that they won't run out while handling these damn slices
1530 c->vLumBufSize = c->vLumFilterSize;
1531 c->vChrBufSize = c->vChrFilterSize;
1532 for (i = 0; i < dstH; i++) {
1533 int chrI = (int64_t)i * c->chrDstH / dstH;
1534 int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1535 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1536 << c->chrSrcVSubSample));
1538 nextSlice >>= c->chrSrcVSubSample;
1539 nextSlice <<= c->chrSrcVSubSample;
1540 if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1541 c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1542 if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1543 (nextSlice >> c->chrSrcVSubSample))
1544 c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1545 c->vChrFilterPos[chrI];
1548 for (i = 0; i < 4; i++)
1549 FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1551 /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1552 * need to allocate several megabytes to handle all possible cases) */
1553 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1554 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1555 FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1556 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1557 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1558 /* Note we need at least one pixel more at the end because of the MMX code
1559 * (just in case someone wants to replace the 4000/8000). */
1560 /* align at 16 bytes for AltiVec */
1561 for (i = 0; i < c->vLumBufSize; i++) {
1562 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1563 dst_stride + 16, fail);
1564 c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1566 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1567 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1568 c->uv_offx2 = dst_stride + 16;
1569 for (i = 0; i < c->vChrBufSize; i++) {
1570 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1571 dst_stride * 2 + 32, fail);
1572 c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1573 c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1574 = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1576 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1577 for (i = 0; i < c->vLumBufSize; i++) {
1578 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1579 dst_stride + 16, fail);
1580 c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1583 // try to avoid drawing green stuff between the right end and the stride end
1584 for (i = 0; i < c->vChrBufSize; i++)
1585 if(desc_dst->comp[0].depth_minus1 == 15){
1586 av_assert0(c->dstBpc > 14);
1587 for(j=0; j<dst_stride/2+1; j++)
1588 ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1590 for(j=0; j<dst_stride+1; j++)
1591 ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1593 av_assert0(c->chrDstH <= dstH);
1595 if (flags & SWS_PRINT_INFO) {
1596 const char *scaler = NULL, *cpucaps;
1598 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1599 if (flags & scale_algorithms[i].flag) {
1600 scaler = scale_algorithms[i].description;
1605 scaler = "ehh flags invalid?!";
1606 av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1608 av_get_pix_fmt_name(srcFormat),
1610 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1611 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1612 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1617 av_get_pix_fmt_name(dstFormat));
1619 if (INLINE_MMXEXT(cpu_flags))
1621 else if (INLINE_AMD3DNOW(cpu_flags))
1623 else if (INLINE_MMX(cpu_flags))
1625 else if (PPC_ALTIVEC(cpu_flags))
1626 cpucaps = "AltiVec";
1630 av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1632 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1633 av_log(c, AV_LOG_DEBUG,
1634 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1635 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1636 av_log(c, AV_LOG_DEBUG,
1637 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1638 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1639 c->chrXInc, c->chrYInc);
1642 /* unscaled special cases */
1643 if (unscaled && !usesHFilter && !usesVFilter &&
1644 (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1645 ff_get_unscaled_swscale(c);
1648 if (flags & SWS_PRINT_INFO)
1649 av_log(c, AV_LOG_INFO,
1650 "using unscaled %s -> %s special converter\n",
1651 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1656 c->swscale = ff_getSwsFunc(c);
1658 fail: // FIXME replace things by appropriate error codes
1662 #if FF_API_SWS_GETCONTEXT
1663 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1664 int dstW, int dstH, enum AVPixelFormat dstFormat,
1665 int flags, SwsFilter *srcFilter,
1666 SwsFilter *dstFilter, const double *param)
1670 if (!(c = sws_alloc_context()))
1678 c->srcFormat = srcFormat;
1679 c->dstFormat = dstFormat;
1682 c->param[0] = param[0];
1683 c->param[1] = param[1];
1686 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1695 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1696 float lumaSharpen, float chromaSharpen,
1697 float chromaHShift, float chromaVShift,
1700 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1704 if (lumaGBlur != 0.0) {
1705 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1706 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1708 filter->lumH = sws_getIdentityVec();
1709 filter->lumV = sws_getIdentityVec();
1712 if (chromaGBlur != 0.0) {
1713 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1714 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1716 filter->chrH = sws_getIdentityVec();
1717 filter->chrV = sws_getIdentityVec();
1720 if (chromaSharpen != 0.0) {
1721 SwsVector *id = sws_getIdentityVec();
1722 sws_scaleVec(filter->chrH, -chromaSharpen);
1723 sws_scaleVec(filter->chrV, -chromaSharpen);
1724 sws_addVec(filter->chrH, id);
1725 sws_addVec(filter->chrV, id);
1729 if (lumaSharpen != 0.0) {
1730 SwsVector *id = sws_getIdentityVec();
1731 sws_scaleVec(filter->lumH, -lumaSharpen);
1732 sws_scaleVec(filter->lumV, -lumaSharpen);
1733 sws_addVec(filter->lumH, id);
1734 sws_addVec(filter->lumV, id);
1738 if (chromaHShift != 0.0)
1739 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1741 if (chromaVShift != 0.0)
1742 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1744 sws_normalizeVec(filter->chrH, 1.0);
1745 sws_normalizeVec(filter->chrV, 1.0);
1746 sws_normalizeVec(filter->lumH, 1.0);
1747 sws_normalizeVec(filter->lumV, 1.0);
1750 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1752 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1757 SwsVector *sws_allocVec(int length)
1761 if(length <= 0 || length > INT_MAX/ sizeof(double))
1764 vec = av_malloc(sizeof(SwsVector));
1767 vec->length = length;
1768 vec->coeff = av_malloc(sizeof(double) * length);
1774 SwsVector *sws_getGaussianVec(double variance, double quality)
1776 const int length = (int)(variance * quality + 0.5) | 1;
1778 double middle = (length - 1) * 0.5;
1781 if(variance < 0 || quality < 0)
1784 vec = sws_allocVec(length);
1789 for (i = 0; i < length; i++) {
1790 double dist = i - middle;
1791 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1792 sqrt(2 * variance * M_PI);
1795 sws_normalizeVec(vec, 1.0);
1800 SwsVector *sws_getConstVec(double c, int length)
1803 SwsVector *vec = sws_allocVec(length);
1808 for (i = 0; i < length; i++)
1814 SwsVector *sws_getIdentityVec(void)
1816 return sws_getConstVec(1.0, 1);
1819 static double sws_dcVec(SwsVector *a)
1824 for (i = 0; i < a->length; i++)
1830 void sws_scaleVec(SwsVector *a, double scalar)
1834 for (i = 0; i < a->length; i++)
1835 a->coeff[i] *= scalar;
1838 void sws_normalizeVec(SwsVector *a, double height)
1840 sws_scaleVec(a, height / sws_dcVec(a));
1843 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1845 int length = a->length + b->length - 1;
1847 SwsVector *vec = sws_getConstVec(0.0, length);
1852 for (i = 0; i < a->length; i++) {
1853 for (j = 0; j < b->length; j++) {
1854 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1861 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1863 int length = FFMAX(a->length, b->length);
1865 SwsVector *vec = sws_getConstVec(0.0, length);
1870 for (i = 0; i < a->length; i++)
1871 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1872 for (i = 0; i < b->length; i++)
1873 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1878 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1880 int length = FFMAX(a->length, b->length);
1882 SwsVector *vec = sws_getConstVec(0.0, length);
1887 for (i = 0; i < a->length; i++)
1888 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1889 for (i = 0; i < b->length; i++)
1890 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1895 /* shift left / or right if "shift" is negative */
1896 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1898 int length = a->length + FFABS(shift) * 2;
1900 SwsVector *vec = sws_getConstVec(0.0, length);
1905 for (i = 0; i < a->length; i++) {
1906 vec->coeff[i + (length - 1) / 2 -
1907 (a->length - 1) / 2 - shift] = a->coeff[i];
1913 void sws_shiftVec(SwsVector *a, int shift)
1915 SwsVector *shifted = sws_getShiftedVec(a, shift);
1917 a->coeff = shifted->coeff;
1918 a->length = shifted->length;
1922 void sws_addVec(SwsVector *a, SwsVector *b)
1924 SwsVector *sum = sws_sumVec(a, b);
1926 a->coeff = sum->coeff;
1927 a->length = sum->length;
1931 void sws_subVec(SwsVector *a, SwsVector *b)
1933 SwsVector *diff = sws_diffVec(a, b);
1935 a->coeff = diff->coeff;
1936 a->length = diff->length;
1940 void sws_convVec(SwsVector *a, SwsVector *b)
1942 SwsVector *conv = sws_getConvVec(a, b);
1944 a->coeff = conv->coeff;
1945 a->length = conv->length;
1949 SwsVector *sws_cloneVec(SwsVector *a)
1951 SwsVector *vec = sws_allocVec(a->length);
1956 memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
1961 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1968 for (i = 0; i < a->length; i++)
1969 if (a->coeff[i] > max)
1972 for (i = 0; i < a->length; i++)
1973 if (a->coeff[i] < min)
1978 for (i = 0; i < a->length; i++) {
1979 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1980 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1982 av_log(log_ctx, log_level, " ");
1983 av_log(log_ctx, log_level, "|\n");
1987 void sws_freeVec(SwsVector *a)
1991 av_freep(&a->coeff);
1996 void sws_freeFilter(SwsFilter *filter)
2001 sws_freeVec(filter->lumH);
2002 sws_freeVec(filter->lumV);
2003 sws_freeVec(filter->chrH);
2004 sws_freeVec(filter->chrV);
2008 void sws_freeContext(SwsContext *c)
2015 for (i = 0; i < c->vLumBufSize; i++)
2016 av_freep(&c->lumPixBuf[i]);
2017 av_freep(&c->lumPixBuf);
2020 if (c->chrUPixBuf) {
2021 for (i = 0; i < c->vChrBufSize; i++)
2022 av_freep(&c->chrUPixBuf[i]);
2023 av_freep(&c->chrUPixBuf);
2024 av_freep(&c->chrVPixBuf);
2027 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
2028 for (i = 0; i < c->vLumBufSize; i++)
2029 av_freep(&c->alpPixBuf[i]);
2030 av_freep(&c->alpPixBuf);
2033 for (i = 0; i < 4; i++)
2034 av_freep(&c->dither_error[i]);
2036 av_freep(&c->vLumFilter);
2037 av_freep(&c->vChrFilter);
2038 av_freep(&c->hLumFilter);
2039 av_freep(&c->hChrFilter);
2041 av_freep(&c->vYCoeffsBank);
2042 av_freep(&c->vCCoeffsBank);
2045 av_freep(&c->vLumFilterPos);
2046 av_freep(&c->vChrFilterPos);
2047 av_freep(&c->hLumFilterPos);
2048 av_freep(&c->hChrFilterPos);
2052 if (c->lumMmxextFilterCode)
2053 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
2054 if (c->chrMmxextFilterCode)
2055 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
2056 #elif HAVE_VIRTUALALLOC
2057 if (c->lumMmxextFilterCode)
2058 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
2059 if (c->chrMmxextFilterCode)
2060 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
2062 av_free(c->lumMmxextFilterCode);
2063 av_free(c->chrMmxextFilterCode);
2065 c->lumMmxextFilterCode = NULL;
2066 c->chrMmxextFilterCode = NULL;
2067 #endif /* HAVE_MMX_INLINE */
2069 av_freep(&c->yuvTable);
2070 av_freep(&c->formatConvBuffer);
2075 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
2076 int srcH, enum AVPixelFormat srcFormat,
2078 enum AVPixelFormat dstFormat, int flags,
2079 SwsFilter *srcFilter,
2080 SwsFilter *dstFilter,
2081 const double *param)
2083 static const double default_param[2] = { SWS_PARAM_DEFAULT,
2084 SWS_PARAM_DEFAULT };
2087 param = default_param;
2090 (context->srcW != srcW ||
2091 context->srcH != srcH ||
2092 context->srcFormat != srcFormat ||
2093 context->dstW != dstW ||
2094 context->dstH != dstH ||
2095 context->dstFormat != dstFormat ||
2096 context->flags != flags ||
2097 context->param[0] != param[0] ||
2098 context->param[1] != param[1])) {
2099 sws_freeContext(context);
2104 if (!(context = sws_alloc_context()))
2106 context->srcW = srcW;
2107 context->srcH = srcH;
2108 context->srcFormat = srcFormat;
2109 context->dstW = dstW;
2110 context->dstH = dstH;
2111 context->dstFormat = dstFormat;
2112 context->flags = flags;
2113 context->param[0] = param[0];
2114 context->param[1] = param[1];
2115 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2116 sws_freeContext(context);