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