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