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