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