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