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