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