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