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