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