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