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