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