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