]> git.sesse.net Git - ffmpeg/blob - postproc/swscale.c
sync with mplayer xp
[ffmpeg] / postproc / swscale.c
1 /*
2     Copyright (C) 2001-2002 Michael Niedermayer <michaelni@gmx.at>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /*
20   supported Input formats: YV12, I420, IYUV, YUY2, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
21   supported output formats: YV12, I420, IYUV, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
22   BGR15/16 support dithering
23   
24   unscaled special converters
25   YV12/I420/IYUV -> BGR15/BGR16/BGR24/BGR32
26   YV12/I420/IYUV -> YV12/I420/IYUV
27   YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
28   BGR24 -> BGR32 & RGB24 -> RGB32
29   BGR32 -> BGR24 & RGB32 -> RGB24
30   BGR15 -> BGR16
31 */
32
33 /* 
34 tested special converters
35  YV12/I420 -> BGR16
36  YV12 -> YV12
37  BGR15 -> BGR16
38  BGR16 -> BGR16
39
40 untested special converters
41   YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
42   YV12/I420 -> YV12/I420
43   YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
44   BGR24 -> BGR32 & RGB24 -> RGB32
45   BGR32 -> BGR24 & RGB32 -> RGB24
46   BGR24 -> YV12
47 */
48
49 #include <inttypes.h>
50 #include <string.h>
51 #include <math.h>
52 #include <stdio.h>
53 #include "../config.h"
54 #include "../mangle.h"
55 #include <assert.h>
56 #ifdef HAVE_MALLOC_H
57 #include <malloc.h>
58 #else
59 #include <stdlib.h>
60 #endif
61 #include "swscale.h"
62 #include "../cpudetect.h"
63 #include "../bswap.h"
64 #include "../libvo/img_format.h"
65 #include "rgb2rgb.h"
66 #include "../libvo/fastmemcpy.h"
67 #include "../mp_msg.h"
68
69 #define MSG_WARN(args...) mp_msg(MSGT_SWS,MSGL_WARN, ##args )
70 #define MSG_FATAL(args...) mp_msg(MSGT_SWS,MSGL_FATAL, ##args )
71 #define MSG_ERR(args...) mp_msg(MSGT_SWS,MSGL_ERR, ##args )
72 #define MSG_V(args...) mp_msg(MSGT_SWS,MSGL_V, ##args )
73 #define MSG_DBG2(args...) mp_msg(MSGT_SWS,MSGL_DBG2, ##args )
74 #define MSG_INFO(args...) mp_msg(MSGT_SWS,MSGL_INFO, ##args )
75
76 #undef MOVNTQ
77 #undef PAVGB
78
79 //#undef HAVE_MMX2
80 //#define HAVE_3DNOW
81 //#undef HAVE_MMX
82 //#undef ARCH_X86
83 //#define WORDS_BIGENDIAN
84 #define DITHER1XBPP
85
86 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
87
88 #define RET 0xC3 //near return opcode for X86
89
90 #ifdef MP_DEBUG
91 #define ASSERT(x) assert(x);
92 #else
93 #define ASSERT(x) ;
94 #endif
95
96 #ifdef M_PI
97 #define PI M_PI
98 #else
99 #define PI 3.14159265358979323846
100 #endif
101
102 //FIXME replace this with something faster
103 #define isBGR(x)       ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
104 #define isRGB(x)       ((x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24|| (x)==IMGFMT_RGB16|| (x)==IMGFMT_RGB15)
105 #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_IYUV|| (x)==IMGFMT_YVU9 || (x)==IMGFMT_IF09)
106 #define isYUV(x)       (!(isBGR(x) || isRGB(x)))
107 #define isHalfChrV(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_IYUV)
108 #define isHalfChrH(x)  ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
109 #define isPacked(x)    (isYUV(x) && !isPlanarYUV(x))
110 #define isGray(x)      ((x)==IMGFMT_Y800) /* Behaviour the same as PACKED but it's PLANAR */
111 #define isSupportedIn(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
112                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
113                         || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
114                         || (x)==IMGFMT_Y800)
115 #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
116                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
117 #define isSupportedUnscaledIn(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_NV12 \
118                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
119                         || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
120                         || (x)==IMGFMT_Y800)
121 #define isSupportedUnscaledOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x) == IMGFMT_YUY2 \
122                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
123
124 #define RGB2YUV_SHIFT 16
125 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
126 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
127 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
128 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
129 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
130 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
131 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
132 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
133 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
134
135 extern int verbose; // defined in mplayer.c
136 /*
137 NOTES
138 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
139
140 TODO
141 more intelligent missalignment avoidance for the horizontal scaler
142 write special vertical cubic upscale version
143 Optimize C code (yv12 / minmax)
144 add support for packed pixel yuv input & output
145 add support for Y8 output
146 optimize bgr24 & bgr32
147 add BGR4 output support
148 write special BGR->BGR scaler
149 deglobalize yuv2rgb*.c
150 */
151
152 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
153 #define MIN(a,b) ((a) > (b) ? (b) : (a))
154 #define MAX(a,b) ((a) < (b) ? (b) : (a))
155
156 #ifdef ARCH_X86
157 #define CAN_COMPILE_X86_ASM
158 #endif
159
160 #ifdef CAN_COMPILE_X86_ASM
161 static uint64_t __attribute__((aligned(8))) yCoeff=    0x2568256825682568LL;
162 static uint64_t __attribute__((aligned(8))) vrCoeff=   0x3343334333433343LL;
163 static uint64_t __attribute__((aligned(8))) ubCoeff=   0x40cf40cf40cf40cfLL;
164 static uint64_t __attribute__((aligned(8))) vgCoeff=   0xE5E2E5E2E5E2E5E2LL;
165 static uint64_t __attribute__((aligned(8))) ugCoeff=   0xF36EF36EF36EF36ELL;
166 static uint64_t __attribute__((aligned(8))) bF8=       0xF8F8F8F8F8F8F8F8LL;
167 static uint64_t __attribute__((aligned(8))) bFC=       0xFCFCFCFCFCFCFCFCLL;
168 static uint64_t __attribute__((aligned(8))) w400=      0x0400040004000400LL;
169 static uint64_t __attribute__((aligned(8))) w80=       0x0080008000800080LL;
170 static uint64_t __attribute__((aligned(8))) w10=       0x0010001000100010LL;
171 static uint64_t __attribute__((aligned(8))) w02=       0x0002000200020002LL;
172 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
173 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
174 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
175 static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
176
177 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
178 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
179 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
180 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
181
182 static uint64_t __attribute__((aligned(8))) dither4[2]={
183         0x0103010301030103LL,
184         0x0200020002000200LL,};
185
186 static uint64_t __attribute__((aligned(8))) dither8[2]={
187         0x0602060206020602LL,
188         0x0004000400040004LL,};
189
190 static uint64_t __attribute__((aligned(8))) b16Mask=   0x001F001F001F001FLL;
191 static uint64_t __attribute__((aligned(8))) g16Mask=   0x07E007E007E007E0LL;
192 static uint64_t __attribute__((aligned(8))) r16Mask=   0xF800F800F800F800LL;
193 static uint64_t __attribute__((aligned(8))) b15Mask=   0x001F001F001F001FLL;
194 static uint64_t __attribute__((aligned(8))) g15Mask=   0x03E003E003E003E0LL;
195 static uint64_t __attribute__((aligned(8))) r15Mask=   0x7C007C007C007C00LL;
196
197 static uint64_t __attribute__((aligned(8))) M24A=   0x00FF0000FF0000FFLL;
198 static uint64_t __attribute__((aligned(8))) M24B=   0xFF0000FF0000FF00LL;
199 static uint64_t __attribute__((aligned(8))) M24C=   0x0000FF0000FF0000LL;
200
201 #ifdef FAST_BGR2YV12
202 static const uint64_t bgr2YCoeff  __attribute__((aligned(8))) = 0x000000210041000DULL;
203 static const uint64_t bgr2UCoeff  __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
204 static const uint64_t bgr2VCoeff  __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
205 #else
206 static const uint64_t bgr2YCoeff  __attribute__((aligned(8))) = 0x000020E540830C8BULL;
207 static const uint64_t bgr2UCoeff  __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
208 static const uint64_t bgr2VCoeff  __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
209 #endif
210 static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
211 static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
212 static const uint64_t w1111       __attribute__((aligned(8))) = 0x0001000100010001ULL;
213
214 // FIXME remove
215 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
216 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
217 #endif
218
219 // clipping helper table for C implementations:
220 static unsigned char clip_table[768];
221
222 static unsigned short clip_table16b[768];
223 static unsigned short clip_table16g[768];
224 static unsigned short clip_table16r[768];
225 static unsigned short clip_table15b[768];
226 static unsigned short clip_table15g[768];
227 static unsigned short clip_table15r[768];
228
229 // yuv->rgb conversion tables:
230 static    int yuvtab_2568[256];
231 static    int yuvtab_3343[256];
232 static    int yuvtab_0c92[256];
233 static    int yuvtab_1a1e[256];
234 static    int yuvtab_40cf[256];
235 // Needed for cubic scaler to catch overflows
236 static    int clip_yuvtab_2568[768];
237 static    int clip_yuvtab_3343[768];
238 static    int clip_yuvtab_0c92[768];
239 static    int clip_yuvtab_1a1e[768];
240 static    int clip_yuvtab_40cf[768];
241
242 //global sws_flags from the command line
243 int sws_flags=2;
244
245 //global srcFilter
246 SwsFilter src_filter= {NULL, NULL, NULL, NULL};
247
248 float sws_lum_gblur= 0.0;
249 float sws_chr_gblur= 0.0;
250 int sws_chr_vshift= 0;
251 int sws_chr_hshift= 0;
252 float sws_chr_sharpen= 0.0;
253 float sws_lum_sharpen= 0.0;
254
255 /* cpuCaps combined from cpudetect and whats actually compiled in
256    (if there is no support for something compiled in it wont appear here) */
257 static CpuCaps cpuCaps;
258
259 void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
260              int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
261
262 static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
263
264 #ifdef CAN_COMPILE_X86_ASM
265 void in_asm_used_var_warning_killer()
266 {
267  volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
268  bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
269  M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
270  if(i) i=0;
271 }
272 #endif
273
274 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
275                                     int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
276                                     uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
277 {
278         //FIXME Optimize (just quickly writen not opti..)
279         int i;
280         for(i=0; i<dstW; i++)
281         {
282                 int val=0;
283                 int j;
284                 for(j=0; j<lumFilterSize; j++)
285                         val += lumSrc[j][i] * lumFilter[j];
286
287                 dest[i]= MIN(MAX(val>>19, 0), 255);
288         }
289
290         if(uDest != NULL)
291                 for(i=0; i<(dstW>>1); i++)
292                 {
293                         int u=0;
294                         int v=0;
295                         int j;
296                         for(j=0; j<chrFilterSize; j++)
297                         {
298                                 u += chrSrc[j][i] * chrFilter[j];
299                                 v += chrSrc[j][i + 2048] * chrFilter[j];
300                         }
301
302                         uDest[i]= MIN(MAX(u>>19, 0), 255);
303                         vDest[i]= MIN(MAX(v>>19, 0), 255);
304                 }
305 }
306
307 static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
308                                     int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
309                                     uint8_t *dest, int dstW, int dstFormat)
310 {
311         if(dstFormat==IMGFMT_BGR32)
312         {
313                 int i;
314 #ifdef WORDS_BIGENDIAN
315         dest++;
316 #endif
317                 for(i=0; i<(dstW>>1); i++){
318                         int j;
319                         int Y1=0;
320                         int Y2=0;
321                         int U=0;
322                         int V=0;
323                         int Cb, Cr, Cg;
324                         for(j=0; j<lumFilterSize; j++)
325                         {
326                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
327                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
328                         }
329                         for(j=0; j<chrFilterSize; j++)
330                         {
331                                 U += chrSrc[j][i] * chrFilter[j];
332                                 V += chrSrc[j][i+2048] * chrFilter[j];
333                         }
334                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
335                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
336                         U >>= 19;
337                         V >>= 19;
338
339                         Cb= clip_yuvtab_40cf[U+ 256];
340                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
341                         Cr= clip_yuvtab_3343[V+ 256];
342
343                         dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
344                         dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
345                         dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
346
347                         dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
348                         dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
349                         dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
350                 }
351         }
352         else if(dstFormat==IMGFMT_BGR24)
353         {
354                 int i;
355                 for(i=0; i<(dstW>>1); i++){
356                         int j;
357                         int Y1=0;
358                         int Y2=0;
359                         int U=0;
360                         int V=0;
361                         int Cb, Cr, Cg;
362                         for(j=0; j<lumFilterSize; j++)
363                         {
364                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
365                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
366                         }
367                         for(j=0; j<chrFilterSize; j++)
368                         {
369                                 U += chrSrc[j][i] * chrFilter[j];
370                                 V += chrSrc[j][i+2048] * chrFilter[j];
371                         }
372                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
373                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
374                         U >>= 19;
375                         V >>= 19;
376
377                         Cb= clip_yuvtab_40cf[U+ 256];
378                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
379                         Cr= clip_yuvtab_3343[V+ 256];
380
381                         dest[0]=clip_table[((Y1 + Cb) >>13)];
382                         dest[1]=clip_table[((Y1 + Cg) >>13)];
383                         dest[2]=clip_table[((Y1 + Cr) >>13)];
384
385                         dest[3]=clip_table[((Y2 + Cb) >>13)];
386                         dest[4]=clip_table[((Y2 + Cg) >>13)];
387                         dest[5]=clip_table[((Y2 + Cr) >>13)];
388                         dest+=6;
389                 }
390         }
391         else if(dstFormat==IMGFMT_BGR16)
392         {
393                 int i;
394 #ifdef DITHER1XBPP
395                 static int ditherb1=1<<14;
396                 static int ditherg1=1<<13;
397                 static int ditherr1=2<<14;
398                 static int ditherb2=3<<14;
399                 static int ditherg2=3<<13;
400                 static int ditherr2=0<<14;
401
402                 ditherb1 ^= (1^2)<<14;
403                 ditherg1 ^= (1^2)<<13;
404                 ditherr1 ^= (1^2)<<14;
405                 ditherb2 ^= (3^0)<<14;
406                 ditherg2 ^= (3^0)<<13;
407                 ditherr2 ^= (3^0)<<14;
408 #else
409                 const int ditherb1=0;
410                 const int ditherg1=0;
411                 const int ditherr1=0;
412                 const int ditherb2=0;
413                 const int ditherg2=0;
414                 const int ditherr2=0;
415 #endif
416                 for(i=0; i<(dstW>>1); i++){
417                         int j;
418                         int Y1=0;
419                         int Y2=0;
420                         int U=0;
421                         int V=0;
422                         int Cb, Cr, Cg;
423                         for(j=0; j<lumFilterSize; j++)
424                         {
425                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
426                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
427                         }
428                         for(j=0; j<chrFilterSize; j++)
429                         {
430                                 U += chrSrc[j][i] * chrFilter[j];
431                                 V += chrSrc[j][i+2048] * chrFilter[j];
432                         }
433                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
434                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
435                         U >>= 19;
436                         V >>= 19;
437
438                         Cb= clip_yuvtab_40cf[U+ 256];
439                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
440                         Cr= clip_yuvtab_3343[V+ 256];
441
442                         ((uint16_t*)dest)[2*i] =
443                                 clip_table16b[(Y1 + Cb + ditherb1) >>13] |
444                                 clip_table16g[(Y1 + Cg + ditherg1) >>13] |
445                                 clip_table16r[(Y1 + Cr + ditherr1) >>13];
446
447                         ((uint16_t*)dest)[2*i+1] =
448                                 clip_table16b[(Y2 + Cb + ditherb2) >>13] |
449                                 clip_table16g[(Y2 + Cg + ditherg2) >>13] |
450                                 clip_table16r[(Y2 + Cr + ditherr2) >>13];
451                 }
452         }
453         else if(dstFormat==IMGFMT_BGR15)
454         {
455                 int i;
456 #ifdef DITHER1XBPP
457                 static int ditherb1=1<<14;
458                 static int ditherg1=1<<14;
459                 static int ditherr1=2<<14;
460                 static int ditherb2=3<<14;
461                 static int ditherg2=3<<14;
462                 static int ditherr2=0<<14;
463
464                 ditherb1 ^= (1^2)<<14;
465                 ditherg1 ^= (1^2)<<14;
466                 ditherr1 ^= (1^2)<<14;
467                 ditherb2 ^= (3^0)<<14;
468                 ditherg2 ^= (3^0)<<14;
469                 ditherr2 ^= (3^0)<<14;
470 #else
471                 const int ditherb1=0;
472                 const int ditherg1=0;
473                 const int ditherr1=0;
474                 const int ditherb2=0;
475                 const int ditherg2=0;
476                 const int ditherr2=0;
477 #endif
478                 for(i=0; i<(dstW>>1); i++){
479                         int j;
480                         int Y1=0;
481                         int Y2=0;
482                         int U=0;
483                         int V=0;
484                         int Cb, Cr, Cg;
485                         for(j=0; j<lumFilterSize; j++)
486                         {
487                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
488                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
489                         }
490                         for(j=0; j<chrFilterSize; j++)
491                         {
492                                 U += chrSrc[j][i] * chrFilter[j];
493                                 V += chrSrc[j][i+2048] * chrFilter[j];
494                         }
495                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
496                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
497                         U >>= 19;
498                         V >>= 19;
499
500                         Cb= clip_yuvtab_40cf[U+ 256];
501                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
502                         Cr= clip_yuvtab_3343[V+ 256];
503
504                         ((uint16_t*)dest)[2*i] =
505                                 clip_table15b[(Y1 + Cb + ditherb1) >>13] |
506                                 clip_table15g[(Y1 + Cg + ditherg1) >>13] |
507                                 clip_table15r[(Y1 + Cr + ditherr1) >>13];
508
509                         ((uint16_t*)dest)[2*i+1] =
510                                 clip_table15b[(Y2 + Cb + ditherb2) >>13] |
511                                 clip_table15g[(Y2 + Cg + ditherg2) >>13] |
512                                 clip_table15r[(Y2 + Cr + ditherr2) >>13];
513                 }
514         }
515 }
516
517
518 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
519 //Plain C versions
520 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
521 #define COMPILE_C
522 #endif
523
524 #ifdef CAN_COMPILE_X86_ASM
525
526 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
527 #define COMPILE_MMX
528 #endif
529
530 #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
531 #define COMPILE_MMX2
532 #endif
533
534 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
535 #define COMPILE_3DNOW
536 #endif
537 #endif //CAN_COMPILE_X86_ASM
538
539 #undef HAVE_MMX
540 #undef HAVE_MMX2
541 #undef HAVE_3DNOW
542
543 #ifdef COMPILE_C
544 #undef HAVE_MMX
545 #undef HAVE_MMX2
546 #undef HAVE_3DNOW
547 #define RENAME(a) a ## _C
548 #include "swscale_template.c"
549 #endif
550
551 #ifdef CAN_COMPILE_X86_ASM
552
553 //X86 versions
554 /*
555 #undef RENAME
556 #undef HAVE_MMX
557 #undef HAVE_MMX2
558 #undef HAVE_3DNOW
559 #define ARCH_X86
560 #define RENAME(a) a ## _X86
561 #include "swscale_template.c"
562 */
563 //MMX versions
564 #ifdef COMPILE_MMX
565 #undef RENAME
566 #define HAVE_MMX
567 #undef HAVE_MMX2
568 #undef HAVE_3DNOW
569 #define RENAME(a) a ## _MMX
570 #include "swscale_template.c"
571 #endif
572
573 //MMX2 versions
574 #ifdef COMPILE_MMX2
575 #undef RENAME
576 #define HAVE_MMX
577 #define HAVE_MMX2
578 #undef HAVE_3DNOW
579 #define RENAME(a) a ## _MMX2
580 #include "swscale_template.c"
581 #endif
582
583 //3DNOW versions
584 #ifdef COMPILE_3DNOW
585 #undef RENAME
586 #define HAVE_MMX
587 #undef HAVE_MMX2
588 #define HAVE_3DNOW
589 #define RENAME(a) a ## _3DNow
590 #include "swscale_template.c"
591 #endif
592
593 #endif //CAN_COMPILE_X86_ASM
594
595 // minor note: the HAVE_xyz is messed up after that line so dont use it
596
597
598 // old global scaler, dont use for new code
599 // will use sws_flags from the command line
600 void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
601                              int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
602                              int srcW, int srcH, int dstW, int dstH){
603
604         static SwsContext *context=NULL;
605         int dstFormat;
606         int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
607
608         switch(dstbpp)
609         {
610                 case 8 : dstFormat= IMGFMT_Y8;          break;
611                 case 12: dstFormat= IMGFMT_YV12;        break;
612                 case 15: dstFormat= IMGFMT_BGR15;       break;
613                 case 16: dstFormat= IMGFMT_BGR16;       break;
614                 case 24: dstFormat= IMGFMT_BGR24;       break;
615                 case 32: dstFormat= IMGFMT_BGR32;       break;
616                 default: return;
617         }
618
619         if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
620
621         context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
622 }
623
624 // will use sws_flags & src_filter (from cmd line)
625 SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
626 {
627         int flags=0;
628         static int firstTime=1;
629
630 #ifdef ARCH_X86
631         if(gCpuCaps.hasMMX)
632                 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
633 #endif
634         if(firstTime)
635         {
636                 firstTime=0;
637                 flags= SWS_PRINT_INFO;
638         }
639         else if(verbose>1) flags= SWS_PRINT_INFO;
640
641         if(src_filter.lumH) freeVec(src_filter.lumH);
642         if(src_filter.lumV) freeVec(src_filter.lumV);
643         if(src_filter.chrH) freeVec(src_filter.chrH);
644         if(src_filter.chrV) freeVec(src_filter.chrV);
645
646         if(sws_lum_gblur!=0.0){
647                 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
648                 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
649         }else{
650                 src_filter.lumH= getIdentityVec();
651                 src_filter.lumV= getIdentityVec();
652         }
653
654         if(sws_chr_gblur!=0.0){
655                 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
656                 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
657         }else{
658                 src_filter.chrH= getIdentityVec();
659                 src_filter.chrV= getIdentityVec();
660         }
661
662         if(sws_chr_sharpen!=0.0){
663                 SwsVector *g= getConstVec(-1.0, 3);
664                 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
665                 g->coeff[1]=2.0;
666                 addVec(id, g);
667                 convVec(src_filter.chrH, id);
668                 convVec(src_filter.chrV, id);
669                 freeVec(g);
670                 freeVec(id);
671         }
672
673         if(sws_lum_sharpen!=0.0){
674                 SwsVector *g= getConstVec(-1.0, 3);
675                 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
676                 g->coeff[1]=2.0;
677                 addVec(id, g);
678                 convVec(src_filter.lumH, id);
679                 convVec(src_filter.lumV, id);
680                 freeVec(g);
681                 freeVec(id);
682         }
683
684         if(sws_chr_hshift)
685                 shiftVec(src_filter.chrH, sws_chr_hshift);
686
687         if(sws_chr_vshift)
688                 shiftVec(src_filter.chrV, sws_chr_vshift);
689
690         normalizeVec(src_filter.chrH, 1.0);
691         normalizeVec(src_filter.chrV, 1.0);
692         normalizeVec(src_filter.lumH, 1.0);
693         normalizeVec(src_filter.lumV, 1.0);
694
695         if(verbose > 1) printVec(src_filter.chrH);
696         if(verbose > 1) printVec(src_filter.lumH);
697
698         switch(sws_flags)
699         {
700                 case 0: flags|= SWS_FAST_BILINEAR; break;
701                 case 1: flags|= SWS_BILINEAR; break;
702                 case 2: flags|= SWS_BICUBIC; break;
703                 case 3: flags|= SWS_X; break;
704                 case 4: flags|= SWS_POINT; break;
705                 case 5: flags|= SWS_AREA; break;
706                 default:flags|= SWS_BILINEAR; break;
707         }
708
709         return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
710 }
711
712
713 static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
714                               int srcW, int dstW, int filterAlign, int one, int flags,
715                               SwsVector *srcFilter, SwsVector *dstFilter)
716 {
717         int i;
718         int filterSize;
719         int filter2Size;
720         int minFilterSize;
721         double *filter=NULL;
722         double *filter2=NULL;
723 #ifdef ARCH_X86
724         if(gCpuCaps.hasMMX)
725                 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
726 #endif
727
728         // Note the +1 is for the MMXscaler which reads over the end
729         *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
730
731         if(ABS(xInc - 0x10000) <10) // unscaled
732         {
733                 int i;
734                 filterSize= 1;
735                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
736                 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
737
738                 for(i=0; i<dstW; i++)
739                 {
740                         filter[i*filterSize]=1;
741                         (*filterPos)[i]=i;
742                 }
743
744         }
745         else if(flags&SWS_POINT) // lame looking point sampling mode
746         {
747                 int i;
748                 int xDstInSrc;
749                 filterSize= 1;
750                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
751                 
752                 xDstInSrc= xInc/2 - 0x8000;
753                 for(i=0; i<dstW; i++)
754                 {
755                         int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
756
757                         (*filterPos)[i]= xx;
758                         filter[i]= 1.0;
759                         xDstInSrc+= xInc;
760                 }
761         }
762         else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
763         {
764                 int i;
765                 int xDstInSrc;
766                 if     (flags&SWS_BICUBIC) filterSize= 4;
767                 else if(flags&SWS_X      ) filterSize= 4;
768                 else                       filterSize= 2; // SWS_BILINEAR / SWS_AREA 
769                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
770
771                 xDstInSrc= xInc/2 - 0x8000;
772                 for(i=0; i<dstW; i++)
773                 {
774                         int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
775                         int j;
776
777                         (*filterPos)[i]= xx;
778                         if((flags & SWS_BICUBIC) || (flags & SWS_X))
779                         {
780                                 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
781                                 double y1,y2,y3,y4;
782                                 double A= -0.6;
783                                 if(flags & SWS_BICUBIC){
784                                                 // Equation is from VirtualDub
785                                         y1 = (        +     A*d -       2.0*A*d*d +       A*d*d*d);
786                                         y2 = (+ 1.0             -     (A+3.0)*d*d + (A+2.0)*d*d*d);
787                                         y3 = (        -     A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
788                                         y4 = (                  +           A*d*d -       A*d*d*d);
789                                 }else{
790                                                 // cubic interpolation (derived it myself)
791                                         y1 = (    -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
792                                         y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
793                                         y3 = (    +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
794                                         y4 = (    -1.0*d           + 1.0*d*d*d)/6.0;
795                                 }
796
797                                 filter[i*filterSize + 0]= y1;
798                                 filter[i*filterSize + 1]= y2;
799                                 filter[i*filterSize + 2]= y3;
800                                 filter[i*filterSize + 3]= y4;
801                         }
802                         else
803                         {
804                                 //Bilinear upscale / linear interpolate / Area averaging
805                                 for(j=0; j<filterSize; j++)
806                                 {
807                                         double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
808                                         double coeff= 1.0 - d;
809                                         if(coeff<0) coeff=0;
810                                         filter[i*filterSize + j]= coeff;
811                                         xx++;
812                                 }
813                         }
814                         xDstInSrc+= xInc;
815                 }
816         }
817         else // downscale
818         {
819                 int xDstInSrc;
820                 ASSERT(dstW <= srcW)
821
822                 if(flags&SWS_BICUBIC)   filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
823                 else if(flags&SWS_X)    filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
824                 else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
825                 else /* BILINEAR */     filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
826                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
827
828                 xDstInSrc= xInc/2 - 0x8000;
829                 for(i=0; i<dstW; i++)
830                 {
831                         int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
832                         int j;
833                         (*filterPos)[i]= xx;
834                         for(j=0; j<filterSize; j++)
835                         {
836                                 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
837                                 double coeff;
838                                 if((flags & SWS_BICUBIC) || (flags & SWS_X))
839                                 {
840                                         double A= -0.75;
841 //                                      d*=2;
842                                         // Equation is from VirtualDub
843                                         if(d<1.0)
844                                                 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
845                                         else if(d<2.0)
846                                                 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
847                                         else
848                                                 coeff=0.0;
849                                 }
850                                 else if(flags & SWS_AREA)
851                                 {
852                                         double srcPixelSize= (1<<16)/(double)xInc;
853                                         if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
854                                         else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
855                                         else coeff=0.0;
856                                 }
857                                 else
858                                 {
859                                         coeff= 1.0 - d;
860                                         if(coeff<0) coeff=0;
861                                 }
862                                 filter[i*filterSize + j]= coeff;
863                                 xx++;
864                         }
865                         xDstInSrc+= xInc;
866                 }
867         }
868
869         /* apply src & dst Filter to filter -> filter2
870            free(filter);
871         */
872         ASSERT(filterSize>0)
873         filter2Size= filterSize;
874         if(srcFilter) filter2Size+= srcFilter->length - 1;
875         if(dstFilter) filter2Size+= dstFilter->length - 1;
876         ASSERT(filter2Size>0)
877         filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
878
879         for(i=0; i<dstW; i++)
880         {
881                 int j;
882                 SwsVector scaleFilter;
883                 SwsVector *outVec;
884
885                 scaleFilter.coeff= filter + i*filterSize;
886                 scaleFilter.length= filterSize;
887
888                 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
889                 else          outVec= &scaleFilter;
890
891                 ASSERT(outVec->length == filter2Size)
892                 //FIXME dstFilter
893
894                 for(j=0; j<outVec->length; j++)
895                 {
896                         filter2[i*filter2Size + j]= outVec->coeff[j];
897                 }
898
899                 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
900
901                 if(outVec != &scaleFilter) freeVec(outVec);
902         }
903         free(filter); filter=NULL;
904
905         /* try to reduce the filter-size (step1 find size and shift left) */
906         // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
907         minFilterSize= 0;
908         for(i=dstW-1; i>=0; i--)
909         {
910                 int min= filter2Size;
911                 int j;
912                 double cutOff=0.0;
913
914                 /* get rid off near zero elements on the left by shifting left */
915                 for(j=0; j<filter2Size; j++)
916                 {
917                         int k;
918                         cutOff += ABS(filter2[i*filter2Size]);
919
920                         if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
921
922                         /* preserve Monotonicity because the core cant handle the filter otherwise */
923                         if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
924
925                         // Move filter coeffs left
926                         for(k=1; k<filter2Size; k++)
927                                 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
928                         filter2[i*filter2Size + k - 1]= 0.0;
929                         (*filterPos)[i]++;
930                 }
931
932                 cutOff=0.0;
933                 /* count near zeros on the right */
934                 for(j=filter2Size-1; j>0; j--)
935                 {
936                         cutOff += ABS(filter2[i*filter2Size + j]);
937
938                         if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
939                         min--;
940                 }
941
942                 if(min>minFilterSize) minFilterSize= min;
943         }
944
945         ASSERT(minFilterSize > 0)
946         filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
947         ASSERT(filterSize > 0)
948         filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
949         *outFilterSize= filterSize;
950
951         if(flags&SWS_PRINT_INFO)
952                 MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
953         /* try to reduce the filter-size (step2 reduce it) */
954         for(i=0; i<dstW; i++)
955         {
956                 int j;
957
958                 for(j=0; j<filterSize; j++)
959                 {
960                         if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
961                         else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
962                 }
963         }
964         free(filter2); filter2=NULL;
965         
966
967         //FIXME try to align filterpos if possible
968
969         //fix borders
970         for(i=0; i<dstW; i++)
971         {
972                 int j;
973                 if((*filterPos)[i] < 0)
974                 {
975                         // Move filter coeffs left to compensate for filterPos
976                         for(j=1; j<filterSize; j++)
977                         {
978                                 int left= MAX(j + (*filterPos)[i], 0);
979                                 filter[i*filterSize + left] += filter[i*filterSize + j];
980                                 filter[i*filterSize + j]=0;
981                         }
982                         (*filterPos)[i]= 0;
983                 }
984
985                 if((*filterPos)[i] + filterSize > srcW)
986                 {
987                         int shift= (*filterPos)[i] + filterSize - srcW;
988                         // Move filter coeffs right to compensate for filterPos
989                         for(j=filterSize-2; j>=0; j--)
990                         {
991                                 int right= MIN(j + shift, filterSize-1);
992                                 filter[i*filterSize +right] += filter[i*filterSize +j];
993                                 filter[i*filterSize +j]=0;
994                         }
995                         (*filterPos)[i]= srcW - filterSize;
996                 }
997         }
998
999         // Note the +1 is for the MMXscaler which reads over the end
1000         *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
1001         memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
1002
1003         /* Normalize & Store in outFilter */
1004         for(i=0; i<dstW; i++)
1005         {
1006                 int j;
1007                 double sum=0;
1008                 double scale= one;
1009                 for(j=0; j<filterSize; j++)
1010                 {
1011                         sum+= filter[i*filterSize + j];
1012                 }
1013                 scale/= sum;
1014                 for(j=0; j<filterSize; j++)
1015                 {
1016                         (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
1017                 }
1018         }
1019         
1020         (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1021         for(i=0; i<*outFilterSize; i++)
1022         {
1023                 int j= dstW*(*outFilterSize);
1024                 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1025         }
1026
1027         free(filter);
1028 }
1029
1030 #ifdef ARCH_X86
1031 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1032 {
1033         uint8_t *fragmentA;
1034         int imm8OfPShufW1A;
1035         int imm8OfPShufW2A;
1036         int fragmentLengthA;
1037         uint8_t *fragmentB;
1038         int imm8OfPShufW1B;
1039         int imm8OfPShufW2B;
1040         int fragmentLengthB;
1041         int fragmentPos;
1042
1043         int xpos, i;
1044
1045         // create an optimized horizontal scaling routine
1046
1047         //code fragment
1048
1049         asm volatile(
1050                 "jmp 9f                         \n\t"
1051         // Begin
1052                 "0:                             \n\t"
1053                 "movq (%%edx, %%eax), %%mm3     \n\t" 
1054                 "movd (%%ecx, %%esi), %%mm0     \n\t" 
1055                 "movd 1(%%ecx, %%esi), %%mm1    \n\t"
1056                 "punpcklbw %%mm7, %%mm1         \n\t"
1057                 "punpcklbw %%mm7, %%mm0         \n\t"
1058                 "pshufw $0xFF, %%mm1, %%mm1     \n\t"
1059                 "1:                             \n\t"
1060                 "pshufw $0xFF, %%mm0, %%mm0     \n\t"
1061                 "2:                             \n\t"
1062                 "psubw %%mm1, %%mm0             \n\t"
1063                 "movl 8(%%ebx, %%eax), %%esi    \n\t"
1064                 "pmullw %%mm3, %%mm0            \n\t"
1065                 "psllw $7, %%mm1                \n\t"
1066                 "paddw %%mm1, %%mm0             \n\t"
1067
1068                 "movq %%mm0, (%%edi, %%eax)     \n\t"
1069
1070                 "addl $8, %%eax                 \n\t"
1071         // End
1072                 "9:                             \n\t"
1073 //              "int $3\n\t"
1074                 "leal 0b, %0                    \n\t"
1075                 "leal 1b, %1                    \n\t"
1076                 "leal 2b, %2                    \n\t"
1077                 "decl %1                        \n\t"
1078                 "decl %2                        \n\t"
1079                 "subl %0, %1                    \n\t"
1080                 "subl %0, %2                    \n\t"
1081                 "leal 9b, %3                    \n\t"
1082                 "subl %0, %3                    \n\t"
1083
1084
1085                 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1086                 "=r" (fragmentLengthA)
1087         );
1088
1089         asm volatile(
1090                 "jmp 9f                         \n\t"
1091         // Begin
1092                 "0:                             \n\t"
1093                 "movq (%%edx, %%eax), %%mm3     \n\t" 
1094                 "movd (%%ecx, %%esi), %%mm0     \n\t" 
1095                 "punpcklbw %%mm7, %%mm0         \n\t"
1096                 "pshufw $0xFF, %%mm0, %%mm1     \n\t"
1097                 "1:                             \n\t"
1098                 "pshufw $0xFF, %%mm0, %%mm0     \n\t"
1099                 "2:                             \n\t"
1100                 "psubw %%mm1, %%mm0             \n\t"
1101                 "movl 8(%%ebx, %%eax), %%esi    \n\t"
1102                 "pmullw %%mm3, %%mm0            \n\t"
1103                 "psllw $7, %%mm1                \n\t"
1104                 "paddw %%mm1, %%mm0             \n\t"
1105
1106                 "movq %%mm0, (%%edi, %%eax)     \n\t"
1107
1108                 "addl $8, %%eax                 \n\t"
1109         // End
1110                 "9:                             \n\t"
1111 //              "int $3\n\t"
1112                 "leal 0b, %0                    \n\t"
1113                 "leal 1b, %1                    \n\t"
1114                 "leal 2b, %2                    \n\t"
1115                 "decl %1                        \n\t"
1116                 "decl %2                        \n\t"
1117                 "subl %0, %1                    \n\t"
1118                 "subl %0, %2                    \n\t"
1119                 "leal 9b, %3                    \n\t"
1120                 "subl %0, %3                    \n\t"
1121
1122
1123                 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1124                 "=r" (fragmentLengthB)
1125         );
1126
1127         xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1128         fragmentPos=0;
1129         
1130         for(i=0; i<dstW/numSplits; i++)
1131         {
1132                 int xx=xpos>>16;
1133
1134                 if((i&3) == 0)
1135                 {
1136                         int a=0;
1137                         int b=((xpos+xInc)>>16) - xx;
1138                         int c=((xpos+xInc*2)>>16) - xx;
1139                         int d=((xpos+xInc*3)>>16) - xx;
1140
1141                         filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1142                         filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1143                         filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1144                         filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1145                         filterPos[i/2]= xx;
1146
1147                         if(d+1<4)
1148                         {
1149                                 int maxShift= 3-(d+1);
1150                                 int shift=0;
1151
1152                                 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1153
1154                                 funnyCode[fragmentPos + imm8OfPShufW1B]=
1155                                         (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1156                                 funnyCode[fragmentPos + imm8OfPShufW2B]=
1157                                         a | (b<<2) | (c<<4) | (d<<6);
1158
1159                                 if(i+3>=dstW) shift=maxShift; //avoid overread
1160                                 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1161
1162                                 if(shift && i>=shift)
1163                                 {
1164                                         funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1165                                         funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1166                                         filterPos[i/2]-=shift;
1167                                 }
1168
1169                                 fragmentPos+= fragmentLengthB;
1170                         }
1171                         else
1172                         {
1173                                 int maxShift= 3-d;
1174                                 int shift=0;
1175
1176                                 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1177
1178                                 funnyCode[fragmentPos + imm8OfPShufW1A]=
1179                                 funnyCode[fragmentPos + imm8OfPShufW2A]=
1180                                         a | (b<<2) | (c<<4) | (d<<6);
1181
1182                                 if(i+4>=dstW) shift=maxShift; //avoid overread
1183                                 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1184
1185                                 if(shift && i>=shift)
1186                                 {
1187                                         funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1188                                         funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1189                                         filterPos[i/2]-=shift;
1190                                 }
1191
1192                                 fragmentPos+= fragmentLengthA;
1193                         }
1194
1195                         funnyCode[fragmentPos]= RET;
1196                 }
1197                 xpos+=xInc;
1198         }
1199         filterPos[i/2]= xpos>>16; // needed to jump to the next part
1200 }
1201 #endif // ARCH_X86
1202
1203 //FIXME remove
1204 void SwScale_Init(){
1205 }
1206
1207 static void globalInit(){
1208     // generating tables:
1209     int i;
1210     for(i=0; i<768; i++){
1211         int c= MIN(MAX(i-256, 0), 255);
1212         clip_table[i]=c;
1213         yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
1214         yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
1215         yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
1216         yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
1217         yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
1218     }
1219
1220     for(i=0; i<768; i++)
1221     {
1222         int v= clip_table[i];
1223         clip_table16b[i]=  v>>3;
1224         clip_table16g[i]= (v<<3)&0x07E0;
1225         clip_table16r[i]= (v<<8)&0xF800;
1226         clip_table15b[i]=  v>>3;
1227         clip_table15g[i]= (v<<2)&0x03E0;
1228         clip_table15r[i]= (v<<7)&0x7C00;
1229     }
1230
1231 cpuCaps= gCpuCaps;
1232
1233 #ifdef RUNTIME_CPUDETECT
1234 #ifdef CAN_COMPILE_X86_ASM
1235         // ordered per speed fasterst first
1236         if(gCpuCaps.hasMMX2)
1237                 swScale= swScale_MMX2;
1238         else if(gCpuCaps.has3DNow)
1239                 swScale= swScale_3DNow;
1240         else if(gCpuCaps.hasMMX)
1241                 swScale= swScale_MMX;
1242         else
1243                 swScale= swScale_C;
1244
1245 #else
1246         swScale= swScale_C;
1247         cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1248 #endif
1249 #else //RUNTIME_CPUDETECT
1250 #ifdef HAVE_MMX2
1251         swScale= swScale_MMX2;
1252         cpuCaps.has3DNow = 0;
1253 #elif defined (HAVE_3DNOW)
1254         swScale= swScale_3DNow;
1255         cpuCaps.hasMMX2 = 0;
1256 #elif defined (HAVE_MMX)
1257         swScale= swScale_MMX;
1258         cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1259 #else
1260         swScale= swScale_C;
1261         cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1262 #endif
1263 #endif //!RUNTIME_CPUDETECT
1264 }
1265
1266 static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1267              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1268         uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1269         /* Copy Y plane */
1270         if(dstStride[0]==srcStride[0])
1271                 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1272         else
1273         {
1274                 int i;
1275                 uint8_t *srcPtr= src[0];
1276                 uint8_t *dstPtr= dst;
1277                 for(i=0; i<srcSliceH; i++)
1278                 {
1279                         memcpy(dstPtr, srcPtr, srcStride[0]);
1280                         srcPtr+= srcStride[0];
1281                         dstPtr+= dstStride[0];
1282                 }
1283         }
1284         dst = dstParam[1] + dstStride[1]*srcSliceY;
1285         if(c->srcFormat==IMGFMT_YV12)
1286                 interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
1287         else /* I420 & IYUV */
1288                 interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
1289 }
1290
1291
1292 /* Warper functions for yuv2bgr */
1293 static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1294              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1295         uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1296
1297         if(c->srcFormat==IMGFMT_YV12)
1298                 yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1299         else /* I420 & IYUV */
1300                 yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1301 }
1302
1303 static void Planar2PackedWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1304              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1305         uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1306
1307         if(c->srcFormat==IMGFMT_YV12)
1308                 yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1309         else /* I420 & IYUV */
1310                 yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1311 }
1312
1313 static void bgr24to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1314              int srcSliceH, uint8_t* dst[], int dstStride[]){
1315         
1316         if(dstStride[0]*3==srcStride[0]*4)
1317                 rgb24to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1318         else
1319         {
1320                 int i;
1321                 uint8_t *srcPtr= src[0];
1322                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1323
1324                 for(i=0; i<srcSliceH; i++)
1325                 {
1326                         rgb24to32(srcPtr, dstPtr, c->srcW*3);
1327                         srcPtr+= srcStride[0];
1328                         dstPtr+= dstStride[0];
1329                 }
1330         }     
1331 }
1332
1333 static void bgr24to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1334              int srcSliceH, uint8_t* dst[], int dstStride[]){
1335         
1336         if(dstStride[0]*3==srcStride[0]*2)
1337                 rgb24to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1338         else
1339         {
1340                 int i;
1341                 uint8_t *srcPtr= src[0];
1342                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1343
1344                 for(i=0; i<srcSliceH; i++)
1345                 {
1346                         rgb24to16(srcPtr, dstPtr, c->srcW*3);
1347                         srcPtr+= srcStride[0];
1348                         dstPtr+= dstStride[0];
1349                 }
1350         }     
1351 }
1352
1353 static void bgr24to15Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1354              int srcSliceH, uint8_t* dst[], int dstStride[]){
1355         
1356         if(dstStride[0]*3==srcStride[0]*2)
1357                 rgb24to15(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1358         else
1359         {
1360                 int i;
1361                 uint8_t *srcPtr= src[0];
1362                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1363
1364                 for(i=0; i<srcSliceH; i++)
1365                 {
1366                         rgb24to15(srcPtr, dstPtr, c->srcW*3);
1367                         srcPtr+= srcStride[0];
1368                         dstPtr+= dstStride[0];
1369                 }
1370         }     
1371 }
1372
1373 static void bgr32to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1374              int srcSliceH, uint8_t* dst[], int dstStride[]){
1375         
1376         if(dstStride[0]*4==srcStride[0]*3)
1377                 rgb32to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1378         else
1379         {
1380                 int i;
1381                 uint8_t *srcPtr= src[0];
1382                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1383
1384                 for(i=0; i<srcSliceH; i++)
1385                 {
1386                         rgb32to24(srcPtr, dstPtr, c->srcW<<2);
1387                         srcPtr+= srcStride[0];
1388                         dstPtr+= dstStride[0];
1389                 }
1390         }     
1391 }
1392
1393 static void bgr32to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1394              int srcSliceH, uint8_t* dst[], int dstStride[]){
1395         
1396         if(dstStride[0]*4==srcStride[0]*2)
1397                 rgb32to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1398         else
1399         {
1400                 int i;
1401                 uint8_t *srcPtr= src[0];
1402                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1403
1404                 for(i=0; i<srcSliceH; i++)
1405                 {
1406                         rgb32to16(srcPtr, dstPtr, c->srcW<<2);
1407                         srcPtr+= srcStride[0];
1408                         dstPtr+= dstStride[0];
1409                 }
1410         }     
1411 }
1412
1413 static void bgr32to15Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1414              int srcSliceH, uint8_t* dst[], int dstStride[]){
1415         
1416         if(dstStride[0]*4==srcStride[0]*2)
1417                 rgb32to15(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1418         else
1419         {
1420                 int i;
1421                 uint8_t *srcPtr= src[0];
1422                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1423
1424                 for(i=0; i<srcSliceH; i++)
1425                 {
1426                         rgb32to15(srcPtr, dstPtr, c->srcW<<2);
1427                         srcPtr+= srcStride[0];
1428                         dstPtr+= dstStride[0];
1429                 }
1430         }     
1431 }
1432
1433 static void bgr15to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1434              int srcSliceH, uint8_t* dst[], int dstStride[]){
1435         
1436         if(dstStride[0]==srcStride[0])
1437                 rgb15to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1438         else
1439         {
1440                 int i;
1441                 uint8_t *srcPtr= src[0];
1442                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1443
1444                 for(i=0; i<srcSliceH; i++)
1445                 {
1446                         rgb15to16(srcPtr, dstPtr, c->srcW<<1);
1447                         srcPtr+= srcStride[0];
1448                         dstPtr+= dstStride[0];
1449                 }
1450         }     
1451 }
1452
1453 static void bgr15to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1454              int srcSliceH, uint8_t* dst[], int dstStride[]){
1455         
1456         if(dstStride[0]*2==srcStride[0]*3)
1457                 rgb15to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1458         else
1459         {
1460                 int i;
1461                 uint8_t *srcPtr= src[0];
1462                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1463
1464                 for(i=0; i<srcSliceH; i++)
1465                 {
1466                         rgb15to24(srcPtr, dstPtr, c->srcW<<1);
1467                         srcPtr+= srcStride[0];
1468                         dstPtr+= dstStride[0];
1469                 }
1470         }     
1471 }
1472
1473 static void bgr15to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1474              int srcSliceH, uint8_t* dst[], int dstStride[]){
1475         
1476         if(dstStride[0]*2==srcStride[0]*4)
1477                 rgb15to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1478         else
1479         {
1480                 int i;
1481                 uint8_t *srcPtr= src[0];
1482                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1483
1484                 for(i=0; i<srcSliceH; i++)
1485                 {
1486                         rgb15to32(srcPtr, dstPtr, c->srcW<<1);
1487                         srcPtr+= srcStride[0];
1488                         dstPtr+= dstStride[0];
1489                 }
1490         }     
1491 }
1492
1493 static void bgr16to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1494              int srcSliceH, uint8_t* dst[], int dstStride[]){
1495         
1496         if(dstStride[0]*2==srcStride[0]*3)
1497                 rgb16to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1498         else
1499         {
1500                 int i;
1501                 uint8_t *srcPtr= src[0];
1502                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1503
1504                 for(i=0; i<srcSliceH; i++)
1505                 {
1506                         rgb16to24(srcPtr, dstPtr, c->srcW<<1);
1507                         srcPtr+= srcStride[0];
1508                         dstPtr+= dstStride[0];
1509                 }
1510         }     
1511 }
1512
1513 static void bgr16to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1514              int srcSliceH, uint8_t* dst[], int dstStride[]){
1515         
1516         if(dstStride[0]*2==srcStride[0]*4)
1517                 rgb16to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1518         else
1519         {
1520                 int i;
1521                 uint8_t *srcPtr= src[0];
1522                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1523
1524                 for(i=0; i<srcSliceH; i++)
1525                 {
1526                         rgb16to32(srcPtr, dstPtr, c->srcW<<1);
1527                         srcPtr+= srcStride[0];
1528                         dstPtr+= dstStride[0];
1529                 }
1530         }     
1531 }
1532
1533 static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1534              int srcSliceH, uint8_t* dst[], int dstStride[]){
1535
1536         rgb24toyv12(
1537                 src[0], 
1538                 dst[0]+ srcSliceY    *dstStride[0], 
1539                 dst[1]+(srcSliceY>>1)*dstStride[1], 
1540                 dst[2]+(srcSliceY>>1)*dstStride[2],
1541                 c->srcW, srcSliceH, 
1542                 dstStride[0], dstStride[1], srcStride[0]);
1543 }
1544
1545
1546 /* unscaled copy like stuff (assumes nearly identical formats) */
1547 static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
1548              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1549
1550         int srcStride[3];
1551         uint8_t *src[3];
1552         uint8_t *dst[3];
1553
1554         if(isPlanarYUV(c->srcFormat))
1555         {
1556             if(c->srcFormat == IMGFMT_I420 || c->srcFormat == IMGFMT_IYUV){
1557                 src[0]= srcParam[0];
1558                 src[1]= srcParam[2];
1559                 src[2]= srcParam[1];
1560                 srcStride[0]= srcStrideParam[0];
1561                 srcStride[1]= srcStrideParam[2];
1562                 srcStride[2]= srcStrideParam[1];
1563             }
1564             else
1565             {
1566                 src[0]= srcParam[0];
1567                 src[1]= srcParam[1];
1568                 src[2]= srcParam[2];
1569                 srcStride[0]= srcStrideParam[0];
1570                 srcStride[1]= srcStrideParam[1];
1571                 srcStride[2]= srcStrideParam[2];
1572             }
1573         }
1574         else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
1575                 src[0]= srcParam[0];
1576                 src[1]=
1577                 src[2]= NULL;
1578                 srcStride[0]= srcStrideParam[0];
1579                 srcStride[1]=
1580                 srcStride[2]= 0;
1581         }
1582
1583         if(c->dstFormat == IMGFMT_I420 || c->dstFormat == IMGFMT_IYUV){
1584                 dst[0]= dstParam[0];
1585                 dst[1]= dstParam[2];
1586                 dst[2]= dstParam[1];
1587                 
1588         }else{
1589                 dst[0]= dstParam[0];
1590                 dst[1]= dstParam[1];
1591                 dst[2]= dstParam[2];
1592         }
1593
1594         if(isPacked(c->srcFormat))
1595         {
1596                 if(dstStride[0]==srcStride[0])
1597                         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1598                 else
1599                 {
1600                         int i;
1601                         uint8_t *srcPtr= src[0];
1602                         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1603                         int length=0;
1604
1605                         /* universal length finder */
1606                         while(length+c->srcW <= ABS(dstStride[0]) 
1607                            && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
1608                         ASSERT(length!=0);
1609
1610                         for(i=0; i<srcSliceH; i++)
1611                         {
1612                                 memcpy(dstPtr, srcPtr, length);
1613                                 srcPtr+= srcStride[0];
1614                                 dstPtr+= dstStride[0];
1615                         }
1616                 }
1617         }
1618         else 
1619         { /* Planar YUV */
1620                 int plane;
1621                 for(plane=0; plane<3; plane++)
1622                 {
1623                         int length;
1624                         int y;
1625                         int height;
1626                         if(c->srcFormat == IMGFMT_YVU9 || c->srcFormat == IMGFMT_IF09)
1627                         {
1628                             length= plane==0 ? c->srcW  : ((c->srcW+1)>>2);
1629                             y=      plane==0 ? srcSliceY: ((srcSliceY+1)>>2);
1630                             height= plane==0 ? srcSliceH: ((srcSliceH+1)>>2);
1631                         }
1632                         else
1633                         {
1634                             length= plane==0 ? c->srcW  : ((c->srcW+1)>>1);
1635                             y=      plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
1636                             height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
1637                         }
1638
1639                         if(dstStride[plane]==srcStride[plane])
1640                                 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1641                         else
1642                         {
1643                                 int i;
1644                                 uint8_t *srcPtr= src[plane];
1645                                 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1646                                 for(i=0; i<height; i++)
1647                                 {
1648                                         memcpy(dstPtr, srcPtr, length);
1649                                         srcPtr+= srcStride[plane];
1650                                         dstPtr+= dstStride[plane];
1651                                 }
1652                         }
1653                 }
1654         }
1655 }
1656
1657 static uint32_t remove_dup_fourcc(uint32_t fourcc)
1658 {
1659         switch(fourcc)
1660         {
1661             case IMGFMT_IYUV: return IMGFMT_I420;
1662             case IMGFMT_Y8  : return IMGFMT_Y800;
1663             default: return fourcc;
1664         }
1665 }
1666
1667 SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1668                          SwsFilter *srcFilter, SwsFilter *dstFilter){
1669
1670         SwsContext *c;
1671         int i;
1672         int usesFilter;
1673         int simple_copy, unscaled_copy;
1674         SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1675
1676 #ifdef ARCH_X86
1677         if(gCpuCaps.hasMMX)
1678                 asm volatile("emms\n\t"::: "memory");
1679 #endif
1680
1681         if(swScale==NULL) globalInit();
1682
1683         /* avoid dupplicate Formats, so we dont need to check to much */
1684         srcFormat = remove_dup_fourcc(srcFormat);
1685         dstFormat = remove_dup_fourcc(dstFormat);
1686         /* don't refuse this beauty */
1687         unscaled_copy = (srcW == dstW && srcH == dstH);
1688         simple_copy = (srcW == dstW && srcH == dstH && srcFormat == dstFormat);
1689         if(!simple_copy)
1690         {
1691             if(unscaled_copy)
1692             {
1693                 if(!isSupportedUnscaledIn(srcFormat)) 
1694                 {
1695                     MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1696                     return NULL;
1697                 }
1698                 if(!isSupportedUnscaledOut(dstFormat))
1699                 {
1700                     MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1701                     return NULL;
1702                 }
1703             }
1704             else
1705             {
1706                 if(!isSupportedIn(srcFormat)) 
1707                 {
1708                     MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1709                     return NULL;
1710                 }
1711                 if(!isSupportedOut(dstFormat))
1712                 {
1713                     MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1714                     return NULL;
1715                 }
1716             }
1717         }
1718         /* sanity check */
1719         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
1720         {
1721                  MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n", 
1722                         srcW, srcH, dstW, dstH);
1723                 return NULL;
1724         }
1725
1726         if(!dstFilter) dstFilter= &dummyFilter;
1727         if(!srcFilter) srcFilter= &dummyFilter;
1728
1729         c= memalign(64, sizeof(SwsContext));
1730         memset(c, 0, sizeof(SwsContext));
1731
1732         c->srcW= srcW;
1733         c->srcH= srcH;
1734         c->dstW= dstW;
1735         c->dstH= dstH;
1736         c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1737         c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1738         c->flags= flags;
1739         c->dstFormat= dstFormat;
1740         c->srcFormat= srcFormat;
1741
1742         usesFilter=0;
1743         if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
1744         if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
1745         if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
1746         if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
1747         if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
1748         if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
1749         if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
1750         if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
1751         
1752         /* unscaled special Cases */
1753         if(srcW==dstW && srcH==dstH && !usesFilter)
1754         {
1755                 /* yv12_to_nv12 */
1756                 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
1757                 {
1758                         c->swScale= PlanarToNV12Wrapper;
1759
1760                         if(flags&SWS_PRINT_INFO)
1761                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1762                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1763                         return c;
1764                 }
1765                 /* yv12_to_yuy2 */
1766                 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
1767                 {
1768                         c->swScale= Planar2PackedWrapper;
1769
1770                         if(flags&SWS_PRINT_INFO)
1771                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1772                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1773                         return c;
1774                 }
1775                 /* yuv2bgr */
1776                 if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
1777                 {
1778                         // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
1779 #ifdef WORDS_BIGENDIAN
1780                         if(dstFormat==IMGFMT_BGR32)
1781                                 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
1782                         else
1783                                 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1784 #else
1785                         yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1786 #endif
1787                         c->swScale= planarYuvToBgr;
1788
1789                         if(flags&SWS_PRINT_INFO)
1790                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1791                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1792                         return c;
1793                 }
1794
1795                 /* simple copy */
1796                 if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
1797                 {
1798                         c->swScale= simpleCopy;
1799
1800                         if(flags&SWS_PRINT_INFO)
1801                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1802                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1803                         return c;
1804                 }
1805                 
1806                 /* bgr32to24 & rgb32to24*/
1807                 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR24)
1808                  ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB24))
1809                 {
1810                         c->swScale= bgr32to24Wrapper;
1811
1812                         if(flags&SWS_PRINT_INFO)
1813                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1814                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1815                         return c;
1816                 }
1817
1818                 /* bgr32to16 & rgb32to16*/
1819                 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR16)
1820                  ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB16))
1821                 {
1822                         c->swScale= bgr32to16Wrapper;
1823
1824                         if(flags&SWS_PRINT_INFO)
1825                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1826                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1827                         return c;
1828                 }
1829
1830                 /* bgr32to15 & rgb32to15*/
1831                 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR15)
1832                  ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB15))
1833                 {
1834                         c->swScale= bgr32to15Wrapper;
1835
1836                         if(flags&SWS_PRINT_INFO)
1837                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1838                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1839                         return c;
1840                 }
1841                 
1842                 /* bgr24to32 & rgb24to32*/
1843                 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR32)
1844                  ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB32))
1845                 {
1846                         c->swScale= bgr24to32Wrapper;
1847
1848                         if(flags&SWS_PRINT_INFO)
1849                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1850                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1851                         return c;
1852                 }
1853
1854                 /* bgr24to16 & rgb24to16*/
1855                 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR16)
1856                  ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB16))
1857                 {
1858                         c->swScale= bgr24to16Wrapper;
1859
1860                         if(flags&SWS_PRINT_INFO)
1861                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1862                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1863                         return c;
1864                 }
1865
1866                 /* bgr24to15 & rgb24to15*/
1867                 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR15)
1868                  ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB15))
1869                 {
1870                         c->swScale= bgr24to15Wrapper;
1871
1872                         if(flags&SWS_PRINT_INFO)
1873                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1874                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1875                         return c;
1876                 }
1877
1878                 /* bgr15to16 */
1879                 if(srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR16)
1880                 {
1881                         c->swScale= bgr15to16Wrapper;
1882
1883                         if(flags&SWS_PRINT_INFO)
1884                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1885                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1886                         return c;
1887                 }
1888
1889                 /* bgr15to24 */
1890                 if((srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR24)
1891                  ||(srcFormat==IMGFMT_RGB15 && dstFormat==IMGFMT_RGB24))
1892                 {
1893                         c->swScale= bgr15to24Wrapper;
1894
1895                         if(flags&SWS_PRINT_INFO)
1896                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1897                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1898                         return c;
1899                 }
1900
1901                 /* bgr15to32 */
1902                 if((srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR32)
1903                  ||(srcFormat==IMGFMT_RGB15 && dstFormat==IMGFMT_RGB32))
1904                 {
1905                         c->swScale= bgr15to32Wrapper;
1906
1907                         if(flags&SWS_PRINT_INFO)
1908                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1909                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1910                         return c;
1911                 }
1912
1913                 /* bgr16to24 */
1914                 if((srcFormat==IMGFMT_BGR16 && dstFormat==IMGFMT_BGR24)
1915                  ||(srcFormat==IMGFMT_RGB16 && dstFormat==IMGFMT_RGB24))
1916                 {
1917                         c->swScale= bgr16to24Wrapper;
1918
1919                         if(flags&SWS_PRINT_INFO)
1920                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1921                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1922                         return c;
1923                 }
1924
1925                 /* bgr16to32 */
1926                 if((srcFormat==IMGFMT_BGR16 && dstFormat==IMGFMT_BGR32)
1927                  ||(srcFormat==IMGFMT_RGB16 && dstFormat==IMGFMT_RGB32))
1928                 {
1929                         c->swScale= bgr16to32Wrapper;
1930
1931                         if(flags&SWS_PRINT_INFO)
1932                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1933                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1934                         return c;
1935                 }
1936
1937                 /* bgr24toYV12 */
1938                 if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
1939                 {
1940                         c->swScale= bgr24toyv12Wrapper;
1941
1942                         if(flags&SWS_PRINT_INFO)
1943                                 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
1944                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1945                         return c;
1946                 }
1947         }
1948
1949         if(cpuCaps.hasMMX2)
1950         {
1951                 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
1952                 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
1953                 {
1954                         if(flags&SWS_PRINT_INFO)
1955                                 MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
1956                 }
1957         }
1958         else
1959                 c->canMMX2BeUsed=0;
1960
1961
1962         /* dont use full vertical UV input/internaly if the source doesnt even have it */
1963         if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
1964         /* dont use full horizontal UV input if the source doesnt even have it */
1965         if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
1966         /* dont use full horizontal UV internally if the destination doesnt even have it */
1967         if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
1968
1969         if(flags&SWS_FULL_CHR_H_INP)    c->chrSrcW= srcW;
1970         else                            c->chrSrcW= (srcW+1)>>1;
1971
1972         if(flags&SWS_FULL_CHR_H_INT)    c->chrDstW= dstW;
1973         else                            c->chrDstW= (dstW+1)>>1;
1974
1975         if(flags&SWS_FULL_CHR_V)        c->chrSrcH= srcH;
1976         else                            c->chrSrcH= (srcH+1)>>1;
1977
1978         if(isHalfChrV(dstFormat))       c->chrDstH= (dstH+1)>>1;
1979         else                            c->chrDstH= dstH;
1980
1981         c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
1982         c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
1983
1984
1985         // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1986         // but only for the FAST_BILINEAR mode otherwise do correct scaling
1987         // n-2 is the last chrominance sample available
1988         // this is not perfect, but noone shuld notice the difference, the more correct variant
1989         // would be like the vertical one, but that would require some special code for the
1990         // first and last pixel
1991         if(flags&SWS_FAST_BILINEAR)
1992         {
1993                 if(c->canMMX2BeUsed)
1994                 {
1995                         c->lumXInc+= 20;
1996                         c->chrXInc+= 20;
1997                 }
1998                 //we dont use the x86asm scaler if mmx is available
1999                 else if(cpuCaps.hasMMX)
2000                 {
2001                         c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2002                         c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2003                 }
2004         }
2005
2006         /* precalculate horizontal scaler filter coefficients */
2007         {
2008                 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
2009
2010                 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2011                                  srcW      ,       dstW, filterAlign, 1<<14, flags,
2012                                  srcFilter->lumH, dstFilter->lumH);
2013                 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2014                                 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
2015                                  srcFilter->chrH, dstFilter->chrH);
2016
2017 #ifdef ARCH_X86
2018 // cant downscale !!!
2019                 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2020                 {
2021                         c->lumMmx2Filter   = (int16_t*)memalign(8, (dstW        /8+8)*sizeof(int16_t));
2022                         c->chrMmx2Filter   = (int16_t*)memalign(8, (c->chrDstW  /4+8)*sizeof(int16_t));
2023                         c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW      /2/8+8)*sizeof(int32_t));
2024                         c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
2025
2026                         initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2027                         initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2028                 }
2029 #endif
2030         } // Init Horizontal stuff
2031
2032
2033
2034         /* precalculate vertical scaler filter coefficients */
2035         initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2036                         srcH      ,        dstH, 1, (1<<12)-4, flags,
2037                         srcFilter->lumV, dstFilter->lumV);
2038         initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2039                         (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
2040                          srcFilter->chrV, dstFilter->chrV);
2041
2042         // Calculate Buffer Sizes so that they wont run out while handling these damn slices
2043         c->vLumBufSize= c->vLumFilterSize;
2044         c->vChrBufSize= c->vChrFilterSize;
2045         for(i=0; i<dstH; i++)
2046         {
2047                 int chrI= i*c->chrDstH / dstH;
2048                 int nextSlice= MAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2049                                  ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
2050                 nextSlice&= ~1; // Slices start at even boundaries
2051                 if(c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2052                         c->vLumBufSize= nextSlice - c->vLumFilterPos[i   ];
2053                 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
2054                         c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
2055         }
2056
2057         // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2058         c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
2059         c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
2060         //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)
2061         for(i=0; i<c->vLumBufSize; i++)
2062                 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
2063         for(i=0; i<c->vChrBufSize; i++)
2064                 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
2065
2066         //try to avoid drawing green stuff between the right end and the stride end
2067         for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
2068         for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2069
2070         ASSERT(c->chrDstH <= dstH)
2071
2072         // pack filter data for mmx code
2073         if(cpuCaps.hasMMX)
2074         {
2075                 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize*      dstH*4*sizeof(int16_t));
2076                 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
2077                 for(i=0; i<c->vLumFilterSize*dstH; i++)
2078                         c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
2079                                 c->vLumFilter[i];
2080                 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
2081                         c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
2082                                 c->vChrFilter[i];
2083         }
2084
2085         if(flags&SWS_PRINT_INFO)
2086         {
2087 #ifdef DITHER1XBPP
2088                 char *dither= " dithered";
2089 #else
2090                 char *dither= "";
2091 #endif
2092                 if(flags&SWS_FAST_BILINEAR)
2093                         MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
2094                 else if(flags&SWS_BILINEAR)
2095                         MSG_INFO("\nSwScaler: BILINEAR scaler, ");
2096                 else if(flags&SWS_BICUBIC)
2097                         MSG_INFO("\nSwScaler: BICUBIC scaler, ");
2098                 else if(flags&SWS_X)
2099                         MSG_INFO("\nSwScaler: Experimental scaler, ");
2100                 else if(flags&SWS_POINT)
2101                         MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
2102                 else if(flags&SWS_AREA)
2103                         MSG_INFO("\nSwScaler: Area Averageing scaler, ");
2104                 else
2105                         MSG_INFO("\nSwScaler: ehh flags invalid?! ");
2106
2107                 if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
2108                         MSG_INFO("from %s to%s %s ", 
2109                                 vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
2110                 else
2111                         MSG_INFO("from %s to %s ", 
2112                                 vo_format_name(srcFormat), vo_format_name(dstFormat));
2113
2114                 if(cpuCaps.hasMMX2)
2115                         MSG_INFO("using MMX2\n");
2116                 else if(cpuCaps.has3DNow)
2117                         MSG_INFO("using 3DNOW\n");
2118                 else if(cpuCaps.hasMMX)
2119                         MSG_INFO("using MMX\n");
2120                 else
2121                         MSG_INFO("using C\n");
2122         }
2123
2124         if((flags & SWS_PRINT_INFO) && verbose)
2125         {
2126                 if(cpuCaps.hasMMX)
2127                 {
2128                         if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2129                                 MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2130                         else
2131                         {
2132                                 if(c->hLumFilterSize==4)
2133                                         MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2134                                 else if(c->hLumFilterSize==8)
2135                                         MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2136                                 else
2137                                         MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2138
2139                                 if(c->hChrFilterSize==4)
2140                                         MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2141                                 else if(c->hChrFilterSize==8)
2142                                         MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2143                                 else
2144                                         MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2145                         }
2146                 }
2147                 else
2148                 {
2149 #ifdef ARCH_X86
2150                         MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
2151 #else
2152                         if(flags & SWS_FAST_BILINEAR)
2153                                 MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2154                         else
2155                                 MSG_V("SwScaler: using C scaler for horizontal scaling\n");
2156 #endif
2157                 }
2158                 if(isPlanarYUV(dstFormat))
2159                 {
2160                         if(c->vLumFilterSize==1)
2161                                 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
2162                         else
2163                                 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
2164                 }
2165                 else
2166                 {
2167                         if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
2168                                 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2169                                        "SwScaler:       2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
2170                         else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
2171                                 MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
2172                         else
2173                                 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
2174                 }
2175
2176                 if(dstFormat==IMGFMT_BGR24)
2177                         MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
2178                                 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
2179                 else if(dstFormat==IMGFMT_BGR32)
2180                         MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2181                 else if(dstFormat==IMGFMT_BGR16)
2182                         MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2183                 else if(dstFormat==IMGFMT_BGR15)
2184                         MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2185
2186                 MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2187         }
2188         if((flags & SWS_PRINT_INFO) && verbose>1)
2189         {
2190                 MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2191                         c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2192                 MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2193                         c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2194         }
2195
2196         c->swScale= swScale;
2197         return c;
2198 }
2199
2200 /**
2201  * returns a normalized gaussian curve used to filter stuff
2202  * quality=3 is high quality, lowwer is lowwer quality
2203  */
2204
2205 SwsVector *getGaussianVec(double variance, double quality){
2206         const int length= (int)(variance*quality + 0.5) | 1;
2207         int i;
2208         double *coeff= memalign(sizeof(double), length*sizeof(double));
2209         double middle= (length-1)*0.5;
2210         SwsVector *vec= malloc(sizeof(SwsVector));
2211
2212         vec->coeff= coeff;
2213         vec->length= length;
2214
2215         for(i=0; i<length; i++)
2216         {
2217                 double dist= i-middle;
2218                 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2219         }
2220
2221         normalizeVec(vec, 1.0);
2222
2223         return vec;
2224 }
2225
2226 SwsVector *getConstVec(double c, int length){
2227         int i;
2228         double *coeff= memalign(sizeof(double), length*sizeof(double));
2229         SwsVector *vec= malloc(sizeof(SwsVector));
2230
2231         vec->coeff= coeff;
2232         vec->length= length;
2233
2234         for(i=0; i<length; i++)
2235                 coeff[i]= c;
2236
2237         return vec;
2238 }
2239
2240
2241 SwsVector *getIdentityVec(void){
2242         double *coeff= memalign(sizeof(double), sizeof(double));
2243         SwsVector *vec= malloc(sizeof(SwsVector));
2244         coeff[0]= 1.0;
2245
2246         vec->coeff= coeff;
2247         vec->length= 1;
2248
2249         return vec;
2250 }
2251
2252 void normalizeVec(SwsVector *a, double height){
2253         int i;
2254         double sum=0;
2255         double inv;
2256
2257         for(i=0; i<a->length; i++)
2258                 sum+= a->coeff[i];
2259
2260         inv= height/sum;
2261
2262         for(i=0; i<a->length; i++)
2263                 a->coeff[i]*= height;
2264 }
2265
2266 void scaleVec(SwsVector *a, double scalar){
2267         int i;
2268
2269         for(i=0; i<a->length; i++)
2270                 a->coeff[i]*= scalar;
2271 }
2272
2273 static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
2274         int length= a->length + b->length - 1;
2275         double *coeff= memalign(sizeof(double), length*sizeof(double));
2276         int i, j;
2277         SwsVector *vec= malloc(sizeof(SwsVector));
2278
2279         vec->coeff= coeff;
2280         vec->length= length;
2281
2282         for(i=0; i<length; i++) coeff[i]= 0.0;
2283
2284         for(i=0; i<a->length; i++)
2285         {
2286                 for(j=0; j<b->length; j++)
2287                 {
2288                         coeff[i+j]+= a->coeff[i]*b->coeff[j];
2289                 }
2290         }
2291
2292         return vec;
2293 }
2294
2295 static SwsVector *sumVec(SwsVector *a, SwsVector *b){
2296         int length= MAX(a->length, b->length);
2297         double *coeff= memalign(sizeof(double), length*sizeof(double));
2298         int i;
2299         SwsVector *vec= malloc(sizeof(SwsVector));
2300
2301         vec->coeff= coeff;
2302         vec->length= length;
2303
2304         for(i=0; i<length; i++) coeff[i]= 0.0;
2305
2306         for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2307         for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2308
2309         return vec;
2310 }
2311
2312 static SwsVector *diffVec(SwsVector *a, SwsVector *b){
2313         int length= MAX(a->length, b->length);
2314         double *coeff= memalign(sizeof(double), length*sizeof(double));
2315         int i;
2316         SwsVector *vec= malloc(sizeof(SwsVector));
2317
2318         vec->coeff= coeff;
2319         vec->length= length;
2320
2321         for(i=0; i<length; i++) coeff[i]= 0.0;
2322
2323         for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2324         for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2325
2326         return vec;
2327 }
2328
2329 /* shift left / or right if "shift" is negative */
2330 static SwsVector *getShiftedVec(SwsVector *a, int shift){
2331         int length= a->length + ABS(shift)*2;
2332         double *coeff= memalign(sizeof(double), length*sizeof(double));
2333         int i;
2334         SwsVector *vec= malloc(sizeof(SwsVector));
2335
2336         vec->coeff= coeff;
2337         vec->length= length;
2338
2339         for(i=0; i<length; i++) coeff[i]= 0.0;
2340
2341         for(i=0; i<a->length; i++)
2342         {
2343                 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2344         }
2345
2346         return vec;
2347 }
2348
2349 void shiftVec(SwsVector *a, int shift){
2350         SwsVector *shifted= getShiftedVec(a, shift);
2351         free(a->coeff);
2352         a->coeff= shifted->coeff;
2353         a->length= shifted->length;
2354         free(shifted);
2355 }
2356
2357 void addVec(SwsVector *a, SwsVector *b){
2358         SwsVector *sum= sumVec(a, b);
2359         free(a->coeff);
2360         a->coeff= sum->coeff;
2361         a->length= sum->length;
2362         free(sum);
2363 }
2364
2365 void subVec(SwsVector *a, SwsVector *b){
2366         SwsVector *diff= diffVec(a, b);
2367         free(a->coeff);
2368         a->coeff= diff->coeff;
2369         a->length= diff->length;
2370         free(diff);
2371 }
2372
2373 void convVec(SwsVector *a, SwsVector *b){
2374         SwsVector *conv= getConvVec(a, b);
2375         free(a->coeff);
2376         a->coeff= conv->coeff;
2377         a->length= conv->length;
2378         free(conv);
2379 }
2380
2381 SwsVector *cloneVec(SwsVector *a){
2382         double *coeff= memalign(sizeof(double), a->length*sizeof(double));
2383         int i;
2384         SwsVector *vec= malloc(sizeof(SwsVector));
2385
2386         vec->coeff= coeff;
2387         vec->length= a->length;
2388
2389         for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2390
2391         return vec;
2392 }
2393
2394 void printVec(SwsVector *a){
2395         int i;
2396         double max=0;
2397         double min=0;
2398         double range;
2399
2400         for(i=0; i<a->length; i++)
2401                 if(a->coeff[i]>max) max= a->coeff[i];
2402
2403         for(i=0; i<a->length; i++)
2404                 if(a->coeff[i]<min) min= a->coeff[i];
2405
2406         range= max - min;
2407
2408         for(i=0; i<a->length; i++)
2409         {
2410                 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2411                 MSG_DBG2("%1.3f ", a->coeff[i]);
2412                 for(;x>0; x--) MSG_DBG2(" ");
2413                 MSG_DBG2("|\n");
2414         }
2415 }
2416
2417 void freeVec(SwsVector *a){
2418         if(!a) return;
2419         if(a->coeff) free(a->coeff);
2420         a->coeff=NULL;
2421         a->length=0;
2422         free(a);
2423 }
2424
2425 void freeSwsContext(SwsContext *c){
2426         int i;
2427
2428         if(!c) return;
2429
2430         if(c->lumPixBuf)
2431         {
2432                 for(i=0; i<c->vLumBufSize; i++)
2433                 {
2434                         if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
2435                         c->lumPixBuf[i]=NULL;
2436                 }
2437                 free(c->lumPixBuf);
2438                 c->lumPixBuf=NULL;
2439         }
2440
2441         if(c->chrPixBuf)
2442         {
2443                 for(i=0; i<c->vChrBufSize; i++)
2444                 {
2445                         if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
2446                         c->chrPixBuf[i]=NULL;
2447                 }
2448                 free(c->chrPixBuf);
2449                 c->chrPixBuf=NULL;
2450         }
2451
2452         if(c->vLumFilter) free(c->vLumFilter);
2453         c->vLumFilter = NULL;
2454         if(c->vChrFilter) free(c->vChrFilter);
2455         c->vChrFilter = NULL;
2456         if(c->hLumFilter) free(c->hLumFilter);
2457         c->hLumFilter = NULL;
2458         if(c->hChrFilter) free(c->hChrFilter);
2459         c->hChrFilter = NULL;
2460
2461         if(c->vLumFilterPos) free(c->vLumFilterPos);
2462         c->vLumFilterPos = NULL;
2463         if(c->vChrFilterPos) free(c->vChrFilterPos);
2464         c->vChrFilterPos = NULL;
2465         if(c->hLumFilterPos) free(c->hLumFilterPos);
2466         c->hLumFilterPos = NULL;
2467         if(c->hChrFilterPos) free(c->hChrFilterPos);
2468         c->hChrFilterPos = NULL;
2469
2470         if(c->lumMmxFilter) free(c->lumMmxFilter);
2471         c->lumMmxFilter = NULL;
2472         if(c->chrMmxFilter) free(c->chrMmxFilter);
2473         c->chrMmxFilter = NULL;
2474
2475         if(c->lumMmx2Filter) free(c->lumMmx2Filter);
2476         c->lumMmx2Filter=NULL;
2477         if(c->chrMmx2Filter) free(c->chrMmx2Filter);
2478         c->chrMmx2Filter=NULL;
2479         if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
2480         c->lumMmx2FilterPos=NULL;
2481         if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
2482         c->chrMmx2FilterPos=NULL;
2483
2484         free(c);
2485 }
2486
2487