]> git.sesse.net Git - ffmpeg/blob - postproc/swscale.c
511b10c317843f195c918cd0445d6c3a036d4d6c
[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
39 untested special converters
40   YV12/I420/IYUV -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
41   YV12/I420/IYUV -> YV12/I420/IYUV 
42   YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
43   BGR24 -> BGR32 & RGB24 -> RGB32
44   BGR32 -> BGR24 & RGB32 -> RGB24
45 */
46
47 #include <inttypes.h>
48 #include <string.h>
49 #include <math.h>
50 #include <stdio.h>
51 #include "../config.h"
52 #include "../mangle.h"
53 #ifdef HAVE_MALLOC_H
54 #include <malloc.h>
55 #endif
56 #include "swscale.h"
57 #include "../cpudetect.h"
58 #include "../bswap.h"
59 #include "../libvo/img_format.h"
60 #include "rgb2rgb.h"
61 #include "../libvo/fastmemcpy.h"
62 #undef MOVNTQ
63 #undef PAVGB
64
65 //#undef HAVE_MMX2
66 //#define HAVE_3DNOW
67 //#undef HAVE_MMX
68 //#undef ARCH_X86
69 #define DITHER1XBPP
70
71 #define RET 0xC3 //near return opcode for X86
72
73 #ifdef MP_DEBUG
74 #define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
75 #else
76 #define ASSERT(x) ;
77 #endif
78
79 #ifdef M_PI
80 #define PI M_PI
81 #else
82 #define PI 3.14159265358979323846
83 #endif
84
85 //FIXME replace this with something faster
86 #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
87 #define isYUV(x)       ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
88 #define isHalfChrV(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
89 #define isHalfChrH(x)  ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
90 #define isPacked(x)    ((x)==IMGFMT_YUY2 || ((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR || ((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
91 #define isGray(x)      ((x)==IMGFMT_Y800)
92 #define isSupportedIn(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
93                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
94                         || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
95                         || (x)==IMGFMT_Y800)
96 #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
97                         || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
98 #define isBGR(x)       ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
99
100 #define RGB2YUV_SHIFT 16
101 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
102 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
103 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
104 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
105 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
106 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
107 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
108 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
109 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
110
111 extern int verbose; // defined in mplayer.c
112 /*
113 NOTES
114
115 known BUGS with known cause (no bugreports please!, but patches are welcome :) )
116 horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
117
118 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
119
120 TODO
121 more intelligent missalignment avoidance for the horizontal scaler
122 write special vertical cubic upscale version
123 Optimize C code (yv12 / minmax)
124 add support for packed pixel yuv input & output
125 add support for Y8 output
126 optimize bgr24 & bgr32
127 add BGR4 output support
128 write special BGR->BGR scaler
129 deglobalize yuv2rgb*.c
130 */
131
132 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
133 #define MIN(a,b) ((a) > (b) ? (b) : (a))
134 #define MAX(a,b) ((a) < (b) ? (b) : (a))
135
136 #ifdef ARCH_X86
137 #define CAN_COMPILE_X86_ASM
138 #endif
139
140 #ifdef CAN_COMPILE_X86_ASM
141 static uint64_t __attribute__((aligned(8))) yCoeff=    0x2568256825682568LL;
142 static uint64_t __attribute__((aligned(8))) vrCoeff=   0x3343334333433343LL;
143 static uint64_t __attribute__((aligned(8))) ubCoeff=   0x40cf40cf40cf40cfLL;
144 static uint64_t __attribute__((aligned(8))) vgCoeff=   0xE5E2E5E2E5E2E5E2LL;
145 static uint64_t __attribute__((aligned(8))) ugCoeff=   0xF36EF36EF36EF36ELL;
146 static uint64_t __attribute__((aligned(8))) bF8=       0xF8F8F8F8F8F8F8F8LL;
147 static uint64_t __attribute__((aligned(8))) bFC=       0xFCFCFCFCFCFCFCFCLL;
148 static uint64_t __attribute__((aligned(8))) w400=      0x0400040004000400LL;
149 static uint64_t __attribute__((aligned(8))) w80=       0x0080008000800080LL;
150 static uint64_t __attribute__((aligned(8))) w10=       0x0010001000100010LL;
151 static uint64_t __attribute__((aligned(8))) w02=       0x0002000200020002LL;
152 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
153 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
154 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
155 static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
156
157 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
158 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
159 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
160 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
161
162 static uint64_t __attribute__((aligned(8))) dither4[2]={
163         0x0103010301030103LL,
164         0x0200020002000200LL,};
165
166 static uint64_t __attribute__((aligned(8))) dither8[2]={
167         0x0602060206020602LL,
168         0x0004000400040004LL,};
169
170 static uint64_t __attribute__((aligned(8))) b16Mask=   0x001F001F001F001FLL;
171 static uint64_t __attribute__((aligned(8))) g16Mask=   0x07E007E007E007E0LL;
172 static uint64_t __attribute__((aligned(8))) r16Mask=   0xF800F800F800F800LL;
173 static uint64_t __attribute__((aligned(8))) b15Mask=   0x001F001F001F001FLL;
174 static uint64_t __attribute__((aligned(8))) g15Mask=   0x03E003E003E003E0LL;
175 static uint64_t __attribute__((aligned(8))) r15Mask=   0x7C007C007C007C00LL;
176
177 static uint64_t __attribute__((aligned(8))) M24A=   0x00FF0000FF0000FFLL;
178 static uint64_t __attribute__((aligned(8))) M24B=   0xFF0000FF0000FF00LL;
179 static uint64_t __attribute__((aligned(8))) M24C=   0x0000FF0000FF0000LL;
180
181 // FIXME remove
182 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
183 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
184 #endif
185
186 // clipping helper table for C implementations:
187 static unsigned char clip_table[768];
188
189 static unsigned short clip_table16b[768];
190 static unsigned short clip_table16g[768];
191 static unsigned short clip_table16r[768];
192 static unsigned short clip_table15b[768];
193 static unsigned short clip_table15g[768];
194 static unsigned short clip_table15r[768];
195
196 // yuv->rgb conversion tables:
197 static    int yuvtab_2568[256];
198 static    int yuvtab_3343[256];
199 static    int yuvtab_0c92[256];
200 static    int yuvtab_1a1e[256];
201 static    int yuvtab_40cf[256];
202 // Needed for cubic scaler to catch overflows
203 static    int clip_yuvtab_2568[768];
204 static    int clip_yuvtab_3343[768];
205 static    int clip_yuvtab_0c92[768];
206 static    int clip_yuvtab_1a1e[768];
207 static    int clip_yuvtab_40cf[768];
208
209 //global sws_flags from the command line
210 int sws_flags=2;
211
212 //global srcFilter
213 SwsFilter src_filter= {NULL, NULL, NULL, NULL};
214
215 float sws_lum_gblur= 0.0;
216 float sws_chr_gblur= 0.0;
217 int sws_chr_vshift= 0;
218 int sws_chr_hshift= 0;
219 float sws_chr_sharpen= 0.0;
220 float sws_lum_sharpen= 0.0;
221
222 /* cpuCaps combined from cpudetect and whats actually compiled in
223    (if there is no support for something compiled in it wont appear here) */
224 static CpuCaps cpuCaps;
225
226 void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
227              int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
228
229 static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
230
231 #ifdef CAN_COMPILE_X86_ASM
232 void in_asm_used_var_warning_killer()
233 {
234  volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
235  bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
236  M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
237  if(i) i=0;
238 }
239 #endif
240
241 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
242                                     int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
243                                     uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
244 {
245         //FIXME Optimize (just quickly writen not opti..)
246         int i;
247         for(i=0; i<dstW; i++)
248         {
249                 int val=0;
250                 int j;
251                 for(j=0; j<lumFilterSize; j++)
252                         val += lumSrc[j][i] * lumFilter[j];
253
254                 dest[i]= MIN(MAX(val>>19, 0), 255);
255         }
256
257         if(uDest != NULL)
258                 for(i=0; i<(dstW>>1); i++)
259                 {
260                         int u=0;
261                         int v=0;
262                         int j;
263                         for(j=0; j<chrFilterSize; j++)
264                         {
265                                 u += chrSrc[j][i] * chrFilter[j];
266                                 v += chrSrc[j][i + 2048] * chrFilter[j];
267                         }
268
269                         uDest[i]= MIN(MAX(u>>19, 0), 255);
270                         vDest[i]= MIN(MAX(v>>19, 0), 255);
271                 }
272 }
273
274 static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
275                                     int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
276                                     uint8_t *dest, int dstW, int dstFormat)
277 {
278         if(dstFormat==IMGFMT_BGR32)
279         {
280                 int i;
281                 for(i=0; i<(dstW>>1); i++){
282                         int j;
283                         int Y1=0;
284                         int Y2=0;
285                         int U=0;
286                         int V=0;
287                         int Cb, Cr, Cg;
288                         for(j=0; j<lumFilterSize; j++)
289                         {
290                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
291                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
292                         }
293                         for(j=0; j<chrFilterSize; j++)
294                         {
295                                 U += chrSrc[j][i] * chrFilter[j];
296                                 V += chrSrc[j][i+2048] * chrFilter[j];
297                         }
298                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
299                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
300                         U >>= 19;
301                         V >>= 19;
302
303                         Cb= clip_yuvtab_40cf[U+ 256];
304                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
305                         Cr= clip_yuvtab_3343[V+ 256];
306
307                         dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
308                         dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
309                         dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
310
311                         dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
312                         dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
313                         dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
314                 }
315         }
316         else if(dstFormat==IMGFMT_BGR24)
317         {
318                 int i;
319                 for(i=0; i<(dstW>>1); i++){
320                         int j;
321                         int Y1=0;
322                         int Y2=0;
323                         int U=0;
324                         int V=0;
325                         int Cb, Cr, Cg;
326                         for(j=0; j<lumFilterSize; j++)
327                         {
328                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
329                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
330                         }
331                         for(j=0; j<chrFilterSize; j++)
332                         {
333                                 U += chrSrc[j][i] * chrFilter[j];
334                                 V += chrSrc[j][i+2048] * chrFilter[j];
335                         }
336                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
337                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
338                         U >>= 19;
339                         V >>= 19;
340
341                         Cb= clip_yuvtab_40cf[U+ 256];
342                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
343                         Cr= clip_yuvtab_3343[V+ 256];
344
345                         dest[0]=clip_table[((Y1 + Cb) >>13)];
346                         dest[1]=clip_table[((Y1 + Cg) >>13)];
347                         dest[2]=clip_table[((Y1 + Cr) >>13)];
348
349                         dest[3]=clip_table[((Y2 + Cb) >>13)];
350                         dest[4]=clip_table[((Y2 + Cg) >>13)];
351                         dest[5]=clip_table[((Y2 + Cr) >>13)];
352                         dest+=6;
353                 }
354         }
355         else if(dstFormat==IMGFMT_BGR16)
356         {
357                 int i;
358 #ifdef DITHER1XBPP
359                 static int ditherb1=1<<14;
360                 static int ditherg1=1<<13;
361                 static int ditherr1=2<<14;
362                 static int ditherb2=3<<14;
363                 static int ditherg2=3<<13;
364                 static int ditherr2=0<<14;
365
366                 ditherb1 ^= (1^2)<<14;
367                 ditherg1 ^= (1^2)<<13;
368                 ditherr1 ^= (1^2)<<14;
369                 ditherb2 ^= (3^0)<<14;
370                 ditherg2 ^= (3^0)<<13;
371                 ditherr2 ^= (3^0)<<14;
372 #else
373                 const int ditherb1=0;
374                 const int ditherg1=0;
375                 const int ditherr1=0;
376                 const int ditherb2=0;
377                 const int ditherg2=0;
378                 const int ditherr2=0;
379 #endif
380                 for(i=0; i<(dstW>>1); i++){
381                         int j;
382                         int Y1=0;
383                         int Y2=0;
384                         int U=0;
385                         int V=0;
386                         int Cb, Cr, Cg;
387                         for(j=0; j<lumFilterSize; j++)
388                         {
389                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
390                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
391                         }
392                         for(j=0; j<chrFilterSize; j++)
393                         {
394                                 U += chrSrc[j][i] * chrFilter[j];
395                                 V += chrSrc[j][i+2048] * chrFilter[j];
396                         }
397                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
398                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
399                         U >>= 19;
400                         V >>= 19;
401
402                         Cb= clip_yuvtab_40cf[U+ 256];
403                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
404                         Cr= clip_yuvtab_3343[V+ 256];
405
406                         ((uint16_t*)dest)[2*i] =
407                                 clip_table16b[(Y1 + Cb + ditherb1) >>13] |
408                                 clip_table16g[(Y1 + Cg + ditherg1) >>13] |
409                                 clip_table16r[(Y1 + Cr + ditherr1) >>13];
410
411                         ((uint16_t*)dest)[2*i+1] =
412                                 clip_table16b[(Y2 + Cb + ditherb2) >>13] |
413                                 clip_table16g[(Y2 + Cg + ditherg2) >>13] |
414                                 clip_table16r[(Y2 + Cr + ditherr2) >>13];
415                 }
416         }
417         else if(dstFormat==IMGFMT_BGR15)
418         {
419                 int i;
420 #ifdef DITHER1XBPP
421                 static int ditherb1=1<<14;
422                 static int ditherg1=1<<14;
423                 static int ditherr1=2<<14;
424                 static int ditherb2=3<<14;
425                 static int ditherg2=3<<14;
426                 static int ditherr2=0<<14;
427
428                 ditherb1 ^= (1^2)<<14;
429                 ditherg1 ^= (1^2)<<14;
430                 ditherr1 ^= (1^2)<<14;
431                 ditherb2 ^= (3^0)<<14;
432                 ditherg2 ^= (3^0)<<14;
433                 ditherr2 ^= (3^0)<<14;
434 #else
435                 const int ditherb1=0;
436                 const int ditherg1=0;
437                 const int ditherr1=0;
438                 const int ditherb2=0;
439                 const int ditherg2=0;
440                 const int ditherr2=0;
441 #endif
442                 for(i=0; i<(dstW>>1); i++){
443                         int j;
444                         int Y1=0;
445                         int Y2=0;
446                         int U=0;
447                         int V=0;
448                         int Cb, Cr, Cg;
449                         for(j=0; j<lumFilterSize; j++)
450                         {
451                                 Y1 += lumSrc[j][2*i] * lumFilter[j];
452                                 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
453                         }
454                         for(j=0; j<chrFilterSize; j++)
455                         {
456                                 U += chrSrc[j][i] * chrFilter[j];
457                                 V += chrSrc[j][i+2048] * chrFilter[j];
458                         }
459                         Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
460                         Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
461                         U >>= 19;
462                         V >>= 19;
463
464                         Cb= clip_yuvtab_40cf[U+ 256];
465                         Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
466                         Cr= clip_yuvtab_3343[V+ 256];
467
468                         ((uint16_t*)dest)[2*i] =
469                                 clip_table15b[(Y1 + Cb + ditherb1) >>13] |
470                                 clip_table15g[(Y1 + Cg + ditherg1) >>13] |
471                                 clip_table15r[(Y1 + Cr + ditherr1) >>13];
472
473                         ((uint16_t*)dest)[2*i+1] =
474                                 clip_table15b[(Y2 + Cb + ditherb2) >>13] |
475                                 clip_table15g[(Y2 + Cg + ditherg2) >>13] |
476                                 clip_table15r[(Y2 + Cr + ditherr2) >>13];
477                 }
478         }
479 }
480
481
482 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
483 //Plain C versions
484 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
485 #define COMPILE_C
486 #endif
487
488 #ifdef CAN_COMPILE_X86_ASM
489
490 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
491 #define COMPILE_MMX
492 #endif
493
494 #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
495 #define COMPILE_MMX2
496 #endif
497
498 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
499 #define COMPILE_3DNOW
500 #endif
501 #endif //CAN_COMPILE_X86_ASM
502
503 #undef HAVE_MMX
504 #undef HAVE_MMX2
505 #undef HAVE_3DNOW
506
507 #ifdef COMPILE_C
508 #undef HAVE_MMX
509 #undef HAVE_MMX2
510 #undef HAVE_3DNOW
511 #define RENAME(a) a ## _C
512 #include "swscale_template.c"
513 #endif
514
515 #ifdef CAN_COMPILE_X86_ASM
516
517 //X86 versions
518 /*
519 #undef RENAME
520 #undef HAVE_MMX
521 #undef HAVE_MMX2
522 #undef HAVE_3DNOW
523 #define ARCH_X86
524 #define RENAME(a) a ## _X86
525 #include "swscale_template.c"
526 */
527 //MMX versions
528 #ifdef COMPILE_MMX
529 #undef RENAME
530 #define HAVE_MMX
531 #undef HAVE_MMX2
532 #undef HAVE_3DNOW
533 #define RENAME(a) a ## _MMX
534 #include "swscale_template.c"
535 #endif
536
537 //MMX2 versions
538 #ifdef COMPILE_MMX2
539 #undef RENAME
540 #define HAVE_MMX
541 #define HAVE_MMX2
542 #undef HAVE_3DNOW
543 #define RENAME(a) a ## _MMX2
544 #include "swscale_template.c"
545 #endif
546
547 //3DNOW versions
548 #ifdef COMPILE_3DNOW
549 #undef RENAME
550 #define HAVE_MMX
551 #undef HAVE_MMX2
552 #define HAVE_3DNOW
553 #define RENAME(a) a ## _3DNow
554 #include "swscale_template.c"
555 #endif
556
557 #endif //CAN_COMPILE_X86_ASM
558
559 // minor note: the HAVE_xyz is messed up after that line so dont use it
560
561
562 // old global scaler, dont use for new code
563 // will use sws_flags from the command line
564 void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
565                              int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
566                              int srcW, int srcH, int dstW, int dstH){
567
568         static SwsContext *context=NULL;
569         int dstFormat;
570         int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
571
572         switch(dstbpp)
573         {
574                 case 8 : dstFormat= IMGFMT_Y8;          break;
575                 case 12: dstFormat= IMGFMT_YV12;        break;
576                 case 15: dstFormat= IMGFMT_BGR15;       break;
577                 case 16: dstFormat= IMGFMT_BGR16;       break;
578                 case 24: dstFormat= IMGFMT_BGR24;       break;
579                 case 32: dstFormat= IMGFMT_BGR32;       break;
580                 default: return;
581         }
582
583         if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
584
585         context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
586 }
587
588 // will use sws_flags & src_filter (from cmd line)
589 SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
590 {
591         int flags=0;
592         static int firstTime=1;
593
594 #ifdef ARCH_X86
595         if(gCpuCaps.hasMMX)
596                 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
597 #endif
598         if(firstTime)
599         {
600                 firstTime=0;
601                 flags= SWS_PRINT_INFO;
602         }
603         else if(verbose>1) flags= SWS_PRINT_INFO;
604
605         if(src_filter.lumH) freeVec(src_filter.lumH);
606         if(src_filter.lumV) freeVec(src_filter.lumV);
607         if(src_filter.chrH) freeVec(src_filter.chrH);
608         if(src_filter.chrV) freeVec(src_filter.chrV);
609
610         if(sws_lum_gblur!=0.0){
611                 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
612                 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
613         }else{
614                 src_filter.lumH= getIdentityVec();
615                 src_filter.lumV= getIdentityVec();
616         }
617
618         if(sws_chr_gblur!=0.0){
619                 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
620                 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
621         }else{
622                 src_filter.chrH= getIdentityVec();
623                 src_filter.chrV= getIdentityVec();
624         }
625
626         if(sws_chr_sharpen!=0.0){
627                 SwsVector *g= getConstVec(-1.0, 3);
628                 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
629                 g->coeff[1]=2.0;
630                 addVec(id, g);
631                 convVec(src_filter.chrH, id);
632                 convVec(src_filter.chrV, id);
633                 freeVec(g);
634                 freeVec(id);
635         }
636
637         if(sws_lum_sharpen!=0.0){
638                 SwsVector *g= getConstVec(-1.0, 3);
639                 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
640                 g->coeff[1]=2.0;
641                 addVec(id, g);
642                 convVec(src_filter.lumH, id);
643                 convVec(src_filter.lumV, id);
644                 freeVec(g);
645                 freeVec(id);
646         }
647
648         if(sws_chr_hshift)
649                 shiftVec(src_filter.chrH, sws_chr_hshift);
650
651         if(sws_chr_vshift)
652                 shiftVec(src_filter.chrV, sws_chr_vshift);
653
654         normalizeVec(src_filter.chrH, 1.0);
655         normalizeVec(src_filter.chrV, 1.0);
656         normalizeVec(src_filter.lumH, 1.0);
657         normalizeVec(src_filter.lumV, 1.0);
658
659         if(verbose > 1) printVec(src_filter.chrH);
660         if(verbose > 1) printVec(src_filter.lumH);
661
662         switch(sws_flags)
663         {
664                 case 0: flags|= SWS_FAST_BILINEAR; break;
665                 case 1: flags|= SWS_BILINEAR; break;
666                 case 2: flags|= SWS_BICUBIC; break;
667                 case 3: flags|= SWS_X; break;
668                 case 4: flags|= SWS_POINT; break;
669                 case 5: flags|= SWS_AREA; break;
670                 default:flags|= SWS_BILINEAR; break;
671         }
672
673         return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
674 }
675
676
677 static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
678                               int srcW, int dstW, int filterAlign, int one, int flags,
679                               SwsVector *srcFilter, SwsVector *dstFilter)
680 {
681         int i;
682         int filterSize;
683         int filter2Size;
684         int minFilterSize;
685         double *filter=NULL;
686         double *filter2=NULL;
687 #ifdef ARCH_X86
688         if(gCpuCaps.hasMMX)
689                 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
690 #endif
691
692         // Note the +1 is for the MMXscaler which reads over the end
693         *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
694
695         if(ABS(xInc - 0x10000) <10) // unscaled
696         {
697                 int i;
698                 filterSize= 1;
699                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
700                 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
701
702                 for(i=0; i<dstW; i++)
703                 {
704                         filter[i*filterSize]=1;
705                         (*filterPos)[i]=i;
706                 }
707
708         }
709         else if(flags&SWS_POINT) // lame looking point sampling mode
710         {
711                 int i;
712                 int xDstInSrc;
713                 filterSize= 1;
714                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
715                 
716                 xDstInSrc= xInc/2 - 0x8000;
717                 for(i=0; i<dstW; i++)
718                 {
719                         int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
720
721                         (*filterPos)[i]= xx;
722                         filter[i]= 1.0;
723                         xDstInSrc+= xInc;
724                 }
725         }
726         else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
727         {
728                 int i;
729                 int xDstInSrc;
730                 if     (flags&SWS_BICUBIC) filterSize= 4;
731                 else if(flags&SWS_X      ) filterSize= 4;
732                 else                       filterSize= 2; // SWS_BILINEAR / SWS_AREA 
733 //              printf("%d %d %d\n", filterSize, srcW, dstW);
734                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
735
736                 xDstInSrc= xInc/2 - 0x8000;
737                 for(i=0; i<dstW; i++)
738                 {
739                         int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
740                         int j;
741
742                         (*filterPos)[i]= xx;
743                         if((flags & SWS_BICUBIC) || (flags & SWS_X))
744                         {
745                                 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
746                                 double y1,y2,y3,y4;
747                                 double A= -0.6;
748                                 if(flags & SWS_BICUBIC){
749                                                 // Equation is from VirtualDub
750                                         y1 = (        +     A*d -       2.0*A*d*d +       A*d*d*d);
751                                         y2 = (+ 1.0             -     (A+3.0)*d*d + (A+2.0)*d*d*d);
752                                         y3 = (        -     A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
753                                         y4 = (                  +           A*d*d -       A*d*d*d);
754                                 }else{
755                                                 // cubic interpolation (derived it myself)
756                                         y1 = (    -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
757                                         y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
758                                         y3 = (    +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
759                                         y4 = (    -1.0*d           + 1.0*d*d*d)/6.0;
760                                 }
761
762 //                              printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
763                                 filter[i*filterSize + 0]= y1;
764                                 filter[i*filterSize + 1]= y2;
765                                 filter[i*filterSize + 2]= y3;
766                                 filter[i*filterSize + 3]= y4;
767 //                              printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
768                         }
769                         else
770                         {
771                                 //Bilinear upscale / linear interpolate / Area averaging
772                                 for(j=0; j<filterSize; j++)
773                                 {
774                                         double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
775                                         double coeff= 1.0 - d;
776                                         if(coeff<0) coeff=0;
777         //                              printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
778                                         filter[i*filterSize + j]= coeff;
779                                         xx++;
780                                 }
781                         }
782                         xDstInSrc+= xInc;
783                 }
784         }
785         else // downscale
786         {
787                 int xDstInSrc;
788                 if(flags&SWS_BICUBIC)   filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
789                 else if(flags&SWS_X)    filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
790                 else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
791                 else /* BILINEAR */     filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
792 //              printf("%d %d %d\n", *filterSize, srcW, dstW);
793                 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
794
795                 xDstInSrc= xInc/2 - 0x8000;
796                 for(i=0; i<dstW; i++)
797                 {
798                         int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
799                         int j;
800                         (*filterPos)[i]= xx;
801                         for(j=0; j<filterSize; j++)
802                         {
803                                 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
804                                 double coeff;
805                                 if((flags & SWS_BICUBIC) || (flags & SWS_X))
806                                 {
807                                         double A= -0.75;
808 //                                      d*=2;
809                                         // Equation is from VirtualDub
810                                         if(d<1.0)
811                                                 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
812                                         else if(d<2.0)
813                                                 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
814                                         else
815                                                 coeff=0.0;
816                                 }
817                                 else if(flags & SWS_AREA)
818                                 {
819                                         double srcPixelSize= (1<<16)/(double)xInc;
820                                         if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
821                                         else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
822                                         else coeff=0.0;
823                                 }
824                                 else
825                                 {
826                                         coeff= 1.0 - d;
827                                         if(coeff<0) coeff=0;
828                                 }
829 //                              printf("%1.3f %2.3f %d \n", coeff, d, xDstInSrc);
830                                 filter[i*filterSize + j]= coeff;
831                                 xx++;
832                         }
833                         xDstInSrc+= xInc;
834                 }
835         }
836
837         /* apply src & dst Filter to filter -> filter2
838            free(filter);
839         */
840         filter2Size= filterSize;
841         if(srcFilter) filter2Size+= srcFilter->length - 1;
842         if(dstFilter) filter2Size+= dstFilter->length - 1;
843         filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
844
845         for(i=0; i<dstW; i++)
846         {
847                 int j;
848                 SwsVector scaleFilter;
849                 SwsVector *outVec;
850
851                 scaleFilter.coeff= filter + i*filterSize;
852                 scaleFilter.length= filterSize;
853
854                 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
855                 else          outVec= &scaleFilter;
856
857                 ASSERT(outVec->length == filter2Size)
858                 //FIXME dstFilter
859
860                 for(j=0; j<outVec->length; j++)
861                 {
862                         filter2[i*filter2Size + j]= outVec->coeff[j];
863                 }
864
865                 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
866
867                 if(outVec != &scaleFilter) freeVec(outVec);
868         }
869         free(filter); filter=NULL;
870
871         /* try to reduce the filter-size (step1 find size and shift left) */
872         // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
873         minFilterSize= 0;
874         for(i=dstW-1; i>=0; i--)
875         {
876                 int min= filter2Size;
877                 int j;
878                 double cutOff=0.0;
879
880                 /* get rid off near zero elements on the left by shifting left */
881                 for(j=0; j<filter2Size; j++)
882                 {
883                         int k;
884                         cutOff += ABS(filter2[i*filter2Size]);
885
886                         if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
887
888                         /* preserve Monotonicity because the core cant handle the filter otherwise */
889                         if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
890
891                         // Move filter coeffs left
892                         for(k=1; k<filter2Size; k++)
893                                 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
894                         filter2[i*filter2Size + k - 1]= 0.0;
895                         (*filterPos)[i]++;
896                 }
897
898                 cutOff=0.0;
899                 /* count near zeros on the right */
900                 for(j=filter2Size-1; j>0; j--)
901                 {
902                         cutOff += ABS(filter2[i*filter2Size + j]);
903
904                         if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
905                         min--;
906                 }
907
908                 if(min>minFilterSize) minFilterSize= min;
909         }
910
911         filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
912         filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
913         *outFilterSize= filterSize;
914
915         if((flags&SWS_PRINT_INFO) && verbose)
916                 printf("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
917         /* try to reduce the filter-size (step2 reduce it) */
918         for(i=0; i<dstW; i++)
919         {
920                 int j;
921
922                 for(j=0; j<filterSize; j++)
923                 {
924                         if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
925                         else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
926                 }
927         }
928         free(filter2); filter2=NULL;
929         
930         ASSERT(filterSize > 0)
931
932         //FIXME try to align filterpos if possible
933
934         //fix borders
935         for(i=0; i<dstW; i++)
936         {
937                 int j;
938                 if((*filterPos)[i] < 0)
939                 {
940                         // Move filter coeffs left to compensate for filterPos
941                         for(j=1; j<filterSize; j++)
942                         {
943                                 int left= MAX(j + (*filterPos)[i], 0);
944                                 filter[i*filterSize + left] += filter[i*filterSize + j];
945                                 filter[i*filterSize + j]=0;
946                         }
947                         (*filterPos)[i]= 0;
948                 }
949
950                 if((*filterPos)[i] + filterSize > srcW)
951                 {
952                         int shift= (*filterPos)[i] + filterSize - srcW;
953                         // Move filter coeffs right to compensate for filterPos
954                         for(j=filterSize-2; j>=0; j--)
955                         {
956                                 int right= MIN(j + shift, filterSize-1);
957                                 filter[i*filterSize +right] += filter[i*filterSize +j];
958                                 filter[i*filterSize +j]=0;
959                         }
960                         (*filterPos)[i]= srcW - filterSize;
961                 }
962         }
963
964         // Note the +1 is for the MMXscaler which reads over the end
965         *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
966         memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
967
968         /* Normalize & Store in outFilter */
969         for(i=0; i<dstW; i++)
970         {
971                 int j;
972                 double sum=0;
973                 double scale= one;
974                 for(j=0; j<filterSize; j++)
975                 {
976                         sum+= filter[i*filterSize + j];
977                 }
978                 scale/= sum;
979                 for(j=0; j<filterSize; j++)
980                 {
981                         (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
982                 }
983         }
984         
985         (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
986         for(i=0; i<*outFilterSize; i++)
987         {
988                 int j= dstW*(*outFilterSize);
989                 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
990         }
991
992         free(filter);
993 }
994
995 #ifdef ARCH_X86
996 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
997 {
998         uint8_t *fragment;
999         int imm8OfPShufW1;
1000         int imm8OfPShufW2;
1001         int fragmentLength;
1002
1003         int xpos, i;
1004
1005         // create an optimized horizontal scaling routine
1006
1007         //code fragment
1008
1009         asm volatile(
1010                 "jmp 9f                         \n\t"
1011         // Begin
1012                 "0:                             \n\t"
1013                 "movq (%%esi), %%mm0            \n\t" //FIXME Alignment
1014                 "movq %%mm0, %%mm1              \n\t"
1015                 "psrlq $8, %%mm0                \n\t"
1016                 "punpcklbw %%mm7, %%mm1 \n\t"
1017                 "movq %%mm2, %%mm3              \n\t"
1018                 "punpcklbw %%mm7, %%mm0 \n\t"
1019                 "addw %%bx, %%cx                \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
1020                 "pshufw $0xFF, %%mm1, %%mm1     \n\t"
1021                 "1:                             \n\t"
1022                 "adcl %%edx, %%esi              \n\t" //xx+= (4*lumXInc)>>16 + carry
1023                 "pshufw $0xFF, %%mm0, %%mm0     \n\t"
1024                 "2:                             \n\t"
1025                 "psrlw $9, %%mm3                \n\t"
1026                 "psubw %%mm1, %%mm0             \n\t"
1027                 "pmullw %%mm3, %%mm0            \n\t"
1028                 "paddw %%mm6, %%mm2             \n\t" // 2*alpha += xpos&0xFFFF
1029                 "psllw $7, %%mm1                \n\t"
1030                 "paddw %%mm1, %%mm0             \n\t"
1031
1032                 "movq %%mm0, (%%edi, %%eax)     \n\t"
1033
1034                 "addl $8, %%eax                 \n\t"
1035         // End
1036                 "9:                             \n\t"
1037 //              "int $3\n\t"
1038                 "leal 0b, %0                    \n\t"
1039                 "leal 1b, %1                    \n\t"
1040                 "leal 2b, %2                    \n\t"
1041                 "decl %1                        \n\t"
1042                 "decl %2                        \n\t"
1043                 "subl %0, %1                    \n\t"
1044                 "subl %0, %2                    \n\t"
1045                 "leal 9b, %3                    \n\t"
1046                 "subl %0, %3                    \n\t"
1047                 :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
1048                 "=r" (fragmentLength)
1049         );
1050
1051         xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1052
1053         for(i=0; i<dstW/8; i++)
1054         {
1055                 int xx=xpos>>16;
1056
1057                 if((i&3) == 0)
1058                 {
1059                         int a=0;
1060                         int b=((xpos+xInc)>>16) - xx;
1061                         int c=((xpos+xInc*2)>>16) - xx;
1062                         int d=((xpos+xInc*3)>>16) - xx;
1063
1064                         memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
1065
1066                         funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
1067                         funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
1068                                 a | (b<<2) | (c<<4) | (d<<6);
1069
1070                         // if we dont need to read 8 bytes than dont :), reduces the chance of
1071                         // crossing a cache line
1072                         if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
1073
1074                         funnyCode[fragmentLength*(i+4)/4]= RET;
1075                 }
1076                 xpos+=xInc;
1077         }
1078 }
1079 #endif // ARCH_X86
1080
1081 //FIXME remove
1082 void SwScale_Init(){
1083 }
1084
1085 static void globalInit(){
1086     // generating tables:
1087     int i;
1088     for(i=0; i<768; i++){
1089         int c= MIN(MAX(i-256, 0), 255);
1090         clip_table[i]=c;
1091         yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
1092         yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
1093         yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
1094         yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
1095         yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
1096     }
1097
1098     for(i=0; i<768; i++)
1099     {
1100         int v= clip_table[i];
1101         clip_table16b[i]= le2me_16( v>>3);
1102         clip_table16g[i]= le2me_16((v<<3)&0x07E0);
1103         clip_table16r[i]= le2me_16((v<<8)&0xF800);
1104         clip_table15b[i]= le2me_16( v>>3);
1105         clip_table15g[i]= le2me_16((v<<2)&0x03E0);
1106         clip_table15r[i]= le2me_16((v<<7)&0x7C00);
1107     }
1108
1109 cpuCaps= gCpuCaps;
1110
1111 #ifdef RUNTIME_CPUDETECT
1112 #ifdef CAN_COMPILE_X86_ASM
1113         // ordered per speed fasterst first
1114         if(gCpuCaps.hasMMX2)
1115                 swScale= swScale_MMX2;
1116         else if(gCpuCaps.has3DNow)
1117                 swScale= swScale_3DNow;
1118         else if(gCpuCaps.hasMMX)
1119                 swScale= swScale_MMX;
1120         else
1121                 swScale= swScale_C;
1122
1123 #else
1124         swScale= swScale_C;
1125         cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1126 #endif
1127 #else //RUNTIME_CPUDETECT
1128 #ifdef HAVE_MMX2
1129         swScale= swScale_MMX2;
1130         cpuCaps.has3DNow = 0;
1131 #elif defined (HAVE_3DNOW)
1132         swScale= swScale_3DNow;
1133         cpuCaps.hasMMX2 = 0;
1134 #elif defined (HAVE_MMX)
1135         swScale= swScale_MMX;
1136         cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1137 #else
1138         swScale= swScale_C;
1139         cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1140 #endif
1141 #endif //!RUNTIME_CPUDETECT
1142 }
1143
1144 /* Warper functions for yuv2bgr */
1145 static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1146              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1147         uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1148
1149         if(c->srcFormat==IMGFMT_YV12)
1150                 yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1151         else /* I420 & IYUV */
1152                 yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1153 }
1154
1155 static void bgr24to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1156              int srcSliceH, uint8_t* dst[], int dstStride[]){
1157         
1158         if(dstStride[0]*3==srcStride[0]*4)
1159                 rgb24to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1160         else
1161         {
1162                 int i;
1163                 uint8_t *srcPtr= src[0];
1164                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1165
1166                 for(i=0; i<srcSliceH; i++)
1167                 {
1168                         rgb24to32(srcPtr, dstPtr, c->srcW*3);
1169                         srcPtr+= srcStride[0];
1170                         dstPtr+= dstStride[0];
1171                 }
1172         }     
1173 }
1174
1175 static void bgr32to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1176              int srcSliceH, uint8_t* dst[], int dstStride[]){
1177         
1178         if(dstStride[0]*4==srcStride[0]*3)
1179                 rgb32to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1180         else
1181         {
1182                 int i;
1183                 uint8_t *srcPtr= src[0];
1184                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1185
1186                 for(i=0; i<srcSliceH; i++)
1187                 {
1188                         rgb32to24(srcPtr, dstPtr, c->srcW<<2);
1189                         srcPtr+= srcStride[0];
1190                         dstPtr+= dstStride[0];
1191                 }
1192         }     
1193 }
1194
1195 static void bgr15to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1196              int srcSliceH, uint8_t* dst[], int dstStride[]){
1197         
1198         if(dstStride[0]==srcStride[0])
1199                 rgb15to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1200         else
1201         {
1202                 int i;
1203                 uint8_t *srcPtr= src[0];
1204                 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1205
1206                 for(i=0; i<srcSliceH; i++)
1207                 {
1208                         rgb15to16(srcPtr, dstPtr, c->srcW<<1);
1209                         srcPtr+= srcStride[0];
1210                         dstPtr+= dstStride[0];
1211                 }
1212         }     
1213 }
1214
1215
1216 /* unscaled copy like stuff (assumes nearly identical formats) */
1217 static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
1218              int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1219
1220         int srcStride[3];
1221         uint8_t *src[3];
1222         uint8_t *dst[3];
1223
1224         if(c->srcFormat == IMGFMT_I420){
1225                 src[0]= srcParam[0];
1226                 src[1]= srcParam[2];
1227                 src[2]= srcParam[1];
1228                 srcStride[0]= srcStrideParam[0];
1229                 srcStride[1]= srcStrideParam[2];
1230                 srcStride[2]= srcStrideParam[1];
1231         }
1232         else if(c->srcFormat==IMGFMT_YV12){
1233                 src[0]= srcParam[0];
1234                 src[1]= srcParam[1];
1235                 src[2]= srcParam[2];
1236                 srcStride[0]= srcStrideParam[0];
1237                 srcStride[1]= srcStrideParam[1];
1238                 srcStride[2]= srcStrideParam[2];
1239         }
1240         else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
1241                 src[0]= srcParam[0];
1242                 src[1]=
1243                 src[2]= NULL;
1244                 srcStride[0]= srcStrideParam[0];
1245                 srcStride[1]=
1246                 srcStride[2]= 0;
1247         }
1248
1249         if(c->dstFormat == IMGFMT_I420){
1250                 dst[0]= dstParam[0];
1251                 dst[1]= dstParam[2];
1252                 dst[2]= dstParam[1];
1253                 
1254         }else{
1255                 dst[0]= dstParam[0];
1256                 dst[1]= dstParam[1];
1257                 dst[2]= dstParam[2];
1258         }
1259
1260         if(isPacked(c->srcFormat))
1261         {
1262                 if(dstStride[0]==srcStride[0])
1263                         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1264                 else
1265                 {
1266                         int i;
1267                         uint8_t *srcPtr= src[0];
1268                         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1269                         int length=0;
1270
1271                         /* universal length finder */
1272                         while(length+c->srcW <= dstStride[0] 
1273                            && length+c->srcW <= srcStride[0]) length+= c->srcW;
1274                         ASSERT(length!=0);
1275
1276                         for(i=0; i<srcSliceH; i++)
1277                         {
1278                                 memcpy(dstPtr, srcPtr, length);
1279                                 srcPtr+= srcStride[0];
1280                                 dstPtr+= dstStride[0];
1281                         }
1282                 }
1283         }
1284         else 
1285         { /* Planar YUV */
1286                 int plane;
1287                 for(plane=0; plane<3; plane++)
1288                 {
1289                         int length= plane==0 ? c->srcW  : ((c->srcW+1)>>1);
1290                         int y=      plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
1291                         int height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
1292
1293                         if(dstStride[plane]==srcStride[plane])
1294                                 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1295                         else
1296                         {
1297                                 int i;
1298                                 uint8_t *srcPtr= src[plane];
1299                                 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1300                                 for(i=0; i<height; i++)
1301                                 {
1302                                         memcpy(dstPtr, srcPtr, length);
1303                                         srcPtr+= srcStride[plane];
1304                                         dstPtr+= dstStride[plane];
1305                                 }
1306                         }
1307                 }
1308         }
1309 }
1310
1311 SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1312                          SwsFilter *srcFilter, SwsFilter *dstFilter){
1313
1314         SwsContext *c;
1315         int i;
1316         int usesFilter;
1317         SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1318
1319 #ifdef ARCH_X86
1320         if(gCpuCaps.hasMMX)
1321                 asm volatile("emms\n\t"::: "memory");
1322 #endif
1323
1324         if(swScale==NULL) globalInit();
1325
1326         /* avoid dupplicate Formats, so we dont need to check to much */
1327         if(srcFormat==IMGFMT_IYUV) srcFormat=IMGFMT_I420;
1328         if(srcFormat==IMGFMT_Y8)   srcFormat=IMGFMT_Y800;
1329         if(dstFormat==IMGFMT_Y8)   dstFormat=IMGFMT_Y800;
1330
1331         if(!isSupportedIn(srcFormat)) 
1332         {
1333                 fprintf(stderr, "swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1334                 return NULL;
1335         }
1336         if(!isSupportedOut(dstFormat))
1337         {
1338                 fprintf(stderr, "swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1339                 return NULL;
1340         }
1341
1342         /* sanity check */
1343         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
1344         {
1345                 fprintf(stderr, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n", 
1346                         srcW, srcH, dstW, dstH);
1347                 return NULL;
1348         }
1349
1350         if(!dstFilter) dstFilter= &dummyFilter;
1351         if(!srcFilter) srcFilter= &dummyFilter;
1352
1353         c= memalign(64, sizeof(SwsContext));
1354         memset(c, 0, sizeof(SwsContext));
1355
1356         c->srcW= srcW;
1357         c->srcH= srcH;
1358         c->dstW= dstW;
1359         c->dstH= dstH;
1360         c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1361         c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1362         c->flags= flags;
1363         c->dstFormat= dstFormat;
1364         c->srcFormat= srcFormat;
1365
1366         usesFilter=0;
1367         if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
1368         if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
1369         if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
1370         if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
1371         if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
1372         if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
1373         if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
1374         if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
1375         
1376         /* unscaled special Cases */
1377         if(srcW==dstW && srcH==dstH && !usesFilter)
1378         {
1379                 /* yuv2bgr */
1380                 if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
1381                 {
1382                         // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
1383                         yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1384                         c->swScale= planarYuvToBgr;
1385
1386                         if(flags&SWS_PRINT_INFO)
1387                                 printf("SwScaler: using unscaled %s -> %s special converter\n", 
1388                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1389                         return c;
1390                 }
1391
1392                 /* simple copy */
1393                 if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
1394                 {
1395                         c->swScale= simpleCopy;
1396
1397                         if(flags&SWS_PRINT_INFO)
1398                                 printf("SwScaler: using unscaled %s -> %s special converter\n", 
1399                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1400                         return c;
1401                 }
1402                 
1403                 /* bgr32to24 & rgb32to24*/
1404                 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR24)
1405                  ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB24))
1406                 {
1407                         c->swScale= bgr32to24Wrapper;
1408
1409                         if(flags&SWS_PRINT_INFO)
1410                                 printf("SwScaler: using unscaled %s -> %s special converter\n", 
1411                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1412                         return c;
1413                 }
1414                 
1415                 /* bgr24to32 & rgb24to32*/
1416                 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR32)
1417                  ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB32))
1418                 {
1419                         c->swScale= bgr24to32Wrapper;
1420
1421                         if(flags&SWS_PRINT_INFO)
1422                                 printf("SwScaler: using unscaled %s -> %s special converter\n", 
1423                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1424                         return c;
1425                 }
1426
1427                 /* bgr15to16 */
1428                 if(srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR16)
1429                 {
1430                         c->swScale= bgr15to16Wrapper;
1431
1432                         if(flags&SWS_PRINT_INFO)
1433                                 printf("SwScaler: using unscaled %s -> %s special converter\n", 
1434                                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1435                         return c;
1436                 }
1437         }
1438
1439         if(cpuCaps.hasMMX2)
1440         {
1441                 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
1442                 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
1443                 {
1444                         if(flags&SWS_PRINT_INFO)
1445                                 fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
1446                 }
1447         }
1448         else
1449                 c->canMMX2BeUsed=0;
1450
1451
1452         /* dont use full vertical UV input/internaly if the source doesnt even have it */
1453         if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
1454         /* dont use full horizontal UV input if the source doesnt even have it */
1455         if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
1456         /* dont use full horizontal UV internally if the destination doesnt even have it */
1457         if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
1458
1459         if(flags&SWS_FULL_CHR_H_INP)    c->chrSrcW= srcW;
1460         else                            c->chrSrcW= (srcW+1)>>1;
1461
1462         if(flags&SWS_FULL_CHR_H_INT)    c->chrDstW= dstW;
1463         else                            c->chrDstW= (dstW+1)>>1;
1464
1465         if(flags&SWS_FULL_CHR_V)        c->chrSrcH= srcH;
1466         else                            c->chrSrcH= (srcH+1)>>1;
1467
1468         if(isHalfChrV(dstFormat))       c->chrDstH= (dstH+1)>>1;
1469         else                            c->chrDstH= dstH;
1470
1471         c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
1472         c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
1473
1474
1475         // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1476         // but only for the FAST_BILINEAR mode otherwise do correct scaling
1477         // n-2 is the last chrominance sample available
1478         // this is not perfect, but noone shuld notice the difference, the more correct variant
1479         // would be like the vertical one, but that would require some special code for the
1480         // first and last pixel
1481         if(flags&SWS_FAST_BILINEAR)
1482         {
1483                 if(c->canMMX2BeUsed)
1484                 {
1485                         c->lumXInc+= 20;
1486                         c->chrXInc+= 20;
1487                 }
1488                 //we dont use the x86asm scaler if mmx is available
1489                 else if(cpuCaps.hasMMX)
1490                 {
1491                         c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
1492                         c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
1493                 }
1494         }
1495
1496         /* precalculate horizontal scaler filter coefficients */
1497         {
1498                 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
1499
1500                 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
1501                                  srcW      ,       dstW, filterAlign, 1<<14, flags,
1502                                  srcFilter->lumH, dstFilter->lumH);
1503                 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
1504                                 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
1505                                  srcFilter->chrH, dstFilter->chrH);
1506
1507 #ifdef ARCH_X86
1508 // cant downscale !!!
1509                 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
1510                 {
1511                         initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode);
1512                         initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
1513                 }
1514 #endif
1515         } // Init Horizontal stuff
1516
1517
1518
1519         /* precalculate vertical scaler filter coefficients */
1520         initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
1521                         srcH      ,        dstH, 1, (1<<12)-4, flags,
1522                         srcFilter->lumV, dstFilter->lumV);
1523         initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
1524                         (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
1525                          srcFilter->chrV, dstFilter->chrV);
1526
1527         // Calculate Buffer Sizes so that they wont run out while handling these damn slices
1528         c->vLumBufSize= c->vLumFilterSize;
1529         c->vChrBufSize= c->vChrFilterSize;
1530         for(i=0; i<dstH; i++)
1531         {
1532                 int chrI= i*c->chrDstH / dstH;
1533                 int nextSlice= MAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
1534                                  ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
1535                 nextSlice&= ~1; // Slices start at even boundaries
1536                 if(c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
1537                         c->vLumBufSize= nextSlice - c->vLumFilterPos[i   ];
1538                 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
1539                         c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
1540         }
1541
1542         // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1543         c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
1544         c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
1545         //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)
1546         for(i=0; i<c->vLumBufSize; i++)
1547                 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
1548         for(i=0; i<c->vChrBufSize; i++)
1549                 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
1550
1551         //try to avoid drawing green stuff between the right end and the stride end
1552         for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
1553         for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
1554
1555         ASSERT(c->chrDstH <= dstH)
1556
1557         // pack filter data for mmx code
1558         if(cpuCaps.hasMMX)
1559         {
1560                 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize*      dstH*4*sizeof(int16_t));
1561                 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
1562                 for(i=0; i<c->vLumFilterSize*dstH; i++)
1563                         c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
1564                                 c->vLumFilter[i];
1565                 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
1566                         c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
1567                                 c->vChrFilter[i];
1568         }
1569
1570         if(flags&SWS_PRINT_INFO)
1571         {
1572 #ifdef DITHER1XBPP
1573                 char *dither= " dithered";
1574 #else
1575                 char *dither= "";
1576 #endif
1577                 if(flags&SWS_FAST_BILINEAR)
1578                         fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler, ");
1579                 else if(flags&SWS_BILINEAR)
1580                         fprintf(stderr, "\nSwScaler: BILINEAR scaler, ");
1581                 else if(flags&SWS_BICUBIC)
1582                         fprintf(stderr, "\nSwScaler: BICUBIC scaler, ");
1583                 else if(flags&SWS_X)
1584                         fprintf(stderr, "\nSwScaler: Experimental scaler, ");
1585                 else if(flags&SWS_POINT)
1586                         fprintf(stderr, "\nSwScaler: Nearest Neighbor / POINT scaler, ");
1587                 else if(flags&SWS_AREA)
1588                         fprintf(stderr, "\nSwScaler: Area Averageing scaler, ");
1589                 else
1590                         fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
1591
1592                 if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
1593                         fprintf(stderr, "from %s to%s %s ", 
1594                                 vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
1595                 else
1596                         fprintf(stderr, "from %s to %s ", 
1597                                 vo_format_name(srcFormat), vo_format_name(dstFormat));
1598
1599                 if(cpuCaps.hasMMX2)
1600                         fprintf(stderr, "using MMX2\n");
1601                 else if(cpuCaps.has3DNow)
1602                         fprintf(stderr, "using 3DNOW\n");
1603                 else if(cpuCaps.hasMMX)
1604                         fprintf(stderr, "using MMX\n");
1605                 else
1606                         fprintf(stderr, "using C\n");
1607         }
1608
1609         if((flags & SWS_PRINT_INFO) && verbose)
1610         {
1611                 if(cpuCaps.hasMMX)
1612                 {
1613                         if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1614                                 printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1615                         else
1616                         {
1617                                 if(c->hLumFilterSize==4)
1618                                         printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
1619                                 else if(c->hLumFilterSize==8)
1620                                         printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
1621                                 else
1622                                         printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
1623
1624                                 if(c->hChrFilterSize==4)
1625                                         printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
1626                                 else if(c->hChrFilterSize==8)
1627                                         printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
1628                                 else
1629                                         printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
1630                         }
1631                 }
1632                 else
1633                 {
1634 #ifdef ARCH_X86
1635                         printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
1636 #else
1637                         if(flags & SWS_FAST_BILINEAR)
1638                                 printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
1639                         else
1640                                 printf("SwScaler: using C scaler for horizontal scaling\n");
1641 #endif
1642                 }
1643                 if(isPlanarYUV(dstFormat))
1644                 {
1645                         if(c->vLumFilterSize==1)
1646                                 printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
1647                         else
1648                                 printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
1649                 }
1650                 else
1651                 {
1652                         if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
1653                                 printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1654                                        "SwScaler:       2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
1655                         else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
1656                                 printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1657                         else
1658                                 printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1659                 }
1660
1661                 if(dstFormat==IMGFMT_BGR24)
1662                         printf("SwScaler: using %s YV12->BGR24 Converter\n",
1663                                 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
1664                 else if(dstFormat==IMGFMT_BGR32)
1665                         printf("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1666                 else if(dstFormat==IMGFMT_BGR16)
1667                         printf("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1668                 else if(dstFormat==IMGFMT_BGR15)
1669                         printf("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1670
1671                 printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1672         }
1673         if((flags & SWS_PRINT_INFO) && verbose>1)
1674         {
1675                 printf("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1676                         c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1677                 printf("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1678                         c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
1679         }
1680
1681         c->swScale= swScale;
1682         return c;
1683 }
1684
1685 /**
1686  * returns a normalized gaussian curve used to filter stuff
1687  * quality=3 is high quality, lowwer is lowwer quality
1688  */
1689
1690 SwsVector *getGaussianVec(double variance, double quality){
1691         const int length= (int)(variance*quality + 0.5) | 1;
1692         int i;
1693         double *coeff= memalign(sizeof(double), length*sizeof(double));
1694         double middle= (length-1)*0.5;
1695         SwsVector *vec= malloc(sizeof(SwsVector));
1696
1697         vec->coeff= coeff;
1698         vec->length= length;
1699
1700         for(i=0; i<length; i++)
1701         {
1702                 double dist= i-middle;
1703                 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
1704         }
1705
1706         normalizeVec(vec, 1.0);
1707
1708         return vec;
1709 }
1710
1711 SwsVector *getConstVec(double c, int length){
1712         int i;
1713         double *coeff= memalign(sizeof(double), length*sizeof(double));
1714         SwsVector *vec= malloc(sizeof(SwsVector));
1715
1716         vec->coeff= coeff;
1717         vec->length= length;
1718
1719         for(i=0; i<length; i++)
1720                 coeff[i]= c;
1721
1722         return vec;
1723 }
1724
1725
1726 SwsVector *getIdentityVec(void){
1727         double *coeff= memalign(sizeof(double), sizeof(double));
1728         SwsVector *vec= malloc(sizeof(SwsVector));
1729         coeff[0]= 1.0;
1730
1731         vec->coeff= coeff;
1732         vec->length= 1;
1733
1734         return vec;
1735 }
1736
1737 void normalizeVec(SwsVector *a, double height){
1738         int i;
1739         double sum=0;
1740         double inv;
1741
1742         for(i=0; i<a->length; i++)
1743                 sum+= a->coeff[i];
1744
1745         inv= height/sum;
1746
1747         for(i=0; i<a->length; i++)
1748                 a->coeff[i]*= height;
1749 }
1750
1751 void scaleVec(SwsVector *a, double scalar){
1752         int i;
1753
1754         for(i=0; i<a->length; i++)
1755                 a->coeff[i]*= scalar;
1756 }
1757
1758 static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
1759         int length= a->length + b->length - 1;
1760         double *coeff= memalign(sizeof(double), length*sizeof(double));
1761         int i, j;
1762         SwsVector *vec= malloc(sizeof(SwsVector));
1763
1764         vec->coeff= coeff;
1765         vec->length= length;
1766
1767         for(i=0; i<length; i++) coeff[i]= 0.0;
1768
1769         for(i=0; i<a->length; i++)
1770         {
1771                 for(j=0; j<b->length; j++)
1772                 {
1773                         coeff[i+j]+= a->coeff[i]*b->coeff[j];
1774                 }
1775         }
1776
1777         return vec;
1778 }
1779
1780 static SwsVector *sumVec(SwsVector *a, SwsVector *b){
1781         int length= MAX(a->length, b->length);
1782         double *coeff= memalign(sizeof(double), length*sizeof(double));
1783         int i;
1784         SwsVector *vec= malloc(sizeof(SwsVector));
1785
1786         vec->coeff= coeff;
1787         vec->length= length;
1788
1789         for(i=0; i<length; i++) coeff[i]= 0.0;
1790
1791         for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1792         for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1793
1794         return vec;
1795 }
1796
1797 static SwsVector *diffVec(SwsVector *a, SwsVector *b){
1798         int length= MAX(a->length, b->length);
1799         double *coeff= memalign(sizeof(double), length*sizeof(double));
1800         int i;
1801         SwsVector *vec= malloc(sizeof(SwsVector));
1802
1803         vec->coeff= coeff;
1804         vec->length= length;
1805
1806         for(i=0; i<length; i++) coeff[i]= 0.0;
1807
1808         for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1809         for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1810
1811         return vec;
1812 }
1813
1814 /* shift left / or right if "shift" is negative */
1815 static SwsVector *getShiftedVec(SwsVector *a, int shift){
1816         int length= a->length + ABS(shift)*2;
1817         double *coeff= memalign(sizeof(double), length*sizeof(double));
1818         int i;
1819         SwsVector *vec= malloc(sizeof(SwsVector));
1820
1821         vec->coeff= coeff;
1822         vec->length= length;
1823
1824         for(i=0; i<length; i++) coeff[i]= 0.0;
1825
1826         for(i=0; i<a->length; i++)
1827         {
1828                 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1829         }
1830
1831         return vec;
1832 }
1833
1834 void shiftVec(SwsVector *a, int shift){
1835         SwsVector *shifted= getShiftedVec(a, shift);
1836         free(a->coeff);
1837         a->coeff= shifted->coeff;
1838         a->length= shifted->length;
1839         free(shifted);
1840 }
1841
1842 void addVec(SwsVector *a, SwsVector *b){
1843         SwsVector *sum= sumVec(a, b);
1844         free(a->coeff);
1845         a->coeff= sum->coeff;
1846         a->length= sum->length;
1847         free(sum);
1848 }
1849
1850 void subVec(SwsVector *a, SwsVector *b){
1851         SwsVector *diff= diffVec(a, b);
1852         free(a->coeff);
1853         a->coeff= diff->coeff;
1854         a->length= diff->length;
1855         free(diff);
1856 }
1857
1858 void convVec(SwsVector *a, SwsVector *b){
1859         SwsVector *conv= getConvVec(a, b);
1860         free(a->coeff);
1861         a->coeff= conv->coeff;
1862         a->length= conv->length;
1863         free(conv);
1864 }
1865
1866 SwsVector *cloneVec(SwsVector *a){
1867         double *coeff= memalign(sizeof(double), a->length*sizeof(double));
1868         int i;
1869         SwsVector *vec= malloc(sizeof(SwsVector));
1870
1871         vec->coeff= coeff;
1872         vec->length= a->length;
1873
1874         for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
1875
1876         return vec;
1877 }
1878
1879 void printVec(SwsVector *a){
1880         int i;
1881         double max=0;
1882         double min=0;
1883         double range;
1884
1885         for(i=0; i<a->length; i++)
1886                 if(a->coeff[i]>max) max= a->coeff[i];
1887
1888         for(i=0; i<a->length; i++)
1889                 if(a->coeff[i]<min) min= a->coeff[i];
1890
1891         range= max - min;
1892
1893         for(i=0; i<a->length; i++)
1894         {
1895                 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1896                 printf("%1.3f ", a->coeff[i]);
1897                 for(;x>0; x--) printf(" ");
1898                 printf("|\n");
1899         }
1900 }
1901
1902 void freeVec(SwsVector *a){
1903         if(!a) return;
1904         if(a->coeff) free(a->coeff);
1905         a->coeff=NULL;
1906         a->length=0;
1907         free(a);
1908 }
1909
1910 void freeSwsContext(SwsContext *c){
1911         int i;
1912
1913         if(!c) return;
1914
1915         if(c->lumPixBuf)
1916         {
1917                 for(i=0; i<c->vLumBufSize; i++)
1918                 {
1919                         if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
1920                         c->lumPixBuf[i]=NULL;
1921                 }
1922                 free(c->lumPixBuf);
1923                 c->lumPixBuf=NULL;
1924         }
1925
1926         if(c->chrPixBuf)
1927         {
1928                 for(i=0; i<c->vChrBufSize; i++)
1929                 {
1930                         if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
1931                         c->chrPixBuf[i]=NULL;
1932                 }
1933                 free(c->chrPixBuf);
1934                 c->chrPixBuf=NULL;
1935         }
1936
1937         if(c->vLumFilter) free(c->vLumFilter);
1938         c->vLumFilter = NULL;
1939         if(c->vChrFilter) free(c->vChrFilter);
1940         c->vChrFilter = NULL;
1941         if(c->hLumFilter) free(c->hLumFilter);
1942         c->hLumFilter = NULL;
1943         if(c->hChrFilter) free(c->hChrFilter);
1944         c->hChrFilter = NULL;
1945
1946         if(c->vLumFilterPos) free(c->vLumFilterPos);
1947         c->vLumFilterPos = NULL;
1948         if(c->vChrFilterPos) free(c->vChrFilterPos);
1949         c->vChrFilterPos = NULL;
1950         if(c->hLumFilterPos) free(c->hLumFilterPos);
1951         c->hLumFilterPos = NULL;
1952         if(c->hChrFilterPos) free(c->hChrFilterPos);
1953         c->hChrFilterPos = NULL;
1954
1955         if(c->lumMmxFilter) free(c->lumMmxFilter);
1956         c->lumMmxFilter = NULL;
1957         if(c->chrMmxFilter) free(c->chrMmxFilter);
1958         c->chrMmxFilter = NULL;
1959
1960         free(c);
1961 }
1962
1963