Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 69796008

History | View | Annotate | Download (76.9 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 <assert.h>
59
#ifdef HAVE_MALLOC_H
60
#include <malloc.h>
61
#else
62
#include <stdlib.h>
63
#endif
64
#ifdef HAVE_SYS_MMAN_H
65
#include <sys/mman.h>
66
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67
#define MAP_ANONYMOUS MAP_ANON
68
#endif
69
#endif
70
#include "swscale.h"
71
#include "swscale_internal.h"
72
#include "x86_cpu.h"
73
#include "bswap.h"
74
#include "img_format.h"
75
#include "rgb2rgb.h"
76
#ifdef USE_FASTMEMCPY
77
#include "libvo/fastmemcpy.h"
78
#endif
79

    
80
#undef MOVNTQ
81
#undef PAVGB
82

    
83
//#undef HAVE_MMX2
84
//#define HAVE_3DNOW
85
//#undef HAVE_MMX
86
//#undef ARCH_X86
87
//#define WORDS_BIGENDIAN
88
#define DITHER1XBPP
89

    
90
#define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
91

    
92
#define RET 0xC3 //near return opcode for X86
93

    
94
#ifdef MP_DEBUG
95
#define ASSERT(x) assert(x);
96
#else
97
#define ASSERT(x) ;
98
#endif
99

    
100
#ifdef M_PI
101
#define PI M_PI
102
#else
103
#define PI 3.14159265358979323846
104
#endif
105

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

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

    
137
extern const int32_t Inverse_Table_6_9[8][4];
138

    
139
/*
140
NOTES
141
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
142

143
TODO
144
more intelligent missalignment avoidance for the horizontal scaler
145
write special vertical cubic upscale version
146
Optimize C code (yv12 / minmax)
147
add support for packed pixel yuv input & output
148
add support for Y8 output
149
optimize bgr24 & bgr32
150
add BGR4 output support
151
write special BGR->BGR scaler
152
*/
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 /* FAST_BGR2YV12 */
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 /* defined(ARCH_X86) || defined(ARCH_X86_64) */
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
char *sws_format_name(int format)
214
{
215
    static char fmt_name[64];
216
    char *res;
217
    static int buffer;
218

    
219
    res = fmt_name + buffer * 32;
220
    buffer = 1 - buffer;
221
    snprintf(res, 32, "0x%x (%c%c%c%c)", format,
222
                    format >> 24, (format >> 16) & 0xFF,
223
                    (format >> 8) & 0xFF,
224
                    format & 0xFF);
225

    
226
    return res;
227
}
228

    
229
#if defined(ARCH_X86) || defined(ARCH_X86_64)
230
void in_asm_used_var_warning_killer()
231
{
232
 volatile int i= bF8+bFC+w10+
233
 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
234
 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
235
 if(i) i=0;
236
}
237
#endif
238

    
239
static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
240
                                    int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
241
                                    uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
242
{
243
        //FIXME Optimize (just quickly writen not opti..)
244
        int i;
245
        for(i=0; i<dstW; i++)
246
        {
247
                int val=1<<18;
248
                int j;
249
                for(j=0; j<lumFilterSize; j++)
250
                        val += lumSrc[j][i] * lumFilter[j];
251

    
252
                dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
253
        }
254

    
255
        if(uDest != NULL)
256
                for(i=0; i<chrDstW; i++)
257
                {
258
                        int u=1<<18;
259
                        int v=1<<18;
260
                        int j;
261
                        for(j=0; j<chrFilterSize; j++)
262
                        {
263
                                u += chrSrc[j][i] * chrFilter[j];
264
                                v += chrSrc[j][i + 2048] * chrFilter[j];
265
                        }
266

    
267
                        uDest[i]= FFMIN(FFMAX(u>>19, 0), 255);
268
                        vDest[i]= FFMIN(FFMAX(v>>19, 0), 255);
269
                }
270
}
271

    
272
static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
273
                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
274
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
275
{
276
        //FIXME Optimize (just quickly writen not opti..)
277
        int i;
278
        for(i=0; i<dstW; i++)
279
        {
280
                int val=1<<18;
281
                int j;
282
                for(j=0; j<lumFilterSize; j++)
283
                        val += lumSrc[j][i] * lumFilter[j];
284

    
285
                dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
286
        }
287

    
288
        if(uDest == NULL)
289
                return;
290

    
291
        if(dstFormat == IMGFMT_NV12)
292
                for(i=0; i<chrDstW; i++)
293
                {
294
                        int u=1<<18;
295
                        int v=1<<18;
296
                        int j;
297
                        for(j=0; j<chrFilterSize; j++)
298
                        {
299
                                u += chrSrc[j][i] * chrFilter[j];
300
                                v += chrSrc[j][i + 2048] * chrFilter[j];
301
                        }
302

    
303
                        uDest[2*i]= FFMIN(FFMAX(u>>19, 0), 255);
304
                        uDest[2*i+1]= FFMIN(FFMAX(v>>19, 0), 255);
305
                }
306
        else
307
                for(i=0; i<chrDstW; i++)
308
                {
309
                        int u=1<<18;
310
                        int v=1<<18;
311
                        int j;
312
                        for(j=0; j<chrFilterSize; j++)
313
                        {
314
                                u += chrSrc[j][i] * chrFilter[j];
315
                                v += chrSrc[j][i + 2048] * chrFilter[j];
316
                        }
317

    
318
                        uDest[2*i]= FFMIN(FFMAX(v>>19, 0), 255);
319
                        uDest[2*i+1]= FFMIN(FFMAX(u>>19, 0), 255);
320
                }
321
}
322

    
323
#define YSCALE_YUV_2_PACKEDX_C(type) \
324
                for(i=0; i<(dstW>>1); i++){\
325
                        int j;\
326
                        int Y1=1<<18;\
327
                        int Y2=1<<18;\
328
                        int U=1<<18;\
329
                        int V=1<<18;\
330
                        type *r, *b, *g;\
331
                        const int i2= 2*i;\
332
                        \
333
                        for(j=0; j<lumFilterSize; j++)\
334
                        {\
335
                                Y1 += lumSrc[j][i2] * lumFilter[j];\
336
                                Y2 += lumSrc[j][i2+1] * lumFilter[j];\
337
                        }\
338
                        for(j=0; j<chrFilterSize; j++)\
339
                        {\
340
                                U += chrSrc[j][i] * chrFilter[j];\
341
                                V += chrSrc[j][i+2048] * chrFilter[j];\
342
                        }\
343
                        Y1>>=19;\
344
                        Y2>>=19;\
345
                        U >>=19;\
346
                        V >>=19;\
347
                        if((Y1|Y2|U|V)&256)\
348
                        {\
349
                                if(Y1>255)   Y1=255;\
350
                                else if(Y1<0)Y1=0;\
351
                                if(Y2>255)   Y2=255;\
352
                                else if(Y2<0)Y2=0;\
353
                                if(U>255)    U=255;\
354
                                else if(U<0) U=0;\
355
                                if(V>255)    V=255;\
356
                                else if(V<0) V=0;\
357
                        }
358
                        
359
#define YSCALE_YUV_2_RGBX_C(type) \
360
                        YSCALE_YUV_2_PACKEDX_C(type)\
361
                        r = c->table_rV[V];\
362
                        g = c->table_gU[U] + c->table_gV[V];\
363
                        b = c->table_bU[U];\
364

    
365
#define YSCALE_YUV_2_PACKED2_C \
366
                for(i=0; i<(dstW>>1); i++){\
367
                        const int i2= 2*i;\
368
                        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;\
369
                        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
370
                        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;\
371
                        int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
372

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

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

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

    
395
#define YSCALE_YUV_2_PACKED1B_C \
396
                for(i=0; i<(dstW>>1); i++){\
397
                        const int i2= 2*i;\
398
                        int Y1= buf0[i2  ]>>7;\
399
                        int Y2= buf0[i2+1]>>7;\
400
                        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
401
                        int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
402

    
403
#define YSCALE_YUV_2_RGB1B_C(type) \
404
                        YSCALE_YUV_2_PACKED1B_C\
405
                        type *r, *b, *g;\
406
                        r = c->table_rV[V];\
407
                        g = c->table_gU[U] + c->table_gV[V];\
408
                        b = c->table_bU[U];\
409

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

    
588

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

    
699
                                for(j=0; j<lumFilterSize; j++)
700
                                {
701
                                        Y1 += lumSrc[j][i] * lumFilter[j];
702
                                        Y2 += lumSrc[j][i+1] * lumFilter[j];
703
                                }
704
                                Y1>>=19;
705
                                Y2>>=19;
706
                                if((Y1|Y2)&256)
707
                                {
708
                                        if(Y1>255)   Y1=255;
709
                                        else if(Y1<0)Y1=0;
710
                                        if(Y2>255)   Y2=255;
711
                                        else if(Y2<0)Y2=0;
712
                                }
713
                                acc+= acc + g[Y1+d128[(i+0)&7]];
714
                                acc+= acc + g[Y2+d128[(i+1)&7]];
715
                                if((i&7)==6){
716
                                        ((uint8_t*)dest)[0]= acc;
717
                                        dest++;
718
                                }
719
                        }
720
                }
721
                break;
722
        case IMGFMT_YUY2:
723
                YSCALE_YUV_2_PACKEDX_C(void)
724
                        ((uint8_t*)dest)[2*i2+0]= Y1;
725
                        ((uint8_t*)dest)[2*i2+1]= U;
726
                        ((uint8_t*)dest)[2*i2+2]= Y2;
727
                        ((uint8_t*)dest)[2*i2+3]= V;
728
                }
729
                break;
730
        case IMGFMT_UYVY:
731
                YSCALE_YUV_2_PACKEDX_C(void)
732
                        ((uint8_t*)dest)[2*i2+0]= U;
733
                        ((uint8_t*)dest)[2*i2+1]= Y1;
734
                        ((uint8_t*)dest)[2*i2+2]= V;
735
                        ((uint8_t*)dest)[2*i2+3]= Y2;
736
                }
737
                break;
738
        }
739
}
740

    
741

    
742
//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
743
//Plain C versions
744
#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
745
#define COMPILE_C
746
#endif
747

    
748
#ifdef ARCH_POWERPC
749
#if defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)
750
#define COMPILE_ALTIVEC
751
#endif //HAVE_ALTIVEC
752
#endif //ARCH_POWERPC
753

    
754
#if defined(ARCH_X86) || defined(ARCH_X86_64)
755

    
756
#if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
757
#define COMPILE_MMX
758
#endif
759

    
760
#if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
761
#define COMPILE_MMX2
762
#endif
763

    
764
#if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
765
#define COMPILE_3DNOW
766
#endif
767
#endif //ARCH_X86 || ARCH_X86_64
768

    
769
#undef HAVE_MMX
770
#undef HAVE_MMX2
771
#undef HAVE_3DNOW
772

    
773
#ifdef COMPILE_C
774
#undef HAVE_MMX
775
#undef HAVE_MMX2
776
#undef HAVE_3DNOW
777
#undef HAVE_ALTIVEC
778
#define RENAME(a) a ## _C
779
#include "swscale_template.c"
780
#endif
781

    
782
#ifdef ARCH_POWERPC
783
#ifdef COMPILE_ALTIVEC
784
#undef RENAME
785
#define HAVE_ALTIVEC
786
#define RENAME(a) a ## _altivec
787
#include "swscale_template.c"
788
#endif
789
#endif //ARCH_POWERPC
790

    
791
#if defined(ARCH_X86) || defined(ARCH_X86_64)
792

    
793
//X86 versions
794
/*
795
#undef RENAME
796
#undef HAVE_MMX
797
#undef HAVE_MMX2
798
#undef HAVE_3DNOW
799
#define ARCH_X86
800
#define RENAME(a) a ## _X86
801
#include "swscale_template.c"
802
*/
803
//MMX versions
804
#ifdef COMPILE_MMX
805
#undef RENAME
806
#define HAVE_MMX
807
#undef HAVE_MMX2
808
#undef HAVE_3DNOW
809
#define RENAME(a) a ## _MMX
810
#include "swscale_template.c"
811
#endif
812

    
813
//MMX2 versions
814
#ifdef COMPILE_MMX2
815
#undef RENAME
816
#define HAVE_MMX
817
#define HAVE_MMX2
818
#undef HAVE_3DNOW
819
#define RENAME(a) a ## _MMX2
820
#include "swscale_template.c"
821
#endif
822

    
823
//3DNOW versions
824
#ifdef COMPILE_3DNOW
825
#undef RENAME
826
#define HAVE_MMX
827
#undef HAVE_MMX2
828
#define HAVE_3DNOW
829
#define RENAME(a) a ## _3DNow
830
#include "swscale_template.c"
831
#endif
832

    
833
#endif //ARCH_X86 || ARCH_X86_64
834

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

    
837
static double getSplineCoeff(double a, double b, double c, double d, double dist)
838
{
839
//        printf("%f %f %f %f %f\n", a,b,c,d,dist);
840
        if(dist<=1.0)         return ((d*dist + c)*dist + b)*dist +a;
841
        else                return getSplineCoeff(        0.0, 
842
                                                 b+ 2.0*c + 3.0*d,
843
                                                        c + 3.0*d,
844
                                                -b- 3.0*c - 6.0*d,
845
                                                dist-1.0);
846
}
847

    
848
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
849
                              int srcW, int dstW, int filterAlign, int one, int flags,
850
                              SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
851
{
852
        int i;
853
        int filterSize;
854
        int filter2Size;
855
        int minFilterSize;
856
        double *filter=NULL;
857
        double *filter2=NULL;
858
#if defined(ARCH_X86) || defined(ARCH_X86_64)
859
        if(flags & SWS_CPU_CAPS_MMX)
860
                asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
861
#endif
862

    
863
        // Note the +1 is for the MMXscaler which reads over the end
864
        *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
865

    
866
        if(ABS(xInc - 0x10000) <10) // unscaled
867
        {
868
                int i;
869
                filterSize= 1;
870
                filter= av_malloc(dstW*sizeof(double)*filterSize);
871
                for(i=0; i<dstW*filterSize; i++) filter[i]=0;
872

    
873
                for(i=0; i<dstW; i++)
874
                {
875
                        filter[i*filterSize]=1;
876
                        (*filterPos)[i]=i;
877
                }
878

    
879
        }
880
        else if(flags&SWS_POINT) // lame looking point sampling mode
881
        {
882
                int i;
883
                int xDstInSrc;
884
                filterSize= 1;
885
                filter= av_malloc(dstW*sizeof(double)*filterSize);
886
                
887
                xDstInSrc= xInc/2 - 0x8000;
888
                for(i=0; i<dstW; i++)
889
                {
890
                        int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
891

    
892
                        (*filterPos)[i]= xx;
893
                        filter[i]= 1.0;
894
                        xDstInSrc+= xInc;
895
                }
896
        }
897
        else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
898
        {
899
                int i;
900
                int xDstInSrc;
901
                if     (flags&SWS_BICUBIC) filterSize= 4;
902
                else if(flags&SWS_X      ) filterSize= 4;
903
                else                           filterSize= 2; // SWS_BILINEAR / SWS_AREA 
904
                filter= av_malloc(dstW*sizeof(double)*filterSize);
905

    
906
                xDstInSrc= xInc/2 - 0x8000;
907
                for(i=0; i<dstW; i++)
908
                {
909
                        int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
910
                        int j;
911

    
912
                        (*filterPos)[i]= xx;
913
                                //Bilinear upscale / linear interpolate / Area averaging
914
                                for(j=0; j<filterSize; j++)
915
                                {
916
                                        double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
917
                                        double coeff= 1.0 - d;
918
                                        if(coeff<0) coeff=0;
919
                                        filter[i*filterSize + j]= coeff;
920
                                        xx++;
921
                                }
922
                        xDstInSrc+= xInc;
923
                }
924
        }
925
        else
926
        {
927
                double xDstInSrc;
928
                double sizeFactor, filterSizeInSrc;
929
                const double xInc1= (double)xInc / (double)(1<<16);
930

    
931
                if     (flags&SWS_BICUBIC)        sizeFactor= 4.0;
932
                else if(flags&SWS_X)                sizeFactor= 8.0;
933
                else if(flags&SWS_AREA)                sizeFactor= 1.0; //downscale only, for upscale it is bilinear
934
                else if(flags&SWS_GAUSS)        sizeFactor= 8.0;   // infinite ;)
935
                else if(flags&SWS_LANCZOS)        sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
936
                else if(flags&SWS_SINC)                sizeFactor= 20.0; // infinite ;)
937
                else if(flags&SWS_SPLINE)        sizeFactor= 20.0;  // infinite ;)
938
                else if(flags&SWS_BILINEAR)        sizeFactor= 2.0;
939
                else {
940
                        sizeFactor= 0.0; //GCC warning killer
941
                        ASSERT(0)
942
                }
943
                
944
                if(xInc1 <= 1.0)        filterSizeInSrc= sizeFactor; // upscale
945
                else                        filterSizeInSrc= sizeFactor*srcW / (double)dstW;
946

    
947
                filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
948
                if(filterSize > srcW-2) filterSize=srcW-2;
949

    
950
                filter= av_malloc(dstW*sizeof(double)*filterSize);
951

    
952
                xDstInSrc= xInc1 / 2.0 - 0.5;
953
                for(i=0; i<dstW; i++)
954
                {
955
                        int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
956
                        int j;
957
                        (*filterPos)[i]= xx;
958
                        for(j=0; j<filterSize; j++)
959
                        {
960
                                double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
961
                                double coeff;
962
                                if(flags & SWS_BICUBIC)
963
                                {
964
                                        double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
965
                                        double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
966

    
967
                                        if(d<1.0) 
968
                                                coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
969
                                        else if(d<2.0)
970
                                                coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
971
                                        else
972
                                                coeff=0.0;
973
                                }
974
/*                                else if(flags & SWS_X)
975
                                {
976
                                        double p= param ? param*0.01 : 0.3;
977
                                        coeff = d ? sin(d*PI)/(d*PI) : 1.0;
978
                                        coeff*= pow(2.0, - p*d*d);
979
                                }*/
980
                                else if(flags & SWS_X)
981
                                {
982
                                        double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
983
                                        
984
                                        if(d<1.0)
985
                                                coeff = cos(d*PI);
986
                                        else
987
                                                coeff=-1.0;
988
                                        if(coeff<0.0)         coeff= -pow(-coeff, A);
989
                                        else                coeff=  pow( coeff, A);
990
                                        coeff= coeff*0.5 + 0.5;
991
                                }
992
                                else if(flags & SWS_AREA)
993
                                {
994
                                        double srcPixelSize= 1.0/xInc1;
995
                                        if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
996
                                        else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
997
                                        else coeff=0.0;
998
                                }
999
                                else if(flags & SWS_GAUSS)
1000
                                {
1001
                                        double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1002
                                        coeff = pow(2.0, - p*d*d);
1003
                                }
1004
                                else if(flags & SWS_SINC)
1005
                                {
1006
                                        coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1007
                                }
1008
                                else if(flags & SWS_LANCZOS)
1009
                                {
1010
                                        double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0; 
1011
                                        coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1012
                                        if(d>p) coeff=0;
1013
                                }
1014
                                else if(flags & SWS_BILINEAR)
1015
                                {
1016
                                        coeff= 1.0 - d;
1017
                                        if(coeff<0) coeff=0;
1018
                                }
1019
                                else if(flags & SWS_SPLINE)
1020
                                {
1021
                                        double p=-2.196152422706632;
1022
                                        coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1023
                                }
1024
                                else {
1025
                                        coeff= 0.0; //GCC warning killer
1026
                                        ASSERT(0)
1027
                                }
1028

    
1029
                                filter[i*filterSize + j]= coeff;
1030
                                xx++;
1031
                        }
1032
                        xDstInSrc+= xInc1;
1033
                }
1034
        }
1035

    
1036
        /* apply src & dst Filter to filter -> filter2
1037
           av_free(filter);
1038
        */
1039
        ASSERT(filterSize>0)
1040
        filter2Size= filterSize;
1041
        if(srcFilter) filter2Size+= srcFilter->length - 1;
1042
        if(dstFilter) filter2Size+= dstFilter->length - 1;
1043
        ASSERT(filter2Size>0)
1044
        filter2= av_malloc(filter2Size*dstW*sizeof(double));
1045

    
1046
        for(i=0; i<dstW; i++)
1047
        {
1048
                int j;
1049
                SwsVector scaleFilter;
1050
                SwsVector *outVec;
1051

    
1052
                scaleFilter.coeff= filter + i*filterSize;
1053
                scaleFilter.length= filterSize;
1054

    
1055
                if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1056
                else              outVec= &scaleFilter;
1057

    
1058
                ASSERT(outVec->length == filter2Size)
1059
                //FIXME dstFilter
1060

    
1061
                for(j=0; j<outVec->length; j++)
1062
                {
1063
                        filter2[i*filter2Size + j]= outVec->coeff[j];
1064
                }
1065

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

    
1068
                if(outVec != &scaleFilter) sws_freeVec(outVec);
1069
        }
1070
        av_free(filter); filter=NULL;
1071

    
1072
        /* try to reduce the filter-size (step1 find size and shift left) */
1073
        // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
1074
        minFilterSize= 0;
1075
        for(i=dstW-1; i>=0; i--)
1076
        {
1077
                int min= filter2Size;
1078
                int j;
1079
                double cutOff=0.0;
1080

    
1081
                /* get rid off near zero elements on the left by shifting left */
1082
                for(j=0; j<filter2Size; j++)
1083
                {
1084
                        int k;
1085
                        cutOff += ABS(filter2[i*filter2Size]);
1086

    
1087
                        if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1088

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

    
1092
                        // Move filter coeffs left
1093
                        for(k=1; k<filter2Size; k++)
1094
                                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1095
                        filter2[i*filter2Size + k - 1]= 0.0;
1096
                        (*filterPos)[i]++;
1097
                }
1098

    
1099
                cutOff=0.0;
1100
                /* count near zeros on the right */
1101
                for(j=filter2Size-1; j>0; j--)
1102
                {
1103
                        cutOff += ABS(filter2[i*filter2Size + j]);
1104

    
1105
                        if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1106
                        min--;
1107
                }
1108

    
1109
                if(min>minFilterSize) minFilterSize= min;
1110
        }
1111

    
1112
        if (flags & SWS_CPU_CAPS_ALTIVEC) {
1113
          // we can handle the special case 4,
1114
          // so we don't want to go to the full 8
1115
          if (minFilterSize < 5)
1116
            filterAlign = 4;
1117

    
1118
          // we really don't want to waste our time
1119
          // doing useless computation, so fall-back on
1120
          // the scalar C code for very small filter.
1121
          // vectorizing is worth it only if you have
1122
          // decent-sized vector.
1123
          if (minFilterSize < 3)
1124
            filterAlign = 1;
1125
        }
1126

    
1127
        if (flags & SWS_CPU_CAPS_MMX) {
1128
                // special case for unscaled vertical filtering
1129
                if(minFilterSize == 1 && filterAlign == 2)
1130
                        filterAlign= 1;
1131
        }
1132

    
1133
        ASSERT(minFilterSize > 0)
1134
        filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1135
        ASSERT(filterSize > 0)
1136
        filter= av_malloc(filterSize*dstW*sizeof(double));
1137
        if(filterSize >= MAX_FILTER_SIZE)
1138
                return -1;
1139
        *outFilterSize= filterSize;
1140

    
1141
        if(flags&SWS_PRINT_INFO)
1142
                MSG_V("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1143
        /* try to reduce the filter-size (step2 reduce it) */
1144
        for(i=0; i<dstW; i++)
1145
        {
1146
                int j;
1147

    
1148
                for(j=0; j<filterSize; j++)
1149
                {
1150
                        if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
1151
                        else                   filter[i*filterSize + j]= filter2[i*filter2Size + j];
1152
                }
1153
        }
1154
        av_free(filter2); filter2=NULL;
1155
        
1156

    
1157
        //FIXME try to align filterpos if possible
1158

    
1159
        //fix borders
1160
        for(i=0; i<dstW; i++)
1161
        {
1162
                int j;
1163
                if((*filterPos)[i] < 0)
1164
                {
1165
                        // Move filter coeffs left to compensate for filterPos
1166
                        for(j=1; j<filterSize; j++)
1167
                        {
1168
                                int left= FFMAX(j + (*filterPos)[i], 0);
1169
                                filter[i*filterSize + left] += filter[i*filterSize + j];
1170
                                filter[i*filterSize + j]=0;
1171
                        }
1172
                        (*filterPos)[i]= 0;
1173
                }
1174

    
1175
                if((*filterPos)[i] + filterSize > srcW)
1176
                {
1177
                        int shift= (*filterPos)[i] + filterSize - srcW;
1178
                        // Move filter coeffs right to compensate for filterPos
1179
                        for(j=filterSize-2; j>=0; j--)
1180
                        {
1181
                                int right= FFMIN(j + shift, filterSize-1);
1182
                                filter[i*filterSize +right] += filter[i*filterSize +j];
1183
                                filter[i*filterSize +j]=0;
1184
                        }
1185
                        (*filterPos)[i]= srcW - filterSize;
1186
                }
1187
        }
1188

    
1189
        // Note the +1 is for the MMXscaler which reads over the end
1190
        /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1191
        *outFilter= av_malloc(*outFilterSize*(dstW+1)*sizeof(int16_t));
1192
        memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
1193

    
1194
        /* Normalize & Store in outFilter */
1195
        for(i=0; i<dstW; i++)
1196
        {
1197
                int j;
1198
                double error=0;
1199
                double sum=0;
1200
                double scale= one;
1201

    
1202
                for(j=0; j<filterSize; j++)
1203
                {
1204
                        sum+= filter[i*filterSize + j];
1205
                }
1206
                scale/= sum;
1207
                for(j=0; j<*outFilterSize; j++)
1208
                {
1209
                        double v= filter[i*filterSize + j]*scale + error;
1210
                        int intV= floor(v + 0.5);
1211
                        (*outFilter)[i*(*outFilterSize) + j]= intV;
1212
                        error = v - intV;
1213
                }
1214
        }
1215
        
1216
        (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1217
        for(i=0; i<*outFilterSize; i++)
1218
        {
1219
                int j= dstW*(*outFilterSize);
1220
                (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1221
        }
1222

    
1223
        av_free(filter);
1224
        return 0;
1225
}
1226

    
1227
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1228
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1229
{
1230
        uint8_t *fragmentA;
1231
        long imm8OfPShufW1A;
1232
        long imm8OfPShufW2A;
1233
        long fragmentLengthA;
1234
        uint8_t *fragmentB;
1235
        long imm8OfPShufW1B;
1236
        long imm8OfPShufW2B;
1237
        long fragmentLengthB;
1238
        int fragmentPos;
1239

    
1240
        int xpos, i;
1241

    
1242
        // create an optimized horizontal scaling routine
1243

    
1244
        //code fragment
1245

    
1246
        asm volatile(
1247
                "jmp 9f                                \n\t"
1248
        // Begin
1249
                "0:                                \n\t"
1250
                "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t" 
1251
                "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t" 
1252
                "movd 1(%%"REG_c", %%"REG_S"), %%mm1\n\t"
1253
                "punpcklbw %%mm7, %%mm1                \n\t"
1254
                "punpcklbw %%mm7, %%mm0                \n\t"
1255
                "pshufw $0xFF, %%mm1, %%mm1        \n\t"
1256
                "1:                                \n\t"
1257
                "pshufw $0xFF, %%mm0, %%mm0        \n\t"
1258
                "2:                                \n\t"
1259
                "psubw %%mm1, %%mm0                \n\t"
1260
                "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
1261
                "pmullw %%mm3, %%mm0                \n\t"
1262
                "psllw $7, %%mm1                \n\t"
1263
                "paddw %%mm1, %%mm0                \n\t"
1264

    
1265
                "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
1266

    
1267
                "add $8, %%"REG_a"                \n\t"
1268
        // End
1269
                "9:                                \n\t"
1270
//                "int $3\n\t"
1271
                "lea 0b, %0                        \n\t"
1272
                "lea 1b, %1                        \n\t"
1273
                "lea 2b, %2                        \n\t"
1274
                "dec %1                                \n\t"
1275
                "dec %2                                \n\t"
1276
                "sub %0, %1                        \n\t"
1277
                "sub %0, %2                        \n\t"
1278
                "lea 9b, %3                        \n\t"
1279
                "sub %0, %3                        \n\t"
1280

    
1281

    
1282
                :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1283
                "=r" (fragmentLengthA)
1284
        );
1285

    
1286
        asm volatile(
1287
                "jmp 9f                                \n\t"
1288
        // Begin
1289
                "0:                                \n\t"
1290
                "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t" 
1291
                "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t" 
1292
                "punpcklbw %%mm7, %%mm0                \n\t"
1293
                "pshufw $0xFF, %%mm0, %%mm1        \n\t"
1294
                "1:                                \n\t"
1295
                "pshufw $0xFF, %%mm0, %%mm0        \n\t"
1296
                "2:                                \n\t"
1297
                "psubw %%mm1, %%mm0                \n\t"
1298
                "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
1299
                "pmullw %%mm3, %%mm0                \n\t"
1300
                "psllw $7, %%mm1                \n\t"
1301
                "paddw %%mm1, %%mm0                \n\t"
1302

    
1303
                "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
1304

    
1305
                "add $8, %%"REG_a"                \n\t"
1306
        // End
1307
                "9:                                \n\t"
1308
//                "int $3\n\t"
1309
                "lea 0b, %0                        \n\t"
1310
                "lea 1b, %1                        \n\t"
1311
                "lea 2b, %2                        \n\t"
1312
                "dec %1                                \n\t"
1313
                "dec %2                                \n\t"
1314
                "sub %0, %1                        \n\t"
1315
                "sub %0, %2                        \n\t"
1316
                "lea 9b, %3                        \n\t"
1317
                "sub %0, %3                        \n\t"
1318

    
1319

    
1320
                :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1321
                "=r" (fragmentLengthB)
1322
        );
1323

    
1324
        xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1325
        fragmentPos=0;
1326
        
1327
        for(i=0; i<dstW/numSplits; i++)
1328
        {
1329
                int xx=xpos>>16;
1330

    
1331
                if((i&3) == 0)
1332
                {
1333
                        int a=0;
1334
                        int b=((xpos+xInc)>>16) - xx;
1335
                        int c=((xpos+xInc*2)>>16) - xx;
1336
                        int d=((xpos+xInc*3)>>16) - xx;
1337

    
1338
                        filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1339
                        filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1340
                        filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1341
                        filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1342
                        filterPos[i/2]= xx;
1343

    
1344
                        if(d+1<4)
1345
                        {
1346
                                int maxShift= 3-(d+1);
1347
                                int shift=0;
1348

    
1349
                                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1350

    
1351
                                funnyCode[fragmentPos + imm8OfPShufW1B]=
1352
                                        (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1353
                                funnyCode[fragmentPos + imm8OfPShufW2B]=
1354
                                        a | (b<<2) | (c<<4) | (d<<6);
1355

    
1356
                                if(i+3>=dstW) shift=maxShift; //avoid overread
1357
                                else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1358

    
1359
                                if(shift && i>=shift)
1360
                                {
1361
                                        funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1362
                                        funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1363
                                        filterPos[i/2]-=shift;
1364
                                }
1365

    
1366
                                fragmentPos+= fragmentLengthB;
1367
                        }
1368
                        else
1369
                        {
1370
                                int maxShift= 3-d;
1371
                                int shift=0;
1372

    
1373
                                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1374

    
1375
                                funnyCode[fragmentPos + imm8OfPShufW1A]=
1376
                                funnyCode[fragmentPos + imm8OfPShufW2A]=
1377
                                        a | (b<<2) | (c<<4) | (d<<6);
1378

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

    
1382
                                if(shift && i>=shift)
1383
                                {
1384
                                        funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1385
                                        funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1386
                                        filterPos[i/2]-=shift;
1387
                                }
1388

    
1389
                                fragmentPos+= fragmentLengthA;
1390
                        }
1391

    
1392
                        funnyCode[fragmentPos]= RET;
1393
                }
1394
                xpos+=xInc;
1395
        }
1396
        filterPos[i/2]= xpos>>16; // needed to jump to the next part
1397
}
1398
#endif // ARCH_X86 || ARCH_X86_64
1399

    
1400
static void globalInit(void){
1401
    // generating tables:
1402
    int i;
1403
    for(i=0; i<768; i++){
1404
        int c= FFMIN(FFMAX(i-256, 0), 255);
1405
        clip_table[i]=c;
1406
    }
1407
}
1408

    
1409
static SwsFunc getSwsFunc(int flags){
1410
    
1411
#ifdef RUNTIME_CPUDETECT
1412
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1413
        // ordered per speed fasterst first
1414
        if(flags & SWS_CPU_CAPS_MMX2)
1415
                return swScale_MMX2;
1416
        else if(flags & SWS_CPU_CAPS_3DNOW)
1417
                return swScale_3DNow;
1418
        else if(flags & SWS_CPU_CAPS_MMX)
1419
                return swScale_MMX;
1420
        else
1421
                return swScale_C;
1422

    
1423
#else
1424
#ifdef ARCH_POWERPC
1425
        if(flags & SWS_CPU_CAPS_ALTIVEC)
1426
          return swScale_altivec;
1427
        else
1428
          return swScale_C;
1429
#endif
1430
        return swScale_C;
1431
#endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
1432
#else //RUNTIME_CPUDETECT
1433
#ifdef HAVE_MMX2
1434
        return swScale_MMX2;
1435
#elif defined (HAVE_3DNOW)
1436
        return swScale_3DNow;
1437
#elif defined (HAVE_MMX)
1438
        return swScale_MMX;
1439
#elif defined (HAVE_ALTIVEC)
1440
        return swScale_altivec;
1441
#else
1442
        return swScale_C;
1443
#endif
1444
#endif //!RUNTIME_CPUDETECT
1445
}
1446

    
1447
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1448
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1449
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1450
        /* Copy Y plane */
1451
        if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
1452
                memcpy(dst, src[0], srcSliceH*dstStride[0]);
1453
        else
1454
        {
1455
                int i;
1456
                uint8_t *srcPtr= src[0];
1457
                uint8_t *dstPtr= dst;
1458
                for(i=0; i<srcSliceH; i++)
1459
                {
1460
                        memcpy(dstPtr, srcPtr, c->srcW);
1461
                        srcPtr+= srcStride[0];
1462
                        dstPtr+= dstStride[0];
1463
                }
1464
        }
1465
        dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1466
        if (c->dstFormat == IMGFMT_NV12)
1467
                interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
1468
        else
1469
                interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
1470

    
1471
        return srcSliceH;
1472
}
1473

    
1474
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1475
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1476
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1477

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

    
1480
        return srcSliceH;
1481
}
1482

    
1483
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1484
             int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1485
        uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1486

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

    
1489
        return srcSliceH;
1490
}
1491

    
1492
/* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1493
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1494
                           int srcSliceH, uint8_t* dst[], int dstStride[]){
1495
        const int srcFormat= c->srcFormat;
1496
        const int dstFormat= c->dstFormat;
1497
        const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
1498
        const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
1499
        const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 
1500
        const int dstId= (dstFormat&0xFF)>>2;
1501
        void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1502

    
1503
        /* BGR -> BGR */
1504
        if(   (isBGR(srcFormat) && isBGR(dstFormat))
1505
           || (isRGB(srcFormat) && isRGB(dstFormat))){
1506
                switch(srcId | (dstId<<4)){
1507
                case 0x34: conv= rgb16to15; break;
1508
                case 0x36: conv= rgb24to15; break;
1509
                case 0x38: conv= rgb32to15; break;
1510
                case 0x43: conv= rgb15to16; break;
1511
                case 0x46: conv= rgb24to16; break;
1512
                case 0x48: conv= rgb32to16; break;
1513
                case 0x63: conv= rgb15to24; break;
1514
                case 0x64: conv= rgb16to24; break;
1515
                case 0x68: conv= rgb32to24; break;
1516
                case 0x83: conv= rgb15to32; break;
1517
                case 0x84: conv= rgb16to32; break;
1518
                case 0x86: conv= rgb24to32; break;
1519
                default: MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1520
                                 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1521
                }
1522
        }else if(   (isBGR(srcFormat) && isRGB(dstFormat))
1523
                 || (isRGB(srcFormat) && isBGR(dstFormat))){
1524
                switch(srcId | (dstId<<4)){
1525
                case 0x33: conv= rgb15tobgr15; break;
1526
                case 0x34: conv= rgb16tobgr15; break;
1527
                case 0x36: conv= rgb24tobgr15; break;
1528
                case 0x38: conv= rgb32tobgr15; break;
1529
                case 0x43: conv= rgb15tobgr16; break;
1530
                case 0x44: conv= rgb16tobgr16; break;
1531
                case 0x46: conv= rgb24tobgr16; break;
1532
                case 0x48: conv= rgb32tobgr16; break;
1533
                case 0x63: conv= rgb15tobgr24; break;
1534
                case 0x64: conv= rgb16tobgr24; break;
1535
                case 0x66: conv= rgb24tobgr24; break;
1536
                case 0x68: conv= rgb32tobgr24; break;
1537
                case 0x83: conv= rgb15tobgr32; break;
1538
                case 0x84: conv= rgb16tobgr32; break;
1539
                case 0x86: conv= rgb24tobgr32; break;
1540
                case 0x88: conv= rgb32tobgr32; break;
1541
                default: MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1542
                                 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1543
                }
1544
        }else{
1545
                MSG_ERR("swScaler: internal error %s -> %s converter\n", 
1546
                         sws_format_name(srcFormat), sws_format_name(dstFormat));
1547
        }
1548

    
1549
        if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
1550
                conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1551
        else
1552
        {
1553
                int i;
1554
                uint8_t *srcPtr= src[0];
1555
                uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1556

    
1557
                for(i=0; i<srcSliceH; i++)
1558
                {
1559
                        conv(srcPtr, dstPtr, c->srcW*srcBpp);
1560
                        srcPtr+= srcStride[0];
1561
                        dstPtr+= dstStride[0];
1562
                }
1563
        }     
1564
        return srcSliceH;
1565
}
1566

    
1567
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1568
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1569

    
1570
        rgb24toyv12(
1571
                src[0], 
1572
                dst[0]+ srcSliceY    *dstStride[0], 
1573
                dst[1]+(srcSliceY>>1)*dstStride[1], 
1574
                dst[2]+(srcSliceY>>1)*dstStride[2],
1575
                c->srcW, srcSliceH, 
1576
                dstStride[0], dstStride[1], srcStride[0]);
1577
        return srcSliceH;
1578
}
1579

    
1580
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1581
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1582
        int i;
1583

    
1584
        /* copy Y */
1585
        if(srcStride[0]==dstStride[0] && srcStride[0] > 0) 
1586
                memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1587
        else{
1588
                uint8_t *srcPtr= src[0];
1589
                uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1590

    
1591
                for(i=0; i<srcSliceH; i++)
1592
                {
1593
                        memcpy(dstPtr, srcPtr, c->srcW);
1594
                        srcPtr+= srcStride[0];
1595
                        dstPtr+= dstStride[0];
1596
                }
1597
        }
1598

    
1599
        if(c->dstFormat==IMGFMT_YV12){
1600
                planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1601
                planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1602
        }else{
1603
                planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1604
                planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1605
        }
1606
        return srcSliceH;
1607
}
1608

    
1609
/**
1610
 * bring pointers in YUV order instead of YVU
1611
 */
1612
static inline void sws_orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
1613
        if(format == IMGFMT_YV12 || format == IMGFMT_YVU9
1614
           || format == IMGFMT_444P || format == IMGFMT_422P || format == IMGFMT_411P){
1615
                sortedP[0]= p[0];
1616
                sortedP[1]= p[2];
1617
                sortedP[2]= p[1];
1618
                sortedStride[0]= stride[0];
1619
                sortedStride[1]= stride[2];
1620
                sortedStride[2]= stride[1];
1621
        }
1622
        else if(isPacked(format) || isGray(format) || format == IMGFMT_Y8)
1623
        {
1624
                sortedP[0]= p[0];
1625
                sortedP[1]= 
1626
                sortedP[2]= NULL;
1627
                sortedStride[0]= stride[0];
1628
                sortedStride[1]= 
1629
                sortedStride[2]= 0;
1630
        }
1631
        else if(format == IMGFMT_I420 || format == IMGFMT_IYUV)
1632
        {
1633
                sortedP[0]= p[0];
1634
                sortedP[1]= p[1];
1635
                sortedP[2]= p[2];
1636
                sortedStride[0]= stride[0];
1637
                sortedStride[1]= stride[1];
1638
                sortedStride[2]= stride[2];
1639
        }
1640
        else if(format == IMGFMT_NV12 || format == IMGFMT_NV21)
1641
        {
1642
                sortedP[0]= p[0];
1643
                sortedP[1]= p[1];
1644
                sortedP[2]= NULL;
1645
                sortedStride[0]= stride[0];
1646
                sortedStride[1]= stride[1];
1647
                sortedStride[2]= 0;
1648
        }else{
1649
                MSG_ERR("internal error in orderYUV\n");
1650
        }
1651
}
1652

    
1653
/* unscaled copy like stuff (assumes nearly identical formats) */
1654
static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1655
             int srcSliceH, uint8_t* dst[], int dstStride[]){
1656

    
1657
        if(isPacked(c->srcFormat))
1658
        {
1659
                if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
1660
                        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1661
                else
1662
                {
1663
                        int i;
1664
                        uint8_t *srcPtr= src[0];
1665
                        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1666
                        int length=0;
1667

    
1668
                        /* universal length finder */
1669
                        while(length+c->srcW <= ABS(dstStride[0]) 
1670
                           && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
1671
                        ASSERT(length!=0);
1672

    
1673
                        for(i=0; i<srcSliceH; i++)
1674
                        {
1675
                                memcpy(dstPtr, srcPtr, length);
1676
                                srcPtr+= srcStride[0];
1677
                                dstPtr+= dstStride[0];
1678
                        }
1679
                }
1680
        }
1681
        else 
1682
        { /* Planar YUV or gray */
1683
                int plane;
1684
                for(plane=0; plane<3; plane++)
1685
                {
1686
                        int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1687
                        int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1688
                        int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1689

    
1690
                        if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1691
                        {
1692
                                if(!isGray(c->dstFormat))
1693
                                        memset(dst[plane], 128, dstStride[plane]*height);
1694
                        }
1695
                        else
1696
                        {
1697
                                if(dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1698
                                        memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1699
                                else
1700
                                {
1701
                                        int i;
1702
                                        uint8_t *srcPtr= src[plane];
1703
                                        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1704
                                        for(i=0; i<height; i++)
1705
                                        {
1706
                                                memcpy(dstPtr, srcPtr, length);
1707
                                                srcPtr+= srcStride[plane];
1708
                                                dstPtr+= dstStride[plane];
1709
                                        }
1710
                                }
1711
                        }
1712
                }
1713
        }
1714
        return srcSliceH;
1715
}
1716

    
1717
static int remove_dup_fourcc(int fourcc)
1718
{
1719
        switch(fourcc)
1720
        {
1721
            case IMGFMT_I420:
1722
            case IMGFMT_IYUV: return IMGFMT_YV12;
1723
            case IMGFMT_Y8  : return IMGFMT_Y800;
1724
            case IMGFMT_IF09: return IMGFMT_YVU9;
1725
            default: return fourcc;
1726
        }
1727
}
1728

    
1729
static void getSubSampleFactors(int *h, int *v, int format){
1730
        switch(format){
1731
        case IMGFMT_UYVY:
1732
        case IMGFMT_YUY2:
1733
                *h=1;
1734
                *v=0;
1735
                break;
1736
        case IMGFMT_YV12:
1737
        case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
1738
        case IMGFMT_NV12:
1739
        case IMGFMT_NV21:
1740
                *h=1;
1741
                *v=1;
1742
                break;
1743
        case IMGFMT_YVU9:
1744
                *h=2;
1745
                *v=2;
1746
                break;
1747
        case IMGFMT_444P:
1748
                *h=0;
1749
                *v=0;
1750
                break;
1751
        case IMGFMT_422P:
1752
                *h=1;
1753
                *v=0;
1754
                break;
1755
        case IMGFMT_411P:
1756
                *h=2;
1757
                *v=0;
1758
                break;
1759
        default:
1760
                *h=0;
1761
                *v=0;
1762
                break;
1763
        }
1764
}
1765

    
1766
static uint16_t roundToInt16(int64_t f){
1767
        int r= (f + (1<<15))>>16;
1768
             if(r<-0x7FFF) return 0x8000;
1769
        else if(r> 0x7FFF) return 0x7FFF;
1770
        else               return r;
1771
}
1772

    
1773
/**
1774
 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1775
 * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
1776
 * @return -1 if not supported
1777
 */
1778
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1779
        int64_t crv =  inv_table[0];
1780
        int64_t cbu =  inv_table[1];
1781
        int64_t cgu = -inv_table[2];
1782
        int64_t cgv = -inv_table[3];
1783
        int64_t cy  = 1<<16;
1784
        int64_t oy  = 0;
1785

    
1786
        if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1787
        memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1788
        memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
1789

    
1790
        c->brightness= brightness;
1791
        c->contrast  = contrast;
1792
        c->saturation= saturation;
1793
        c->srcRange  = srcRange;
1794
        c->dstRange  = dstRange;
1795

    
1796
        c->uOffset=   0x0400040004000400LL;
1797
        c->vOffset=   0x0400040004000400LL;
1798

    
1799
        if(!srcRange){
1800
                cy= (cy*255) / 219;
1801
                oy= 16<<16;
1802
        }
1803

    
1804
        cy = (cy *contrast             )>>16;
1805
        crv= (crv*contrast * saturation)>>32;
1806
        cbu= (cbu*contrast * saturation)>>32;
1807
        cgu= (cgu*contrast * saturation)>>32;
1808
        cgv= (cgv*contrast * saturation)>>32;
1809

    
1810
        oy -= 256*brightness;
1811

    
1812
        c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
1813
        c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
1814
        c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1815
        c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1816
        c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1817
        c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
1818

    
1819
        yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1820
        //FIXME factorize
1821

    
1822
#ifdef COMPILE_ALTIVEC
1823
        if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1824
            yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1825
#endif        
1826
        return 0;
1827
}
1828

    
1829
/**
1830
 * @return -1 if not supported
1831
 */
1832
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1833
        if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1834

    
1835
        *inv_table = c->srcColorspaceTable;
1836
        *table     = c->dstColorspaceTable;
1837
        *srcRange  = c->srcRange;
1838
        *dstRange  = c->dstRange;
1839
        *brightness= c->brightness;
1840
        *contrast  = c->contrast;
1841
        *saturation= c->saturation;
1842
        
1843
        return 0;        
1844
}
1845

    
1846
SwsContext *sws_getContext(int srcW, int srcH, int origSrcFormat, int dstW, int dstH, int origDstFormat, int flags,
1847
                         SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
1848

    
1849
        SwsContext *c;
1850
        int i;
1851
        int usesVFilter, usesHFilter;
1852
        int unscaled, needsDither;
1853
        int srcFormat, dstFormat;
1854
        SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1855
#if defined(ARCH_X86) || defined(ARCH_X86_64)
1856
        if(flags & SWS_CPU_CAPS_MMX)
1857
                asm volatile("emms\n\t"::: "memory");
1858
#endif
1859

    
1860
#ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
1861
        flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC);
1862
#ifdef HAVE_MMX2
1863
        flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1864
#elif defined (HAVE_3DNOW)
1865
        flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1866
#elif defined (HAVE_MMX)
1867
        flags |= SWS_CPU_CAPS_MMX;
1868
#elif defined (HAVE_ALTIVEC)
1869
        flags |= SWS_CPU_CAPS_ALTIVEC;
1870
#endif
1871
#endif /* RUNTIME_CPUDETECT */
1872
        if(clip_table[512] != 255) globalInit();
1873
        if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
1874

    
1875
        /* avoid duplicate Formats, so we don't need to check to much */
1876
        srcFormat = remove_dup_fourcc(origSrcFormat);
1877
        dstFormat = remove_dup_fourcc(origDstFormat);
1878

    
1879
        unscaled = (srcW == dstW && srcH == dstH);
1880
        needsDither= (isBGR(dstFormat) || isRGB(dstFormat)) 
1881
                     && (dstFormat&0xFF)<24
1882
                     && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
1883

    
1884
        if(!isSupportedIn(srcFormat)) 
1885
        {
1886
                MSG_ERR("swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
1887
                return NULL;
1888
        }
1889
        if(!isSupportedOut(dstFormat))
1890
        {
1891
                MSG_ERR("swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
1892
                return NULL;
1893
        }
1894

    
1895
        /* sanity check */
1896
        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
1897
        {
1898
                 MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n", 
1899
                        srcW, srcH, dstW, dstH);
1900
                return NULL;
1901
        }
1902

    
1903
        if(!dstFilter) dstFilter= &dummyFilter;
1904
        if(!srcFilter) srcFilter= &dummyFilter;
1905

    
1906
        c= av_malloc(sizeof(SwsContext));
1907
        memset(c, 0, sizeof(SwsContext));
1908

    
1909
        c->srcW= srcW;
1910
        c->srcH= srcH;
1911
        c->dstW= dstW;
1912
        c->dstH= dstH;
1913
        c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1914
        c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1915
        c->flags= flags;
1916
        c->dstFormat= dstFormat;
1917
        c->srcFormat= srcFormat;
1918
        c->origDstFormat= origDstFormat;
1919
        c->origSrcFormat= origSrcFormat;
1920
        c->vRounder= 4* 0x0001000100010001ULL;
1921

    
1922
        usesHFilter= usesVFilter= 0;
1923
        if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
1924
        if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
1925
        if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
1926
        if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
1927
        if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
1928
        if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
1929
        if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
1930
        if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
1931

    
1932
        getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
1933
        getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
1934

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

    
1938
        // drop some chroma lines if the user wants it
1939
        c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
1940
        c->chrSrcVSubSample+= c->vChrDrop;
1941

    
1942
        // drop every 2. pixel for chroma calculation unless user wants full chroma
1943
        if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)) 
1944
                c->chrSrcHSubSample=1;
1945

    
1946
        if(param){
1947
                c->param[0] = param[0];
1948
                c->param[1] = param[1];
1949
        }else{
1950
                c->param[0] =
1951
                c->param[1] = SWS_PARAM_DEFAULT;
1952
        }
1953

    
1954
        c->chrIntHSubSample= c->chrDstHSubSample;
1955
        c->chrIntVSubSample= c->chrSrcVSubSample;
1956

    
1957
        // note the -((-x)>>y) is so that we allways round toward +inf
1958
        c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
1959
        c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
1960
        c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
1961
        c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
1962

    
1963
        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); 
1964

    
1965
        /* unscaled special Cases */
1966
        if(unscaled && !usesHFilter && !usesVFilter)
1967
        {
1968
                /* yv12_to_nv12 */
1969
                if(srcFormat == IMGFMT_YV12 && (dstFormat == IMGFMT_NV12 || dstFormat == IMGFMT_NV21))
1970
                {
1971
                        c->swScale= PlanarToNV12Wrapper;
1972
                }
1973
                /* yuv2bgr */
1974
                if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
1975
                {
1976
                        c->swScale= yuv2rgb_get_func_ptr(c);
1977
                }
1978
                
1979
                if( srcFormat==IMGFMT_YVU9 && dstFormat==IMGFMT_YV12 )
1980
                {
1981
                        c->swScale= yvu9toyv12Wrapper;
1982
                }
1983

    
1984
                /* bgr24toYV12 */
1985
                if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
1986
                        c->swScale= bgr24toyv12Wrapper;
1987
                
1988
                /* rgb/bgr -> rgb/bgr (no dither needed forms) */
1989
                if(   (isBGR(srcFormat) || isRGB(srcFormat))
1990
                   && (isBGR(dstFormat) || isRGB(dstFormat)) 
1991
                   && !needsDither)
1992
                        c->swScale= rgb2rgbWrapper;
1993

    
1994
                /* LQ converters if -sws 0 or -sws 4*/
1995
                if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
1996
                        /* rgb/bgr -> rgb/bgr (dither needed forms) */
1997
                        if(  (isBGR(srcFormat) || isRGB(srcFormat))
1998
                          && (isBGR(dstFormat) || isRGB(dstFormat)) 
1999
                          && needsDither)
2000
                                c->swScale= rgb2rgbWrapper;
2001

    
2002
                        /* yv12_to_yuy2 */
2003
                        if(srcFormat == IMGFMT_YV12 && 
2004
                            (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY))
2005
                        {
2006
                                if (dstFormat == IMGFMT_YUY2)
2007
                                    c->swScale= PlanarToYuy2Wrapper;
2008
                                else
2009
                                    c->swScale= PlanarToUyvyWrapper;
2010
                        }
2011
                }
2012

    
2013
#ifdef COMPILE_ALTIVEC
2014
                if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2015
                    ((srcFormat == IMGFMT_YV12 && 
2016
                      (dstFormat == IMGFMT_YUY2 || dstFormat == IMGFMT_UYVY)))) {
2017
                  // unscaled YV12 -> packed YUV, we want speed
2018
                  if (dstFormat == IMGFMT_YUY2)
2019
                    c->swScale= yv12toyuy2_unscaled_altivec;
2020
                  else
2021
                    c->swScale= yv12touyvy_unscaled_altivec;
2022
                }
2023
#endif
2024

    
2025
                /* simple copy */
2026
                if(   srcFormat == dstFormat
2027
                   || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2028
                   || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2029
                  )
2030
                {
2031
                        c->swScale= simpleCopy;
2032
                }
2033

    
2034
                if(c->swScale){
2035
                        if(flags&SWS_PRINT_INFO)
2036
                                MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n", 
2037
                                        sws_format_name(srcFormat), sws_format_name(dstFormat));
2038
                        return c;
2039
                }
2040
        }
2041

    
2042
        if(flags & SWS_CPU_CAPS_MMX2)
2043
        {
2044
                c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2045
                if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2046
                {
2047
                        if(flags&SWS_PRINT_INFO)
2048
                                MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
2049
                }
2050
                if(usesHFilter) c->canMMX2BeUsed=0;
2051
        }
2052
        else
2053
                c->canMMX2BeUsed=0;
2054

    
2055
        c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2056
        c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2057

    
2058
        // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2059
        // but only for the FAST_BILINEAR mode otherwise do correct scaling
2060
        // n-2 is the last chrominance sample available
2061
        // this is not perfect, but noone shuld notice the difference, the more correct variant
2062
        // would be like the vertical one, but that would require some special code for the
2063
        // first and last pixel
2064
        if(flags&SWS_FAST_BILINEAR)
2065
        {
2066
                if(c->canMMX2BeUsed)
2067
                {
2068
                        c->lumXInc+= 20;
2069
                        c->chrXInc+= 20;
2070
                }
2071
                //we don't use the x86asm scaler if mmx is available
2072
                else if(flags & SWS_CPU_CAPS_MMX)
2073
                {
2074
                        c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2075
                        c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2076
                }
2077
        }
2078

    
2079
        /* precalculate horizontal scaler filter coefficients */
2080
        {
2081
                const int filterAlign=
2082
                  (flags & SWS_CPU_CAPS_MMX) ? 4 :
2083
                  (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2084
                  1;
2085

    
2086
                initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2087
                                 srcW      ,       dstW, filterAlign, 1<<14,
2088
                                 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2089
                                 srcFilter->lumH, dstFilter->lumH, c->param);
2090
                initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2091
                                 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2092
                                 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2093
                                 srcFilter->chrH, dstFilter->chrH, c->param);
2094

    
2095
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2096
// can't downscale !!!
2097
                if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2098
                {
2099
#define MAX_FUNNY_CODE_SIZE 10000
2100
#ifdef MAP_ANONYMOUS
2101
                        c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2102
                        c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2103
#else
2104
                        c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2105
                        c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2106
#endif
2107

    
2108
                        c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2109
                        c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2110
                        c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2111
                        c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2112

    
2113
                        initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2114
                        initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2115
                }
2116
#endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
2117
        } // Init Horizontal stuff
2118

    
2119

    
2120

    
2121
        /* precalculate vertical scaler filter coefficients */
2122
        {
2123
                const int filterAlign=
2124
                  (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2125
                  (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2126
                  1;
2127

    
2128
                initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2129
                                srcH      ,        dstH, filterAlign, (1<<12)-4,
2130
                                (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2131
                                srcFilter->lumV, dstFilter->lumV, c->param);
2132
                initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2133
                                c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2134
                                (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2135
                                srcFilter->chrV, dstFilter->chrV, c->param);
2136

    
2137
#ifdef HAVE_ALTIVEC
2138
                c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2139
                c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2140

    
2141
                for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2142
                  int j;
2143
                  short *p = (short *)&c->vYCoeffsBank[i];
2144
                  for (j=0;j<8;j++)
2145
                    p[j] = c->vLumFilter[i];
2146
                }
2147

    
2148
                for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2149
                  int j;
2150
                  short *p = (short *)&c->vCCoeffsBank[i];
2151
                  for (j=0;j<8;j++)
2152
                    p[j] = c->vChrFilter[i];
2153
                }
2154
#endif
2155
        }
2156

    
2157
        // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2158
        c->vLumBufSize= c->vLumFilterSize;
2159
        c->vChrBufSize= c->vChrFilterSize;
2160
        for(i=0; i<dstH; i++)
2161
        {
2162
                int chrI= i*c->chrDstH / dstH;
2163
                int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2164
                                 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2165

    
2166
                nextSlice>>= c->chrSrcVSubSample;
2167
                nextSlice<<= c->chrSrcVSubSample;
2168
                if(c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2169
                        c->vLumBufSize= nextSlice - c->vLumFilterPos[i   ];
2170
                if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2171
                        c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2172
        }
2173

    
2174
        // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2175
        c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2176
        c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2177
        //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)
2178
        /* align at 16 bytes for AltiVec */
2179
        for(i=0; i<c->vLumBufSize; i++)
2180
                c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_malloc(4000);
2181
        for(i=0; i<c->vChrBufSize; i++)
2182
                c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
2183

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

    
2188
        ASSERT(c->chrDstH <= dstH)
2189

    
2190
        if(flags&SWS_PRINT_INFO)
2191
        {
2192
#ifdef DITHER1XBPP
2193
                char *dither= " dithered";
2194
#else
2195
                char *dither= "";
2196
#endif
2197
                if(flags&SWS_FAST_BILINEAR)
2198
                        MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
2199
                else if(flags&SWS_BILINEAR)
2200
                        MSG_INFO("\nSwScaler: BILINEAR scaler, ");
2201
                else if(flags&SWS_BICUBIC)
2202
                        MSG_INFO("\nSwScaler: BICUBIC scaler, ");
2203
                else if(flags&SWS_X)
2204
                        MSG_INFO("\nSwScaler: Experimental scaler, ");
2205
                else if(flags&SWS_POINT)
2206
                        MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
2207
                else if(flags&SWS_AREA)
2208
                        MSG_INFO("\nSwScaler: Area Averageing scaler, ");
2209
                else if(flags&SWS_BICUBLIN)
2210
                        MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
2211
                else if(flags&SWS_GAUSS)
2212
                        MSG_INFO("\nSwScaler: Gaussian scaler, ");
2213
                else if(flags&SWS_SINC)
2214
                        MSG_INFO("\nSwScaler: Sinc scaler, ");
2215
                else if(flags&SWS_LANCZOS)
2216
                        MSG_INFO("\nSwScaler: Lanczos scaler, ");
2217
                else if(flags&SWS_SPLINE)
2218
                        MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
2219
                else
2220
                        MSG_INFO("\nSwScaler: ehh flags invalid?! ");
2221

    
2222
                if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
2223
                        MSG_INFO("from %s to%s %s ", 
2224
                                sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2225
                else
2226
                        MSG_INFO("from %s to %s ", 
2227
                                sws_format_name(srcFormat), sws_format_name(dstFormat));
2228

    
2229
                if(flags & SWS_CPU_CAPS_MMX2)
2230
                        MSG_INFO("using MMX2\n");
2231
                else if(flags & SWS_CPU_CAPS_3DNOW)
2232
                        MSG_INFO("using 3DNOW\n");
2233
                else if(flags & SWS_CPU_CAPS_MMX)
2234
                        MSG_INFO("using MMX\n");
2235
                else if(flags & SWS_CPU_CAPS_ALTIVEC)
2236
                        MSG_INFO("using AltiVec\n");
2237
                else 
2238
                        MSG_INFO("using C\n");
2239
        }
2240

    
2241
        if(flags & SWS_PRINT_INFO)
2242
        {
2243
                if(flags & SWS_CPU_CAPS_MMX)
2244
                {
2245
                        if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2246
                                MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2247
                        else
2248
                        {
2249
                                if(c->hLumFilterSize==4)
2250
                                        MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2251
                                else if(c->hLumFilterSize==8)
2252
                                        MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2253
                                else
2254
                                        MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2255

    
2256
                                if(c->hChrFilterSize==4)
2257
                                        MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2258
                                else if(c->hChrFilterSize==8)
2259
                                        MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2260
                                else
2261
                                        MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2262
                        }
2263
                }
2264
                else
2265
                {
2266
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2267
                        MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
2268
#else
2269
                        if(flags & SWS_FAST_BILINEAR)
2270
                                MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2271
                        else
2272
                                MSG_V("SwScaler: using C scaler for horizontal scaling\n");
2273
#endif
2274
                }
2275
                if(isPlanarYUV(dstFormat))
2276
                {
2277
                        if(c->vLumFilterSize==1)
2278
                                MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2279
                        else
2280
                                MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2281
                }
2282
                else
2283
                {
2284
                        if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
2285
                                MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2286
                                       "SwScaler:       2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2287
                        else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
2288
                                MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2289
                        else
2290
                                MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2291
                }
2292

    
2293
                if(dstFormat==IMGFMT_BGR24)
2294
                        MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
2295
                                (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2296
                else if(dstFormat==IMGFMT_BGR32)
2297
                        MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2298
                else if(dstFormat==IMGFMT_BGR16)
2299
                        MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2300
                else if(dstFormat==IMGFMT_BGR15)
2301
                        MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2302

    
2303
                MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2304
        }
2305
        if(flags & SWS_PRINT_INFO)
2306
        {
2307
                MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2308
                        c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2309
                MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2310
                        c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2311
        }
2312

    
2313
        c->swScale= getSwsFunc(flags);
2314
        return c;
2315
}
2316

    
2317
/**
2318
 * swscale warper, so we don't need to export the SwsContext.
2319
 * assumes planar YUV to be in YUV order instead of YVU
2320
 */
2321
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2322
                           int srcSliceH, uint8_t* dst[], int dstStride[]){
2323
        if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2324
            MSG_ERR("swScaler: slices start in the middle!\n");
2325
            return 0;
2326
        }
2327
        if (c->sliceDir == 0) {
2328
            if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2329
        }
2330

    
2331
        // copy strides, so they can safely be modified
2332
        if (c->sliceDir == 1) {
2333
            // slices go from top to bottom
2334
            int srcStride2[3]= {srcStride[0], srcStride[1], srcStride[2]};
2335
            int dstStride2[3]= {dstStride[0], dstStride[1], dstStride[2]};
2336
            return c->swScale(c, src, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2337
        } else {
2338
            // slices go from bottom to top => we flip the image internally
2339
            uint8_t* src2[3]= {src[0] + (srcSliceH-1)*srcStride[0],
2340
                               src[1] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1],
2341
                               src[2] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2]
2342
            };
2343
            uint8_t* dst2[3]= {dst[0] + (c->dstH-1)*dstStride[0],
2344
                               dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2345
                               dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2346
            int srcStride2[3]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2347
            int dstStride2[3]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2348
            
2349
            return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2350
        }
2351
}
2352

    
2353
/**
2354
 * swscale warper, so we don't need to export the SwsContext
2355
 */
2356
int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
2357
                           int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
2358
        int srcStride[3];
2359
        int dstStride[3];
2360
        uint8_t *src[3];
2361
        uint8_t *dst[3];
2362
        sws_orderYUV(c->origSrcFormat, src, srcStride, srcParam, srcStrideParam);
2363
        sws_orderYUV(c->origDstFormat, dst, dstStride, dstParam, dstStrideParam);
2364
//printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
2365

    
2366
        return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2367
}
2368

    
2369
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur, 
2370
                                float lumaSharpen, float chromaSharpen,
2371
                                float chromaHShift, float chromaVShift,
2372
                                int verbose)
2373
{
2374
        SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2375

    
2376
        if(lumaGBlur!=0.0){
2377
                filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2378
                filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2379
        }else{
2380
                filter->lumH= sws_getIdentityVec();
2381
                filter->lumV= sws_getIdentityVec();
2382
        }
2383

    
2384
        if(chromaGBlur!=0.0){
2385
                filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2386
                filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2387
        }else{
2388
                filter->chrH= sws_getIdentityVec();
2389
                filter->chrV= sws_getIdentityVec();
2390
        }
2391

    
2392
        if(chromaSharpen!=0.0){
2393
                SwsVector *id= sws_getIdentityVec();
2394
                sws_scaleVec(filter->chrH, -chromaSharpen);
2395
                sws_scaleVec(filter->chrV, -chromaSharpen);
2396
                sws_addVec(filter->chrH, id);
2397
                sws_addVec(filter->chrV, id);
2398
                sws_freeVec(id);
2399
        }
2400

    
2401
        if(lumaSharpen!=0.0){
2402
                SwsVector *id= sws_getIdentityVec();
2403
                sws_scaleVec(filter->lumH, -lumaSharpen);
2404
                sws_scaleVec(filter->lumV, -lumaSharpen);
2405
                sws_addVec(filter->lumH, id);
2406
                sws_addVec(filter->lumV, id);
2407
                sws_freeVec(id);
2408
        }
2409

    
2410
        if(chromaHShift != 0.0)
2411
                sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2412

    
2413
        if(chromaVShift != 0.0)
2414
                sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2415

    
2416
        sws_normalizeVec(filter->chrH, 1.0);
2417
        sws_normalizeVec(filter->chrV, 1.0);
2418
        sws_normalizeVec(filter->lumH, 1.0);
2419
        sws_normalizeVec(filter->lumV, 1.0);
2420

    
2421
        if(verbose) sws_printVec(filter->chrH);
2422
        if(verbose) sws_printVec(filter->lumH);
2423

    
2424
        return filter;
2425
}
2426

    
2427
/**
2428
 * returns a normalized gaussian curve used to filter stuff
2429
 * quality=3 is high quality, lowwer is lowwer quality
2430
 */
2431
SwsVector *sws_getGaussianVec(double variance, double quality){
2432
        const int length= (int)(variance*quality + 0.5) | 1;
2433
        int i;
2434
        double *coeff= av_malloc(length*sizeof(double));
2435
        double middle= (length-1)*0.5;
2436
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2437

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

    
2441
        for(i=0; i<length; i++)
2442
        {
2443
                double dist= i-middle;
2444
                coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2445
        }
2446

    
2447
        sws_normalizeVec(vec, 1.0);
2448

    
2449
        return vec;
2450
}
2451

    
2452
SwsVector *sws_getConstVec(double c, int length){
2453
        int i;
2454
        double *coeff= av_malloc(length*sizeof(double));
2455
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2456

    
2457
        vec->coeff= coeff;
2458
        vec->length= length;
2459

    
2460
        for(i=0; i<length; i++)
2461
                coeff[i]= c;
2462

    
2463
        return vec;
2464
}
2465

    
2466

    
2467
SwsVector *sws_getIdentityVec(void){
2468
        return sws_getConstVec(1.0, 1);
2469
}
2470

    
2471
double sws_dcVec(SwsVector *a){
2472
        int i;
2473
        double sum=0;
2474

    
2475
        for(i=0; i<a->length; i++)
2476
                sum+= a->coeff[i];
2477

    
2478
        return sum;
2479
}
2480

    
2481
void sws_scaleVec(SwsVector *a, double scalar){
2482
        int i;
2483

    
2484
        for(i=0; i<a->length; i++)
2485
                a->coeff[i]*= scalar;
2486
}
2487

    
2488
void sws_normalizeVec(SwsVector *a, double height){
2489
        sws_scaleVec(a, height/sws_dcVec(a));
2490
}
2491

    
2492
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2493
        int length= a->length + b->length - 1;
2494
        double *coeff= av_malloc(length*sizeof(double));
2495
        int i, j;
2496
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2497

    
2498
        vec->coeff= coeff;
2499
        vec->length= length;
2500

    
2501
        for(i=0; i<length; i++) coeff[i]= 0.0;
2502

    
2503
        for(i=0; i<a->length; i++)
2504
        {
2505
                for(j=0; j<b->length; j++)
2506
                {
2507
                        coeff[i+j]+= a->coeff[i]*b->coeff[j];
2508
                }
2509
        }
2510

    
2511
        return vec;
2512
}
2513

    
2514
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2515
        int length= FFMAX(a->length, b->length);
2516
        double *coeff= av_malloc(length*sizeof(double));
2517
        int i;
2518
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2519

    
2520
        vec->coeff= coeff;
2521
        vec->length= length;
2522

    
2523
        for(i=0; i<length; i++) coeff[i]= 0.0;
2524

    
2525
        for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2526
        for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2527

    
2528
        return vec;
2529
}
2530

    
2531
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2532
        int length= FFMAX(a->length, b->length);
2533
        double *coeff= av_malloc(length*sizeof(double));
2534
        int i;
2535
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2536

    
2537
        vec->coeff= coeff;
2538
        vec->length= length;
2539

    
2540
        for(i=0; i<length; i++) coeff[i]= 0.0;
2541

    
2542
        for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2543
        for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2544

    
2545
        return vec;
2546
}
2547

    
2548
/* shift left / or right if "shift" is negative */
2549
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2550
        int length= a->length + ABS(shift)*2;
2551
        double *coeff= av_malloc(length*sizeof(double));
2552
        int i;
2553
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2554

    
2555
        vec->coeff= coeff;
2556
        vec->length= length;
2557

    
2558
        for(i=0; i<length; i++) coeff[i]= 0.0;
2559

    
2560
        for(i=0; i<a->length; i++)
2561
        {
2562
                coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2563
        }
2564

    
2565
        return vec;
2566
}
2567

    
2568
void sws_shiftVec(SwsVector *a, int shift){
2569
        SwsVector *shifted= sws_getShiftedVec(a, shift);
2570
        av_free(a->coeff);
2571
        a->coeff= shifted->coeff;
2572
        a->length= shifted->length;
2573
        av_free(shifted);
2574
}
2575

    
2576
void sws_addVec(SwsVector *a, SwsVector *b){
2577
        SwsVector *sum= sws_sumVec(a, b);
2578
        av_free(a->coeff);
2579
        a->coeff= sum->coeff;
2580
        a->length= sum->length;
2581
        av_free(sum);
2582
}
2583

    
2584
void sws_subVec(SwsVector *a, SwsVector *b){
2585
        SwsVector *diff= sws_diffVec(a, b);
2586
        av_free(a->coeff);
2587
        a->coeff= diff->coeff;
2588
        a->length= diff->length;
2589
        av_free(diff);
2590
}
2591

    
2592
void sws_convVec(SwsVector *a, SwsVector *b){
2593
        SwsVector *conv= sws_getConvVec(a, b);
2594
        av_free(a->coeff);  
2595
        a->coeff= conv->coeff;
2596
        a->length= conv->length;
2597
        av_free(conv);
2598
}
2599

    
2600
SwsVector *sws_cloneVec(SwsVector *a){
2601
        double *coeff= av_malloc(a->length*sizeof(double));
2602
        int i;
2603
        SwsVector *vec= av_malloc(sizeof(SwsVector));
2604

    
2605
        vec->coeff= coeff;
2606
        vec->length= a->length;
2607

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

    
2610
        return vec;
2611
}
2612

    
2613
void sws_printVec(SwsVector *a){
2614
        int i;
2615
        double max=0;
2616
        double min=0;
2617
        double range;
2618

    
2619
        for(i=0; i<a->length; i++)
2620
                if(a->coeff[i]>max) max= a->coeff[i];
2621

    
2622
        for(i=0; i<a->length; i++)
2623
                if(a->coeff[i]<min) min= a->coeff[i];
2624

    
2625
        range= max - min;
2626

    
2627
        for(i=0; i<a->length; i++)
2628
        {
2629
                int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2630
                MSG_DBG2("%1.3f ", a->coeff[i]);
2631
                for(;x>0; x--) MSG_DBG2(" ");
2632
                MSG_DBG2("|\n");
2633
        }
2634
}
2635

    
2636
void sws_freeVec(SwsVector *a){
2637
        if(!a) return;
2638
        av_free(a->coeff);
2639
        a->coeff=NULL;
2640
        a->length=0;
2641
        av_free(a);
2642
}
2643

    
2644
void sws_freeFilter(SwsFilter *filter){
2645
        if(!filter) return;
2646

    
2647
        if(filter->lumH) sws_freeVec(filter->lumH);
2648
        if(filter->lumV) sws_freeVec(filter->lumV);
2649
        if(filter->chrH) sws_freeVec(filter->chrH);
2650
        if(filter->chrV) sws_freeVec(filter->chrV);
2651
        av_free(filter);
2652
}
2653

    
2654

    
2655
void sws_freeContext(SwsContext *c){
2656
        int i;
2657
        if(!c) return;
2658

    
2659
        if(c->lumPixBuf)
2660
        {
2661
                for(i=0; i<c->vLumBufSize; i++)
2662
                {
2663
                        av_free(c->lumPixBuf[i]);
2664
                        c->lumPixBuf[i]=NULL;
2665
                }
2666
                av_free(c->lumPixBuf);
2667
                c->lumPixBuf=NULL;
2668
        }
2669

    
2670
        if(c->chrPixBuf)
2671
        {
2672
                for(i=0; i<c->vChrBufSize; i++)
2673
                {
2674
                        av_free(c->chrPixBuf[i]);
2675
                        c->chrPixBuf[i]=NULL;
2676
                }
2677
                av_free(c->chrPixBuf);
2678
                c->chrPixBuf=NULL;
2679
        }
2680

    
2681
        av_free(c->vLumFilter);
2682
        c->vLumFilter = NULL;
2683
        av_free(c->vChrFilter);
2684
        c->vChrFilter = NULL;
2685
        av_free(c->hLumFilter);
2686
        c->hLumFilter = NULL;
2687
        av_free(c->hChrFilter);
2688
        c->hChrFilter = NULL;
2689
#ifdef HAVE_ALTIVEC
2690
        av_free(c->vYCoeffsBank);
2691
        c->vYCoeffsBank = NULL;
2692
        av_free(c->vCCoeffsBank);
2693
        c->vCCoeffsBank = NULL;
2694
#endif
2695

    
2696
        av_free(c->vLumFilterPos);
2697
        c->vLumFilterPos = NULL;
2698
        av_free(c->vChrFilterPos);
2699
        c->vChrFilterPos = NULL;
2700
        av_free(c->hLumFilterPos);
2701
        c->hLumFilterPos = NULL;
2702
        av_free(c->hChrFilterPos);
2703
        c->hChrFilterPos = NULL;
2704

    
2705
#if defined(ARCH_X86) || defined(ARCH_X86_64)
2706
#ifdef MAP_ANONYMOUS
2707
        if(c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2708
        if(c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2709
#else
2710
        av_free(c->funnyYCode);
2711
        av_free(c->funnyUVCode);
2712
#endif
2713
        c->funnyYCode=NULL;
2714
        c->funnyUVCode=NULL;
2715
#endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
2716

    
2717
        av_free(c->lumMmx2Filter);
2718
        c->lumMmx2Filter=NULL;
2719
        av_free(c->chrMmx2Filter);
2720
        c->chrMmx2Filter=NULL;
2721
        av_free(c->lumMmx2FilterPos);
2722
        c->lumMmx2FilterPos=NULL;
2723
        av_free(c->chrMmx2FilterPos);
2724
        c->chrMmx2FilterPos=NULL;
2725
        av_free(c->yuvTable);
2726
        c->yuvTable=NULL;
2727

    
2728
        av_free(c);
2729
}
2730