]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
4a69ab8ea15b83161dd069b75b2c92d09bf0ddfb
[ffmpeg] / libswscale / swscale.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * FFmpeg 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * the C code (not assembly, mmx, ...) of this file can be used
21  * under the LGPL license too
22  */
23
24 /*
25   supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
26   supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27   {BGR,RGB}{1,4,8,15,16} support dithering
28
29   unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30   YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
31   x -> x
32   YUV9 -> YV12
33   YUV9/YV12 -> Y800
34   Y800 -> YUV9/YV12
35   BGR24 -> BGR32 & RGB24 -> RGB32
36   BGR32 -> BGR24 & RGB32 -> RGB24
37   BGR15 -> BGR16
38 */
39
40 /*
41 tested special converters (most are tested actually, but I did not write it down ...)
42  YV12 -> BGR16
43  YV12 -> YV12
44  BGR15 -> BGR16
45  BGR16 -> BGR16
46  YVU9 -> YV12
47
48 untested special converters
49   YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
50   YV12/I420 -> YV12/I420
51   YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
52   BGR24 -> BGR32 & RGB24 -> RGB32
53   BGR32 -> BGR24 & RGB32 -> RGB24
54   BGR24 -> YV12
55 */
56
57 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
58 #include <inttypes.h>
59 #include <string.h>
60 #include <math.h>
61 #include <stdio.h>
62 #include "config.h"
63 #include <assert.h>
64 #if HAVE_SYS_MMAN_H
65 #include <sys/mman.h>
66 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67 #define MAP_ANONYMOUS MAP_ANON
68 #endif
69 #endif
70 #if HAVE_VIRTUALALLOC
71 #define WIN32_LEAN_AND_MEAN
72 #include <windows.h>
73 #endif
74 #include "swscale.h"
75 #include "swscale_internal.h"
76 #include "rgb2rgb.h"
77 #include "libavutil/x86_cpu.h"
78 #include "libavutil/bswap.h"
79
80 unsigned swscale_version(void)
81 {
82     return LIBSWSCALE_VERSION_INT;
83 }
84
85 #undef MOVNTQ
86 #undef PAVGB
87
88 //#undef HAVE_MMX2
89 //#define HAVE_AMD3DNOW
90 //#undef HAVE_MMX
91 //#undef ARCH_X86
92 #define DITHER1XBPP
93
94 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
95
96 #define RET 0xC3 //near return opcode for x86
97
98 #ifdef M_PI
99 #define PI M_PI
100 #else
101 #define PI 3.14159265358979323846
102 #endif
103
104 #define isSupportedIn(x)    (       \
105            (x)==PIX_FMT_YUV420P     \
106         || (x)==PIX_FMT_YUVA420P    \
107         || (x)==PIX_FMT_YUYV422     \
108         || (x)==PIX_FMT_UYVY422     \
109         || (x)==PIX_FMT_RGB48BE     \
110         || (x)==PIX_FMT_RGB48LE     \
111         || (x)==PIX_FMT_RGB32       \
112         || (x)==PIX_FMT_RGB32_1     \
113         || (x)==PIX_FMT_BGR24       \
114         || (x)==PIX_FMT_BGR565      \
115         || (x)==PIX_FMT_BGR555      \
116         || (x)==PIX_FMT_BGR32       \
117         || (x)==PIX_FMT_BGR32_1     \
118         || (x)==PIX_FMT_RGB24       \
119         || (x)==PIX_FMT_RGB565      \
120         || (x)==PIX_FMT_RGB555      \
121         || (x)==PIX_FMT_GRAY8       \
122         || (x)==PIX_FMT_YUV410P     \
123         || (x)==PIX_FMT_YUV440P     \
124         || (x)==PIX_FMT_GRAY16BE    \
125         || (x)==PIX_FMT_GRAY16LE    \
126         || (x)==PIX_FMT_YUV444P     \
127         || (x)==PIX_FMT_YUV422P     \
128         || (x)==PIX_FMT_YUV411P     \
129         || (x)==PIX_FMT_PAL8        \
130         || (x)==PIX_FMT_BGR8        \
131         || (x)==PIX_FMT_RGB8        \
132         || (x)==PIX_FMT_BGR4_BYTE   \
133         || (x)==PIX_FMT_RGB4_BYTE   \
134         || (x)==PIX_FMT_YUV440P     \
135         || (x)==PIX_FMT_MONOWHITE   \
136         || (x)==PIX_FMT_MONOBLACK   \
137         || (x)==PIX_FMT_YUV420PLE   \
138         || (x)==PIX_FMT_YUV422PLE   \
139         || (x)==PIX_FMT_YUV444PLE   \
140         || (x)==PIX_FMT_YUV420PBE   \
141         || (x)==PIX_FMT_YUV422PBE   \
142         || (x)==PIX_FMT_YUV444PBE   \
143     )
144 #define isSupportedOut(x)   (       \
145            (x)==PIX_FMT_YUV420P     \
146         || (x)==PIX_FMT_YUVA420P    \
147         || (x)==PIX_FMT_YUYV422     \
148         || (x)==PIX_FMT_UYVY422     \
149         || (x)==PIX_FMT_YUV444P     \
150         || (x)==PIX_FMT_YUV422P     \
151         || (x)==PIX_FMT_YUV411P     \
152         || isRGB(x)                 \
153         || isBGR(x)                 \
154         || (x)==PIX_FMT_NV12        \
155         || (x)==PIX_FMT_NV21        \
156         || (x)==PIX_FMT_GRAY16BE    \
157         || (x)==PIX_FMT_GRAY16LE    \
158         || (x)==PIX_FMT_GRAY8       \
159         || (x)==PIX_FMT_YUV410P     \
160         || (x)==PIX_FMT_YUV440P     \
161         || (x)==PIX_FMT_YUV420PLE   \
162         || (x)==PIX_FMT_YUV422PLE   \
163         || (x)==PIX_FMT_YUV444PLE   \
164         || (x)==PIX_FMT_YUV420PBE   \
165         || (x)==PIX_FMT_YUV422PBE   \
166         || (x)==PIX_FMT_YUV444PBE   \
167     )
168 #define isPacked(x)         (       \
169            (x)==PIX_FMT_PAL8        \
170         || (x)==PIX_FMT_YUYV422     \
171         || (x)==PIX_FMT_UYVY422     \
172         || isRGB(x)                 \
173         || isBGR(x)                 \
174     )
175 #define usePal(x)           (       \
176            (x)==PIX_FMT_PAL8        \
177         || (x)==PIX_FMT_BGR4_BYTE   \
178         || (x)==PIX_FMT_RGB4_BYTE   \
179         || (x)==PIX_FMT_BGR8        \
180         || (x)==PIX_FMT_RGB8        \
181     )
182
183 #define RGB2YUV_SHIFT 15
184 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
185 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
186 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
187 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
188 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
189 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
190 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
191 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
192 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
193
194 extern const int32_t ff_yuv2rgb_coeffs[8][4];
195
196 static const double rgb2yuv_table[8][9]={
197     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
198     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
199     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
200     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
201     {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
202     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
203     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
204     {0.701 , 0.087 , 0.212 , -0.384, 0.5  -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
205 };
206
207 /*
208 NOTES
209 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
210
211 TODO
212 more intelligent misalignment avoidance for the horizontal scaler
213 write special vertical cubic upscale version
214 optimize C code (YV12 / minmax)
215 add support for packed pixel YUV input & output
216 add support for Y8 output
217 optimize BGR24 & BGR32
218 add BGR4 output support
219 write special BGR->BGR scaler
220 */
221
222 #if ARCH_X86 && CONFIG_GPL
223 DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
224 DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
225 DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
226 DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
227 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
228 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
229 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
230 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
231
232 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
233         0x0103010301030103LL,
234         0x0200020002000200LL,};
235
236 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
237         0x0602060206020602LL,
238         0x0004000400040004LL,};
239
240 DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
241 DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
242 DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
243 DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
244 DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
245 DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
246
247 DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
248 DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
250
251 #ifdef FAST_BGR2YV12
252 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
253 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
254 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
255 #else
256 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
257 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
258 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
259 #endif /* FAST_BGR2YV12 */
260 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
261 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
262 DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
263
264 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
265 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
266 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
267 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
268 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
269
270 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
271     {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
272     {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
273 };
274
275 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
276
277 #endif /* ARCH_X86 && CONFIG_GPL */
278
279 // clipping helper table for C implementations:
280 static unsigned char clip_table[768];
281
282 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
283
284 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
285 {  1,   3,   1,   3,   1,   3,   1,   3, },
286 {  2,   0,   2,   0,   2,   0,   2,   0, },
287 };
288
289 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
290 {  6,   2,   6,   2,   6,   2,   6,   2, },
291 {  0,   4,   0,   4,   0,   4,   0,   4, },
292 };
293
294 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
295 { 17,   9,  23,  15,  16,   8,  22,  14, },
296 {  5,  29,   3,  27,   4,  28,   2,  26, },
297 { 21,  13,  19,  11,  20,  12,  18,  10, },
298 {  0,  24,   6,  30,   1,  25,   7,  31, },
299 { 16,   8,  22,  14,  17,   9,  23,  15, },
300 {  4,  28,   2,  26,   5,  29,   3,  27, },
301 { 20,  12,  18,  10,  21,  13,  19,  11, },
302 {  1,  25,   7,  31,   0,  24,   6,  30, },
303 };
304
305 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
306 {  0,  55,  14,  68,   3,  58,  17,  72, },
307 { 37,  18,  50,  32,  40,  22,  54,  35, },
308 {  9,  64,   5,  59,  13,  67,   8,  63, },
309 { 46,  27,  41,  23,  49,  31,  44,  26, },
310 {  2,  57,  16,  71,   1,  56,  15,  70, },
311 { 39,  21,  52,  34,  38,  19,  51,  33, },
312 { 11,  66,   7,  62,  10,  65,   6,  60, },
313 { 48,  30,  43,  25,  47,  29,  42,  24, },
314 };
315
316 #if 1
317 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
318 {117,  62, 158, 103, 113,  58, 155, 100, },
319 { 34, 199,  21, 186,  31, 196,  17, 182, },
320 {144,  89, 131,  76, 141,  86, 127,  72, },
321 {  0, 165,  41, 206,  10, 175,  52, 217, },
322 {110,  55, 151,  96, 120,  65, 162, 107, },
323 { 28, 193,  14, 179,  38, 203,  24, 189, },
324 {138,  83, 124,  69, 148,  93, 134,  79, },
325 {  7, 172,  48, 213,   3, 168,  45, 210, },
326 };
327 #elif 1
328 // tries to correct a gamma of 1.5
329 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
330 {  0, 143,  18, 200,   2, 156,  25, 215, },
331 { 78,  28, 125,  64,  89,  36, 138,  74, },
332 { 10, 180,   3, 161,  16, 195,   8, 175, },
333 {109,  51,  93,  38, 121,  60, 105,  47, },
334 {  1, 152,  23, 210,   0, 147,  20, 205, },
335 { 85,  33, 134,  71,  81,  30, 130,  67, },
336 { 14, 190,   6, 171,  12, 185,   5, 166, },
337 {117,  57, 101,  44, 113,  54,  97,  41, },
338 };
339 #elif 1
340 // tries to correct a gamma of 2.0
341 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
342 {  0, 124,   8, 193,   0, 140,  12, 213, },
343 { 55,  14, 104,  42,  66,  19, 119,  52, },
344 {  3, 168,   1, 145,   6, 187,   3, 162, },
345 { 86,  31,  70,  21,  99,  39,  82,  28, },
346 {  0, 134,  11, 206,   0, 129,   9, 200, },
347 { 62,  17, 114,  48,  58,  16, 109,  45, },
348 {  5, 181,   2, 157,   4, 175,   1, 151, },
349 { 95,  36,  78,  26,  90,  34,  74,  24, },
350 };
351 #else
352 // tries to correct a gamma of 2.5
353 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
354 {  0, 107,   3, 187,   0, 125,   6, 212, },
355 { 39,   7,  86,  28,  49,  11, 102,  36, },
356 {  1, 158,   0, 131,   3, 180,   1, 151, },
357 { 68,  19,  52,  12,  81,  25,  64,  17, },
358 {  0, 119,   5, 203,   0, 113,   4, 195, },
359 { 45,   9,  96,  33,  42,   8,  91,  30, },
360 {  2, 172,   1, 144,   2, 165,   0, 137, },
361 { 77,  23,  60,  15,  72,  21,  56,  14, },
362 };
363 #endif
364
365 const char *sws_format_name(enum PixelFormat format)
366 {
367     switch (format) {
368         case PIX_FMT_YUV420P:
369             return "yuv420p";
370         case PIX_FMT_YUVA420P:
371             return "yuva420p";
372         case PIX_FMT_YUYV422:
373             return "yuyv422";
374         case PIX_FMT_RGB24:
375             return "rgb24";
376         case PIX_FMT_BGR24:
377             return "bgr24";
378         case PIX_FMT_YUV422P:
379             return "yuv422p";
380         case PIX_FMT_YUV444P:
381             return "yuv444p";
382         case PIX_FMT_RGB32:
383             return "rgb32";
384         case PIX_FMT_YUV410P:
385             return "yuv410p";
386         case PIX_FMT_YUV411P:
387             return "yuv411p";
388         case PIX_FMT_RGB565:
389             return "rgb565";
390         case PIX_FMT_RGB555:
391             return "rgb555";
392         case PIX_FMT_GRAY16BE:
393             return "gray16be";
394         case PIX_FMT_GRAY16LE:
395             return "gray16le";
396         case PIX_FMT_GRAY8:
397             return "gray8";
398         case PIX_FMT_MONOWHITE:
399             return "mono white";
400         case PIX_FMT_MONOBLACK:
401             return "mono black";
402         case PIX_FMT_PAL8:
403             return "Palette";
404         case PIX_FMT_YUVJ420P:
405             return "yuvj420p";
406         case PIX_FMT_YUVJ422P:
407             return "yuvj422p";
408         case PIX_FMT_YUVJ444P:
409             return "yuvj444p";
410         case PIX_FMT_XVMC_MPEG2_MC:
411             return "xvmc_mpeg2_mc";
412         case PIX_FMT_XVMC_MPEG2_IDCT:
413             return "xvmc_mpeg2_idct";
414         case PIX_FMT_UYVY422:
415             return "uyvy422";
416         case PIX_FMT_UYYVYY411:
417             return "uyyvyy411";
418         case PIX_FMT_RGB32_1:
419             return "rgb32x";
420         case PIX_FMT_BGR32_1:
421             return "bgr32x";
422         case PIX_FMT_BGR32:
423             return "bgr32";
424         case PIX_FMT_BGR565:
425             return "bgr565";
426         case PIX_FMT_BGR555:
427             return "bgr555";
428         case PIX_FMT_BGR8:
429             return "bgr8";
430         case PIX_FMT_BGR4:
431             return "bgr4";
432         case PIX_FMT_BGR4_BYTE:
433             return "bgr4 byte";
434         case PIX_FMT_RGB8:
435             return "rgb8";
436         case PIX_FMT_RGB4:
437             return "rgb4";
438         case PIX_FMT_RGB4_BYTE:
439             return "rgb4 byte";
440         case PIX_FMT_RGB48BE:
441             return "rgb48be";
442         case PIX_FMT_RGB48LE:
443             return "rgb48le";
444         case PIX_FMT_NV12:
445             return "nv12";
446         case PIX_FMT_NV21:
447             return "nv21";
448         case PIX_FMT_YUV440P:
449             return "yuv440p";
450         case PIX_FMT_VDPAU_H264:
451             return "vdpau_h264";
452         case PIX_FMT_VDPAU_MPEG1:
453             return "vdpau_mpeg1";
454         case PIX_FMT_VDPAU_MPEG2:
455             return "vdpau_mpeg2";
456         case PIX_FMT_VDPAU_WMV3:
457             return "vdpau_wmv3";
458         case PIX_FMT_VDPAU_VC1:
459             return "vdpau_vc1";
460         case PIX_FMT_YUV420PLE:
461             return "yuv420ple";
462         case PIX_FMT_YUV422PLE:
463             return "yuv422ple";
464         case PIX_FMT_YUV444PLE:
465             return "yuv444ple";
466         case PIX_FMT_YUV420PBE:
467             return "yuv420pbe";
468         case PIX_FMT_YUV422PBE:
469             return "yuv422pbe";
470         case PIX_FMT_YUV444PBE:
471             return "yuv444pbe";
472         default:
473             return "Unknown format";
474     }
475 }
476
477 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
478                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
479                                const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
480 {
481     //FIXME Optimize (just quickly written not optimized..)
482     int i;
483     for (i=0; i<dstW; i++)
484     {
485         int val=1<<18;
486         int j;
487         for (j=0; j<lumFilterSize; j++)
488             val += lumSrc[j][i] * lumFilter[j];
489
490         dest[i]= av_clip_uint8(val>>19);
491     }
492
493     if (uDest)
494         for (i=0; i<chrDstW; i++)
495         {
496             int u=1<<18;
497             int v=1<<18;
498             int j;
499             for (j=0; j<chrFilterSize; j++)
500             {
501                 u += chrSrc[j][i] * chrFilter[j];
502                 v += chrSrc[j][i + VOFW] * chrFilter[j];
503             }
504
505             uDest[i]= av_clip_uint8(u>>19);
506             vDest[i]= av_clip_uint8(v>>19);
507         }
508
509     if (CONFIG_SWSCALE_ALPHA && aDest)
510         for (i=0; i<dstW; i++){
511             int val=1<<18;
512             int j;
513             for (j=0; j<lumFilterSize; j++)
514                 val += alpSrc[j][i] * lumFilter[j];
515
516             aDest[i]= av_clip_uint8(val>>19);
517         }
518
519 }
520
521 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
522                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
523                                 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
524 {
525     //FIXME Optimize (just quickly written not optimized..)
526     int i;
527     for (i=0; i<dstW; i++)
528     {
529         int val=1<<18;
530         int j;
531         for (j=0; j<lumFilterSize; j++)
532             val += lumSrc[j][i] * lumFilter[j];
533
534         dest[i]= av_clip_uint8(val>>19);
535     }
536
537     if (!uDest)
538         return;
539
540     if (dstFormat == PIX_FMT_NV12)
541         for (i=0; i<chrDstW; i++)
542         {
543             int u=1<<18;
544             int v=1<<18;
545             int j;
546             for (j=0; j<chrFilterSize; j++)
547             {
548                 u += chrSrc[j][i] * chrFilter[j];
549                 v += chrSrc[j][i + VOFW] * chrFilter[j];
550             }
551
552             uDest[2*i]= av_clip_uint8(u>>19);
553             uDest[2*i+1]= av_clip_uint8(v>>19);
554         }
555     else
556         for (i=0; i<chrDstW; i++)
557         {
558             int u=1<<18;
559             int v=1<<18;
560             int j;
561             for (j=0; j<chrFilterSize; j++)
562             {
563                 u += chrSrc[j][i] * chrFilter[j];
564                 v += chrSrc[j][i + VOFW] * chrFilter[j];
565             }
566
567             uDest[2*i]= av_clip_uint8(v>>19);
568             uDest[2*i+1]= av_clip_uint8(u>>19);
569         }
570 }
571
572 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
573     for (i=0; i<(dstW>>1); i++){\
574         int j;\
575         int Y1 = 1<<18;\
576         int Y2 = 1<<18;\
577         int U  = 1<<18;\
578         int V  = 1<<18;\
579         int av_unused A1, A2;\
580         type av_unused *r, *b, *g;\
581         const int i2= 2*i;\
582         \
583         for (j=0; j<lumFilterSize; j++)\
584         {\
585             Y1 += lumSrc[j][i2] * lumFilter[j];\
586             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
587         }\
588         for (j=0; j<chrFilterSize; j++)\
589         {\
590             U += chrSrc[j][i] * chrFilter[j];\
591             V += chrSrc[j][i+VOFW] * chrFilter[j];\
592         }\
593         Y1>>=19;\
594         Y2>>=19;\
595         U >>=19;\
596         V >>=19;\
597         if (alpha){\
598             A1 = 1<<18;\
599             A2 = 1<<18;\
600             for (j=0; j<lumFilterSize; j++){\
601                 A1 += alpSrc[j][i2  ] * lumFilter[j];\
602                 A2 += alpSrc[j][i2+1] * lumFilter[j];\
603             }\
604             A1>>=19;\
605             A2>>=19;\
606         }\
607
608 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
609         YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
610         if ((Y1|Y2|U|V)&256)\
611         {\
612             if (Y1>255)   Y1=255; \
613             else if (Y1<0)Y1=0;   \
614             if (Y2>255)   Y2=255; \
615             else if (Y2<0)Y2=0;   \
616             if (U>255)    U=255;  \
617             else if (U<0) U=0;    \
618             if (V>255)    V=255;  \
619             else if (V<0) V=0;    \
620         }\
621         if (alpha && ((A1|A2)&256)){\
622             A1=av_clip_uint8(A1);\
623             A2=av_clip_uint8(A2);\
624         }
625
626 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
627     for (i=0; i<dstW; i++){\
628         int j;\
629         int Y = 0;\
630         int U = -128<<19;\
631         int V = -128<<19;\
632         int av_unused A;\
633         int R,G,B;\
634         \
635         for (j=0; j<lumFilterSize; j++){\
636             Y += lumSrc[j][i     ] * lumFilter[j];\
637         }\
638         for (j=0; j<chrFilterSize; j++){\
639             U += chrSrc[j][i     ] * chrFilter[j];\
640             V += chrSrc[j][i+VOFW] * chrFilter[j];\
641         }\
642         Y >>=10;\
643         U >>=10;\
644         V >>=10;\
645         if (alpha){\
646             A = rnd;\
647             for (j=0; j<lumFilterSize; j++)\
648                 A += alpSrc[j][i     ] * lumFilter[j];\
649             A >>=19;\
650             if (A&256)\
651                 A = av_clip_uint8(A);\
652         }\
653
654 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
655     YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
656         Y-= c->yuv2rgb_y_offset;\
657         Y*= c->yuv2rgb_y_coeff;\
658         Y+= rnd;\
659         R= Y + V*c->yuv2rgb_v2r_coeff;\
660         G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
661         B= Y +                          U*c->yuv2rgb_u2b_coeff;\
662         if ((R|G|B)&(0xC0000000)){\
663             if (R>=(256<<22))   R=(256<<22)-1; \
664             else if (R<0)R=0;   \
665             if (G>=(256<<22))   G=(256<<22)-1; \
666             else if (G<0)G=0;   \
667             if (B>=(256<<22))   B=(256<<22)-1; \
668             else if (B<0)B=0;   \
669         }\
670
671
672 #define YSCALE_YUV_2_GRAY16_C \
673     for (i=0; i<(dstW>>1); i++){\
674         int j;\
675         int Y1 = 1<<18;\
676         int Y2 = 1<<18;\
677         int U  = 1<<18;\
678         int V  = 1<<18;\
679         \
680         const int i2= 2*i;\
681         \
682         for (j=0; j<lumFilterSize; j++)\
683         {\
684             Y1 += lumSrc[j][i2] * lumFilter[j];\
685             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
686         }\
687         Y1>>=11;\
688         Y2>>=11;\
689         if ((Y1|Y2|U|V)&65536)\
690         {\
691             if (Y1>65535)   Y1=65535; \
692             else if (Y1<0)Y1=0;   \
693             if (Y2>65535)   Y2=65535; \
694             else if (Y2<0)Y2=0;   \
695         }
696
697 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
698     YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
699     r = (type *)c->table_rV[V];   \
700     g = (type *)(c->table_gU[U] + c->table_gV[V]); \
701     b = (type *)c->table_bU[U];   \
702
703 #define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
704     for (i=0; i<(dstW>>1); i++){ \
705         const int i2= 2*i;       \
706         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
707         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
708         int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
709         int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
710         type av_unused *r, *b, *g;                                    \
711         int av_unused A1, A2;                                         \
712         if (alpha){\
713             A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
714             A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
715         }\
716
717 #define YSCALE_YUV_2_GRAY16_2_C   \
718     for (i=0; i<(dstW>>1); i++){ \
719         const int i2= 2*i;       \
720         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
721         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
722
723 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
724     YSCALE_YUV_2_PACKED2_C(type,alpha)\
725     r = (type *)c->table_rV[V];\
726     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
727     b = (type *)c->table_bU[U];\
728
729 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
730     for (i=0; i<(dstW>>1); i++){\
731         const int i2= 2*i;\
732         int Y1= buf0[i2  ]>>7;\
733         int Y2= buf0[i2+1]>>7;\
734         int U= (uvbuf1[i     ])>>7;\
735         int V= (uvbuf1[i+VOFW])>>7;\
736         type av_unused *r, *b, *g;\
737         int av_unused A1, A2;\
738         if (alpha){\
739             A1= abuf0[i2  ]>>7;\
740             A2= abuf0[i2+1]>>7;\
741         }\
742
743 #define YSCALE_YUV_2_GRAY16_1_C \
744     for (i=0; i<(dstW>>1); i++){\
745         const int i2= 2*i;\
746         int Y1= buf0[i2  ]<<1;\
747         int Y2= buf0[i2+1]<<1;\
748
749 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
750     YSCALE_YUV_2_PACKED1_C(type,alpha)\
751     r = (type *)c->table_rV[V];\
752     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
753     b = (type *)c->table_bU[U];\
754
755 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
756     for (i=0; i<(dstW>>1); i++){\
757         const int i2= 2*i;\
758         int Y1= buf0[i2  ]>>7;\
759         int Y2= buf0[i2+1]>>7;\
760         int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
761         int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
762         type av_unused *r, *b, *g;\
763         int av_unused A1, A2;\
764         if (alpha){\
765             A1= abuf0[i2  ]>>7;\
766             A2= abuf0[i2+1]>>7;\
767         }\
768
769 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
770     YSCALE_YUV_2_PACKED1B_C(type,alpha)\
771     r = (type *)c->table_rV[V];\
772     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
773     b = (type *)c->table_bU[U];\
774
775 #define YSCALE_YUV_2_MONO2_C \
776     const uint8_t * const d128=dither_8x8_220[y&7];\
777     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
778     for (i=0; i<dstW-7; i+=8){\
779         int acc;\
780         acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
781         acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
782         acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
783         acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
784         acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
785         acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
786         acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
787         acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
788         ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
789         dest++;\
790     }\
791
792
793 #define YSCALE_YUV_2_MONOX_C \
794     const uint8_t * const d128=dither_8x8_220[y&7];\
795     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
796     int acc=0;\
797     for (i=0; i<dstW-1; i+=2){\
798         int j;\
799         int Y1=1<<18;\
800         int Y2=1<<18;\
801 \
802         for (j=0; j<lumFilterSize; j++)\
803         {\
804             Y1 += lumSrc[j][i] * lumFilter[j];\
805             Y2 += lumSrc[j][i+1] * lumFilter[j];\
806         }\
807         Y1>>=19;\
808         Y2>>=19;\
809         if ((Y1|Y2)&256)\
810         {\
811             if (Y1>255)   Y1=255;\
812             else if (Y1<0)Y1=0;\
813             if (Y2>255)   Y2=255;\
814             else if (Y2<0)Y2=0;\
815         }\
816         acc+= acc + g[Y1+d128[(i+0)&7]];\
817         acc+= acc + g[Y2+d128[(i+1)&7]];\
818         if ((i&7)==6){\
819             ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
820             dest++;\
821         }\
822     }
823
824
825 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
826     switch(c->dstFormat)\
827     {\
828     case PIX_FMT_RGB48BE:\
829     case PIX_FMT_RGB48LE:\
830         func(uint8_t,0)\
831             ((uint8_t*)dest)[ 0]= r[Y1];\
832             ((uint8_t*)dest)[ 1]= r[Y1];\
833             ((uint8_t*)dest)[ 2]= g[Y1];\
834             ((uint8_t*)dest)[ 3]= g[Y1];\
835             ((uint8_t*)dest)[ 4]= b[Y1];\
836             ((uint8_t*)dest)[ 5]= b[Y1];\
837             ((uint8_t*)dest)[ 6]= r[Y2];\
838             ((uint8_t*)dest)[ 7]= r[Y2];\
839             ((uint8_t*)dest)[ 8]= g[Y2];\
840             ((uint8_t*)dest)[ 9]= g[Y2];\
841             ((uint8_t*)dest)[10]= b[Y2];\
842             ((uint8_t*)dest)[11]= b[Y2];\
843             dest+=12;\
844         }\
845         break;\
846     case PIX_FMT_RGBA:\
847     case PIX_FMT_BGRA:\
848         if (CONFIG_SMALL){\
849             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
850             func(uint32_t,needAlpha)\
851                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
852                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
853             }\
854         }else{\
855             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
856                 func(uint32_t,1)\
857                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
858                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
859                 }\
860             }else{\
861                 func(uint32_t,0)\
862                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
863                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
864                 }\
865             }\
866         }\
867         break;\
868     case PIX_FMT_ARGB:\
869     case PIX_FMT_ABGR:\
870         if (CONFIG_SMALL){\
871             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
872             func(uint32_t,needAlpha)\
873                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
874                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
875             }\
876         }else{\
877             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
878                 func(uint32_t,1)\
879                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
880                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
881                 }\
882             }else{\
883                 func(uint32_t,0)\
884                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
885                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
886                 }\
887             }\
888         }                \
889         break;\
890     case PIX_FMT_RGB24:\
891         func(uint8_t,0)\
892             ((uint8_t*)dest)[0]= r[Y1];\
893             ((uint8_t*)dest)[1]= g[Y1];\
894             ((uint8_t*)dest)[2]= b[Y1];\
895             ((uint8_t*)dest)[3]= r[Y2];\
896             ((uint8_t*)dest)[4]= g[Y2];\
897             ((uint8_t*)dest)[5]= b[Y2];\
898             dest+=6;\
899         }\
900         break;\
901     case PIX_FMT_BGR24:\
902         func(uint8_t,0)\
903             ((uint8_t*)dest)[0]= b[Y1];\
904             ((uint8_t*)dest)[1]= g[Y1];\
905             ((uint8_t*)dest)[2]= r[Y1];\
906             ((uint8_t*)dest)[3]= b[Y2];\
907             ((uint8_t*)dest)[4]= g[Y2];\
908             ((uint8_t*)dest)[5]= r[Y2];\
909             dest+=6;\
910         }\
911         break;\
912     case PIX_FMT_RGB565:\
913     case PIX_FMT_BGR565:\
914         {\
915             const int dr1= dither_2x2_8[y&1    ][0];\
916             const int dg1= dither_2x2_4[y&1    ][0];\
917             const int db1= dither_2x2_8[(y&1)^1][0];\
918             const int dr2= dither_2x2_8[y&1    ][1];\
919             const int dg2= dither_2x2_4[y&1    ][1];\
920             const int db2= dither_2x2_8[(y&1)^1][1];\
921             func(uint16_t,0)\
922                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
923                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
924             }\
925         }\
926         break;\
927     case PIX_FMT_RGB555:\
928     case PIX_FMT_BGR555:\
929         {\
930             const int dr1= dither_2x2_8[y&1    ][0];\
931             const int dg1= dither_2x2_8[y&1    ][1];\
932             const int db1= dither_2x2_8[(y&1)^1][0];\
933             const int dr2= dither_2x2_8[y&1    ][1];\
934             const int dg2= dither_2x2_8[y&1    ][0];\
935             const int db2= dither_2x2_8[(y&1)^1][1];\
936             func(uint16_t,0)\
937                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
938                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
939             }\
940         }\
941         break;\
942     case PIX_FMT_RGB8:\
943     case PIX_FMT_BGR8:\
944         {\
945             const uint8_t * const d64= dither_8x8_73[y&7];\
946             const uint8_t * const d32= dither_8x8_32[y&7];\
947             func(uint8_t,0)\
948                 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
949                 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
950             }\
951         }\
952         break;\
953     case PIX_FMT_RGB4:\
954     case PIX_FMT_BGR4:\
955         {\
956             const uint8_t * const d64= dither_8x8_73 [y&7];\
957             const uint8_t * const d128=dither_8x8_220[y&7];\
958             func(uint8_t,0)\
959                 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
960                                  + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
961             }\
962         }\
963         break;\
964     case PIX_FMT_RGB4_BYTE:\
965     case PIX_FMT_BGR4_BYTE:\
966         {\
967             const uint8_t * const d64= dither_8x8_73 [y&7];\
968             const uint8_t * const d128=dither_8x8_220[y&7];\
969             func(uint8_t,0)\
970                 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
971                 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
972             }\
973         }\
974         break;\
975     case PIX_FMT_MONOBLACK:\
976     case PIX_FMT_MONOWHITE:\
977         {\
978             func_monoblack\
979         }\
980         break;\
981     case PIX_FMT_YUYV422:\
982         func2\
983             ((uint8_t*)dest)[2*i2+0]= Y1;\
984             ((uint8_t*)dest)[2*i2+1]= U;\
985             ((uint8_t*)dest)[2*i2+2]= Y2;\
986             ((uint8_t*)dest)[2*i2+3]= V;\
987         }                \
988         break;\
989     case PIX_FMT_UYVY422:\
990         func2\
991             ((uint8_t*)dest)[2*i2+0]= U;\
992             ((uint8_t*)dest)[2*i2+1]= Y1;\
993             ((uint8_t*)dest)[2*i2+2]= V;\
994             ((uint8_t*)dest)[2*i2+3]= Y2;\
995         }                \
996         break;\
997     case PIX_FMT_GRAY16BE:\
998         func_g16\
999             ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1000             ((uint8_t*)dest)[2*i2+1]= Y1;\
1001             ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1002             ((uint8_t*)dest)[2*i2+3]= Y2;\
1003         }                \
1004         break;\
1005     case PIX_FMT_GRAY16LE:\
1006         func_g16\
1007             ((uint8_t*)dest)[2*i2+0]= Y1;\
1008             ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1009             ((uint8_t*)dest)[2*i2+2]= Y2;\
1010             ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1011         }                \
1012         break;\
1013     }\
1014
1015
1016 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1017                                   const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1018                                   const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1019 {
1020     int i;
1021     YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
1022 }
1023
1024 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1025                                     const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1026                                     const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1027 {
1028     int i;
1029     int step= fmt_depth(c->dstFormat)/8;
1030     int aidx= 3;
1031
1032     switch(c->dstFormat){
1033     case PIX_FMT_ARGB:
1034         dest++;
1035         aidx= 0;
1036     case PIX_FMT_RGB24:
1037         aidx--;
1038     case PIX_FMT_RGBA:
1039         if (CONFIG_SMALL){
1040             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1041             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1042                 dest[aidx]= needAlpha ? A : 255;
1043                 dest[0]= R>>22;
1044                 dest[1]= G>>22;
1045                 dest[2]= B>>22;
1046                 dest+= step;
1047             }
1048         }else{
1049             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1050                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1051                     dest[aidx]= A;
1052                     dest[0]= R>>22;
1053                     dest[1]= G>>22;
1054                     dest[2]= B>>22;
1055                     dest+= step;
1056                 }
1057             }else{
1058                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1059                     dest[aidx]= 255;
1060                     dest[0]= R>>22;
1061                     dest[1]= G>>22;
1062                     dest[2]= B>>22;
1063                     dest+= step;
1064                 }
1065             }
1066         }
1067         break;
1068     case PIX_FMT_ABGR:
1069         dest++;
1070         aidx= 0;
1071     case PIX_FMT_BGR24:
1072         aidx--;
1073     case PIX_FMT_BGRA:
1074         if (CONFIG_SMALL){
1075             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1076             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1077                 dest[aidx]= needAlpha ? A : 255;
1078                 dest[0]= B>>22;
1079                 dest[1]= G>>22;
1080                 dest[2]= R>>22;
1081                 dest+= step;
1082             }
1083         }else{
1084             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1085                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1086                     dest[aidx]= A;
1087                     dest[0]= B>>22;
1088                     dest[1]= G>>22;
1089                     dest[2]= R>>22;
1090                     dest+= step;
1091                 }
1092             }else{
1093                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1094                     dest[aidx]= 255;
1095                     dest[0]= B>>22;
1096                     dest[1]= G>>22;
1097                     dest[2]= R>>22;
1098                     dest+= step;
1099                 }
1100             }
1101         }
1102         break;
1103     default:
1104         assert(0);
1105     }
1106 }
1107
1108 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1109     int i;
1110     uint8_t *ptr = plane + stride*y;
1111     for (i=0; i<height; i++){
1112         memset(ptr, val, width);
1113         ptr += stride;
1114     }
1115 }
1116
1117 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1118 {
1119     int i;
1120     for (i = 0; i < width; i++) {
1121         int r = src[i*6+0];
1122         int g = src[i*6+2];
1123         int b = src[i*6+4];
1124
1125         dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1126     }
1127 }
1128
1129 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1130                              uint8_t *src1, uint8_t *src2, int width)
1131 {
1132     int i;
1133     assert(src1==src2);
1134     for (i = 0; i < width; i++) {
1135         int r = src1[6*i + 0];
1136         int g = src1[6*i + 2];
1137         int b = src1[6*i + 4];
1138
1139         dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1140         dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1141     }
1142 }
1143
1144 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1145                                   uint8_t *src1, uint8_t *src2, int width)
1146 {
1147     int i;
1148     assert(src1==src2);
1149     for (i = 0; i < width; i++) {
1150         int r= src1[12*i + 0] + src1[12*i + 6];
1151         int g= src1[12*i + 2] + src1[12*i + 8];
1152         int b= src1[12*i + 4] + src1[12*i + 10];
1153
1154         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1155         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1156     }
1157 }
1158
1159 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1160 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1161 {\
1162     int i;\
1163     for (i=0; i<width; i++)\
1164     {\
1165         int b= (((const type*)src)[i]>>shb)&maskb;\
1166         int g= (((const type*)src)[i]>>shg)&maskg;\
1167         int r= (((const type*)src)[i]>>shr)&maskr;\
1168 \
1169         dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1170     }\
1171 }
1172
1173 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1174 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1175 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1176 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1177 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1178 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1179
1180 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused){
1181     int i;
1182     for (i=0; i<width; i++){
1183         dst[i]= src[4*i];
1184     }
1185 }
1186
1187 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1188 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1189 {\
1190     int i;\
1191     for (i=0; i<width; i++)\
1192     {\
1193         int b= (((const type*)src)[i]&maskb)>>shb;\
1194         int g= (((const type*)src)[i]&maskg)>>shg;\
1195         int r= (((const type*)src)[i]&maskr)>>shr;\
1196 \
1197         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1198         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1199     }\
1200 }\
1201 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1202 {\
1203     int i;\
1204     for (i=0; i<width; i++)\
1205     {\
1206         int pix0= ((const type*)src)[2*i+0];\
1207         int pix1= ((const type*)src)[2*i+1];\
1208         int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1209         int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1210         int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1211         g&= maskg|(2*maskg);\
1212 \
1213         g>>=shg;\
1214 \
1215         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1216         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1217     }\
1218 }
1219
1220 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1221 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1222 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0,          0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1223 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0,          0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1224 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0,          0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1225 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0,          0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1226
1227 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1228 {
1229     int i;
1230     for (i=0; i<width; i++)
1231     {
1232         int d= src[i];
1233
1234         dst[i]= pal[d] & 0xFF;
1235     }
1236 }
1237
1238 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1239                            const uint8_t *src1, const uint8_t *src2,
1240                            long width, uint32_t *pal)
1241 {
1242     int i;
1243     assert(src1 == src2);
1244     for (i=0; i<width; i++)
1245     {
1246         int p= pal[src1[i]];
1247
1248         dstU[i]= p>>8;
1249         dstV[i]= p>>16;
1250     }
1251 }
1252
1253 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1254 {
1255     int i, j;
1256     for (i=0; i<width/8; i++){
1257         int d= ~src[i];
1258         for(j=0; j<8; j++)
1259             dst[8*i+j]= ((d>>(7-j))&1)*255;
1260     }
1261 }
1262
1263 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1264 {
1265     int i, j;
1266     for (i=0; i<width/8; i++){
1267         int d= src[i];
1268         for(j=0; j<8; j++)
1269             dst[8*i+j]= ((d>>(7-j))&1)*255;
1270     }
1271 }
1272
1273
1274 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1275 //Plain C versions
1276 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1277 #define COMPILE_C
1278 #endif
1279
1280 #if ARCH_PPC
1281 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1282 #define COMPILE_ALTIVEC
1283 #endif
1284 #endif //ARCH_PPC
1285
1286 #if ARCH_X86
1287
1288 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1289 #define COMPILE_MMX
1290 #endif
1291
1292 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1293 #define COMPILE_MMX2
1294 #endif
1295
1296 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1297 #define COMPILE_3DNOW
1298 #endif
1299 #endif //ARCH_X86
1300
1301 #define COMPILE_TEMPLATE_MMX 0
1302 #define COMPILE_TEMPLATE_MMX2 0
1303 #define COMPILE_TEMPLATE_AMD3DNOW 0
1304 #define COMPILE_TEMPLATE_ALTIVEC 0
1305
1306 #ifdef COMPILE_C
1307 #define RENAME(a) a ## _C
1308 #include "swscale_template.c"
1309 #endif
1310
1311 #ifdef COMPILE_ALTIVEC
1312 #undef RENAME
1313 #undef COMPILE_TEMPLATE_ALTIVEC
1314 #define COMPILE_TEMPLATE_ALTIVEC 1
1315 #define RENAME(a) a ## _altivec
1316 #include "swscale_template.c"
1317 #endif
1318
1319 #if ARCH_X86
1320
1321 //MMX versions
1322 #ifdef COMPILE_MMX
1323 #undef RENAME
1324 #undef COMPILE_TEMPLATE_MMX
1325 #undef COMPILE_TEMPLATE_MMX2
1326 #undef COMPILE_TEMPLATE_AMD3DNOW
1327 #define COMPILE_TEMPLATE_MMX 1
1328 #define COMPILE_TEMPLATE_MMX2 0
1329 #define COMPILE_TEMPLATE_AMD3DNOW 0
1330 #define RENAME(a) a ## _MMX
1331 #include "swscale_template.c"
1332 #endif
1333
1334 //MMX2 versions
1335 #ifdef COMPILE_MMX2
1336 #undef RENAME
1337 #undef COMPILE_TEMPLATE_MMX
1338 #undef COMPILE_TEMPLATE_MMX2
1339 #undef COMPILE_TEMPLATE_AMD3DNOW
1340 #define COMPILE_TEMPLATE_MMX 1
1341 #define COMPILE_TEMPLATE_MMX2 1
1342 #define COMPILE_TEMPLATE_AMD3DNOW 0
1343 #define RENAME(a) a ## _MMX2
1344 #include "swscale_template.c"
1345 #endif
1346
1347 //3DNOW versions
1348 #ifdef COMPILE_3DNOW
1349 #undef RENAME
1350 #undef COMPILE_TEMPLATE_MMX
1351 #undef COMPILE_TEMPLATE_MMX2
1352 #undef COMPILE_TEMPLATE_AMD3DNOW
1353 #define COMPILE_TEMPLATE_MMX 1
1354 #define COMPILE_TEMPLATE_MMX2 0
1355 #define COMPILE_TEMPLATE_AMD3DNOW 1
1356 #define RENAME(a) a ## _3DNow
1357 #include "swscale_template.c"
1358 #endif
1359
1360 #endif //ARCH_X86
1361
1362 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1363 {
1364 //    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1365     if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
1366     else                return getSplineCoeff(        0.0,
1367                                              b+ 2.0*c + 3.0*d,
1368                                                     c + 3.0*d,
1369                                             -b- 3.0*c - 6.0*d,
1370                                             dist-1.0);
1371 }
1372
1373 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1374                              int srcW, int dstW, int filterAlign, int one, int flags,
1375                              SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1376 {
1377     int i;
1378     int filterSize;
1379     int filter2Size;
1380     int minFilterSize;
1381     int64_t *filter=NULL;
1382     int64_t *filter2=NULL;
1383     const int64_t fone= 1LL<<54;
1384     int ret= -1;
1385 #if ARCH_X86
1386     if (flags & SWS_CPU_CAPS_MMX)
1387         __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1388 #endif
1389
1390     // NOTE: the +1 is for the MMX scaler which reads over the end
1391     *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1392
1393     if (FFABS(xInc - 0x10000) <10) // unscaled
1394     {
1395         int i;
1396         filterSize= 1;
1397         filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1398
1399         for (i=0; i<dstW; i++)
1400         {
1401             filter[i*filterSize]= fone;
1402             (*filterPos)[i]=i;
1403         }
1404
1405     }
1406     else if (flags&SWS_POINT) // lame looking point sampling mode
1407     {
1408         int i;
1409         int xDstInSrc;
1410         filterSize= 1;
1411         filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1412
1413         xDstInSrc= xInc/2 - 0x8000;
1414         for (i=0; i<dstW; i++)
1415         {
1416             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1417
1418             (*filterPos)[i]= xx;
1419             filter[i]= fone;
1420             xDstInSrc+= xInc;
1421         }
1422     }
1423     else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1424     {
1425         int i;
1426         int xDstInSrc;
1427         filterSize= 2;
1428         filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1429
1430         xDstInSrc= xInc/2 - 0x8000;
1431         for (i=0; i<dstW; i++)
1432         {
1433             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1434             int j;
1435
1436             (*filterPos)[i]= xx;
1437                 //bilinear upscale / linear interpolate / area averaging
1438                 for (j=0; j<filterSize; j++)
1439                 {
1440                     int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1441                     if (coeff<0) coeff=0;
1442                     filter[i*filterSize + j]= coeff;
1443                     xx++;
1444                 }
1445             xDstInSrc+= xInc;
1446         }
1447     }
1448     else
1449     {
1450         int xDstInSrc;
1451         int sizeFactor;
1452
1453         if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1454         else if (flags&SWS_X)            sizeFactor=  8;
1455         else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1456         else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1457         else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1458         else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1459         else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1460         else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1461         else {
1462             sizeFactor= 0; //GCC warning killer
1463             assert(0);
1464         }
1465
1466         if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1467         else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1468
1469         if (filterSize > srcW-2) filterSize=srcW-2;
1470
1471         filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1472
1473         xDstInSrc= xInc - 0x10000;
1474         for (i=0; i<dstW; i++)
1475         {
1476             int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1477             int j;
1478             (*filterPos)[i]= xx;
1479             for (j=0; j<filterSize; j++)
1480             {
1481                 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1482                 double floatd;
1483                 int64_t coeff;
1484
1485                 if (xInc > 1<<16)
1486                     d= d*dstW/srcW;
1487                 floatd= d * (1.0/(1<<30));
1488
1489                 if (flags & SWS_BICUBIC)
1490                 {
1491                     int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1492                     int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1493                     int64_t dd = ( d*d)>>30;
1494                     int64_t ddd= (dd*d)>>30;
1495
1496                     if      (d < 1LL<<30)
1497                         coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1498                     else if (d < 1LL<<31)
1499                         coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1500                     else
1501                         coeff=0.0;
1502                     coeff *= fone>>(30+24);
1503                 }
1504 /*                else if (flags & SWS_X)
1505                 {
1506                     double p= param ? param*0.01 : 0.3;
1507                     coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1508                     coeff*= pow(2.0, - p*d*d);
1509                 }*/
1510                 else if (flags & SWS_X)
1511                 {
1512                     double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1513                     double c;
1514
1515                     if (floatd<1.0)
1516                         c = cos(floatd*PI);
1517                     else
1518                         c=-1.0;
1519                     if (c<0.0)      c= -pow(-c, A);
1520                     else            c=  pow( c, A);
1521                     coeff= (c*0.5 + 0.5)*fone;
1522                 }
1523                 else if (flags & SWS_AREA)
1524                 {
1525                     int64_t d2= d - (1<<29);
1526                     if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1527                     else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1528                     else coeff=0.0;
1529                     coeff *= fone>>(30+16);
1530                 }
1531                 else if (flags & SWS_GAUSS)
1532                 {
1533                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1534                     coeff = (pow(2.0, - p*floatd*floatd))*fone;
1535                 }
1536                 else if (flags & SWS_SINC)
1537                 {
1538                     coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1539                 }
1540                 else if (flags & SWS_LANCZOS)
1541                 {
1542                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1543                     coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1544                     if (floatd>p) coeff=0;
1545                 }
1546                 else if (flags & SWS_BILINEAR)
1547                 {
1548                     coeff= (1<<30) - d;
1549                     if (coeff<0) coeff=0;
1550                     coeff *= fone >> 30;
1551                 }
1552                 else if (flags & SWS_SPLINE)
1553                 {
1554                     double p=-2.196152422706632;
1555                     coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1556                 }
1557                 else {
1558                     coeff= 0.0; //GCC warning killer
1559                     assert(0);
1560                 }
1561
1562                 filter[i*filterSize + j]= coeff;
1563                 xx++;
1564             }
1565             xDstInSrc+= 2*xInc;
1566         }
1567     }
1568
1569     /* apply src & dst Filter to filter -> filter2
1570        av_free(filter);
1571     */
1572     assert(filterSize>0);
1573     filter2Size= filterSize;
1574     if (srcFilter) filter2Size+= srcFilter->length - 1;
1575     if (dstFilter) filter2Size+= dstFilter->length - 1;
1576     assert(filter2Size>0);
1577     filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1578
1579     for (i=0; i<dstW; i++)
1580     {
1581         int j, k;
1582
1583         if(srcFilter){
1584             for (k=0; k<srcFilter->length; k++){
1585                 for (j=0; j<filterSize; j++)
1586                     filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1587             }
1588         }else{
1589             for (j=0; j<filterSize; j++)
1590                 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1591         }
1592         //FIXME dstFilter
1593
1594         (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1595     }
1596     av_freep(&filter);
1597
1598     /* try to reduce the filter-size (step1 find size and shift left) */
1599     // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1600     minFilterSize= 0;
1601     for (i=dstW-1; i>=0; i--)
1602     {
1603         int min= filter2Size;
1604         int j;
1605         int64_t cutOff=0.0;
1606
1607         /* get rid off near zero elements on the left by shifting left */
1608         for (j=0; j<filter2Size; j++)
1609         {
1610             int k;
1611             cutOff += FFABS(filter2[i*filter2Size]);
1612
1613             if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1614
1615             /* preserve monotonicity because the core can't handle the filter otherwise */
1616             if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1617
1618             // move filter coefficients left
1619             for (k=1; k<filter2Size; k++)
1620                 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1621             filter2[i*filter2Size + k - 1]= 0;
1622             (*filterPos)[i]++;
1623         }
1624
1625         cutOff=0;
1626         /* count near zeros on the right */
1627         for (j=filter2Size-1; j>0; j--)
1628         {
1629             cutOff += FFABS(filter2[i*filter2Size + j]);
1630
1631             if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1632             min--;
1633         }
1634
1635         if (min>minFilterSize) minFilterSize= min;
1636     }
1637
1638     if (flags & SWS_CPU_CAPS_ALTIVEC) {
1639         // we can handle the special case 4,
1640         // so we don't want to go to the full 8
1641         if (minFilterSize < 5)
1642             filterAlign = 4;
1643
1644         // We really don't want to waste our time
1645         // doing useless computation, so fall back on
1646         // the scalar C code for very small filters.
1647         // Vectorizing is worth it only if you have a
1648         // decent-sized vector.
1649         if (minFilterSize < 3)
1650             filterAlign = 1;
1651     }
1652
1653     if (flags & SWS_CPU_CAPS_MMX) {
1654         // special case for unscaled vertical filtering
1655         if (minFilterSize == 1 && filterAlign == 2)
1656             filterAlign= 1;
1657     }
1658
1659     assert(minFilterSize > 0);
1660     filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1661     assert(filterSize > 0);
1662     filter= av_malloc(filterSize*dstW*sizeof(*filter));
1663     if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1664         goto error;
1665     *outFilterSize= filterSize;
1666
1667     if (flags&SWS_PRINT_INFO)
1668         av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1669     /* try to reduce the filter-size (step2 reduce it) */
1670     for (i=0; i<dstW; i++)
1671     {
1672         int j;
1673
1674         for (j=0; j<filterSize; j++)
1675         {
1676             if (j>=filter2Size) filter[i*filterSize + j]= 0;
1677             else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1678             if((flags & SWS_BITEXACT) && j>=minFilterSize)
1679                 filter[i*filterSize + j]= 0;
1680         }
1681     }
1682
1683
1684     //FIXME try to align filterPos if possible
1685
1686     //fix borders
1687     for (i=0; i<dstW; i++)
1688     {
1689         int j;
1690         if ((*filterPos)[i] < 0)
1691         {
1692             // move filter coefficients left to compensate for filterPos
1693             for (j=1; j<filterSize; j++)
1694             {
1695                 int left= FFMAX(j + (*filterPos)[i], 0);
1696                 filter[i*filterSize + left] += filter[i*filterSize + j];
1697                 filter[i*filterSize + j]=0;
1698             }
1699             (*filterPos)[i]= 0;
1700         }
1701
1702         if ((*filterPos)[i] + filterSize > srcW)
1703         {
1704             int shift= (*filterPos)[i] + filterSize - srcW;
1705             // move filter coefficients right to compensate for filterPos
1706             for (j=filterSize-2; j>=0; j--)
1707             {
1708                 int right= FFMIN(j + shift, filterSize-1);
1709                 filter[i*filterSize +right] += filter[i*filterSize +j];
1710                 filter[i*filterSize +j]=0;
1711             }
1712             (*filterPos)[i]= srcW - filterSize;
1713         }
1714     }
1715
1716     // Note the +1 is for the MMX scaler which reads over the end
1717     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1718     *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1719
1720     /* normalize & store in outFilter */
1721     for (i=0; i<dstW; i++)
1722     {
1723         int j;
1724         int64_t error=0;
1725         int64_t sum=0;
1726
1727         for (j=0; j<filterSize; j++)
1728         {
1729             sum+= filter[i*filterSize + j];
1730         }
1731         sum= (sum + one/2)/ one;
1732         for (j=0; j<*outFilterSize; j++)
1733         {
1734             int64_t v= filter[i*filterSize + j] + error;
1735             int intV= ROUNDED_DIV(v, sum);
1736             (*outFilter)[i*(*outFilterSize) + j]= intV;
1737             error= v - intV*sum;
1738         }
1739     }
1740
1741     (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1742     for (i=0; i<*outFilterSize; i++)
1743     {
1744         int j= dstW*(*outFilterSize);
1745         (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1746     }
1747
1748     ret=0;
1749 error:
1750     av_free(filter);
1751     av_free(filter2);
1752     return ret;
1753 }
1754
1755 #ifdef COMPILE_MMX2
1756 static void initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1757 {
1758     uint8_t *fragmentA;
1759     x86_reg imm8OfPShufW1A;
1760     x86_reg imm8OfPShufW2A;
1761     x86_reg fragmentLengthA;
1762     uint8_t *fragmentB;
1763     x86_reg imm8OfPShufW1B;
1764     x86_reg imm8OfPShufW2B;
1765     x86_reg fragmentLengthB;
1766     int fragmentPos;
1767
1768     int xpos, i;
1769
1770     // create an optimized horizontal scaling routine
1771
1772     //code fragment
1773
1774     __asm__ volatile(
1775         "jmp                         9f                 \n\t"
1776     // Begin
1777         "0:                                             \n\t"
1778         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1779         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1780         "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1781         "punpcklbw                %%mm7, %%mm1          \n\t"
1782         "punpcklbw                %%mm7, %%mm0          \n\t"
1783         "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1784         "1:                                             \n\t"
1785         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1786         "2:                                             \n\t"
1787         "psubw                    %%mm1, %%mm0          \n\t"
1788         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1789         "pmullw                   %%mm3, %%mm0          \n\t"
1790         "psllw                       $7, %%mm1          \n\t"
1791         "paddw                    %%mm1, %%mm0          \n\t"
1792
1793         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1794
1795         "add                         $8, %%"REG_a"      \n\t"
1796     // End
1797         "9:                                             \n\t"
1798 //        "int $3                                         \n\t"
1799         "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1800         "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1801         "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1802         "dec                         %1                 \n\t"
1803         "dec                         %2                 \n\t"
1804         "sub                         %0, %1             \n\t"
1805         "sub                         %0, %2             \n\t"
1806         "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1807         "sub                         %0, %3             \n\t"
1808
1809
1810         :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1811         "=r" (fragmentLengthA)
1812     );
1813
1814     __asm__ volatile(
1815         "jmp                         9f                 \n\t"
1816     // Begin
1817         "0:                                             \n\t"
1818         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1819         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1820         "punpcklbw                %%mm7, %%mm0          \n\t"
1821         "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1822         "1:                                             \n\t"
1823         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1824         "2:                                             \n\t"
1825         "psubw                    %%mm1, %%mm0          \n\t"
1826         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1827         "pmullw                   %%mm3, %%mm0          \n\t"
1828         "psllw                       $7, %%mm1          \n\t"
1829         "paddw                    %%mm1, %%mm0          \n\t"
1830
1831         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1832
1833         "add                         $8, %%"REG_a"      \n\t"
1834     // End
1835         "9:                                             \n\t"
1836 //        "int                       $3                   \n\t"
1837         "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1838         "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1839         "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1840         "dec                         %1                 \n\t"
1841         "dec                         %2                 \n\t"
1842         "sub                         %0, %1             \n\t"
1843         "sub                         %0, %2             \n\t"
1844         "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1845         "sub                         %0, %3             \n\t"
1846
1847
1848         :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1849         "=r" (fragmentLengthB)
1850     );
1851
1852     xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1853     fragmentPos=0;
1854
1855     for (i=0; i<dstW/numSplits; i++)
1856     {
1857         int xx=xpos>>16;
1858
1859         if ((i&3) == 0)
1860         {
1861             int a=0;
1862             int b=((xpos+xInc)>>16) - xx;
1863             int c=((xpos+xInc*2)>>16) - xx;
1864             int d=((xpos+xInc*3)>>16) - xx;
1865             int inc                = (d+1<4);
1866             uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
1867             x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
1868             x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
1869             x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1870             int maxShift= 3-(d+inc);
1871             int shift=0;
1872
1873             filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1874             filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1875             filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1876             filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1877             filterPos[i/2]= xx;
1878
1879             memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1880
1881             filterCode[fragmentPos + imm8OfPShufW1]=
1882                 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1883             filterCode[fragmentPos + imm8OfPShufW2]=
1884                 a | (b<<2) | (c<<4) | (d<<6);
1885
1886             if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1887             else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1888
1889             if (shift && i>=shift)
1890             {
1891                 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1892                 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1893                 filterPos[i/2]-=shift;
1894             }
1895
1896             fragmentPos+= fragmentLength;
1897
1898             filterCode[fragmentPos]= RET;
1899         }
1900         xpos+=xInc;
1901     }
1902     filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1903 }
1904 #endif /* COMPILE_MMX2 */
1905
1906 static void globalInit(void){
1907     // generating tables:
1908     int i;
1909     for (i=0; i<768; i++){
1910         int c= av_clip_uint8(i-256);
1911         clip_table[i]=c;
1912     }
1913 }
1914
1915 static SwsFunc getSwsFunc(SwsContext *c)
1916 {
1917 #if CONFIG_RUNTIME_CPUDETECT
1918     int flags = c->flags;
1919
1920 #if ARCH_X86 && CONFIG_GPL
1921     // ordered per speed fastest first
1922     if (flags & SWS_CPU_CAPS_MMX2) {
1923         sws_init_swScale_MMX2(c);
1924         return swScale_MMX2;
1925     } else if (flags & SWS_CPU_CAPS_3DNOW) {
1926         sws_init_swScale_3DNow(c);
1927         return swScale_3DNow;
1928     } else if (flags & SWS_CPU_CAPS_MMX) {
1929         sws_init_swScale_MMX(c);
1930         return swScale_MMX;
1931     } else {
1932         sws_init_swScale_C(c);
1933         return swScale_C;
1934     }
1935
1936 #else
1937 #if ARCH_PPC
1938     if (flags & SWS_CPU_CAPS_ALTIVEC) {
1939         sws_init_swScale_altivec(c);
1940         return swScale_altivec;
1941     } else {
1942         sws_init_swScale_C(c);
1943         return swScale_C;
1944     }
1945 #endif
1946     sws_init_swScale_C(c);
1947     return swScale_C;
1948 #endif /* ARCH_X86 && CONFIG_GPL */
1949 #else //CONFIG_RUNTIME_CPUDETECT
1950 #if   COMPILE_TEMPLATE_MMX2
1951     sws_init_swScale_MMX2(c);
1952     return swScale_MMX2;
1953 #elif COMPILE_TEMPLATE_AMD3DNOW
1954     sws_init_swScale_3DNow(c);
1955     return swScale_3DNow;
1956 #elif COMPILE_TEMPLATE_MMX
1957     sws_init_swScale_MMX(c);
1958     return swScale_MMX;
1959 #elif COMPILE_TEMPLATE_ALTIVEC
1960     sws_init_swScale_altivec(c);
1961     return swScale_altivec;
1962 #else
1963     sws_init_swScale_C(c);
1964     return swScale_C;
1965 #endif
1966 #endif //!CONFIG_RUNTIME_CPUDETECT
1967 }
1968
1969 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1970                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1971     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1972     /* Copy Y plane */
1973     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1974         memcpy(dst, src[0], srcSliceH*dstStride[0]);
1975     else
1976     {
1977         int i;
1978         const uint8_t *srcPtr= src[0];
1979         uint8_t *dstPtr= dst;
1980         for (i=0; i<srcSliceH; i++)
1981         {
1982             memcpy(dstPtr, srcPtr, c->srcW);
1983             srcPtr+= srcStride[0];
1984             dstPtr+= dstStride[0];
1985         }
1986     }
1987     dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1988     if (c->dstFormat == PIX_FMT_NV12)
1989         interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1990     else
1991         interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1992
1993     return srcSliceH;
1994 }
1995
1996 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1997                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1998     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1999
2000     yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2001
2002     return srcSliceH;
2003 }
2004
2005 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2006                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2007     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2008
2009     yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2010
2011     return srcSliceH;
2012 }
2013
2014 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2015                                 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2016     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2017
2018     yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2019
2020     return srcSliceH;
2021 }
2022
2023 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2024                                 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2025     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2026
2027     yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2028
2029     return srcSliceH;
2030 }
2031
2032 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2033                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2034     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2035     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2036     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2037
2038     yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2039
2040     if (dstParam[3])
2041         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2042
2043     return srcSliceH;
2044 }
2045
2046 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2047                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2048     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2049     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2050     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2051
2052     yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2053
2054     return srcSliceH;
2055 }
2056
2057 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2058                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2059     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2060     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2061     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2062
2063     uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2064
2065     if (dstParam[3])
2066         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2067
2068     return srcSliceH;
2069 }
2070
2071 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2072                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2073     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2074     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2075     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2076
2077     uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2078
2079     return srcSliceH;
2080 }
2081
2082 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2083                           int srcSliceH, uint8_t* dst[], int dstStride[]){
2084     const enum PixelFormat srcFormat= c->srcFormat;
2085     const enum PixelFormat dstFormat= c->dstFormat;
2086     void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2087                  const uint8_t *palette)=NULL;
2088     int i;
2089     uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2090     uint8_t *srcPtr= src[0];
2091
2092     if (!usePal(srcFormat))
2093         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2094                sws_format_name(srcFormat), sws_format_name(dstFormat));
2095
2096     switch(dstFormat){
2097     case PIX_FMT_RGB32  : conv = palette8topacked32; break;
2098     case PIX_FMT_BGR32  : conv = palette8topacked32; break;
2099     case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2100     case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2101     case PIX_FMT_RGB24  : conv = palette8topacked24; break;
2102     case PIX_FMT_BGR24  : conv = palette8topacked24; break;
2103     default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2104                     sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2105     }
2106
2107
2108     for (i=0; i<srcSliceH; i++) {
2109         conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2110         srcPtr+= srcStride[0];
2111         dstPtr+= dstStride[0];
2112     }
2113
2114     return srcSliceH;
2115 }
2116
2117 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2118 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2119                           int srcSliceH, uint8_t* dst[], int dstStride[]){
2120     const enum PixelFormat srcFormat= c->srcFormat;
2121     const enum PixelFormat dstFormat= c->dstFormat;
2122     const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2123     const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2124     const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2125     const int dstId= fmt_depth(dstFormat) >> 2;
2126     void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2127
2128     /* BGR -> BGR */
2129     if (  (isBGR(srcFormat) && isBGR(dstFormat))
2130        || (isRGB(srcFormat) && isRGB(dstFormat))){
2131         switch(srcId | (dstId<<4)){
2132         case 0x34: conv= rgb16to15; break;
2133         case 0x36: conv= rgb24to15; break;
2134         case 0x38: conv= rgb32to15; break;
2135         case 0x43: conv= rgb15to16; break;
2136         case 0x46: conv= rgb24to16; break;
2137         case 0x48: conv= rgb32to16; break;
2138         case 0x63: conv= rgb15to24; break;
2139         case 0x64: conv= rgb16to24; break;
2140         case 0x68: conv= rgb32to24; break;
2141         case 0x83: conv= rgb15to32; break;
2142         case 0x84: conv= rgb16to32; break;
2143         case 0x86: conv= rgb24to32; break;
2144         default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2145                         sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2146         }
2147     }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
2148              || (isRGB(srcFormat) && isBGR(dstFormat))){
2149         switch(srcId | (dstId<<4)){
2150         case 0x33: conv= rgb15tobgr15; break;
2151         case 0x34: conv= rgb16tobgr15; break;
2152         case 0x36: conv= rgb24tobgr15; break;
2153         case 0x38: conv= rgb32tobgr15; break;
2154         case 0x43: conv= rgb15tobgr16; break;
2155         case 0x44: conv= rgb16tobgr16; break;
2156         case 0x46: conv= rgb24tobgr16; break;
2157         case 0x48: conv= rgb32tobgr16; break;
2158         case 0x63: conv= rgb15tobgr24; break;
2159         case 0x64: conv= rgb16tobgr24; break;
2160         case 0x66: conv= rgb24tobgr24; break;
2161         case 0x68: conv= rgb32tobgr24; break;
2162         case 0x83: conv= rgb15tobgr32; break;
2163         case 0x84: conv= rgb16tobgr32; break;
2164         case 0x86: conv= rgb24tobgr32; break;
2165         case 0x88: conv= rgb32tobgr32; break;
2166         default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2167                         sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2168         }
2169     }else{
2170         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2171                sws_format_name(srcFormat), sws_format_name(dstFormat));
2172     }
2173
2174     if(conv)
2175     {
2176         uint8_t *srcPtr= src[0];
2177         if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2178             srcPtr += ALT32_CORR;
2179
2180         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2181             conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2182         else
2183         {
2184             int i;
2185             uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2186
2187             for (i=0; i<srcSliceH; i++)
2188             {
2189                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2190                 srcPtr+= srcStride[0];
2191                 dstPtr+= dstStride[0];
2192             }
2193         }
2194     }
2195     return srcSliceH;
2196 }
2197
2198 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2199                               int srcSliceH, uint8_t* dst[], int dstStride[]){
2200
2201     rgb24toyv12(
2202         src[0],
2203         dst[0]+ srcSliceY    *dstStride[0],
2204         dst[1]+(srcSliceY>>1)*dstStride[1],
2205         dst[2]+(srcSliceY>>1)*dstStride[2],
2206         c->srcW, srcSliceH,
2207         dstStride[0], dstStride[1], srcStride[0]);
2208     if (dst[3])
2209         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2210     return srcSliceH;
2211 }
2212
2213 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2214                              int srcSliceH, uint8_t* dst[], int dstStride[]){
2215     int i;
2216
2217     /* copy Y */
2218     if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2219         memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2220     else{
2221         uint8_t *srcPtr= src[0];
2222         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2223
2224         for (i=0; i<srcSliceH; i++)
2225         {
2226             memcpy(dstPtr, srcPtr, c->srcW);
2227             srcPtr+= srcStride[0];
2228             dstPtr+= dstStride[0];
2229         }
2230     }
2231
2232     if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2233         planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2234                  srcSliceH >> 2, srcStride[1], dstStride[1]);
2235         planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2236                  srcSliceH >> 2, srcStride[2], dstStride[2]);
2237     }else{
2238         planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2239                  srcSliceH >> 2, srcStride[1], dstStride[2]);
2240         planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2241                  srcSliceH >> 2, srcStride[2], dstStride[1]);
2242     }
2243     if (dst[3])
2244         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2245     return srcSliceH;
2246 }
2247
2248 /* unscaled copy like stuff (assumes nearly identical formats) */
2249 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2250                       int srcSliceH, uint8_t* dst[], int dstStride[])
2251 {
2252     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2253         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2254     else
2255     {
2256         int i;
2257         uint8_t *srcPtr= src[0];
2258         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2259         int length=0;
2260
2261         /* universal length finder */
2262         while(length+c->srcW <= FFABS(dstStride[0])
2263            && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2264         assert(length!=0);
2265
2266         for (i=0; i<srcSliceH; i++)
2267         {
2268             memcpy(dstPtr, srcPtr, length);
2269             srcPtr+= srcStride[0];
2270             dstPtr+= dstStride[0];
2271         }
2272     }
2273     return srcSliceH;
2274 }
2275
2276 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2277                       int srcSliceH, uint8_t* dst[], int dstStride[])
2278 {
2279     int plane, i, j;
2280     for (plane=0; plane<4; plane++)
2281     {
2282         int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2283         int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2284         int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2285         uint8_t *srcPtr= src[plane];
2286         uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2287
2288         if (!dst[plane]) continue;
2289         // ignore palette for GRAY8
2290         if (plane == 1 && !dst[2]) continue;
2291         if (!src[plane] || (plane == 1 && !src[2])){
2292             if(is16BPS(c->dstFormat))
2293                 length*=2;
2294             fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2295         }else
2296         {
2297             if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2298                 if (!isBE(c->srcFormat)) srcPtr++;
2299                 for (i=0; i<height; i++){
2300                     for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2301                     srcPtr+= srcStride[plane];
2302                     dstPtr+= dstStride[plane];
2303                 }
2304             }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2305                 for (i=0; i<height; i++){
2306                     for (j=0; j<length; j++){
2307                         dstPtr[ j<<1   ] = srcPtr[j];
2308                         dstPtr[(j<<1)+1] = srcPtr[j];
2309                     }
2310                     srcPtr+= srcStride[plane];
2311                     dstPtr+= dstStride[plane];
2312                 }
2313             }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2314                   && isBE(c->srcFormat) != isBE(c->dstFormat)){
2315
2316                 for (i=0; i<height; i++){
2317                     for (j=0; j<length; j++)
2318                         ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2319                     srcPtr+= srcStride[plane];
2320                     dstPtr+= dstStride[plane];
2321                 }
2322             } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2323                 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2324             else
2325             {
2326                 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2327                     length*=2;
2328                 for (i=0; i<height; i++)
2329                 {
2330                     memcpy(dstPtr, srcPtr, length);
2331                     srcPtr+= srcStride[plane];
2332                     dstPtr+= dstStride[plane];
2333                 }
2334             }
2335         }
2336     }
2337     return srcSliceH;
2338 }
2339
2340
2341 static void getSubSampleFactors(int *h, int *v, int format){
2342     switch(format){
2343     case PIX_FMT_UYVY422:
2344     case PIX_FMT_YUYV422:
2345         *h=1;
2346         *v=0;
2347         break;
2348     case PIX_FMT_YUV420P:
2349     case PIX_FMT_YUV420PLE:
2350     case PIX_FMT_YUV420PBE:
2351     case PIX_FMT_YUVA420P:
2352     case PIX_FMT_GRAY16BE:
2353     case PIX_FMT_GRAY16LE:
2354     case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2355     case PIX_FMT_NV12:
2356     case PIX_FMT_NV21:
2357         *h=1;
2358         *v=1;
2359         break;
2360     case PIX_FMT_YUV440P:
2361         *h=0;
2362         *v=1;
2363         break;
2364     case PIX_FMT_YUV410P:
2365         *h=2;
2366         *v=2;
2367         break;
2368     case PIX_FMT_YUV444P:
2369     case PIX_FMT_YUV444PLE:
2370     case PIX_FMT_YUV444PBE:
2371         *h=0;
2372         *v=0;
2373         break;
2374     case PIX_FMT_YUV422P:
2375     case PIX_FMT_YUV422PLE:
2376     case PIX_FMT_YUV422PBE:
2377         *h=1;
2378         *v=0;
2379         break;
2380     case PIX_FMT_YUV411P:
2381         *h=2;
2382         *v=0;
2383         break;
2384     default:
2385         *h=0;
2386         *v=0;
2387         break;
2388     }
2389 }
2390
2391 static uint16_t roundToInt16(int64_t f){
2392     int r= (f + (1<<15))>>16;
2393          if (r<-0x7FFF) return 0x8000;
2394     else if (r> 0x7FFF) return 0x7FFF;
2395     else                return r;
2396 }
2397
2398 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2399     int64_t crv =  inv_table[0];
2400     int64_t cbu =  inv_table[1];
2401     int64_t cgu = -inv_table[2];
2402     int64_t cgv = -inv_table[3];
2403     int64_t cy  = 1<<16;
2404     int64_t oy  = 0;
2405
2406     memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2407     memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2408
2409     c->brightness= brightness;
2410     c->contrast  = contrast;
2411     c->saturation= saturation;
2412     c->srcRange  = srcRange;
2413     c->dstRange  = dstRange;
2414     if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2415
2416     c->uOffset=   0x0400040004000400LL;
2417     c->vOffset=   0x0400040004000400LL;
2418
2419     if (!srcRange){
2420         cy= (cy*255) / 219;
2421         oy= 16<<16;
2422     }else{
2423         crv= (crv*224) / 255;
2424         cbu= (cbu*224) / 255;
2425         cgu= (cgu*224) / 255;
2426         cgv= (cgv*224) / 255;
2427     }
2428
2429     cy = (cy *contrast             )>>16;
2430     crv= (crv*contrast * saturation)>>32;
2431     cbu= (cbu*contrast * saturation)>>32;
2432     cgu= (cgu*contrast * saturation)>>32;
2433     cgv= (cgv*contrast * saturation)>>32;
2434
2435     oy -= 256*brightness;
2436
2437     c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2438     c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2439     c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2440     c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2441     c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2442     c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2443
2444     c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2445     c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2446     c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2447     c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2448     c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2449     c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2450
2451     ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2452     //FIXME factorize
2453
2454 #ifdef COMPILE_ALTIVEC
2455     if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2456         ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2457 #endif
2458     return 0;
2459 }
2460
2461 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2462     if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2463
2464     *inv_table = c->srcColorspaceTable;
2465     *table     = c->dstColorspaceTable;
2466     *srcRange  = c->srcRange;
2467     *dstRange  = c->dstRange;
2468     *brightness= c->brightness;
2469     *contrast  = c->contrast;
2470     *saturation= c->saturation;
2471
2472     return 0;
2473 }
2474
2475 static int handle_jpeg(enum PixelFormat *format)
2476 {
2477     switch (*format) {
2478         case PIX_FMT_YUVJ420P:
2479             *format = PIX_FMT_YUV420P;
2480             return 1;
2481         case PIX_FMT_YUVJ422P:
2482             *format = PIX_FMT_YUV422P;
2483             return 1;
2484         case PIX_FMT_YUVJ444P:
2485             *format = PIX_FMT_YUV444P;
2486             return 1;
2487         case PIX_FMT_YUVJ440P:
2488             *format = PIX_FMT_YUV440P;
2489             return 1;
2490         default:
2491             return 0;
2492     }
2493 }
2494
2495 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2496                            SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2497 {
2498
2499     SwsContext *c;
2500     int i;
2501     int usesVFilter, usesHFilter;
2502     int unscaled, needsDither;
2503     int srcRange, dstRange;
2504     SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2505 #if ARCH_X86
2506     if (flags & SWS_CPU_CAPS_MMX)
2507         __asm__ volatile("emms\n\t"::: "memory");
2508 #endif
2509
2510 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2511     flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2512 #if   COMPILE_TEMPLATE_MMX2
2513     flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2514 #elif COMPILE_TEMPLATE_AMD3DNOW
2515     flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2516 #elif COMPILE_TEMPLATE_MMX
2517     flags |= SWS_CPU_CAPS_MMX;
2518 #elif COMPILE_TEMPLATE_ALTIVEC
2519     flags |= SWS_CPU_CAPS_ALTIVEC;
2520 #elif ARCH_BFIN
2521     flags |= SWS_CPU_CAPS_BFIN;
2522 #endif
2523 #endif /* CONFIG_RUNTIME_CPUDETECT */
2524     if (clip_table[512] != 255) globalInit();
2525     if (!rgb15to16) sws_rgb2rgb_init(flags);
2526
2527     unscaled = (srcW == dstW && srcH == dstH);
2528     needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2529         && (fmt_depth(dstFormat))<24
2530         && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2531
2532     srcRange = handle_jpeg(&srcFormat);
2533     dstRange = handle_jpeg(&dstFormat);
2534
2535     if (!isSupportedIn(srcFormat))
2536     {
2537         av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2538         return NULL;
2539     }
2540     if (!isSupportedOut(dstFormat))
2541     {
2542         av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2543         return NULL;
2544     }
2545
2546     i= flags & ( SWS_POINT
2547                 |SWS_AREA
2548                 |SWS_BILINEAR
2549                 |SWS_FAST_BILINEAR
2550                 |SWS_BICUBIC
2551                 |SWS_X
2552                 |SWS_GAUSS
2553                 |SWS_LANCZOS
2554                 |SWS_SINC
2555                 |SWS_SPLINE
2556                 |SWS_BICUBLIN);
2557     if(!i || (i & (i-1)))
2558     {
2559         av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2560         return NULL;
2561     }
2562
2563     /* sanity check */
2564     if (srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
2565     {
2566         av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2567                srcW, srcH, dstW, dstH);
2568         return NULL;
2569     }
2570     if(srcW > VOFW || dstW > VOFW){
2571         av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2572         return NULL;
2573     }
2574
2575     if (!dstFilter) dstFilter= &dummyFilter;
2576     if (!srcFilter) srcFilter= &dummyFilter;
2577
2578     c= av_mallocz(sizeof(SwsContext));
2579
2580     c->av_class = &sws_context_class;
2581     c->srcW= srcW;
2582     c->srcH= srcH;
2583     c->dstW= dstW;
2584     c->dstH= dstH;
2585     c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2586     c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2587     c->flags= flags;
2588     c->dstFormat= dstFormat;
2589     c->srcFormat= srcFormat;
2590     c->vRounder= 4* 0x0001000100010001ULL;
2591
2592     usesHFilter= usesVFilter= 0;
2593     if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2594     if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2595     if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2596     if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2597     if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2598     if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2599     if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2600     if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2601
2602     getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2603     getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2604
2605     // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2606     if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2607
2608     // drop some chroma lines if the user wants it
2609     c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2610     c->chrSrcVSubSample+= c->vChrDrop;
2611
2612     // drop every other pixel for chroma calculation unless user wants full chroma
2613     if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2614       && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2615       && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2616       && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2617       && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2618         c->chrSrcHSubSample=1;
2619
2620     if (param){
2621         c->param[0] = param[0];
2622         c->param[1] = param[1];
2623     }else{
2624         c->param[0] =
2625         c->param[1] = SWS_PARAM_DEFAULT;
2626     }
2627
2628     // Note the -((-x)>>y) is so that we always round toward +inf.
2629     c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2630     c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2631     c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2632     c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2633
2634     sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2635
2636     /* unscaled special cases */
2637     if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2638     {
2639         /* yv12_to_nv12 */
2640         if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2641         {
2642             c->swScale= PlanarToNV12Wrapper;
2643         }
2644         /* yuv2bgr */
2645         if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2646             && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2647         {
2648             c->swScale= ff_yuv2rgb_get_func_ptr(c);
2649         }
2650
2651         if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2652         {
2653             c->swScale= yvu9toyv12Wrapper;
2654         }
2655
2656         /* bgr24toYV12 */
2657         if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2658             c->swScale= bgr24toyv12Wrapper;
2659
2660         /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2661         if (  (isBGR(srcFormat) || isRGB(srcFormat))
2662            && (isBGR(dstFormat) || isRGB(dstFormat))
2663            && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2664            && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2665            && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2666            && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2667            && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2668            && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2669            && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2670            && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2671                                              && dstFormat != PIX_FMT_RGB32_1
2672                                              && dstFormat != PIX_FMT_BGR32_1
2673            && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
2674            && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
2675            && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2676              c->swScale= rgb2rgbWrapper;
2677
2678         if ((usePal(srcFormat) && (
2679                  dstFormat == PIX_FMT_RGB32   ||
2680                  dstFormat == PIX_FMT_RGB32_1 ||
2681                  dstFormat == PIX_FMT_RGB24   ||
2682                  dstFormat == PIX_FMT_BGR32   ||
2683                  dstFormat == PIX_FMT_BGR32_1 ||
2684                  dstFormat == PIX_FMT_BGR24)))
2685              c->swScale= pal2rgbWrapper;
2686
2687         if (srcFormat == PIX_FMT_YUV422P)
2688         {
2689             if (dstFormat == PIX_FMT_YUYV422)
2690                 c->swScale= YUV422PToYuy2Wrapper;
2691             else if (dstFormat == PIX_FMT_UYVY422)
2692                 c->swScale= YUV422PToUyvyWrapper;
2693         }
2694
2695         /* LQ converters if -sws 0 or -sws 4*/
2696         if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2697             /* yv12_to_yuy2 */
2698             if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2699             {
2700                 if (dstFormat == PIX_FMT_YUYV422)
2701                     c->swScale= PlanarToYuy2Wrapper;
2702                 else if (dstFormat == PIX_FMT_UYVY422)
2703                     c->swScale= PlanarToUyvyWrapper;
2704             }
2705         }
2706         if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2707             c->swScale= YUYV2YUV420Wrapper;
2708         if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2709             c->swScale= UYVY2YUV420Wrapper;
2710         if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2711             c->swScale= YUYV2YUV422Wrapper;
2712         if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2713             c->swScale= UYVY2YUV422Wrapper;
2714
2715 #ifdef COMPILE_ALTIVEC
2716         if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2717             !(c->flags & SWS_BITEXACT) &&
2718             srcFormat == PIX_FMT_YUV420P) {
2719           // unscaled YV12 -> packed YUV, we want speed
2720           if (dstFormat == PIX_FMT_YUYV422)
2721               c->swScale= yv12toyuy2_unscaled_altivec;
2722           else if (dstFormat == PIX_FMT_UYVY422)
2723               c->swScale= yv12touyvy_unscaled_altivec;
2724         }
2725 #endif
2726
2727         /* simple copy */
2728         if (  srcFormat == dstFormat
2729             || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2730             || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2731             || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2732             || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2733             || (isGray(dstFormat) && isGray(srcFormat))
2734             || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2735                 && c->chrDstHSubSample == c->chrSrcHSubSample
2736                 && c->chrDstVSubSample == c->chrSrcVSubSample
2737                 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2738                 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2739         {
2740             if (isPacked(c->srcFormat))
2741                 c->swScale= packedCopy;
2742             else /* Planar YUV or gray */
2743                 c->swScale= planarCopy;
2744         }
2745 #if ARCH_BFIN
2746         if (flags & SWS_CPU_CAPS_BFIN)
2747             ff_bfin_get_unscaled_swscale (c);
2748 #endif
2749
2750         if (c->swScale){
2751             if (flags&SWS_PRINT_INFO)
2752                 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2753                                 sws_format_name(srcFormat), sws_format_name(dstFormat));
2754             return c;
2755         }
2756     }
2757
2758     if (flags & SWS_CPU_CAPS_MMX2)
2759     {
2760         c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2761         if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2762         {
2763             if (flags&SWS_PRINT_INFO)
2764                 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2765         }
2766         if (usesHFilter) c->canMMX2BeUsed=0;
2767     }
2768     else
2769         c->canMMX2BeUsed=0;
2770
2771     c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2772     c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2773
2774     // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2775     // but only for the FAST_BILINEAR mode otherwise do correct scaling
2776     // n-2 is the last chrominance sample available
2777     // this is not perfect, but no one should notice the difference, the more correct variant
2778     // would be like the vertical one, but that would require some special code for the
2779     // first and last pixel
2780     if (flags&SWS_FAST_BILINEAR)
2781     {
2782         if (c->canMMX2BeUsed)
2783         {
2784             c->lumXInc+= 20;
2785             c->chrXInc+= 20;
2786         }
2787         //we don't use the x86 asm scaler if MMX is available
2788         else if (flags & SWS_CPU_CAPS_MMX)
2789         {
2790             c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2791             c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2792         }
2793     }
2794
2795     /* precalculate horizontal scaler filter coefficients */
2796     {
2797         const int filterAlign=
2798             (flags & SWS_CPU_CAPS_MMX) ? 4 :
2799             (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2800             1;
2801
2802         initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2803                    srcW      ,       dstW, filterAlign, 1<<14,
2804                    (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2805                    srcFilter->lumH, dstFilter->lumH, c->param);
2806         initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2807                    c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2808                    (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2809                    srcFilter->chrH, dstFilter->chrH, c->param);
2810
2811 #define MAX_MMX2_FILTER_CODE_SIZE 10000
2812 #if defined(COMPILE_MMX2)
2813 // can't downscale !!!
2814         if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2815         {
2816 #ifdef MAP_ANONYMOUS
2817             c->lumMmx2FilterCode = mmap(NULL, MAX_MMX2_FILTER_CODE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2818             c->chrMmx2FilterCode = mmap(NULL, MAX_MMX2_FILTER_CODE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2819 #elif HAVE_VIRTUALALLOC
2820             c->lumMmx2FilterCode = VirtualAlloc(NULL, MAX_MMX2_FILTER_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2821             c->chrMmx2FilterCode = VirtualAlloc(NULL, MAX_MMX2_FILTER_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2822 #else
2823             c->lumMmx2FilterCode = av_malloc(MAX_MMX2_FILTER_CODE_SIZE);
2824             c->chrMmx2FilterCode = av_malloc(MAX_MMX2_FILTER_CODE_SIZE);
2825 #endif
2826
2827             c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2828             c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2829             c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2830             c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2831
2832             initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2833             initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2834
2835 #ifdef MAP_ANONYMOUS
2836             mprotect(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, PROT_EXEC | PROT_READ);
2837             mprotect(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, PROT_EXEC | PROT_READ);
2838 #endif
2839         }
2840 #endif /* defined(COMPILE_MMX2) */
2841     } // initialize horizontal stuff
2842
2843
2844
2845     /* precalculate vertical scaler filter coefficients */
2846     {
2847         const int filterAlign=
2848             (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2849             (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2850             1;
2851
2852         initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2853                    srcH      ,        dstH, filterAlign, (1<<12),
2854                    (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2855                    srcFilter->lumV, dstFilter->lumV, c->param);
2856         initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2857                    c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2858                    (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2859                    srcFilter->chrV, dstFilter->chrV, c->param);
2860
2861 #ifdef COMPILE_ALTIVEC
2862         c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2863         c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2864
2865         for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2866             int j;
2867             short *p = (short *)&c->vYCoeffsBank[i];
2868             for (j=0;j<8;j++)
2869                 p[j] = c->vLumFilter[i];
2870         }
2871
2872         for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2873             int j;
2874             short *p = (short *)&c->vCCoeffsBank[i];
2875             for (j=0;j<8;j++)
2876                 p[j] = c->vChrFilter[i];
2877         }
2878 #endif
2879     }
2880
2881     // calculate buffer sizes so that they won't run out while handling these damn slices
2882     c->vLumBufSize= c->vLumFilterSize;
2883     c->vChrBufSize= c->vChrFilterSize;
2884     for (i=0; i<dstH; i++)
2885     {
2886         int chrI= i*c->chrDstH / dstH;
2887         int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2888                            ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2889
2890         nextSlice>>= c->chrSrcVSubSample;
2891         nextSlice<<= c->chrSrcVSubSample;
2892         if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2893             c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2894         if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2895             c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2896     }
2897
2898     // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2899     c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2900     c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2901     if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2902         c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2903     //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
2904     /* align at 16 bytes for AltiVec */
2905     for (i=0; i<c->vLumBufSize; i++)
2906         c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2907     for (i=0; i<c->vChrBufSize; i++)
2908         c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2909     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2910         for (i=0; i<c->vLumBufSize; i++)
2911             c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2912
2913     //try to avoid drawing green stuff between the right end and the stride end
2914     for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2915
2916     assert(2*VOFW == VOF);
2917
2918     assert(c->chrDstH <= dstH);
2919
2920     if (flags&SWS_PRINT_INFO)
2921     {
2922 #ifdef DITHER1XBPP
2923         const char *dither= " dithered";
2924 #else
2925         const char *dither= "";
2926 #endif
2927         if (flags&SWS_FAST_BILINEAR)
2928             av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2929         else if (flags&SWS_BILINEAR)
2930             av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2931         else if (flags&SWS_BICUBIC)
2932             av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2933         else if (flags&SWS_X)
2934             av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2935         else if (flags&SWS_POINT)
2936             av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2937         else if (flags&SWS_AREA)
2938             av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2939         else if (flags&SWS_BICUBLIN)
2940             av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2941         else if (flags&SWS_GAUSS)
2942             av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2943         else if (flags&SWS_SINC)
2944             av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2945         else if (flags&SWS_LANCZOS)
2946             av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2947         else if (flags&SWS_SPLINE)
2948             av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2949         else
2950             av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2951
2952         if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2953             av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2954                    sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2955         else
2956             av_log(c, AV_LOG_INFO, "from %s to %s ",
2957                    sws_format_name(srcFormat), sws_format_name(dstFormat));
2958
2959         if (flags & SWS_CPU_CAPS_MMX2)
2960             av_log(c, AV_LOG_INFO, "using MMX2\n");
2961         else if (flags & SWS_CPU_CAPS_3DNOW)
2962             av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2963         else if (flags & SWS_CPU_CAPS_MMX)
2964             av_log(c, AV_LOG_INFO, "using MMX\n");
2965         else if (flags & SWS_CPU_CAPS_ALTIVEC)
2966             av_log(c, AV_LOG_INFO, "using AltiVec\n");
2967         else
2968             av_log(c, AV_LOG_INFO, "using C\n");
2969     }
2970
2971     if (flags & SWS_PRINT_INFO)
2972     {
2973         if (flags & SWS_CPU_CAPS_MMX)
2974         {
2975             if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2976                 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2977             else
2978             {
2979                 if (c->hLumFilterSize==4)
2980                     av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2981                 else if (c->hLumFilterSize==8)
2982                     av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2983                 else
2984                     av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2985
2986                 if (c->hChrFilterSize==4)
2987                     av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2988                 else if (c->hChrFilterSize==8)
2989                     av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2990                 else
2991                     av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2992             }
2993         }
2994         else
2995         {
2996 #if ARCH_X86
2997             av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2998 #else
2999             if (flags & SWS_FAST_BILINEAR)
3000                 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3001             else
3002                 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3003 #endif
3004         }
3005         if (isPlanarYUV(dstFormat))
3006         {
3007             if (c->vLumFilterSize==1)
3008                 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3009             else
3010                 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3011         }
3012         else
3013         {
3014             if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3015                 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3016                        "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3017             else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3018                 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3019             else
3020                 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3021         }
3022
3023         if (dstFormat==PIX_FMT_BGR24)
3024             av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3025                    (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3026         else if (dstFormat==PIX_FMT_RGB32)
3027             av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3028         else if (dstFormat==PIX_FMT_BGR565)
3029             av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3030         else if (dstFormat==PIX_FMT_BGR555)
3031             av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3032
3033         av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3034     }
3035     if (flags & SWS_PRINT_INFO)
3036     {
3037         av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3038                c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3039         av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3040                c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3041     }
3042
3043     c->swScale= getSwsFunc(c);
3044     return c;
3045 }
3046
3047 static void reset_ptr(uint8_t* src[], int format){
3048     if(!isALPHA(format))
3049         src[3]=NULL;
3050     if(!isPlanarYUV(format)){
3051         src[3]=src[2]=NULL;
3052         if(   format != PIX_FMT_PAL8
3053            && format != PIX_FMT_RGB8
3054            && format != PIX_FMT_BGR8
3055            && format != PIX_FMT_RGB4_BYTE
3056            && format != PIX_FMT_BGR4_BYTE
3057           )
3058             src[1]= NULL;
3059     }
3060 }
3061
3062 /**
3063  * swscale wrapper, so we don't need to export the SwsContext.
3064  * Assumes planar YUV to be in YUV order instead of YVU.
3065  */
3066 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3067               int srcSliceH, uint8_t* dst[], int dstStride[]){
3068     int i;
3069     uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3070     uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3071
3072     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3073         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3074         return 0;
3075     }
3076     if (c->sliceDir == 0) {
3077         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3078     }
3079
3080     if (usePal(c->srcFormat)){
3081         for (i=0; i<256; i++){
3082             int p, r, g, b,y,u,v;
3083             if(c->srcFormat == PIX_FMT_PAL8){
3084                 p=((uint32_t*)(src[1]))[i];
3085                 r= (p>>16)&0xFF;
3086                 g= (p>> 8)&0xFF;
3087                 b=  p     &0xFF;
3088             }else if(c->srcFormat == PIX_FMT_RGB8){
3089                 r= (i>>5    )*36;
3090                 g= ((i>>2)&7)*36;
3091                 b= (i&3     )*85;
3092             }else if(c->srcFormat == PIX_FMT_BGR8){
3093                 b= (i>>6    )*85;
3094                 g= ((i>>3)&7)*36;
3095                 r= (i&7     )*36;
3096             }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3097                 r= (i>>3    )*255;
3098                 g= ((i>>1)&3)*85;
3099                 b= (i&1     )*255;
3100             }else {
3101                 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3102                 b= (i>>3    )*255;
3103                 g= ((i>>1)&3)*85;
3104                 r= (i&1     )*255;
3105             }
3106             y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3107             u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3108             v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3109             c->pal_yuv[i]= y + (u<<8) + (v<<16);
3110
3111
3112             switch(c->dstFormat) {
3113             case PIX_FMT_BGR32:
3114 #if !HAVE_BIGENDIAN
3115             case PIX_FMT_RGB24:
3116 #endif
3117                 c->pal_rgb[i]=  r + (g<<8) + (b<<16);
3118                 break;
3119             case PIX_FMT_BGR32_1:
3120 #if HAVE_BIGENDIAN
3121             case PIX_FMT_BGR24:
3122 #endif
3123                 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3124                 break;
3125             case PIX_FMT_RGB32_1:
3126 #if HAVE_BIGENDIAN
3127             case PIX_FMT_RGB24:
3128 #endif
3129                 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3130                 break;
3131             case PIX_FMT_RGB32:
3132 #if !HAVE_BIGENDIAN
3133             case PIX_FMT_BGR24:
3134 #endif
3135             default:
3136                 c->pal_rgb[i]=  b + (g<<8) + (r<<16);
3137             }
3138         }
3139     }
3140
3141     // copy strides, so they can safely be modified
3142     if (c->sliceDir == 1) {
3143         // slices go from top to bottom
3144         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3145         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3146
3147         reset_ptr(src2, c->srcFormat);
3148         reset_ptr(dst2, c->dstFormat);
3149
3150         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3151     } else {
3152         // slices go from bottom to top => we flip the image internally
3153         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3154         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3155
3156         src2[0] += (srcSliceH-1)*srcStride[0];
3157         if (!usePal(c->srcFormat))
3158             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3159         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3160         src2[3] += (srcSliceH-1)*srcStride[3];
3161         dst2[0] += ( c->dstH                      -1)*dstStride[0];
3162         dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3163         dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3164         dst2[3] += ( c->dstH                      -1)*dstStride[3];
3165
3166         reset_ptr(src2, c->srcFormat);
3167         reset_ptr(dst2, c->dstFormat);
3168
3169         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3170     }
3171 }
3172
3173 #if LIBSWSCALE_VERSION_MAJOR < 1
3174 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3175                       int srcSliceH, uint8_t* dst[], int dstStride[]){
3176     return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3177 }
3178 #endif
3179
3180 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3181                                 float lumaSharpen, float chromaSharpen,
3182                                 float chromaHShift, float chromaVShift,
3183                                 int verbose)
3184 {
3185     SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3186
3187     if (lumaGBlur!=0.0){
3188         filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3189         filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3190     }else{
3191         filter->lumH= sws_getIdentityVec();
3192         filter->lumV= sws_getIdentityVec();
3193     }
3194
3195     if (chromaGBlur!=0.0){
3196         filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3197         filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3198     }else{
3199         filter->chrH= sws_getIdentityVec();
3200         filter->chrV= sws_getIdentityVec();
3201     }
3202
3203     if (chromaSharpen!=0.0){
3204         SwsVector *id= sws_getIdentityVec();
3205         sws_scaleVec(filter->chrH, -chromaSharpen);
3206         sws_scaleVec(filter->chrV, -chromaSharpen);
3207         sws_addVec(filter->chrH, id);
3208         sws_addVec(filter->chrV, id);
3209         sws_freeVec(id);
3210     }
3211
3212     if (lumaSharpen!=0.0){
3213         SwsVector *id= sws_getIdentityVec();
3214         sws_scaleVec(filter->lumH, -lumaSharpen);
3215         sws_scaleVec(filter->lumV, -lumaSharpen);
3216         sws_addVec(filter->lumH, id);
3217         sws_addVec(filter->lumV, id);
3218         sws_freeVec(id);
3219     }
3220
3221     if (chromaHShift != 0.0)
3222         sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3223
3224     if (chromaVShift != 0.0)
3225         sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3226
3227     sws_normalizeVec(filter->chrH, 1.0);
3228     sws_normalizeVec(filter->chrV, 1.0);
3229     sws_normalizeVec(filter->lumH, 1.0);
3230     sws_normalizeVec(filter->lumV, 1.0);
3231
3232     if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3233     if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3234
3235     return filter;
3236 }
3237
3238 SwsVector *sws_getGaussianVec(double variance, double quality){
3239     const int length= (int)(variance*quality + 0.5) | 1;
3240     int i;
3241     double *coeff= av_malloc(length*sizeof(double));
3242     double middle= (length-1)*0.5;
3243     SwsVector *vec= av_malloc(sizeof(SwsVector));
3244
3245     vec->coeff= coeff;
3246     vec->length= length;
3247
3248     for (i=0; i<length; i++)
3249     {
3250         double dist= i-middle;
3251         coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3252     }
3253
3254     sws_normalizeVec(vec, 1.0);
3255
3256     return vec;
3257 }
3258
3259 SwsVector *sws_getConstVec(double c, int length){
3260     int i;
3261     double *coeff= av_malloc(length*sizeof(double));
3262     SwsVector *vec= av_malloc(sizeof(SwsVector));
3263
3264     vec->coeff= coeff;
3265     vec->length= length;
3266
3267     for (i=0; i<length; i++)
3268         coeff[i]= c;
3269
3270     return vec;
3271 }
3272
3273
3274 SwsVector *sws_getIdentityVec(void){
3275     return sws_getConstVec(1.0, 1);
3276 }
3277
3278 double sws_dcVec(SwsVector *a){
3279     int i;
3280     double sum=0;
3281
3282     for (i=0; i<a->length; i++)
3283         sum+= a->coeff[i];
3284
3285     return sum;
3286 }
3287
3288 void sws_scaleVec(SwsVector *a, double scalar){
3289     int i;
3290
3291     for (i=0; i<a->length; i++)
3292         a->coeff[i]*= scalar;
3293 }
3294
3295 void sws_normalizeVec(SwsVector *a, double height){
3296     sws_scaleVec(a, height/sws_dcVec(a));
3297 }
3298
3299 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3300     int length= a->length + b->length - 1;
3301     double *coeff= av_malloc(length*sizeof(double));
3302     int i, j;
3303     SwsVector *vec= av_malloc(sizeof(SwsVector));
3304
3305     vec->coeff= coeff;
3306     vec->length= length;
3307
3308     for (i=0; i<length; i++) coeff[i]= 0.0;
3309
3310     for (i=0; i<a->length; i++)
3311     {
3312         for (j=0; j<b->length; j++)
3313         {
3314             coeff[i+j]+= a->coeff[i]*b->coeff[j];
3315         }
3316     }
3317
3318     return vec;
3319 }
3320
3321 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3322     int length= FFMAX(a->length, b->length);
3323     double *coeff= av_malloc(length*sizeof(double));
3324     int i;
3325     SwsVector *vec= av_malloc(sizeof(SwsVector));
3326
3327     vec->coeff= coeff;
3328     vec->length= length;
3329
3330     for (i=0; i<length; i++) coeff[i]= 0.0;
3331
3332     for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3333     for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3334
3335     return vec;
3336 }
3337
3338 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3339     int length= FFMAX(a->length, b->length);
3340     double *coeff= av_malloc(length*sizeof(double));
3341     int i;
3342     SwsVector *vec= av_malloc(sizeof(SwsVector));
3343
3344     vec->coeff= coeff;
3345     vec->length= length;
3346
3347     for (i=0; i<length; i++) coeff[i]= 0.0;
3348
3349     for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3350     for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3351
3352     return vec;
3353 }
3354
3355 /* shift left / or right if "shift" is negative */
3356 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3357     int length= a->length + FFABS(shift)*2;
3358     double *coeff= av_malloc(length*sizeof(double));
3359     int i;
3360     SwsVector *vec= av_malloc(sizeof(SwsVector));
3361
3362     vec->coeff= coeff;
3363     vec->length= length;
3364
3365     for (i=0; i<length; i++) coeff[i]= 0.0;
3366
3367     for (i=0; i<a->length; i++)
3368     {
3369         coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3370     }
3371
3372     return vec;
3373 }
3374
3375 void sws_shiftVec(SwsVector *a, int shift){
3376     SwsVector *shifted= sws_getShiftedVec(a, shift);
3377     av_free(a->coeff);
3378     a->coeff= shifted->coeff;
3379     a->length= shifted->length;
3380     av_free(shifted);
3381 }
3382
3383 void sws_addVec(SwsVector *a, SwsVector *b){
3384     SwsVector *sum= sws_sumVec(a, b);
3385     av_free(a->coeff);
3386     a->coeff= sum->coeff;
3387     a->length= sum->length;
3388     av_free(sum);
3389 }
3390
3391 void sws_subVec(SwsVector *a, SwsVector *b){
3392     SwsVector *diff= sws_diffVec(a, b);
3393     av_free(a->coeff);
3394     a->coeff= diff->coeff;
3395     a->length= diff->length;
3396     av_free(diff);
3397 }
3398
3399 void sws_convVec(SwsVector *a, SwsVector *b){
3400     SwsVector *conv= sws_getConvVec(a, b);
3401     av_free(a->coeff);
3402     a->coeff= conv->coeff;
3403     a->length= conv->length;
3404     av_free(conv);
3405 }
3406
3407 SwsVector *sws_cloneVec(SwsVector *a){
3408     double *coeff= av_malloc(a->length*sizeof(double));
3409     int i;
3410     SwsVector *vec= av_malloc(sizeof(SwsVector));
3411
3412     vec->coeff= coeff;
3413     vec->length= a->length;
3414
3415     for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3416
3417     return vec;
3418 }
3419
3420 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3421     int i;
3422     double max=0;
3423     double min=0;
3424     double range;
3425
3426     for (i=0; i<a->length; i++)
3427         if (a->coeff[i]>max) max= a->coeff[i];
3428
3429     for (i=0; i<a->length; i++)
3430         if (a->coeff[i]<min) min= a->coeff[i];
3431
3432     range= max - min;
3433
3434     for (i=0; i<a->length; i++)
3435     {
3436         int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3437         av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3438         for (;x>0; x--) av_log(log_ctx, log_level, " ");
3439         av_log(log_ctx, log_level, "|\n");
3440     }
3441 }
3442
3443 #if LIBSWSCALE_VERSION_MAJOR < 1
3444 void sws_printVec(SwsVector *a){
3445     sws_printVec2(a, NULL, AV_LOG_DEBUG);
3446 }
3447 #endif
3448
3449 void sws_freeVec(SwsVector *a){
3450     if (!a) return;
3451     av_freep(&a->coeff);
3452     a->length=0;
3453     av_free(a);
3454 }
3455
3456 void sws_freeFilter(SwsFilter *filter){
3457     if (!filter) return;
3458
3459     if (filter->lumH) sws_freeVec(filter->lumH);
3460     if (filter->lumV) sws_freeVec(filter->lumV);
3461     if (filter->chrH) sws_freeVec(filter->chrH);
3462     if (filter->chrV) sws_freeVec(filter->chrV);
3463     av_free(filter);
3464 }
3465
3466
3467 void sws_freeContext(SwsContext *c){
3468     int i;
3469     if (!c) return;
3470
3471     if (c->lumPixBuf)
3472     {
3473         for (i=0; i<c->vLumBufSize; i++)
3474             av_freep(&c->lumPixBuf[i]);
3475         av_freep(&c->lumPixBuf);
3476     }
3477
3478     if (c->chrPixBuf)
3479     {
3480         for (i=0; i<c->vChrBufSize; i++)
3481             av_freep(&c->chrPixBuf[i]);
3482         av_freep(&c->chrPixBuf);
3483     }
3484
3485     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3486         for (i=0; i<c->vLumBufSize; i++)
3487             av_freep(&c->alpPixBuf[i]);
3488         av_freep(&c->alpPixBuf);
3489     }
3490
3491     av_freep(&c->vLumFilter);
3492     av_freep(&c->vChrFilter);
3493     av_freep(&c->hLumFilter);
3494     av_freep(&c->hChrFilter);
3495 #ifdef COMPILE_ALTIVEC
3496     av_freep(&c->vYCoeffsBank);
3497     av_freep(&c->vCCoeffsBank);
3498 #endif
3499
3500     av_freep(&c->vLumFilterPos);
3501     av_freep(&c->vChrFilterPos);
3502     av_freep(&c->hLumFilterPos);
3503     av_freep(&c->hChrFilterPos);
3504
3505 #if ARCH_X86 && CONFIG_GPL
3506 #ifdef MAP_ANONYMOUS
3507     if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE);
3508     if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE);
3509 #elif HAVE_VIRTUALALLOC
3510     if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, MEM_RELEASE);
3511     if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, MEM_RELEASE);
3512 #else
3513     av_free(c->lumMmx2FilterCode);
3514     av_free(c->chrMmx2FilterCode);
3515 #endif
3516     c->lumMmx2FilterCode=NULL;
3517     c->chrMmx2FilterCode=NULL;
3518 #endif /* ARCH_X86 && CONFIG_GPL */
3519
3520     av_freep(&c->lumMmx2Filter);
3521     av_freep(&c->chrMmx2Filter);
3522     av_freep(&c->lumMmx2FilterPos);
3523     av_freep(&c->chrMmx2FilterPos);
3524     av_freep(&c->yuvTable);
3525
3526     av_free(c);
3527 }
3528
3529 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3530                                         int srcW, int srcH, enum PixelFormat srcFormat,
3531                                         int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3532                                         SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3533 {
3534     static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3535
3536     if (!param)
3537         param = default_param;
3538
3539     if (context) {
3540         if (context->srcW != srcW || context->srcH != srcH ||
3541             context->srcFormat != srcFormat ||
3542             context->dstW != dstW || context->dstH != dstH ||
3543             context->dstFormat != dstFormat || context->flags != flags ||
3544             context->param[0] != param[0] || context->param[1] != param[1])
3545         {
3546             sws_freeContext(context);
3547             context = NULL;
3548         }
3549     }
3550     if (!context) {
3551         return sws_getContext(srcW, srcH, srcFormat,
3552                               dstW, dstH, dstFormat, flags,
3553                               srcFilter, dstFilter, param);
3554     }
3555     return context;
3556 }
3557