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