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