1 /*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
23 #define _DEFAULT_SOURCE
24 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
25 #define _DARWIN_C_SOURCE // needed for MAP_ANON
26 #include <inttypes.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
30 #if HAVE_MMAP
31 #include <sys/mman.h>
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 #define MAP_ANONYMOUS MAP_ANON
34 #endif
35 #endif
36 #if HAVE_VIRTUALALLOC
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #endif
40
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"
56
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
61
62 #include "rgb2rgb.h"
63 #include "swscale.h"
64 #include "swscale_internal.h"
65
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);
71 #endif
72
73 static void handle_formats(SwsContext *c);
74
swscale_version(void)75 unsigned swscale_version(void)
76 {
77 av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
78 return LIBSWSCALE_VERSION_INT;
79 }
80
swscale_configuration(void)81 const char *swscale_configuration(void)
82 {
83 return FFMPEG_CONFIGURATION;
84 }
85
swscale_license(void)86 const char *swscale_license(void)
87 {
88 #define LICENSE_PREFIX "libswscale license: "
89 return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
90 }
91
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;
96 } FormatEntry;
97
98 static const FormatEntry format_entries[] = {
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_GRAY9BE] = { 1, 1 },
135 [AV_PIX_FMT_GRAY9LE] = { 1, 1 },
136 [AV_PIX_FMT_GRAY10BE] = { 1, 1 },
137 [AV_PIX_FMT_GRAY10LE] = { 1, 1 },
138 [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
139 [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
140 [AV_PIX_FMT_GRAY14BE] = { 1, 1 },
141 [AV_PIX_FMT_GRAY14LE] = { 1, 1 },
142 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
143 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
144 [AV_PIX_FMT_YUV440P] = { 1, 1 },
145 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
146 [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
147 [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
148 [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
149 [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
150 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
151 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
152 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
153 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
154 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
155 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
156 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
157 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
158 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
159 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
160 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
161 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
162 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
163 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
164 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
165 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
166 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
167 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
168 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
169 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
170 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
171 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
172 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
173 [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
174 [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
175 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
176 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
177 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
178 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
179 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
180 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
181 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
182 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
183 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
184 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
185 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
186 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
187 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
188 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
189 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
190 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
191 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
192 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
193 [AV_PIX_FMT_YA8] = { 1, 1 },
194 [AV_PIX_FMT_YA16BE] = { 1, 1 },
195 [AV_PIX_FMT_YA16LE] = { 1, 1 },
196 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
197 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
198 [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
199 [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
200 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
201 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
202 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
203 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
204 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
205 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
206 [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
207 [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
208 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
209 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
210 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
211 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
212 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
213 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
214 [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
215 [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
216 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
217 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
218 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
219 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
220 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
221 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
222 [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
223 [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
224 [AV_PIX_FMT_GBRP] = { 1, 1 },
225 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
226 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
227 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
228 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
229 [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
230 [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
231 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
232 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
233 [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
234 [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
235 [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
236 [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
237 [AV_PIX_FMT_GBRP16LE] = { 1, 1 },
238 [AV_PIX_FMT_GBRP16BE] = { 1, 1 },
239 [AV_PIX_FMT_GBRPF32LE] = { 1, 1 },
240 [AV_PIX_FMT_GBRPF32BE] = { 1, 1 },
241 [AV_PIX_FMT_GBRAPF32LE] = { 1, 1 },
242 [AV_PIX_FMT_GBRAPF32BE] = { 1, 1 },
243 [AV_PIX_FMT_GBRAP] = { 1, 1 },
244 [AV_PIX_FMT_GBRAP16LE] = { 1, 1 },
245 [AV_PIX_FMT_GBRAP16BE] = { 1, 1 },
246 [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
247 [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
248 [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
249 [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
250 [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
251 [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
252 [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
253 [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
254 [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
255 [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
256 [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
257 [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
258 [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
259 [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
260 [AV_PIX_FMT_AYUV64LE] = { 1, 1},
261 [AV_PIX_FMT_P010LE] = { 1, 1 },
262 [AV_PIX_FMT_P010BE] = { 1, 1 },
263 [AV_PIX_FMT_P016LE] = { 1, 1 },
264 [AV_PIX_FMT_P016BE] = { 1, 1 },
265 [AV_PIX_FMT_GRAYF32LE] = { 1, 1 },
266 [AV_PIX_FMT_GRAYF32BE] = { 1, 1 },
267 [AV_PIX_FMT_YUVA422P12BE] = { 1, 1 },
268 [AV_PIX_FMT_YUVA422P12LE] = { 1, 1 },
269 [AV_PIX_FMT_YUVA444P12BE] = { 1, 1 },
270 [AV_PIX_FMT_YUVA444P12LE] = { 1, 1 },
271 [AV_PIX_FMT_NV24] = { 1, 1 },
272 [AV_PIX_FMT_NV42] = { 1, 1 },
273 [AV_PIX_FMT_Y210LE] = { 1, 0 },
274 };
275
sws_isSupportedInput(enum AVPixelFormat pix_fmt)276 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
277 {
278 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
279 format_entries[pix_fmt].is_supported_in : 0;
280 }
281
sws_isSupportedOutput(enum AVPixelFormat pix_fmt)282 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
283 {
284 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
285 format_entries[pix_fmt].is_supported_out : 0;
286 }
287
sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)288 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
289 {
290 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
291 format_entries[pix_fmt].is_supported_endianness : 0;
292 }
293
getSplineCoeff(double a,double b,double c,double d,double dist)294 static double getSplineCoeff(double a, double b, double c, double d,
295 double dist)
296 {
297 if (dist <= 1.0)
298 return ((d * dist + c) * dist + b) * dist + a;
299 else
300 return getSplineCoeff(0.0,
301 b + 2.0 * c + 3.0 * d,
302 c + 3.0 * d,
303 -b - 3.0 * c - 6.0 * d,
304 dist - 1.0);
305 }
306
get_local_pos(SwsContext * s,int chr_subsample,int pos,int dir)307 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
308 {
309 if (pos == -1 || pos <= -513) {
310 pos = (128 << chr_subsample) - 128;
311 }
312 pos += 128; // relative to ideal left edge
313 return pos >> chr_subsample;
314 }
315
316 typedef struct {
317 int flag; ///< flag associated to the algorithm
318 const char *description; ///< human-readable description
319 int size_factor; ///< size factor used when initing the filters
320 } ScaleAlgorithm;
321
322 static const ScaleAlgorithm scale_algorithms[] = {
323 { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
324 { SWS_BICUBIC, "bicubic", 4 },
325 { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
326 { SWS_BILINEAR, "bilinear", 2 },
327 { SWS_FAST_BILINEAR, "fast bilinear", -1 },
328 { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
329 { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
330 { SWS_POINT, "nearest neighbor / point", -1 },
331 { SWS_SINC, "sinc", 20 /* infinite ;) */ },
332 { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
333 { SWS_X, "experimental", 8 },
334 };
335
initFilter(int16_t ** outFilter,int32_t ** filterPos,int * outFilterSize,int xInc,int srcW,int dstW,int filterAlign,int one,int flags,int cpu_flags,SwsVector * srcFilter,SwsVector * dstFilter,double param[2],int srcPos,int dstPos)336 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
337 int *outFilterSize, int xInc, int srcW,
338 int dstW, int filterAlign, int one,
339 int flags, int cpu_flags,
340 SwsVector *srcFilter, SwsVector *dstFilter,
341 double param[2], int srcPos, int dstPos)
342 {
343 int i;
344 int filterSize;
345 int filter2Size;
346 int minFilterSize;
347 int64_t *filter = NULL;
348 int64_t *filter2 = NULL;
349 const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
350 int ret = -1;
351
352 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
353
354 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
355 FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
356
357 if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
358 int i;
359 filterSize = 1;
360 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
361 dstW, sizeof(*filter) * filterSize, fail);
362
363 for (i = 0; i < dstW; i++) {
364 filter[i * filterSize] = fone;
365 (*filterPos)[i] = i;
366 }
367 } else if (flags & SWS_POINT) { // lame looking point sampling mode
368 int i;
369 int64_t xDstInSrc;
370 filterSize = 1;
371 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
372 dstW, sizeof(*filter) * filterSize, fail);
373
374 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
375 for (i = 0; i < dstW; i++) {
376 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
377
378 (*filterPos)[i] = xx;
379 filter[i] = fone;
380 xDstInSrc += xInc;
381 }
382 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
383 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
384 int i;
385 int64_t xDstInSrc;
386 filterSize = 2;
387 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
388 dstW, sizeof(*filter) * filterSize, fail);
389
390 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
391 for (i = 0; i < dstW; i++) {
392 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
393 int j;
394
395 (*filterPos)[i] = xx;
396 // bilinear upscale / linear interpolate / area averaging
397 for (j = 0; j < filterSize; j++) {
398 int64_t coeff = fone - FFABS((int64_t)xx * (1 << 16) - xDstInSrc) * (fone >> 16);
399 if (coeff < 0)
400 coeff = 0;
401 filter[i * filterSize + j] = coeff;
402 xx++;
403 }
404 xDstInSrc += xInc;
405 }
406 } else {
407 int64_t xDstInSrc;
408 int sizeFactor = -1;
409
410 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
411 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
412 sizeFactor = scale_algorithms[i].size_factor;
413 break;
414 }
415 }
416 if (flags & SWS_LANCZOS)
417 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
418 av_assert0(sizeFactor > 0);
419
420 if (xInc <= 1 << 16)
421 filterSize = 1 + sizeFactor; // upscale
422 else
423 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
424
425 filterSize = FFMIN(filterSize, srcW - 2);
426 filterSize = FFMAX(filterSize, 1);
427
428 FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
429 dstW, sizeof(*filter) * filterSize, fail);
430
431 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
432 for (i = 0; i < dstW; i++) {
433 int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
434 int j;
435 (*filterPos)[i] = xx;
436 for (j = 0; j < filterSize; j++) {
437 int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
438 double floatd;
439 int64_t coeff;
440
441 if (xInc > 1 << 16)
442 d = d * dstW / srcW;
443 floatd = d * (1.0 / (1 << 30));
444
445 if (flags & SWS_BICUBIC) {
446 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
447 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
448
449 if (d >= 1LL << 31) {
450 coeff = 0.0;
451 } else {
452 int64_t dd = (d * d) >> 30;
453 int64_t ddd = (dd * d) >> 30;
454
455 if (d < 1LL << 30)
456 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
457 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
458 (6 * (1 << 24) - 2 * B) * (1 << 30);
459 else
460 coeff = (-B - 6 * C) * ddd +
461 (6 * B + 30 * C) * dd +
462 (-12 * B - 48 * C) * d +
463 (8 * B + 24 * C) * (1 << 30);
464 }
465 coeff /= (1LL<<54)/fone;
466 } else if (flags & SWS_X) {
467 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
468 double c;
469
470 if (floatd < 1.0)
471 c = cos(floatd * M_PI);
472 else
473 c = -1.0;
474 if (c < 0.0)
475 c = -pow(-c, A);
476 else
477 c = pow(c, A);
478 coeff = (c * 0.5 + 0.5) * fone;
479 } else if (flags & SWS_AREA) {
480 int64_t d2 = d - (1 << 29);
481 if (d2 * xInc < -(1LL << (29 + 16)))
482 coeff = 1.0 * (1LL << (30 + 16));
483 else if (d2 * xInc < (1LL << (29 + 16)))
484 coeff = -d2 * xInc + (1LL << (29 + 16));
485 else
486 coeff = 0.0;
487 coeff *= fone >> (30 + 16);
488 } else if (flags & SWS_GAUSS) {
489 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
490 coeff = exp2(-p * floatd * floatd) * fone;
491 } else if (flags & SWS_SINC) {
492 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
493 } else if (flags & SWS_LANCZOS) {
494 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
495 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
496 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
497 if (floatd > p)
498 coeff = 0;
499 } else if (flags & SWS_BILINEAR) {
500 coeff = (1 << 30) - d;
501 if (coeff < 0)
502 coeff = 0;
503 coeff *= fone >> 30;
504 } else if (flags & SWS_SPLINE) {
505 double p = -2.196152422706632;
506 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
507 } else {
508 av_assert0(0);
509 }
510
511 filter[i * filterSize + j] = coeff;
512 xx++;
513 }
514 xDstInSrc += 2 * xInc;
515 }
516 }
517
518 /* apply src & dst Filter to filter -> filter2
519 * av_free(filter);
520 */
521 av_assert0(filterSize > 0);
522 filter2Size = filterSize;
523 if (srcFilter)
524 filter2Size += srcFilter->length - 1;
525 if (dstFilter)
526 filter2Size += dstFilter->length - 1;
527 av_assert0(filter2Size > 0);
528 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
529
530 for (i = 0; i < dstW; i++) {
531 int j, k;
532
533 if (srcFilter) {
534 for (k = 0; k < srcFilter->length; k++) {
535 for (j = 0; j < filterSize; j++)
536 filter2[i * filter2Size + k + j] +=
537 srcFilter->coeff[k] * filter[i * filterSize + j];
538 }
539 } else {
540 for (j = 0; j < filterSize; j++)
541 filter2[i * filter2Size + j] = filter[i * filterSize + j];
542 }
543 // FIXME dstFilter
544
545 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
546 }
547 av_freep(&filter);
548
549 /* try to reduce the filter-size (step1 find size and shift left) */
550 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
551 minFilterSize = 0;
552 for (i = dstW - 1; i >= 0; i--) {
553 int min = filter2Size;
554 int j;
555 int64_t cutOff = 0.0;
556
557 /* get rid of near zero elements on the left by shifting left */
558 for (j = 0; j < filter2Size; j++) {
559 int k;
560 cutOff += FFABS(filter2[i * filter2Size]);
561
562 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
563 break;
564
565 /* preserve monotonicity because the core can't handle the
566 * filter otherwise */
567 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
568 break;
569
570 // move filter coefficients left
571 for (k = 1; k < filter2Size; k++)
572 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
573 filter2[i * filter2Size + k - 1] = 0;
574 (*filterPos)[i]++;
575 }
576
577 cutOff = 0;
578 /* count near zeros on the right */
579 for (j = filter2Size - 1; j > 0; j--) {
580 cutOff += FFABS(filter2[i * filter2Size + j]);
581
582 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
583 break;
584 min--;
585 }
586
587 if (min > minFilterSize)
588 minFilterSize = min;
589 }
590
591 if (PPC_ALTIVEC(cpu_flags)) {
592 // we can handle the special case 4, so we don't want to go the full 8
593 if (minFilterSize < 5)
594 filterAlign = 4;
595
596 /* We really don't want to waste our time doing useless computation, so
597 * fall back on the scalar C code for very small filters.
598 * Vectorizing is worth it only if you have a decent-sized vector. */
599 if (minFilterSize < 3)
600 filterAlign = 1;
601 }
602
603 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
604 // special case for unscaled vertical filtering
605 if (minFilterSize == 1 && filterAlign == 2)
606 filterAlign = 1;
607 }
608
609 av_assert0(minFilterSize > 0);
610 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
611 av_assert0(filterSize > 0);
612 filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
613 if (!filter)
614 goto fail;
615 if (filterSize >= MAX_FILTER_SIZE * 16 /
616 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
617 ret = RETCODE_USE_CASCADE;
618 goto fail;
619 }
620 *outFilterSize = filterSize;
621
622 if (flags & SWS_PRINT_INFO)
623 av_log(NULL, AV_LOG_VERBOSE,
624 "SwScaler: reducing / aligning filtersize %d -> %d\n",
625 filter2Size, filterSize);
626 /* try to reduce the filter-size (step2 reduce it) */
627 for (i = 0; i < dstW; i++) {
628 int j;
629
630 for (j = 0; j < filterSize; j++) {
631 if (j >= filter2Size)
632 filter[i * filterSize + j] = 0;
633 else
634 filter[i * filterSize + j] = filter2[i * filter2Size + j];
635 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
636 filter[i * filterSize + j] = 0;
637 }
638 }
639
640 // FIXME try to align filterPos if possible
641
642 // fix borders
643 for (i = 0; i < dstW; i++) {
644 int j;
645 if ((*filterPos)[i] < 0) {
646 // move filter coefficients left to compensate for filterPos
647 for (j = 1; j < filterSize; j++) {
648 int left = FFMAX(j + (*filterPos)[i], 0);
649 filter[i * filterSize + left] += filter[i * filterSize + j];
650 filter[i * filterSize + j] = 0;
651 }
652 (*filterPos)[i]= 0;
653 }
654
655 if ((*filterPos)[i] + filterSize > srcW) {
656 int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
657 int64_t acc = 0;
658
659 for (j = filterSize - 1; j >= 0; j--) {
660 if ((*filterPos)[i] + j >= srcW) {
661 acc += filter[i * filterSize + j];
662 filter[i * filterSize + j] = 0;
663 }
664 }
665 for (j = filterSize - 1; j >= 0; j--) {
666 if (j < shift) {
667 filter[i * filterSize + j] = 0;
668 } else {
669 filter[i * filterSize + j] = filter[i * filterSize + j - shift];
670 }
671 }
672
673 (*filterPos)[i]-= shift;
674 filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
675 }
676 av_assert0((*filterPos)[i] >= 0);
677 av_assert0((*filterPos)[i] < srcW);
678 if ((*filterPos)[i] + filterSize > srcW) {
679 for (j = 0; j < filterSize; j++) {
680 av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
681 }
682 }
683 }
684
685 // Note the +1 is for the MMX scaler which reads over the end
686 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
687 FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
688 (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
689
690 /* normalize & store in outFilter */
691 for (i = 0; i < dstW; i++) {
692 int j;
693 int64_t error = 0;
694 int64_t sum = 0;
695
696 for (j = 0; j < filterSize; j++) {
697 sum += filter[i * filterSize + j];
698 }
699 sum = (sum + one / 2) / one;
700 if (!sum) {
701 av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
702 sum = 1;
703 }
704 for (j = 0; j < *outFilterSize; j++) {
705 int64_t v = filter[i * filterSize + j] + error;
706 int intV = ROUNDED_DIV(v, sum);
707 (*outFilter)[i * (*outFilterSize) + j] = intV;
708 error = v - intV * sum;
709 }
710 }
711
712 (*filterPos)[dstW + 0] =
713 (*filterPos)[dstW + 1] =
714 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
715 * read over the end */
716 for (i = 0; i < *outFilterSize; i++) {
717 int k = (dstW - 1) * (*outFilterSize) + i;
718 (*outFilter)[k + 1 * (*outFilterSize)] =
719 (*outFilter)[k + 2 * (*outFilterSize)] =
720 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
721 }
722
723 ret = 0;
724
725 fail:
726 if(ret < 0)
727 av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
728 av_free(filter);
729 av_free(filter2);
730 return ret;
731 }
732
fill_rgb2yuv_table(SwsContext * c,const int table[4],int dstRange)733 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
734 {
735 int64_t W, V, Z, Cy, Cu, Cv;
736 int64_t vr = table[0];
737 int64_t ub = table[1];
738 int64_t ug = -table[2];
739 int64_t vg = -table[3];
740 int64_t ONE = 65536;
741 int64_t cy = ONE;
742 uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
743 int i;
744 static const int8_t map[] = {
745 BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
746 RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
747 RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
748 BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
749 BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
750 RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
751 RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
752 BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
753 BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
754 RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
755 RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
756 BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
757 RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
758 BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
759 GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
760 -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
761 RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
762 BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
763 GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
764 -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
765 RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
766 BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
767 GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
768 -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
769 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
770 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
771 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
772 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
773 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
774 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
775 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
776 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
777 BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
778 BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
779 BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
780 };
781
782 dstRange = 0; //FIXME range = 1 is handled elsewhere
783
784 if (!dstRange) {
785 cy = cy * 255 / 219;
786 } else {
787 vr = vr * 224 / 255;
788 ub = ub * 224 / 255;
789 ug = ug * 224 / 255;
790 vg = vg * 224 / 255;
791 }
792 W = ROUNDED_DIV(ONE*ONE*ug, ub);
793 V = ROUNDED_DIV(ONE*ONE*vg, vr);
794 Z = ONE*ONE-W-V;
795
796 Cy = ROUNDED_DIV(cy*Z, ONE);
797 Cu = ROUNDED_DIV(ub*Z, ONE);
798 Cv = ROUNDED_DIV(vr*Z, ONE);
799
800 c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
801 c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
802 c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
803
804 c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
805 c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
806 c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
807
808 c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
809 c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
810 c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
811
812 if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
813 c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
814 c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
815 c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
816 c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
817 c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
818 c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
819 c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
820 c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
821 c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
822 }
823 for(i=0; i<FF_ARRAY_ELEMS(map); i++)
824 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
825 }
826
fill_xyztables(struct SwsContext * c)827 static void fill_xyztables(struct SwsContext *c)
828 {
829 int i;
830 double xyzgamma = XYZ_GAMMA;
831 double rgbgamma = 1.0 / RGB_GAMMA;
832 double xyzgammainv = 1.0 / XYZ_GAMMA;
833 double rgbgammainv = RGB_GAMMA;
834 static const int16_t xyz2rgb_matrix[3][4] = {
835 {13270, -6295, -2041},
836 {-3969, 7682, 170},
837 { 228, -835, 4329} };
838 static const int16_t rgb2xyz_matrix[3][4] = {
839 {1689, 1464, 739},
840 { 871, 2929, 296},
841 { 79, 488, 3891} };
842 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
843
844 memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
845 memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
846 c->xyzgamma = xyzgamma_tab;
847 c->rgbgamma = rgbgamma_tab;
848 c->xyzgammainv = xyzgammainv_tab;
849 c->rgbgammainv = rgbgammainv_tab;
850
851 if (rgbgamma_tab[4095])
852 return;
853
854 /* set gamma vectors */
855 for (i = 0; i < 4096; i++) {
856 xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
857 rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
858 xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
859 rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
860 }
861 }
862
sws_setColorspaceDetails(struct SwsContext * c,const int inv_table[4],int srcRange,const int table[4],int dstRange,int brightness,int contrast,int saturation)863 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
864 int srcRange, const int table[4], int dstRange,
865 int brightness, int contrast, int saturation)
866 {
867 const AVPixFmtDescriptor *desc_dst;
868 const AVPixFmtDescriptor *desc_src;
869 int need_reinit = 0;
870
871 handle_formats(c);
872 desc_dst = av_pix_fmt_desc_get(c->dstFormat);
873 desc_src = av_pix_fmt_desc_get(c->srcFormat);
874
875 if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
876 dstRange = 0;
877 if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
878 srcRange = 0;
879
880 if (c->srcRange != srcRange ||
881 c->dstRange != dstRange ||
882 c->brightness != brightness ||
883 c->contrast != contrast ||
884 c->saturation != saturation ||
885 memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
886 memcmp(c->dstColorspaceTable, table, sizeof(int) * 4)
887 )
888 need_reinit = 1;
889
890 memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
891 memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
892
893
894
895 c->brightness = brightness;
896 c->contrast = contrast;
897 c->saturation = saturation;
898 c->srcRange = srcRange;
899 c->dstRange = dstRange;
900
901 //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
902 //and what we have in ticket 2939 looks better with this check
903 if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
904 ff_sws_init_range_convert(c);
905
906 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
907 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
908
909 if (c->cascaded_context[c->cascaded_mainindex])
910 return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness, contrast, saturation);
911
912 if (!need_reinit)
913 return 0;
914
915 if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
916 if (!c->cascaded_context[0] &&
917 memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
918 c->srcW && c->srcH && c->dstW && c->dstH) {
919 enum AVPixelFormat tmp_format;
920 int tmp_width, tmp_height;
921 int srcW = c->srcW;
922 int srcH = c->srcH;
923 int dstW = c->dstW;
924 int dstH = c->dstH;
925 int ret;
926 av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
927
928 if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
929 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
930 tmp_format = AV_PIX_FMT_BGRA64;
931 } else {
932 tmp_format = AV_PIX_FMT_BGR48;
933 }
934 } else {
935 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
936 tmp_format = AV_PIX_FMT_BGRA;
937 } else {
938 tmp_format = AV_PIX_FMT_BGR24;
939 }
940 }
941
942 if (srcW*srcH > dstW*dstH) {
943 tmp_width = dstW;
944 tmp_height = dstH;
945 } else {
946 tmp_width = srcW;
947 tmp_height = srcH;
948 }
949
950 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
951 tmp_width, tmp_height, tmp_format, 64);
952 if (ret < 0)
953 return ret;
954
955 c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
956 tmp_width, tmp_height, tmp_format,
957 c->flags, c->param);
958 if (!c->cascaded_context[0])
959 return -1;
960
961 c->cascaded_context[0]->alphablend = c->alphablend;
962 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
963 if (ret < 0)
964 return ret;
965 //we set both src and dst depending on that the RGB side will be ignored
966 sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
967 srcRange, table, dstRange,
968 brightness, contrast, saturation);
969
970 c->cascaded_context[1] = sws_getContext(tmp_width, tmp_height, tmp_format,
971 dstW, dstH, c->dstFormat,
972 c->flags, NULL, NULL, c->param);
973 if (!c->cascaded_context[1])
974 return -1;
975 sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
976 srcRange, table, dstRange,
977 0, 1 << 16, 1 << 16);
978 return 0;
979 }
980 return -1;
981 }
982
983 if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
984 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
985 contrast, saturation);
986 // FIXME factorize
987
988 if (ARCH_PPC)
989 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
990 contrast, saturation);
991 }
992
993 fill_rgb2yuv_table(c, table, dstRange);
994
995 return 0;
996 }
997
sws_getColorspaceDetails(struct SwsContext * c,int ** inv_table,int * srcRange,int ** table,int * dstRange,int * brightness,int * contrast,int * saturation)998 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
999 int *srcRange, int **table, int *dstRange,
1000 int *brightness, int *contrast, int *saturation)
1001 {
1002 if (!c )
1003 return -1;
1004
1005 *inv_table = c->srcColorspaceTable;
1006 *table = c->dstColorspaceTable;
1007 *srcRange = c->srcRange;
1008 *dstRange = c->dstRange;
1009 *brightness = c->brightness;
1010 *contrast = c->contrast;
1011 *saturation = c->saturation;
1012
1013 return 0;
1014 }
1015
handle_jpeg(enum AVPixelFormat * format)1016 static int handle_jpeg(enum AVPixelFormat *format)
1017 {
1018 switch (*format) {
1019 case AV_PIX_FMT_YUVJ420P:
1020 *format = AV_PIX_FMT_YUV420P;
1021 return 1;
1022 case AV_PIX_FMT_YUVJ411P:
1023 *format = AV_PIX_FMT_YUV411P;
1024 return 1;
1025 case AV_PIX_FMT_YUVJ422P:
1026 *format = AV_PIX_FMT_YUV422P;
1027 return 1;
1028 case AV_PIX_FMT_YUVJ444P:
1029 *format = AV_PIX_FMT_YUV444P;
1030 return 1;
1031 case AV_PIX_FMT_YUVJ440P:
1032 *format = AV_PIX_FMT_YUV440P;
1033 return 1;
1034 case AV_PIX_FMT_GRAY8:
1035 case AV_PIX_FMT_YA8:
1036 case AV_PIX_FMT_GRAY9LE:
1037 case AV_PIX_FMT_GRAY9BE:
1038 case AV_PIX_FMT_GRAY10LE:
1039 case AV_PIX_FMT_GRAY10BE:
1040 case AV_PIX_FMT_GRAY12LE:
1041 case AV_PIX_FMT_GRAY12BE:
1042 case AV_PIX_FMT_GRAY14LE:
1043 case AV_PIX_FMT_GRAY14BE:
1044 case AV_PIX_FMT_GRAY16LE:
1045 case AV_PIX_FMT_GRAY16BE:
1046 case AV_PIX_FMT_YA16BE:
1047 case AV_PIX_FMT_YA16LE:
1048 return 1;
1049 default:
1050 return 0;
1051 }
1052 }
1053
handle_0alpha(enum AVPixelFormat * format)1054 static int handle_0alpha(enum AVPixelFormat *format)
1055 {
1056 switch (*format) {
1057 case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
1058 case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
1059 case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
1060 case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
1061 default: return 0;
1062 }
1063 }
1064
handle_xyz(enum AVPixelFormat * format)1065 static int handle_xyz(enum AVPixelFormat *format)
1066 {
1067 switch (*format) {
1068 case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1069 case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1070 default: return 0;
1071 }
1072 }
1073
handle_formats(SwsContext * c)1074 static void handle_formats(SwsContext *c)
1075 {
1076 c->src0Alpha |= handle_0alpha(&c->srcFormat);
1077 c->dst0Alpha |= handle_0alpha(&c->dstFormat);
1078 c->srcXYZ |= handle_xyz(&c->srcFormat);
1079 c->dstXYZ |= handle_xyz(&c->dstFormat);
1080 if (c->srcXYZ || c->dstXYZ)
1081 fill_xyztables(c);
1082 }
1083
sws_alloc_context(void)1084 SwsContext *sws_alloc_context(void)
1085 {
1086 SwsContext *c = av_mallocz(sizeof(SwsContext));
1087
1088 av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
1089
1090 if (c) {
1091 c->av_class = &ff_sws_context_class;
1092 av_opt_set_defaults(c);
1093 }
1094
1095 return c;
1096 }
1097
alloc_gamma_tbl(double e)1098 static uint16_t * alloc_gamma_tbl(double e)
1099 {
1100 int i = 0;
1101 uint16_t * tbl;
1102 tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
1103 if (!tbl)
1104 return NULL;
1105
1106 for (i = 0; i < 65536; ++i) {
1107 tbl[i] = pow(i / 65535.0, e) * 65535.0;
1108 }
1109 return tbl;
1110 }
1111
alphaless_fmt(enum AVPixelFormat fmt)1112 static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
1113 {
1114 switch(fmt) {
1115 case AV_PIX_FMT_ARGB: return AV_PIX_FMT_RGB24;
1116 case AV_PIX_FMT_RGBA: return AV_PIX_FMT_RGB24;
1117 case AV_PIX_FMT_ABGR: return AV_PIX_FMT_BGR24;
1118 case AV_PIX_FMT_BGRA: return AV_PIX_FMT_BGR24;
1119 case AV_PIX_FMT_YA8: return AV_PIX_FMT_GRAY8;
1120
1121 case AV_PIX_FMT_YUVA420P: return AV_PIX_FMT_YUV420P;
1122 case AV_PIX_FMT_YUVA422P: return AV_PIX_FMT_YUV422P;
1123 case AV_PIX_FMT_YUVA444P: return AV_PIX_FMT_YUV444P;
1124
1125 case AV_PIX_FMT_GBRAP: return AV_PIX_FMT_GBRP;
1126
1127 case AV_PIX_FMT_GBRAP10LE: return AV_PIX_FMT_GBRP10;
1128 case AV_PIX_FMT_GBRAP10BE: return AV_PIX_FMT_GBRP10;
1129
1130 case AV_PIX_FMT_GBRAP12LE: return AV_PIX_FMT_GBRP12;
1131 case AV_PIX_FMT_GBRAP12BE: return AV_PIX_FMT_GBRP12;
1132
1133 case AV_PIX_FMT_GBRAP16LE: return AV_PIX_FMT_GBRP16;
1134 case AV_PIX_FMT_GBRAP16BE: return AV_PIX_FMT_GBRP16;
1135
1136 case AV_PIX_FMT_RGBA64LE: return AV_PIX_FMT_RGB48;
1137 case AV_PIX_FMT_RGBA64BE: return AV_PIX_FMT_RGB48;
1138 case AV_PIX_FMT_BGRA64LE: return AV_PIX_FMT_BGR48;
1139 case AV_PIX_FMT_BGRA64BE: return AV_PIX_FMT_BGR48;
1140
1141 case AV_PIX_FMT_YA16BE: return AV_PIX_FMT_GRAY16;
1142 case AV_PIX_FMT_YA16LE: return AV_PIX_FMT_GRAY16;
1143
1144 case AV_PIX_FMT_YUVA420P9BE: return AV_PIX_FMT_YUV420P9;
1145 case AV_PIX_FMT_YUVA422P9BE: return AV_PIX_FMT_YUV422P9;
1146 case AV_PIX_FMT_YUVA444P9BE: return AV_PIX_FMT_YUV444P9;
1147 case AV_PIX_FMT_YUVA420P9LE: return AV_PIX_FMT_YUV420P9;
1148 case AV_PIX_FMT_YUVA422P9LE: return AV_PIX_FMT_YUV422P9;
1149 case AV_PIX_FMT_YUVA444P9LE: return AV_PIX_FMT_YUV444P9;
1150 case AV_PIX_FMT_YUVA420P10BE: return AV_PIX_FMT_YUV420P10;
1151 case AV_PIX_FMT_YUVA422P10BE: return AV_PIX_FMT_YUV422P10;
1152 case AV_PIX_FMT_YUVA444P10BE: return AV_PIX_FMT_YUV444P10;
1153 case AV_PIX_FMT_YUVA420P10LE: return AV_PIX_FMT_YUV420P10;
1154 case AV_PIX_FMT_YUVA422P10LE: return AV_PIX_FMT_YUV422P10;
1155 case AV_PIX_FMT_YUVA444P10LE: return AV_PIX_FMT_YUV444P10;
1156 case AV_PIX_FMT_YUVA420P16BE: return AV_PIX_FMT_YUV420P16;
1157 case AV_PIX_FMT_YUVA422P16BE: return AV_PIX_FMT_YUV422P16;
1158 case AV_PIX_FMT_YUVA444P16BE: return AV_PIX_FMT_YUV444P16;
1159 case AV_PIX_FMT_YUVA420P16LE: return AV_PIX_FMT_YUV420P16;
1160 case AV_PIX_FMT_YUVA422P16LE: return AV_PIX_FMT_YUV422P16;
1161 case AV_PIX_FMT_YUVA444P16LE: return AV_PIX_FMT_YUV444P16;
1162
1163 // case AV_PIX_FMT_AYUV64LE:
1164 // case AV_PIX_FMT_AYUV64BE:
1165 // case AV_PIX_FMT_PAL8:
1166 default: return AV_PIX_FMT_NONE;
1167 }
1168 }
1169
sws_init_context(SwsContext * c,SwsFilter * srcFilter,SwsFilter * dstFilter)1170 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
1171 SwsFilter *dstFilter)
1172 {
1173 int i;
1174 int usesVFilter, usesHFilter;
1175 int unscaled;
1176 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1177 int srcW = c->srcW;
1178 int srcH = c->srcH;
1179 int dstW = c->dstW;
1180 int dstH = c->dstH;
1181 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1182 int flags, cpu_flags;
1183 enum AVPixelFormat srcFormat = c->srcFormat;
1184 enum AVPixelFormat dstFormat = c->dstFormat;
1185 const AVPixFmtDescriptor *desc_src;
1186 const AVPixFmtDescriptor *desc_dst;
1187 int ret = 0;
1188 enum AVPixelFormat tmpFmt;
1189 static const float float_mult = 1.0f / 255.0f;
1190
1191 cpu_flags = av_get_cpu_flags();
1192 flags = c->flags;
1193 emms_c();
1194 if (!rgb15to16)
1195 ff_sws_rgb2rgb_init();
1196
1197 unscaled = (srcW == dstW && srcH == dstH);
1198
1199 c->srcRange |= handle_jpeg(&c->srcFormat);
1200 c->dstRange |= handle_jpeg(&c->dstFormat);
1201
1202 if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
1203 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
1204
1205 if (!c->contrast && !c->saturation && !c->dstFormatBpp)
1206 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1207 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1208 c->dstRange, 0, 1 << 16, 1 << 16);
1209
1210 handle_formats(c);
1211 srcFormat = c->srcFormat;
1212 dstFormat = c->dstFormat;
1213 desc_src = av_pix_fmt_desc_get(srcFormat);
1214 desc_dst = av_pix_fmt_desc_get(dstFormat);
1215
1216 // If the source has no alpha then disable alpha blendaway
1217 if (c->src0Alpha)
1218 c->alphablend = SWS_ALPHA_BLEND_NONE;
1219
1220 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1221 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1222 if (!sws_isSupportedInput(srcFormat)) {
1223 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1224 av_get_pix_fmt_name(srcFormat));
1225 return AVERROR(EINVAL);
1226 }
1227 if (!sws_isSupportedOutput(dstFormat)) {
1228 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1229 av_get_pix_fmt_name(dstFormat));
1230 return AVERROR(EINVAL);
1231 }
1232 }
1233 av_assert2(desc_src && desc_dst);
1234
1235 i = flags & (SWS_POINT |
1236 SWS_AREA |
1237 SWS_BILINEAR |
1238 SWS_FAST_BILINEAR |
1239 SWS_BICUBIC |
1240 SWS_X |
1241 SWS_GAUSS |
1242 SWS_LANCZOS |
1243 SWS_SINC |
1244 SWS_SPLINE |
1245 SWS_BICUBLIN);
1246
1247 /* provide a default scaler if not set by caller */
1248 if (!i) {
1249 if (dstW < srcW && dstH < srcH)
1250 flags |= SWS_BICUBIC;
1251 else if (dstW > srcW && dstH > srcH)
1252 flags |= SWS_BICUBIC;
1253 else
1254 flags |= SWS_BICUBIC;
1255 c->flags = flags;
1256 } else if (i & (i - 1)) {
1257 av_log(c, AV_LOG_ERROR,
1258 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1259 return AVERROR(EINVAL);
1260 }
1261 /* sanity check */
1262 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1263 /* FIXME check if these are enough and try to lower them after
1264 * fixing the relevant parts of the code */
1265 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1266 srcW, srcH, dstW, dstH);
1267 return AVERROR(EINVAL);
1268 }
1269 if (flags & SWS_FAST_BILINEAR) {
1270 if (srcW < 8 || dstW < 8) {
1271 flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
1272 c->flags = flags;
1273 }
1274 }
1275
1276 if (!dstFilter)
1277 dstFilter = &dummyFilter;
1278 if (!srcFilter)
1279 srcFilter = &dummyFilter;
1280
1281 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1282 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1283 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1284 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1285 c->vRounder = 4 * 0x0001000100010001ULL;
1286
1287 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1288 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1289 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1290 (dstFilter->chrV && dstFilter->chrV->length > 1);
1291 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1292 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1293 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1294 (dstFilter->chrH && dstFilter->chrH->length > 1);
1295
1296 av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1297 av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1298
1299 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1300 if (dstW&1) {
1301 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1302 flags |= SWS_FULL_CHR_H_INT;
1303 c->flags = flags;
1304 }
1305
1306 if ( c->chrSrcHSubSample == 0
1307 && c->chrSrcVSubSample == 0
1308 && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1309 && !(c->flags & SWS_FAST_BILINEAR)
1310 ) {
1311 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1312 flags |= SWS_FULL_CHR_H_INT;
1313 c->flags = flags;
1314 }
1315 }
1316
1317 if (c->dither == SWS_DITHER_AUTO) {
1318 if (flags & SWS_ERROR_DIFFUSION)
1319 c->dither = SWS_DITHER_ED;
1320 }
1321
1322 if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1323 dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1324 dstFormat == AV_PIX_FMT_BGR8 ||
1325 dstFormat == AV_PIX_FMT_RGB8) {
1326 if (c->dither == SWS_DITHER_AUTO)
1327 c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1328 if (!(flags & SWS_FULL_CHR_H_INT)) {
1329 if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
1330 av_log(c, AV_LOG_DEBUG,
1331 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1332 av_get_pix_fmt_name(dstFormat));
1333 flags |= SWS_FULL_CHR_H_INT;
1334 c->flags = flags;
1335 }
1336 }
1337 if (flags & SWS_FULL_CHR_H_INT) {
1338 if (c->dither == SWS_DITHER_BAYER) {
1339 av_log(c, AV_LOG_DEBUG,
1340 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1341 av_get_pix_fmt_name(dstFormat));
1342 c->dither = SWS_DITHER_ED;
1343 }
1344 }
1345 }
1346 if (isPlanarRGB(dstFormat)) {
1347 if (!(flags & SWS_FULL_CHR_H_INT)) {
1348 av_log(c, AV_LOG_DEBUG,
1349 "%s output is not supported with half chroma resolution, switching to full\n",
1350 av_get_pix_fmt_name(dstFormat));
1351 flags |= SWS_FULL_CHR_H_INT;
1352 c->flags = flags;
1353 }
1354 }
1355
1356 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1357 * chroma interpolation */
1358 if (flags & SWS_FULL_CHR_H_INT &&
1359 isAnyRGB(dstFormat) &&
1360 !isPlanarRGB(dstFormat) &&
1361 dstFormat != AV_PIX_FMT_RGBA64LE &&
1362 dstFormat != AV_PIX_FMT_RGBA64BE &&
1363 dstFormat != AV_PIX_FMT_BGRA64LE &&
1364 dstFormat != AV_PIX_FMT_BGRA64BE &&
1365 dstFormat != AV_PIX_FMT_RGB48LE &&
1366 dstFormat != AV_PIX_FMT_RGB48BE &&
1367 dstFormat != AV_PIX_FMT_BGR48LE &&
1368 dstFormat != AV_PIX_FMT_BGR48BE &&
1369 dstFormat != AV_PIX_FMT_RGBA &&
1370 dstFormat != AV_PIX_FMT_ARGB &&
1371 dstFormat != AV_PIX_FMT_BGRA &&
1372 dstFormat != AV_PIX_FMT_ABGR &&
1373 dstFormat != AV_PIX_FMT_RGB24 &&
1374 dstFormat != AV_PIX_FMT_BGR24 &&
1375 dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1376 dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1377 dstFormat != AV_PIX_FMT_BGR8 &&
1378 dstFormat != AV_PIX_FMT_RGB8
1379 ) {
1380 av_log(c, AV_LOG_WARNING,
1381 "full chroma interpolation for destination format '%s' not yet implemented\n",
1382 av_get_pix_fmt_name(dstFormat));
1383 flags &= ~SWS_FULL_CHR_H_INT;
1384 c->flags = flags;
1385 }
1386 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1387 c->chrDstHSubSample = 1;
1388
1389 // drop some chroma lines if the user wants it
1390 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1391 SWS_SRC_V_CHR_DROP_SHIFT;
1392 c->chrSrcVSubSample += c->vChrDrop;
1393
1394 /* drop every other pixel for chroma calculation unless user
1395 * wants full chroma */
1396 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1397 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1398 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1399 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1400 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1401 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1402 srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
1403 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1404 srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
1405 srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1406 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1407 srcFormat != AV_PIX_FMT_GBRAP16BE && srcFormat != AV_PIX_FMT_GBRAP16LE &&
1408 srcFormat != AV_PIX_FMT_GBRPF32BE && srcFormat != AV_PIX_FMT_GBRPF32LE &&
1409 srcFormat != AV_PIX_FMT_GBRAPF32BE && srcFormat != AV_PIX_FMT_GBRAPF32LE &&
1410 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1411 (flags & SWS_FAST_BILINEAR)))
1412 c->chrSrcHSubSample = 1;
1413
1414 // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
1415 c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1416 c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1417 c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1418 c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1419
1420 FF_ALLOCZ_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1421
1422 c->srcBpc = desc_src->comp[0].depth;
1423 if (c->srcBpc < 8)
1424 c->srcBpc = 8;
1425 c->dstBpc = desc_dst->comp[0].depth;
1426 if (c->dstBpc < 8)
1427 c->dstBpc = 8;
1428 if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1429 c->srcBpc = 16;
1430 if (c->dstBpc == 16)
1431 dst_stride <<= 1;
1432
1433 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1434 c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1435 c->chrDstW >= c->chrSrcW &&
1436 (srcW & 15) == 0;
1437 if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1438
1439 && (flags & SWS_FAST_BILINEAR)) {
1440 if (flags & SWS_PRINT_INFO)
1441 av_log(c, AV_LOG_INFO,
1442 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1443 }
1444 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1445 c->canMMXEXTBeUsed = 0;
1446 } else
1447 c->canMMXEXTBeUsed = 0;
1448
1449 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1450 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1451
1452 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1453 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1454 * correct scaling.
1455 * n-2 is the last chrominance sample available.
1456 * This is not perfect, but no one should notice the difference, the more
1457 * correct variant would be like the vertical one, but that would require
1458 * some special code for the first and last pixel */
1459 if (flags & SWS_FAST_BILINEAR) {
1460 if (c->canMMXEXTBeUsed) {
1461 c->lumXInc += 20;
1462 c->chrXInc += 20;
1463 }
1464 // we don't use the x86 asm scaler if MMX is available
1465 else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1466 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1467 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1468 }
1469 }
1470
1471 // hardcoded for now
1472 c->gamma_value = 2.2;
1473 tmpFmt = AV_PIX_FMT_RGBA64LE;
1474
1475
1476 if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
1477 SwsContext *c2;
1478 c->cascaded_context[0] = NULL;
1479
1480 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1481 srcW, srcH, tmpFmt, 64);
1482 if (ret < 0)
1483 return ret;
1484
1485 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1486 srcW, srcH, tmpFmt,
1487 flags, NULL, NULL, c->param);
1488 if (!c->cascaded_context[0]) {
1489 return -1;
1490 }
1491
1492 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
1493 dstW, dstH, tmpFmt,
1494 flags, srcFilter, dstFilter, c->param);
1495
1496 if (!c->cascaded_context[1])
1497 return -1;
1498
1499 c2 = c->cascaded_context[1];
1500 c2->is_internal_gamma = 1;
1501 c2->gamma = alloc_gamma_tbl( c->gamma_value);
1502 c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
1503 if (!c2->gamma || !c2->inv_gamma)
1504 return AVERROR(ENOMEM);
1505
1506 // is_internal_flag is set after creating the context
1507 // to properly create the gamma convert FilterDescriptor
1508 // we have to re-initialize it
1509 ff_free_filters(c2);
1510 if (ff_init_filters(c2) < 0) {
1511 sws_freeContext(c2);
1512 c->cascaded_context[1] = NULL;
1513 return -1;
1514 }
1515
1516 c->cascaded_context[2] = NULL;
1517 if (dstFormat != tmpFmt) {
1518 ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
1519 dstW, dstH, tmpFmt, 64);
1520 if (ret < 0)
1521 return ret;
1522
1523 c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
1524 dstW, dstH, dstFormat,
1525 flags, NULL, NULL, c->param);
1526 if (!c->cascaded_context[2])
1527 return -1;
1528 }
1529 return 0;
1530 }
1531
1532 if (isBayer(srcFormat)) {
1533 if (!unscaled ||
1534 (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
1535 enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
1536
1537 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1538 srcW, srcH, tmpFormat, 64);
1539 if (ret < 0)
1540 return ret;
1541
1542 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1543 srcW, srcH, tmpFormat,
1544 flags, srcFilter, NULL, c->param);
1545 if (!c->cascaded_context[0])
1546 return -1;
1547
1548 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
1549 dstW, dstH, dstFormat,
1550 flags, NULL, dstFilter, c->param);
1551 if (!c->cascaded_context[1])
1552 return -1;
1553 return 0;
1554 }
1555 }
1556
1557 if (unscaled && c->srcBpc == 8 && dstFormat == AV_PIX_FMT_GRAYF32){
1558 for (i = 0; i < 256; ++i){
1559 c->uint2float_lut[i] = (float)i * float_mult;
1560 }
1561 }
1562
1563 // float will be converted to uint16_t
1564 if ((srcFormat == AV_PIX_FMT_GRAYF32BE || srcFormat == AV_PIX_FMT_GRAYF32LE) &&
1565 (!unscaled || unscaled && dstFormat != srcFormat && (srcFormat != AV_PIX_FMT_GRAYF32 ||
1566 dstFormat != AV_PIX_FMT_GRAY8))){
1567 c->srcBpc = 16;
1568 }
1569
1570 if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
1571 enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
1572
1573 if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE)
1574 if (!unscaled ||
1575 dstFormat != tmpFormat ||
1576 usesHFilter || usesVFilter ||
1577 c->srcRange != c->dstRange
1578 ) {
1579 c->cascaded_mainindex = 1;
1580 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1581 srcW, srcH, tmpFormat, 64);
1582 if (ret < 0)
1583 return ret;
1584
1585 c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
1586 srcW, srcH, tmpFormat,
1587 flags, c->param);
1588 if (!c->cascaded_context[0])
1589 return -1;
1590 c->cascaded_context[0]->alphablend = c->alphablend;
1591 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
1592 if (ret < 0)
1593 return ret;
1594
1595 c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
1596 dstW, dstH, dstFormat,
1597 flags, c->param);
1598 if (!c->cascaded_context[1])
1599 return -1;
1600
1601 c->cascaded_context[1]->srcRange = c->srcRange;
1602 c->cascaded_context[1]->dstRange = c->dstRange;
1603 ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
1604 if (ret < 0)
1605 return ret;
1606
1607 return 0;
1608 }
1609 }
1610
1611 #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
1612 #define USE_MMAP 1
1613 #else
1614 #define USE_MMAP 0
1615 #endif
1616
1617 /* precalculate horizontal scaler filter coefficients */
1618 {
1619 #if HAVE_MMXEXT_INLINE
1620 // can't downscale !!!
1621 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1622 c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1623 NULL, NULL, 8);
1624 c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1625 NULL, NULL, NULL, 4);
1626
1627 #if USE_MMAP
1628 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1629 PROT_READ | PROT_WRITE,
1630 MAP_PRIVATE | MAP_ANONYMOUS,
1631 -1, 0);
1632 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1633 PROT_READ | PROT_WRITE,
1634 MAP_PRIVATE | MAP_ANONYMOUS,
1635 -1, 0);
1636 #elif HAVE_VIRTUALALLOC
1637 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1638 c->lumMmxextFilterCodeSize,
1639 MEM_COMMIT,
1640 PAGE_EXECUTE_READWRITE);
1641 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1642 c->chrMmxextFilterCodeSize,
1643 MEM_COMMIT,
1644 PAGE_EXECUTE_READWRITE);
1645 #else
1646 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1647 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1648 #endif
1649
1650 #ifdef MAP_ANONYMOUS
1651 if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1652 #else
1653 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1654 #endif
1655 {
1656 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1657 return AVERROR(ENOMEM);
1658 }
1659
1660 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1661 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1662 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1663 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1664
1665 ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1666 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1667 ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1668 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1669
1670 #if USE_MMAP
1671 if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1672 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1673 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1674 goto fail;
1675 }
1676 #endif
1677 } else
1678 #endif /* HAVE_MMXEXT_INLINE */
1679 {
1680 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1681 PPC_ALTIVEC(cpu_flags) ? 8 :
1682 have_neon(cpu_flags) ? 8 : 1;
1683
1684 if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
1685 &c->hLumFilterSize, c->lumXInc,
1686 srcW, dstW, filterAlign, 1 << 14,
1687 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1688 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1689 c->param,
1690 get_local_pos(c, 0, 0, 0),
1691 get_local_pos(c, 0, 0, 0))) < 0)
1692 goto fail;
1693 if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
1694 &c->hChrFilterSize, c->chrXInc,
1695 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1696 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1697 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1698 c->param,
1699 get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1700 get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
1701 goto fail;
1702 }
1703 } // initialize horizontal stuff
1704
1705 /* precalculate vertical scaler filter coefficients */
1706 {
1707 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1708 PPC_ALTIVEC(cpu_flags) ? 8 :
1709 have_neon(cpu_flags) ? 2 : 1;
1710
1711 if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1712 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1713 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1714 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1715 c->param,
1716 get_local_pos(c, 0, 0, 1),
1717 get_local_pos(c, 0, 0, 1))) < 0)
1718 goto fail;
1719 if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1720 c->chrYInc, c->chrSrcH, c->chrDstH,
1721 filterAlign, (1 << 12),
1722 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1723 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1724 c->param,
1725 get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1726 get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
1727
1728 goto fail;
1729
1730 #if HAVE_ALTIVEC
1731 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1732 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1733
1734 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1735 int j;
1736 short *p = (short *)&c->vYCoeffsBank[i];
1737 for (j = 0; j < 8; j++)
1738 p[j] = c->vLumFilter[i];
1739 }
1740
1741 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1742 int j;
1743 short *p = (short *)&c->vCCoeffsBank[i];
1744 for (j = 0; j < 8; j++)
1745 p[j] = c->vChrFilter[i];
1746 }
1747 #endif
1748 }
1749
1750 for (i = 0; i < 4; i++)
1751 FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1752
1753 c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
1754
1755 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1756 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1757 c->uv_offx2 = dst_stride + 16;
1758
1759 av_assert0(c->chrDstH <= dstH);
1760
1761 if (flags & SWS_PRINT_INFO) {
1762 const char *scaler = NULL, *cpucaps;
1763
1764 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1765 if (flags & scale_algorithms[i].flag) {
1766 scaler = scale_algorithms[i].description;
1767 break;
1768 }
1769 }
1770 if (!scaler)
1771 scaler = "ehh flags invalid?!";
1772 av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1773 scaler,
1774 av_get_pix_fmt_name(srcFormat),
1775 #ifdef DITHER1XBPP
1776 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1777 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1778 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1779 "dithered " : "",
1780 #else
1781 "",
1782 #endif
1783 av_get_pix_fmt_name(dstFormat));
1784
1785 if (INLINE_MMXEXT(cpu_flags))
1786 cpucaps = "MMXEXT";
1787 else if (INLINE_AMD3DNOW(cpu_flags))
1788 cpucaps = "3DNOW";
1789 else if (INLINE_MMX(cpu_flags))
1790 cpucaps = "MMX";
1791 else if (PPC_ALTIVEC(cpu_flags))
1792 cpucaps = "AltiVec";
1793 else
1794 cpucaps = "C";
1795
1796 av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1797
1798 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1799 av_log(c, AV_LOG_DEBUG,
1800 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1801 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1802 av_log(c, AV_LOG_DEBUG,
1803 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1804 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1805 c->chrXInc, c->chrYInc);
1806 }
1807
1808 /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
1809 if (unscaled && !usesHFilter && !usesVFilter &&
1810 c->alphablend != SWS_ALPHA_BLEND_NONE &&
1811 isALPHA(srcFormat) &&
1812 (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
1813 alphaless_fmt(srcFormat) == dstFormat
1814 ) {
1815 c->swscale = ff_sws_alphablendaway;
1816
1817 if (flags & SWS_PRINT_INFO)
1818 av_log(c, AV_LOG_INFO,
1819 "using alpha blendaway %s -> %s special converter\n",
1820 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1821 return 0;
1822 }
1823
1824 /* unscaled special cases */
1825 if (unscaled && !usesHFilter && !usesVFilter &&
1826 (c->srcRange == c->dstRange || isAnyRGB(dstFormat) ||
1827 isFloat(srcFormat) || isFloat(dstFormat))){
1828 ff_get_unscaled_swscale(c);
1829
1830 if (c->swscale) {
1831 if (flags & SWS_PRINT_INFO)
1832 av_log(c, AV_LOG_INFO,
1833 "using unscaled %s -> %s special converter\n",
1834 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1835 return 0;
1836 }
1837 }
1838
1839 c->swscale = ff_getSwsFunc(c);
1840 return ff_init_filters(c);
1841 fail: // FIXME replace things by appropriate error codes
1842 if (ret == RETCODE_USE_CASCADE) {
1843 int tmpW = sqrt(srcW * (int64_t)dstW);
1844 int tmpH = sqrt(srcH * (int64_t)dstH);
1845 enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
1846
1847 if (isALPHA(srcFormat))
1848 tmpFormat = AV_PIX_FMT_YUVA420P;
1849
1850 if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
1851 return AVERROR(EINVAL);
1852
1853 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1854 tmpW, tmpH, tmpFormat, 64);
1855 if (ret < 0)
1856 return ret;
1857
1858 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1859 tmpW, tmpH, tmpFormat,
1860 flags, srcFilter, NULL, c->param);
1861 if (!c->cascaded_context[0])
1862 return -1;
1863
1864 c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
1865 dstW, dstH, dstFormat,
1866 flags, NULL, dstFilter, c->param);
1867 if (!c->cascaded_context[1])
1868 return -1;
1869 return 0;
1870 }
1871 return -1;
1872 }
1873
sws_alloc_set_opts(int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,const double * param)1874 SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
1875 int dstW, int dstH, enum AVPixelFormat dstFormat,
1876 int flags, const double *param)
1877 {
1878 SwsContext *c;
1879
1880 if (!(c = sws_alloc_context()))
1881 return NULL;
1882
1883 c->flags = flags;
1884 c->srcW = srcW;
1885 c->srcH = srcH;
1886 c->dstW = dstW;
1887 c->dstH = dstH;
1888 c->srcFormat = srcFormat;
1889 c->dstFormat = dstFormat;
1890
1891 if (param) {
1892 c->param[0] = param[0];
1893 c->param[1] = param[1];
1894 }
1895
1896 return c;
1897 }
1898
sws_getContext(int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,SwsFilter * srcFilter,SwsFilter * dstFilter,const double * param)1899 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1900 int dstW, int dstH, enum AVPixelFormat dstFormat,
1901 int flags, SwsFilter *srcFilter,
1902 SwsFilter *dstFilter, const double *param)
1903 {
1904 SwsContext *c;
1905
1906 c = sws_alloc_set_opts(srcW, srcH, srcFormat,
1907 dstW, dstH, dstFormat,
1908 flags, param);
1909 if (!c)
1910 return NULL;
1911
1912 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1913 sws_freeContext(c);
1914 return NULL;
1915 }
1916
1917 return c;
1918 }
1919
isnan_vec(SwsVector * a)1920 static int isnan_vec(SwsVector *a)
1921 {
1922 int i;
1923 for (i=0; i<a->length; i++)
1924 if (isnan(a->coeff[i]))
1925 return 1;
1926 return 0;
1927 }
1928
makenan_vec(SwsVector * a)1929 static void makenan_vec(SwsVector *a)
1930 {
1931 int i;
1932 for (i=0; i<a->length; i++)
1933 a->coeff[i] = NAN;
1934 }
1935
sws_getDefaultFilter(float lumaGBlur,float chromaGBlur,float lumaSharpen,float chromaSharpen,float chromaHShift,float chromaVShift,int verbose)1936 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1937 float lumaSharpen, float chromaSharpen,
1938 float chromaHShift, float chromaVShift,
1939 int verbose)
1940 {
1941 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1942 if (!filter)
1943 return NULL;
1944
1945 if (lumaGBlur != 0.0) {
1946 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1947 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1948 } else {
1949 filter->lumH = sws_getIdentityVec();
1950 filter->lumV = sws_getIdentityVec();
1951 }
1952
1953 if (chromaGBlur != 0.0) {
1954 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1955 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1956 } else {
1957 filter->chrH = sws_getIdentityVec();
1958 filter->chrV = sws_getIdentityVec();
1959 }
1960
1961 if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
1962 goto fail;
1963
1964 if (chromaSharpen != 0.0) {
1965 SwsVector *id = sws_getIdentityVec();
1966 if (!id)
1967 goto fail;
1968 sws_scaleVec(filter->chrH, -chromaSharpen);
1969 sws_scaleVec(filter->chrV, -chromaSharpen);
1970 sws_addVec(filter->chrH, id);
1971 sws_addVec(filter->chrV, id);
1972 sws_freeVec(id);
1973 }
1974
1975 if (lumaSharpen != 0.0) {
1976 SwsVector *id = sws_getIdentityVec();
1977 if (!id)
1978 goto fail;
1979 sws_scaleVec(filter->lumH, -lumaSharpen);
1980 sws_scaleVec(filter->lumV, -lumaSharpen);
1981 sws_addVec(filter->lumH, id);
1982 sws_addVec(filter->lumV, id);
1983 sws_freeVec(id);
1984 }
1985
1986 if (chromaHShift != 0.0)
1987 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1988
1989 if (chromaVShift != 0.0)
1990 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1991
1992 sws_normalizeVec(filter->chrH, 1.0);
1993 sws_normalizeVec(filter->chrV, 1.0);
1994 sws_normalizeVec(filter->lumH, 1.0);
1995 sws_normalizeVec(filter->lumV, 1.0);
1996
1997 if (isnan_vec(filter->chrH) ||
1998 isnan_vec(filter->chrV) ||
1999 isnan_vec(filter->lumH) ||
2000 isnan_vec(filter->lumV))
2001 goto fail;
2002
2003 if (verbose)
2004 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2005 if (verbose)
2006 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
2007
2008 return filter;
2009
2010 fail:
2011 sws_freeVec(filter->lumH);
2012 sws_freeVec(filter->lumV);
2013 sws_freeVec(filter->chrH);
2014 sws_freeVec(filter->chrV);
2015 av_freep(&filter);
2016 return NULL;
2017 }
2018
sws_allocVec(int length)2019 SwsVector *sws_allocVec(int length)
2020 {
2021 SwsVector *vec;
2022
2023 if(length <= 0 || length > INT_MAX/ sizeof(double))
2024 return NULL;
2025
2026 vec = av_malloc(sizeof(SwsVector));
2027 if (!vec)
2028 return NULL;
2029 vec->length = length;
2030 vec->coeff = av_malloc(sizeof(double) * length);
2031 if (!vec->coeff)
2032 av_freep(&vec);
2033 return vec;
2034 }
2035
sws_getGaussianVec(double variance,double quality)2036 SwsVector *sws_getGaussianVec(double variance, double quality)
2037 {
2038 const int length = (int)(variance * quality + 0.5) | 1;
2039 int i;
2040 double middle = (length - 1) * 0.5;
2041 SwsVector *vec;
2042
2043 if(variance < 0 || quality < 0)
2044 return NULL;
2045
2046 vec = sws_allocVec(length);
2047
2048 if (!vec)
2049 return NULL;
2050
2051 for (i = 0; i < length; i++) {
2052 double dist = i - middle;
2053 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
2054 sqrt(2 * variance * M_PI);
2055 }
2056
2057 sws_normalizeVec(vec, 1.0);
2058
2059 return vec;
2060 }
2061
2062 /**
2063 * Allocate and return a vector with length coefficients, all
2064 * with the same value c.
2065 */
2066 #if !FF_API_SWS_VECTOR
2067 static
2068 #endif
sws_getConstVec(double c,int length)2069 SwsVector *sws_getConstVec(double c, int length)
2070 {
2071 int i;
2072 SwsVector *vec = sws_allocVec(length);
2073
2074 if (!vec)
2075 return NULL;
2076
2077 for (i = 0; i < length; i++)
2078 vec->coeff[i] = c;
2079
2080 return vec;
2081 }
2082
2083 /**
2084 * Allocate and return a vector with just one coefficient, with
2085 * value 1.0.
2086 */
2087 #if !FF_API_SWS_VECTOR
2088 static
2089 #endif
sws_getIdentityVec(void)2090 SwsVector *sws_getIdentityVec(void)
2091 {
2092 return sws_getConstVec(1.0, 1);
2093 }
2094
sws_dcVec(SwsVector * a)2095 static double sws_dcVec(SwsVector *a)
2096 {
2097 int i;
2098 double sum = 0;
2099
2100 for (i = 0; i < a->length; i++)
2101 sum += a->coeff[i];
2102
2103 return sum;
2104 }
2105
sws_scaleVec(SwsVector * a,double scalar)2106 void sws_scaleVec(SwsVector *a, double scalar)
2107 {
2108 int i;
2109
2110 for (i = 0; i < a->length; i++)
2111 a->coeff[i] *= scalar;
2112 }
2113
sws_normalizeVec(SwsVector * a,double height)2114 void sws_normalizeVec(SwsVector *a, double height)
2115 {
2116 sws_scaleVec(a, height / sws_dcVec(a));
2117 }
2118
2119 #if FF_API_SWS_VECTOR
sws_getConvVec(SwsVector * a,SwsVector * b)2120 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
2121 {
2122 int length = a->length + b->length - 1;
2123 int i, j;
2124 SwsVector *vec = sws_getConstVec(0.0, length);
2125
2126 if (!vec)
2127 return NULL;
2128
2129 for (i = 0; i < a->length; i++) {
2130 for (j = 0; j < b->length; j++) {
2131 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
2132 }
2133 }
2134
2135 return vec;
2136 }
2137 #endif
2138
sws_sumVec(SwsVector * a,SwsVector * b)2139 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
2140 {
2141 int length = FFMAX(a->length, b->length);
2142 int i;
2143 SwsVector *vec = sws_getConstVec(0.0, length);
2144
2145 if (!vec)
2146 return NULL;
2147
2148 for (i = 0; i < a->length; i++)
2149 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2150 for (i = 0; i < b->length; i++)
2151 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
2152
2153 return vec;
2154 }
2155
2156 #if FF_API_SWS_VECTOR
sws_diffVec(SwsVector * a,SwsVector * b)2157 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
2158 {
2159 int length = FFMAX(a->length, b->length);
2160 int i;
2161 SwsVector *vec = sws_getConstVec(0.0, length);
2162
2163 if (!vec)
2164 return NULL;
2165
2166 for (i = 0; i < a->length; i++)
2167 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2168 for (i = 0; i < b->length; i++)
2169 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
2170
2171 return vec;
2172 }
2173 #endif
2174
2175 /* shift left / or right if "shift" is negative */
sws_getShiftedVec(SwsVector * a,int shift)2176 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
2177 {
2178 int length = a->length + FFABS(shift) * 2;
2179 int i;
2180 SwsVector *vec = sws_getConstVec(0.0, length);
2181
2182 if (!vec)
2183 return NULL;
2184
2185 for (i = 0; i < a->length; i++) {
2186 vec->coeff[i + (length - 1) / 2 -
2187 (a->length - 1) / 2 - shift] = a->coeff[i];
2188 }
2189
2190 return vec;
2191 }
2192
2193 #if !FF_API_SWS_VECTOR
2194 static
2195 #endif
sws_shiftVec(SwsVector * a,int shift)2196 void sws_shiftVec(SwsVector *a, int shift)
2197 {
2198 SwsVector *shifted = sws_getShiftedVec(a, shift);
2199 if (!shifted) {
2200 makenan_vec(a);
2201 return;
2202 }
2203 av_free(a->coeff);
2204 a->coeff = shifted->coeff;
2205 a->length = shifted->length;
2206 av_free(shifted);
2207 }
2208
2209 #if !FF_API_SWS_VECTOR
2210 static
2211 #endif
sws_addVec(SwsVector * a,SwsVector * b)2212 void sws_addVec(SwsVector *a, SwsVector *b)
2213 {
2214 SwsVector *sum = sws_sumVec(a, b);
2215 if (!sum) {
2216 makenan_vec(a);
2217 return;
2218 }
2219 av_free(a->coeff);
2220 a->coeff = sum->coeff;
2221 a->length = sum->length;
2222 av_free(sum);
2223 }
2224
2225 #if FF_API_SWS_VECTOR
sws_subVec(SwsVector * a,SwsVector * b)2226 void sws_subVec(SwsVector *a, SwsVector *b)
2227 {
2228 SwsVector *diff = sws_diffVec(a, b);
2229 if (!diff) {
2230 makenan_vec(a);
2231 return;
2232 }
2233 av_free(a->coeff);
2234 a->coeff = diff->coeff;
2235 a->length = diff->length;
2236 av_free(diff);
2237 }
2238
sws_convVec(SwsVector * a,SwsVector * b)2239 void sws_convVec(SwsVector *a, SwsVector *b)
2240 {
2241 SwsVector *conv = sws_getConvVec(a, b);
2242 if (!conv) {
2243 makenan_vec(a);
2244 return;
2245 }
2246 av_free(a->coeff);
2247 a->coeff = conv->coeff;
2248 a->length = conv->length;
2249 av_free(conv);
2250 }
2251
sws_cloneVec(SwsVector * a)2252 SwsVector *sws_cloneVec(SwsVector *a)
2253 {
2254 SwsVector *vec = sws_allocVec(a->length);
2255
2256 if (!vec)
2257 return NULL;
2258
2259 memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
2260
2261 return vec;
2262 }
2263 #endif
2264
2265 /**
2266 * Print with av_log() a textual representation of the vector a
2267 * if log_level <= av_log_level.
2268 */
2269 #if !FF_API_SWS_VECTOR
2270 static
2271 #endif
sws_printVec2(SwsVector * a,AVClass * log_ctx,int log_level)2272 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
2273 {
2274 int i;
2275 double max = 0;
2276 double min = 0;
2277 double range;
2278
2279 for (i = 0; i < a->length; i++)
2280 if (a->coeff[i] > max)
2281 max = a->coeff[i];
2282
2283 for (i = 0; i < a->length; i++)
2284 if (a->coeff[i] < min)
2285 min = a->coeff[i];
2286
2287 range = max - min;
2288
2289 for (i = 0; i < a->length; i++) {
2290 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
2291 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
2292 for (; x > 0; x--)
2293 av_log(log_ctx, log_level, " ");
2294 av_log(log_ctx, log_level, "|\n");
2295 }
2296 }
2297
sws_freeVec(SwsVector * a)2298 void sws_freeVec(SwsVector *a)
2299 {
2300 if (!a)
2301 return;
2302 av_freep(&a->coeff);
2303 a->length = 0;
2304 av_free(a);
2305 }
2306
sws_freeFilter(SwsFilter * filter)2307 void sws_freeFilter(SwsFilter *filter)
2308 {
2309 if (!filter)
2310 return;
2311
2312 sws_freeVec(filter->lumH);
2313 sws_freeVec(filter->lumV);
2314 sws_freeVec(filter->chrH);
2315 sws_freeVec(filter->chrV);
2316 av_free(filter);
2317 }
2318
sws_freeContext(SwsContext * c)2319 void sws_freeContext(SwsContext *c)
2320 {
2321 int i;
2322 if (!c)
2323 return;
2324
2325 for (i = 0; i < 4; i++)
2326 av_freep(&c->dither_error[i]);
2327
2328 av_freep(&c->vLumFilter);
2329 av_freep(&c->vChrFilter);
2330 av_freep(&c->hLumFilter);
2331 av_freep(&c->hChrFilter);
2332 #if HAVE_ALTIVEC
2333 av_freep(&c->vYCoeffsBank);
2334 av_freep(&c->vCCoeffsBank);
2335 #endif
2336
2337 av_freep(&c->vLumFilterPos);
2338 av_freep(&c->vChrFilterPos);
2339 av_freep(&c->hLumFilterPos);
2340 av_freep(&c->hChrFilterPos);
2341
2342 #if HAVE_MMX_INLINE
2343 #if USE_MMAP
2344 if (c->lumMmxextFilterCode)
2345 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
2346 if (c->chrMmxextFilterCode)
2347 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
2348 #elif HAVE_VIRTUALALLOC
2349 if (c->lumMmxextFilterCode)
2350 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
2351 if (c->chrMmxextFilterCode)
2352 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
2353 #else
2354 av_free(c->lumMmxextFilterCode);
2355 av_free(c->chrMmxextFilterCode);
2356 #endif
2357 c->lumMmxextFilterCode = NULL;
2358 c->chrMmxextFilterCode = NULL;
2359 #endif /* HAVE_MMX_INLINE */
2360
2361 av_freep(&c->yuvTable);
2362 av_freep(&c->formatConvBuffer);
2363
2364 sws_freeContext(c->cascaded_context[0]);
2365 sws_freeContext(c->cascaded_context[1]);
2366 sws_freeContext(c->cascaded_context[2]);
2367 memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
2368 av_freep(&c->cascaded_tmp[0]);
2369 av_freep(&c->cascaded1_tmp[0]);
2370
2371 av_freep(&c->gamma);
2372 av_freep(&c->inv_gamma);
2373
2374 ff_free_filters(c);
2375
2376 av_free(c);
2377 }
2378
sws_getCachedContext(struct SwsContext * context,int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,SwsFilter * srcFilter,SwsFilter * dstFilter,const double * param)2379 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
2380 int srcH, enum AVPixelFormat srcFormat,
2381 int dstW, int dstH,
2382 enum AVPixelFormat dstFormat, int flags,
2383 SwsFilter *srcFilter,
2384 SwsFilter *dstFilter,
2385 const double *param)
2386 {
2387 static const double default_param[2] = { SWS_PARAM_DEFAULT,
2388 SWS_PARAM_DEFAULT };
2389 int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
2390 src_v_chr_pos = -513, dst_v_chr_pos = -513;
2391
2392 if (!param)
2393 param = default_param;
2394
2395 if (context &&
2396 (context->srcW != srcW ||
2397 context->srcH != srcH ||
2398 context->srcFormat != srcFormat ||
2399 context->dstW != dstW ||
2400 context->dstH != dstH ||
2401 context->dstFormat != dstFormat ||
2402 context->flags != flags ||
2403 context->param[0] != param[0] ||
2404 context->param[1] != param[1])) {
2405
2406 av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
2407 av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
2408 av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
2409 av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
2410 sws_freeContext(context);
2411 context = NULL;
2412 }
2413
2414 if (!context) {
2415 if (!(context = sws_alloc_context()))
2416 return NULL;
2417 context->srcW = srcW;
2418 context->srcH = srcH;
2419 context->srcFormat = srcFormat;
2420 context->dstW = dstW;
2421 context->dstH = dstH;
2422 context->dstFormat = dstFormat;
2423 context->flags = flags;
2424 context->param[0] = param[0];
2425 context->param[1] = param[1];
2426
2427 av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
2428 av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
2429 av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
2430 av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
2431
2432 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2433 sws_freeContext(context);
2434 return NULL;
2435 }
2436 }
2437 return context;
2438 }
2439