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