c4fb7451ebb2fbe52126bee5b3ae71d5a2b6d6d3
[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_YA16BE] = { 1, 0 },
159 [AV_PIX_FMT_YA16LE] = { 1, 0 },
160 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
161 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
162 [AV_PIX_FMT_BGRA64BE] = { 0, 0, 1 },
163 [AV_PIX_FMT_BGRA64LE] = { 0, 0, 1 },
164 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
165 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
166 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
167 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
168 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
169 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
170 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
171 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
172 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
173 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
174 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
175 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
176 [AV_PIX_FMT_GBRP] = { 1, 1 },
177 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
178 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
179 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
180 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
181 [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
182 [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
183 [AV_PIX_FMT_GBRAP] = { 1, 1 },
184 [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
185 [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
186 [AV_PIX_FMT_XYZ12BE] = { 0, 0, 1 },
187 [AV_PIX_FMT_XYZ12LE] = { 0, 0, 1 },
188 [AV_PIX_FMT_P010LE] = { 1, 0 },
189 [AV_PIX_FMT_P010BE] = { 1, 0 },
190 };
191
192 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
193 {
194 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
195 format_entries[pix_fmt].is_supported_in : 0;
196 }
197
198 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
199 {
200 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
201 format_entries[pix_fmt].is_supported_out : 0;
202 }
203
204 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
205 {
206 return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
207 format_entries[pix_fmt].is_supported_endianness : 0;
208 }
209
210 const char *sws_format_name(enum AVPixelFormat format)
211 {
212 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
213 if (desc)
214 return desc->name;
215 else
216 return "Unknown format";
217 }
218
219 static double getSplineCoeff(double a, double b, double c, double d,
220 double dist)
221 {
222 if (dist <= 1.0)
223 return ((d * dist + c) * dist + b) * dist + a;
224 else
225 return getSplineCoeff(0.0,
226 b + 2.0 * c + 3.0 * d,
227 c + 3.0 * d,
228 -b - 3.0 * c - 6.0 * d,
229 dist - 1.0);
230 }
231
232 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
233 int *outFilterSize, int xInc, int srcW,
234 int dstW, int filterAlign, int one,
235 int flags, int cpu_flags,
236 SwsVector *srcFilter, SwsVector *dstFilter,
237 double param[2], int is_horizontal)
238 {
239 int i;
240 int filterSize;
241 int filter2Size;
242 int minFilterSize;
243 int64_t *filter = NULL;
244 int64_t *filter2 = NULL;
245 const int64_t fone = 1LL << 54;
246 int ret = -1;
247
248 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
249
250 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
251 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
252
253 if (FFABS(xInc - 0x10000) < 10) { // unscaled
254 int i;
255 filterSize = 1;
256 FF_ALLOCZ_OR_GOTO(NULL, filter,
257 dstW * sizeof(*filter) * filterSize, fail);
258
259 for (i = 0; i < dstW; i++) {
260 filter[i * filterSize] = fone;
261 (*filterPos)[i] = i;
262 }
263 } else if (flags & SWS_POINT) { // lame looking point sampling mode
264 int i;
265 int xDstInSrc;
266 filterSize = 1;
267 FF_ALLOC_OR_GOTO(NULL, filter,
268 dstW * sizeof(*filter) * filterSize, fail);
269
270 xDstInSrc = xInc / 2 - 0x8000;
271 for (i = 0; i < dstW; i++) {
272 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
273
274 (*filterPos)[i] = xx;
275 filter[i] = fone;
276 xDstInSrc += xInc;
277 }
278 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
279 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
280 int i;
281 int xDstInSrc;
282 filterSize = 2;
283 FF_ALLOC_OR_GOTO(NULL, filter,
284 dstW * sizeof(*filter) * filterSize, fail);
285
286 xDstInSrc = xInc / 2 - 0x8000;
287 for (i = 0; i < dstW; i++) {
288 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
289 int j;
290
291 (*filterPos)[i] = xx;
292 // bilinear upscale / linear interpolate / area averaging
293 for (j = 0; j < filterSize; j++) {
294 int64_t coeff = fone - FFABS((xx << 16) - xDstInSrc) *
295 (fone >> 16);
296 if (coeff < 0)
297 coeff = 0;
298 filter[i * filterSize + j] = coeff;
299 xx++;
300 }
301 xDstInSrc += xInc;
302 }
303 } else {
304 int64_t xDstInSrc;
305 int sizeFactor;
306
307 if (flags & SWS_BICUBIC)
308 sizeFactor = 4;
309 else if (flags & SWS_X)
310 sizeFactor = 8;
311 else if (flags & SWS_AREA)
312 sizeFactor = 1; // downscale only, for upscale it is bilinear
313 else if (flags & SWS_GAUSS)
314 sizeFactor = 8; // infinite ;)
315 else if (flags & SWS_LANCZOS)
316 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
317 else if (flags & SWS_SINC)
318 sizeFactor = 20; // infinite ;)
319 else if (flags & SWS_SPLINE)
320 sizeFactor = 20; // infinite ;)
321 else if (flags & SWS_BILINEAR)
322 sizeFactor = 2;
323 else {
324 sizeFactor = 0; // GCC warning killer
325 assert(0);
326 }
327
328 if (xInc <= 1 << 16)
329 filterSize = 1 + sizeFactor; // upscale
330 else
331 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
332
333 filterSize = FFMIN(filterSize, srcW - 2);
334 filterSize = FFMAX(filterSize, 1);
335
336 FF_ALLOC_OR_GOTO(NULL, filter,
337 dstW * sizeof(*filter) * filterSize, fail);
338
339 xDstInSrc = xInc - 0x10000;
340 for (i = 0; i < dstW; i++) {
341 int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
342 int j;
343 (*filterPos)[i] = xx;
344 for (j = 0; j < filterSize; j++) {
345 int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
346 double floatd;
347 int64_t coeff;
348
349 if (xInc > 1 << 16)
350 d = d * dstW / srcW;
351 floatd = d * (1.0 / (1 << 30));
352
353 if (flags & SWS_BICUBIC) {
354 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
355 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
356
357 if (d >= 1LL << 31) {
358 coeff = 0.0;
359 } else {
360 int64_t dd = (d * d) >> 30;
361 int64_t ddd = (dd * d) >> 30;
362
363 if (d < 1LL << 30)
364 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
365 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
366 (6 * (1 << 24) - 2 * B) * (1 << 30);
367 else
368 coeff = (-B - 6 * C) * ddd +
369 (6 * B + 30 * C) * dd +
370 (-12 * B - 48 * C) * d +
371 (8 * B + 24 * C) * (1 << 30);
372 }
373 coeff *= fone >> (30 + 24);
374 }
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 (%%"FF_REG_d", %%"FF_REG_a"), %%mm3 \n\t"
648 "movd (%%"FF_REG_c", %%"FF_REG_S"), %%mm0 \n\t"
649 "movd 1(%%"FF_REG_c", %%"FF_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(%%"FF_REG_b", %%"FF_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, (%%"FF_REG_D", %%"FF_REG_a") \n\t"
663
664 "add $8, %%"FF_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 (%%"FF_REG_d", %%"FF_REG_a"), %%mm3 \n\t"
688 "movd (%%"FF_REG_c", %%"FF_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(%%"FF_REG_b", %%"FF_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, (%%"FF_REG_D", %%"FF_REG_a") \n\t"
701
702 "add $8, %%"FF_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 = &ff_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 ff_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 AV_CEIL_RSHIFT is so that we always round toward +inf.
1018 c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1019 c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1020 c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1021 c->chrDstH = AV_CEIL_RSHIFT(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 = desc_src->comp[0].depth;
1038 if (c->srcBpc < 8)
1039 c->srcBpc = 8;
1040 c->dstBpc = desc_dst->comp[0].depth;
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 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1320 int dstW, int dstH, enum AVPixelFormat dstFormat,
1321 int flags, SwsFilter *srcFilter,
1322 SwsFilter *dstFilter, const double *param)
1323 {
1324 SwsContext *c;
1325
1326 if (!(c = sws_alloc_context()))
1327 return NULL;
1328
1329 c->flags = flags;
1330 c->srcW = srcW;
1331 c->srcH = srcH;
1332 c->dstW = dstW;
1333 c->dstH = dstH;
1334 c->srcRange = handle_jpeg(&srcFormat);
1335 c->dstRange = handle_jpeg(&dstFormat);
1336 c->srcFormat = srcFormat;
1337 c->dstFormat = dstFormat;
1338
1339 if (param) {
1340 c->param[0] = param[0];
1341 c->param[1] = param[1];
1342 }
1343 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1344 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1345 c->dstRange, 0, 1 << 16, 1 << 16);
1346
1347 if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1348 sws_freeContext(c);
1349 return NULL;
1350 }
1351
1352 return c;
1353 }
1354
1355 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1356 float lumaSharpen, float chromaSharpen,
1357 float chromaHShift, float chromaVShift,
1358 int verbose)
1359 {
1360 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1361 if (!filter)
1362 return NULL;
1363
1364 if (lumaGBlur != 0.0) {
1365 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1366 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1367 } else {
1368 filter->lumH = sws_getIdentityVec();
1369 filter->lumV = sws_getIdentityVec();
1370 }
1371
1372 if (chromaGBlur != 0.0) {
1373 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1374 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1375 } else {
1376 filter->chrH = sws_getIdentityVec();
1377 filter->chrV = sws_getIdentityVec();
1378 }
1379
1380 if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
1381 goto fail;
1382
1383 if (chromaSharpen != 0.0) {
1384 SwsVector *id = sws_getIdentityVec();
1385 if (!id)
1386 goto fail;
1387 sws_scaleVec(filter->chrH, -chromaSharpen);
1388 sws_scaleVec(filter->chrV, -chromaSharpen);
1389 sws_addVec(filter->chrH, id);
1390 sws_addVec(filter->chrV, id);
1391 sws_freeVec(id);
1392 }
1393
1394 if (lumaSharpen != 0.0) {
1395 SwsVector *id = sws_getIdentityVec();
1396 if (!id)
1397 goto fail;
1398 sws_scaleVec(filter->lumH, -lumaSharpen);
1399 sws_scaleVec(filter->lumV, -lumaSharpen);
1400 sws_addVec(filter->lumH, id);
1401 sws_addVec(filter->lumV, id);
1402 sws_freeVec(id);
1403 }
1404
1405 if (chromaHShift != 0.0)
1406 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1407
1408 if (chromaVShift != 0.0)
1409 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1410
1411 sws_normalizeVec(filter->chrH, 1.0);
1412 sws_normalizeVec(filter->chrV, 1.0);
1413 sws_normalizeVec(filter->lumH, 1.0);
1414 sws_normalizeVec(filter->lumV, 1.0);
1415
1416 if (verbose)
1417 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1418 if (verbose)
1419 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1420
1421 return filter;
1422
1423 fail:
1424 sws_freeVec(filter->lumH);
1425 sws_freeVec(filter->lumV);
1426 sws_freeVec(filter->chrH);
1427 sws_freeVec(filter->chrV);
1428 av_freep(&filter);
1429 return NULL;
1430 }
1431
1432 SwsVector *sws_allocVec(int length)
1433 {
1434 SwsVector *vec = av_malloc(sizeof(SwsVector));
1435 if (!vec)
1436 return NULL;
1437 vec->length = length;
1438 vec->coeff = av_malloc(sizeof(double) * length);
1439 if (!vec->coeff)
1440 av_freep(&vec);
1441 return vec;
1442 }
1443
1444 SwsVector *sws_getGaussianVec(double variance, double quality)
1445 {
1446 const int length = (int)(variance * quality + 0.5) | 1;
1447 int i;
1448 double middle = (length - 1) * 0.5;
1449 SwsVector *vec = sws_allocVec(length);
1450
1451 if (!vec)
1452 return NULL;
1453
1454 for (i = 0; i < length; i++) {
1455 double dist = i - middle;
1456 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1457 sqrt(2 * variance * M_PI);
1458 }
1459
1460 sws_normalizeVec(vec, 1.0);
1461
1462 return vec;
1463 }
1464
1465 SwsVector *sws_getConstVec(double c, int length)
1466 {
1467 int i;
1468 SwsVector *vec = sws_allocVec(length);
1469
1470 if (!vec)
1471 return NULL;
1472
1473 for (i = 0; i < length; i++)
1474 vec->coeff[i] = c;
1475
1476 return vec;
1477 }
1478
1479 SwsVector *sws_getIdentityVec(void)
1480 {
1481 return sws_getConstVec(1.0, 1);
1482 }
1483
1484 static double sws_dcVec(SwsVector *a)
1485 {
1486 int i;
1487 double sum = 0;
1488
1489 for (i = 0; i < a->length; i++)
1490 sum += a->coeff[i];
1491
1492 return sum;
1493 }
1494
1495 void sws_scaleVec(SwsVector *a, double scalar)
1496 {
1497 int i;
1498
1499 for (i = 0; i < a->length; i++)
1500 a->coeff[i] *= scalar;
1501 }
1502
1503 void sws_normalizeVec(SwsVector *a, double height)
1504 {
1505 sws_scaleVec(a, height / sws_dcVec(a));
1506 }
1507
1508 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1509 {
1510 int length = a->length + b->length - 1;
1511 int i, j;
1512 SwsVector *vec = sws_getConstVec(0.0, length);
1513
1514 if (!vec)
1515 return NULL;
1516
1517 for (i = 0; i < a->length; i++) {
1518 for (j = 0; j < b->length; j++) {
1519 vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1520 }
1521 }
1522
1523 return vec;
1524 }
1525
1526 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1527 {
1528 int length = FFMAX(a->length, b->length);
1529 int i;
1530 SwsVector *vec = sws_getConstVec(0.0, length);
1531
1532 if (!vec)
1533 return NULL;
1534
1535 for (i = 0; i < a->length; i++)
1536 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1537 for (i = 0; i < b->length; i++)
1538 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1539
1540 return vec;
1541 }
1542
1543 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1544 {
1545 int length = FFMAX(a->length, b->length);
1546 int i;
1547 SwsVector *vec = sws_getConstVec(0.0, length);
1548
1549 if (!vec)
1550 return NULL;
1551
1552 for (i = 0; i < a->length; i++)
1553 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1554 for (i = 0; i < b->length; i++)
1555 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1556
1557 return vec;
1558 }
1559
1560 /* shift left / or right if "shift" is negative */
1561 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1562 {
1563 int length = a->length + FFABS(shift) * 2;
1564 int i;
1565 SwsVector *vec = sws_getConstVec(0.0, length);
1566
1567 if (!vec)
1568 return NULL;
1569
1570 for (i = 0; i < a->length; i++) {
1571 vec->coeff[i + (length - 1) / 2 -
1572 (a->length - 1) / 2 - shift] = a->coeff[i];
1573 }
1574
1575 return vec;
1576 }
1577
1578 void sws_shiftVec(SwsVector *a, int shift)
1579 {
1580 SwsVector *shifted = sws_getShiftedVec(a, shift);
1581 av_free(a->coeff);
1582 a->coeff = shifted->coeff;
1583 a->length = shifted->length;
1584 av_free(shifted);
1585 }
1586
1587 void sws_addVec(SwsVector *a, SwsVector *b)
1588 {
1589 SwsVector *sum = sws_sumVec(a, b);
1590 av_free(a->coeff);
1591 a->coeff = sum->coeff;
1592 a->length = sum->length;
1593 av_free(sum);
1594 }
1595
1596 void sws_subVec(SwsVector *a, SwsVector *b)
1597 {
1598 SwsVector *diff = sws_diffVec(a, b);
1599 av_free(a->coeff);
1600 a->coeff = diff->coeff;
1601 a->length = diff->length;
1602 av_free(diff);
1603 }
1604
1605 void sws_convVec(SwsVector *a, SwsVector *b)
1606 {
1607 SwsVector *conv = sws_getConvVec(a, b);
1608 av_free(a->coeff);
1609 a->coeff = conv->coeff;
1610 a->length = conv->length;
1611 av_free(conv);
1612 }
1613
1614 SwsVector *sws_cloneVec(SwsVector *a)
1615 {
1616 int i;
1617 SwsVector *vec = sws_allocVec(a->length);
1618
1619 if (!vec)
1620 return NULL;
1621
1622 for (i = 0; i < a->length; i++)
1623 vec->coeff[i] = a->coeff[i];
1624
1625 return vec;
1626 }
1627
1628 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1629 {
1630 int i;
1631 double max = 0;
1632 double min = 0;
1633 double range;
1634
1635 for (i = 0; i < a->length; i++)
1636 if (a->coeff[i] > max)
1637 max = a->coeff[i];
1638
1639 for (i = 0; i < a->length; i++)
1640 if (a->coeff[i] < min)
1641 min = a->coeff[i];
1642
1643 range = max - min;
1644
1645 for (i = 0; i < a->length; i++) {
1646 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1647 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1648 for (; x > 0; x--)
1649 av_log(log_ctx, log_level, " ");
1650 av_log(log_ctx, log_level, "|\n");
1651 }
1652 }
1653
1654 void sws_freeVec(SwsVector *a)
1655 {
1656 if (!a)
1657 return;
1658 av_freep(&a->coeff);
1659 a->length = 0;
1660 av_free(a);
1661 }
1662
1663 void sws_freeFilter(SwsFilter *filter)
1664 {
1665 if (!filter)
1666 return;
1667
1668 if (filter->lumH)
1669 sws_freeVec(filter->lumH);
1670 if (filter->lumV)
1671 sws_freeVec(filter->lumV);
1672 if (filter->chrH)
1673 sws_freeVec(filter->chrH);
1674 if (filter->chrV)
1675 sws_freeVec(filter->chrV);
1676 av_free(filter);
1677 }
1678
1679 void sws_freeContext(SwsContext *c)
1680 {
1681 int i;
1682 if (!c)
1683 return;
1684
1685 if (c->lumPixBuf) {
1686 for (i = 0; i < c->vLumBufSize; i++)
1687 av_freep(&c->lumPixBuf[i]);
1688 av_freep(&c->lumPixBuf);
1689 }
1690
1691 if (c->chrUPixBuf) {
1692 for (i = 0; i < c->vChrBufSize; i++)
1693 av_freep(&c->chrUPixBuf[i]);
1694 av_freep(&c->chrUPixBuf);
1695 av_freep(&c->chrVPixBuf);
1696 }
1697
1698 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1699 for (i = 0; i < c->vLumBufSize; i++)
1700 av_freep(&c->alpPixBuf[i]);
1701 av_freep(&c->alpPixBuf);
1702 }
1703
1704 av_freep(&c->vLumFilter);
1705 av_freep(&c->vChrFilter);
1706 av_freep(&c->hLumFilter);
1707 av_freep(&c->hChrFilter);
1708 #if HAVE_ALTIVEC
1709 av_freep(&c->vYCoeffsBank);
1710 av_freep(&c->vCCoeffsBank);
1711 #endif
1712
1713 av_freep(&c->vLumFilterPos);
1714 av_freep(&c->vChrFilterPos);
1715 av_freep(&c->hLumFilterPos);
1716 av_freep(&c->hChrFilterPos);
1717
1718 #if HAVE_MMX_INLINE
1719 #if USE_MMAP
1720 if (c->lumMmxextFilterCode)
1721 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
1722 if (c->chrMmxextFilterCode)
1723 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
1724 #elif HAVE_VIRTUALALLOC
1725 if (c->lumMmxextFilterCode)
1726 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
1727 if (c->chrMmxextFilterCode)
1728 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
1729 #else
1730 av_free(c->lumMmxextFilterCode);
1731 av_free(c->chrMmxextFilterCode);
1732 #endif
1733 c->lumMmxextFilterCode = NULL;
1734 c->chrMmxextFilterCode = NULL;
1735 #endif /* HAVE_MMX_INLINE */
1736
1737 av_freep(&c->yuvTable);
1738 av_free(c->formatConvBuffer);
1739
1740 av_free(c);
1741 }
1742
1743 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
1744 int srcH, enum AVPixelFormat srcFormat,
1745 int dstW, int dstH,
1746 enum AVPixelFormat dstFormat, int flags,
1747 SwsFilter *srcFilter,
1748 SwsFilter *dstFilter,
1749 const double *param)
1750 {
1751 static const double default_param[2] = { SWS_PARAM_DEFAULT,
1752 SWS_PARAM_DEFAULT };
1753
1754 if (!param)
1755 param = default_param;
1756
1757 if (context &&
1758 (context->srcW != srcW ||
1759 context->srcH != srcH ||
1760 context->srcFormat != srcFormat ||
1761 context->dstW != dstW ||
1762 context->dstH != dstH ||
1763 context->dstFormat != dstFormat ||
1764 context->flags != flags ||
1765 context->param[0] != param[0] ||
1766 context->param[1] != param[1])) {
1767 sws_freeContext(context);
1768 context = NULL;
1769 }
1770
1771 if (!context) {
1772 if (!(context = sws_alloc_context()))
1773 return NULL;
1774 context->srcW = srcW;
1775 context->srcH = srcH;
1776 context->srcRange = handle_jpeg(&srcFormat);
1777 context->srcFormat = srcFormat;
1778 context->dstW = dstW;
1779 context->dstH = dstH;
1780 context->dstRange = handle_jpeg(&dstFormat);
1781 context->dstFormat = dstFormat;
1782 context->flags = flags;
1783 context->param[0] = param[0];
1784 context->param[1] = param[1];
1785 sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1786 context->srcRange,
1787 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1788 context->dstRange, 0, 1 << 16, 1 << 16);
1789 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1790 sws_freeContext(context);
1791 return NULL;
1792 }
1793 }
1794 return context;
1795 }