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