Statistics
| Branch: | Revision:

ffmpeg / postproc / swscale.c @ 6118e52e

History | View | Annotate | Download (74.7 KB)

1
/*
2
    Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3

4
    This program is free software; you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation; either version 2 of the License, or
7
    (at your option) any later version.
8

9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU General Public License for more details.
13

14
    You should have received a copy of the GNU General Public License
15
    along with this program; if not, write to the Free Software
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
*/
18

    
19
/*
20
  supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
21
  supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
22
  {BGR,RGB}{1,4,8,15,16} support dithering
23
  
24
  unscaled special converters (YV12=I420=IYUV, Y800=Y8)
25
  YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
26
  x -> x
27
  YUV9 -> YV12
28
  YUV9/YV12 -> Y800
29
  Y800 -> YUV9/YV12
30
  BGR24 -> BGR32 & RGB24 -> RGB32
31
  BGR32 -> BGR24 & RGB32 -> RGB24
32
  BGR15 -> BGR16
33
*/
34

    
35
/* 
36
tested special converters (most are tested actually but i didnt write it down ...)
37
 YV12 -> BGR16
38
 YV12 -> YV12
39
 BGR15 -> BGR16
40
 BGR16 -> BGR16
41
 YVU9 -> YV12
42

43
untested special converters
44
  YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
45
  YV12/I420 -> YV12/I420
46
  YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
47
  BGR24 -> BGR32 & RGB24 -> RGB32
48
  BGR32 -> BGR24 & RGB32 -> RGB24
49
  BGR24 -> YV12
50
*/
51

    
52
#include <inttypes.h>
53
#include <string.h>
54
#include <math.h>
55
#include <stdio.h>
56
#include <unistd.h>
57
#include "../config.h"
58
#include "../mangle.h"
59
#include <assert.h>
60
#ifdef HAVE_MALLOC_H
61
#include <malloc.h>
62
#else
63
#include <stdlib.h>
64
#endif
65
#ifdef HAVE_SYS_MMAN_H
66
#include <sys/mman.h>
67
#endif
68
#include "swscale.h"
69
#include "swscale_internal.h"
70
#include "../cpudetect.h"
71
#include "../bswap.h"
72
#include "../libvo/img_format.h"
73
#include "rgb2rgb.h"
74
#include "../libvo/fastmemcpy.h"
75

    
76
#undef MOVNTQ
77
#undef PAVGB
78

    
79
//#undef HAVE_MMX2
80
//#define HAVE_3DNOW
81
//#undef HAVE_MMX
82
//#undef ARCH_X86
83
//#define WORDS_BIGENDIAN
84
#define DITHER1XBPP
85

    
86
#define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
87

    
88
#define RET 0xC3 //near return opcode for X86
89

    
90
#ifdef MP_DEBUG
91
#define ASSERT(x) assert(x);
92
#else
93
#define ASSERT(x) ;
94
#endif
95

    
96
#ifdef M_PI
97
#define PI M_PI
98
#else
99
#define PI 3.14159265358979323846
100
#endif
101

    
102
//FIXME replace this with something faster
103
#define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YVU9 \
104
                        || (x)==IMGFMT_NV12 || (x)==IMGFMT_NV21 \
105
                        || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
106
#define isYUV(x)       ((x)==IMGFMT_UYVY || (x)==IMGFMT_YUY2 || isPlanarYUV(x))
107
#define isGray(x)      ((x)==IMGFMT_Y800)
108
#define isRGB(x)       (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
109
#define isBGR(x)       (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
110
#define isSupportedIn(x)  ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
111
                        || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
112
                        || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
113
                        || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9\
114
                        || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
115
#define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
116
                        || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P\
117
                        || isRGB(x) || isBGR(x)\
118
                        || (x)==IMGFMT_NV12 || (x)==IMGFMT_NV21\
119
                        || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
120
#define isPacked(x)    ((x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY ||isRGB(x) || isBGR(x))
121

    
122
#define RGB2YUV_SHIFT 16
123
#define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
124
#define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
125
#define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
126
#define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
127
#define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
128
#define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
129
#define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
130
#define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
131
#define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
132

    
133
extern const int32_t Inverse_Table_6_9[8][4];
134

    
135
/*
136
NOTES
137
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
138

139
TODO
140
more intelligent missalignment avoidance for the horizontal scaler
141
write special vertical cubic upscale version
142
Optimize C code (yv12 / minmax)
143
add support for packed pixel yuv input & output
144
add support for Y8 output
145
optimize bgr24 & bgr32
146
add BGR4 output support
147
write special BGR->BGR scaler
148
*/
149

    
150
#define ABS(a) ((a) > 0 ? (a) : (-(a)))
151
#define MIN(a,b) ((a) > (b) ? (b) : (a))
152
#define MAX(a,b) ((a) < (b) ? (b) : (a))
153

    
154
#if defined(ARCH_X86) || defined(ARCH_X86_64)
155
static uint64_t attribute_used __attribute__((aligned(8))) bF8=       0xF8F8F8F8F8F8F8F8LL;
156
static uint64_t attribute_used __attribute__((aligned(8))) bFC=       0xFCFCFCFCFCFCFCFCLL;
157
static uint64_t __attribute__((aligned(8))) w10=       0x0010001000100010LL;
158
static uint64_t attribute_used __attribute__((aligned(8))) w02=       0x0002000200020002LL;
159
static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
160
static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
161
static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
162
static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
163

    
164
static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
165
static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
166
static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
167
static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
168

    
169
static uint64_t __attribute__((aligned(8))) dither4[2]={
170
        0x0103010301030103LL,
171
        0x0200020002000200LL,};
172

    
173
static uint64_t __attribute__((aligned(8))) dither8[2]={
174
        0x0602060206020602LL,
175
        0x0004000400040004LL,};
176

    
177
static uint64_t __attribute__((aligned(8))) b16Mask=   0x001F001F001F001FLL;
178
static uint64_t attribute_used __attribute__((aligned(8))) g16Mask=   0x07E007E007E007E0LL;
179
static uint64_t attribute_used __attribute__((aligned(8))) r16Mask=   0xF800F800F800F800LL;
180
static uint64_t __attribute__((aligned(8))) b15Mask=   0x001F001F001F001FLL;
181
static uint64_t attribute_used __attribute__((aligned(8))) g15Mask=   0x03E003E003E003E0LL;
182
static uint64_t attribute_used __attribute__((aligned(8))) r15Mask=   0x7C007C007C007C00LL;
183

    
184
static uint64_t attribute_used __attribute__((aligned(8))) M24A=   0x00FF0000FF0000FFLL;
185
static uint64_t attribute_used __attribute__((aligned(8))) M24B=   0xFF0000FF0000FF00LL;
186
static uint64_t attribute_used __attribute__((aligned(8))) M24C=   0x0000FF0000FF0000LL;
187

    
188
#ifdef FAST_BGR2YV12
189
static const uint64_t bgr2YCoeff  attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
190
static const uint64_t bgr2UCoeff  attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
191
static const uint64_t bgr2VCoeff  attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
192
#else
193
static const uint64_t bgr2YCoeff  attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
194
static const uint64_t bgr2UCoeff  attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
195
static const uint64_t bgr2VCoeff  attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
196
#endif
197
static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
198
static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
199
static const uint64_t w1111       attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
200
#endif
201

    
202
// clipping helper table for C implementations:
203
static unsigned char clip_table[768];
204

    
205
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
206
                  
207
extern const uint8_t dither_2x2_4[2][8];
208
extern const uint8_t dither_2x2_8[2][8];
209
extern const uint8_t dither_8x8_32[8][8];
210
extern const uint8_t dither_8x8_73[8][8];
211
extern const uint8_t dither_8x8_220[8][8];
212

    
213
#if defined(ARCH_X86) || defined(ARCH_X86_64)
214
void in_asm_used_var_warning_killer()
215
{
216
 volatile int i= bF8+bFC+w10+
217
 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
218
 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
219
 if(i) i=0;
220
}
221
#endif
222

    
223
static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
224
                                    int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
225
                                    uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
226
{
227
        //FIXME Optimize (just quickly writen not opti..)
228
        int i;
229
        for(i=0; i<dstW; i++)
230
        {
231
                int val=1<<18;
232
                int j;
233
                for(j=0; j<lumFilterSize; j++)
234
                        val += lumSrc[j][i] * lumFilter[j];
235

    
236
                dest[i]= MIN(MAX(val>>19, 0), 255);
237
        }
238

    
239
        if(uDest != NULL)
240
                for(i=0; i<chrDstW; i++)
241
                {
242
                        int u=1<<18;
243
                        int v=1<<18;
244
                        int j;
245
                        for(j=0; j<chrFilterSize; j++)
246
                        {
247
                                u += chrSrc[j][i] * chrFilter[j];
248
                                v += chrSrc[j][i + 2048] * chrFilter[j];
249
                        }
250

    
251
                        uDest[i]= MIN(MAX(u>>19, 0), 255);
252
                        vDest[i]= MIN(MAX(v>>19, 0), 255);
253
                }
254
}
255

    
256
static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
257
                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
258
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
259
{
260
        //FIXME Optimize (just quickly writen not opti..)
261
        int i;
262
        for(i=0; i<dstW; i++)
263
        {
264
                int val=1<<18;
265
                int j;
266
                for(j=0; j<lumFilterSize; j++)
267
                        val += lumSrc[j][i] * lumFilter[j];
268

    
269
                dest[i]= MIN(MAX(val>>19, 0), 255);
270
        }
271

    
272
        if(uDest == NULL)
273
                return;
274

    
275
        if(dstFormat == IMGFMT_NV12)
276
                for(i=0; i<chrDstW; i++)
277
                {
278
                        int u=1<<18;
279
                        int v=1<<18;
280
                        int j;
281
                        for(j=0; j<chrFilterSize; j++)
282
                        {
283
                                u += chrSrc[j][i] * chrFilter[j];
284
                                v += chrSrc[j][i + 2048] * chrFilter[j];
285
                        }
286

    
287
                        uDest[2*i]= MIN(MAX(u>>19, 0), 255);
288
                        uDest[2*i+1]= MIN(MAX(v>>19, 0), 255);
289
                }
290
        else
291
                for(i=0; i<chrDstW; i++)
292
                {
293
                        int u=1<<18;
294
                        int v=1<<18;
295
                        int j;
296
                        for(j=0; j<chrFilterSize; j++)
297
                        {
298
                                u += chrSrc[j][i] * chrFilter[j];
299
                                v += chrSrc[j][i + 2048] * chrFilter[j];
300
                        }
301

    
302
                        uDest[2*i]= MIN(MAX(v>>19, 0), 255);
303
                        uDest[2*i+1]= MIN(MAX(u>>19, 0), 255);
304
                }
305
}
306

    
307
#define YSCALE_YUV_2_PACKEDX_C(type) \
308
                for(i=0; i<(dstW>>1); i++){\
309
                        int j;\
310
                        int Y1=1<<18;\
311
                        int Y2=1<<18;\
312
                        int U=1<<18;\
313
                        int V=1<<18;\
314
                        type *r, *b, *g;\
315
                        const int i2= 2*i;\
316
                        \
317
                        for(j=0; j<lumFilterSize; j++)\
318
                        {\
319
                                Y1 += lumSrc[j][i2] * lumFilter[j];\
320
                                Y2 += lumSrc[j][i2+1] * lumFilter[j];\
321
                        }\
322
                        for(j=0; j<chrFilterSize; j++)\
323
                        {\
324
                                U += chrSrc[j][i] * chrFilter[j];\
325
                                V += chrSrc[j][i+2048] * chrFilter[j];\
326
                        }\
327
                        Y1>>=19;\
328
                        Y2>>=19;\
329
                        U >>=19;\
330
                        V >>=19;\
331
                        if((Y1|Y2|U|V)&256)\
332
                        {\
333
                                if(Y1>255)   Y1=255;\
334
                                else if(Y1<0)Y1=0;\
335
                                if(Y2>255)   Y2=255;\
336
                                else if(Y2<0)Y2=0;\
337
                                if(U>255)    U=255;\
338
                                else if(U<0) U=0;\
339
                                if(V>255)    V=255;\
340
                                else if(V<0) V=0;\
341
                        }
342
                        
343
#define YSCALE_YUV_2_RGBX_C(type) \
344
                        YSCALE_YUV_2_PACKEDX_C(type)\
345
                        r = c->table_rV[V];\
346
                        g = c->table_gU[U] + c->table_gV[V];\
347
                        b = c->table_bU[U];\
348

    
349
#define YSCALE_YUV_2_PACKED2_C \
350
                for(i=0; i<(dstW>>1); i++){\
351
                        const int i2= 2*i;\
352
                        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;\
353
                        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
354
                        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;\
355
                        int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
356

    
357
#define YSCALE_YUV_2_RGB2_C(type) \
358
                        YSCALE_YUV_2_PACKED2_C\
359
                        type *r, *b, *g;\
360
                        r = c->table_rV[V];\
361
                        g = c->table_gU[U] + c->table_gV[V];\
362
                        b = c->table_bU[U];\
363

    
364
#define YSCALE_YUV_2_PACKED1_C \
365
                for(i=0; i<(dstW>>1); i++){\
366
                        const int i2= 2*i;\
367
                        int Y1= buf0[i2  ]>>7;\
368
                        int Y2= buf0[i2+1]>>7;\
369
                        int U= (uvbuf1[i     ])>>7;\
370
                        int V= (uvbuf1[i+2048])>>7;\
371

    
372
#define YSCALE_YUV_2_RGB1_C(type) \
373
                        YSCALE_YUV_2_PACKED1_C\
374
                        type *r, *b, *g;\
375
                        r = c->table_rV[V];\
376
                        g = c->table_gU[U] + c->table_gV[V];\
377
                        b = c->table_bU[U];\
378

    
379
#define YSCALE_YUV_2_PACKED1B_C \
380
                for(i=0; i<(dstW>>1); i++){\
381
                        const int i2= 2*i;\
382
                        int Y1= buf0[i2  ]>>7;\
383
                        int Y2= buf0[i2+1]>>7;\
384
                        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
385
                        int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
386

    
387
#define YSCALE_YUV_2_RGB1B_C(type) \
388
                        YSCALE_YUV_2_PACKED1B_C\
389
                        type *r, *b, *g;\
390
                        r = c->table_rV[V];\
391
                        g = c->table_gU[U] + c->table_gV[V];\
392
                        b = c->table_bU[U];\
393

    
394
#define YSCALE_YUV_2_ANYRGB_C(func, func2)\
395
        switch(c->dstFormat)\
396
        {\
397
        case IMGFMT_BGR32:\
398
        case IMGFMT_RGB32:\
399
                func(uint32_t)\
400
                        ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
401
                        ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
402
                }                \
403
                break;\
404
        case IMGFMT_RGB24:\
405
                func(uint8_t)\
406
                        ((uint8_t*)dest)[0]= r[Y1];\
407
                        ((uint8_t*)dest)[1]= g[Y1];\
408
                        ((uint8_t*)dest)[2]= b[Y1];\
409
                        ((uint8_t*)dest)[3]= r[Y2];\
410
                        ((uint8_t*)dest)[4]= g[Y2];\
411
                        ((uint8_t*)dest)[5]= b[Y2];\
412
                        dest+=6;\
413
                }\
414
                break;\
415
        case IMGFMT_BGR24:\
416
                func(uint8_t)\
417
                        ((uint8_t*)dest)[0]= b[Y1];\
418
                        ((uint8_t*)dest)[1]= g[Y1];\
419
                        ((uint8_t*)dest)[2]= r[Y1];\
420
                        ((uint8_t*)dest)[3]= b[Y2];\
421
                        ((uint8_t*)dest)[4]= g[Y2];\
422
                        ((uint8_t*)dest)[5]= r[Y2];\
423
                        dest+=6;\
424
                }\
425
                break;\
426
        case IMGFMT_RGB16:\
427
        case IMGFMT_BGR16:\
428
                {\
429
                        const int dr1= dither_2x2_8[y&1    ][0];\
430
                        const int dg1= dither_2x2_4[y&1    ][0];\
431
                        const int db1= dither_2x2_8[(y&1)^1][0];\
432
                        const int dr2= dither_2x2_8[y&1    ][1];\
433
                        const int dg2= dither_2x2_4[y&1    ][1];\
434
                        const int db2= dither_2x2_8[(y&1)^1][1];\
435
                        func(uint16_t)\
436
                                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
437
                                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
438
                        }\
439
                }\
440
                break;\
441
        case IMGFMT_RGB15:\
442
        case IMGFMT_BGR15:\
443
                {\
444
                        const int dr1= dither_2x2_8[y&1    ][0];\
445
                        const int dg1= dither_2x2_8[y&1    ][1];\
446
                        const int db1= dither_2x2_8[(y&1)^1][0];\
447
                        const int dr2= dither_2x2_8[y&1    ][1];\
448
                        const int dg2= dither_2x2_8[y&1    ][0];\
449
                        const int db2= dither_2x2_8[(y&1)^1][1];\
450
                        func(uint16_t)\
451
                                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
452
                                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
453
                        }\
454
                }\
455
                break;\
456
        case IMGFMT_RGB8:\
457
        case IMGFMT_BGR8:\
458
                {\
459
                        const uint8_t * const d64= dither_8x8_73[y&7];\
460
                        const uint8_t * const d32= dither_8x8_32[y&7];\
461
                        func(uint8_t)\
462
                                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
463
                                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
464
                        }\
465
                }\
466
                break;\
467
        case IMGFMT_RGB4:\
468
        case IMGFMT_BGR4:\
469
                {\
470
                        const uint8_t * const d64= dither_8x8_73 [y&7];\
471
                        const uint8_t * const d128=dither_8x8_220[y&7];\
472
                        func(uint8_t)\
473
                                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
474
                                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
475
                        }\
476
                }\
477
                break;\
478
        case IMGFMT_RG4B:\
479
        case IMGFMT_BG4B:\
480
                {\
481
                        const uint8_t * const d64= dither_8x8_73 [y&7];\
482
                        const uint8_t * const d128=dither_8x8_220[y&7];\
483
                        func(uint8_t)\
484
                                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
485
                                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
486
                        }\
487
                }\
488
                break;\
489
        case IMGFMT_RGB1:\
490
        case IMGFMT_BGR1:\
491
                {\
492
                        const uint8_t * const d128=dither_8x8_220[y&7];\
493
                        uint8_t *g= c->table_gU[128] + c->table_gV[128];\
494
                        for(i=0; i<dstW-7; i+=8){\
495
                                int acc;\
496
                                acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
497
                                acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
498
                                acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
499
                                acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
500
                                acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
501
                                acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
502
                                acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
503
                                acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
504
                                ((uint8_t*)dest)[0]= acc;\
505
                                dest++;\
506
                        }\
507
\
508
/*\
509
((uint8_t*)dest)-= dstW>>4;\
510
{\
511
                        int acc=0;\
512
                        int left=0;\
513
                        static int top[1024];\
514
                        static int last_new[1024][1024];\
515
                        static int last_in3[1024][1024];\
516
                        static int drift[1024][1024];\
517
                        int topLeft=0;\
518
                        int shift=0;\
519
                        int count=0;\
520
                        const uint8_t * const d128=dither_8x8_220[y&7];\
521
                        int error_new=0;\
522
                        int error_in3=0;\
523
                        int f=0;\
524
                        \
525
                        for(i=dstW>>1; i<dstW; i++){\
526
                                int in= ((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19);\
527
                                int in2 = (76309 * (in - 16) + 32768) >> 16;\
528
                                int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
529
                                int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
530
                                        + (last_new[y][i] - in3)*f/256;\
531
                                int new= old> 128 ? 255 : 0;\
532
\
533
                                error_new+= ABS(last_new[y][i] - new);\
534
                                error_in3+= ABS(last_in3[y][i] - in3);\
535
                                f= error_new - error_in3*4;\
536
                                if(f<0) f=0;\
537
                                if(f>256) f=256;\
538
\
539
                                topLeft= top[i];\
540
                                left= top[i]= old - new;\
541
                                last_new[y][i]= new;\
542
                                last_in3[y][i]= in3;\
543
\
544
                                acc+= acc + (new&1);\
545
                                if((i&7)==6){\
546
                                        ((uint8_t*)dest)[0]= acc;\
547
                                        ((uint8_t*)dest)++;\
548
                                }\
549
                        }\
550
}\
551
*/\
552
                }\
553
                break;\
554
        case IMGFMT_YUY2:\
555
                func2\
556
                        ((uint8_t*)dest)[2*i2+0]= Y1;\
557
                        ((uint8_t*)dest)[2*i2+1]= U;\
558
                        ((uint8_t*)dest)[2*i2+2]= Y2;\
559
                        ((uint8_t*)dest)[2*i2+3]= V;\
560
                }                \
561
                break;\
562
        case IMGFMT_UYVY:\
563
                func2\
564
                        ((uint8_t*)dest)[2*i2+0]= U;\
565
                        ((uint8_t*)dest)[2*i2+1]= Y1;\
566
                        ((uint8_t*)dest)[2*i2+2]= V;\
567
                        ((uint8_t*)dest)[2*i2+3]= Y2;\
568
                }                \
569
                break;\
570
        }\
571

    
572

    
573
static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
574
                                    int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
575
                                    uint8_t *dest, int dstW, int y)
576
{
577
        int i;
578
        switch(c->dstFormat)
579
        {
580
        case IMGFMT_RGB32:
581
        case IMGFMT_BGR32:
582
                YSCALE_YUV_2_RGBX_C(uint32_t)
583
                        ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
584
                        ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
585
                }
586
                break;
587
        case IMGFMT_RGB24:
588
                YSCALE_YUV_2_RGBX_C(uint8_t)
589
                        ((uint8_t*)dest)[0]= r[Y1];
590
                        ((uint8_t*)dest)[1]= g[Y1];
591
                        ((uint8_t*)dest)[2]= b[Y1];
592
                        ((uint8_t*)dest)[3]= r[Y2];
593
                        ((uint8_t*)dest)[4]= g[Y2];
594
                        ((uint8_t*)dest)[5]= b[Y2];
595
                        dest+=6;
596
                }
597
                break;
598
        case IMGFMT_BGR24:
599
                YSCALE_YUV_2_RGBX_C(uint8_t)
600
                        ((uint8_t*)dest)[0]= b[Y1];
601
                        ((uint8_t*)dest)[1]= g[Y1];
602
                        ((uint8_t*)dest)[2]= r[Y1];
603
                        ((uint8_t*)dest)[3]= b[Y2];
604
                        ((uint8_t*)dest)[4]= g[Y2];
605
                        ((uint8_t*)dest)[5]= r[Y2];
606
                        dest+=6;
607
                }
608
                break;
609
        case IMGFMT_RGB16:
610
        case IMGFMT_BGR16:
611
                {
612
                        const int dr1= dither_2x2_8[y&1    ][0];
613
                        const int dg1= dither_2x2_4[y&1    ][0];
614
                        const int db1= dither_2x2_8[(y&1)^1][0];
615
                        const int dr2= dither_2x2_8[y&1    ][1];
616
                        const int dg2= dither_2x2_4[y&1    ][1];
617
                        const int db2= dither_2x2_8[(y&1)^1][1];
618
                        YSCALE_YUV_2_RGBX_C(uint16_t)
619
                                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
620
                                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
621
                        }
622
                }
623
                break;
624
        case IMGFMT_RGB15:
625
        case IMGFMT_BGR15:
626
                {
627
                        const int dr1= dither_2x2_8[y&1    ][0];
628
                        const int dg1= dither_2x2_8[y&1    ][1];
629
                        const int db1= dither_2x2_8[(y&1)^1][0];
630
                        const int dr2= dither_2x2_8[y&1    ][1];
631
                        const int dg2= dither_2x2_8[y&1    ][0];
632
                        const int db2= dither_2x2_8[(y&1)^1][1];
633
                        YSCALE_YUV_2_RGBX_C(uint16_t)
634
                                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
635
                                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
636
                        }
637
                }
638
                break;
639
        case IMGFMT_RGB8:
640
        case IMGFMT_BGR8:
641
                {
642
                        const uint8_t * const d64= dither_8x8_73[y&7];
643
                        const uint8_t * const d32= dither_8x8_32[y&7];
644
                        YSCALE_YUV_2_RGBX_C(uint8_t)
645
                                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
646
                                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
647
                        }
648
                }
649
                break;
650
        case IMGFMT_RGB4:
651
        case IMGFMT_BGR4:
652
                {
653
                        const uint8_t * const d64= dither_8x8_73 [y&7];
654
                        const uint8_t * const d128=dither_8x8_220[y&7];
655
                        YSCALE_YUV_2_RGBX_C(uint8_t)
656
                                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
657
                                                  +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
658
                        }
659
                }
660
                break;
661
        case IMGFMT_RG4B:
662
        case IMGFMT_BG4B:
663
                {
664
                        const uint8_t * const d64= dither_8x8_73 [y&7];
665
                        const uint8_t * const d128=dither_8x8_220[y&7];
666
                        YSCALE_YUV_2_RGBX_C(uint8_t)
667
                                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
668
                                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
669
                        }
670
                }
671
                break;
672
        case IMGFMT_RGB1:
673
        case IMGFMT_BGR1:
674
                {
675
                        const uint8_t * const d128=dither_8x8_220[y&7];
676
                        uint8_t *g= c->table_gU[128] + c->table_gV[128];
677
                        int acc=0;
678
                        for(i=0; i<dstW-1; i+=2){
679
                                int j;
680
                                int Y1=1<<18;
681
                                int Y2=1<<18;
682

    
683
                                for(j=0; j<lumFilterSize; j++)
684
                                {
685
                                        Y1 += lumSrc[j][i] * lumFilter[j];
686
                                        Y2 += lumSrc[j][i+1] * lumFilter[j];
687
                                }
688
                                Y1>>=19;
689
                                Y2>>=19;
690
                                if((Y1|Y2)&256)
691
                                {
692
                                        if(Y1>255)   Y1=255;
693
                                        else if(Y1<0)Y1=0;
694
                                        if(Y2>255)   Y2=255;
695
                                        else if(Y2<0)Y2=0;
696
                                }
697
                                acc+= acc + g[Y1+d128[(i+0)&7]];
698
                                acc+= acc + g[Y2+d128[(i+1)&7]];
699
                                if((i&7)==6){
700
                                        ((uint8_t*)dest)[0]= acc;
701
                                        dest++;
702
                                }
703
                        }
704
                }
705
                break;
706
        case IMGFMT_YUY2:
707
                YSCALE_YUV_2_PACKEDX_C(void)
708
                        ((uint8_t*)dest)[2*i2+0]= Y1;
709
                        ((uint8_t*)dest)[2*i2+1]= U;
710
                        ((uint8_t*)dest)[2*i2+2]= Y2;
711
                        ((uint8_t*)dest)[2*i2+3]= V;
712
                }
713
                break;
714
        case IMGFMT_UYVY:
715
                YSCALE_YUV_2_PACKEDX_C(void)
716
                        ((uint8_t*)dest)[2*i2+0]= U;
717
                        ((uint8_t*)dest)[2*i2+1]= Y1;
718
                        ((uint8_t*)dest)[2*i2+2]= V;
719
                        ((uint8_t*)dest)[2*i2+3]= Y2;
720
                }
721
                break;
722
        }
723
}
724

    
725

    
726
//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
727
//Plain C versions
728
#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
729
#define COMPILE_C
730
#endif
731

    
732
#ifdef ARCH_POWERPC
733
#ifdef HAVE_ALTIVEC
734
#define COMPILE_ALTIVEC
735
#endif //HAVE_ALTIVEC
736
#endif //ARCH_POWERPC
737

    
738
#if defined(ARCH_X86) || defined(ARCH_X86_64)
739

    
740
#if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
741
#define COMPILE_MMX
742
#endif
743

    
744
#if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
745
#define COMPILE_MMX2
746
#endif
747

    
748
#if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
749
#define COMPILE_3DNOW
750
#endif
751
#endif //ARCH_X86 || ARCH_X86_64
752

    
753
#undef HAVE_MMX
754
#undef HAVE_MMX2
755
#undef HAVE_3DNOW
756

    
757
#ifdef COMPILE_C
758
#undef HAVE_MMX
759
#undef HAVE_MMX2
760
#undef HAVE_3DNOW
761
#undef HAVE_ALTIVEC
762
#define RENAME(a) a ## _C
763
#include "swscale_template.c"
764
#endif
765

    
766
#ifdef ARCH_POWERPC
767
#ifdef COMPILE_ALTIVEC
768
#undef RENAME
769
#define HAVE_ALTIVEC
770
#define RENAME(a) a ## _altivec
771
#include "swscale_template.c"
772
#endif
773
#endif //ARCH_POWERPC
774

    
775
#if defined(ARCH_X86) || defined(ARCH_X86_64)
776

    
777
//X86 versions
778
/*
779
#undef RENAME
780
#undef HAVE_MMX
781
#undef HAVE_MMX2
782
#undef HAVE_3DNOW
783
#define ARCH_X86
784
#define RENAME(a) a ## _X86
785
#include "swscale_template.c"
786
*/
787
//MMX versions
788
#ifdef COMPILE_MMX
789
#undef RENAME
790
#define HAVE_MMX
791
#undef HAVE_MMX2
792
#undef HAVE_3DNOW
793
#define RENAME(a) a ## _MMX
794
#include "swscale_template.c"
795
#endif
796

    
797
//MMX2 versions
798
#ifdef COMPILE_MMX2
799
#undef RENAME
800
#define HAVE_MMX
801
#define HAVE_MMX2
802
#undef HAVE_3DNOW
803
#define RENAME(a) a ## _MMX2
804
#include "swscale_template.c"
805
#endif
806

    
807
//3DNOW versions
808
#ifdef COMPILE_3DNOW
809
#undef RENAME
810
#define HAVE_MMX
811
#undef HAVE_MMX2
812
#define HAVE_3DNOW
813
#define RENAME(a) a ## _3DNow
814
#include "swscale_template.c"
815
#endif
816

    
817
#endif //ARCH_X86 || ARCH_X86_64
818

    
819
// minor note: the HAVE_xyz is messed up after that line so don't use it
820

    
821
static double getSplineCoeff(double a, double b, double c, double d, double dist)
822
{
823
//        printf("%f %f %f %f %f\n", a,b,c,d,dist);
824
        if(dist<=1.0)         return ((d*dist + c)*dist + b)*dist +a;
825
        else                return getSplineCoeff(        0.0, 
826
                                                 b+ 2.0*c + 3.0*d,
827
                                                        c + 3.0*d,
828
                                                -b- 3.0*c - 6.0*d,
829
                                                dist-1.0);
830
}
831

    
832
static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
833
                              int srcW, int dstW, int filterAlign, int one, int flags,
834
                              SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
835
{
836
        int i;
837
        int filterSize;
838
        int filter2Size;
839
        int minFilterSize;
840
        double *filter=NULL;
841
        double *filter2=NULL;
842
#if defined(ARCH_X86) || defined(ARCH_X86_64)
843
        if(flags & SWS_CPU_CAPS_MMX)
844
                asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
845
#endif
846

    
847
        // Note the +1 is for the MMXscaler which reads over the end
848
        *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
849

    
850
        if(ABS(xInc - 0x10000) <10) // unscaled
851
        {
852
                int i;
853
                filterSize= 1;
854
                filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
855
                for(i=0; i<dstW*filterSize; i++) filter[i]=0;
856

    
857
                for(i=0; i<dstW; i++)
858
                {
859
                        filter[i*filterSize]=1;
860
                        (*filterPos)[i]=i;
861
                }
862

    
863
        }
864
        else if(flags&SWS_POINT) // lame looking point sampling mode
865
        {
866
                int i;
867
                int xDstInSrc;
868
                filterSize= 1;
869
                filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
870
                
871
                xDstInSrc= xInc/2 - 0x8000;
872
                for(i=0; i<dstW; i++)
873
                {
874
                        int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
875

    
876
                        (*filterPos)[i]= xx;
877
                        filter[i]= 1.0;
878
                        xDstInSrc+= xInc;
879
                }
880
        }
881
        else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
882
        {
883
                int i;
884
                int xDstInSrc;
885
                if     (flags&SWS_BICUBIC) filterSize= 4;
886
                else if(flags&SWS_X      ) filterSize= 4;
887
                else                           filterSize= 2; // SWS_BILINEAR / SWS_AREA 
888
                filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
889

    
890
                xDstInSrc= xInc/2 - 0x8000;
891
                for(i=0; i<dstW; i++)
892
                {
893
                        int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
894
                        int j;
895

    
896
                        (*filterPos)[i]= xx;
897
                                //Bilinear upscale / linear interpolate / Area averaging
898
                                for(j=0; j<filterSize; j++)
899
                                {
900
                                        double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
901
                                        double coeff= 1.0 - d;
902
                                        if(coeff<0) coeff=0;
903
                                        filter[i*filterSize + j]= coeff;
904
                                        xx++;
905
                                }
906
                        xDstInSrc+= xInc;
907
                }
908
        }
909
        else
910
        {
911
                double xDstInSrc;
912
                double sizeFactor, filterSizeInSrc;
913
                const double xInc1= (double)xInc / (double)(1<<16);
914

    
915
                if     (flags&SWS_BICUBIC)        sizeFactor= 4.0;
916
                else if(flags&SWS_X)                sizeFactor= 8.0;
917
                else if(flags&SWS_AREA)                sizeFactor= 1.0; //downscale only, for upscale it is bilinear
918
                else if(flags&SWS_GAUSS)        sizeFactor= 8.0;   // infinite ;)
919
                else if(flags&SWS_LANCZOS)        sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
920
                else if(flags&SWS_SINC)                sizeFactor= 20.0; // infinite ;)
921
                else if(flags&SWS_SPLINE)        sizeFactor= 20.0;  // infinite ;)
922
                else if(flags&SWS_BILINEAR)        sizeFactor= 2.0;
923
                else {
924
                        sizeFactor= 0.0; //GCC warning killer
925
                        ASSERT(0)
926
                }
927
                
928
                if(xInc1 <= 1.0)        filterSizeInSrc= sizeFactor; // upscale
929
                else                        filterSizeInSrc= sizeFactor*srcW / (double)dstW;
930

    
931
                filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
932
                if(filterSize > srcW-2) filterSize=srcW-2;
933

    
934
                filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);
935

    
936
                xDstInSrc= xInc1 / 2.0 - 0.5;
937
                for(i=0; i<dstW; i++)
938
                {
939
                        int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
940
                        int j;
941
                        (*filterPos)[i]= xx;
942
                        for(j=0; j<filterSize; j++)
943
                        {
944
                                double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
945
                                double coeff;
946
                                if(flags & SWS_BICUBIC)
947
                                {
948
                                        double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
949
                                        double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
950

    
951
                                        if(d<1.0) 
952
                                                coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
953
                                        else if(d<2.0)
954
                                                coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
955
                                        else
956
                                                coeff=0.0;
957
                                }
958
/*                                else if(flags & SWS_X)
959
                                {
960
                                        double p= param ? param*0.01 : 0.3;
961
                                        coeff = d ? sin(d*PI)/(d*PI) : 1.0;
962
                                        coeff*= pow(2.0, - p*d*d);
963
                                }*/
964
                                else if(flags & SWS_X)
965
                                {
966
                                        double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
967
                                        
968
                                        if(d<1.0)
969
                                                coeff = cos(d*PI);
970
                                        else
971
                                                coeff=-1.0;
972
                                        if(coeff<0.0)         coeff= -pow(-coeff, A);
973
                                        else                coeff=  pow( coeff, A);
974
                                        coeff= coeff*0.5 + 0.5;
975
                                }
976
                                else if(flags & SWS_AREA)
977
                                {
978
                                        double srcPixelSize= 1.0/xInc1;
979
                                        if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
980
                                        else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
981
                                        else coeff=0.0;
982
                                }
983
                                else if(flags & SWS_GAUSS)
984
                                {
985
                                        double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
986
                                        coeff = pow(2.0, - p*d*d);
987
                                }
988
                                else if(flags & SWS_SINC)
989
                                {
990
                                        coeff = d ? sin(d*PI)/(d*PI) : 1.0;
991
                                }
992
                                else if(flags & SWS_LANCZOS)
993
                                {
994
                                        double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0; 
995
                                        coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
996
                                        if(d>p) coeff=0;
997
                                }
998
                                else if(flags & SWS_BILINEAR)
999
                                {
1000
                                        coeff= 1.0 - d;
1001
                                        if(coeff<0) coeff=0;
1002
                                }
1003
                                else if(flags & SWS_SPLINE)
1004
                                {
1005
                                        double p=-2.196152422706632;
1006
                                        coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1007
                                }
1008
                                else {
1009
                                        coeff= 0.0; //GCC warning killer
1010
                                        ASSERT(0)
1011
                                }
1012

    
1013
                                filter[i*filterSize + j]= coeff;
1014
                                xx++;
1015
                        }
1016
                        xDstInSrc+= xInc1;
1017
                }
1018
        }
1019

    
1020
        /* apply src & dst Filter to filter -> filter2
1021
           free(filter);
1022
        */
1023
        ASSERT(filterSize>0)
1024
        filter2Size= filterSize;
1025
        if(srcFilter) filter2Size+= srcFilter->length - 1;
1026
        if(dstFilter) filter2Size+= dstFilter->length - 1;
1027
        ASSERT(filter2Size>0)
1028
        filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
1029

    
1030
        for(i=0; i<dstW; i++)
1031
        {
1032
                int j;
1033
                SwsVector scaleFilter;
1034
                SwsVector *outVec;
1035

    
1036
                scaleFilter.coeff= filter + i*filterSize;
1037
                scaleFilter.length= filterSize;
1038

    
1039
                if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1040
                else              outVec= &scaleFilter;
1041

    
1042
                ASSERT(outVec->length == filter2Size)
1043
                //FIXME dstFilter
1044

    
1045
                for(j=0; j<outVec->length; j++)
1046
                {
1047
                        filter2[i*filter2Size + j]= outVec->coeff[j];
1048
                }
1049

    
1050
                (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1051

    
1052
                if(outVec != &scaleFilter) sws_freeVec(outVec);
1053
        }
1054
        free(filter); filter=NULL;
1055

    
1056
        /* try to reduce the filter-size (step1 find size and shift left) */
1057
        // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
1058
        minFilterSize= 0;
1059
        for(i=dstW-1; i>=0; i--)
1060
        {
1061
                int min= filter2Size;
1062
                int j;
1063
                double cutOff=0.0;
1064

    
1065
                /* get rid off near zero elements on the left by shifting left */
1066
                for(j=0; j<filter2Size; j++)
1067
                {
1068
                        int k;
1069
                        cutOff += ABS(filter2[i*filter2Size]);
1070

    
1071
                        if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1072

    
1073
                        /* preserve Monotonicity because the core can't handle the filter otherwise */
1074
                        if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1075

    
1076
                        // Move filter coeffs left
1077
                        for(k=1; k<filter2Size; k++)
1078
                                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1079
                        filter2[i*filter2Size + k - 1]= 0.0;
1080
                        (*filterPos)[i]++;
1081
                }
1082

    
1083
                cutOff=0.0;
1084
                /* count near zeros on the right */
1085
                for(j=filter2Size-1; j>0; j--)
1086
                {
1087
                        cutOff += ABS(filter2[i*filter2Size + j]);
1088

    
1089
                        if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1090
                        min--;
1091
                }
1092

    
1093
                if(min>minFilterSize) minFilterSize= min;
1094
        }
1095

    
1096
        if (flags & SWS_CPU_CAPS_ALTIVEC) {
1097
          // we can handle the special case 4,
1098
          // so we don't want to go to the full 8
1099
          if (minFilterSize < 5)
1100
            filterAlign = 4;
1101

    
1102
          // we really don't want to waste our time
1103
          // doing useless computation, so fall-back on
1104
          // the scalar C code for very small filter.
1105
          // vectorizing is worth it only if you have
1106
          // decent-sized vector.
1107
          if (minFilterSize < 3)
1108
            filterAlign = 1;
1109
        }
1110

    
1111
        ASSERT(minFilterSize > 0)
1112
        filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1113
        ASSERT(filterSize > 0)
1114
        filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
1115
        *outFilterSize= filterSize;
1116

    
1117
        if(flags&SWS_PRINT_INFO)
1118
                MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1119
        /* try to reduce the filter-size (step2 reduce it) */
1120
        for(i=0; i<dstW; i++)
1121
        {
1122
                int j;
1123

    
1124
                for(j=0; j<filterSize; j++)
1125
                {
1126
                        if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
1127
                        else                   filter[i*filterSize + j]= filter2[i*filter2Size + j];
1128
                }
1129
        }
1130
        free(filter2); filter2=NULL;
1131
        
1132

    
1133
        //FIXME try to align filterpos if possible
1134

    
1135
        //fix borders
1136
        for(i=0; i<dstW; i++)
1137
        {
1138
                int j;
1139
                if((*filterPos)[i] < 0)
1140
                {
1141
                        // Move filter coeffs left to compensate for filterPos
1142
                        for(j=1; j<filterSize; j++)
1143
                        {
1144
                                int left= MAX(j + (*filterPos)[i], 0);
1145
                                filter[i*filterSize + left] += filter[i*filterSize + j];
1146
                                filter[i*filterSize + j]=0;
1147
                        }
1148
                        (*filterPos)[i]= 0;
1149
                }
1150

    
1151
                if((*filterPos)[i] + filterSize > srcW)
1152
                {
1153
                        int shift= (*filterPos)[i] + filterSize - srcW;
1154
                        // Move filter coeffs right to compensate for filterPos
1155
                        for(j=filterSize-2; j>=0; j--)
1156
                        {
1157
                                int right= MIN(j + shift, filterSize-1);
1158
                                filter[i*filterSize +right] += filter[i*filterSize +j];
1159
                                filter[i*filterSize +j]=0;
1160
                        }
1161
                        (*filterPos)[i]= srcW - filterSize;
1162
                }
1163
        }
1164

    
1165
        // Note the +1 is for the MMXscaler which reads over the end
1166
        *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
1167
        memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
1168

    
1169
        /* Normalize & Store in outFilter */
1170
        for(i=0; i<dstW; i++)
1171
        {
1172
                int j;
1173
                double error=0;
1174
                double sum=0;
1175
                double scale= one;
1176

    
1177
                for(j=0; j<filterSize; j++)
1178
                {
1179
                        sum+= filter[i*filterSize + j];
1180
                }
1181
                scale/= sum;
1182
                for(j=0; j<*outFilterSize; j++)
1183
                {
1184
                        double v= filter[i*filterSize + j]*scale + error;
1185
                        int intV= floor(v + 0.5);
1186
                        (*outFilter)[i*(*outFilterSize) + j]= intV;
1187
                        error = v - intV;
1188
                }
1189
        }
1190
        
1191
        (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1192
        for(i=0; i<*outFilterSize; i++)
1193
        {
1194
                int j= dstW*(*outFilterSize);
1195
                (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1196
        }
1197

    
1198
        free(filter);
1199
}
1200

    
1201
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1202
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1203
{
1204
        uint8_t *fragmentA;
1205
        long imm8OfPShufW1A;
1206
        long imm8OfPShufW2A;
1207
        long fragmentLengthA;
1208
        uint8_t *fragmentB;
1209
        long imm8OfPShufW1B;
1210
        long imm8OfPShufW2B;
1211
        long fragmentLengthB;
1212
        int fragmentPos;
1213

    
1214
        int xpos, i;
1215

    
1216
        // create an optimized horizontal scaling routine
1217

    
1218
        //code fragment
1219

    
1220
        asm volatile(
1221
                "jmp 9f                                \n\t"
1222
        // Begin
1223
                "0:                                \n\t"
1224
                "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t" 
1225
                "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t" 
1226
                "movd 1(%%"REG_c", %%"REG_S"), %%mm1\n\t"
1227
                "punpcklbw %%mm7, %%mm1                \n\t"
1228
                "punpcklbw %%mm7, %%mm0                \n\t"
1229
                "pshufw $0xFF, %%mm1, %%mm1        \n\t"
1230
                "1:                                \n\t"
1231
                "pshufw $0xFF, %%mm0, %%mm0        \n\t"
1232
                "2:                                \n\t"
1233
                "psubw %%mm1, %%mm0                \n\t"
1234
                "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
1235
                "pmullw %%mm3, %%mm0                \n\t"
1236
                "psllw $7, %%mm1                \n\t"
1237
                "paddw %%mm1, %%mm0                \n\t"
1238

    
1239
                "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
1240

    
1241
                "add $8, %%"REG_a"                \n\t"
1242
        // End
1243
                "9:                                \n\t"
1244
//                "int $3\n\t"
1245
                "lea 0b, %0                        \n\t"
1246
                "lea 1b, %1                        \n\t"
1247
                "lea 2b, %2                        \n\t"
1248
                "dec %1                                \n\t"
1249
                "dec %2                                \n\t"
1250
                "sub %0, %1                        \n\t"
1251
                "sub %0, %2                        \n\t"
1252
                "lea 9b, %3                        \n\t"
1253
                "sub %0, %3                        \n\t"
1254

    
1255

    
1256
                :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1257
                "=r" (fragmentLengthA)
1258
        );
1259

    
1260
        asm volatile(
1261
                "jmp 9f                                \n\t"
1262
        // Begin
1263
                "0:                                \n\t"
1264
                "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t" 
1265
                "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t" 
1266
                "punpcklbw %%mm7, %%mm0                \n\t"
1267
                "pshufw $0xFF, %%mm0, %%mm1        \n\t"
1268
                "1:                                \n\t"
1269
                "pshufw $0xFF, %%mm0, %%mm0        \n\t"
1270
                "2:                                \n\t"
1271
                "psubw %%mm1, %%mm0                \n\t"
1272
                "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
1273
                "pmullw %%mm3, %%mm0                \n\t"
1274
                "psllw $7, %%mm1                \n\t"
1275
                "paddw %%mm1, %%mm0                \n\t"
1276

    
1277
                "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
1278

    
1279
                "add $8, %%"REG_a"                \n\t"
1280
        // End
1281
                "9:                                \n\t"
1282
//                "int $3\n\t"
1283
                "lea 0b, %0                        \n\t"
1284
                "lea 1b, %1                        \n\t"
1285
                "lea 2b, %2                        \n\t"
1286
                "dec %1                                \n\t"
1287
                "dec %2                                \n\t"
1288
                "sub %0, %1                        \n\t"
1289
                "sub %0, %2                        \n\t"
1290
                "lea 9b, %3                        \n\t"
1291
                "sub %0, %3                        \n\t"
1292

    
1293

    
1294
                :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1295
                "=r" (fragmentLengthB)
1296
        );
1297

    
1298
        xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1299
        fragmentPos=0;
1300
        
1301
        for(i=0; i<dstW/numSplits; i++)
1302
        {
1303
                int xx=xpos>>16;
1304

    
1305
                if((i&3) == 0)
1306
                {
1307
                        int a=0;
1308
                        int b=((xpos+xInc)>>16) - xx;
1309
                        int c=((xpos+xInc*2)>>16) - xx;
1310
                        int d=((xpos+xInc*3)>>16) - xx;
1311

    
1312
                        filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1313
                        filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1314
                        filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1315
                        filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1316
                        filterPos[i/2]= xx;
1317

    
1318
                        if(d+1<4)
1319
                        {
1320
                                int maxShift= 3-(d+1);
1321
                                int shift=0;
1322

    
1323
                                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1324

    
1325
                                funnyCode[fragmentPos + imm8OfPShufW1B]=
1326
                                        (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1327
                                funnyCode[fragmentPos + imm8OfPShufW2B]=
1328
                                        a | (b<<2) | (c<<4) | (d<<6);
1329

    
1330
                                if(i+3>=dstW) shift=maxShift; //avoid overread
1331
                                else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1332

    
1333
                                if(shift && i>=shift)
1334
                                {
1335
                                        funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1336
                                        funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1337
                                        filterPos[i/2]-=shift;
1338
                                }
1339

    
1340
                                fragmentPos+= fragmentLengthB;
1341
                        }
1342
                        else
1343
                        {
1344
                                int maxShift= 3-d;
1345
                                int shift=0;
1346

    
1347
                                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1348

    
1349
                                funnyCode[fragmentPos + imm8OfPShufW1A]=
1350
                                funnyCode[fragmentPos + imm8OfPShufW2A]=
1351
                                        a | (b<<2) | (c<<4) | (d<<6);
1352

    
1353
                                if(i+4>=dstW) shift=maxShift; //avoid overread
1354
                                else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1355

    
1356
                                if(shift && i>=shift)
1357
                                {
1358
                                        funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1359
                                        funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1360
                                        filterPos[i/2]-=shift;
1361
                                }
1362

    
1363
                                fragmentPos+= fragmentLengthA;
1364
                        }
1365

    
1366
                        funnyCode[fragmentPos]= RET;
1367
                }
1368
                xpos+=xInc;
1369
        }
1370
        filterPos[i/2]= xpos>>16; // needed to jump to the next part
1371
}
1372
#endif // ARCH_X86 || ARCH_X86_64
1373

    
1374
static void globalInit(){
1375
    // generating tables:
1376
    int i;
1377
    for(i=0; i<768; i++){
1378
        int c= MIN(MAX(i-256, 0), 255);
1379
        clip_table[i]=c;
1380
    }
1381
}
1382

    
1383
static SwsFunc getSwsFunc(int flags){
1384
    
1385
#ifdef RUNTIME_CPUDETECT
1386
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1387
        // ordered per speed fasterst first
1388
        if(flags & SWS_CPU_CAPS_MMX2)
1389
                return swScale_MMX2;
1390
        else if(flags & SWS_CPU_CAPS_3DNOW)
1391
                return swScale_3DNow;
1392
        else if(flags & SWS_CPU_CAPS_MMX)
1393
                return swScale_MMX;
1394
        else
1395
                return swScale_C;
1396

    
1397
#else
1398
#ifdef ARCH_POWERPC
1399
        if(flags & SWS_CPU_CAPS_ALTIVEC)
1400
          return swScale_altivec;
1401
        else
1402
          return swScale_C;
1403
#endif
1404
        return swScale_C;
1405
#endif
1406
#else //RUNTIME_CPUDETECT
1407
#ifdef HAVE_MMX2
1408
        return swScale_MMX2;
1409
#elif defined (HAVE_3DNOW)
1410
        return swScale_3DNow;
1411
#elif defined (HAVE_MMX)
1412
        return swScale_MMX;
1413
#elif defined (HAVE_ALTIVEC)
1414
        return swScale_altivec;
1415
#else
1416
        return swScale_C;
1417
#endif
1418
#endif //!RUNTIME_CPUDETECT
1419
}
1420

    
1421
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1422
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1423
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1424
        /* Copy Y plane */
1425
        if(dstStride[0]==srcStride[0])
1426
                memcpy(dst, src[0], srcSliceH*dstStride[0]);
1427
        else
1428
        {
1429
                int i;
1430
                uint8_t *srcPtr= src[0];
1431
                uint8_t *dstPtr= dst;
1432
                for(i=0; i<srcSliceH; i++)
1433
                {
1434
                        memcpy(dstPtr, srcPtr, c->srcW);
1435
                        srcPtr+= srcStride[0];
1436
                        dstPtr+= dstStride[0];
1437
                }
1438
        }
1439
        dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1440
        if (c->dstFormat == IMGFMT_NV12)
1441
                interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
1442
        else
1443
                interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
1444

    
1445
        return srcSliceH;
1446
}
1447

    
1448
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1449
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1450
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1451

    
1452
        yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1453

    
1454
        return srcSliceH;
1455
}
1456

    
1457
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1458
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1459
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1460

    
1461
        yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1462

    
1463
        return srcSliceH;
1464
}
1465

    
1466
/* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1467
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1468
                           int srcSliceH, uint8_t* dst[], int dstStride[]){
1469
        const int srcFormat= c->srcFormat;
1470
        const int dstFormat= c->dstFormat;
1471
        const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
1472
        const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
1473
        const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 
1474
        const int dstId= (dstFormat&0xFF)>>2;
1475
        void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
1476

    
1477
        /* BGR -> BGR */
1478
        if(   (isBGR(srcFormat) && isBGR(dstFormat))
1479
           || (isRGB(srcFormat) && isRGB(dstFormat))){
1480
                switch(srcId | (dstId<<4)){
1481
                case 0x34: conv= rgb16to15; break;
1482
                case 0x36: conv= rgb24to15; break;
1483
                case 0x38: conv= rgb32to15; break;
1484
                case 0x43: conv= rgb15to16; break;
1485
                case 0x46: conv= rgb24to16; break;
1486
                case 0x48: conv= rgb32to16; break;
1487
                case 0x63: conv= rgb15to24; break;
1488
                case 0x64: conv= rgb16to24; break;
1489
                case 0x68: conv= rgb32to24; break;
1490
                case 0x83: conv= rgb15to32; break;
1491
                case 0x84: conv= rgb16to32; break;
1492
                case 0x86: conv= rgb24to32; break;
1493
                default: MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1494
                                 vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
1495
                }
1496
        }else if(   (isBGR(srcFormat) && isRGB(dstFormat))
1497
                 || (isRGB(srcFormat) && isBGR(dstFormat))){
1498
                switch(srcId | (dstId<<4)){
1499
                case 0x33: conv= rgb15tobgr15; break;
1500
                case 0x34: conv= rgb16tobgr15; break;
1501
                case 0x36: conv= rgb24tobgr15; break;
1502
                case 0x38: conv= rgb32tobgr15; break;
1503
                case 0x43: conv= rgb15tobgr16; break;
1504
                case 0x44: conv= rgb16tobgr16; break;
1505
                case 0x46: conv= rgb24tobgr16; break;
1506
                case 0x48: conv= rgb32tobgr16; break;
1507
                case 0x63: conv= rgb15tobgr24; break;
1508
                case 0x64: conv= rgb16tobgr24; break;
1509
                case 0x66: conv= rgb24tobgr24; break;
1510
                case 0x68: conv= rgb32tobgr24; break;
1511
                case 0x83: conv= rgb15tobgr32; break;
1512
                case 0x84: conv= rgb16tobgr32; break;
1513
                case 0x86: conv= rgb24tobgr32; break;
1514
                case 0x88: conv= rgb32tobgr32; break;
1515
                default: MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1516
                                 vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
1517
                }
1518
        }else{
1519
                MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1520
                         vo_format_name(srcFormat), vo_format_name(dstFormat));
1521
        }
1522

    
1523
        if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
1524
                conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1525
        else
1526
        {
1527
                int i;
1528
                uint8_t *srcPtr= src[0];
1529
                uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1530

    
1531
                for(i=0; i<srcSliceH; i++)
1532
                {
1533
                        conv(srcPtr, dstPtr, c->srcW*srcBpp);
1534
                        srcPtr+= srcStride[0];
1535
                        dstPtr+= dstStride[0];
1536
                }
1537
        }     
1538
        return srcSliceH;
1539
}
1540

    
1541
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1542
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1543

    
1544
        rgb24toyv12(
1545
                src[0], 
1546
                dst[0]+ srcSliceY    *dstStride[0], 
1547
                dst[1]+(srcSliceY>>1)*dstStride[1], 
1548
                dst[2]+(srcSliceY>>1)*dstStride[2],
1549
                c->srcW, srcSliceH, 
1550
                dstStride[0], dstStride[1], srcStride[0]);
1551
        return srcSliceH;
1552
}
1553

    
1554
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1555
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1556
        int i;
1557

    
1558
        /* copy Y */
1559
        if(srcStride[0]==dstStride[0]) 
1560
                memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1561
        else{
1562
                uint8_t *srcPtr= src[0];
1563
                uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1564

    
1565
                for(i=0; i<srcSliceH; i++)
1566
                {
1567
                        memcpy(dstPtr, srcPtr, c->srcW);
1568
                        srcPtr+= srcStride[0];
1569
                        dstPtr+= dstStride[0];
1570
                }
1571
        }
1572

    
1573
        if(c->dstFormat==IMGFMT_YV12){
1574
                planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1575
                planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1576
        }else{
1577
                planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1578
                planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1579
        }
1580
        return srcSliceH;
1581
}
1582

    
1583
/**
1584
 * bring pointers in YUV order instead of YVU
1585
 */
1586
static inline void sws_orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
1587
        if(format == IMGFMT_YV12 || format == IMGFMT_YVU9
1588
           || format == IMGFMT_444P || format == IMGFMT_422P || format == IMGFMT_411P){
1589
                sortedP[0]= p[0];
1590
                sortedP[1]= p[2];
1591
                sortedP[2]= p[1];
1592
                sortedStride[0]= stride[0];
1593
                sortedStride[1]= stride[2];
1594
                sortedStride[2]= stride[1];
1595
        }
1596
        else if(isPacked(format) || isGray(format) || format == IMGFMT_Y8)
1597
        {
1598
                sortedP[0]= p[0];
1599
                sortedP[1]= 
1600
                sortedP[2]= NULL;
1601
                sortedStride[0]= stride[0];
1602
                sortedStride[1]= 
1603
                sortedStride[2]= 0;
1604
        }
1605
        else if(format == IMGFMT_I420 || format == IMGFMT_IYUV)
1606
        {
1607
                sortedP[0]= p[0];
1608
                sortedP[1]= p[1];
1609
                sortedP[2]= p[2];
1610
                sortedStride[0]= stride[0];
1611
                sortedStride[1]= stride[1];
1612
                sortedStride[2]= stride[2];
1613
        }
1614
        else if(format == IMGFMT_NV12 || format == IMGFMT_NV21)
1615
        {
1616
                sortedP[0]= p[0];
1617
                sortedP[1]= p[1];
1618
                sortedP[2]= NULL;
1619
                sortedStride[0]= stride[0];
1620
                sortedStride[1]= stride[1];
1621
                sortedStride[2]= 0;
1622
        }else{
1623
                MSG_ERR("internal error in orderYUV\n");
1624
        }
1625
}
1626

    
1627
/* unscaled copy like stuff (assumes nearly identical formats) */
1628
static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1629
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1630

    
1631
        if(isPacked(c->srcFormat))
1632
        {
1633
                if(dstStride[0]==srcStride[0])
1634
                        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1635
                else
1636
                {
1637
                        int i;
1638
                        uint8_t *srcPtr= src[0];
1639
                        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1640
                        int length=0;
1641

    
1642
                        /* universal length finder */
1643
                        while(length+c->srcW <= ABS(dstStride[0]) 
1644
                           && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
1645
                        ASSERT(length!=0);
1646

    
1647
                        for(i=0; i<srcSliceH; i++)
1648
                        {
1649
                                memcpy(dstPtr, srcPtr, length);
1650
                                srcPtr+= srcStride[0];
1651
                                dstPtr+= dstStride[0];
1652
                        }
1653
                }
1654
        }
1655
        else 
1656
        { /* Planar YUV or gray */
1657
                int plane;
1658
                for(plane=0; plane<3; plane++)
1659
                {
1660
                        int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1661
                        int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1662
                        int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1663

    
1664
                        if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1665
                        {
1666
                                if(!isGray(c->dstFormat))
1667
                                        memset(dst[plane], 128, dstStride[plane]*height);
1668
                        }
1669
                        else
1670
                        {
1671
                                if(dstStride[plane]==srcStride[plane])
1672
                                        memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1673
                                else
1674
                                {
1675
                                        int i;
1676
                                        uint8_t *srcPtr= src[plane];
1677
                                        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1678
                                        for(i=0; i<height; i++)
1679
                                        {
1680
                                                memcpy(dstPtr, srcPtr, length);
1681
                                                srcPtr+= srcStride[plane];
1682
                                                dstPtr+= dstStride[plane];
1683
                                        }
1684
                                }
1685
                        }
1686
                }
1687
        }
1688
        return srcSliceH;
1689
}
1690

    
1691
static int remove_dup_fourcc(int fourcc)
1692
{
1693
        switch(fourcc)
1694
        {
1695
            case IMGFMT_I420:
1696
            case IMGFMT_IYUV: return IMGFMT_YV12;
1697
            case IMGFMT_Y8  : return IMGFMT_Y800;
1698
            case IMGFMT_IF09: return IMGFMT_YVU9;
1699
            default: return fourcc;
1700
        }
1701
}
1702

    
1703
static void getSubSampleFactors(int *h, int *v, int format){
1704
        switch(format){
1705
        case IMGFMT_UYVY:
1706
        case IMGFMT_YUY2:
1707
                *h=1;
1708
                *v=0;
1709
                break;
1710
        case IMGFMT_YV12:
1711
        case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
1712
        case IMGFMT_NV12:
1713
        case IMGFMT_NV21:
1714
                *h=1;
1715
                *v=1;
1716
                break;
1717
        case IMGFMT_YVU9:
1718
                *h=2;
1719
                *v=2;
1720
                break;
1721
        case IMGFMT_444P:
1722
                *h=0;
1723
                *v=0;
1724
                break;
1725
        case IMGFMT_422P:
1726
                *h=1;
1727
                *v=0;
1728
                break;
1729
        case IMGFMT_411P:
1730
                *h=2;
1731
                *v=0;
1732
                break;
1733
        default:
1734
                *h=0;
1735
                *v=0;
1736
                break;
1737
        }
1738
}
1739

    
1740
static uint16_t roundToInt16(int64_t f){
1741
        int r= (f + (1<<15))>>16;
1742
             if(r<-0x7FFF) return 0x8000;
1743
        else if(r> 0x7FFF) return 0x7FFF;
1744
        else               return r;
1745
}
1746

    
1747
/**
1748
 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1749
 * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
1750
 * @return -1 if not supported
1751
 */
1752
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1753
        int64_t crv =  inv_table[0];
1754
        int64_t cbu =  inv_table[1];
1755
        int64_t cgu = -inv_table[2];
1756
        int64_t cgv = -inv_table[3];
1757
        int64_t cy  = 1<<16;
1758
        int64_t oy  = 0;
1759

    
1760
        if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1761
        memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1762
        memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
1763

    
1764
        c->brightness= brightness;
1765
        c->contrast  = contrast;
1766
        c->saturation= saturation;
1767
        c->srcRange  = srcRange;
1768
        c->dstRange  = dstRange;
1769

    
1770
        c->uOffset=   0x0400040004000400LL;
1771
        c->vOffset=   0x0400040004000400LL;
1772

    
1773
        if(!srcRange){
1774
                cy= (cy*255) / 219;
1775
                oy= 16<<16;
1776
        }
1777

    
1778
        cy = (cy *contrast             )>>16;
1779
        crv= (crv*contrast * saturation)>>32;
1780
        cbu= (cbu*contrast * saturation)>>32;
1781
        cgu= (cgu*contrast * saturation)>>32;
1782
        cgv= (cgv*contrast * saturation)>>32;
1783

    
1784
        oy -= 256*brightness;
1785

    
1786
        c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
1787
        c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
1788
        c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1789
        c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1790
        c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1791
        c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
1792

    
1793
        yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1794
        //FIXME factorize
1795

    
1796
#ifdef HAVE_ALTIVEC
1797
        yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1798
#endif        
1799
        return 0;
1800
}
1801

    
1802
/**
1803
 * @return -1 if not supported
1804
 */
1805
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1806
        if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1807

    
1808
        *inv_table = c->srcColorspaceTable;
1809
        *table     = c->dstColorspaceTable;
1810
        *srcRange  = c->srcRange;
1811
        *dstRange  = c->dstRange;
1812
        *brightness= c->brightness;
1813
        *contrast  = c->contrast;
1814
        *saturation= c->saturation;
1815
        
1816
        return 0;        
1817
}
1818

    
1819
SwsContext *sws_getContext(int srcW, int srcH, int origSrcFormat, int dstW, int dstH, int origDstFormat, int flags,
1820
                         SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
1821

    
1822
        SwsContext *c;
1823
        int i;
1824
        int usesVFilter, usesHFilter;
1825
        int unscaled, needsDither;
1826
        int srcFormat, dstFormat;
1827
        SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1828
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1829
        if(flags & SWS_CPU_CAPS_MMX)
1830
                asm volatile("emms\n\t"::: "memory");
1831
#endif
1832

    
1833
#ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
1834
        flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC);
1835
#ifdef HAVE_MMX2
1836
        flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1837
#elif defined (HAVE_3DNOW)
1838
        flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1839
#elif defined (HAVE_MMX)
1840
        flags |= SWS_CPU_CAPS_MMX;
1841
#elif defined (HAVE_ALTIVEC)
1842
        flags |= SWS_CPU_CAPS_ALTIVEC;
1843
#endif
1844
#endif
1845
        if(clip_table[512] != 255) globalInit();
1846
        if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
1847

    
1848
        /* avoid duplicate Formats, so we don't need to check to much */
1849
        srcFormat = remove_dup_fourcc(origSrcFormat);
1850
        dstFormat = remove_dup_fourcc(origDstFormat);
1851

    
1852
        unscaled = (srcW == dstW && srcH == dstH);
1853
        needsDither= (isBGR(dstFormat) || isRGB(dstFormat)) 
1854
                     && (dstFormat&0xFF)<24
1855
                     && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
1856

    
1857
        if(!isSupportedIn(srcFormat)) 
1858
        {
1859
                MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1860
                return NULL;
1861
        }
1862
        if(!isSupportedOut(dstFormat))
1863
        {
1864
                MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1865
                return NULL;
1866
        }
1867

    
1868
        /* sanity check */
1869
        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
1870
        {
1871
                 MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n", 
1872
                        srcW, srcH, dstW, dstH);
1873
                return NULL;
1874
        }
1875

    
1876
        if(!dstFilter) dstFilter= &dummyFilter;
1877
        if(!srcFilter) srcFilter= &dummyFilter;
1878

    
1879
        c= memalign(64, sizeof(SwsContext));
1880
        memset(c, 0, sizeof(SwsContext));
1881

    
1882
        c->srcW= srcW;
1883
        c->srcH= srcH;
1884
        c->dstW= dstW;
1885
        c->dstH= dstH;
1886
        c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1887
        c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1888
        c->flags= flags;
1889
        c->dstFormat= dstFormat;
1890
        c->srcFormat= srcFormat;
1891
        c->origDstFormat= origDstFormat;
1892
        c->origSrcFormat= origSrcFormat;
1893
        c->vRounder= 4* 0x0001000100010001ULL;
1894

    
1895
        usesHFilter= usesVFilter= 0;
1896
        if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
1897
        if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
1898
        if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
1899
        if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
1900
        if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
1901
        if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
1902
        if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
1903
        if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
1904

    
1905
        getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
1906
        getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
1907

    
1908
        // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
1909
        if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
1910

    
1911
        // drop some chroma lines if the user wants it
1912
        c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
1913
        c->chrSrcVSubSample+= c->vChrDrop;
1914

    
1915
        // drop every 2. pixel for chroma calculation unless user wants full chroma
1916
        if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)) 
1917
                c->chrSrcHSubSample=1;
1918

    
1919
        if(param){
1920
                c->param[0] = param[0];
1921
                c->param[1] = param[1];
1922
        }else{
1923
                c->param[0] =
1924
                c->param[1] = SWS_PARAM_DEFAULT;
1925
        }
1926

    
1927
        c->chrIntHSubSample= c->chrDstHSubSample;
1928
        c->chrIntVSubSample= c->chrSrcVSubSample;
1929

    
1930
        // note the -((-x)>>y) is so that we allways round toward +inf
1931
        c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
1932
        c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
1933
        c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
1934
        c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
1935

    
1936
        sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], 0, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, 0, 0, 1<<16, 1<<16); 
1937

    
1938
        /* unscaled special Cases */
1939
        if(unscaled && !usesHFilter && !usesVFilter)
1940
        {
1941
                /* yv12_to_nv12 */
1942
                if(srcFormat == IMGFMT_YV12 && (dstFormat == IMGFMT_NV12 || dstFormat == IMGFMT_NV21))
1943
                {
1944
                        c->swScale= PlanarToNV12Wrapper;
1945
                }
1946
                /* yuv2bgr */
1947
                if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
1948
                {
1949
                        c->swScale= yuv2rgb_get_func_ptr(c);
1950
                }
1951
                
1952
                if( srcFormat==IMGFMT_YVU9 && dstFormat==IMGFMT_YV12 )
1953
                {
1954
                        c->swScale= yvu9toyv12Wrapper;
1955
                }
1956

    
1957
                /* bgr24toYV12 */
1958
                if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
1959
                        c->swScale= bgr24toyv12Wrapper;
1960
                
1961
                /* rgb/bgr -> rgb/bgr (no dither needed forms) */
1962
                if(   (isBGR(srcFormat) || isRGB(srcFormat))
1963
                   && (isBGR(dstFormat) || isRGB(dstFormat)) 
1964
                   && !needsDither)
1965
                        c->swScale= rgb2rgbWrapper;
1966

    
1967
                /* LQ converters if -sws 0 or -sws 4*/
1968
                if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
1969
                        /* rgb/bgr -> rgb/bgr (dither needed forms) */
1970
                        if(  (isBGR(srcFormat) || isRGB(srcFormat))
1971
                          && (isBGR(dstFormat) || isRGB(dstFormat)) 
1972
                          && needsDither)
1973
                                c->swScale= rgb2rgbWrapper;
1974

    
1975
                        /* yv12_to_yuy2 */
1976
                        if(srcFormat == IMGFMT_YV12 && 
1977
                            (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY))
1978
                        {
1979
                                if (dstFormat == IMGFMT_YUY2)
1980
                                    c->swScale= PlanarToYuy2Wrapper;
1981
                                else
1982
                                    c->swScale= PlanarToUyvyWrapper;
1983
                        }
1984
                }
1985

    
1986
#ifdef HAVE_ALTIVEC
1987
                if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1988
                    ((srcFormat == IMGFMT_YV12 && 
1989
                      (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY)))) {
1990
                  // unscaled YV12 -> packed YUV, we want speed
1991
                  if (dstFormat == IMGFMT_YUY2)
1992
                    c->swScale= yv12toyuy2_unscaled_altivec;
1993
                  else
1994
                    c->swScale= yv12touyvy_unscaled_altivec;
1995
                }
1996
#endif
1997

    
1998
                /* simple copy */
1999
                if(   srcFormat == dstFormat
2000
                   || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2001
                   || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2002
                  )
2003
                {
2004
                        c->swScale= simpleCopy;
2005
                }
2006

    
2007
                if(c->swScale){
2008
                        if(flags&SWS_PRINT_INFO)
2009
                                MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
2010
                                        vo_format_name(srcFormat), vo_format_name(dstFormat));
2011
                        return c;
2012
                }
2013
        }
2014

    
2015
        if(flags & SWS_CPU_CAPS_MMX2)
2016
        {
2017
                c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2018
                if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2019
                {
2020
                        if(flags&SWS_PRINT_INFO)
2021
                                MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
2022
                }
2023
                if(usesHFilter) c->canMMX2BeUsed=0;
2024
        }
2025
        else
2026
                c->canMMX2BeUsed=0;
2027

    
2028
        c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2029
        c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2030

    
2031
        // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2032
        // but only for the FAST_BILINEAR mode otherwise do correct scaling
2033
        // n-2 is the last chrominance sample available
2034
        // this is not perfect, but noone shuld notice the difference, the more correct variant
2035
        // would be like the vertical one, but that would require some special code for the
2036
        // first and last pixel
2037
        if(flags&SWS_FAST_BILINEAR)
2038
        {
2039
                if(c->canMMX2BeUsed)
2040
                {
2041
                        c->lumXInc+= 20;
2042
                        c->chrXInc+= 20;
2043
                }
2044
                //we don't use the x86asm scaler if mmx is available
2045
                else if(flags & SWS_CPU_CAPS_MMX)
2046
                {
2047
                        c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2048
                        c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2049
                }
2050
        }
2051

    
2052
        /* precalculate horizontal scaler filter coefficients */
2053
        {
2054
                const int filterAlign=
2055
                  (flags & SWS_CPU_CAPS_MMX) ? 4 :
2056
                  (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2057
                  1;
2058

    
2059
                initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2060
                                 srcW      ,       dstW, filterAlign, 1<<14,
2061
                                 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2062
                                 srcFilter->lumH, dstFilter->lumH, c->param);
2063
                initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2064
                                 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2065
                                 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2066
                                 srcFilter->chrH, dstFilter->chrH, c->param);
2067

    
2068
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2069
// can't downscale !!!
2070
                if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2071
                {
2072
#define MAX_FUNNY_CODE_SIZE 10000
2073
#ifdef HAVE_SYS_MMAN_H
2074
                        c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2075
                        c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2076
#else
2077
                        c->funnyYCode = (uint8_t*)memalign(32, MAX_FUNNY_CODE_SIZE);
2078
                        c->funnyUVCode = (uint8_t*)memalign(32, MAX_FUNNY_CODE_SIZE);
2079
#endif
2080

    
2081
                        c->lumMmx2Filter   = (int16_t*)memalign(8, (dstW        /8+8)*sizeof(int16_t));
2082
                        c->chrMmx2Filter   = (int16_t*)memalign(8, (c->chrDstW  /4+8)*sizeof(int16_t));
2083
                        c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW      /2/8+8)*sizeof(int32_t));
2084
                        c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
2085

    
2086
                        initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2087
                        initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2088
                }
2089
#endif
2090
        } // Init Horizontal stuff
2091

    
2092

    
2093

    
2094
        /* precalculate vertical scaler filter coefficients */
2095
        {
2096
                const int filterAlign=
2097
                  (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2098
                  1;
2099

    
2100
                initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2101
                                srcH      ,        dstH, filterAlign, (1<<12)-4,
2102
                                (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2103
                                srcFilter->lumV, dstFilter->lumV, c->param);
2104
                initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2105
                                c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2106
                                (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2107
                                srcFilter->chrV, dstFilter->chrV, c->param);
2108
        }
2109

    
2110
        // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2111
        c->vLumBufSize= c->vLumFilterSize;
2112
        c->vChrBufSize= c->vChrFilterSize;
2113
        for(i=0; i<dstH; i++)
2114
        {
2115
                int chrI= i*c->chrDstH / dstH;
2116
                int nextSlice= MAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2117
                                 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2118

    
2119
                nextSlice>>= c->chrSrcVSubSample;
2120
                nextSlice<<= c->chrSrcVSubSample;
2121
                if(c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2122
                        c->vLumBufSize= nextSlice - c->vLumFilterPos[i   ];
2123
                if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2124
                        c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2125
        }
2126

    
2127
        // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2128
        c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
2129
        c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
2130
        //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)
2131
        for(i=0; i<c->vLumBufSize; i++)
2132
                c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
2133
        for(i=0; i<c->vChrBufSize; i++)
2134
                c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
2135

    
2136
        //try to avoid drawing green stuff between the right end and the stride end
2137
        for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
2138
        for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2139

    
2140
        ASSERT(c->chrDstH <= dstH)
2141

    
2142
        if(flags&SWS_PRINT_INFO)
2143
        {
2144
#ifdef DITHER1XBPP
2145
                char *dither= " dithered";
2146
#else
2147
                char *dither= "";
2148
#endif
2149
                if(flags&SWS_FAST_BILINEAR)
2150
                        MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
2151
                else if(flags&SWS_BILINEAR)
2152
                        MSG_INFO("\nSwScaler: BILINEAR scaler, ");
2153
                else if(flags&SWS_BICUBIC)
2154
                        MSG_INFO("\nSwScaler: BICUBIC scaler, ");
2155
                else if(flags&SWS_X)
2156
                        MSG_INFO("\nSwScaler: Experimental scaler, ");
2157
                else if(flags&SWS_POINT)
2158
                        MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
2159
                else if(flags&SWS_AREA)
2160
                        MSG_INFO("\nSwScaler: Area Averageing scaler, ");
2161
                else if(flags&SWS_BICUBLIN)
2162
                        MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
2163
                else if(flags&SWS_GAUSS)
2164
                        MSG_INFO("\nSwScaler: Gaussian scaler, ");
2165
                else if(flags&SWS_SINC)
2166
                        MSG_INFO("\nSwScaler: Sinc scaler, ");
2167
                else if(flags&SWS_LANCZOS)
2168
                        MSG_INFO("\nSwScaler: Lanczos scaler, ");
2169
                else if(flags&SWS_SPLINE)
2170
                        MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
2171
                else
2172
                        MSG_INFO("\nSwScaler: ehh flags invalid?! ");
2173

    
2174
                if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
2175
                        MSG_INFO("from %s to%s %s ", 
2176
                                vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
2177
                else
2178
                        MSG_INFO("from %s to %s ", 
2179
                                vo_format_name(srcFormat), vo_format_name(dstFormat));
2180

    
2181
                if(flags & SWS_CPU_CAPS_MMX2)
2182
                        MSG_INFO("using MMX2\n");
2183
                else if(flags & SWS_CPU_CAPS_3DNOW)
2184
                        MSG_INFO("using 3DNOW\n");
2185
                else if(flags & SWS_CPU_CAPS_MMX)
2186
                        MSG_INFO("using MMX\n");
2187
                else if(flags & SWS_CPU_CAPS_ALTIVEC)
2188
                        MSG_INFO("using AltiVec\n");
2189
                else 
2190
                        MSG_INFO("using C\n");
2191
        }
2192

    
2193
        if(flags & SWS_PRINT_INFO)
2194
        {
2195
                if(flags & SWS_CPU_CAPS_MMX)
2196
                {
2197
                        if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2198
                                MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2199
                        else
2200
                        {
2201
                                if(c->hLumFilterSize==4)
2202
                                        MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2203
                                else if(c->hLumFilterSize==8)
2204
                                        MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2205
                                else
2206
                                        MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2207

    
2208
                                if(c->hChrFilterSize==4)
2209
                                        MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2210
                                else if(c->hChrFilterSize==8)
2211
                                        MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2212
                                else
2213
                                        MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2214
                        }
2215
                }
2216
                else
2217
                {
2218
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2219
                        MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
2220
#else
2221
                        if(flags & SWS_FAST_BILINEAR)
2222
                                MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2223
                        else
2224
                                MSG_V("SwScaler: using C scaler for horizontal scaling\n");
2225
#endif
2226
                }
2227
                if(isPlanarYUV(dstFormat))
2228
                {
2229
                        if(c->vLumFilterSize==1)
2230
                                MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2231
                        else
2232
                                MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2233
                }
2234
                else
2235
                {
2236
                        if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
2237
                                MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2238
                                       "SwScaler:       2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2239
                        else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
2240
                                MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2241
                        else
2242
                                MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2243
                }
2244

    
2245
                if(dstFormat==IMGFMT_BGR24)
2246
                        MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
2247
                                (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2248
                else if(dstFormat==IMGFMT_BGR32)
2249
                        MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2250
                else if(dstFormat==IMGFMT_BGR16)
2251
                        MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2252
                else if(dstFormat==IMGFMT_BGR15)
2253
                        MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2254

    
2255
                MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2256
        }
2257
        if(flags & SWS_PRINT_INFO)
2258
        {
2259
                MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2260
                        c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2261
                MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2262
                        c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2263
        }
2264

    
2265
        c->swScale= getSwsFunc(flags);
2266
        return c;
2267
}
2268

    
2269
/**
2270
 * swscale warper, so we don't need to export the SwsContext.
2271
 * assumes planar YUV to be in YUV order instead of YVU
2272
 */
2273
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2274
                           int srcSliceH, uint8_t* dst[], int dstStride[]){
2275
        //copy strides, so they can safely be modified
2276
        int srcStride2[3]= {srcStride[0], srcStride[1], srcStride[2]};
2277
        int dstStride2[3]= {dstStride[0], dstStride[1], dstStride[2]};
2278
        return c->swScale(c, src, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2279
}
2280

    
2281
/**
2282
 * swscale warper, so we don't need to export the SwsContext
2283
 */
2284
int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
2285
                           int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
2286
        int srcStride[3];
2287
        int dstStride[3];
2288
        uint8_t *src[3];
2289
        uint8_t *dst[3];
2290
        sws_orderYUV(c->origSrcFormat, src, srcStride, srcParam, srcStrideParam);
2291
        sws_orderYUV(c->origDstFormat, dst, dstStride, dstParam, dstStrideParam);
2292
//printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
2293

    
2294
        return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2295
}
2296

    
2297
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur, 
2298
                                float lumaSharpen, float chromaSharpen,
2299
                                float chromaHShift, float chromaVShift,
2300
                                int verbose)
2301
{
2302
        SwsFilter *filter= malloc(sizeof(SwsFilter));
2303

    
2304
        if(lumaGBlur!=0.0){
2305
                filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2306
                filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2307
        }else{
2308
                filter->lumH= sws_getIdentityVec();
2309
                filter->lumV= sws_getIdentityVec();
2310
        }
2311

    
2312
        if(chromaGBlur!=0.0){
2313
                filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2314
                filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2315
        }else{
2316
                filter->chrH= sws_getIdentityVec();
2317
                filter->chrV= sws_getIdentityVec();
2318
        }
2319

    
2320
        if(chromaSharpen!=0.0){
2321
                SwsVector *g= sws_getConstVec(-1.0, 3);
2322
                SwsVector *id= sws_getConstVec(10.0/chromaSharpen, 1);
2323
                g->coeff[1]=2.0;
2324
                sws_addVec(id, g);
2325
                sws_convVec(filter->chrH, id);
2326
                sws_convVec(filter->chrV, id);
2327
                sws_freeVec(g);
2328
                sws_freeVec(id);
2329
        }
2330

    
2331
        if(lumaSharpen!=0.0){
2332
                SwsVector *g= sws_getConstVec(-1.0, 3);
2333
                SwsVector *id= sws_getConstVec(10.0/lumaSharpen, 1);
2334
                g->coeff[1]=2.0;
2335
                sws_addVec(id, g);
2336
                sws_convVec(filter->lumH, id);
2337
                sws_convVec(filter->lumV, id);
2338
                sws_freeVec(g);
2339
                sws_freeVec(id);
2340
        }
2341

    
2342
        if(chromaHShift != 0.0)
2343
                sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2344

    
2345
        if(chromaVShift != 0.0)
2346
                sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2347

    
2348
        sws_normalizeVec(filter->chrH, 1.0);
2349
        sws_normalizeVec(filter->chrV, 1.0);
2350
        sws_normalizeVec(filter->lumH, 1.0);
2351
        sws_normalizeVec(filter->lumV, 1.0);
2352

    
2353
        if(verbose) sws_printVec(filter->chrH);
2354
        if(verbose) sws_printVec(filter->lumH);
2355

    
2356
        return filter;
2357
}
2358

    
2359
/**
2360
 * returns a normalized gaussian curve used to filter stuff
2361
 * quality=3 is high quality, lowwer is lowwer quality
2362
 */
2363
SwsVector *sws_getGaussianVec(double variance, double quality){
2364
        const int length= (int)(variance*quality + 0.5) | 1;
2365
        int i;
2366
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2367
        double middle= (length-1)*0.5;
2368
        SwsVector *vec= malloc(sizeof(SwsVector));
2369

    
2370
        vec->coeff= coeff;
2371
        vec->length= length;
2372

    
2373
        for(i=0; i<length; i++)
2374
        {
2375
                double dist= i-middle;
2376
                coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2377
        }
2378

    
2379
        sws_normalizeVec(vec, 1.0);
2380

    
2381
        return vec;
2382
}
2383

    
2384
SwsVector *sws_getConstVec(double c, int length){
2385
        int i;
2386
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2387
        SwsVector *vec= malloc(sizeof(SwsVector));
2388

    
2389
        vec->coeff= coeff;
2390
        vec->length= length;
2391

    
2392
        for(i=0; i<length; i++)
2393
                coeff[i]= c;
2394

    
2395
        return vec;
2396
}
2397

    
2398

    
2399
SwsVector *sws_getIdentityVec(void){
2400
        double *coeff= memalign(sizeof(double), sizeof(double));
2401
        SwsVector *vec= malloc(sizeof(SwsVector));
2402
        coeff[0]= 1.0;
2403

    
2404
        vec->coeff= coeff;
2405
        vec->length= 1;
2406

    
2407
        return vec;
2408
}
2409

    
2410
void sws_normalizeVec(SwsVector *a, double height){
2411
        int i;
2412
        double sum=0;
2413
        double inv;
2414

    
2415
        for(i=0; i<a->length; i++)
2416
                sum+= a->coeff[i];
2417

    
2418
        inv= height/sum;
2419

    
2420
        for(i=0; i<a->length; i++)
2421
                a->coeff[i]*= inv;
2422
}
2423

    
2424
void sws_scaleVec(SwsVector *a, double scalar){
2425
        int i;
2426

    
2427
        for(i=0; i<a->length; i++)
2428
                a->coeff[i]*= scalar;
2429
}
2430

    
2431
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2432
        int length= a->length + b->length - 1;
2433
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2434
        int i, j;
2435
        SwsVector *vec= malloc(sizeof(SwsVector));
2436

    
2437
        vec->coeff= coeff;
2438
        vec->length= length;
2439

    
2440
        for(i=0; i<length; i++) coeff[i]= 0.0;
2441

    
2442
        for(i=0; i<a->length; i++)
2443
        {
2444
                for(j=0; j<b->length; j++)
2445
                {
2446
                        coeff[i+j]+= a->coeff[i]*b->coeff[j];
2447
                }
2448
        }
2449

    
2450
        return vec;
2451
}
2452

    
2453
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2454
        int length= MAX(a->length, b->length);
2455
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2456
        int i;
2457
        SwsVector *vec= malloc(sizeof(SwsVector));
2458

    
2459
        vec->coeff= coeff;
2460
        vec->length= length;
2461

    
2462
        for(i=0; i<length; i++) coeff[i]= 0.0;
2463

    
2464
        for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2465
        for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2466

    
2467
        return vec;
2468
}
2469

    
2470
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2471
        int length= MAX(a->length, b->length);
2472
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2473
        int i;
2474
        SwsVector *vec= malloc(sizeof(SwsVector));
2475

    
2476
        vec->coeff= coeff;
2477
        vec->length= length;
2478

    
2479
        for(i=0; i<length; i++) coeff[i]= 0.0;
2480

    
2481
        for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2482
        for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2483

    
2484
        return vec;
2485
}
2486

    
2487
/* shift left / or right if "shift" is negative */
2488
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2489
        int length= a->length + ABS(shift)*2;
2490
        double *coeff= memalign(sizeof(double), length*sizeof(double));
2491
        int i;
2492
        SwsVector *vec= malloc(sizeof(SwsVector));
2493

    
2494
        vec->coeff= coeff;
2495
        vec->length= length;
2496

    
2497
        for(i=0; i<length; i++) coeff[i]= 0.0;
2498

    
2499
        for(i=0; i<a->length; i++)
2500
        {
2501
                coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2502
        }
2503

    
2504
        return vec;
2505
}
2506

    
2507
void sws_shiftVec(SwsVector *a, int shift){
2508
        SwsVector *shifted= sws_getShiftedVec(a, shift);
2509
        free(a->coeff);
2510
        a->coeff= shifted->coeff;
2511
        a->length= shifted->length;
2512
        free(shifted);
2513
}
2514

    
2515
void sws_addVec(SwsVector *a, SwsVector *b){
2516
        SwsVector *sum= sws_sumVec(a, b);
2517
        free(a->coeff);
2518
        a->coeff= sum->coeff;
2519
        a->length= sum->length;
2520
        free(sum);
2521
}
2522

    
2523
void sws_subVec(SwsVector *a, SwsVector *b){
2524
        SwsVector *diff= sws_diffVec(a, b);
2525
        free(a->coeff);
2526
        a->coeff= diff->coeff;
2527
        a->length= diff->length;
2528
        free(diff);
2529
}
2530

    
2531
void sws_convVec(SwsVector *a, SwsVector *b){
2532
        SwsVector *conv= sws_getConvVec(a, b);
2533
        free(a->coeff);  
2534
        a->coeff= conv->coeff;
2535
        a->length= conv->length;
2536
        free(conv);
2537
}
2538

    
2539
SwsVector *sws_cloneVec(SwsVector *a){
2540
        double *coeff= memalign(sizeof(double), a->length*sizeof(double));
2541
        int i;
2542
        SwsVector *vec= malloc(sizeof(SwsVector));
2543

    
2544
        vec->coeff= coeff;
2545
        vec->length= a->length;
2546

    
2547
        for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2548

    
2549
        return vec;
2550
}
2551

    
2552
void sws_printVec(SwsVector *a){
2553
        int i;
2554
        double max=0;
2555
        double min=0;
2556
        double range;
2557

    
2558
        for(i=0; i<a->length; i++)
2559
                if(a->coeff[i]>max) max= a->coeff[i];
2560

    
2561
        for(i=0; i<a->length; i++)
2562
                if(a->coeff[i]<min) min= a->coeff[i];
2563

    
2564
        range= max - min;
2565

    
2566
        for(i=0; i<a->length; i++)
2567
        {
2568
                int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2569
                MSG_DBG2("%1.3f ", a->coeff[i]);
2570
                for(;x>0; x--) MSG_DBG2(" ");
2571
                MSG_DBG2("|\n");
2572
        }
2573
}
2574

    
2575
void sws_freeVec(SwsVector *a){
2576
        if(!a) return;
2577
        if(a->coeff) free(a->coeff);
2578
        a->coeff=NULL;
2579
        a->length=0;
2580
        free(a);
2581
}
2582

    
2583
void sws_freeFilter(SwsFilter *filter){
2584
        if(!filter) return;
2585

    
2586
        if(filter->lumH) sws_freeVec(filter->lumH);
2587
        if(filter->lumV) sws_freeVec(filter->lumV);
2588
        if(filter->chrH) sws_freeVec(filter->chrH);
2589
        if(filter->chrV) sws_freeVec(filter->chrV);
2590
        free(filter);
2591
}
2592

    
2593

    
2594
void sws_freeContext(SwsContext *c){
2595
        int i;
2596
        if(!c) return;
2597

    
2598
        if(c->lumPixBuf)
2599
        {
2600
                for(i=0; i<c->vLumBufSize; i++)
2601
                {
2602
                        if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
2603
                        c->lumPixBuf[i]=NULL;
2604
                }
2605
                free(c->lumPixBuf);
2606
                c->lumPixBuf=NULL;
2607
        }
2608

    
2609
        if(c->chrPixBuf)
2610
        {
2611
                for(i=0; i<c->vChrBufSize; i++)
2612
                {
2613
                        if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
2614
                        c->chrPixBuf[i]=NULL;
2615
                }
2616
                free(c->chrPixBuf);
2617
                c->chrPixBuf=NULL;
2618
        }
2619

    
2620
        if(c->vLumFilter) free(c->vLumFilter);
2621
        c->vLumFilter = NULL;
2622
        if(c->vChrFilter) free(c->vChrFilter);
2623
        c->vChrFilter = NULL;
2624
        if(c->hLumFilter) free(c->hLumFilter);
2625
        c->hLumFilter = NULL;
2626
        if(c->hChrFilter) free(c->hChrFilter);
2627
        c->hChrFilter = NULL;
2628

    
2629
        if(c->vLumFilterPos) free(c->vLumFilterPos);
2630
        c->vLumFilterPos = NULL;
2631
        if(c->vChrFilterPos) free(c->vChrFilterPos);
2632
        c->vChrFilterPos = NULL;
2633
        if(c->hLumFilterPos) free(c->hLumFilterPos);
2634
        c->hLumFilterPos = NULL;
2635
        if(c->hChrFilterPos) free(c->hChrFilterPos);
2636
        c->hChrFilterPos = NULL;
2637

    
2638
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2639
#ifdef HAVE_SYS_MMAN_H
2640
        if(c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2641
        if(c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2642
#else
2643
        if(c->funnyYCode) free(c->funnyYCode);
2644
        if(c->funnyUVCode) free(c->funnyUVCode);
2645
#endif
2646
        c->funnyYCode=NULL;
2647
        c->funnyUVCode=NULL;
2648
#endif
2649

    
2650
        if(c->lumMmx2Filter) free(c->lumMmx2Filter);
2651
        c->lumMmx2Filter=NULL;
2652
        if(c->chrMmx2Filter) free(c->chrMmx2Filter);
2653
        c->chrMmx2Filter=NULL;
2654
        if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
2655
        c->lumMmx2FilterPos=NULL;
2656
        if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
2657
        c->chrMmx2FilterPos=NULL;
2658
        if(c->yuvTable) free(c->yuvTable);
2659
        c->yuvTable=NULL;
2660

    
2661
        free(c);
2662
}
2663