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