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