build: Add feature test macros for glibc 2.19+
[libav.git] / libswscale / utils.c
CommitLineData
a4388ebd
RP
1/*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
2912e87a 4 * This file is part of Libav.
a4388ebd 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
819ee683
DB
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.
a4388ebd 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
a4388ebd 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
819ee683
DB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
a4388ebd 15 *
819ee683 16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
a4388ebd 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a4388ebd
RP
19 */
20
ef0ee7f6
DB
21#include "config.h"
22
825e463a 23#define _DEFAULT_SOURCE
ef0ee7f6
DB
24#define _SVID_SOURCE // needed for MAP_ANONYMOUS
25#include <assert.h>
a4388ebd 26#include <inttypes.h>
a4388ebd
RP
27#include <math.h>
28#include <stdio.h>
ef0ee7f6 29#include <string.h>
a4388ebd
RP
30#if HAVE_SYS_MMAN_H
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
ef0ee7f6 40
5a6e3c03 41#include "libavutil/attributes.h"
a4388ebd
RP
42#include "libavutil/avutil.h"
43#include "libavutil/bswap.h"
ef0ee7f6
DB
44#include "libavutil/cpu.h"
45#include "libavutil/intreadwrite.h"
5089ce1b 46#include "libavutil/mathematics.h"
f34fcdc8 47#include "libavutil/opt.h"
a4388ebd 48#include "libavutil/pixdesc.h"
f61bece6 49#include "libavutil/ppc/cpu.h"
c318626c 50#include "libavutil/x86/asm.h"
e0c6cce4 51#include "libavutil/x86/cpu.h"
ef0ee7f6
DB
52#include "rgb2rgb.h"
53#include "swscale.h"
54#include "swscale_internal.h"
a4388ebd
RP
55
56unsigned swscale_version(void)
57{
58 return LIBSWSCALE_VERSION_INT;
59}
60
61const char *swscale_configuration(void)
62{
29ba0911 63 return LIBAV_CONFIGURATION;
a4388ebd
RP
64}
65
66const char *swscale_license(void)
67{
68#define LICENSE_PREFIX "libswscale license: "
a03be6e1 69 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
a4388ebd
RP
70}
71
ef0ee7f6 72#define RET 0xC3 // near return opcode for x86
a4388ebd 73
347167ec 74typedef struct FormatEntry {
c1eb3e7f
JG
75 uint8_t is_supported_in :1;
76 uint8_t is_supported_out :1;
77 uint8_t is_supported_endianness :1;
347167ec
SS
78} FormatEntry;
79
716d413c
AK
80static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
81 [AV_PIX_FMT_YUV420P] = { 1, 1 },
82 [AV_PIX_FMT_YUYV422] = { 1, 1 },
83 [AV_PIX_FMT_RGB24] = { 1, 1 },
84 [AV_PIX_FMT_BGR24] = { 1, 1 },
85 [AV_PIX_FMT_YUV422P] = { 1, 1 },
86 [AV_PIX_FMT_YUV444P] = { 1, 1 },
87 [AV_PIX_FMT_YUV410P] = { 1, 1 },
88 [AV_PIX_FMT_YUV411P] = { 1, 1 },
89 [AV_PIX_FMT_GRAY8] = { 1, 1 },
90 [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
91 [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
92 [AV_PIX_FMT_PAL8] = { 1, 0 },
93 [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
94 [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
95 [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
92b099da 96 [AV_PIX_FMT_YVYU422] = { 1, 1 },
716d413c
AK
97 [AV_PIX_FMT_UYVY422] = { 1, 1 },
98 [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
99 [AV_PIX_FMT_BGR8] = { 1, 1 },
100 [AV_PIX_FMT_BGR4] = { 0, 1 },
101 [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
102 [AV_PIX_FMT_RGB8] = { 1, 1 },
103 [AV_PIX_FMT_RGB4] = { 0, 1 },
104 [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
105 [AV_PIX_FMT_NV12] = { 1, 1 },
106 [AV_PIX_FMT_NV21] = { 1, 1 },
107 [AV_PIX_FMT_ARGB] = { 1, 1 },
108 [AV_PIX_FMT_RGBA] = { 1, 1 },
109 [AV_PIX_FMT_ABGR] = { 1, 1 },
110 [AV_PIX_FMT_BGRA] = { 1, 1 },
ab839054
LB
111 [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
112 [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
716d413c
AK
113 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
114 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
115 [AV_PIX_FMT_YUV440P] = { 1, 1 },
116 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
117 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
6d5600e8
LB
118 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
119 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
7658295b
LB
120 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
121 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
122 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
123 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
124 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
125 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
126 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
127 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
128 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
129 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
130 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
131 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
132 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
133 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
134 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
135 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
136 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
137 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
716d413c
AK
138 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
139 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
0ca0924c
JG
140 [AV_PIX_FMT_RGBA64BE] = { 0, 0, 1 },
141 [AV_PIX_FMT_RGBA64LE] = { 0, 0, 1 },
716d413c
AK
142 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
143 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
144 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
145 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
146 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
147 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
148 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
149 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
150 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
151 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
152 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
153 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
154 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
155 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
156 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
157 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
158 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
159 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
e96c3b81 160 [AV_PIX_FMT_YA8] = { 1, 0 },
f84a1b59
VG
161 [AV_PIX_FMT_YA16BE] = { 1, 0 },
162 [AV_PIX_FMT_YA16LE] = { 1, 0 },
716d413c
AK
163 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
164 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
0ca0924c
JG
165 [AV_PIX_FMT_BGRA64BE] = { 0, 0, 1 },
166 [AV_PIX_FMT_BGRA64LE] = { 0, 0, 1 },
716d413c
AK
167 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
168 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
169 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
170 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
85406e7a
LB
171 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
172 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
716d413c
AK
173 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
174 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
175 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
176 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
0aebbbd0
LB
177 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
178 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
716d413c
AK
179 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
180 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
181 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
182 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
9bd6ea56
LB
183 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
184 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
4eb93bed 185 [AV_PIX_FMT_GBRP] = { 1, 1 },
d6d5ef55
MN
186 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
187 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
188 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
189 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
ef3740c3
LB
190 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
191 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
d192ac3e
MS
192 [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
193 [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
92806167 194 [AV_PIX_FMT_GBRAP] = { 1, 1 },
ac29b82e
MN
195 [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
196 [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
881477c7
LB
197 [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
198 [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
92806167
PM
199 [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
200 [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
9cfa21c8
JG
201 [AV_PIX_FMT_XYZ12BE] = { 0, 0, 1 },
202 [AV_PIX_FMT_XYZ12LE] = { 0, 0, 1 },
e78e5b73
HL
203 [AV_PIX_FMT_P010LE] = { 1, 0 },
204 [AV_PIX_FMT_P010BE] = { 1, 0 },
347167ec 205};
a4388ebd 206
716d413c 207int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
a4388ebd 208{
716d413c 209 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
ef0ee7f6 210 format_entries[pix_fmt].is_supported_in : 0;
a4388ebd
RP
211}
212
716d413c 213int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
a4388ebd 214{
716d413c 215 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
ef0ee7f6 216 format_entries[pix_fmt].is_supported_out : 0;
a4388ebd
RP
217}
218
c1eb3e7f
JG
219int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
220{
221 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
222 format_entries[pix_fmt].is_supported_endianness : 0;
223}
224
716d413c 225const char *sws_format_name(enum AVPixelFormat format)
a4388ebd 226{
0a7068fa
AK
227 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
228 if (desc)
229 return desc->name;
a4388ebd
RP
230 else
231 return "Unknown format";
232}
233
ef0ee7f6
DB
234static double getSplineCoeff(double a, double b, double c, double d,
235 double dist)
a4388ebd 236{
ef0ee7f6
DB
237 if (dist <= 1.0)
238 return ((d * dist + c) * dist + b) * dist + a;
239 else
240 return getSplineCoeff(0.0,
241 b + 2.0 * c + 3.0 * d,
242 c + 3.0 * d,
243 -b - 3.0 * c - 6.0 * d,
244 dist - 1.0);
a4388ebd
RP
245}
246
2b677ffc
DB
247static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
248 int *outFilterSize, int xInc, int srcW,
249 int dstW, int filterAlign, int one,
250 int flags, int cpu_flags,
251 SwsVector *srcFilter, SwsVector *dstFilter,
252 double param[2], int is_horizontal)
a4388ebd
RP
253{
254 int i;
255 int filterSize;
256 int filter2Size;
257 int minFilterSize;
ef0ee7f6
DB
258 int64_t *filter = NULL;
259 int64_t *filter2 = NULL;
260 const int64_t fone = 1LL << 54;
261 int ret = -1;
e66149e7 262
ef0ee7f6 263 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
a4388ebd 264
ef0ee7f6
DB
265 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
266 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
a4388ebd 267
ef0ee7f6 268 if (FFABS(xInc - 0x10000) < 10) { // unscaled
a4388ebd 269 int i;
ef0ee7f6
DB
270 filterSize = 1;
271 FF_ALLOCZ_OR_GOTO(NULL, filter,
272 dstW * sizeof(*filter) * filterSize, fail);
a4388ebd 273
ef0ee7f6
DB
274 for (i = 0; i < dstW; i++) {
275 filter[i * filterSize] = fone;
276 (*filterPos)[i] = i;
a4388ebd 277 }
ef0ee7f6 278 } else if (flags & SWS_POINT) { // lame looking point sampling mode
a4388ebd
RP
279 int i;
280 int xDstInSrc;
ef0ee7f6
DB
281 filterSize = 1;
282 FF_ALLOC_OR_GOTO(NULL, filter,
283 dstW * sizeof(*filter) * filterSize, fail);
a4388ebd 284
ef0ee7f6
DB
285 xDstInSrc = xInc / 2 - 0x8000;
286 for (i = 0; i < dstW; i++) {
287 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
a4388ebd 288
ef0ee7f6
DB
289 (*filterPos)[i] = xx;
290 filter[i] = fone;
291 xDstInSrc += xInc;
a4388ebd 292 }
ef0ee7f6
DB
293 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
294 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
a4388ebd
RP
295 int i;
296 int xDstInSrc;
ef0ee7f6
DB
297 filterSize = 2;
298 FF_ALLOC_OR_GOTO(NULL, filter,
299 dstW * sizeof(*filter) * filterSize, fail);
a4388ebd 300
ef0ee7f6
DB
301 xDstInSrc = xInc / 2 - 0x8000;
302 for (i = 0; i < dstW; i++) {
303 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
a4388ebd
RP
304 int j;
305
ef0ee7f6
DB
306 (*filterPos)[i] = xx;
307 // bilinear upscale / linear interpolate / area averaging
308 for (j = 0; j < filterSize; j++) {
309 int64_t coeff = fone - FFABS((xx << 16) - xDstInSrc) *
310 (fone >> 16);
311 if (coeff < 0)
312 coeff = 0;
313 filter[i * filterSize + j] = coeff;
a4388ebd
RP
314 xx++;
315 }
ef0ee7f6 316 xDstInSrc += xInc;
a4388ebd
RP
317 }
318 } else {
19a65b5b 319 int64_t xDstInSrc;
a4388ebd
RP
320 int sizeFactor;
321
ef0ee7f6
DB
322 if (flags & SWS_BICUBIC)
323 sizeFactor = 4;
324 else if (flags & SWS_X)
325 sizeFactor = 8;
326 else if (flags & SWS_AREA)
327 sizeFactor = 1; // downscale only, for upscale it is bilinear
328 else if (flags & SWS_GAUSS)
329 sizeFactor = 8; // infinite ;)
330 else if (flags & SWS_LANCZOS)
331 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
332 else if (flags & SWS_SINC)
333 sizeFactor = 20; // infinite ;)
334 else if (flags & SWS_SPLINE)
335 sizeFactor = 20; // infinite ;)
336 else if (flags & SWS_BILINEAR)
337 sizeFactor = 2;
a4388ebd 338 else {
ef0ee7f6 339 sizeFactor = 0; // GCC warning killer
a4388ebd
RP
340 assert(0);
341 }
342
ef0ee7f6
DB
343 if (xInc <= 1 << 16)
344 filterSize = 1 + sizeFactor; // upscale
345 else
346 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
a4388ebd 347
1254022e
RB
348 filterSize = FFMIN(filterSize, srcW - 2);
349 filterSize = FFMAX(filterSize, 1);
a4388ebd 350
ef0ee7f6
DB
351 FF_ALLOC_OR_GOTO(NULL, filter,
352 dstW * sizeof(*filter) * filterSize, fail);
a4388ebd 353
ef0ee7f6
DB
354 xDstInSrc = xInc - 0x10000;
355 for (i = 0; i < dstW; i++) {
3a177a9c 356 int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
a4388ebd 357 int j;
ef0ee7f6
DB
358 (*filterPos)[i] = xx;
359 for (j = 0; j < filterSize; j++) {
360 int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
a4388ebd
RP
361 double floatd;
362 int64_t coeff;
363
ef0ee7f6
DB
364 if (xInc > 1 << 16)
365 d = d * dstW / srcW;
366 floatd = d * (1.0 / (1 << 30));
a4388ebd
RP
367
368 if (flags & SWS_BICUBIC) {
ef0ee7f6
DB
369 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
370 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
a4388ebd 371
ef0ee7f6 372 if (d >= 1LL << 31) {
8305041e
RB
373 coeff = 0.0;
374 } else {
375 int64_t dd = (d * d) >> 30;
376 int64_t ddd = (dd * d) >> 30;
377
ef0ee7f6
DB
378 if (d < 1LL << 30)
379 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
380 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
381 (6 * (1 << 24) - 2 * B) * (1 << 30);
8305041e 382 else
ef0ee7f6
DB
383 coeff = (-B - 6 * C) * ddd +
384 (6 * B + 30 * C) * dd +
385 (-12 * B - 48 * C) * d +
386 (8 * B + 24 * C) * (1 << 30);
8305041e 387 }
ef0ee7f6 388 coeff *= fone >> (30 + 24);
a4388ebd 389 }
a4388ebd 390 else if (flags & SWS_X) {
ef0ee7f6 391 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
a4388ebd
RP
392 double c;
393
ef0ee7f6
DB
394 if (floatd < 1.0)
395 c = cos(floatd * M_PI);
a4388ebd 396 else
ef0ee7f6
DB
397 c = -1.0;
398 if (c < 0.0)
399 c = -pow(-c, A);
400 else
401 c = pow(c, A);
402 coeff = (c * 0.5 + 0.5) * fone;
a4388ebd 403 } else if (flags & SWS_AREA) {
ef0ee7f6
DB
404 int64_t d2 = d - (1 << 29);
405 if (d2 * xInc < -(1LL << (29 + 16)))
406 coeff = 1.0 * (1LL << (30 + 16));
407 else if (d2 * xInc < (1LL << (29 + 16)))
408 coeff = -d2 * xInc + (1LL << (29 + 16));
409 else
410 coeff = 0.0;
411 coeff *= fone >> (30 + 16);
a4388ebd 412 } else if (flags & SWS_GAUSS) {
ef0ee7f6
DB
413 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
414 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
a4388ebd 415 } else if (flags & SWS_SINC) {
ef0ee7f6 416 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
a4388ebd 417 } else if (flags & SWS_LANCZOS) {
ef0ee7f6
DB
418 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
419 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
420 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
421 if (floatd > p)
422 coeff = 0;
a4388ebd 423 } else if (flags & SWS_BILINEAR) {
ef0ee7f6
DB
424 coeff = (1 << 30) - d;
425 if (coeff < 0)
426 coeff = 0;
a4388ebd
RP
427 coeff *= fone >> 30;
428 } else if (flags & SWS_SPLINE) {
ef0ee7f6
DB
429 double p = -2.196152422706632;
430 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
a4388ebd 431 } else {
ef0ee7f6 432 coeff = 0.0; // GCC warning killer
a4388ebd
RP
433 assert(0);
434 }
435
ef0ee7f6 436 filter[i * filterSize + j] = coeff;
a4388ebd
RP
437 xx++;
438 }
ef0ee7f6 439 xDstInSrc += 2 * xInc;
a4388ebd
RP
440 }
441 }
442
443 /* apply src & dst Filter to filter -> filter2
ef0ee7f6
DB
444 * av_free(filter);
445 */
446 assert(filterSize > 0);
447 filter2Size = filterSize;
448 if (srcFilter)
449 filter2Size += srcFilter->length - 1;
450 if (dstFilter)
451 filter2Size += dstFilter->length - 1;
452 assert(filter2Size > 0);
453 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
454
455 for (i = 0; i < dstW; i++) {
a4388ebd
RP
456 int j, k;
457
ef0ee7f6
DB
458 if (srcFilter) {
459 for (k = 0; k < srcFilter->length; k++) {
460 for (j = 0; j < filterSize; j++)
461 filter2[i * filter2Size + k + j] +=
462 srcFilter->coeff[k] * filter[i * filterSize + j];
a4388ebd
RP
463 }
464 } else {
ef0ee7f6
DB
465 for (j = 0; j < filterSize; j++)
466 filter2[i * filter2Size + j] = filter[i * filterSize + j];
a4388ebd 467 }
ef0ee7f6 468 // FIXME dstFilter
a4388ebd 469
ef0ee7f6 470 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
a4388ebd
RP
471 }
472 av_freep(&filter);
473
474 /* try to reduce the filter-size (step1 find size and shift left) */
475 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
ef0ee7f6
DB
476 minFilterSize = 0;
477 for (i = dstW - 1; i >= 0; i--) {
478 int min = filter2Size;
a4388ebd 479 int j;
ef0ee7f6 480 int64_t cutOff = 0.0;
a4388ebd
RP
481
482 /* get rid of near zero elements on the left by shifting left */
ef0ee7f6 483 for (j = 0; j < filter2Size; j++) {
a4388ebd 484 int k;
ef0ee7f6 485 cutOff += FFABS(filter2[i * filter2Size]);
a4388ebd 486
ef0ee7f6
DB
487 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
488 break;
a4388ebd 489
ef0ee7f6
DB
490 /* preserve monotonicity because the core can't handle the
491 * filter otherwise */
492 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
493 break;
a4388ebd
RP
494
495 // move filter coefficients left
ef0ee7f6
DB
496 for (k = 1; k < filter2Size; k++)
497 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
498 filter2[i * filter2Size + k - 1] = 0;
a4388ebd
RP
499 (*filterPos)[i]++;
500 }
501
ef0ee7f6 502 cutOff = 0;
a4388ebd 503 /* count near zeros on the right */
ef0ee7f6
DB
504 for (j = filter2Size - 1; j > 0; j--) {
505 cutOff += FFABS(filter2[i * filter2Size + j]);
a4388ebd 506
ef0ee7f6
DB
507 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
508 break;
a4388ebd
RP
509 min--;
510 }
511
ef0ee7f6
DB
512 if (min > minFilterSize)
513 minFilterSize = min;
a4388ebd
RP
514 }
515
f61bece6 516 if (PPC_ALTIVEC(cpu_flags)) {
ef0ee7f6 517 // we can handle the special case 4, so we don't want to go the full 8
a4388ebd
RP
518 if (minFilterSize < 5)
519 filterAlign = 4;
520
ef0ee7f6
DB
521 /* We really don't want to waste our time doing useless computation, so
522 * fall back on the scalar C code for very small filters.
523 * Vectorizing is worth it only if you have a decent-sized vector. */
a4388ebd
RP
524 if (minFilterSize < 3)
525 filterAlign = 1;
526 }
527
e0c6cce4 528 if (INLINE_MMX(cpu_flags)) {
a4388ebd
RP
529 // special case for unscaled vertical filtering
530 if (minFilterSize == 1 && filterAlign == 2)
ef0ee7f6 531 filterAlign = 1;
a4388ebd
RP
532 }
533
534 assert(minFilterSize > 0);
ef0ee7f6 535 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
a4388ebd 536 assert(filterSize > 0);
ef0ee7f6
DB
537 filter = av_malloc(filterSize * dstW * sizeof(*filter));
538 if (filterSize >= MAX_FILTER_SIZE * 16 /
539 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
a4388ebd 540 goto fail;
ef0ee7f6 541 *outFilterSize = filterSize;
a4388ebd 542
ef0ee7f6
DB
543 if (flags & SWS_PRINT_INFO)
544 av_log(NULL, AV_LOG_VERBOSE,
545 "SwScaler: reducing / aligning filtersize %d -> %d\n",
546 filter2Size, filterSize);
a4388ebd 547 /* try to reduce the filter-size (step2 reduce it) */
ef0ee7f6 548 for (i = 0; i < dstW; i++) {
a4388ebd
RP
549 int j;
550
ef0ee7f6
DB
551 for (j = 0; j < filterSize; j++) {
552 if (j >= filter2Size)
553 filter[i * filterSize + j] = 0;
554 else
555 filter[i * filterSize + j] = filter2[i * filter2Size + j];
556 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
557 filter[i * filterSize + j] = 0;
a4388ebd
RP
558 }
559 }
560
ef0ee7f6 561 // FIXME try to align filterPos if possible
a4388ebd 562
ef0ee7f6 563 // fix borders
d49352c7
RB
564 if (is_horizontal) {
565 for (i = 0; i < dstW; i++) {
566 int j;
567 if ((*filterPos)[i] < 0) {
568 // move filter coefficients left to compensate for filterPos
569 for (j = 1; j < filterSize; j++) {
570 int left = FFMAX(j + (*filterPos)[i], 0);
571 filter[i * filterSize + left] += filter[i * filterSize + j];
ef0ee7f6 572 filter[i * filterSize + j] = 0;
d49352c7
RB
573 }
574 (*filterPos)[i] = 0;
a4388ebd 575 }
a4388ebd 576
d49352c7
RB
577 if ((*filterPos)[i] + filterSize > srcW) {
578 int shift = (*filterPos)[i] + filterSize - srcW;
579 // move filter coefficients right to compensate for filterPos
580 for (j = filterSize - 2; j >= 0; j--) {
581 int right = FFMIN(j + shift, filterSize - 1);
582 filter[i * filterSize + right] += filter[i * filterSize + j];
ef0ee7f6 583 filter[i * filterSize + j] = 0;
d49352c7
RB
584 }
585 (*filterPos)[i] = srcW - filterSize;
a4388ebd 586 }
a4388ebd
RP
587 }
588 }
589
590 // Note the +1 is for the MMX scaler which reads over the end
591 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
ef0ee7f6
DB
592 FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
593 *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
a4388ebd
RP
594
595 /* normalize & store in outFilter */
ef0ee7f6 596 for (i = 0; i < dstW; i++) {
a4388ebd 597 int j;
ef0ee7f6
DB
598 int64_t error = 0;
599 int64_t sum = 0;
a4388ebd 600
ef0ee7f6
DB
601 for (j = 0; j < filterSize; j++) {
602 sum += filter[i * filterSize + j];
a4388ebd 603 }
ef0ee7f6
DB
604 sum = (sum + one / 2) / one;
605 for (j = 0; j < *outFilterSize; j++) {
606 int64_t v = filter[i * filterSize + j] + error;
607 int intV = ROUNDED_DIV(v, sum);
608 (*outFilter)[i * (*outFilterSize) + j] = intV;
609 error = v - intV * sum;
a4388ebd
RP
610 }
611 }
612
ef0ee7f6
DB
613 (*filterPos)[dstW + 0] =
614 (*filterPos)[dstW + 1] =
615 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
616 * read over the end */
617 for (i = 0; i < *outFilterSize; i++) {
618 int k = (dstW - 1) * (*outFilterSize) + i;
ea540401
RB
619 (*outFilter)[k + 1 * (*outFilterSize)] =
620 (*outFilter)[k + 2 * (*outFilterSize)] =
621 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
a4388ebd
RP
622 }
623
ef0ee7f6
DB
624 ret = 0;
625
a4388ebd
RP
626fail:
627 av_free(filter);
628 av_free(filter2);
629 return ret;
630}
631
17337f54 632#if HAVE_MMXEXT_INLINE
2b677ffc
DB
633static av_cold int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
634 int16_t *filter, int32_t *filterPos,
635 int numSplits)
a4388ebd
RP
636{
637 uint8_t *fragmentA;
638 x86_reg imm8OfPShufW1A;
639 x86_reg imm8OfPShufW2A;
640 x86_reg fragmentLengthA;
641 uint8_t *fragmentB;
642 x86_reg imm8OfPShufW1B;
643 x86_reg imm8OfPShufW2B;
644 x86_reg fragmentLengthB;
645 int fragmentPos;
646
647 int xpos, i;
648
649 // create an optimized horizontal scaling routine
652f5185 650 /* This scaler is made of runtime-generated MMXEXT code using specially tuned
ef0ee7f6
DB
651 * pshufw instructions. For every four output pixels, if four input pixels
652 * are enough for the fast bilinear scaling, then a chunk of fragmentB is
653 * used. If five input pixels are needed, then a chunk of fragmentA is used.
a4388ebd
RP
654 */
655
ef0ee7f6 656 // code fragment
a4388ebd 657
ef0ee7f6 658 __asm__ volatile (
a4388ebd 659 "jmp 9f \n\t"
ef0ee7f6 660 // Begin
a4388ebd 661 "0: \n\t"
1e9c5bf4
DB
662 "movq (%%"FF_REG_d", %%"FF_REG_a"), %%mm3 \n\t"
663 "movd (%%"FF_REG_c", %%"FF_REG_S"), %%mm0 \n\t"
664 "movd 1(%%"FF_REG_c", %%"FF_REG_S"), %%mm1 \n\t"
a4388ebd
RP
665 "punpcklbw %%mm7, %%mm1 \n\t"
666 "punpcklbw %%mm7, %%mm0 \n\t"
667 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
668 "1: \n\t"
669 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
670 "2: \n\t"
671 "psubw %%mm1, %%mm0 \n\t"
1e9c5bf4 672 "movl 8(%%"FF_REG_b", %%"FF_REG_a"), %%esi \n\t"
a4388ebd
RP
673 "pmullw %%mm3, %%mm0 \n\t"
674 "psllw $7, %%mm1 \n\t"
675 "paddw %%mm1, %%mm0 \n\t"
676
1e9c5bf4 677 "movq %%mm0, (%%"FF_REG_D", %%"FF_REG_a") \n\t"
a4388ebd 678
1e9c5bf4 679 "add $8, %%"FF_REG_a" \n\t"
ef0ee7f6 680 // End
a4388ebd 681 "9: \n\t"
ef0ee7f6
DB
682 // "int $3 \n\t"
683 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
684 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
685 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
a4388ebd
RP
686 "dec %1 \n\t"
687 "dec %2 \n\t"
688 "sub %0, %1 \n\t"
689 "sub %0, %2 \n\t"
ef0ee7f6 690 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
a4388ebd
RP
691 "sub %0, %3 \n\t"
692
693
ef0ee7f6
DB
694 : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
695 "=r" (fragmentLengthA)
696 );
a4388ebd 697
ef0ee7f6 698 __asm__ volatile (
a4388ebd 699 "jmp 9f \n\t"
ef0ee7f6 700 // Begin
a4388ebd 701 "0: \n\t"
1e9c5bf4
DB
702 "movq (%%"FF_REG_d", %%"FF_REG_a"), %%mm3 \n\t"
703 "movd (%%"FF_REG_c", %%"FF_REG_S"), %%mm0 \n\t"
a4388ebd
RP
704 "punpcklbw %%mm7, %%mm0 \n\t"
705 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
706 "1: \n\t"
707 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
708 "2: \n\t"
709 "psubw %%mm1, %%mm0 \n\t"
1e9c5bf4 710 "movl 8(%%"FF_REG_b", %%"FF_REG_a"), %%esi \n\t"
a4388ebd
RP
711 "pmullw %%mm3, %%mm0 \n\t"
712 "psllw $7, %%mm1 \n\t"
713 "paddw %%mm1, %%mm0 \n\t"
714
1e9c5bf4 715 "movq %%mm0, (%%"FF_REG_D", %%"FF_REG_a") \n\t"
a4388ebd 716
1e9c5bf4 717 "add $8, %%"FF_REG_a" \n\t"
ef0ee7f6 718 // End
a4388ebd 719 "9: \n\t"
ef0ee7f6
DB
720 // "int $3 \n\t"
721 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
722 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
723 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
a4388ebd
RP
724 "dec %1 \n\t"
725 "dec %2 \n\t"
726 "sub %0, %1 \n\t"
727 "sub %0, %2 \n\t"
ef0ee7f6 728 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
a4388ebd
RP
729 "sub %0, %3 \n\t"
730
731
ef0ee7f6
DB
732 : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
733 "=r" (fragmentLengthB)
734 );
a4388ebd 735
ef0ee7f6
DB
736 xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
737 fragmentPos = 0;
a4388ebd 738
ef0ee7f6
DB
739 for (i = 0; i < dstW / numSplits; i++) {
740 int xx = xpos >> 16;
a4388ebd 741
ef0ee7f6
DB
742 if ((i & 3) == 0) {
743 int a = 0;
744 int b = ((xpos + xInc) >> 16) - xx;
745 int c = ((xpos + xInc * 2) >> 16) - xx;
746 int d = ((xpos + xInc * 3) >> 16) - xx;
747 int inc = (d + 1 < 4);
748 uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
749 x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
750 x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
751 x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
752 int maxShift = 3 - (d + inc);
753 int shift = 0;
a4388ebd
RP
754
755 if (filterCode) {
ef0ee7f6
DB
756 filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
757 filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
758 filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
759 filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
760 filterPos[i / 2] = xx;
a4388ebd
RP
761
762 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
763
ef0ee7f6
DB
764 filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
765 ((b + inc) << 2) |
766 ((c + inc) << 4) |
767 ((d + inc) << 6);
768 filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
769 (c << 4) |
770 (d << 6);
771
772 if (i + 4 - inc >= dstW)
773 shift = maxShift; // avoid overread
774 else if ((filterPos[i / 2] & 3) <= maxShift)
775 shift = filterPos[i / 2] & 3; // align
776
777 if (shift && i >= shift) {
778 filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
779 filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
780 filterPos[i / 2] -= shift;
a4388ebd
RP
781 }
782 }
783
ef0ee7f6 784 fragmentPos += fragmentLength;
a4388ebd
RP
785
786 if (filterCode)
ef0ee7f6 787 filterCode[fragmentPos] = RET;
a4388ebd 788 }
ef0ee7f6 789 xpos += xInc;
a4388ebd
RP
790 }
791 if (filterCode)
ef0ee7f6 792 filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
a4388ebd
RP
793
794 return fragmentPos + 1;
795}
17337f54 796#endif /* HAVE_MMXEXT_INLINE */
a4388ebd 797
716d413c 798static void getSubSampleFactors(int *h, int *v, enum AVPixelFormat format)
a4388ebd 799{
0a7068fa
AK
800 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
801 *h = desc->log2_chroma_w;
802 *v = desc->log2_chroma_h;
a4388ebd
RP
803}
804
96c1e6d4
DB
805int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
806 int srcRange, const int table[4], int dstRange,
807 int brightness, int contrast, int saturation)
a4388ebd 808{
0a7068fa
AK
809 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
810 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
ef0ee7f6
DB
811 memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
812 memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
a4388ebd 813
ef0ee7f6
DB
814 c->brightness = brightness;
815 c->contrast = contrast;
816 c->saturation = saturation;
817 c->srcRange = srcRange;
818 c->dstRange = dstRange;
819 if (isYUV(c->dstFormat) || isGray(c->dstFormat))
820 return -1;
a4388ebd 821
0a7068fa
AK
822 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
823 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
635d4aed 824
ef0ee7f6
DB
825 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
826 contrast, saturation);
827 // FIXME factorize
a4388ebd 828
c2503d9c
DB
829 if (ARCH_PPC)
830 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
831 contrast, saturation);
a4388ebd
RP
832 return 0;
833}
834
96c1e6d4
DB
835int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
836 int *srcRange, int **table, int *dstRange,
837 int *brightness, int *contrast, int *saturation)
a4388ebd 838{
ef0ee7f6
DB
839 if (isYUV(c->dstFormat) || isGray(c->dstFormat))
840 return -1;
a4388ebd 841
ef0ee7f6
DB
842 *inv_table = c->srcColorspaceTable;
843 *table = c->dstColorspaceTable;
844 *srcRange = c->srcRange;
845 *dstRange = c->dstRange;
846 *brightness = c->brightness;
847 *contrast = c->contrast;
848 *saturation = c->saturation;
a4388ebd
RP
849
850 return 0;
851}
852
716d413c 853static int handle_jpeg(enum AVPixelFormat *format)
a4388ebd
RP
854{
855 switch (*format) {
716d413c
AK
856 case AV_PIX_FMT_YUVJ420P:
857 *format = AV_PIX_FMT_YUV420P;
ef0ee7f6 858 return 1;
716d413c
AK
859 case AV_PIX_FMT_YUVJ422P:
860 *format = AV_PIX_FMT_YUV422P;
ef0ee7f6 861 return 1;
716d413c
AK
862 case AV_PIX_FMT_YUVJ444P:
863 *format = AV_PIX_FMT_YUV444P;
ef0ee7f6 864 return 1;
716d413c
AK
865 case AV_PIX_FMT_YUVJ440P:
866 *format = AV_PIX_FMT_YUV440P;
ef0ee7f6
DB
867 return 1;
868 default:
869 return 0;
a4388ebd
RP
870 }
871}
872
c24b404b
SS
873SwsContext *sws_alloc_context(void)
874{
ef0ee7f6 875 SwsContext *c = av_mallocz(sizeof(SwsContext));
635d4aed 876
f73f76fd 877 if (c) {
c0fd2fb2 878 c->av_class = &ff_sws_context_class;
f73f76fd
XW
879 av_opt_set_defaults(c);
880 }
635d4aed
MN
881
882 return c;
883}
884
5a6e3c03
DB
885av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
886 SwsFilter *dstFilter)
c24b404b 887{
a4388ebd
RP
888 int i;
889 int usesVFilter, usesHFilter;
890 int unscaled;
ef0ee7f6
DB
891 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
892 int srcW = c->srcW;
893 int srcH = c->srcH;
894 int dstW = c->dstW;
895 int dstH = c->dstH;
896 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16);
897 int dst_stride_px = dst_stride >> 1;
e66149e7 898 int flags, cpu_flags;
716d413c
AK
899 enum AVPixelFormat srcFormat = c->srcFormat;
900 enum AVPixelFormat dstFormat = c->dstFormat;
0a7068fa
AK
901 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
902 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
635d4aed 903
e66149e7
RB
904 cpu_flags = av_get_cpu_flags();
905 flags = c->flags;
c4fd283a 906 emms_c();
ef0ee7f6 907 if (!rgb15to16)
facdfe40 908 ff_rgb2rgb_init();
a4388ebd
RP
909
910 unscaled = (srcW == dstW && srcH == dstH);
911
c1eb3e7f
JG
912 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
913 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
347167ec 914 if (!sws_isSupportedInput(srcFormat)) {
ef0ee7f6
DB
915 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
916 sws_format_name(srcFormat));
635d4aed 917 return AVERROR(EINVAL);
a4388ebd 918 }
347167ec 919 if (!sws_isSupportedOutput(dstFormat)) {
ef0ee7f6
DB
920 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
921 sws_format_name(dstFormat));
635d4aed 922 return AVERROR(EINVAL);
a4388ebd 923 }
c1eb3e7f 924 }
a4388ebd 925
ef0ee7f6
DB
926 i = flags & (SWS_POINT |
927 SWS_AREA |
928 SWS_BILINEAR |
929 SWS_FAST_BILINEAR |
930 SWS_BICUBIC |
931 SWS_X |
932 SWS_GAUSS |
933 SWS_LANCZOS |
934 SWS_SINC |
935 SWS_SPLINE |
936 SWS_BICUBLIN);
6b3ff6f9
VG
937
938 /* provide a default scaler if not set by caller */
939 if (!i) {
940 if (dstW < srcW && dstH < srcH)
941 flags |= SWS_GAUSS;
942 else if (dstW > srcW && dstH > srcH)
943 flags |= SWS_SINC;
944 else
945 flags |= SWS_LANCZOS;
946 c->flags = flags;
947 } else if (i & (i - 1)) {
ef0ee7f6
DB
948 av_log(c, AV_LOG_ERROR,
949 "Exactly one scaler algorithm must be chosen\n");
635d4aed 950 return AVERROR(EINVAL);
a4388ebd 951 }
a4388ebd 952 /* sanity check */
ef0ee7f6
DB
953 if (srcW < 4 || srcH < 1 || dstW < 8 || dstH < 1) {
954 /* FIXME check if these are enough and try to lower them after
955 * fixing the relevant parts of the code */
3636e791 956 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
a4388ebd 957 srcW, srcH, dstW, dstH);
635d4aed 958 return AVERROR(EINVAL);
a4388ebd 959 }
a4388ebd 960
ef0ee7f6
DB
961 if (!dstFilter)
962 dstFilter = &dummyFilter;
963 if (!srcFilter)
964 srcFilter = &dummyFilter;
a4388ebd 965
ef0ee7f6
DB
966 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
967 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
0a7068fa
AK
968 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
969 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
ef0ee7f6 970 c->vRounder = 4 * 0x0001000100010001ULL;
a4388ebd 971
ef0ee7f6
DB
972 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
973 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
974 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
975 (dstFilter->chrV && dstFilter->chrV->length > 1);
976 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
977 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
978 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
979 (dstFilter->chrH && dstFilter->chrH->length > 1);
a4388ebd
RP
980
981 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
982 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
983
d6d5ef55 984 if (isPlanarRGB(dstFormat)) {
4eb93bed
MN
985 if (!(flags & SWS_FULL_CHR_H_INT)) {
986 av_log(c, AV_LOG_DEBUG,
987 "%s output is not supported with half chroma resolution, switching to full\n",
988 av_get_pix_fmt_name(dstFormat));
989 flags |= SWS_FULL_CHR_H_INT;
990 c->flags = flags;
991 }
992 }
993
ef0ee7f6
DB
994 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
995 * chroma interpolation */
8f440223 996 if (flags & SWS_FULL_CHR_H_INT &&
ef0ee7f6 997 isAnyRGB(dstFormat) &&
d6d5ef55 998 !isPlanarRGB(dstFormat) &&
716d413c
AK
999 dstFormat != AV_PIX_FMT_RGBA &&
1000 dstFormat != AV_PIX_FMT_ARGB &&
1001 dstFormat != AV_PIX_FMT_BGRA &&
1002 dstFormat != AV_PIX_FMT_ABGR &&
1003 dstFormat != AV_PIX_FMT_RGB24 &&
1004 dstFormat != AV_PIX_FMT_BGR24) {
8f440223
RB
1005 av_log(c, AV_LOG_ERROR,
1006 "full chroma interpolation for destination format '%s' not yet implemented\n",
1007 sws_format_name(dstFormat));
ef0ee7f6 1008 flags &= ~SWS_FULL_CHR_H_INT;
8f440223
RB
1009 c->flags = flags;
1010 }
ef0ee7f6
DB
1011 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1012 c->chrDstHSubSample = 1;
a4388ebd
RP
1013
1014 // drop some chroma lines if the user wants it
ef0ee7f6
DB
1015 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1016 SWS_SRC_V_CHR_DROP_SHIFT;
1017 c->chrSrcVSubSample += c->vChrDrop;
1018
1019 /* drop every other pixel for chroma calculation unless user
1020 * wants full chroma */
1021 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
716d413c
AK
1022 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1023 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1024 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
7b41c24c
MN
1025 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1026 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
f6a9c20a 1027 srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
328ea6a9 1028 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
7b41c24c 1029 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
ef0ee7f6
DB
1030 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1031 (flags & SWS_FAST_BILINEAR)))
1032 c->chrSrcHSubSample = 1;
a4388ebd 1033
e8bc6422
CB
1034 // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
1035 c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1036 c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1037 c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1038 c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
a4388ebd 1039
a4388ebd 1040 /* unscaled special cases */
ef0ee7f6
DB
1041 if (unscaled && !usesHFilter && !usesVFilter &&
1042 (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
a4388ebd
RP
1043 ff_get_unscaled_swscale(c);
1044
1909f6b1 1045 if (c->swscale) {
ef0ee7f6
DB
1046 if (flags & SWS_PRINT_INFO)
1047 av_log(c, AV_LOG_INFO,
1048 "using unscaled %s -> %s special converter\n",
a4388ebd 1049 sws_format_name(srcFormat), sws_format_name(dstFormat));
635d4aed 1050 return 0;
a4388ebd
RP
1051 }
1052 }
1053
2268db2c 1054 c->srcBpc = desc_src->comp[0].depth;
28c1115a
RB
1055 if (c->srcBpc < 8)
1056 c->srcBpc = 8;
2268db2c 1057 c->dstBpc = desc_dst->comp[0].depth;
28c1115a
RB
1058 if (c->dstBpc < 8)
1059 c->dstBpc = 8;
1060 if (c->dstBpc == 16)
ef1ee362 1061 dst_stride <<= 1;
28c1115a 1062 FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
124e5645 1063 (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
28c1115a 1064 fail);
328ea6a9 1065 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 12) {
a65bdceb
DB
1066 c->canMMXEXTBeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
1067 (srcW & 15) == 0) ? 1 : 0;
1068 if (!c->canMMXEXTBeUsed && dstW >= srcW && (srcW & 15) == 0
ef0ee7f6
DB
1069 && (flags & SWS_FAST_BILINEAR)) {
1070 if (flags & SWS_PRINT_INFO)
1071 av_log(c, AV_LOG_INFO,
652f5185 1072 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
a4388ebd 1073 }
ef0ee7f6 1074 if (usesHFilter)
a65bdceb 1075 c->canMMXEXTBeUsed = 0;
ef0ee7f6 1076 } else
a65bdceb 1077 c->canMMXEXTBeUsed = 0;
ef0ee7f6
DB
1078
1079 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1080 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1081
1082 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1083 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1084 * correct scaling.
1085 * n-2 is the last chrominance sample available.
1086 * This is not perfect, but no one should notice the difference, the more
1087 * correct variant would be like the vertical one, but that would require
1088 * some special code for the first and last pixel */
1089 if (flags & SWS_FAST_BILINEAR) {
a65bdceb 1090 if (c->canMMXEXTBeUsed) {
ef0ee7f6
DB
1091 c->lumXInc += 20;
1092 c->chrXInc += 20;
a4388ebd 1093 }
ef0ee7f6 1094 // we don't use the x86 asm scaler if MMX is available
e0c6cce4 1095 else if (INLINE_MMX(cpu_flags)) {
ef0ee7f6
DB
1096 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1097 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
a4388ebd
RP
1098 }
1099 }
1100
95cd815c
MR
1101#define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1102
a4388ebd
RP
1103 /* precalculate horizontal scaler filter coefficients */
1104 {
17337f54 1105#if HAVE_MMXEXT_INLINE
a4388ebd 1106// can't downscale !!!
a65bdceb 1107 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
d8eda370
DB
1108 c->lumMmxextFilterCodeSize = init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1109 NULL, NULL, 8);
1110 c->chrMmxextFilterCodeSize = init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1111 NULL, NULL, NULL, 4);
a4388ebd 1112
95cd815c 1113#if USE_MMAP
a65bdceb
DB
1114 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1115 PROT_READ | PROT_WRITE,
1116 MAP_PRIVATE | MAP_ANONYMOUS,
1117 -1, 0);
1118 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1119 PROT_READ | PROT_WRITE,
1120 MAP_PRIVATE | MAP_ANONYMOUS,
1121 -1, 0);
a4388ebd 1122#elif HAVE_VIRTUALALLOC
a65bdceb
DB
1123 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1124 c->lumMmxextFilterCodeSize,
1125 MEM_COMMIT,
1126 PAGE_EXECUTE_READWRITE);
1127 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1128 c->chrMmxextFilterCodeSize,
1129 MEM_COMMIT,
1130 PAGE_EXECUTE_READWRITE);
a4388ebd 1131#else
a65bdceb
DB
1132 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1133 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
a4388ebd
RP
1134#endif
1135
a65bdceb 1136 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
635d4aed 1137 return AVERROR(ENOMEM);
ef0ee7f6
DB
1138 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1139 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1140 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1141 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
a4388ebd 1142
d8eda370
DB
1143 init_hscaler_mmxext(dstW, c->lumXInc, c->lumMmxextFilterCode,
1144 c->hLumFilter, c->hLumFilterPos, 8);
1145 init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1146 c->hChrFilter, c->hChrFilterPos, 4);
a4388ebd 1147
95cd815c 1148#if USE_MMAP
a65bdceb
DB
1149 mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
1150 mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
a4388ebd
RP
1151#endif
1152 } else
17337f54 1153#endif /* HAVE_MMXEXT_INLINE */
a4388ebd 1154 {
79aec43c
DB
1155 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1156 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
a4388ebd 1157
ef0ee7f6
DB
1158 if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1159 &c->hLumFilterSize, c->lumXInc,
1160 srcW, dstW, filterAlign, 1 << 14,
1161 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1162 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1163 c->param, 1) < 0)
a4388ebd 1164 goto fail;
ef0ee7f6
DB
1165 if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1166 &c->hChrFilterSize, c->chrXInc,
1167 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1168 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1169 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1170 c->param, 1) < 0)
a4388ebd
RP
1171 goto fail;
1172 }
1173 } // initialize horizontal stuff
1174
1175 /* precalculate vertical scaler filter coefficients */
1176 {
79aec43c
DB
1177 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1178 PPC_ALTIVEC(cpu_flags) ? 8 : 1;
a4388ebd 1179
ef0ee7f6
DB
1180 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1181 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1182 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1183 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1184 c->param, 0) < 0)
a4388ebd 1185 goto fail;
ef0ee7f6
DB
1186 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1187 c->chrYInc, c->chrSrcH, c->chrDstH,
1188 filterAlign, (1 << 12),
1189 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1190 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1191 c->param, 0) < 0)
a4388ebd
RP
1192 goto fail;
1193
f684f3c5 1194#if HAVE_ALTIVEC
ef0ee7f6
DB
1195 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1196 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
a4388ebd 1197
ef0ee7f6 1198 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
a4388ebd
RP
1199 int j;
1200 short *p = (short *)&c->vYCoeffsBank[i];
ef0ee7f6 1201 for (j = 0; j < 8; j++)
a4388ebd
RP
1202 p[j] = c->vLumFilter[i];
1203 }
1204
ef0ee7f6 1205 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
a4388ebd
RP
1206 int j;
1207 short *p = (short *)&c->vCCoeffsBank[i];
ef0ee7f6 1208 for (j = 0; j < 8; j++)
a4388ebd
RP
1209 p[j] = c->vChrFilter[i];
1210 }
1211#endif
1212 }
1213
1214 // calculate buffer sizes so that they won't run out while handling these damn slices
ef0ee7f6
DB
1215 c->vLumBufSize = c->vLumFilterSize;
1216 c->vChrBufSize = c->vChrFilterSize;
1217 for (i = 0; i < dstH; i++) {
1218 int chrI = (int64_t)i * c->chrDstH / dstH;
1219 int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1220 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1221 << c->chrSrcVSubSample));
1222
1223 nextSlice >>= c->chrSrcVSubSample;
1224 nextSlice <<= c->chrSrcVSubSample;
1225 if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1226 c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1227 if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1228 (nextSlice >> c->chrSrcVSubSample))
1229 c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1230 c->vChrFilterPos[chrI];
a4388ebd
RP
1231 }
1232
ef0ee7f6
DB
1233 /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1234 * need to allocate several megabytes to handle all possible cases) */
1235 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1236 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1237 FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
a4388ebd 1238 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
ef0ee7f6
DB
1239 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1240 /* Note we need at least one pixel more at the end because of the MMX code
1241 * (just in case someone wants to replace the 4000/8000). */
a4388ebd 1242 /* align at 16 bytes for AltiVec */
ef0ee7f6
DB
1243 for (i = 0; i < c->vLumBufSize; i++) {
1244 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1245 dst_stride + 16, fail);
1246 c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
a4388ebd 1247 }
28c1115a 1248 // 64 / (c->dstBpc & ~7) is the same as 16 / sizeof(scaling_intermediate)
ef0ee7f6 1249 c->uv_off_px = dst_stride_px + 64 / (c->dstBpc & ~7);
baba2eed 1250 c->uv_off_byte = dst_stride + 16;
ef0ee7f6
DB
1251 for (i = 0; i < c->vChrBufSize; i++) {
1252 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1253 dst_stride * 2 + 32, fail);
1254 c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1255 c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1256 = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
a4388ebd
RP
1257 }
1258 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
ef0ee7f6
DB
1259 for (i = 0; i < c->vLumBufSize; i++) {
1260 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1261 dst_stride + 16, fail);
1262 c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
a4388ebd
RP
1263 }
1264
ef0ee7f6
DB
1265 // try to avoid drawing green stuff between the right end and the stride end
1266 for (i = 0; i < c->vChrBufSize; i++)
1267 memset(c->chrUPixBuf[i], 64, dst_stride * 2 + 1);
a4388ebd
RP
1268
1269 assert(c->chrDstH <= dstH);
1270
ef0ee7f6
DB
1271 if (flags & SWS_PRINT_INFO) {
1272 if (flags & SWS_FAST_BILINEAR)
1273 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1274 else if (flags & SWS_BILINEAR)
1275 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1276 else if (flags & SWS_BICUBIC)
1277 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1278 else if (flags & SWS_X)
1279 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1280 else if (flags & SWS_POINT)
1281 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1282 else if (flags & SWS_AREA)
1283 av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1284 else if (flags & SWS_BICUBLIN)
1285 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1286 else if (flags & SWS_GAUSS)
1287 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1288 else if (flags & SWS_SINC)
1289 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1290 else if (flags & SWS_LANCZOS)
1291 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1292 else if (flags & SWS_SPLINE)
1293 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1294 else
1295 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
a4388ebd
RP
1296
1297 av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1298 sws_format_name(srcFormat),
1299#ifdef DITHER1XBPP
716d413c
AK
1300 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1301 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1302 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
ef0ee7f6 1303 "dithered " : "",
a4388ebd
RP
1304#else
1305 "",
1306#endif
1307 sws_format_name(dstFormat));
1308
e0c6cce4 1309 if (INLINE_MMXEXT(cpu_flags))
652f5185 1310 av_log(c, AV_LOG_INFO, "using MMXEXT\n");
e0c6cce4 1311 else if (INLINE_AMD3DNOW(cpu_flags))
ef0ee7f6 1312 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
e0c6cce4 1313 else if (INLINE_MMX(cpu_flags))
ef0ee7f6 1314 av_log(c, AV_LOG_INFO, "using MMX\n");
f61bece6 1315 else if (PPC_ALTIVEC(cpu_flags))
ef0ee7f6
DB
1316 av_log(c, AV_LOG_INFO, "using AltiVec\n");
1317 else
1318 av_log(c, AV_LOG_INFO, "using C\n");
a4388ebd 1319
a4388ebd 1320 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
ef0ee7f6
DB
1321 av_log(c, AV_LOG_DEBUG,
1322 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
a4388ebd 1323 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
ef0ee7f6
DB
1324 av_log(c, AV_LOG_DEBUG,
1325 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1326 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1327 c->chrXInc, c->chrYInc);
a4388ebd
RP
1328 }
1329
1909f6b1 1330 c->swscale = ff_getSwsFunc(c);
635d4aed 1331 return 0;
ef0ee7f6 1332fail: // FIXME replace things by appropriate error codes
635d4aed
MN
1333 return -1;
1334}
a4388ebd 1335
716d413c
AK
1336SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1337 int dstW, int dstH, enum AVPixelFormat dstFormat,
ef0ee7f6
DB
1338 int flags, SwsFilter *srcFilter,
1339 SwsFilter *dstFilter, const double *param)
635d4aed
MN
1340{
1341 SwsContext *c;
1342
ef0ee7f6 1343 if (!(c = sws_alloc_context()))
635d4aed
MN
1344 return NULL;
1345
ef0ee7f6
DB
1346 c->flags = flags;
1347 c->srcW = srcW;
1348 c->srcH = srcH;
1349 c->dstW = dstW;
1350 c->dstH = dstH;
1351 c->srcRange = handle_jpeg(&srcFormat);
1352 c->dstRange = handle_jpeg(&dstFormat);
1353 c->srcFormat = srcFormat;
1354 c->dstFormat = dstFormat;
635d4aed
MN
1355
1356 if (param) {
1357 c->param[0] = param[0];
1358 c->param[1] = param[1];
635d4aed 1359 }
ef0ee7f6
DB
1360 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1361 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1362 c->dstRange, 0, 1 << 16, 1 << 16);
635d4aed 1363
ef0ee7f6 1364 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
635d4aed
MN
1365 sws_freeContext(c);
1366 return NULL;
1367 }
1368
1369 return c;
a4388ebd
RP
1370}
1371
1372SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1373 float lumaSharpen, float chromaSharpen,
1374 float chromaHShift, float chromaVShift,
1375 int verbose)
1376{
ef0ee7f6 1377 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
a4388ebd
RP
1378 if (!filter)
1379 return NULL;
1380
ef0ee7f6
DB
1381 if (lumaGBlur != 0.0) {
1382 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1383 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
a4388ebd 1384 } else {
ef0ee7f6
DB
1385 filter->lumH = sws_getIdentityVec();
1386 filter->lumV = sws_getIdentityVec();
a4388ebd
RP
1387 }
1388
ef0ee7f6
DB
1389 if (chromaGBlur != 0.0) {
1390 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1391 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
a4388ebd 1392 } else {
ef0ee7f6
DB
1393 filter->chrH = sws_getIdentityVec();
1394 filter->chrV = sws_getIdentityVec();
a4388ebd
RP
1395 }
1396
7ebb3022
FT
1397 if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
1398 goto fail;
e51f2212 1399
ef0ee7f6
DB
1400 if (chromaSharpen != 0.0) {
1401 SwsVector *id = sws_getIdentityVec();
7ebb3022
FT
1402 if (!id)
1403 goto fail;
a4388ebd
RP
1404 sws_scaleVec(filter->chrH, -chromaSharpen);
1405 sws_scaleVec(filter->chrV, -chromaSharpen);
1406 sws_addVec(filter->chrH, id);
1407 sws_addVec(filter->chrV, id);
1408 sws_freeVec(id);
1409 }
1410
ef0ee7f6
DB
1411 if (lumaSharpen != 0.0) {
1412 SwsVector *id = sws_getIdentityVec();
7ebb3022
FT
1413 if (!id)
1414 goto fail;
a4388ebd
RP
1415 sws_scaleVec(filter->lumH, -lumaSharpen);
1416 sws_scaleVec(filter->lumV, -lumaSharpen);
1417 sws_addVec(filter->lumH, id);
1418 sws_addVec(filter->lumV, id);
1419 sws_freeVec(id);
1420 }
1421
1422 if (chromaHShift != 0.0)
ef0ee7f6 1423 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
a4388ebd
RP
1424
1425 if (chromaVShift != 0.0)
ef0ee7f6 1426 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
a4388ebd
RP
1427
1428 sws_normalizeVec(filter->chrH, 1.0);
1429 sws_normalizeVec(filter->chrV, 1.0);
1430 sws_normalizeVec(filter->lumH, 1.0);
1431 sws_normalizeVec(filter->lumV, 1.0);
1432
ef0ee7f6
DB
1433 if (verbose)
1434 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1435 if (verbose)
1436 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
a4388ebd
RP
1437
1438 return filter;
7ebb3022
FT
1439
1440fail:
1441 sws_freeVec(filter->lumH);
1442 sws_freeVec(filter->lumV);
1443 sws_freeVec(filter->chrH);
1444 sws_freeVec(filter->chrV);
1445 av_freep(&filter);
1446 return NULL;
a4388ebd
RP
1447}
1448
1449SwsVector *sws_allocVec(int length)
1450{
1451 SwsVector *vec = av_malloc(sizeof(SwsVector));
1452 if (!vec)
1453 return NULL;
1454 vec->length = length;
1455 vec->coeff = av_malloc(sizeof(double) * length);
1456 if (!vec->coeff)
1457 av_freep(&vec);
1458 return vec;
1459}
1460
1461SwsVector *sws_getGaussianVec(double variance, double quality)
1462{
ef0ee7f6 1463 const int length = (int)(variance * quality + 0.5) | 1;
a4388ebd 1464 int i;
ef0ee7f6
DB
1465 double middle = (length - 1) * 0.5;
1466 SwsVector *vec = sws_allocVec(length);
a4388ebd
RP
1467
1468 if (!vec)
1469 return NULL;
1470
ef0ee7f6
DB
1471 for (i = 0; i < length; i++) {
1472 double dist = i - middle;
1473 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1474 sqrt(2 * variance * M_PI);
a4388ebd
RP
1475 }
1476
1477 sws_normalizeVec(vec, 1.0);
1478
1479 return vec;
1480}
1481
1482SwsVector *sws_getConstVec(double c, int length)
1483{
1484 int i;
ef0ee7f6 1485 SwsVector *vec = sws_allocVec(length);
a4388ebd
RP
1486
1487 if (!vec)
1488 return NULL;
1489
ef0ee7f6
DB
1490 for (i = 0; i < length; i++)
1491 vec->coeff[i] = c;
a4388ebd
RP
1492
1493 return vec;
1494}
1495
1496SwsVector *sws_getIdentityVec(void)
1497{
1498 return sws_getConstVec(1.0, 1);
1499}
1500
627686e6 1501static double sws_dcVec(SwsVector *a)
a4388ebd
RP
1502{
1503 int i;
ef0ee7f6 1504 double sum = 0;
a4388ebd 1505
ef0ee7f6
DB
1506 for (i = 0; i < a->length; i++)
1507 sum += a->coeff[i];
a4388ebd
RP
1508
1509 return sum;
1510}
1511
1512void sws_scaleVec(SwsVector *a, double scalar)
1513{
1514 int i;
1515
ef0ee7f6
DB
1516 for (i = 0; i < a->length; i++)
1517 a->coeff[i] *= scalar;
a4388ebd
RP
1518}
1519
1520void sws_normalizeVec(SwsVector *a, double height)
1521{
ef0ee7f6 1522 sws_scaleVec(a, height / sws_dcVec(a));
a4388ebd
RP
1523}
1524
1525static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1526{
ef0ee7f6 1527 int length = a->length + b->length - 1;
a4388ebd 1528 int i, j;
ef0ee7f6 1529 SwsVector *vec = sws_getConstVec(0.0, length);
a4388ebd
RP
1530
1531 if (!vec)
1532 return NULL;
1533
ef0ee7f6
DB
1534 for (i = 0; i < a->length; i++) {
1535 for (j = 0; j < b->length; j++) {
1536 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
a4388ebd
RP
1537 }
1538 }
1539
1540 return vec;
1541}
1542
1543static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1544{
ef0ee7f6 1545 int length = FFMAX(a->length, b->length);
a4388ebd 1546 int i;
ef0ee7f6 1547 SwsVector *vec = sws_getConstVec(0.0, length);
a4388ebd
RP
1548
1549 if (!vec)
1550 return NULL;
1551
ef0ee7f6
DB
1552 for (i = 0; i < a->length; i++)
1553 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1554 for (i = 0; i < b->length; i++)
1555 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
a4388ebd
RP
1556
1557 return vec;
1558}
1559
1560static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1561{
ef0ee7f6 1562 int length = FFMAX(a->length, b->length);
a4388ebd 1563 int i;
ef0ee7f6 1564 SwsVector *vec = sws_getConstVec(0.0, length);
a4388ebd
RP
1565
1566 if (!vec)
1567 return NULL;
1568
ef0ee7f6
DB
1569 for (i = 0; i < a->length; i++)
1570 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1571 for (i = 0; i < b->length; i++)
1572 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
a4388ebd
RP
1573
1574 return vec;
1575}
1576
1577/* shift left / or right if "shift" is negative */
1578static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1579{
ef0ee7f6 1580 int length = a->length + FFABS(shift) * 2;
a4388ebd 1581 int i;
ef0ee7f6 1582 SwsVector *vec = sws_getConstVec(0.0, length);
a4388ebd
RP
1583
1584 if (!vec)
1585 return NULL;
1586
ef0ee7f6
DB
1587 for (i = 0; i < a->length; i++) {
1588 vec->coeff[i + (length - 1) / 2 -
1589 (a->length - 1) / 2 - shift] = a->coeff[i];
a4388ebd
RP
1590 }
1591
1592 return vec;
1593}
1594
1595void sws_shiftVec(SwsVector *a, int shift)
1596{
ef0ee7f6 1597 SwsVector *shifted = sws_getShiftedVec(a, shift);
a4388ebd 1598 av_free(a->coeff);
ef0ee7f6
DB
1599 a->coeff = shifted->coeff;
1600 a->length = shifted->length;
a4388ebd
RP
1601 av_free(shifted);
1602}
1603
1604void sws_addVec(SwsVector *a, SwsVector *b)
1605{
ef0ee7f6 1606 SwsVector *sum = sws_sumVec(a, b);
a4388ebd 1607 av_free(a->coeff);
ef0ee7f6
DB
1608 a->coeff = sum->coeff;
1609 a->length = sum->length;
a4388ebd
RP
1610 av_free(sum);
1611}
1612
1613void sws_subVec(SwsVector *a, SwsVector *b)
1614{
ef0ee7f6 1615 SwsVector *diff = sws_diffVec(a, b);
a4388ebd 1616 av_free(a->coeff);
ef0ee7f6
DB
1617 a->coeff = diff->coeff;
1618 a->length = diff->length;
a4388ebd
RP
1619 av_free(diff);
1620}
1621
1622void sws_convVec(SwsVector *a, SwsVector *b)
1623{
ef0ee7f6 1624 SwsVector *conv = sws_getConvVec(a, b);
a4388ebd 1625 av_free(a->coeff);
ef0ee7f6
DB
1626 a->coeff = conv->coeff;
1627 a->length = conv->length;
a4388ebd
RP
1628 av_free(conv);
1629}
1630
1631SwsVector *sws_cloneVec(SwsVector *a)
1632{
1633 int i;
ef0ee7f6 1634 SwsVector *vec = sws_allocVec(a->length);
a4388ebd
RP
1635
1636 if (!vec)
1637 return NULL;
1638
ef0ee7f6
DB
1639 for (i = 0; i < a->length; i++)
1640 vec->coeff[i] = a->coeff[i];
a4388ebd
RP
1641
1642 return vec;
1643}
1644
1645void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1646{
1647 int i;
ef0ee7f6
DB
1648 double max = 0;
1649 double min = 0;
a4388ebd
RP
1650 double range;
1651
ef0ee7f6
DB
1652 for (i = 0; i < a->length; i++)
1653 if (a->coeff[i] > max)
1654 max = a->coeff[i];
a4388ebd 1655
ef0ee7f6
DB
1656 for (i = 0; i < a->length; i++)
1657 if (a->coeff[i] < min)
1658 min = a->coeff[i];
a4388ebd 1659
ef0ee7f6 1660 range = max - min;
a4388ebd 1661
ef0ee7f6
DB
1662 for (i = 0; i < a->length; i++) {
1663 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
a4388ebd 1664 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
ef0ee7f6
DB
1665 for (; x > 0; x--)
1666 av_log(log_ctx, log_level, " ");
a4388ebd
RP
1667 av_log(log_ctx, log_level, "|\n");
1668 }
1669}
1670
a4388ebd
RP
1671void sws_freeVec(SwsVector *a)
1672{
ef0ee7f6
DB
1673 if (!a)
1674 return;
a4388ebd 1675 av_freep(&a->coeff);
ef0ee7f6 1676 a->length = 0;
a4388ebd
RP
1677 av_free(a);
1678}
1679
1680void sws_freeFilter(SwsFilter *filter)
1681{
ef0ee7f6
DB
1682 if (!filter)
1683 return;
1684
1685 if (filter->lumH)
1686 sws_freeVec(filter->lumH);
1687 if (filter->lumV)
1688 sws_freeVec(filter->lumV);
1689 if (filter->chrH)
1690 sws_freeVec(filter->chrH);
1691 if (filter->chrV)
1692 sws_freeVec(filter->chrV);
a4388ebd
RP
1693 av_free(filter);
1694}
1695
1696void sws_freeContext(SwsContext *c)
1697{
1698 int i;
ef0ee7f6
DB
1699 if (!c)
1700 return;
a4388ebd
RP
1701
1702 if (c->lumPixBuf) {
ef0ee7f6 1703 for (i = 0; i < c->vLumBufSize; i++)
a4388ebd
RP
1704 av_freep(&c->lumPixBuf[i]);
1705 av_freep(&c->lumPixBuf);
1706 }
1707
b4a224c5 1708 if (c->chrUPixBuf) {
ef0ee7f6 1709 for (i = 0; i < c->vChrBufSize; i++)
b4a224c5
RB
1710 av_freep(&c->chrUPixBuf[i]);
1711 av_freep(&c->chrUPixBuf);
1712 av_freep(&c->chrVPixBuf);
a4388ebd
RP
1713 }
1714
1715 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
ef0ee7f6 1716 for (i = 0; i < c->vLumBufSize; i++)
a4388ebd
RP
1717 av_freep(&c->alpPixBuf[i]);
1718 av_freep(&c->alpPixBuf);
1719 }
1720
1721 av_freep(&c->vLumFilter);
1722 av_freep(&c->vChrFilter);
1723 av_freep(&c->hLumFilter);
1724 av_freep(&c->hChrFilter);
f684f3c5 1725#if HAVE_ALTIVEC
a4388ebd
RP
1726 av_freep(&c->vYCoeffsBank);
1727 av_freep(&c->vCCoeffsBank);
1728#endif
1729
1730 av_freep(&c->vLumFilterPos);
1731 av_freep(&c->vChrFilterPos);
1732 av_freep(&c->hLumFilterPos);
1733 av_freep(&c->hChrFilterPos);
1734
17337f54 1735#if HAVE_MMX_INLINE
95cd815c 1736#if USE_MMAP
a65bdceb
DB
1737 if (c->lumMmxextFilterCode)
1738 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
1739 if (c->chrMmxextFilterCode)
1740 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
a4388ebd 1741#elif HAVE_VIRTUALALLOC
a65bdceb
DB
1742 if (c->lumMmxextFilterCode)
1743 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
1744 if (c->chrMmxextFilterCode)
1745 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
a4388ebd 1746#else
a65bdceb
DB
1747 av_free(c->lumMmxextFilterCode);
1748 av_free(c->chrMmxextFilterCode);
a4388ebd 1749#endif
a65bdceb
DB
1750 c->lumMmxextFilterCode = NULL;
1751 c->chrMmxextFilterCode = NULL;
17337f54 1752#endif /* HAVE_MMX_INLINE */
a4388ebd
RP
1753
1754 av_freep(&c->yuvTable);
69645c02 1755 av_free(c->formatConvBuffer);
a4388ebd
RP
1756
1757 av_free(c);
1758}
1759
ef0ee7f6 1760struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
716d413c 1761 int srcH, enum AVPixelFormat srcFormat,
ef0ee7f6 1762 int dstW, int dstH,
716d413c 1763 enum AVPixelFormat dstFormat, int flags,
ef0ee7f6
DB
1764 SwsFilter *srcFilter,
1765 SwsFilter *dstFilter,
1766 const double *param)
a4388ebd 1767{
ef0ee7f6
DB
1768 static const double default_param[2] = { SWS_PARAM_DEFAULT,
1769 SWS_PARAM_DEFAULT };
a4388ebd
RP
1770
1771 if (!param)
1772 param = default_param;
1773
5a011d8b 1774 if (context &&
22b6a24c
SS
1775 (context->srcW != srcW ||
1776 context->srcH != srcH ||
1777 context->srcFormat != srcFormat ||
1778 context->dstW != dstW ||
1779 context->dstH != dstH ||
1780 context->dstFormat != dstFormat ||
1781 context->flags != flags ||
1782 context->param[0] != param[0] ||
b623d0cb
SS
1783 context->param[1] != param[1])) {
1784 sws_freeContext(context);
1785 context = NULL;
1786 }
5a011d8b 1787
a4388ebd 1788 if (!context) {
0810a584
SS
1789 if (!(context = sws_alloc_context()))
1790 return NULL;
1791 context->srcW = srcW;
1792 context->srcH = srcH;
9c158e49 1793 context->srcRange = handle_jpeg(&srcFormat);
0810a584 1794 context->srcFormat = srcFormat;
12eef0d1
RD
1795 context->dstW = dstW;
1796 context->dstH = dstH;
9c158e49 1797 context->dstRange = handle_jpeg(&dstFormat);
0810a584
SS
1798 context->dstFormat = dstFormat;
1799 context->flags = flags;
1800 context->param[0] = param[0];
1801 context->param[1] = param[1];
ef0ee7f6
DB
1802 sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1803 context->srcRange,
1804 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1805 context->dstRange, 0, 1 << 16, 1 << 16);
0810a584
SS
1806 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1807 sws_freeContext(context);
1808 return NULL;
1809 }
a4388ebd
RP
1810 }
1811 return context;
1812}