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