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