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