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