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