]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
Add support for options
[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 St, 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, BGR24, BGR16, BGR15, RGB32, 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 didnt 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 (its 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 #include <inttypes.h>
58 #include <string.h>
59 #include <math.h>
60 #include <stdio.h>
61 #include <unistd.h>
62 #include "config.h"
63 #include <assert.h>
64 #ifdef 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 #include "swscale.h"
71 #include "swscale_internal.h"
72 #include "x86_cpu.h"
73 #include "bswap.h"
74 #include "rgb2rgb.h"
75 #ifdef USE_FASTMEMCPY
76 #include "libvo/fastmemcpy.h"
77 #endif
78
79 #undef MOVNTQ
80 #undef PAVGB
81
82 //#undef HAVE_MMX2
83 //#define HAVE_3DNOW
84 //#undef HAVE_MMX
85 //#undef ARCH_X86
86 //#define WORDS_BIGENDIAN
87 #define DITHER1XBPP
88
89 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
90
91 #define RET 0xC3 //near return opcode for X86
92
93 #ifdef MP_DEBUG
94 #define ASSERT(x) assert(x);
95 #else
96 #define ASSERT(x) ;
97 #endif
98
99 #ifdef M_PI
100 #define PI M_PI
101 #else
102 #define PI 3.14159265358979323846
103 #endif
104
105 #define isSupportedIn(x)    (       \
106            (x)==PIX_FMT_YUV420P     \
107         || (x)==PIX_FMT_YUYV422     \
108         || (x)==PIX_FMT_UYVY422     \
109         || (x)==PIX_FMT_RGB32       \
110         || (x)==PIX_FMT_BGR24       \
111         || (x)==PIX_FMT_BGR565      \
112         || (x)==PIX_FMT_BGR555      \
113         || (x)==PIX_FMT_BGR32       \
114         || (x)==PIX_FMT_RGB24       \
115         || (x)==PIX_FMT_RGB565      \
116         || (x)==PIX_FMT_RGB555      \
117         || (x)==PIX_FMT_GRAY8       \
118         || (x)==PIX_FMT_YUV410P     \
119         || (x)==PIX_FMT_GRAY16BE    \
120         || (x)==PIX_FMT_GRAY16LE    \
121         || (x)==PIX_FMT_YUV444P     \
122         || (x)==PIX_FMT_YUV422P     \
123         || (x)==PIX_FMT_YUV411P     \
124         || (x)==PIX_FMT_PAL8        \
125         || (x)==PIX_FMT_BGR8        \
126         || (x)==PIX_FMT_RGB8        \
127         || (x)==PIX_FMT_BGR4_BYTE   \
128         || (x)==PIX_FMT_RGB4_BYTE   \
129     )
130 #define isSupportedOut(x)   (       \
131            (x)==PIX_FMT_YUV420P     \
132         || (x)==PIX_FMT_YUYV422     \
133         || (x)==PIX_FMT_UYVY422     \
134         || (x)==PIX_FMT_YUV444P     \
135         || (x)==PIX_FMT_YUV422P     \
136         || (x)==PIX_FMT_YUV411P     \
137         || isRGB(x)                 \
138         || isBGR(x)                 \
139         || (x)==PIX_FMT_NV12        \
140         || (x)==PIX_FMT_NV21        \
141         || (x)==PIX_FMT_GRAY16BE    \
142         || (x)==PIX_FMT_GRAY16LE    \
143         || (x)==PIX_FMT_GRAY8       \
144         || (x)==PIX_FMT_YUV410P     \
145     )
146 #define isPacked(x)         (       \
147            (x)==PIX_FMT_PAL8        \
148         || (x)==PIX_FMT_YUYV422     \
149         || (x)==PIX_FMT_UYVY422     \
150         || isRGB(x)                 \
151         || isBGR(x)                 \
152     )
153
154 #define RGB2YUV_SHIFT 16
155 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
156 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
157 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
158 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
159 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
160 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
161 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
162 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
163 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
164
165 extern const int32_t Inverse_Table_6_9[8][4];
166
167 /*
168 NOTES
169 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
170
171 TODO
172 more intelligent missalignment avoidance for the horizontal scaler
173 write special vertical cubic upscale version
174 Optimize C code (yv12 / minmax)
175 add support for packed pixel yuv input & output
176 add support for Y8 output
177 optimize bgr24 & bgr32
178 add BGR4 output support
179 write special BGR->BGR scaler
180 */
181
182 #if defined(ARCH_X86) && defined (CONFIG_GPL)
183 static uint64_t attribute_used __attribute__((aligned(8))) bF8=       0xF8F8F8F8F8F8F8F8LL;
184 static uint64_t attribute_used __attribute__((aligned(8))) bFC=       0xFCFCFCFCFCFCFCFCLL;
185 static uint64_t                __attribute__((aligned(8))) w10=       0x0010001000100010LL;
186 static uint64_t attribute_used __attribute__((aligned(8))) w02=       0x0002000200020002LL;
187 static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
188 static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
189 static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
190 static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
191
192 static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
193 static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
194 static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
195 static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
196
197 static uint64_t __attribute__((aligned(8))) dither4[2]={
198         0x0103010301030103LL,
199         0x0200020002000200LL,};
200
201 static uint64_t __attribute__((aligned(8))) dither8[2]={
202         0x0602060206020602LL,
203         0x0004000400040004LL,};
204
205 static uint64_t                __attribute__((aligned(8))) b16Mask=   0x001F001F001F001FLL;
206 static uint64_t attribute_used __attribute__((aligned(8))) g16Mask=   0x07E007E007E007E0LL;
207 static uint64_t attribute_used __attribute__((aligned(8))) r16Mask=   0xF800F800F800F800LL;
208 static uint64_t                __attribute__((aligned(8))) b15Mask=   0x001F001F001F001FLL;
209 static uint64_t attribute_used __attribute__((aligned(8))) g15Mask=   0x03E003E003E003E0LL;
210 static uint64_t attribute_used __attribute__((aligned(8))) r15Mask=   0x7C007C007C007C00LL;
211
212 static uint64_t attribute_used __attribute__((aligned(8))) M24A=      0x00FF0000FF0000FFLL;
213 static uint64_t attribute_used __attribute__((aligned(8))) M24B=      0xFF0000FF0000FF00LL;
214 static uint64_t attribute_used __attribute__((aligned(8))) M24C=      0x0000FF0000FF0000LL;
215
216 #ifdef FAST_BGR2YV12
217 static const uint64_t bgr2YCoeff   attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
218 static const uint64_t bgr2UCoeff   attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
219 static const uint64_t bgr2VCoeff   attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
220 #else
221 static const uint64_t bgr2YCoeff   attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
222 static const uint64_t bgr2UCoeff   attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
223 static const uint64_t bgr2VCoeff   attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
224 #endif /* FAST_BGR2YV12 */
225 static const uint64_t bgr2YOffset  attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
226 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8))) = 0x8080808080808080ULL;
227 static const uint64_t w1111        attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
228 #endif /* defined(ARCH_X86) */
229
230 // clipping helper table for C implementations:
231 static unsigned char clip_table[768];
232
233 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
234
235 extern const uint8_t dither_2x2_4[2][8];
236 extern const uint8_t dither_2x2_8[2][8];
237 extern const uint8_t dither_8x8_32[8][8];
238 extern const uint8_t dither_8x8_73[8][8];
239 extern const uint8_t dither_8x8_220[8][8];
240
241 static const char * sws_context_to_name(void * ptr) {
242     return "swscaler";
243 }
244
245 static AVClass sws_context_class = { "SWScaler", sws_context_to_name, NULL };
246
247 char *sws_format_name(enum PixelFormat format)
248 {
249     switch (format) {
250         case PIX_FMT_YUV420P:
251             return "yuv420p";
252         case PIX_FMT_YUYV422:
253             return "yuyv422";
254         case PIX_FMT_RGB24:
255             return "rgb24";
256         case PIX_FMT_BGR24:
257             return "bgr24";
258         case PIX_FMT_YUV422P:
259             return "yuv422p";
260         case PIX_FMT_YUV444P:
261             return "yuv444p";
262         case PIX_FMT_RGB32:
263             return "rgb32";
264         case PIX_FMT_YUV410P:
265             return "yuv410p";
266         case PIX_FMT_YUV411P:
267             return "yuv411p";
268         case PIX_FMT_RGB565:
269             return "rgb565";
270         case PIX_FMT_RGB555:
271             return "rgb555";
272         case PIX_FMT_GRAY16BE:
273             return "gray16be";
274         case PIX_FMT_GRAY16LE:
275             return "gray16le";
276         case PIX_FMT_GRAY8:
277             return "gray8";
278         case PIX_FMT_MONOWHITE:
279             return "mono white";
280         case PIX_FMT_MONOBLACK:
281             return "mono black";
282         case PIX_FMT_PAL8:
283             return "Palette";
284         case PIX_FMT_YUVJ420P:
285             return "yuvj420p";
286         case PIX_FMT_YUVJ422P:
287             return "yuvj422p";
288         case PIX_FMT_YUVJ444P:
289             return "yuvj444p";
290         case PIX_FMT_XVMC_MPEG2_MC:
291             return "xvmc_mpeg2_mc";
292         case PIX_FMT_XVMC_MPEG2_IDCT:
293             return "xvmc_mpeg2_idct";
294         case PIX_FMT_UYVY422:
295             return "uyvy422";
296         case PIX_FMT_UYYVYY411:
297             return "uyyvyy411";
298         case PIX_FMT_RGB32_1:
299             return "rgb32x";
300         case PIX_FMT_BGR32_1:
301             return "bgr32x";
302         case PIX_FMT_BGR32:
303             return "bgr32";
304         case PIX_FMT_BGR565:
305             return "bgr565";
306         case PIX_FMT_BGR555:
307             return "bgr555";
308         case PIX_FMT_BGR8:
309             return "bgr8";
310         case PIX_FMT_BGR4:
311             return "bgr4";
312         case PIX_FMT_BGR4_BYTE:
313             return "bgr4 byte";
314         case PIX_FMT_RGB8:
315             return "rgb8";
316         case PIX_FMT_RGB4:
317             return "rgb4";
318         case PIX_FMT_RGB4_BYTE:
319             return "rgb4 byte";
320         case PIX_FMT_NV12:
321             return "nv12";
322         case PIX_FMT_NV21:
323             return "nv21";
324         default:
325             return "Unknown format";
326     }
327 }
328
329 #if defined(ARCH_X86) && defined (CONFIG_GPL)
330 void in_asm_used_var_warning_killer()
331 {
332     volatile int i= bF8+bFC+w10+
333     bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
334     M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
335     if (i) i=0;
336 }
337 #endif
338
339 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
340                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
341                                uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
342 {
343     //FIXME Optimize (just quickly writen not opti..)
344     int i;
345     for (i=0; i<dstW; i++)
346     {
347         int val=1<<18;
348         int j;
349         for (j=0; j<lumFilterSize; j++)
350             val += lumSrc[j][i] * lumFilter[j];
351
352         dest[i]= av_clip_uint8(val>>19);
353     }
354
355     if (uDest != NULL)
356         for (i=0; i<chrDstW; i++)
357         {
358             int u=1<<18;
359             int v=1<<18;
360             int j;
361             for (j=0; j<chrFilterSize; j++)
362             {
363                 u += chrSrc[j][i] * chrFilter[j];
364                 v += chrSrc[j][i + 2048] * chrFilter[j];
365             }
366
367             uDest[i]= av_clip_uint8(u>>19);
368             vDest[i]= av_clip_uint8(v>>19);
369         }
370 }
371
372 static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
373                                 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
374                                 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
375 {
376     //FIXME Optimize (just quickly writen not opti..)
377     int i;
378     for (i=0; i<dstW; i++)
379     {
380         int val=1<<18;
381         int j;
382         for (j=0; j<lumFilterSize; j++)
383             val += lumSrc[j][i] * lumFilter[j];
384
385         dest[i]= av_clip_uint8(val>>19);
386     }
387
388     if (uDest == NULL)
389         return;
390
391     if (dstFormat == PIX_FMT_NV12)
392         for (i=0; i<chrDstW; i++)
393         {
394             int u=1<<18;
395             int v=1<<18;
396             int j;
397             for (j=0; j<chrFilterSize; j++)
398             {
399                 u += chrSrc[j][i] * chrFilter[j];
400                 v += chrSrc[j][i + 2048] * chrFilter[j];
401             }
402
403             uDest[2*i]= av_clip_uint8(u>>19);
404             uDest[2*i+1]= av_clip_uint8(v>>19);
405         }
406     else
407         for (i=0; i<chrDstW; i++)
408         {
409             int u=1<<18;
410             int v=1<<18;
411             int j;
412             for (j=0; j<chrFilterSize; j++)
413             {
414                 u += chrSrc[j][i] * chrFilter[j];
415                 v += chrSrc[j][i + 2048] * chrFilter[j];
416             }
417
418             uDest[2*i]= av_clip_uint8(v>>19);
419             uDest[2*i+1]= av_clip_uint8(u>>19);
420         }
421 }
422
423 #define YSCALE_YUV_2_PACKEDX_C(type) \
424     for (i=0; i<(dstW>>1); i++){\
425         int j;\
426         int Y1 = 1<<18;\
427         int Y2 = 1<<18;\
428         int U  = 1<<18;\
429         int V  = 1<<18;\
430         type attribute_unused *r, *b, *g;\
431         const int i2= 2*i;\
432         \
433         for (j=0; j<lumFilterSize; j++)\
434         {\
435             Y1 += lumSrc[j][i2] * lumFilter[j];\
436             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
437         }\
438         for (j=0; j<chrFilterSize; j++)\
439         {\
440             U += chrSrc[j][i] * chrFilter[j];\
441             V += chrSrc[j][i+2048] * chrFilter[j];\
442         }\
443         Y1>>=19;\
444         Y2>>=19;\
445         U >>=19;\
446         V >>=19;\
447         if ((Y1|Y2|U|V)&256)\
448         {\
449             if (Y1>255)   Y1=255; \
450             else if (Y1<0)Y1=0;   \
451             if (Y2>255)   Y2=255; \
452             else if (Y2<0)Y2=0;   \
453             if (U>255)    U=255;  \
454             else if (U<0) U=0;    \
455             if (V>255)    V=255;  \
456             else if (V<0) V=0;    \
457         }
458
459 #define YSCALE_YUV_2_RGBX_C(type) \
460     YSCALE_YUV_2_PACKEDX_C(type)  \
461     r = (type *)c->table_rV[V];   \
462     g = (type *)(c->table_gU[U] + c->table_gV[V]); \
463     b = (type *)c->table_bU[U];   \
464
465 #define YSCALE_YUV_2_PACKED2_C   \
466     for (i=0; i<(dstW>>1); i++){ \
467         const int i2= 2*i;       \
468         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
469         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
470         int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
471         int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;  \
472
473 #define YSCALE_YUV_2_RGB2_C(type) \
474     YSCALE_YUV_2_PACKED2_C\
475     type *r, *b, *g;\
476     r = (type *)c->table_rV[V];\
477     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
478     b = (type *)c->table_bU[U];\
479
480 #define YSCALE_YUV_2_PACKED1_C \
481     for (i=0; i<(dstW>>1); i++){\
482         const int i2= 2*i;\
483         int Y1= buf0[i2  ]>>7;\
484         int Y2= buf0[i2+1]>>7;\
485         int U= (uvbuf1[i     ])>>7;\
486         int V= (uvbuf1[i+2048])>>7;\
487
488 #define YSCALE_YUV_2_RGB1_C(type) \
489     YSCALE_YUV_2_PACKED1_C\
490     type *r, *b, *g;\
491     r = (type *)c->table_rV[V];\
492     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
493     b = (type *)c->table_bU[U];\
494
495 #define YSCALE_YUV_2_PACKED1B_C \
496     for (i=0; i<(dstW>>1); i++){\
497         const int i2= 2*i;\
498         int Y1= buf0[i2  ]>>7;\
499         int Y2= buf0[i2+1]>>7;\
500         int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
501         int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
502
503 #define YSCALE_YUV_2_RGB1B_C(type) \
504     YSCALE_YUV_2_PACKED1B_C\
505     type *r, *b, *g;\
506     r = (type *)c->table_rV[V];\
507     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
508     b = (type *)c->table_bU[U];\
509
510 #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
511     switch(c->dstFormat)\
512     {\
513     case PIX_FMT_RGB32:\
514     case PIX_FMT_BGR32:\
515         func(uint32_t)\
516             ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
517             ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
518         }                \
519         break;\
520     case PIX_FMT_RGB24:\
521         func(uint8_t)\
522             ((uint8_t*)dest)[0]= r[Y1];\
523             ((uint8_t*)dest)[1]= g[Y1];\
524             ((uint8_t*)dest)[2]= b[Y1];\
525             ((uint8_t*)dest)[3]= r[Y2];\
526             ((uint8_t*)dest)[4]= g[Y2];\
527             ((uint8_t*)dest)[5]= b[Y2];\
528             dest+=6;\
529         }\
530         break;\
531     case PIX_FMT_BGR24:\
532         func(uint8_t)\
533             ((uint8_t*)dest)[0]= b[Y1];\
534             ((uint8_t*)dest)[1]= g[Y1];\
535             ((uint8_t*)dest)[2]= r[Y1];\
536             ((uint8_t*)dest)[3]= b[Y2];\
537             ((uint8_t*)dest)[4]= g[Y2];\
538             ((uint8_t*)dest)[5]= r[Y2];\
539             dest+=6;\
540         }\
541         break;\
542     case PIX_FMT_RGB565:\
543     case PIX_FMT_BGR565:\
544         {\
545             const int dr1= dither_2x2_8[y&1    ][0];\
546             const int dg1= dither_2x2_4[y&1    ][0];\
547             const int db1= dither_2x2_8[(y&1)^1][0];\
548             const int dr2= dither_2x2_8[y&1    ][1];\
549             const int dg2= dither_2x2_4[y&1    ][1];\
550             const int db2= dither_2x2_8[(y&1)^1][1];\
551             func(uint16_t)\
552                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
553                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
554             }\
555         }\
556         break;\
557     case PIX_FMT_RGB555:\
558     case PIX_FMT_BGR555:\
559         {\
560             const int dr1= dither_2x2_8[y&1    ][0];\
561             const int dg1= dither_2x2_8[y&1    ][1];\
562             const int db1= dither_2x2_8[(y&1)^1][0];\
563             const int dr2= dither_2x2_8[y&1    ][1];\
564             const int dg2= dither_2x2_8[y&1    ][0];\
565             const int db2= dither_2x2_8[(y&1)^1][1];\
566             func(uint16_t)\
567                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
568                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
569             }\
570         }\
571         break;\
572     case PIX_FMT_RGB8:\
573     case PIX_FMT_BGR8:\
574         {\
575             const uint8_t * const d64= dither_8x8_73[y&7];\
576             const uint8_t * const d32= dither_8x8_32[y&7];\
577             func(uint8_t)\
578                 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
579                 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
580             }\
581         }\
582         break;\
583     case PIX_FMT_RGB4:\
584     case PIX_FMT_BGR4:\
585         {\
586             const uint8_t * const d64= dither_8x8_73 [y&7];\
587             const uint8_t * const d128=dither_8x8_220[y&7];\
588             func(uint8_t)\
589                 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
590                                  + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
591             }\
592         }\
593         break;\
594     case PIX_FMT_RGB4_BYTE:\
595     case PIX_FMT_BGR4_BYTE:\
596         {\
597             const uint8_t * const d64= dither_8x8_73 [y&7];\
598             const uint8_t * const d128=dither_8x8_220[y&7];\
599             func(uint8_t)\
600                 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
601                 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
602             }\
603         }\
604         break;\
605     case PIX_FMT_MONOBLACK:\
606         {\
607             const uint8_t * const d128=dither_8x8_220[y&7];\
608             uint8_t *g= c->table_gU[128] + c->table_gV[128];\
609             for (i=0; i<dstW-7; i+=8){\
610                 int acc;\
611                 acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
612                 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
613                 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
614                 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
615                 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
616                 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
617                 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
618                 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
619                 ((uint8_t*)dest)[0]= acc;\
620                 dest++;\
621             }\
622 \
623 /*\
624 ((uint8_t*)dest)-= dstW>>4;\
625 {\
626             int acc=0;\
627             int left=0;\
628             static int top[1024];\
629             static int last_new[1024][1024];\
630             static int last_in3[1024][1024];\
631             static int drift[1024][1024];\
632             int topLeft=0;\
633             int shift=0;\
634             int count=0;\
635             const uint8_t * const d128=dither_8x8_220[y&7];\
636             int error_new=0;\
637             int error_in3=0;\
638             int f=0;\
639             \
640             for (i=dstW>>1; i<dstW; i++){\
641                 int in= ((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19);\
642                 int in2 = (76309 * (in - 16) + 32768) >> 16;\
643                 int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
644                 int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
645                          + (last_new[y][i] - in3)*f/256;\
646                 int new= old> 128 ? 255 : 0;\
647 \
648                 error_new+= FFABS(last_new[y][i] - new);\
649                 error_in3+= FFABS(last_in3[y][i] - in3);\
650                 f= error_new - error_in3*4;\
651                 if (f<0) f=0;\
652                 if (f>256) f=256;\
653 \
654                 topLeft= top[i];\
655                 left= top[i]= old - new;\
656                 last_new[y][i]= new;\
657                 last_in3[y][i]= in3;\
658 \
659                 acc+= acc + (new&1);\
660                 if ((i&7)==6){\
661                     ((uint8_t*)dest)[0]= acc;\
662                     ((uint8_t*)dest)++;\
663                 }\
664             }\
665 }\
666 */\
667         }\
668         break;\
669     case PIX_FMT_YUYV422:\
670         func2\
671             ((uint8_t*)dest)[2*i2+0]= Y1;\
672             ((uint8_t*)dest)[2*i2+1]= U;\
673             ((uint8_t*)dest)[2*i2+2]= Y2;\
674             ((uint8_t*)dest)[2*i2+3]= V;\
675         }                \
676         break;\
677     case PIX_FMT_UYVY422:\
678         func2\
679             ((uint8_t*)dest)[2*i2+0]= U;\
680             ((uint8_t*)dest)[2*i2+1]= Y1;\
681             ((uint8_t*)dest)[2*i2+2]= V;\
682             ((uint8_t*)dest)[2*i2+3]= Y2;\
683         }                \
684         break;\
685     }\
686
687
688 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
689                                   int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
690                                   uint8_t *dest, int dstW, int y)
691 {
692     int i;
693     switch(c->dstFormat)
694     {
695     case PIX_FMT_BGR32:
696     case PIX_FMT_RGB32:
697         YSCALE_YUV_2_RGBX_C(uint32_t)
698             ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
699             ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
700         }
701         break;
702     case PIX_FMT_RGB24:
703         YSCALE_YUV_2_RGBX_C(uint8_t)
704             ((uint8_t*)dest)[0]= r[Y1];
705             ((uint8_t*)dest)[1]= g[Y1];
706             ((uint8_t*)dest)[2]= b[Y1];
707             ((uint8_t*)dest)[3]= r[Y2];
708             ((uint8_t*)dest)[4]= g[Y2];
709             ((uint8_t*)dest)[5]= b[Y2];
710             dest+=6;
711         }
712         break;
713     case PIX_FMT_BGR24:
714         YSCALE_YUV_2_RGBX_C(uint8_t)
715             ((uint8_t*)dest)[0]= b[Y1];
716             ((uint8_t*)dest)[1]= g[Y1];
717             ((uint8_t*)dest)[2]= r[Y1];
718             ((uint8_t*)dest)[3]= b[Y2];
719             ((uint8_t*)dest)[4]= g[Y2];
720             ((uint8_t*)dest)[5]= r[Y2];
721             dest+=6;
722         }
723         break;
724     case PIX_FMT_RGB565:
725     case PIX_FMT_BGR565:
726         {
727             const int dr1= dither_2x2_8[y&1    ][0];
728             const int dg1= dither_2x2_4[y&1    ][0];
729             const int db1= dither_2x2_8[(y&1)^1][0];
730             const int dr2= dither_2x2_8[y&1    ][1];
731             const int dg2= dither_2x2_4[y&1    ][1];
732             const int db2= dither_2x2_8[(y&1)^1][1];
733             YSCALE_YUV_2_RGBX_C(uint16_t)
734                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
735                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
736             }
737         }
738         break;
739     case PIX_FMT_RGB555:
740     case PIX_FMT_BGR555:
741         {
742             const int dr1= dither_2x2_8[y&1    ][0];
743             const int dg1= dither_2x2_8[y&1    ][1];
744             const int db1= dither_2x2_8[(y&1)^1][0];
745             const int dr2= dither_2x2_8[y&1    ][1];
746             const int dg2= dither_2x2_8[y&1    ][0];
747             const int db2= dither_2x2_8[(y&1)^1][1];
748             YSCALE_YUV_2_RGBX_C(uint16_t)
749                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
750                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
751             }
752         }
753         break;
754     case PIX_FMT_RGB8:
755     case PIX_FMT_BGR8:
756         {
757             const uint8_t * const d64= dither_8x8_73[y&7];
758             const uint8_t * const d32= dither_8x8_32[y&7];
759             YSCALE_YUV_2_RGBX_C(uint8_t)
760                 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
761                 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
762             }
763         }
764         break;
765     case PIX_FMT_RGB4:
766     case PIX_FMT_BGR4:
767         {
768             const uint8_t * const d64= dither_8x8_73 [y&7];
769             const uint8_t * const d128=dither_8x8_220[y&7];
770             YSCALE_YUV_2_RGBX_C(uint8_t)
771                 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
772                                   +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
773             }
774         }
775         break;
776     case PIX_FMT_RGB4_BYTE:
777     case PIX_FMT_BGR4_BYTE:
778         {
779             const uint8_t * const d64= dither_8x8_73 [y&7];
780             const uint8_t * const d128=dither_8x8_220[y&7];
781             YSCALE_YUV_2_RGBX_C(uint8_t)
782                 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
783                 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
784             }
785         }
786         break;
787     case PIX_FMT_MONOBLACK:
788         {
789             const uint8_t * const d128=dither_8x8_220[y&7];
790             uint8_t *g= c->table_gU[128] + c->table_gV[128];
791             int acc=0;
792             for (i=0; i<dstW-1; i+=2){
793                 int j;
794                 int Y1=1<<18;
795                 int Y2=1<<18;
796
797                 for (j=0; j<lumFilterSize; j++)
798                 {
799                     Y1 += lumSrc[j][i] * lumFilter[j];
800                     Y2 += lumSrc[j][i+1] * lumFilter[j];
801                 }
802                 Y1>>=19;
803                 Y2>>=19;
804                 if ((Y1|Y2)&256)
805                 {
806                     if (Y1>255)   Y1=255;
807                     else if (Y1<0)Y1=0;
808                     if (Y2>255)   Y2=255;
809                     else if (Y2<0)Y2=0;
810                 }
811                 acc+= acc + g[Y1+d128[(i+0)&7]];
812                 acc+= acc + g[Y2+d128[(i+1)&7]];
813                 if ((i&7)==6){
814                     ((uint8_t*)dest)[0]= acc;
815                     dest++;
816                 }
817             }
818         }
819         break;
820     case PIX_FMT_YUYV422:
821         YSCALE_YUV_2_PACKEDX_C(void)
822             ((uint8_t*)dest)[2*i2+0]= Y1;
823             ((uint8_t*)dest)[2*i2+1]= U;
824             ((uint8_t*)dest)[2*i2+2]= Y2;
825             ((uint8_t*)dest)[2*i2+3]= V;
826         }
827         break;
828     case PIX_FMT_UYVY422:
829         YSCALE_YUV_2_PACKEDX_C(void)
830             ((uint8_t*)dest)[2*i2+0]= U;
831             ((uint8_t*)dest)[2*i2+1]= Y1;
832             ((uint8_t*)dest)[2*i2+2]= V;
833             ((uint8_t*)dest)[2*i2+3]= Y2;
834         }
835         break;
836     }
837 }
838
839
840 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
841 //Plain C versions
842 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
843 #define COMPILE_C
844 #endif
845
846 #ifdef ARCH_POWERPC
847 #if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
848 #define COMPILE_ALTIVEC
849 #endif //HAVE_ALTIVEC
850 #endif //ARCH_POWERPC
851
852 #if defined(ARCH_X86)
853
854 #if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
855 #define COMPILE_MMX
856 #endif
857
858 #if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
859 #define COMPILE_MMX2
860 #endif
861
862 #if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
863 #define COMPILE_3DNOW
864 #endif
865 #endif //ARCH_X86 || ARCH_X86_64
866
867 #undef HAVE_MMX
868 #undef HAVE_MMX2
869 #undef HAVE_3DNOW
870
871 #ifdef COMPILE_C
872 #undef HAVE_MMX
873 #undef HAVE_MMX2
874 #undef HAVE_3DNOW
875 #undef HAVE_ALTIVEC
876 #define RENAME(a) a ## _C
877 #include "swscale_template.c"
878 #endif
879
880 #ifdef ARCH_POWERPC
881 #ifdef COMPILE_ALTIVEC
882 #undef RENAME
883 #define HAVE_ALTIVEC
884 #define RENAME(a) a ## _altivec
885 #include "swscale_template.c"
886 #endif
887 #endif //ARCH_POWERPC
888
889 #if defined(ARCH_X86)
890
891 //X86 versions
892 /*
893 #undef RENAME
894 #undef HAVE_MMX
895 #undef HAVE_MMX2
896 #undef HAVE_3DNOW
897 #define ARCH_X86
898 #define RENAME(a) a ## _X86
899 #include "swscale_template.c"
900 */
901 //MMX versions
902 #ifdef COMPILE_MMX
903 #undef RENAME
904 #define HAVE_MMX
905 #undef HAVE_MMX2
906 #undef HAVE_3DNOW
907 #define RENAME(a) a ## _MMX
908 #include "swscale_template.c"
909 #endif
910
911 //MMX2 versions
912 #ifdef COMPILE_MMX2
913 #undef RENAME
914 #define HAVE_MMX
915 #define HAVE_MMX2
916 #undef HAVE_3DNOW
917 #define RENAME(a) a ## _MMX2
918 #include "swscale_template.c"
919 #endif
920
921 //3DNOW versions
922 #ifdef COMPILE_3DNOW
923 #undef RENAME
924 #define HAVE_MMX
925 #undef HAVE_MMX2
926 #define HAVE_3DNOW
927 #define RENAME(a) a ## _3DNow
928 #include "swscale_template.c"
929 #endif
930
931 #endif //ARCH_X86 || ARCH_X86_64
932
933 // minor note: the HAVE_xyz is messed up after that line so don't use it
934
935 static double getSplineCoeff(double a, double b, double c, double d, double dist)
936 {
937 //    printf("%f %f %f %f %f\n", a,b,c,d,dist);
938     if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
939     else                return getSplineCoeff(        0.0,
940                                              b+ 2.0*c + 3.0*d,
941                                                     c + 3.0*d,
942                                             -b- 3.0*c - 6.0*d,
943                                             dist-1.0);
944 }
945
946 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
947                              int srcW, int dstW, int filterAlign, int one, int flags,
948                              SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
949 {
950     int i;
951     int filterSize;
952     int filter2Size;
953     int minFilterSize;
954     double *filter=NULL;
955     double *filter2=NULL;
956 #if defined(ARCH_X86)
957     if (flags & SWS_CPU_CAPS_MMX)
958         asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
959 #endif
960
961     // Note the +1 is for the MMXscaler which reads over the end
962     *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
963
964     if (FFABS(xInc - 0x10000) <10) // unscaled
965     {
966         int i;
967         filterSize= 1;
968         filter= av_malloc(dstW*sizeof(double)*filterSize);
969         for (i=0; i<dstW*filterSize; i++) filter[i]=0;
970
971         for (i=0; i<dstW; i++)
972         {
973             filter[i*filterSize]=1;
974             (*filterPos)[i]=i;
975         }
976
977     }
978     else if (flags&SWS_POINT) // lame looking point sampling mode
979     {
980         int i;
981         int xDstInSrc;
982         filterSize= 1;
983         filter= av_malloc(dstW*sizeof(double)*filterSize);
984
985         xDstInSrc= xInc/2 - 0x8000;
986         for (i=0; i<dstW; i++)
987         {
988             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
989
990             (*filterPos)[i]= xx;
991             filter[i]= 1.0;
992             xDstInSrc+= xInc;
993         }
994     }
995     else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
996     {
997         int i;
998         int xDstInSrc;
999         if      (flags&SWS_BICUBIC) filterSize= 4;
1000         else if (flags&SWS_X      ) filterSize= 4;
1001         else                        filterSize= 2; // SWS_BILINEAR / SWS_AREA
1002         filter= av_malloc(dstW*sizeof(double)*filterSize);
1003
1004         xDstInSrc= xInc/2 - 0x8000;
1005         for (i=0; i<dstW; i++)
1006         {
1007             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1008             int j;
1009
1010             (*filterPos)[i]= xx;
1011                 //Bilinear upscale / linear interpolate / Area averaging
1012                 for (j=0; j<filterSize; j++)
1013                 {
1014                     double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1015                     double coeff= 1.0 - d;
1016                     if (coeff<0) coeff=0;
1017                     filter[i*filterSize + j]= coeff;
1018                     xx++;
1019                 }
1020             xDstInSrc+= xInc;
1021         }
1022     }
1023     else
1024     {
1025         double xDstInSrc;
1026         double sizeFactor, filterSizeInSrc;
1027         const double xInc1= (double)xInc / (double)(1<<16);
1028
1029         if      (flags&SWS_BICUBIC)      sizeFactor=  4.0;
1030         else if (flags&SWS_X)            sizeFactor=  8.0;
1031         else if (flags&SWS_AREA)         sizeFactor=  1.0; //downscale only, for upscale it is bilinear
1032         else if (flags&SWS_GAUSS)        sizeFactor=  8.0;   // infinite ;)
1033         else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
1034         else if (flags&SWS_SINC)         sizeFactor= 20.0; // infinite ;)
1035         else if (flags&SWS_SPLINE)       sizeFactor= 20.0;  // infinite ;)
1036         else if (flags&SWS_BILINEAR)     sizeFactor=  2.0;
1037         else {
1038             sizeFactor= 0.0; //GCC warning killer
1039             ASSERT(0)
1040         }
1041
1042         if (xInc1 <= 1.0)       filterSizeInSrc= sizeFactor; // upscale
1043         else                    filterSizeInSrc= sizeFactor*srcW / (double)dstW;
1044
1045         filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1046         if (filterSize > srcW-2) filterSize=srcW-2;
1047
1048         filter= av_malloc(dstW*sizeof(double)*filterSize);
1049
1050         xDstInSrc= xInc1 / 2.0 - 0.5;
1051         for (i=0; i<dstW; i++)
1052         {
1053             int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
1054             int j;
1055             (*filterPos)[i]= xx;
1056             for (j=0; j<filterSize; j++)
1057             {
1058                 double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
1059                 double coeff;
1060                 if (flags & SWS_BICUBIC)
1061                 {
1062                     double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
1063                     double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
1064
1065                     if (d<1.0)
1066                         coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
1067                     else if (d<2.0)
1068                         coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
1069                     else
1070                         coeff=0.0;
1071                 }
1072 /*                else if (flags & SWS_X)
1073                 {
1074                     double p= param ? param*0.01 : 0.3;
1075                     coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1076                     coeff*= pow(2.0, - p*d*d);
1077                 }*/
1078                 else if (flags & SWS_X)
1079                 {
1080                     double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1081
1082                     if (d<1.0)
1083                         coeff = cos(d*PI);
1084                     else
1085                         coeff=-1.0;
1086                     if (coeff<0.0)      coeff= -pow(-coeff, A);
1087                     else                coeff=  pow( coeff, A);
1088                     coeff= coeff*0.5 + 0.5;
1089                 }
1090                 else if (flags & SWS_AREA)
1091                 {
1092                     double srcPixelSize= 1.0/xInc1;
1093                     if      (d + srcPixelSize/2 < 0.5) coeff= 1.0;
1094                     else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1095                     else coeff=0.0;
1096                 }
1097                 else if (flags & SWS_GAUSS)
1098                 {
1099                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1100                     coeff = pow(2.0, - p*d*d);
1101                 }
1102                 else if (flags & SWS_SINC)
1103                 {
1104                     coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1105                 }
1106                 else if (flags & SWS_LANCZOS)
1107                 {
1108                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1109                     coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1110                     if (d>p) coeff=0;
1111                 }
1112                 else if (flags & SWS_BILINEAR)
1113                 {
1114                     coeff= 1.0 - d;
1115                     if (coeff<0) coeff=0;
1116                 }
1117                 else if (flags & SWS_SPLINE)
1118                 {
1119                     double p=-2.196152422706632;
1120                     coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1121                 }
1122                 else {
1123                     coeff= 0.0; //GCC warning killer
1124                     ASSERT(0)
1125                 }
1126
1127                 filter[i*filterSize + j]= coeff;
1128                 xx++;
1129             }
1130             xDstInSrc+= xInc1;
1131         }
1132     }
1133
1134     /* apply src & dst Filter to filter -> filter2
1135        av_free(filter);
1136     */
1137     ASSERT(filterSize>0)
1138     filter2Size= filterSize;
1139     if (srcFilter) filter2Size+= srcFilter->length - 1;
1140     if (dstFilter) filter2Size+= dstFilter->length - 1;
1141     ASSERT(filter2Size>0)
1142     filter2= av_malloc(filter2Size*dstW*sizeof(double));
1143
1144     for (i=0; i<dstW; i++)
1145     {
1146         int j;
1147         SwsVector scaleFilter;
1148         SwsVector *outVec;
1149
1150         scaleFilter.coeff= filter + i*filterSize;
1151         scaleFilter.length= filterSize;
1152
1153         if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1154         else           outVec= &scaleFilter;
1155
1156         ASSERT(outVec->length == filter2Size)
1157         //FIXME dstFilter
1158
1159         for (j=0; j<outVec->length; j++)
1160         {
1161             filter2[i*filter2Size + j]= outVec->coeff[j];
1162         }
1163
1164         (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1165
1166         if (outVec != &scaleFilter) sws_freeVec(outVec);
1167     }
1168     av_free(filter); filter=NULL;
1169
1170     /* try to reduce the filter-size (step1 find size and shift left) */
1171     // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
1172     minFilterSize= 0;
1173     for (i=dstW-1; i>=0; i--)
1174     {
1175         int min= filter2Size;
1176         int j;
1177         double cutOff=0.0;
1178
1179         /* get rid off near zero elements on the left by shifting left */
1180         for (j=0; j<filter2Size; j++)
1181         {
1182             int k;
1183             cutOff += FFABS(filter2[i*filter2Size]);
1184
1185             if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1186
1187             /* preserve Monotonicity because the core can't handle the filter otherwise */
1188             if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1189
1190             // Move filter coeffs left
1191             for (k=1; k<filter2Size; k++)
1192                 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1193             filter2[i*filter2Size + k - 1]= 0.0;
1194             (*filterPos)[i]++;
1195         }
1196
1197         cutOff=0.0;
1198         /* count near zeros on the right */
1199         for (j=filter2Size-1; j>0; j--)
1200         {
1201             cutOff += FFABS(filter2[i*filter2Size + j]);
1202
1203             if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1204             min--;
1205         }
1206
1207         if (min>minFilterSize) minFilterSize= min;
1208     }
1209
1210     if (flags & SWS_CPU_CAPS_ALTIVEC) {
1211         // we can handle the special case 4,
1212         // so we don't want to go to the full 8
1213         if (minFilterSize < 5)
1214             filterAlign = 4;
1215
1216         // we really don't want to waste our time
1217         // doing useless computation, so fall-back on
1218         // the scalar C code for very small filter.
1219         // vectorizing is worth it only if you have
1220         // decent-sized vector.
1221         if (minFilterSize < 3)
1222             filterAlign = 1;
1223     }
1224
1225     if (flags & SWS_CPU_CAPS_MMX) {
1226         // special case for unscaled vertical filtering
1227         if (minFilterSize == 1 && filterAlign == 2)
1228             filterAlign= 1;
1229     }
1230
1231     ASSERT(minFilterSize > 0)
1232     filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1233     ASSERT(filterSize > 0)
1234     filter= av_malloc(filterSize*dstW*sizeof(double));
1235     if (filterSize >= MAX_FILTER_SIZE)
1236         return -1;
1237     *outFilterSize= filterSize;
1238
1239     if (flags&SWS_PRINT_INFO)
1240         av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1241     /* try to reduce the filter-size (step2 reduce it) */
1242     for (i=0; i<dstW; i++)
1243     {
1244         int j;
1245
1246         for (j=0; j<filterSize; j++)
1247         {
1248             if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
1249             else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1250         }
1251     }
1252     av_free(filter2); filter2=NULL;
1253
1254
1255     //FIXME try to align filterpos if possible
1256
1257     //fix borders
1258     for (i=0; i<dstW; i++)
1259     {
1260         int j;
1261         if ((*filterPos)[i] < 0)
1262         {
1263             // Move filter coeffs left to compensate for filterPos
1264             for (j=1; j<filterSize; j++)
1265             {
1266                 int left= FFMAX(j + (*filterPos)[i], 0);
1267                 filter[i*filterSize + left] += filter[i*filterSize + j];
1268                 filter[i*filterSize + j]=0;
1269             }
1270             (*filterPos)[i]= 0;
1271         }
1272
1273         if ((*filterPos)[i] + filterSize > srcW)
1274         {
1275             int shift= (*filterPos)[i] + filterSize - srcW;
1276             // Move filter coeffs right to compensate for filterPos
1277             for (j=filterSize-2; j>=0; j--)
1278             {
1279                 int right= FFMIN(j + shift, filterSize-1);
1280                 filter[i*filterSize +right] += filter[i*filterSize +j];
1281                 filter[i*filterSize +j]=0;
1282             }
1283             (*filterPos)[i]= srcW - filterSize;
1284         }
1285     }
1286
1287     // Note the +1 is for the MMXscaler which reads over the end
1288     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1289     *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1290
1291     /* Normalize & Store in outFilter */
1292     for (i=0; i<dstW; i++)
1293     {
1294         int j;
1295         double error=0;
1296         double sum=0;
1297         double scale= one;
1298
1299         for (j=0; j<filterSize; j++)
1300         {
1301             sum+= filter[i*filterSize + j];
1302         }
1303         scale/= sum;
1304         for (j=0; j<*outFilterSize; j++)
1305         {
1306             double v= filter[i*filterSize + j]*scale + error;
1307             int intV= floor(v + 0.5);
1308             (*outFilter)[i*(*outFilterSize) + j]= intV;
1309             error = v - intV;
1310         }
1311     }
1312
1313     (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1314     for (i=0; i<*outFilterSize; i++)
1315     {
1316         int j= dstW*(*outFilterSize);
1317         (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1318     }
1319
1320     av_free(filter);
1321     return 0;
1322 }
1323
1324 #ifdef COMPILE_MMX2
1325 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1326 {
1327     uint8_t *fragmentA;
1328     long imm8OfPShufW1A;
1329     long imm8OfPShufW2A;
1330     long fragmentLengthA;
1331     uint8_t *fragmentB;
1332     long imm8OfPShufW1B;
1333     long imm8OfPShufW2B;
1334     long fragmentLengthB;
1335     int fragmentPos;
1336
1337     int xpos, i;
1338
1339     // create an optimized horizontal scaling routine
1340
1341     //code fragment
1342
1343     asm volatile(
1344         "jmp                         9f                 \n\t"
1345     // Begin
1346         "0:                                             \n\t"
1347         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1348         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1349         "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1350         "punpcklbw                %%mm7, %%mm1          \n\t"
1351         "punpcklbw                %%mm7, %%mm0          \n\t"
1352         "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1353         "1:                                             \n\t"
1354         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1355         "2:                                             \n\t"
1356         "psubw                    %%mm1, %%mm0          \n\t"
1357         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1358         "pmullw                   %%mm3, %%mm0          \n\t"
1359         "psllw                       $7, %%mm1          \n\t"
1360         "paddw                    %%mm1, %%mm0          \n\t"
1361
1362         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1363
1364         "add                         $8, %%"REG_a"      \n\t"
1365     // End
1366         "9:                                             \n\t"
1367 //        "int $3                                         \n\t"
1368         "lea                         0b, %0             \n\t"
1369         "lea                         1b, %1             \n\t"
1370         "lea                         2b, %2             \n\t"
1371         "dec                         %1                 \n\t"
1372         "dec                         %2                 \n\t"
1373         "sub                         %0, %1             \n\t"
1374         "sub                         %0, %2             \n\t"
1375         "lea                         9b, %3             \n\t"
1376         "sub                         %0, %3             \n\t"
1377
1378
1379         :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1380         "=r" (fragmentLengthA)
1381     );
1382
1383     asm volatile(
1384         "jmp                         9f                 \n\t"
1385     // Begin
1386         "0:                                             \n\t"
1387         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1388         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1389         "punpcklbw                %%mm7, %%mm0          \n\t"
1390         "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1391         "1:                                             \n\t"
1392         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1393         "2:                                             \n\t"
1394         "psubw                    %%mm1, %%mm0          \n\t"
1395         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1396         "pmullw                   %%mm3, %%mm0          \n\t"
1397         "psllw                       $7, %%mm1          \n\t"
1398         "paddw                    %%mm1, %%mm0          \n\t"
1399
1400         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1401
1402         "add                         $8, %%"REG_a"      \n\t"
1403     // End
1404         "9:                                             \n\t"
1405 //        "int                       $3                   \n\t"
1406         "lea                         0b, %0             \n\t"
1407         "lea                         1b, %1             \n\t"
1408         "lea                         2b, %2             \n\t"
1409         "dec                         %1                 \n\t"
1410         "dec                         %2                 \n\t"
1411         "sub                         %0, %1             \n\t"
1412         "sub                         %0, %2             \n\t"
1413         "lea                         9b, %3             \n\t"
1414         "sub                         %0, %3             \n\t"
1415
1416
1417         :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1418         "=r" (fragmentLengthB)
1419     );
1420
1421     xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1422     fragmentPos=0;
1423
1424     for (i=0; i<dstW/numSplits; i++)
1425     {
1426         int xx=xpos>>16;
1427
1428         if ((i&3) == 0)
1429         {
1430             int a=0;
1431             int b=((xpos+xInc)>>16) - xx;
1432             int c=((xpos+xInc*2)>>16) - xx;
1433             int d=((xpos+xInc*3)>>16) - xx;
1434
1435             filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1436             filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1437             filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1438             filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1439             filterPos[i/2]= xx;
1440
1441             if (d+1<4)
1442             {
1443                 int maxShift= 3-(d+1);
1444                 int shift=0;
1445
1446                 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1447
1448                 funnyCode[fragmentPos + imm8OfPShufW1B]=
1449                     (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1450                 funnyCode[fragmentPos + imm8OfPShufW2B]=
1451                     a | (b<<2) | (c<<4) | (d<<6);
1452
1453                 if (i+3>=dstW) shift=maxShift; //avoid overread
1454                 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1455
1456                 if (shift && i>=shift)
1457                 {
1458                     funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1459                     funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1460                     filterPos[i/2]-=shift;
1461                 }
1462
1463                 fragmentPos+= fragmentLengthB;
1464             }
1465             else
1466             {
1467                 int maxShift= 3-d;
1468                 int shift=0;
1469
1470                 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1471
1472                 funnyCode[fragmentPos + imm8OfPShufW1A]=
1473                 funnyCode[fragmentPos + imm8OfPShufW2A]=
1474                     a | (b<<2) | (c<<4) | (d<<6);
1475
1476                 if (i+4>=dstW) shift=maxShift; //avoid overread
1477                 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1478
1479                 if (shift && i>=shift)
1480                 {
1481                     funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1482                     funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1483                     filterPos[i/2]-=shift;
1484                 }
1485
1486                 fragmentPos+= fragmentLengthA;
1487             }
1488
1489             funnyCode[fragmentPos]= RET;
1490         }
1491         xpos+=xInc;
1492     }
1493     filterPos[i/2]= xpos>>16; // needed to jump to the next part
1494 }
1495 #endif /* COMPILE_MMX2 */
1496
1497 static void globalInit(void){
1498     // generating tables:
1499     int i;
1500     for (i=0; i<768; i++){
1501         int c= av_clip_uint8(i-256);
1502         clip_table[i]=c;
1503     }
1504 }
1505
1506 static SwsFunc getSwsFunc(int flags){
1507
1508 #if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1509 #if defined(ARCH_X86)
1510     // ordered per speed fasterst first
1511     if (flags & SWS_CPU_CAPS_MMX2)
1512         return swScale_MMX2;
1513     else if (flags & SWS_CPU_CAPS_3DNOW)
1514         return swScale_3DNow;
1515     else if (flags & SWS_CPU_CAPS_MMX)
1516         return swScale_MMX;
1517     else
1518         return swScale_C;
1519
1520 #else
1521 #ifdef ARCH_POWERPC
1522     if (flags & SWS_CPU_CAPS_ALTIVEC)
1523         return swScale_altivec;
1524     else
1525         return swScale_C;
1526 #endif
1527     return swScale_C;
1528 #endif /* defined(ARCH_X86) */
1529 #else //RUNTIME_CPUDETECT
1530 #ifdef HAVE_MMX2
1531     return swScale_MMX2;
1532 #elif defined (HAVE_3DNOW)
1533     return swScale_3DNow;
1534 #elif defined (HAVE_MMX)
1535     return swScale_MMX;
1536 #elif defined (HAVE_ALTIVEC)
1537     return swScale_altivec;
1538 #else
1539     return swScale_C;
1540 #endif
1541 #endif //!RUNTIME_CPUDETECT
1542 }
1543
1544 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1545                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1546     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1547     /* Copy Y plane */
1548     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1549         memcpy(dst, src[0], srcSliceH*dstStride[0]);
1550     else
1551     {
1552         int i;
1553         uint8_t *srcPtr= src[0];
1554         uint8_t *dstPtr= dst;
1555         for (i=0; i<srcSliceH; i++)
1556         {
1557             memcpy(dstPtr, srcPtr, c->srcW);
1558             srcPtr+= srcStride[0];
1559             dstPtr+= dstStride[0];
1560         }
1561     }
1562     dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1563     if (c->dstFormat == PIX_FMT_NV12)
1564         interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
1565     else
1566         interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
1567
1568     return srcSliceH;
1569 }
1570
1571 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1572                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1573     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1574
1575     yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1576
1577     return srcSliceH;
1578 }
1579
1580 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1581                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1582     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1583
1584     yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1585
1586     return srcSliceH;
1587 }
1588
1589 /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1590 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1591                           int srcSliceH, uint8_t* dst[], int dstStride[]){
1592     const int srcFormat= c->srcFormat;
1593     const int dstFormat= c->dstFormat;
1594     const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1595     const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1596     const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1597     const int dstId= fmt_depth(dstFormat) >> 2;
1598     void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1599
1600     /* BGR -> BGR */
1601     if (  (isBGR(srcFormat) && isBGR(dstFormat))
1602        || (isRGB(srcFormat) && isRGB(dstFormat))){
1603         switch(srcId | (dstId<<4)){
1604         case 0x34: conv= rgb16to15; break;
1605         case 0x36: conv= rgb24to15; break;
1606         case 0x38: conv= rgb32to15; break;
1607         case 0x43: conv= rgb15to16; break;
1608         case 0x46: conv= rgb24to16; break;
1609         case 0x48: conv= rgb32to16; break;
1610         case 0x63: conv= rgb15to24; break;
1611         case 0x64: conv= rgb16to24; break;
1612         case 0x68: conv= rgb32to24; break;
1613         case 0x83: conv= rgb15to32; break;
1614         case 0x84: conv= rgb16to32; break;
1615         case 0x86: conv= rgb24to32; break;
1616         default: av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1617                         sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1618         }
1619     }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
1620              || (isRGB(srcFormat) && isBGR(dstFormat))){
1621         switch(srcId | (dstId<<4)){
1622         case 0x33: conv= rgb15tobgr15; break;
1623         case 0x34: conv= rgb16tobgr15; break;
1624         case 0x36: conv= rgb24tobgr15; break;
1625         case 0x38: conv= rgb32tobgr15; break;
1626         case 0x43: conv= rgb15tobgr16; break;
1627         case 0x44: conv= rgb16tobgr16; break;
1628         case 0x46: conv= rgb24tobgr16; break;
1629         case 0x48: conv= rgb32tobgr16; break;
1630         case 0x63: conv= rgb15tobgr24; break;
1631         case 0x64: conv= rgb16tobgr24; break;
1632         case 0x66: conv= rgb24tobgr24; break;
1633         case 0x68: conv= rgb32tobgr24; break;
1634         case 0x83: conv= rgb15tobgr32; break;
1635         case 0x84: conv= rgb16tobgr32; break;
1636         case 0x86: conv= rgb24tobgr32; break;
1637         case 0x88: conv= rgb32tobgr32; break;
1638         default: av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1639                         sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1640         }
1641     }else{
1642         av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1643                sws_format_name(srcFormat), sws_format_name(dstFormat));
1644     }
1645
1646     if(conv)
1647     {
1648         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp)
1649             conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1650         else
1651         {
1652             int i;
1653             uint8_t *srcPtr= src[0];
1654             uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1655
1656             for (i=0; i<srcSliceH; i++)
1657             {
1658                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1659                 srcPtr+= srcStride[0];
1660                 dstPtr+= dstStride[0];
1661             }
1662         }
1663     }
1664     return srcSliceH;
1665 }
1666
1667 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1668                               int srcSliceH, uint8_t* dst[], int dstStride[]){
1669
1670     rgb24toyv12(
1671         src[0],
1672         dst[0]+ srcSliceY    *dstStride[0],
1673         dst[1]+(srcSliceY>>1)*dstStride[1],
1674         dst[2]+(srcSliceY>>1)*dstStride[2],
1675         c->srcW, srcSliceH,
1676         dstStride[0], dstStride[1], srcStride[0]);
1677     return srcSliceH;
1678 }
1679
1680 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1681                              int srcSliceH, uint8_t* dst[], int dstStride[]){
1682     int i;
1683
1684     /* copy Y */
1685     if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1686         memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1687     else{
1688         uint8_t *srcPtr= src[0];
1689         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1690
1691         for (i=0; i<srcSliceH; i++)
1692         {
1693             memcpy(dstPtr, srcPtr, c->srcW);
1694             srcPtr+= srcStride[0];
1695             dstPtr+= dstStride[0];
1696         }
1697     }
1698
1699     if (c->dstFormat==PIX_FMT_YUV420P){
1700         planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1701         planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1702     }else{
1703         planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1704         planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1705     }
1706     return srcSliceH;
1707 }
1708
1709 /* unscaled copy like stuff (assumes nearly identical formats) */
1710 static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1711                       int srcSliceH, uint8_t* dst[], int dstStride[]){
1712
1713     if (isPacked(c->srcFormat))
1714     {
1715         if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1716             memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1717         else
1718         {
1719             int i;
1720             uint8_t *srcPtr= src[0];
1721             uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1722             int length=0;
1723
1724             /* universal length finder */
1725             while(length+c->srcW <= FFABS(dstStride[0])
1726                && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1727             ASSERT(length!=0);
1728
1729             for (i=0; i<srcSliceH; i++)
1730             {
1731                 memcpy(dstPtr, srcPtr, length);
1732                 srcPtr+= srcStride[0];
1733                 dstPtr+= dstStride[0];
1734             }
1735         }
1736     }
1737     else
1738     { /* Planar YUV or gray */
1739         int plane;
1740         for (plane=0; plane<3; plane++)
1741         {
1742             int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1743             int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1744             int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1745
1746             if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1747             {
1748                 if (!isGray(c->dstFormat))
1749                     memset(dst[plane], 128, dstStride[plane]*height);
1750             }
1751             else
1752             {
1753                 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1754                     memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1755                 else
1756                 {
1757                     int i;
1758                     uint8_t *srcPtr= src[plane];
1759                     uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1760                     for (i=0; i<height; i++)
1761                     {
1762                         memcpy(dstPtr, srcPtr, length);
1763                         srcPtr+= srcStride[plane];
1764                         dstPtr+= dstStride[plane];
1765                     }
1766                 }
1767             }
1768         }
1769     }
1770     return srcSliceH;
1771 }
1772
1773 static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1774                         int srcSliceH, uint8_t* dst[], int dstStride[]){
1775
1776     int length= c->srcW;
1777     int y=      srcSliceY;
1778     int height= srcSliceH;
1779     int i, j;
1780     uint8_t *srcPtr= src[0];
1781     uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1782
1783     if (!isGray(c->dstFormat)){
1784         int height= -((-srcSliceH)>>c->chrDstVSubSample);
1785         memset(dst[1], 128, dstStride[1]*height);
1786         memset(dst[2], 128, dstStride[2]*height);
1787     }
1788     if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1789     for (i=0; i<height; i++)
1790     {
1791         for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1792         srcPtr+= srcStride[0];
1793         dstPtr+= dstStride[0];
1794     }
1795     return srcSliceH;
1796 }
1797
1798 static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1799                         int srcSliceH, uint8_t* dst[], int dstStride[]){
1800
1801     int length= c->srcW;
1802     int y=      srcSliceY;
1803     int height= srcSliceH;
1804     int i, j;
1805     uint8_t *srcPtr= src[0];
1806     uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1807     for (i=0; i<height; i++)
1808     {
1809         for (j=0; j<length; j++)
1810         {
1811             dstPtr[j<<1] = srcPtr[j];
1812             dstPtr[(j<<1)+1] = srcPtr[j];
1813         }
1814         srcPtr+= srcStride[0];
1815         dstPtr+= dstStride[0];
1816     }
1817     return srcSliceH;
1818 }
1819
1820 static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1821                       int srcSliceH, uint8_t* dst[], int dstStride[]){
1822
1823     int length= c->srcW;
1824     int y=      srcSliceY;
1825     int height= srcSliceH;
1826     int i, j;
1827     uint16_t *srcPtr= src[0];
1828     uint16_t *dstPtr= dst[0] + dstStride[0]*y/2;
1829     for (i=0; i<height; i++)
1830     {
1831         for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1832         srcPtr+= srcStride[0]/2;
1833         dstPtr+= dstStride[0]/2;
1834     }
1835     return srcSliceH;
1836 }
1837
1838
1839 static void getSubSampleFactors(int *h, int *v, int format){
1840     switch(format){
1841     case PIX_FMT_UYVY422:
1842     case PIX_FMT_YUYV422:
1843         *h=1;
1844         *v=0;
1845         break;
1846     case PIX_FMT_YUV420P:
1847     case PIX_FMT_GRAY16BE:
1848     case PIX_FMT_GRAY16LE:
1849     case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
1850     case PIX_FMT_NV12:
1851     case PIX_FMT_NV21:
1852         *h=1;
1853         *v=1;
1854         break;
1855     case PIX_FMT_YUV410P:
1856         *h=2;
1857         *v=2;
1858         break;
1859     case PIX_FMT_YUV444P:
1860         *h=0;
1861         *v=0;
1862         break;
1863     case PIX_FMT_YUV422P:
1864         *h=1;
1865         *v=0;
1866         break;
1867     case PIX_FMT_YUV411P:
1868         *h=2;
1869         *v=0;
1870         break;
1871     default:
1872         *h=0;
1873         *v=0;
1874         break;
1875     }
1876 }
1877
1878 static uint16_t roundToInt16(int64_t f){
1879     int r= (f + (1<<15))>>16;
1880          if (r<-0x7FFF) return 0x8000;
1881     else if (r> 0x7FFF) return 0x7FFF;
1882     else                return r;
1883 }
1884
1885 /**
1886  * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1887  * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
1888  * @return -1 if not supported
1889  */
1890 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1891     int64_t crv =  inv_table[0];
1892     int64_t cbu =  inv_table[1];
1893     int64_t cgu = -inv_table[2];
1894     int64_t cgv = -inv_table[3];
1895     int64_t cy  = 1<<16;
1896     int64_t oy  = 0;
1897
1898     if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1899     memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1900     memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
1901
1902     c->brightness= brightness;
1903     c->contrast  = contrast;
1904     c->saturation= saturation;
1905     c->srcRange  = srcRange;
1906     c->dstRange  = dstRange;
1907
1908     c->uOffset=   0x0400040004000400LL;
1909     c->vOffset=   0x0400040004000400LL;
1910
1911     if (!srcRange){
1912         cy= (cy*255) / 219;
1913         oy= 16<<16;
1914     }else{
1915         crv= (crv*224) / 255;
1916         cbu= (cbu*224) / 255;
1917         cgu= (cgu*224) / 255;
1918         cgv= (cgv*224) / 255;
1919     }
1920
1921     cy = (cy *contrast             )>>16;
1922     crv= (crv*contrast * saturation)>>32;
1923     cbu= (cbu*contrast * saturation)>>32;
1924     cgu= (cgu*contrast * saturation)>>32;
1925     cgv= (cgv*contrast * saturation)>>32;
1926
1927     oy -= 256*brightness;
1928
1929     c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
1930     c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
1931     c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1932     c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1933     c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1934     c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
1935
1936     yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1937     //FIXME factorize
1938
1939 #ifdef COMPILE_ALTIVEC
1940     if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1941         yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1942 #endif
1943     return 0;
1944 }
1945
1946 /**
1947  * @return -1 if not supported
1948  */
1949 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1950     if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1951
1952     *inv_table = c->srcColorspaceTable;
1953     *table     = c->dstColorspaceTable;
1954     *srcRange  = c->srcRange;
1955     *dstRange  = c->dstRange;
1956     *brightness= c->brightness;
1957     *contrast  = c->contrast;
1958     *saturation= c->saturation;
1959
1960     return 0;
1961 }
1962
1963 static int handle_jpeg(int *format)
1964 {
1965     switch (*format) {
1966         case PIX_FMT_YUVJ420P:
1967             *format = PIX_FMT_YUV420P;
1968             return 1;
1969         case PIX_FMT_YUVJ422P:
1970             *format = PIX_FMT_YUV422P;
1971             return 1;
1972         case PIX_FMT_YUVJ444P:
1973             *format = PIX_FMT_YUV444P;
1974             return 1;
1975         default:
1976             return 0;
1977     }
1978 }
1979
1980 SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1981                            SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
1982
1983     SwsContext *c;
1984     int i;
1985     int usesVFilter, usesHFilter;
1986     int unscaled, needsDither;
1987     int srcRange, dstRange;
1988     SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1989 #if defined(ARCH_X86)
1990     if (flags & SWS_CPU_CAPS_MMX)
1991         asm volatile("emms\n\t"::: "memory");
1992 #endif
1993
1994 #if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
1995     flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
1996 #ifdef HAVE_MMX2
1997     flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1998 #elif defined (HAVE_3DNOW)
1999     flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2000 #elif defined (HAVE_MMX)
2001     flags |= SWS_CPU_CAPS_MMX;
2002 #elif defined (HAVE_ALTIVEC)
2003     flags |= SWS_CPU_CAPS_ALTIVEC;
2004 #elif defined (ARCH_BFIN)
2005     flags |= SWS_CPU_CAPS_BFIN;
2006 #endif
2007 #endif /* RUNTIME_CPUDETECT */
2008     if (clip_table[512] != 255) globalInit();
2009     if (rgb15to16 == NULL) sws_rgb2rgb_init(flags);
2010
2011     unscaled = (srcW == dstW && srcH == dstH);
2012     needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2013         && (fmt_depth(dstFormat))<24
2014         && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2015
2016     srcRange = handle_jpeg(&srcFormat);
2017     dstRange = handle_jpeg(&dstFormat);
2018
2019     if (!isSupportedIn(srcFormat))
2020     {
2021         av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
2022         return NULL;
2023     }
2024     if (!isSupportedOut(dstFormat))
2025     {
2026         av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
2027         return NULL;
2028     }
2029
2030     /* sanity check */
2031     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
2032     {
2033         av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2034                srcW, srcH, dstW, dstH);
2035         return NULL;
2036     }
2037
2038     if (!dstFilter) dstFilter= &dummyFilter;
2039     if (!srcFilter) srcFilter= &dummyFilter;
2040
2041     c= av_mallocz(sizeof(SwsContext));
2042
2043     c->av_class = &sws_context_class;
2044     c->srcW= srcW;
2045     c->srcH= srcH;
2046     c->dstW= dstW;
2047     c->dstH= dstH;
2048     c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2049     c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2050     c->flags= flags;
2051     c->dstFormat= dstFormat;
2052     c->srcFormat= srcFormat;
2053     c->vRounder= 4* 0x0001000100010001ULL;
2054
2055     usesHFilter= usesVFilter= 0;
2056     if (dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
2057     if (dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
2058     if (dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
2059     if (dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
2060     if (srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
2061     if (srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
2062     if (srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
2063     if (srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
2064
2065     getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2066     getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2067
2068     // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2069     if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2070
2071     // drop some chroma lines if the user wants it
2072     c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2073     c->chrSrcVSubSample+= c->vChrDrop;
2074
2075     // drop every 2. pixel for chroma calculation unless user wants full chroma
2076     if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2077       && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2078       && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2079       && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE)
2080         c->chrSrcHSubSample=1;
2081
2082     if (param){
2083         c->param[0] = param[0];
2084         c->param[1] = param[1];
2085     }else{
2086         c->param[0] =
2087         c->param[1] = SWS_PARAM_DEFAULT;
2088     }
2089
2090     c->chrIntHSubSample= c->chrDstHSubSample;
2091     c->chrIntVSubSample= c->chrSrcVSubSample;
2092
2093     // Note the -((-x)>>y) is so that we always round toward +inf.
2094     c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2095     c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2096     c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2097     c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2098
2099     sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2100
2101     /* unscaled special Cases */
2102     if (unscaled && !usesHFilter && !usesVFilter)
2103     {
2104         /* yv12_to_nv12 */
2105         if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2106         {
2107             c->swScale= PlanarToNV12Wrapper;
2108         }
2109 #ifdef CONFIG_GPL
2110         /* yuv2bgr */
2111         if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
2112         {
2113             c->swScale= yuv2rgb_get_func_ptr(c);
2114         }
2115 #endif
2116
2117         if ( srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P )
2118         {
2119             c->swScale= yvu9toyv12Wrapper;
2120         }
2121
2122         /* bgr24toYV12 */
2123         if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
2124             c->swScale= bgr24toyv12Wrapper;
2125
2126         /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2127         if (  (isBGR(srcFormat) || isRGB(srcFormat))
2128            && (isBGR(dstFormat) || isRGB(dstFormat))
2129            && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2130            && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2131            && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2132            && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2133            && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2134            && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2135            && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2136            && !needsDither)
2137              c->swScale= rgb2rgbWrapper;
2138
2139         /* LQ converters if -sws 0 or -sws 4*/
2140         if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2141             /* rgb/bgr -> rgb/bgr (dither needed forms) */
2142             if ( (isBGR(srcFormat) || isRGB(srcFormat))
2143               && (isBGR(dstFormat) || isRGB(dstFormat))
2144               && needsDither)
2145                 c->swScale= rgb2rgbWrapper;
2146
2147             /* yv12_to_yuy2 */
2148             if (srcFormat == PIX_FMT_YUV420P &&
2149                 (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
2150             {
2151                 if (dstFormat == PIX_FMT_YUYV422)
2152                     c->swScale= PlanarToYuy2Wrapper;
2153                 else
2154                     c->swScale= PlanarToUyvyWrapper;
2155             }
2156         }
2157
2158 #ifdef COMPILE_ALTIVEC
2159         if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2160             ((srcFormat == PIX_FMT_YUV420P &&
2161              (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
2162           // unscaled YV12 -> packed YUV, we want speed
2163           if (dstFormat == PIX_FMT_YUYV422)
2164               c->swScale= yv12toyuy2_unscaled_altivec;
2165           else
2166               c->swScale= yv12touyvy_unscaled_altivec;
2167         }
2168 #endif
2169
2170         /* simple copy */
2171         if (  srcFormat == dstFormat
2172             || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2173             || (isPlanarYUV(dstFormat) && isGray(srcFormat)) )
2174         {
2175             c->swScale= simpleCopy;
2176         }
2177
2178         /* gray16{le,be} conversions */
2179         if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2180         {
2181             c->swScale= gray16togray;
2182         }
2183         if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2184         {
2185             c->swScale= graytogray16;
2186         }
2187         if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2188         {
2189             c->swScale= gray16swap;
2190         }
2191
2192         if (c->swScale){
2193             if (flags&SWS_PRINT_INFO)
2194                 av_log(c, AV_LOG_INFO, "SwScaler: using unscaled %s -> %s special converter\n",
2195                                 sws_format_name(srcFormat), sws_format_name(dstFormat));
2196             return c;
2197         }
2198     }
2199
2200     if (flags & SWS_CPU_CAPS_MMX2)
2201     {
2202         c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2203         if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2204         {
2205             if (flags&SWS_PRINT_INFO)
2206                 av_log(c, AV_LOG_INFO, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
2207         }
2208         if (usesHFilter) c->canMMX2BeUsed=0;
2209     }
2210     else
2211         c->canMMX2BeUsed=0;
2212
2213     c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2214     c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2215
2216     // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2217     // but only for the FAST_BILINEAR mode otherwise do correct scaling
2218     // n-2 is the last chrominance sample available
2219     // this is not perfect, but noone shuld notice the difference, the more correct variant
2220     // would be like the vertical one, but that would require some special code for the
2221     // first and last pixel
2222     if (flags&SWS_FAST_BILINEAR)
2223     {
2224         if (c->canMMX2BeUsed)
2225         {
2226             c->lumXInc+= 20;
2227             c->chrXInc+= 20;
2228         }
2229         //we don't use the x86asm scaler if mmx is available
2230         else if (flags & SWS_CPU_CAPS_MMX)
2231         {
2232             c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2233             c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2234         }
2235     }
2236
2237     /* precalculate horizontal scaler filter coefficients */
2238     {
2239         const int filterAlign=
2240             (flags & SWS_CPU_CAPS_MMX) ? 4 :
2241             (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2242             1;
2243
2244         initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2245                    srcW      ,       dstW, filterAlign, 1<<14,
2246                    (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2247                    srcFilter->lumH, dstFilter->lumH, c->param);
2248         initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2249                    c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2250                    (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2251                    srcFilter->chrH, dstFilter->chrH, c->param);
2252
2253 #define MAX_FUNNY_CODE_SIZE 10000
2254 #if defined(COMPILE_MMX2)
2255 // can't downscale !!!
2256         if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2257         {
2258 #ifdef MAP_ANONYMOUS
2259             c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2260             c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2261 #else
2262             c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2263             c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2264 #endif
2265
2266             c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2267             c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2268             c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2269             c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2270
2271             initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2272             initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2273         }
2274 #endif /* defined(COMPILE_MMX2) */
2275     } // Init Horizontal stuff
2276
2277
2278
2279     /* precalculate vertical scaler filter coefficients */
2280     {
2281         const int filterAlign=
2282             (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2283             (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2284             1;
2285
2286         initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2287                    srcH      ,        dstH, filterAlign, (1<<12)-4,
2288                    (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2289                    srcFilter->lumV, dstFilter->lumV, c->param);
2290         initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2291                    c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2292                    (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2293                    srcFilter->chrV, dstFilter->chrV, c->param);
2294
2295 #ifdef HAVE_ALTIVEC
2296         c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2297         c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2298
2299         for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2300             int j;
2301             short *p = (short *)&c->vYCoeffsBank[i];
2302             for (j=0;j<8;j++)
2303                 p[j] = c->vLumFilter[i];
2304         }
2305
2306         for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2307             int j;
2308             short *p = (short *)&c->vCCoeffsBank[i];
2309             for (j=0;j<8;j++)
2310                 p[j] = c->vChrFilter[i];
2311         }
2312 #endif
2313     }
2314
2315     // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2316     c->vLumBufSize= c->vLumFilterSize;
2317     c->vChrBufSize= c->vChrFilterSize;
2318     for (i=0; i<dstH; i++)
2319     {
2320         int chrI= i*c->chrDstH / dstH;
2321         int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2322                            ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2323
2324         nextSlice>>= c->chrSrcVSubSample;
2325         nextSlice<<= c->chrSrcVSubSample;
2326         if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2327             c->vLumBufSize= nextSlice - c->vLumFilterPos[i   ];
2328         if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2329             c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2330     }
2331
2332     // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2333     c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2334     c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2335     //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)
2336     /* align at 16 bytes for AltiVec */
2337     for (i=0; i<c->vLumBufSize; i++)
2338         c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(4000);
2339     for (i=0; i<c->vChrBufSize; i++)
2340         c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
2341
2342     //try to avoid drawing green stuff between the right end and the stride end
2343     for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2344
2345     ASSERT(c->chrDstH <= dstH)
2346
2347     if (flags&SWS_PRINT_INFO)
2348     {
2349 #ifdef DITHER1XBPP
2350         char *dither= " dithered";
2351 #else
2352         char *dither= "";
2353 #endif
2354         if (flags&SWS_FAST_BILINEAR)
2355             av_log(c, AV_LOG_INFO, "SwScaler: FAST_BILINEAR scaler, ");
2356         else if (flags&SWS_BILINEAR)
2357             av_log(c, AV_LOG_INFO, "SwScaler: BILINEAR scaler, ");
2358         else if (flags&SWS_BICUBIC)
2359             av_log(c, AV_LOG_INFO, "SwScaler: BICUBIC scaler, ");
2360         else if (flags&SWS_X)
2361             av_log(c, AV_LOG_INFO, "SwScaler: Experimental scaler, ");
2362         else if (flags&SWS_POINT)
2363             av_log(c, AV_LOG_INFO, "SwScaler: Nearest Neighbor / POINT scaler, ");
2364         else if (flags&SWS_AREA)
2365             av_log(c, AV_LOG_INFO, "SwScaler: Area Averageing scaler, ");
2366         else if (flags&SWS_BICUBLIN)
2367             av_log(c, AV_LOG_INFO, "SwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
2368         else if (flags&SWS_GAUSS)
2369             av_log(c, AV_LOG_INFO, "SwScaler: Gaussian scaler, ");
2370         else if (flags&SWS_SINC)
2371             av_log(c, AV_LOG_INFO, "SwScaler: Sinc scaler, ");
2372         else if (flags&SWS_LANCZOS)
2373             av_log(c, AV_LOG_INFO, "SwScaler: Lanczos scaler, ");
2374         else if (flags&SWS_SPLINE)
2375             av_log(c, AV_LOG_INFO, "SwScaler: Bicubic spline scaler, ");
2376         else
2377             av_log(c, AV_LOG_INFO, "SwScaler: ehh flags invalid?! ");
2378
2379         if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2380             av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2381                    sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2382         else
2383             av_log(c, AV_LOG_INFO, "from %s to %s ",
2384                    sws_format_name(srcFormat), sws_format_name(dstFormat));
2385
2386         if (flags & SWS_CPU_CAPS_MMX2)
2387             av_log(c, AV_LOG_INFO, "using MMX2\n");
2388         else if (flags & SWS_CPU_CAPS_3DNOW)
2389             av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2390         else if (flags & SWS_CPU_CAPS_MMX)
2391             av_log(c, AV_LOG_INFO, "using MMX\n");
2392         else if (flags & SWS_CPU_CAPS_ALTIVEC)
2393             av_log(c, AV_LOG_INFO, "using AltiVec\n");
2394         else
2395             av_log(c, AV_LOG_INFO, "using C\n");
2396     }
2397
2398     if (flags & SWS_PRINT_INFO)
2399     {
2400         if (flags & SWS_CPU_CAPS_MMX)
2401         {
2402             if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2403                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2404             else
2405             {
2406                 if (c->hLumFilterSize==4)
2407                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2408                 else if (c->hLumFilterSize==8)
2409                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2410                 else
2411                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2412
2413                 if (c->hChrFilterSize==4)
2414                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2415                 else if (c->hChrFilterSize==8)
2416                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2417                 else
2418                     av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2419             }
2420         }
2421         else
2422         {
2423 #if defined(ARCH_X86)
2424             av_log(c, AV_LOG_VERBOSE, "SwScaler: using X86-Asm scaler for horizontal scaling\n");
2425 #else
2426             if (flags & SWS_FAST_BILINEAR)
2427                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2428             else
2429                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using C scaler for horizontal scaling\n");
2430 #endif
2431         }
2432         if (isPlanarYUV(dstFormat))
2433         {
2434             if (c->vLumFilterSize==1)
2435                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2436             else
2437                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2438         }
2439         else
2440         {
2441             if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2442                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2443                        "SwScaler:       2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2444             else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2445                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2446             else
2447                 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2448         }
2449
2450         if (dstFormat==PIX_FMT_BGR24)
2451             av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR24 Converter\n",
2452                    (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2453         else if (dstFormat==PIX_FMT_RGB32)
2454             av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2455         else if (dstFormat==PIX_FMT_BGR565)
2456             av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2457         else if (dstFormat==PIX_FMT_BGR555)
2458             av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2459
2460         av_log(c, AV_LOG_VERBOSE, "SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2461     }
2462     if (flags & SWS_PRINT_INFO)
2463     {
2464         av_log(c, AV_LOG_DEBUG, "SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2465                c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2466         av_log(c, AV_LOG_DEBUG, "SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2467                c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2468     }
2469
2470     c->swScale= getSwsFunc(flags);
2471     return c;
2472 }
2473
2474 /**
2475  * swscale warper, so we don't need to export the SwsContext.
2476  * assumes planar YUV to be in YUV order instead of YVU
2477  */
2478 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2479               int srcSliceH, uint8_t* dst[], int dstStride[]){
2480     int i;
2481     uint8_t* src2[4]= {src[0], src[1], src[2]};
2482     uint32_t pal[256];
2483     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2484         av_log(c, AV_LOG_ERROR, "swScaler: slices start in the middle!\n");
2485         return 0;
2486     }
2487     if (c->sliceDir == 0) {
2488         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2489     }
2490
2491     if (c->srcFormat == PIX_FMT_PAL8){
2492         for (i=0; i<256; i++){
2493             int p= ((uint32_t*)(src[1]))[i];
2494             int r= (p>>16)&0xFF;
2495             int g= (p>> 8)&0xFF;
2496             int b=  p     &0xFF;
2497             int y= av_clip_uint8(((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16 );
2498             int u= av_clip_uint8(((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128);
2499             int v= av_clip_uint8(((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128);
2500             pal[i]= y + (u<<8) + (v<<16);
2501         }
2502         src2[1]= pal;
2503     }
2504
2505     // copy strides, so they can safely be modified
2506     if (c->sliceDir == 1) {
2507         // slices go from top to bottom
2508         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2509         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2510         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2511     } else {
2512         // slices go from bottom to top => we flip the image internally
2513         uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2514                            dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2515                            dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2516         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2517         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2518
2519         src2[0] += (srcSliceH-1)*srcStride[0];
2520         if (c->srcFormat != PIX_FMT_PAL8)
2521             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2522         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2523
2524         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2525     }
2526 }
2527
2528 /**
2529  * swscale warper, so we don't need to export the SwsContext
2530  */
2531 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2532                       int srcSliceH, uint8_t* dst[], int dstStride[]){
2533     return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2534 }
2535
2536 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2537                                 float lumaSharpen, float chromaSharpen,
2538                                 float chromaHShift, float chromaVShift,
2539                                 int verbose)
2540 {
2541     SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2542
2543     if (lumaGBlur!=0.0){
2544         filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2545         filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2546     }else{
2547         filter->lumH= sws_getIdentityVec();
2548         filter->lumV= sws_getIdentityVec();
2549     }
2550
2551     if (chromaGBlur!=0.0){
2552         filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2553         filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2554     }else{
2555         filter->chrH= sws_getIdentityVec();
2556         filter->chrV= sws_getIdentityVec();
2557     }
2558
2559     if (chromaSharpen!=0.0){
2560         SwsVector *id= sws_getIdentityVec();
2561         sws_scaleVec(filter->chrH, -chromaSharpen);
2562         sws_scaleVec(filter->chrV, -chromaSharpen);
2563         sws_addVec(filter->chrH, id);
2564         sws_addVec(filter->chrV, id);
2565         sws_freeVec(id);
2566     }
2567
2568     if (lumaSharpen!=0.0){
2569         SwsVector *id= sws_getIdentityVec();
2570         sws_scaleVec(filter->lumH, -lumaSharpen);
2571         sws_scaleVec(filter->lumV, -lumaSharpen);
2572         sws_addVec(filter->lumH, id);
2573         sws_addVec(filter->lumV, id);
2574         sws_freeVec(id);
2575     }
2576
2577     if (chromaHShift != 0.0)
2578         sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2579
2580     if (chromaVShift != 0.0)
2581         sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2582
2583     sws_normalizeVec(filter->chrH, 1.0);
2584     sws_normalizeVec(filter->chrV, 1.0);
2585     sws_normalizeVec(filter->lumH, 1.0);
2586     sws_normalizeVec(filter->lumV, 1.0);
2587
2588     if (verbose) sws_printVec(filter->chrH);
2589     if (verbose) sws_printVec(filter->lumH);
2590
2591     return filter;
2592 }
2593
2594 /**
2595  * returns a normalized gaussian curve used to filter stuff
2596  * quality=3 is high quality, lowwer is lowwer quality
2597  */
2598 SwsVector *sws_getGaussianVec(double variance, double quality){
2599     const int length= (int)(variance*quality + 0.5) | 1;
2600     int i;
2601     double *coeff= av_malloc(length*sizeof(double));
2602     double middle= (length-1)*0.5;
2603     SwsVector *vec= av_malloc(sizeof(SwsVector));
2604
2605     vec->coeff= coeff;
2606     vec->length= length;
2607
2608     for (i=0; i<length; i++)
2609     {
2610         double dist= i-middle;
2611         coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2612     }
2613
2614     sws_normalizeVec(vec, 1.0);
2615
2616     return vec;
2617 }
2618
2619 SwsVector *sws_getConstVec(double c, int length){
2620     int i;
2621     double *coeff= av_malloc(length*sizeof(double));
2622     SwsVector *vec= av_malloc(sizeof(SwsVector));
2623
2624     vec->coeff= coeff;
2625     vec->length= length;
2626
2627     for (i=0; i<length; i++)
2628         coeff[i]= c;
2629
2630     return vec;
2631 }
2632
2633
2634 SwsVector *sws_getIdentityVec(void){
2635     return sws_getConstVec(1.0, 1);
2636 }
2637
2638 double sws_dcVec(SwsVector *a){
2639     int i;
2640     double sum=0;
2641
2642     for (i=0; i<a->length; i++)
2643         sum+= a->coeff[i];
2644
2645     return sum;
2646 }
2647
2648 void sws_scaleVec(SwsVector *a, double scalar){
2649     int i;
2650
2651     for (i=0; i<a->length; i++)
2652         a->coeff[i]*= scalar;
2653 }
2654
2655 void sws_normalizeVec(SwsVector *a, double height){
2656     sws_scaleVec(a, height/sws_dcVec(a));
2657 }
2658
2659 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2660     int length= a->length + b->length - 1;
2661     double *coeff= av_malloc(length*sizeof(double));
2662     int i, j;
2663     SwsVector *vec= av_malloc(sizeof(SwsVector));
2664
2665     vec->coeff= coeff;
2666     vec->length= length;
2667
2668     for (i=0; i<length; i++) coeff[i]= 0.0;
2669
2670     for (i=0; i<a->length; i++)
2671     {
2672         for (j=0; j<b->length; j++)
2673         {
2674             coeff[i+j]+= a->coeff[i]*b->coeff[j];
2675         }
2676     }
2677
2678     return vec;
2679 }
2680
2681 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2682     int length= FFMAX(a->length, b->length);
2683     double *coeff= av_malloc(length*sizeof(double));
2684     int i;
2685     SwsVector *vec= av_malloc(sizeof(SwsVector));
2686
2687     vec->coeff= coeff;
2688     vec->length= length;
2689
2690     for (i=0; i<length; i++) coeff[i]= 0.0;
2691
2692     for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2693     for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2694
2695     return vec;
2696 }
2697
2698 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2699     int length= FFMAX(a->length, b->length);
2700     double *coeff= av_malloc(length*sizeof(double));
2701     int i;
2702     SwsVector *vec= av_malloc(sizeof(SwsVector));
2703
2704     vec->coeff= coeff;
2705     vec->length= length;
2706
2707     for (i=0; i<length; i++) coeff[i]= 0.0;
2708
2709     for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2710     for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2711
2712     return vec;
2713 }
2714
2715 /* shift left / or right if "shift" is negative */
2716 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2717     int length= a->length + FFABS(shift)*2;
2718     double *coeff= av_malloc(length*sizeof(double));
2719     int i;
2720     SwsVector *vec= av_malloc(sizeof(SwsVector));
2721
2722     vec->coeff= coeff;
2723     vec->length= length;
2724
2725     for (i=0; i<length; i++) coeff[i]= 0.0;
2726
2727     for (i=0; i<a->length; i++)
2728     {
2729         coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2730     }
2731
2732     return vec;
2733 }
2734
2735 void sws_shiftVec(SwsVector *a, int shift){
2736     SwsVector *shifted= sws_getShiftedVec(a, shift);
2737     av_free(a->coeff);
2738     a->coeff= shifted->coeff;
2739     a->length= shifted->length;
2740     av_free(shifted);
2741 }
2742
2743 void sws_addVec(SwsVector *a, SwsVector *b){
2744     SwsVector *sum= sws_sumVec(a, b);
2745     av_free(a->coeff);
2746     a->coeff= sum->coeff;
2747     a->length= sum->length;
2748     av_free(sum);
2749 }
2750
2751 void sws_subVec(SwsVector *a, SwsVector *b){
2752     SwsVector *diff= sws_diffVec(a, b);
2753     av_free(a->coeff);
2754     a->coeff= diff->coeff;
2755     a->length= diff->length;
2756     av_free(diff);
2757 }
2758
2759 void sws_convVec(SwsVector *a, SwsVector *b){
2760     SwsVector *conv= sws_getConvVec(a, b);
2761     av_free(a->coeff);
2762     a->coeff= conv->coeff;
2763     a->length= conv->length;
2764     av_free(conv);
2765 }
2766
2767 SwsVector *sws_cloneVec(SwsVector *a){
2768     double *coeff= av_malloc(a->length*sizeof(double));
2769     int i;
2770     SwsVector *vec= av_malloc(sizeof(SwsVector));
2771
2772     vec->coeff= coeff;
2773     vec->length= a->length;
2774
2775     for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2776
2777     return vec;
2778 }
2779
2780 void sws_printVec(SwsVector *a){
2781     int i;
2782     double max=0;
2783     double min=0;
2784     double range;
2785
2786     for (i=0; i<a->length; i++)
2787         if (a->coeff[i]>max) max= a->coeff[i];
2788
2789     for (i=0; i<a->length; i++)
2790         if (a->coeff[i]<min) min= a->coeff[i];
2791
2792     range= max - min;
2793
2794     for (i=0; i<a->length; i++)
2795     {
2796         int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2797         av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2798         for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2799         av_log(NULL, AV_LOG_DEBUG, "|\n");
2800     }
2801 }
2802
2803 void sws_freeVec(SwsVector *a){
2804     if (!a) return;
2805     av_free(a->coeff);
2806     a->coeff=NULL;
2807     a->length=0;
2808     av_free(a);
2809 }
2810
2811 void sws_freeFilter(SwsFilter *filter){
2812     if (!filter) return;
2813
2814     if (filter->lumH) sws_freeVec(filter->lumH);
2815     if (filter->lumV) sws_freeVec(filter->lumV);
2816     if (filter->chrH) sws_freeVec(filter->chrH);
2817     if (filter->chrV) sws_freeVec(filter->chrV);
2818     av_free(filter);
2819 }
2820
2821
2822 void sws_freeContext(SwsContext *c){
2823     int i;
2824     if (!c) return;
2825
2826     if (c->lumPixBuf)
2827     {
2828         for (i=0; i<c->vLumBufSize; i++)
2829         {
2830             av_free(c->lumPixBuf[i]);
2831             c->lumPixBuf[i]=NULL;
2832         }
2833         av_free(c->lumPixBuf);
2834         c->lumPixBuf=NULL;
2835     }
2836
2837     if (c->chrPixBuf)
2838     {
2839         for (i=0; i<c->vChrBufSize; i++)
2840         {
2841             av_free(c->chrPixBuf[i]);
2842             c->chrPixBuf[i]=NULL;
2843         }
2844         av_free(c->chrPixBuf);
2845         c->chrPixBuf=NULL;
2846     }
2847
2848     av_free(c->vLumFilter);
2849     c->vLumFilter = NULL;
2850     av_free(c->vChrFilter);
2851     c->vChrFilter = NULL;
2852     av_free(c->hLumFilter);
2853     c->hLumFilter = NULL;
2854     av_free(c->hChrFilter);
2855     c->hChrFilter = NULL;
2856 #ifdef HAVE_ALTIVEC
2857     av_free(c->vYCoeffsBank);
2858     c->vYCoeffsBank = NULL;
2859     av_free(c->vCCoeffsBank);
2860     c->vCCoeffsBank = NULL;
2861 #endif
2862
2863     av_free(c->vLumFilterPos);
2864     c->vLumFilterPos = NULL;
2865     av_free(c->vChrFilterPos);
2866     c->vChrFilterPos = NULL;
2867     av_free(c->hLumFilterPos);
2868     c->hLumFilterPos = NULL;
2869     av_free(c->hChrFilterPos);
2870     c->hChrFilterPos = NULL;
2871
2872 #if defined(ARCH_X86) && defined(CONFIG_GPL)
2873 #ifdef MAP_ANONYMOUS
2874     if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2875     if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2876 #else
2877     av_free(c->funnyYCode);
2878     av_free(c->funnyUVCode);
2879 #endif
2880     c->funnyYCode=NULL;
2881     c->funnyUVCode=NULL;
2882 #endif /* defined(ARCH_X86) */
2883
2884     av_free(c->lumMmx2Filter);
2885     c->lumMmx2Filter=NULL;
2886     av_free(c->chrMmx2Filter);
2887     c->chrMmx2Filter=NULL;
2888     av_free(c->lumMmx2FilterPos);
2889     c->lumMmx2FilterPos=NULL;
2890     av_free(c->chrMmx2FilterPos);
2891     c->chrMmx2FilterPos=NULL;
2892     av_free(c->yuvTable);
2893     c->yuvTable=NULL;
2894
2895     av_free(c);
2896 }
2897
2898 /**
2899  * Checks if context is valid or reallocs a new one instead.
2900  * If context is NULL, just calls sws_getContext() to get a new one.
2901  * Otherwise, checks if the parameters are the same already saved in context.
2902  * If that is the case, returns the current context.
2903  * Otherwise, frees context and gets a new one.
2904  *
2905  * Be warned that srcFilter, dstFilter are not checked, they are
2906  * asumed to remain valid.
2907  */
2908 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
2909                                         int srcW, int srcH, int srcFormat,
2910                                         int dstW, int dstH, int dstFormat, int flags,
2911                                         SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
2912 {
2913     if (context != NULL) {
2914         if ((context->srcW != srcW) || (context->srcH != srcH) ||
2915             (context->srcFormat != srcFormat) ||
2916             (context->dstW != dstW) || (context->dstH != dstH) ||
2917             (context->dstFormat != dstFormat) || (context->flags != flags) ||
2918             (context->param != param))
2919         {
2920             sws_freeContext(context);
2921             context = NULL;
2922         }
2923     }
2924     if (context == NULL) {
2925         return sws_getContext(srcW, srcH, srcFormat,
2926                               dstW, dstH, dstFormat, flags,
2927                               srcFilter, dstFilter, param);
2928     }
2929     return context;
2930 }
2931