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