Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 9990e426

History | View | Annotate | Download (99.2 KB)

1
/*
2
 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 *
20
 * the C code (not assembly, mmx, ...) of this file can be used
21
 * under the LGPL license too
22
 */
23

    
24
/*
25
  supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
26
  supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27
  {BGR,RGB}{1,4,8,15,16} support dithering
28

29
  unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30
  YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
31
  x -> x
32
  YUV9 -> YV12
33
  YUV9/YV12 -> Y800
34
  Y800 -> YUV9/YV12
35
  BGR24 -> BGR32 & RGB24 -> RGB32
36
  BGR32 -> BGR24 & RGB32 -> RGB24
37
  BGR15 -> BGR16
38
*/
39

    
40
/*
41
tested special converters (most are tested actually, but I did not write it down ...)
42
 YV12 -> BGR16
43
 YV12 -> YV12
44
 BGR15 -> BGR16
45
 BGR16 -> BGR16
46
 YVU9 -> YV12
47

48
untested special converters
49
  YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be ok)
50
  YV12/I420 -> YV12/I420
51
  YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
52
  BGR24 -> BGR32 & RGB24 -> RGB32
53
  BGR32 -> BGR24 & RGB32 -> RGB24
54
  BGR24 -> YV12
55
*/
56

    
57
#include <inttypes.h>
58
#include <string.h>
59
#include <math.h>
60
#include <stdio.h>
61
#include <unistd.h>
62
#include "config.h"
63
#include <assert.h>
64
#ifdef HAVE_SYS_MMAN_H
65
#include <sys/mman.h>
66
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67
#define MAP_ANONYMOUS MAP_ANON
68
#endif
69
#endif
70
#include "swscale.h"
71
#include "swscale_internal.h"
72
#include "rgb2rgb.h"
73
#include "libavutil/x86_cpu.h"
74
#include "libavutil/bswap.h"
75

    
76
unsigned swscale_version(void)
77
{
78
    return LIBSWSCALE_VERSION_INT;
79
}
80

    
81
#undef MOVNTQ
82
#undef PAVGB
83

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

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

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

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

    
101
#define isSupportedIn(x)    (       \
102
           (x)==PIX_FMT_YUV420P     \
103
        || (x)==PIX_FMT_YUVA420P    \
104
        || (x)==PIX_FMT_YUYV422     \
105
        || (x)==PIX_FMT_UYVY422     \
106
        || (x)==PIX_FMT_RGB32       \
107
        || (x)==PIX_FMT_RGB32_1     \
108
        || (x)==PIX_FMT_BGR24       \
109
        || (x)==PIX_FMT_BGR565      \
110
        || (x)==PIX_FMT_BGR555      \
111
        || (x)==PIX_FMT_BGR32       \
112
        || (x)==PIX_FMT_BGR32_1     \
113
        || (x)==PIX_FMT_RGB24       \
114
        || (x)==PIX_FMT_RGB565      \
115
        || (x)==PIX_FMT_RGB555      \
116
        || (x)==PIX_FMT_GRAY8       \
117
        || (x)==PIX_FMT_YUV410P     \
118
        || (x)==PIX_FMT_GRAY16BE    \
119
        || (x)==PIX_FMT_GRAY16LE    \
120
        || (x)==PIX_FMT_YUV444P     \
121
        || (x)==PIX_FMT_YUV422P     \
122
        || (x)==PIX_FMT_YUV411P     \
123
        || (x)==PIX_FMT_PAL8        \
124
        || (x)==PIX_FMT_BGR8        \
125
        || (x)==PIX_FMT_RGB8        \
126
        || (x)==PIX_FMT_BGR4_BYTE   \
127
        || (x)==PIX_FMT_RGB4_BYTE   \
128
        || (x)==PIX_FMT_YUV440P     \
129
    )
130
#define isSupportedOut(x)   (       \
131
           (x)==PIX_FMT_YUV420P     \
132
        || (x)==PIX_FMT_YUYV422     \
133
        || (x)==PIX_FMT_UYVY422     \
134
        || (x)==PIX_FMT_YUV444P     \
135
        || (x)==PIX_FMT_YUV422P     \
136
        || (x)==PIX_FMT_YUV411P     \
137
        || isRGB(x)                 \
138
        || isBGR(x)                 \
139
        || (x)==PIX_FMT_NV12        \
140
        || (x)==PIX_FMT_NV21        \
141
        || (x)==PIX_FMT_GRAY16BE    \
142
        || (x)==PIX_FMT_GRAY16LE    \
143
        || (x)==PIX_FMT_GRAY8       \
144
        || (x)==PIX_FMT_YUV410P     \
145
    )
146
#define isPacked(x)         (       \
147
           (x)==PIX_FMT_PAL8        \
148
        || (x)==PIX_FMT_YUYV422     \
149
        || (x)==PIX_FMT_UYVY422     \
150
        || isRGB(x)                 \
151
        || isBGR(x)                 \
152
    )
153

    
154
#define RGB2YUV_SHIFT 16
155
#define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
156
#define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
157
#define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
158
#define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
159
#define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
160
#define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
161
#define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
162
#define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
163
#define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
164

    
165
extern const int32_t Inverse_Table_6_9[8][4];
166

    
167
/*
168
NOTES
169
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
170

171
TODO
172
more intelligent misalignment avoidance for the horizontal scaler
173
write special vertical cubic upscale version
174
Optimize C code (yv12 / minmax)
175
add support for packed pixel yuv input & output
176
add support for Y8 output
177
optimize bgr24 & bgr32
178
add BGR4 output support
179
write special BGR->BGR scaler
180
*/
181

    
182
#if defined(ARCH_X86) && defined (CONFIG_GPL)
183
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
184
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
185
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
186
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
187
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
188
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
189
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
190
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
191

    
192
static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
193
static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
194
static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
195
static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
196

    
197
const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
198
        0x0103010301030103LL,
199
        0x0200020002000200LL,};
200

    
201
const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
202
        0x0602060206020602LL,
203
        0x0004000400040004LL,};
204

    
205
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
206
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
207
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
208
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
209
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
210
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
211

    
212
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
213
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
214
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
215

    
216
#ifdef FAST_BGR2YV12
217
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
218
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
219
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
220
#else
221
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
222
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
223
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
224
#endif /* FAST_BGR2YV12 */
225
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
226
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
227
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
228
#endif /* defined(ARCH_X86) */
229

    
230
// clipping helper table for C implementations:
231
static unsigned char clip_table[768];
232

    
233
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
234

    
235
extern const uint8_t dither_2x2_4[2][8];
236
extern const uint8_t dither_2x2_8[2][8];
237
extern const uint8_t dither_8x8_32[8][8];
238
extern const uint8_t dither_8x8_73[8][8];
239
extern const uint8_t dither_8x8_220[8][8];
240

    
241
const char *sws_format_name(enum PixelFormat format)
242
{
243
    switch (format) {
244
        case PIX_FMT_YUV420P:
245
            return "yuv420p";
246
        case PIX_FMT_YUVA420P:
247
            return "yuva420p";
248
        case PIX_FMT_YUYV422:
249
            return "yuyv422";
250
        case PIX_FMT_RGB24:
251
            return "rgb24";
252
        case PIX_FMT_BGR24:
253
            return "bgr24";
254
        case PIX_FMT_YUV422P:
255
            return "yuv422p";
256
        case PIX_FMT_YUV444P:
257
            return "yuv444p";
258
        case PIX_FMT_RGB32:
259
            return "rgb32";
260
        case PIX_FMT_YUV410P:
261
            return "yuv410p";
262
        case PIX_FMT_YUV411P:
263
            return "yuv411p";
264
        case PIX_FMT_RGB565:
265
            return "rgb565";
266
        case PIX_FMT_RGB555:
267
            return "rgb555";
268
        case PIX_FMT_GRAY16BE:
269
            return "gray16be";
270
        case PIX_FMT_GRAY16LE:
271
            return "gray16le";
272
        case PIX_FMT_GRAY8:
273
            return "gray8";
274
        case PIX_FMT_MONOWHITE:
275
            return "mono white";
276
        case PIX_FMT_MONOBLACK:
277
            return "mono black";
278
        case PIX_FMT_PAL8:
279
            return "Palette";
280
        case PIX_FMT_YUVJ420P:
281
            return "yuvj420p";
282
        case PIX_FMT_YUVJ422P:
283
            return "yuvj422p";
284
        case PIX_FMT_YUVJ444P:
285
            return "yuvj444p";
286
        case PIX_FMT_XVMC_MPEG2_MC:
287
            return "xvmc_mpeg2_mc";
288
        case PIX_FMT_XVMC_MPEG2_IDCT:
289
            return "xvmc_mpeg2_idct";
290
        case PIX_FMT_UYVY422:
291
            return "uyvy422";
292
        case PIX_FMT_UYYVYY411:
293
            return "uyyvyy411";
294
        case PIX_FMT_RGB32_1:
295
            return "rgb32x";
296
        case PIX_FMT_BGR32_1:
297
            return "bgr32x";
298
        case PIX_FMT_BGR32:
299
            return "bgr32";
300
        case PIX_FMT_BGR565:
301
            return "bgr565";
302
        case PIX_FMT_BGR555:
303
            return "bgr555";
304
        case PIX_FMT_BGR8:
305
            return "bgr8";
306
        case PIX_FMT_BGR4:
307
            return "bgr4";
308
        case PIX_FMT_BGR4_BYTE:
309
            return "bgr4 byte";
310
        case PIX_FMT_RGB8:
311
            return "rgb8";
312
        case PIX_FMT_RGB4:
313
            return "rgb4";
314
        case PIX_FMT_RGB4_BYTE:
315
            return "rgb4 byte";
316
        case PIX_FMT_NV12:
317
            return "nv12";
318
        case PIX_FMT_NV21:
319
            return "nv21";
320
        case PIX_FMT_YUV440P:
321
            return "yuv440p";
322
        default:
323
            return "Unknown format";
324
    }
325
}
326

    
327
static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
328
                               int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
329
                               uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
330
{
331
    //FIXME Optimize (just quickly writen not opti..)
332
    int i;
333
    for (i=0; i<dstW; i++)
334
    {
335
        int val=1<<18;
336
        int j;
337
        for (j=0; j<lumFilterSize; j++)
338
            val += lumSrc[j][i] * lumFilter[j];
339

    
340
        dest[i]= av_clip_uint8(val>>19);
341
    }
342

    
343
    if (uDest)
344
        for (i=0; i<chrDstW; i++)
345
        {
346
            int u=1<<18;
347
            int v=1<<18;
348
            int j;
349
            for (j=0; j<chrFilterSize; j++)
350
            {
351
                u += chrSrc[j][i] * chrFilter[j];
352
                v += chrSrc[j][i + VOFW] * chrFilter[j];
353
            }
354

    
355
            uDest[i]= av_clip_uint8(u>>19);
356
            vDest[i]= av_clip_uint8(v>>19);
357
        }
358
}
359

    
360
static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
361
                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
362
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
363
{
364
    //FIXME Optimize (just quickly writen not opti..)
365
    int i;
366
    for (i=0; i<dstW; i++)
367
    {
368
        int val=1<<18;
369
        int j;
370
        for (j=0; j<lumFilterSize; j++)
371
            val += lumSrc[j][i] * lumFilter[j];
372

    
373
        dest[i]= av_clip_uint8(val>>19);
374
    }
375

    
376
    if (!uDest)
377
        return;
378

    
379
    if (dstFormat == PIX_FMT_NV12)
380
        for (i=0; i<chrDstW; i++)
381
        {
382
            int u=1<<18;
383
            int v=1<<18;
384
            int j;
385
            for (j=0; j<chrFilterSize; j++)
386
            {
387
                u += chrSrc[j][i] * chrFilter[j];
388
                v += chrSrc[j][i + VOFW] * chrFilter[j];
389
            }
390

    
391
            uDest[2*i]= av_clip_uint8(u>>19);
392
            uDest[2*i+1]= av_clip_uint8(v>>19);
393
        }
394
    else
395
        for (i=0; i<chrDstW; i++)
396
        {
397
            int u=1<<18;
398
            int v=1<<18;
399
            int j;
400
            for (j=0; j<chrFilterSize; j++)
401
            {
402
                u += chrSrc[j][i] * chrFilter[j];
403
                v += chrSrc[j][i + VOFW] * chrFilter[j];
404
            }
405

    
406
            uDest[2*i]= av_clip_uint8(v>>19);
407
            uDest[2*i+1]= av_clip_uint8(u>>19);
408
        }
409
}
410

    
411
#define YSCALE_YUV_2_PACKEDX_C(type) \
412
    for (i=0; i<(dstW>>1); i++){\
413
        int j;\
414
        int Y1 = 1<<18;\
415
        int Y2 = 1<<18;\
416
        int U  = 1<<18;\
417
        int V  = 1<<18;\
418
        type av_unused *r, *b, *g;\
419
        const int i2= 2*i;\
420
        \
421
        for (j=0; j<lumFilterSize; j++)\
422
        {\
423
            Y1 += lumSrc[j][i2] * lumFilter[j];\
424
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
425
        }\
426
        for (j=0; j<chrFilterSize; j++)\
427
        {\
428
            U += chrSrc[j][i] * chrFilter[j];\
429
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
430
        }\
431
        Y1>>=19;\
432
        Y2>>=19;\
433
        U >>=19;\
434
        V >>=19;\
435
        if ((Y1|Y2|U|V)&256)\
436
        {\
437
            if (Y1>255)   Y1=255; \
438
            else if (Y1<0)Y1=0;   \
439
            if (Y2>255)   Y2=255; \
440
            else if (Y2<0)Y2=0;   \
441
            if (U>255)    U=255;  \
442
            else if (U<0) U=0;    \
443
            if (V>255)    V=255;  \
444
            else if (V<0) V=0;    \
445
        }
446

    
447
#define YSCALE_YUV_2_RGBX_C(type) \
448
    YSCALE_YUV_2_PACKEDX_C(type)  \
449
    r = (type *)c->table_rV[V];   \
450
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
451
    b = (type *)c->table_bU[U];   \
452

    
453
#define YSCALE_YUV_2_PACKED2_C   \
454
    for (i=0; i<(dstW>>1); i++){ \
455
        const int i2= 2*i;       \
456
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
457
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
458
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
459
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
460

    
461
#define YSCALE_YUV_2_RGB2_C(type) \
462
    YSCALE_YUV_2_PACKED2_C\
463
    type *r, *b, *g;\
464
    r = (type *)c->table_rV[V];\
465
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
466
    b = (type *)c->table_bU[U];\
467

    
468
#define YSCALE_YUV_2_PACKED1_C \
469
    for (i=0; i<(dstW>>1); i++){\
470
        const int i2= 2*i;\
471
        int Y1= buf0[i2  ]>>7;\
472
        int Y2= buf0[i2+1]>>7;\
473
        int U= (uvbuf1[i     ])>>7;\
474
        int V= (uvbuf1[i+VOFW])>>7;\
475

    
476
#define YSCALE_YUV_2_RGB1_C(type) \
477
    YSCALE_YUV_2_PACKED1_C\
478
    type *r, *b, *g;\
479
    r = (type *)c->table_rV[V];\
480
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
481
    b = (type *)c->table_bU[U];\
482

    
483
#define YSCALE_YUV_2_PACKED1B_C \
484
    for (i=0; i<(dstW>>1); i++){\
485
        const int i2= 2*i;\
486
        int Y1= buf0[i2  ]>>7;\
487
        int Y2= buf0[i2+1]>>7;\
488
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
489
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
490

    
491
#define YSCALE_YUV_2_RGB1B_C(type) \
492
    YSCALE_YUV_2_PACKED1B_C\
493
    type *r, *b, *g;\
494
    r = (type *)c->table_rV[V];\
495
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
496
    b = (type *)c->table_bU[U];\
497

    
498
#define YSCALE_YUV_2_ANYRGB_C(func, func2)\
499
    switch(c->dstFormat)\
500
    {\
501
    case PIX_FMT_RGB32:\
502
    case PIX_FMT_BGR32:\
503
    case PIX_FMT_RGB32_1:\
504
    case PIX_FMT_BGR32_1:\
505
        func(uint32_t)\
506
            ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
507
            ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
508
        }                \
509
        break;\
510
    case PIX_FMT_RGB24:\
511
        func(uint8_t)\
512
            ((uint8_t*)dest)[0]= r[Y1];\
513
            ((uint8_t*)dest)[1]= g[Y1];\
514
            ((uint8_t*)dest)[2]= b[Y1];\
515
            ((uint8_t*)dest)[3]= r[Y2];\
516
            ((uint8_t*)dest)[4]= g[Y2];\
517
            ((uint8_t*)dest)[5]= b[Y2];\
518
            dest+=6;\
519
        }\
520
        break;\
521
    case PIX_FMT_BGR24:\
522
        func(uint8_t)\
523
            ((uint8_t*)dest)[0]= b[Y1];\
524
            ((uint8_t*)dest)[1]= g[Y1];\
525
            ((uint8_t*)dest)[2]= r[Y1];\
526
            ((uint8_t*)dest)[3]= b[Y2];\
527
            ((uint8_t*)dest)[4]= g[Y2];\
528
            ((uint8_t*)dest)[5]= r[Y2];\
529
            dest+=6;\
530
        }\
531
        break;\
532
    case PIX_FMT_RGB565:\
533
    case PIX_FMT_BGR565:\
534
        {\
535
            const int dr1= dither_2x2_8[y&1    ][0];\
536
            const int dg1= dither_2x2_4[y&1    ][0];\
537
            const int db1= dither_2x2_8[(y&1)^1][0];\
538
            const int dr2= dither_2x2_8[y&1    ][1];\
539
            const int dg2= dither_2x2_4[y&1    ][1];\
540
            const int db2= dither_2x2_8[(y&1)^1][1];\
541
            func(uint16_t)\
542
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
543
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
544
            }\
545
        }\
546
        break;\
547
    case PIX_FMT_RGB555:\
548
    case PIX_FMT_BGR555:\
549
        {\
550
            const int dr1= dither_2x2_8[y&1    ][0];\
551
            const int dg1= dither_2x2_8[y&1    ][1];\
552
            const int db1= dither_2x2_8[(y&1)^1][0];\
553
            const int dr2= dither_2x2_8[y&1    ][1];\
554
            const int dg2= dither_2x2_8[y&1    ][0];\
555
            const int db2= dither_2x2_8[(y&1)^1][1];\
556
            func(uint16_t)\
557
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
558
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
559
            }\
560
        }\
561
        break;\
562
    case PIX_FMT_RGB8:\
563
    case PIX_FMT_BGR8:\
564
        {\
565
            const uint8_t * const d64= dither_8x8_73[y&7];\
566
            const uint8_t * const d32= dither_8x8_32[y&7];\
567
            func(uint8_t)\
568
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
569
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
570
            }\
571
        }\
572
        break;\
573
    case PIX_FMT_RGB4:\
574
    case PIX_FMT_BGR4:\
575
        {\
576
            const uint8_t * const d64= dither_8x8_73 [y&7];\
577
            const uint8_t * const d128=dither_8x8_220[y&7];\
578
            func(uint8_t)\
579
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
580
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
581
            }\
582
        }\
583
        break;\
584
    case PIX_FMT_RGB4_BYTE:\
585
    case PIX_FMT_BGR4_BYTE:\
586
        {\
587
            const uint8_t * const d64= dither_8x8_73 [y&7];\
588
            const uint8_t * const d128=dither_8x8_220[y&7];\
589
            func(uint8_t)\
590
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
591
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
592
            }\
593
        }\
594
        break;\
595
    case PIX_FMT_MONOBLACK:\
596
        {\
597
            const uint8_t * const d128=dither_8x8_220[y&7];\
598
            uint8_t *g= c->table_gU[128] + c->table_gV[128];\
599
            for (i=0; i<dstW-7; i+=8){\
600
                int acc;\
601
                acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
602
                acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
603
                acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
604
                acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
605
                acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
606
                acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
607
                acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
608
                acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
609
                ((uint8_t*)dest)[0]= acc;\
610
                dest++;\
611
            }\
612
\
613
/*\
614
((uint8_t*)dest)-= dstW>>4;\
615
{\
616
            int acc=0;\
617
            int left=0;\
618
            static int top[1024];\
619
            static int last_new[1024][1024];\
620
            static int last_in3[1024][1024];\
621
            static int drift[1024][1024];\
622
            int topLeft=0;\
623
            int shift=0;\
624
            int count=0;\
625
            const uint8_t * const d128=dither_8x8_220[y&7];\
626
            int error_new=0;\
627
            int error_in3=0;\
628
            int f=0;\
629
            \
630
            for (i=dstW>>1; i<dstW; i++){\
631
                int in= ((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19);\
632
                int in2 = (76309 * (in - 16) + 32768) >> 16;\
633
                int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
634
                int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
635
                         + (last_new[y][i] - in3)*f/256;\
636
                int new= old> 128 ? 255 : 0;\
637
\
638
                error_new+= FFABS(last_new[y][i] - new);\
639
                error_in3+= FFABS(last_in3[y][i] - in3);\
640
                f= error_new - error_in3*4;\
641
                if (f<0) f=0;\
642
                if (f>256) f=256;\
643
\
644
                topLeft= top[i];\
645
                left= top[i]= old - new;\
646
                last_new[y][i]= new;\
647
                last_in3[y][i]= in3;\
648
\
649
                acc+= acc + (new&1);\
650
                if ((i&7)==6){\
651
                    ((uint8_t*)dest)[0]= acc;\
652
                    ((uint8_t*)dest)++;\
653
                }\
654
            }\
655
}\
656
*/\
657
        }\
658
        break;\
659
    case PIX_FMT_YUYV422:\
660
        func2\
661
            ((uint8_t*)dest)[2*i2+0]= Y1;\
662
            ((uint8_t*)dest)[2*i2+1]= U;\
663
            ((uint8_t*)dest)[2*i2+2]= Y2;\
664
            ((uint8_t*)dest)[2*i2+3]= V;\
665
        }                \
666
        break;\
667
    case PIX_FMT_UYVY422:\
668
        func2\
669
            ((uint8_t*)dest)[2*i2+0]= U;\
670
            ((uint8_t*)dest)[2*i2+1]= Y1;\
671
            ((uint8_t*)dest)[2*i2+2]= V;\
672
            ((uint8_t*)dest)[2*i2+3]= Y2;\
673
        }                \
674
        break;\
675
    }\
676

    
677

    
678
static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
679
                                  int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
680
                                  uint8_t *dest, int dstW, int y)
681
{
682
    int i;
683
    switch(c->dstFormat)
684
    {
685
    case PIX_FMT_BGR32:
686
    case PIX_FMT_RGB32:
687
    case PIX_FMT_BGR32_1:
688
    case PIX_FMT_RGB32_1:
689
        YSCALE_YUV_2_RGBX_C(uint32_t)
690
            ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
691
            ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
692
        }
693
        break;
694
    case PIX_FMT_RGB24:
695
        YSCALE_YUV_2_RGBX_C(uint8_t)
696
            ((uint8_t*)dest)[0]= r[Y1];
697
            ((uint8_t*)dest)[1]= g[Y1];
698
            ((uint8_t*)dest)[2]= b[Y1];
699
            ((uint8_t*)dest)[3]= r[Y2];
700
            ((uint8_t*)dest)[4]= g[Y2];
701
            ((uint8_t*)dest)[5]= b[Y2];
702
            dest+=6;
703
        }
704
        break;
705
    case PIX_FMT_BGR24:
706
        YSCALE_YUV_2_RGBX_C(uint8_t)
707
            ((uint8_t*)dest)[0]= b[Y1];
708
            ((uint8_t*)dest)[1]= g[Y1];
709
            ((uint8_t*)dest)[2]= r[Y1];
710
            ((uint8_t*)dest)[3]= b[Y2];
711
            ((uint8_t*)dest)[4]= g[Y2];
712
            ((uint8_t*)dest)[5]= r[Y2];
713
            dest+=6;
714
        }
715
        break;
716
    case PIX_FMT_RGB565:
717
    case PIX_FMT_BGR565:
718
        {
719
            const int dr1= dither_2x2_8[y&1    ][0];
720
            const int dg1= dither_2x2_4[y&1    ][0];
721
            const int db1= dither_2x2_8[(y&1)^1][0];
722
            const int dr2= dither_2x2_8[y&1    ][1];
723
            const int dg2= dither_2x2_4[y&1    ][1];
724
            const int db2= dither_2x2_8[(y&1)^1][1];
725
            YSCALE_YUV_2_RGBX_C(uint16_t)
726
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
727
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
728
            }
729
        }
730
        break;
731
    case PIX_FMT_RGB555:
732
    case PIX_FMT_BGR555:
733
        {
734
            const int dr1= dither_2x2_8[y&1    ][0];
735
            const int dg1= dither_2x2_8[y&1    ][1];
736
            const int db1= dither_2x2_8[(y&1)^1][0];
737
            const int dr2= dither_2x2_8[y&1    ][1];
738
            const int dg2= dither_2x2_8[y&1    ][0];
739
            const int db2= dither_2x2_8[(y&1)^1][1];
740
            YSCALE_YUV_2_RGBX_C(uint16_t)
741
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
742
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
743
            }
744
        }
745
        break;
746
    case PIX_FMT_RGB8:
747
    case PIX_FMT_BGR8:
748
        {
749
            const uint8_t * const d64= dither_8x8_73[y&7];
750
            const uint8_t * const d32= dither_8x8_32[y&7];
751
            YSCALE_YUV_2_RGBX_C(uint8_t)
752
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
753
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
754
            }
755
        }
756
        break;
757
    case PIX_FMT_RGB4:
758
    case PIX_FMT_BGR4:
759
        {
760
            const uint8_t * const d64= dither_8x8_73 [y&7];
761
            const uint8_t * const d128=dither_8x8_220[y&7];
762
            YSCALE_YUV_2_RGBX_C(uint8_t)
763
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
764
                                  +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
765
            }
766
        }
767
        break;
768
    case PIX_FMT_RGB4_BYTE:
769
    case PIX_FMT_BGR4_BYTE:
770
        {
771
            const uint8_t * const d64= dither_8x8_73 [y&7];
772
            const uint8_t * const d128=dither_8x8_220[y&7];
773
            YSCALE_YUV_2_RGBX_C(uint8_t)
774
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
775
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
776
            }
777
        }
778
        break;
779
    case PIX_FMT_MONOBLACK:
780
        {
781
            const uint8_t * const d128=dither_8x8_220[y&7];
782
            uint8_t *g= c->table_gU[128] + c->table_gV[128];
783
            int acc=0;
784
            for (i=0; i<dstW-1; i+=2){
785
                int j;
786
                int Y1=1<<18;
787
                int Y2=1<<18;
788

    
789
                for (j=0; j<lumFilterSize; j++)
790
                {
791
                    Y1 += lumSrc[j][i] * lumFilter[j];
792
                    Y2 += lumSrc[j][i+1] * lumFilter[j];
793
                }
794
                Y1>>=19;
795
                Y2>>=19;
796
                if ((Y1|Y2)&256)
797
                {
798
                    if (Y1>255)   Y1=255;
799
                    else if (Y1<0)Y1=0;
800
                    if (Y2>255)   Y2=255;
801
                    else if (Y2<0)Y2=0;
802
                }
803
                acc+= acc + g[Y1+d128[(i+0)&7]];
804
                acc+= acc + g[Y2+d128[(i+1)&7]];
805
                if ((i&7)==6){
806
                    ((uint8_t*)dest)[0]= acc;
807
                    dest++;
808
                }
809
            }
810
        }
811
        break;
812
    case PIX_FMT_YUYV422:
813
        YSCALE_YUV_2_PACKEDX_C(void)
814
            ((uint8_t*)dest)[2*i2+0]= Y1;
815
            ((uint8_t*)dest)[2*i2+1]= U;
816
            ((uint8_t*)dest)[2*i2+2]= Y2;
817
            ((uint8_t*)dest)[2*i2+3]= V;
818
        }
819
        break;
820
    case PIX_FMT_UYVY422:
821
        YSCALE_YUV_2_PACKEDX_C(void)
822
            ((uint8_t*)dest)[2*i2+0]= U;
823
            ((uint8_t*)dest)[2*i2+1]= Y1;
824
            ((uint8_t*)dest)[2*i2+2]= V;
825
            ((uint8_t*)dest)[2*i2+3]= Y2;
826
        }
827
        break;
828
    }
829
}
830

    
831

    
832
//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
833
//Plain C versions
834
#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
835
#define COMPILE_C
836
#endif
837

    
838
#ifdef ARCH_POWERPC
839
#if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
840
#define COMPILE_ALTIVEC
841
#endif //HAVE_ALTIVEC
842
#endif //ARCH_POWERPC
843

    
844
#if defined(ARCH_X86)
845

    
846
#if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
847
#define COMPILE_MMX
848
#endif
849

    
850
#if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
851
#define COMPILE_MMX2
852
#endif
853

    
854
#if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
855
#define COMPILE_3DNOW
856
#endif
857
#endif //ARCH_X86 || ARCH_X86_64
858

    
859
#undef HAVE_MMX
860
#undef HAVE_MMX2
861
#undef HAVE_3DNOW
862

    
863
#ifdef COMPILE_C
864
#undef HAVE_MMX
865
#undef HAVE_MMX2
866
#undef HAVE_3DNOW
867
#undef HAVE_ALTIVEC
868
#define RENAME(a) a ## _C
869
#include "swscale_template.c"
870
#endif
871

    
872
#ifdef COMPILE_ALTIVEC
873
#undef RENAME
874
#define HAVE_ALTIVEC
875
#define RENAME(a) a ## _altivec
876
#include "swscale_template.c"
877
#endif
878

    
879
#if defined(ARCH_X86)
880

    
881
//X86 versions
882
/*
883
#undef RENAME
884
#undef HAVE_MMX
885
#undef HAVE_MMX2
886
#undef HAVE_3DNOW
887
#define ARCH_X86
888
#define RENAME(a) a ## _X86
889
#include "swscale_template.c"
890
*/
891
//MMX versions
892
#ifdef COMPILE_MMX
893
#undef RENAME
894
#define HAVE_MMX
895
#undef HAVE_MMX2
896
#undef HAVE_3DNOW
897
#define RENAME(a) a ## _MMX
898
#include "swscale_template.c"
899
#endif
900

    
901
//MMX2 versions
902
#ifdef COMPILE_MMX2
903
#undef RENAME
904
#define HAVE_MMX
905
#define HAVE_MMX2
906
#undef HAVE_3DNOW
907
#define RENAME(a) a ## _MMX2
908
#include "swscale_template.c"
909
#endif
910

    
911
//3DNOW versions
912
#ifdef COMPILE_3DNOW
913
#undef RENAME
914
#define HAVE_MMX
915
#undef HAVE_MMX2
916
#define HAVE_3DNOW
917
#define RENAME(a) a ## _3DNow
918
#include "swscale_template.c"
919
#endif
920

    
921
#endif //ARCH_X86 || ARCH_X86_64
922

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

    
925
static double getSplineCoeff(double a, double b, double c, double d, double dist)
926
{
927
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
928
    if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
929
    else                return getSplineCoeff(        0.0,
930
                                             b+ 2.0*c + 3.0*d,
931
                                                    c + 3.0*d,
932
                                            -b- 3.0*c - 6.0*d,
933
                                            dist-1.0);
934
}
935

    
936
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
937
                             int srcW, int dstW, int filterAlign, int one, int flags,
938
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
939
{
940
    int i;
941
    int filterSize;
942
    int filter2Size;
943
    int minFilterSize;
944
    double *filter=NULL;
945
    double *filter2=NULL;
946
    int ret= -1;
947
#if defined(ARCH_X86)
948
    if (flags & SWS_CPU_CAPS_MMX)
949
        asm volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
950
#endif
951

    
952
    // Note the +1 is for the MMXscaler which reads over the end
953
    *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
954

    
955
    if (FFABS(xInc - 0x10000) <10) // unscaled
956
    {
957
        int i;
958
        filterSize= 1;
959
        filter= av_malloc(dstW*sizeof(double)*filterSize);
960
        for (i=0; i<dstW*filterSize; i++) filter[i]=0;
961

    
962
        for (i=0; i<dstW; i++)
963
        {
964
            filter[i*filterSize]=1;
965
            (*filterPos)[i]=i;
966
        }
967

    
968
    }
969
    else if (flags&SWS_POINT) // lame looking point sampling mode
970
    {
971
        int i;
972
        int xDstInSrc;
973
        filterSize= 1;
974
        filter= av_malloc(dstW*sizeof(double)*filterSize);
975

    
976
        xDstInSrc= xInc/2 - 0x8000;
977
        for (i=0; i<dstW; i++)
978
        {
979
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
980

    
981
            (*filterPos)[i]= xx;
982
            filter[i]= 1.0;
983
            xDstInSrc+= xInc;
984
        }
985
    }
986
    else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
987
    {
988
        int i;
989
        int xDstInSrc;
990
        if      (flags&SWS_BICUBIC) filterSize= 4;
991
        else if (flags&SWS_X      ) filterSize= 4;
992
        else                        filterSize= 2; // SWS_BILINEAR / SWS_AREA
993
        filter= av_malloc(dstW*sizeof(double)*filterSize);
994

    
995
        xDstInSrc= xInc/2 - 0x8000;
996
        for (i=0; i<dstW; i++)
997
        {
998
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
999
            int j;
1000

    
1001
            (*filterPos)[i]= xx;
1002
                //Bilinear upscale / linear interpolate / Area averaging
1003
                for (j=0; j<filterSize; j++)
1004
                {
1005
                    double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1006
                    double coeff= 1.0 - d;
1007
                    if (coeff<0) coeff=0;
1008
                    filter[i*filterSize + j]= coeff;
1009
                    xx++;
1010
                }
1011
            xDstInSrc+= xInc;
1012
        }
1013
    }
1014
    else
1015
    {
1016
        double xDstInSrc;
1017
        double sizeFactor, filterSizeInSrc;
1018
        const double xInc1= (double)xInc / (double)(1<<16);
1019

    
1020
        if      (flags&SWS_BICUBIC)      sizeFactor=  4.0;
1021
        else if (flags&SWS_X)            sizeFactor=  8.0;
1022
        else if (flags&SWS_AREA)         sizeFactor=  1.0; //downscale only, for upscale it is bilinear
1023
        else if (flags&SWS_GAUSS)        sizeFactor=  8.0;   // infinite ;)
1024
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
1025
        else if (flags&SWS_SINC)         sizeFactor= 20.0; // infinite ;)
1026
        else if (flags&SWS_SPLINE)       sizeFactor= 20.0;  // infinite ;)
1027
        else if (flags&SWS_BILINEAR)     sizeFactor=  2.0;
1028
        else {
1029
            sizeFactor= 0.0; //GCC warning killer
1030
            assert(0);
1031
        }
1032

    
1033
        if (xInc1 <= 1.0)       filterSizeInSrc= sizeFactor; // upscale
1034
        else                    filterSizeInSrc= sizeFactor*srcW / (double)dstW;
1035

    
1036
        filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1037
        if (filterSize > srcW-2) filterSize=srcW-2;
1038

    
1039
        filter= av_malloc(dstW*sizeof(double)*filterSize);
1040

    
1041
        xDstInSrc= xInc1 / 2.0 - 0.5;
1042
        for (i=0; i<dstW; i++)
1043
        {
1044
            int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
1045
            int j;
1046
            (*filterPos)[i]= xx;
1047
            for (j=0; j<filterSize; j++)
1048
            {
1049
                double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
1050
                double coeff;
1051
                if (flags & SWS_BICUBIC)
1052
                {
1053
                    double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
1054
                    double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
1055

    
1056
                    if (d<1.0)
1057
                        coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
1058
                    else if (d<2.0)
1059
                        coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
1060
                    else
1061
                        coeff=0.0;
1062
                }
1063
/*                else if (flags & SWS_X)
1064
                {
1065
                    double p= param ? param*0.01 : 0.3;
1066
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1067
                    coeff*= pow(2.0, - p*d*d);
1068
                }*/
1069
                else if (flags & SWS_X)
1070
                {
1071
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1072

    
1073
                    if (d<1.0)
1074
                        coeff = cos(d*PI);
1075
                    else
1076
                        coeff=-1.0;
1077
                    if (coeff<0.0)      coeff= -pow(-coeff, A);
1078
                    else                coeff=  pow( coeff, A);
1079
                    coeff= coeff*0.5 + 0.5;
1080
                }
1081
                else if (flags & SWS_AREA)
1082
                {
1083
                    double srcPixelSize= 1.0/xInc1;
1084
                    if      (d + srcPixelSize/2 < 0.5) coeff= 1.0;
1085
                    else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1086
                    else coeff=0.0;
1087
                }
1088
                else if (flags & SWS_GAUSS)
1089
                {
1090
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1091
                    coeff = pow(2.0, - p*d*d);
1092
                }
1093
                else if (flags & SWS_SINC)
1094
                {
1095
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1096
                }
1097
                else if (flags & SWS_LANCZOS)
1098
                {
1099
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1100
                    coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1101
                    if (d>p) coeff=0;
1102
                }
1103
                else if (flags & SWS_BILINEAR)
1104
                {
1105
                    coeff= 1.0 - d;
1106
                    if (coeff<0) coeff=0;
1107
                }
1108
                else if (flags & SWS_SPLINE)
1109
                {
1110
                    double p=-2.196152422706632;
1111
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1112
                }
1113
                else {
1114
                    coeff= 0.0; //GCC warning killer
1115
                    assert(0);
1116
                }
1117

    
1118
                filter[i*filterSize + j]= coeff;
1119
                xx++;
1120
            }
1121
            xDstInSrc+= xInc1;
1122
        }
1123
    }
1124

    
1125
    /* apply src & dst Filter to filter -> filter2
1126
       av_free(filter);
1127
    */
1128
    assert(filterSize>0);
1129
    filter2Size= filterSize;
1130
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1131
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1132
    assert(filter2Size>0);
1133
    filter2= av_malloc(filter2Size*dstW*sizeof(double));
1134

    
1135
    for (i=0; i<dstW; i++)
1136
    {
1137
        int j;
1138
        SwsVector scaleFilter;
1139
        SwsVector *outVec;
1140

    
1141
        scaleFilter.coeff= filter + i*filterSize;
1142
        scaleFilter.length= filterSize;
1143

    
1144
        if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1145
        else           outVec= &scaleFilter;
1146

    
1147
        assert(outVec->length == filter2Size);
1148
        //FIXME dstFilter
1149

    
1150
        for (j=0; j<outVec->length; j++)
1151
        {
1152
            filter2[i*filter2Size + j]= outVec->coeff[j];
1153
        }
1154

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

    
1157
        if (outVec != &scaleFilter) sws_freeVec(outVec);
1158
    }
1159
    av_freep(&filter);
1160

    
1161
    /* try to reduce the filter-size (step1 find size and shift left) */
1162
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1163
    minFilterSize= 0;
1164
    for (i=dstW-1; i>=0; i--)
1165
    {
1166
        int min= filter2Size;
1167
        int j;
1168
        double cutOff=0.0;
1169

    
1170
        /* get rid off near zero elements on the left by shifting left */
1171
        for (j=0; j<filter2Size; j++)
1172
        {
1173
            int k;
1174
            cutOff += FFABS(filter2[i*filter2Size]);
1175

    
1176
            if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1177

    
1178
            /* preserve monotonicity because the core can't handle the filter otherwise */
1179
            if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1180

    
1181
            // Move filter coeffs left
1182
            for (k=1; k<filter2Size; k++)
1183
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1184
            filter2[i*filter2Size + k - 1]= 0.0;
1185
            (*filterPos)[i]++;
1186
        }
1187

    
1188
        cutOff=0.0;
1189
        /* count near zeros on the right */
1190
        for (j=filter2Size-1; j>0; j--)
1191
        {
1192
            cutOff += FFABS(filter2[i*filter2Size + j]);
1193

    
1194
            if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1195
            min--;
1196
        }
1197

    
1198
        if (min>minFilterSize) minFilterSize= min;
1199
    }
1200

    
1201
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1202
        // we can handle the special case 4,
1203
        // so we don't want to go to the full 8
1204
        if (minFilterSize < 5)
1205
            filterAlign = 4;
1206

    
1207
        // we really don't want to waste our time
1208
        // doing useless computation, so fall-back on
1209
        // the scalar C code for very small filter.
1210
        // vectorizing is worth it only if you have
1211
        // decent-sized vector.
1212
        if (minFilterSize < 3)
1213
            filterAlign = 1;
1214
    }
1215

    
1216
    if (flags & SWS_CPU_CAPS_MMX) {
1217
        // special case for unscaled vertical filtering
1218
        if (minFilterSize == 1 && filterAlign == 2)
1219
            filterAlign= 1;
1220
    }
1221

    
1222
    assert(minFilterSize > 0);
1223
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1224
    assert(filterSize > 0);
1225
    filter= av_malloc(filterSize*dstW*sizeof(double));
1226
    if (filterSize >= MAX_FILTER_SIZE || !filter)
1227
        goto error;
1228
    *outFilterSize= filterSize;
1229

    
1230
    if (flags&SWS_PRINT_INFO)
1231
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1232
    /* try to reduce the filter-size (step2 reduce it) */
1233
    for (i=0; i<dstW; i++)
1234
    {
1235
        int j;
1236

    
1237
        for (j=0; j<filterSize; j++)
1238
        {
1239
            if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
1240
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1241
        }
1242
    }
1243

    
1244

    
1245
    //FIXME try to align filterpos if possible
1246

    
1247
    //fix borders
1248
    for (i=0; i<dstW; i++)
1249
    {
1250
        int j;
1251
        if ((*filterPos)[i] < 0)
1252
        {
1253
            // Move filter coeffs left to compensate for filterPos
1254
            for (j=1; j<filterSize; j++)
1255
            {
1256
                int left= FFMAX(j + (*filterPos)[i], 0);
1257
                filter[i*filterSize + left] += filter[i*filterSize + j];
1258
                filter[i*filterSize + j]=0;
1259
            }
1260
            (*filterPos)[i]= 0;
1261
        }
1262

    
1263
        if ((*filterPos)[i] + filterSize > srcW)
1264
        {
1265
            int shift= (*filterPos)[i] + filterSize - srcW;
1266
            // Move filter coeffs right to compensate for filterPos
1267
            for (j=filterSize-2; j>=0; j--)
1268
            {
1269
                int right= FFMIN(j + shift, filterSize-1);
1270
                filter[i*filterSize +right] += filter[i*filterSize +j];
1271
                filter[i*filterSize +j]=0;
1272
            }
1273
            (*filterPos)[i]= srcW - filterSize;
1274
        }
1275
    }
1276

    
1277
    // Note the +1 is for the MMXscaler which reads over the end
1278
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1279
    *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1280

    
1281
    /* Normalize & Store in outFilter */
1282
    for (i=0; i<dstW; i++)
1283
    {
1284
        int j;
1285
        double error=0;
1286
        double sum=0;
1287
        double scale= one;
1288

    
1289
        for (j=0; j<filterSize; j++)
1290
        {
1291
            sum+= filter[i*filterSize + j];
1292
        }
1293
        scale/= sum;
1294
        for (j=0; j<*outFilterSize; j++)
1295
        {
1296
            double v= filter[i*filterSize + j]*scale + error;
1297
            int intV= floor(v + 0.5);
1298
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1299
            error = v - intV;
1300
        }
1301
    }
1302

    
1303
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1304
    for (i=0; i<*outFilterSize; i++)
1305
    {
1306
        int j= dstW*(*outFilterSize);
1307
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1308
    }
1309

    
1310
    ret=0;
1311
error:
1312
    av_free(filter);
1313
    av_free(filter2);
1314
    return ret;
1315
}
1316

    
1317
#ifdef COMPILE_MMX2
1318
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1319
{
1320
    uint8_t *fragmentA;
1321
    long imm8OfPShufW1A;
1322
    long imm8OfPShufW2A;
1323
    long fragmentLengthA;
1324
    uint8_t *fragmentB;
1325
    long imm8OfPShufW1B;
1326
    long imm8OfPShufW2B;
1327
    long fragmentLengthB;
1328
    int fragmentPos;
1329

    
1330
    int xpos, i;
1331

    
1332
    // create an optimized horizontal scaling routine
1333

    
1334
    //code fragment
1335

    
1336
    asm volatile(
1337
        "jmp                         9f                 \n\t"
1338
    // Begin
1339
        "0:                                             \n\t"
1340
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1341
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1342
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1343
        "punpcklbw                %%mm7, %%mm1          \n\t"
1344
        "punpcklbw                %%mm7, %%mm0          \n\t"
1345
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1346
        "1:                                             \n\t"
1347
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1348
        "2:                                             \n\t"
1349
        "psubw                    %%mm1, %%mm0          \n\t"
1350
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1351
        "pmullw                   %%mm3, %%mm0          \n\t"
1352
        "psllw                       $7, %%mm1          \n\t"
1353
        "paddw                    %%mm1, %%mm0          \n\t"
1354

    
1355
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1356

    
1357
        "add                         $8, %%"REG_a"      \n\t"
1358
    // End
1359
        "9:                                             \n\t"
1360
//        "int $3                                         \n\t"
1361
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1362
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1363
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1364
        "dec                         %1                 \n\t"
1365
        "dec                         %2                 \n\t"
1366
        "sub                         %0, %1             \n\t"
1367
        "sub                         %0, %2             \n\t"
1368
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1369
        "sub                         %0, %3             \n\t"
1370

    
1371

    
1372
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1373
        "=r" (fragmentLengthA)
1374
    );
1375

    
1376
    asm volatile(
1377
        "jmp                         9f                 \n\t"
1378
    // Begin
1379
        "0:                                             \n\t"
1380
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1381
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1382
        "punpcklbw                %%mm7, %%mm0          \n\t"
1383
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1384
        "1:                                             \n\t"
1385
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1386
        "2:                                             \n\t"
1387
        "psubw                    %%mm1, %%mm0          \n\t"
1388
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1389
        "pmullw                   %%mm3, %%mm0          \n\t"
1390
        "psllw                       $7, %%mm1          \n\t"
1391
        "paddw                    %%mm1, %%mm0          \n\t"
1392

    
1393
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1394

    
1395
        "add                         $8, %%"REG_a"      \n\t"
1396
    // End
1397
        "9:                                             \n\t"
1398
//        "int                       $3                   \n\t"
1399
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1400
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1401
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1402
        "dec                         %1                 \n\t"
1403
        "dec                         %2                 \n\t"
1404
        "sub                         %0, %1             \n\t"
1405
        "sub                         %0, %2             \n\t"
1406
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1407
        "sub                         %0, %3             \n\t"
1408

    
1409

    
1410
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1411
        "=r" (fragmentLengthB)
1412
    );
1413

    
1414
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1415
    fragmentPos=0;
1416

    
1417
    for (i=0; i<dstW/numSplits; i++)
1418
    {
1419
        int xx=xpos>>16;
1420

    
1421
        if ((i&3) == 0)
1422
        {
1423
            int a=0;
1424
            int b=((xpos+xInc)>>16) - xx;
1425
            int c=((xpos+xInc*2)>>16) - xx;
1426
            int d=((xpos+xInc*3)>>16) - xx;
1427

    
1428
            filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1429
            filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1430
            filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1431
            filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1432
            filterPos[i/2]= xx;
1433

    
1434
            if (d+1<4)
1435
            {
1436
                int maxShift= 3-(d+1);
1437
                int shift=0;
1438

    
1439
                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1440

    
1441
                funnyCode[fragmentPos + imm8OfPShufW1B]=
1442
                    (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1443
                funnyCode[fragmentPos + imm8OfPShufW2B]=
1444
                    a | (b<<2) | (c<<4) | (d<<6);
1445

    
1446
                if (i+3>=dstW) shift=maxShift; //avoid overread
1447
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1448

    
1449
                if (shift && i>=shift)
1450
                {
1451
                    funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1452
                    funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1453
                    filterPos[i/2]-=shift;
1454
                }
1455

    
1456
                fragmentPos+= fragmentLengthB;
1457
            }
1458
            else
1459
            {
1460
                int maxShift= 3-d;
1461
                int shift=0;
1462

    
1463
                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1464

    
1465
                funnyCode[fragmentPos + imm8OfPShufW1A]=
1466
                funnyCode[fragmentPos + imm8OfPShufW2A]=
1467
                    a | (b<<2) | (c<<4) | (d<<6);
1468

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

    
1472
                if (shift && i>=shift)
1473
                {
1474
                    funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1475
                    funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1476
                    filterPos[i/2]-=shift;
1477
                }
1478

    
1479
                fragmentPos+= fragmentLengthA;
1480
            }
1481

    
1482
            funnyCode[fragmentPos]= RET;
1483
        }
1484
        xpos+=xInc;
1485
    }
1486
    filterPos[i/2]= xpos>>16; // needed to jump to the next part
1487
}
1488
#endif /* COMPILE_MMX2 */
1489

    
1490
static void globalInit(void){
1491
    // generating tables:
1492
    int i;
1493
    for (i=0; i<768; i++){
1494
        int c= av_clip_uint8(i-256);
1495
        clip_table[i]=c;
1496
    }
1497
}
1498

    
1499
static SwsFunc getSwsFunc(int flags){
1500

    
1501
#if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1502
#if defined(ARCH_X86)
1503
    // ordered per speed fastest first
1504
    if (flags & SWS_CPU_CAPS_MMX2)
1505
        return swScale_MMX2;
1506
    else if (flags & SWS_CPU_CAPS_3DNOW)
1507
        return swScale_3DNow;
1508
    else if (flags & SWS_CPU_CAPS_MMX)
1509
        return swScale_MMX;
1510
    else
1511
        return swScale_C;
1512

    
1513
#else
1514
#ifdef ARCH_POWERPC
1515
    if (flags & SWS_CPU_CAPS_ALTIVEC)
1516
        return swScale_altivec;
1517
    else
1518
        return swScale_C;
1519
#endif
1520
    return swScale_C;
1521
#endif /* defined(ARCH_X86) */
1522
#else //RUNTIME_CPUDETECT
1523
#ifdef HAVE_MMX2
1524
    return swScale_MMX2;
1525
#elif defined (HAVE_3DNOW)
1526
    return swScale_3DNow;
1527
#elif defined (HAVE_MMX)
1528
    return swScale_MMX;
1529
#elif defined (HAVE_ALTIVEC)
1530
    return swScale_altivec;
1531
#else
1532
    return swScale_C;
1533
#endif
1534
#endif //!RUNTIME_CPUDETECT
1535
}
1536

    
1537
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1538
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1539
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1540
    /* Copy Y plane */
1541
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1542
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1543
    else
1544
    {
1545
        int i;
1546
        uint8_t *srcPtr= src[0];
1547
        uint8_t *dstPtr= dst;
1548
        for (i=0; i<srcSliceH; i++)
1549
        {
1550
            memcpy(dstPtr, srcPtr, c->srcW);
1551
            srcPtr+= srcStride[0];
1552
            dstPtr+= dstStride[0];
1553
        }
1554
    }
1555
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1556
    if (c->dstFormat == PIX_FMT_NV12)
1557
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1558
    else
1559
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1560

    
1561
    return srcSliceH;
1562
}
1563

    
1564
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1565
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1566
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1567

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

    
1570
    return srcSliceH;
1571
}
1572

    
1573
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1574
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1575
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1576

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

    
1579
    return srcSliceH;
1580
}
1581

    
1582
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1583
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1584
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1585
    const int srcFormat= c->srcFormat;
1586
    const int dstFormat= c->dstFormat;
1587
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1588
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1589
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1590
    const int dstId= fmt_depth(dstFormat) >> 2;
1591
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1592

    
1593
    /* BGR -> BGR */
1594
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
1595
       || (isRGB(srcFormat) && isRGB(dstFormat))){
1596
        switch(srcId | (dstId<<4)){
1597
        case 0x34: conv= rgb16to15; break;
1598
        case 0x36: conv= rgb24to15; break;
1599
        case 0x38: conv= rgb32to15; break;
1600
        case 0x43: conv= rgb15to16; break;
1601
        case 0x46: conv= rgb24to16; break;
1602
        case 0x48: conv= rgb32to16; break;
1603
        case 0x63: conv= rgb15to24; break;
1604
        case 0x64: conv= rgb16to24; break;
1605
        case 0x68: conv= rgb32to24; break;
1606
        case 0x83: conv= rgb15to32; break;
1607
        case 0x84: conv= rgb16to32; break;
1608
        case 0x86: conv= rgb24to32; break;
1609
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1610
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1611
        }
1612
    }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
1613
             || (isRGB(srcFormat) && isBGR(dstFormat))){
1614
        switch(srcId | (dstId<<4)){
1615
        case 0x33: conv= rgb15tobgr15; break;
1616
        case 0x34: conv= rgb16tobgr15; break;
1617
        case 0x36: conv= rgb24tobgr15; break;
1618
        case 0x38: conv= rgb32tobgr15; break;
1619
        case 0x43: conv= rgb15tobgr16; break;
1620
        case 0x44: conv= rgb16tobgr16; break;
1621
        case 0x46: conv= rgb24tobgr16; break;
1622
        case 0x48: conv= rgb32tobgr16; break;
1623
        case 0x63: conv= rgb15tobgr24; break;
1624
        case 0x64: conv= rgb16tobgr24; break;
1625
        case 0x66: conv= rgb24tobgr24; break;
1626
        case 0x68: conv= rgb32tobgr24; break;
1627
        case 0x83: conv= rgb15tobgr32; break;
1628
        case 0x84: conv= rgb16tobgr32; break;
1629
        case 0x86: conv= rgb24tobgr32; break;
1630
        case 0x88: conv= rgb32tobgr32; break;
1631
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1632
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1633
        }
1634
    }else{
1635
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1636
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1637
    }
1638

    
1639
    if(conv)
1640
    {
1641
        uint8_t *srcPtr= src[0];
1642
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1643
            srcPtr += ALT32_CORR;
1644

    
1645
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1646
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1647
        else
1648
        {
1649
            int i;
1650
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1651

    
1652
            for (i=0; i<srcSliceH; i++)
1653
            {
1654
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
1655
                srcPtr+= srcStride[0];
1656
                dstPtr+= dstStride[0];
1657
            }
1658
        }
1659
    }
1660
    return srcSliceH;
1661
}
1662

    
1663
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1664
                              int srcSliceH, uint8_t* dst[], int dstStride[]){
1665

    
1666
    rgb24toyv12(
1667
        src[0],
1668
        dst[0]+ srcSliceY    *dstStride[0],
1669
        dst[1]+(srcSliceY>>1)*dstStride[1],
1670
        dst[2]+(srcSliceY>>1)*dstStride[2],
1671
        c->srcW, srcSliceH,
1672
        dstStride[0], dstStride[1], srcStride[0]);
1673
    return srcSliceH;
1674
}
1675

    
1676
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1677
                             int srcSliceH, uint8_t* dst[], int dstStride[]){
1678
    int i;
1679

    
1680
    /* copy Y */
1681
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1682
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1683
    else{
1684
        uint8_t *srcPtr= src[0];
1685
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1686

    
1687
        for (i=0; i<srcSliceH; i++)
1688
        {
1689
            memcpy(dstPtr, srcPtr, c->srcW);
1690
            srcPtr+= srcStride[0];
1691
            dstPtr+= dstStride[0];
1692
        }
1693
    }
1694

    
1695
    if (c->dstFormat==PIX_FMT_YUV420P){
1696
        planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1697
        planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1698
    }else{
1699
        planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1700
        planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1701
    }
1702
    return srcSliceH;
1703
}
1704

    
1705
/* unscaled copy like stuff (assumes nearly identical formats) */
1706
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1707
                      int srcSliceH, uint8_t* dst[], int dstStride[])
1708
{
1709
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1710
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1711
    else
1712
    {
1713
        int i;
1714
        uint8_t *srcPtr= src[0];
1715
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1716
        int length=0;
1717

    
1718
        /* universal length finder */
1719
        while(length+c->srcW <= FFABS(dstStride[0])
1720
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1721
        assert(length!=0);
1722

    
1723
        for (i=0; i<srcSliceH; i++)
1724
        {
1725
            memcpy(dstPtr, srcPtr, length);
1726
            srcPtr+= srcStride[0];
1727
            dstPtr+= dstStride[0];
1728
        }
1729
    }
1730
    return srcSliceH;
1731
}
1732

    
1733
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1734
                      int srcSliceH, uint8_t* dst[], int dstStride[])
1735
{
1736
    int plane;
1737
    for (plane=0; plane<3; plane++)
1738
    {
1739
        int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1740
        int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1741
        int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1742

    
1743
        if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1744
        {
1745
            if (!isGray(c->dstFormat))
1746
                memset(dst[plane], 128, dstStride[plane]*height);
1747
        }
1748
        else
1749
        {
1750
            if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1751
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1752
            else
1753
            {
1754
                int i;
1755
                uint8_t *srcPtr= src[plane];
1756
                uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1757
                for (i=0; i<height; i++)
1758
                {
1759
                    memcpy(dstPtr, srcPtr, length);
1760
                    srcPtr+= srcStride[plane];
1761
                    dstPtr+= dstStride[plane];
1762
                }
1763
            }
1764
        }
1765
    }
1766
    return srcSliceH;
1767
}
1768

    
1769
static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1770
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
1771

    
1772
    int length= c->srcW;
1773
    int y=      srcSliceY;
1774
    int height= srcSliceH;
1775
    int i, j;
1776
    uint8_t *srcPtr= src[0];
1777
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1778

    
1779
    if (!isGray(c->dstFormat)){
1780
        int height= -((-srcSliceH)>>c->chrDstVSubSample);
1781
        memset(dst[1], 128, dstStride[1]*height);
1782
        memset(dst[2], 128, dstStride[2]*height);
1783
    }
1784
    if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1785
    for (i=0; i<height; i++)
1786
    {
1787
        for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1788
        srcPtr+= srcStride[0];
1789
        dstPtr+= dstStride[0];
1790
    }
1791
    return srcSliceH;
1792
}
1793

    
1794
static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1795
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
1796

    
1797
    int length= c->srcW;
1798
    int y=      srcSliceY;
1799
    int height= srcSliceH;
1800
    int i, j;
1801
    uint8_t *srcPtr= src[0];
1802
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1803
    for (i=0; i<height; i++)
1804
    {
1805
        for (j=0; j<length; j++)
1806
        {
1807
            dstPtr[j<<1] = srcPtr[j];
1808
            dstPtr[(j<<1)+1] = srcPtr[j];
1809
        }
1810
        srcPtr+= srcStride[0];
1811
        dstPtr+= dstStride[0];
1812
    }
1813
    return srcSliceH;
1814
}
1815

    
1816
static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1817
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
1818

    
1819
    int length= c->srcW;
1820
    int y=      srcSliceY;
1821
    int height= srcSliceH;
1822
    int i, j;
1823
    uint16_t *srcPtr= (uint16_t*)src[0];
1824
    uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
1825
    for (i=0; i<height; i++)
1826
    {
1827
        for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1828
        srcPtr+= srcStride[0]/2;
1829
        dstPtr+= dstStride[0]/2;
1830
    }
1831
    return srcSliceH;
1832
}
1833

    
1834

    
1835
static void getSubSampleFactors(int *h, int *v, int format){
1836
    switch(format){
1837
    case PIX_FMT_UYVY422:
1838
    case PIX_FMT_YUYV422:
1839
        *h=1;
1840
        *v=0;
1841
        break;
1842
    case PIX_FMT_YUV420P:
1843
    case PIX_FMT_YUVA420P:
1844
    case PIX_FMT_GRAY16BE:
1845
    case PIX_FMT_GRAY16LE:
1846
    case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
1847
    case PIX_FMT_NV12:
1848
    case PIX_FMT_NV21:
1849
        *h=1;
1850
        *v=1;
1851
        break;
1852
    case PIX_FMT_YUV440P:
1853
        *h=0;
1854
        *v=1;
1855
        break;
1856
    case PIX_FMT_YUV410P:
1857
        *h=2;
1858
        *v=2;
1859
        break;
1860
    case PIX_FMT_YUV444P:
1861
        *h=0;
1862
        *v=0;
1863
        break;
1864
    case PIX_FMT_YUV422P:
1865
        *h=1;
1866
        *v=0;
1867
        break;
1868
    case PIX_FMT_YUV411P:
1869
        *h=2;
1870
        *v=0;
1871
        break;
1872
    default:
1873
        *h=0;
1874
        *v=0;
1875
        break;
1876
    }
1877
}
1878

    
1879
static uint16_t roundToInt16(int64_t f){
1880
    int r= (f + (1<<15))>>16;
1881
         if (r<-0x7FFF) return 0x8000;
1882
    else if (r> 0x7FFF) return 0x7FFF;
1883
    else                return r;
1884
}
1885

    
1886
/**
1887
 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1888
 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
1889
 * @return -1 if not supported
1890
 */
1891
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1892
    int64_t crv =  inv_table[0];
1893
    int64_t cbu =  inv_table[1];
1894
    int64_t cgu = -inv_table[2];
1895
    int64_t cgv = -inv_table[3];
1896
    int64_t cy  = 1<<16;
1897
    int64_t oy  = 0;
1898

    
1899
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1900
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1901
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
1902

    
1903
    c->brightness= brightness;
1904
    c->contrast  = contrast;
1905
    c->saturation= saturation;
1906
    c->srcRange  = srcRange;
1907
    c->dstRange  = dstRange;
1908

    
1909
    c->uOffset=   0x0400040004000400LL;
1910
    c->vOffset=   0x0400040004000400LL;
1911

    
1912
    if (!srcRange){
1913
        cy= (cy*255) / 219;
1914
        oy= 16<<16;
1915
    }else{
1916
        crv= (crv*224) / 255;
1917
        cbu= (cbu*224) / 255;
1918
        cgu= (cgu*224) / 255;
1919
        cgv= (cgv*224) / 255;
1920
    }
1921

    
1922
    cy = (cy *contrast             )>>16;
1923
    crv= (crv*contrast * saturation)>>32;
1924
    cbu= (cbu*contrast * saturation)>>32;
1925
    cgu= (cgu*contrast * saturation)>>32;
1926
    cgv= (cgv*contrast * saturation)>>32;
1927

    
1928
    oy -= 256*brightness;
1929

    
1930
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
1931
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
1932
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1933
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1934
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1935
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
1936

    
1937
    yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1938
    //FIXME factorize
1939

    
1940
#ifdef COMPILE_ALTIVEC
1941
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1942
        yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1943
#endif
1944
    return 0;
1945
}
1946

    
1947
/**
1948
 * @return -1 if not supported
1949
 */
1950
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1951
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1952

    
1953
    *inv_table = c->srcColorspaceTable;
1954
    *table     = c->dstColorspaceTable;
1955
    *srcRange  = c->srcRange;
1956
    *dstRange  = c->dstRange;
1957
    *brightness= c->brightness;
1958
    *contrast  = c->contrast;
1959
    *saturation= c->saturation;
1960

    
1961
    return 0;
1962
}
1963

    
1964
static int handle_jpeg(int *format)
1965
{
1966
    switch (*format) {
1967
        case PIX_FMT_YUVJ420P:
1968
            *format = PIX_FMT_YUV420P;
1969
            return 1;
1970
        case PIX_FMT_YUVJ422P:
1971
            *format = PIX_FMT_YUV422P;
1972
            return 1;
1973
        case PIX_FMT_YUVJ444P:
1974
            *format = PIX_FMT_YUV444P;
1975
            return 1;
1976
        case PIX_FMT_YUVJ440P:
1977
            *format = PIX_FMT_YUV440P;
1978
            return 1;
1979
        default:
1980
            return 0;
1981
    }
1982
}
1983

    
1984
SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1985
                           SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
1986

    
1987
    SwsContext *c;
1988
    int i;
1989
    int usesVFilter, usesHFilter;
1990
    int unscaled, needsDither;
1991
    int srcRange, dstRange;
1992
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1993
#if defined(ARCH_X86)
1994
    if (flags & SWS_CPU_CAPS_MMX)
1995
        asm volatile("emms\n\t"::: "memory");
1996
#endif
1997

    
1998
#if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
1999
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2000
#ifdef HAVE_MMX2
2001
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2002
#elif defined (HAVE_3DNOW)
2003
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2004
#elif defined (HAVE_MMX)
2005
    flags |= SWS_CPU_CAPS_MMX;
2006
#elif defined (HAVE_ALTIVEC)
2007
    flags |= SWS_CPU_CAPS_ALTIVEC;
2008
#elif defined (ARCH_BFIN)
2009
    flags |= SWS_CPU_CAPS_BFIN;
2010
#endif
2011
#endif /* RUNTIME_CPUDETECT */
2012
    if (clip_table[512] != 255) globalInit();
2013
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2014

    
2015
    unscaled = (srcW == dstW && srcH == dstH);
2016
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2017
        && (fmt_depth(dstFormat))<24
2018
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2019

    
2020
    srcRange = handle_jpeg(&srcFormat);
2021
    dstRange = handle_jpeg(&dstFormat);
2022

    
2023
    if (!isSupportedIn(srcFormat))
2024
    {
2025
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2026
        return NULL;
2027
    }
2028
    if (!isSupportedOut(dstFormat))
2029
    {
2030
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2031
        return NULL;
2032
    }
2033

    
2034
    i= flags & ( SWS_POINT
2035
                |SWS_AREA
2036
                |SWS_BILINEAR
2037
                |SWS_FAST_BILINEAR
2038
                |SWS_BICUBIC
2039
                |SWS_X
2040
                |SWS_GAUSS
2041
                |SWS_LANCZOS
2042
                |SWS_SINC
2043
                |SWS_SPLINE
2044
                |SWS_BICUBLIN);
2045
    if(!i || (i & (i-1)))
2046
    {
2047
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be choosen\n");
2048
        return NULL;
2049
    }
2050

    
2051

    
2052
    /* sanity check */
2053
    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
2054
    {
2055
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2056
               srcW, srcH, dstW, dstH);
2057
        return NULL;
2058
    }
2059
    if(srcW > VOFW || dstW > VOFW){
2060
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2061
        return NULL;
2062
    }
2063

    
2064
    if (!dstFilter) dstFilter= &dummyFilter;
2065
    if (!srcFilter) srcFilter= &dummyFilter;
2066

    
2067
    c= av_mallocz(sizeof(SwsContext));
2068

    
2069
    c->av_class = &sws_context_class;
2070
    c->srcW= srcW;
2071
    c->srcH= srcH;
2072
    c->dstW= dstW;
2073
    c->dstH= dstH;
2074
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2075
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2076
    c->flags= flags;
2077
    c->dstFormat= dstFormat;
2078
    c->srcFormat= srcFormat;
2079
    c->vRounder= 4* 0x0001000100010001ULL;
2080

    
2081
    usesHFilter= usesVFilter= 0;
2082
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2083
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2084
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2085
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2086
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2087
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2088
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2089
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2090

    
2091
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2092
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2093

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

    
2097
    // drop some chroma lines if the user wants it
2098
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2099
    c->chrSrcVSubSample+= c->vChrDrop;
2100

    
2101
    // drop every 2. pixel for chroma calculation unless user wants full chroma
2102
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2103
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2104
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2105
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE)
2106
        c->chrSrcHSubSample=1;
2107

    
2108
    if (param){
2109
        c->param[0] = param[0];
2110
        c->param[1] = param[1];
2111
    }else{
2112
        c->param[0] =
2113
        c->param[1] = SWS_PARAM_DEFAULT;
2114
    }
2115

    
2116
    c->chrIntHSubSample= c->chrDstHSubSample;
2117
    c->chrIntVSubSample= c->chrSrcVSubSample;
2118

    
2119
    // Note the -((-x)>>y) is so that we always round toward +inf.
2120
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2121
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2122
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2123
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2124

    
2125
    sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2126

    
2127
    /* unscaled special Cases */
2128
    if (unscaled && !usesHFilter && !usesVFilter)
2129
    {
2130
        /* yv12_to_nv12 */
2131
        if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2132
        {
2133
            c->swScale= PlanarToNV12Wrapper;
2134
        }
2135
#ifdef CONFIG_GPL
2136
        /* yuv2bgr */
2137
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
2138
        {
2139
            c->swScale= yuv2rgb_get_func_ptr(c);
2140
        }
2141
#endif
2142

    
2143
        if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P)
2144
        {
2145
            c->swScale= yvu9toyv12Wrapper;
2146
        }
2147

    
2148
        /* bgr24toYV12 */
2149
        if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
2150
            c->swScale= bgr24toyv12Wrapper;
2151

    
2152
        /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2153
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2154
           && (isBGR(dstFormat) || isRGB(dstFormat))
2155
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2156
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2157
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2158
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2159
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2160
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2161
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2162
                                             && dstFormat != PIX_FMT_RGB32_1
2163
                                             && dstFormat != PIX_FMT_BGR32_1
2164
           && !needsDither)
2165
             c->swScale= rgb2rgbWrapper;
2166

    
2167
        /* LQ converters if -sws 0 or -sws 4*/
2168
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2169
            /* rgb/bgr -> rgb/bgr (dither needed forms) */
2170
            if ( (isBGR(srcFormat) || isRGB(srcFormat))
2171
              && (isBGR(dstFormat) || isRGB(dstFormat))
2172
              && needsDither)
2173
                c->swScale= rgb2rgbWrapper;
2174

    
2175
            /* yv12_to_yuy2 */
2176
            if (srcFormat == PIX_FMT_YUV420P &&
2177
                (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
2178
            {
2179
                if (dstFormat == PIX_FMT_YUYV422)
2180
                    c->swScale= PlanarToYuy2Wrapper;
2181
                else
2182
                    c->swScale= PlanarToUyvyWrapper;
2183
            }
2184
        }
2185

    
2186
#ifdef COMPILE_ALTIVEC
2187
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2188
            ((srcFormat == PIX_FMT_YUV420P &&
2189
             (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
2190
          // unscaled YV12 -> packed YUV, we want speed
2191
          if (dstFormat == PIX_FMT_YUYV422)
2192
              c->swScale= yv12toyuy2_unscaled_altivec;
2193
          else
2194
              c->swScale= yv12touyvy_unscaled_altivec;
2195
        }
2196
#endif
2197

    
2198
        /* simple copy */
2199
        if (  srcFormat == dstFormat
2200
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2201
            || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2202
        {
2203
            if (isPacked(c->srcFormat))
2204
                c->swScale= packedCopy;
2205
            else /* Planar YUV or gray */
2206
                c->swScale= planarCopy;
2207
        }
2208

    
2209
        /* gray16{le,be} conversions */
2210
        if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2211
        {
2212
            c->swScale= gray16togray;
2213
        }
2214
        if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2215
        {
2216
            c->swScale= graytogray16;
2217
        }
2218
        if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2219
        {
2220
            c->swScale= gray16swap;
2221
        }
2222

    
2223
#ifdef ARCH_BFIN
2224
        if (flags & SWS_CPU_CAPS_BFIN)
2225
            ff_bfin_get_unscaled_swscale (c);
2226
#endif
2227

    
2228
        if (c->swScale){
2229
            if (flags&SWS_PRINT_INFO)
2230
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2231
                                sws_format_name(srcFormat), sws_format_name(dstFormat));
2232
            return c;
2233
        }
2234
    }
2235

    
2236
    if (flags & SWS_CPU_CAPS_MMX2)
2237
    {
2238
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2239
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2240
        {
2241
            if (flags&SWS_PRINT_INFO)
2242
                av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
2243
        }
2244
        if (usesHFilter) c->canMMX2BeUsed=0;
2245
    }
2246
    else
2247
        c->canMMX2BeUsed=0;
2248

    
2249
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2250
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2251

    
2252
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2253
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
2254
    // n-2 is the last chrominance sample available
2255
    // this is not perfect, but no one should notice the difference, the more correct variant
2256
    // would be like the vertical one, but that would require some special code for the
2257
    // first and last pixel
2258
    if (flags&SWS_FAST_BILINEAR)
2259
    {
2260
        if (c->canMMX2BeUsed)
2261
        {
2262
            c->lumXInc+= 20;
2263
            c->chrXInc+= 20;
2264
        }
2265
        //we don't use the x86asm scaler if mmx is available
2266
        else if (flags & SWS_CPU_CAPS_MMX)
2267
        {
2268
            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2269
            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2270
        }
2271
    }
2272

    
2273
    /* precalculate horizontal scaler filter coefficients */
2274
    {
2275
        const int filterAlign=
2276
            (flags & SWS_CPU_CAPS_MMX) ? 4 :
2277
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2278
            1;
2279

    
2280
        initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2281
                   srcW      ,       dstW, filterAlign, 1<<14,
2282
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2283
                   srcFilter->lumH, dstFilter->lumH, c->param);
2284
        initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2285
                   c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2286
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2287
                   srcFilter->chrH, dstFilter->chrH, c->param);
2288

    
2289
#define MAX_FUNNY_CODE_SIZE 10000
2290
#if defined(COMPILE_MMX2)
2291
// can't downscale !!!
2292
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2293
        {
2294
#ifdef MAP_ANONYMOUS
2295
            c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2296
            c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2297
#else
2298
            c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2299
            c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2300
#endif
2301

    
2302
            c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2303
            c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2304
            c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2305
            c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2306

    
2307
            initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2308
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2309
        }
2310
#endif /* defined(COMPILE_MMX2) */
2311
    } // Init Horizontal stuff
2312

    
2313

    
2314

    
2315
    /* precalculate vertical scaler filter coefficients */
2316
    {
2317
        const int filterAlign=
2318
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2319
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2320
            1;
2321

    
2322
        initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2323
                   srcH      ,        dstH, filterAlign, (1<<12)-4,
2324
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2325
                   srcFilter->lumV, dstFilter->lumV, c->param);
2326
        initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2327
                   c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2328
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2329
                   srcFilter->chrV, dstFilter->chrV, c->param);
2330

    
2331
#ifdef HAVE_ALTIVEC
2332
        c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2333
        c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2334

    
2335
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2336
            int j;
2337
            short *p = (short *)&c->vYCoeffsBank[i];
2338
            for (j=0;j<8;j++)
2339
                p[j] = c->vLumFilter[i];
2340
        }
2341

    
2342
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2343
            int j;
2344
            short *p = (short *)&c->vCCoeffsBank[i];
2345
            for (j=0;j<8;j++)
2346
                p[j] = c->vChrFilter[i];
2347
        }
2348
#endif
2349
    }
2350

    
2351
    // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2352
    c->vLumBufSize= c->vLumFilterSize;
2353
    c->vChrBufSize= c->vChrFilterSize;
2354
    for (i=0; i<dstH; i++)
2355
    {
2356
        int chrI= i*c->chrDstH / dstH;
2357
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2358
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2359

    
2360
        nextSlice>>= c->chrSrcVSubSample;
2361
        nextSlice<<= c->chrSrcVSubSample;
2362
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2363
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2364
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2365
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2366
    }
2367

    
2368
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2369
    c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2370
    c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2371
    //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)
2372
    /* align at 16 bytes for AltiVec */
2373
    for (i=0; i<c->vLumBufSize; i++)
2374
        c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2375
    for (i=0; i<c->vChrBufSize; i++)
2376
        c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2377

    
2378
    //try to avoid drawing green stuff between the right end and the stride end
2379
    for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2380

    
2381
    assert(2*VOFW == VOF);
2382

    
2383
    assert(c->chrDstH <= dstH);
2384

    
2385
    if (flags&SWS_PRINT_INFO)
2386
    {
2387
#ifdef DITHER1XBPP
2388
        const char *dither= " dithered";
2389
#else
2390
        const char *dither= "";
2391
#endif
2392
        if (flags&SWS_FAST_BILINEAR)
2393
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2394
        else if (flags&SWS_BILINEAR)
2395
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2396
        else if (flags&SWS_BICUBIC)
2397
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2398
        else if (flags&SWS_X)
2399
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2400
        else if (flags&SWS_POINT)
2401
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2402
        else if (flags&SWS_AREA)
2403
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2404
        else if (flags&SWS_BICUBLIN)
2405
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2406
        else if (flags&SWS_GAUSS)
2407
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2408
        else if (flags&SWS_SINC)
2409
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2410
        else if (flags&SWS_LANCZOS)
2411
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2412
        else if (flags&SWS_SPLINE)
2413
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2414
        else
2415
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2416

    
2417
        if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2418
            av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2419
                   sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2420
        else
2421
            av_log(c, AV_LOG_INFO, "from %s to %s ",
2422
                   sws_format_name(srcFormat), sws_format_name(dstFormat));
2423

    
2424
        if (flags & SWS_CPU_CAPS_MMX2)
2425
            av_log(c, AV_LOG_INFO, "using MMX2\n");
2426
        else if (flags & SWS_CPU_CAPS_3DNOW)
2427
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2428
        else if (flags & SWS_CPU_CAPS_MMX)
2429
            av_log(c, AV_LOG_INFO, "using MMX\n");
2430
        else if (flags & SWS_CPU_CAPS_ALTIVEC)
2431
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
2432
        else
2433
            av_log(c, AV_LOG_INFO, "using C\n");
2434
    }
2435

    
2436
    if (flags & SWS_PRINT_INFO)
2437
    {
2438
        if (flags & SWS_CPU_CAPS_MMX)
2439
        {
2440
            if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2441
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2442
            else
2443
            {
2444
                if (c->hLumFilterSize==4)
2445
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2446
                else if (c->hLumFilterSize==8)
2447
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2448
                else
2449
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2450

    
2451
                if (c->hChrFilterSize==4)
2452
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2453
                else if (c->hChrFilterSize==8)
2454
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2455
                else
2456
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2457
            }
2458
        }
2459
        else
2460
        {
2461
#if defined(ARCH_X86)
2462
            av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
2463
#else
2464
            if (flags & SWS_FAST_BILINEAR)
2465
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2466
            else
2467
                av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2468
#endif
2469
        }
2470
        if (isPlanarYUV(dstFormat))
2471
        {
2472
            if (c->vLumFilterSize==1)
2473
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2474
            else
2475
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2476
        }
2477
        else
2478
        {
2479
            if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2480
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2481
                       "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2482
            else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2483
                av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2484
            else
2485
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2486
        }
2487

    
2488
        if (dstFormat==PIX_FMT_BGR24)
2489
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
2490
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2491
        else if (dstFormat==PIX_FMT_RGB32)
2492
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2493
        else if (dstFormat==PIX_FMT_BGR565)
2494
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2495
        else if (dstFormat==PIX_FMT_BGR555)
2496
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2497

    
2498
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2499
    }
2500
    if (flags & SWS_PRINT_INFO)
2501
    {
2502
        av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2503
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2504
        av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2505
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2506
    }
2507

    
2508
    c->swScale= getSwsFunc(flags);
2509
    return c;
2510
}
2511

    
2512
/**
2513
 * swscale wrapper, so we don't need to export the SwsContext.
2514
 * assumes planar YUV to be in YUV order instead of YVU
2515
 */
2516
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2517
              int srcSliceH, uint8_t* dst[], int dstStride[]){
2518
    int i;
2519
    uint8_t* src2[4]= {src[0], src[1], src[2]};
2520
    uint32_t pal[256];
2521
    int use_pal=   c->srcFormat == PIX_FMT_PAL8
2522
                || c->srcFormat == PIX_FMT_BGR4_BYTE
2523
                || c->srcFormat == PIX_FMT_RGB4_BYTE
2524
                || c->srcFormat == PIX_FMT_BGR8
2525
                || c->srcFormat == PIX_FMT_RGB8;
2526

    
2527
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2528
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2529
        return 0;
2530
    }
2531
    if (c->sliceDir == 0) {
2532
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2533
    }
2534

    
2535
    if (use_pal){
2536
        for (i=0; i<256; i++){
2537
            int p, r, g, b,y,u,v;
2538
            if(c->srcFormat == PIX_FMT_PAL8){
2539
                p=((uint32_t*)(src[1]))[i];
2540
                r= (p>>16)&0xFF;
2541
                g= (p>> 8)&0xFF;
2542
                b=  p     &0xFF;
2543
            }else if(c->srcFormat == PIX_FMT_RGB8){
2544
                r= (i>>5    )*36;
2545
                g= ((i>>2)&7)*36;
2546
                b= (i&3     )*85;
2547
            }else if(c->srcFormat == PIX_FMT_BGR8){
2548
                b= (i>>6    )*85;
2549
                g= ((i>>3)&7)*36;
2550
                r= (i&7     )*36;
2551
            }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2552
                r= (i>>3    )*255;
2553
                g= ((i>>1)&3)*85;
2554
                b= (i&1     )*255;
2555
            }else if(c->srcFormat == PIX_FMT_BGR4_BYTE){
2556
                b= (i>>3    )*255;
2557
                g= ((i>>1)&3)*85;
2558
                r= (i&1     )*255;
2559
            }
2560
            y= av_clip_uint8(((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16 );
2561
            u= av_clip_uint8(((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128);
2562
            v= av_clip_uint8(((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128);
2563
            pal[i]= y + (u<<8) + (v<<16);
2564
        }
2565
        src2[1]= (uint8_t*)pal;
2566
    }
2567

    
2568
    // copy strides, so they can safely be modified
2569
    if (c->sliceDir == 1) {
2570
        // slices go from top to bottom
2571
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2572
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2573
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2574
    } else {
2575
        // slices go from bottom to top => we flip the image internally
2576
        uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2577
                           dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2578
                           dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2579
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2580
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2581

    
2582
        src2[0] += (srcSliceH-1)*srcStride[0];
2583
        if (!use_pal)
2584
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2585
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2586

    
2587
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2588
    }
2589
}
2590

    
2591
/**
2592
 * swscale wrapper, so we don't need to export the SwsContext
2593
 */
2594
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2595
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
2596
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2597
}
2598

    
2599
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2600
                                float lumaSharpen, float chromaSharpen,
2601
                                float chromaHShift, float chromaVShift,
2602
                                int verbose)
2603
{
2604
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2605

    
2606
    if (lumaGBlur!=0.0){
2607
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2608
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2609
    }else{
2610
        filter->lumH= sws_getIdentityVec();
2611
        filter->lumV= sws_getIdentityVec();
2612
    }
2613

    
2614
    if (chromaGBlur!=0.0){
2615
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2616
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2617
    }else{
2618
        filter->chrH= sws_getIdentityVec();
2619
        filter->chrV= sws_getIdentityVec();
2620
    }
2621

    
2622
    if (chromaSharpen!=0.0){
2623
        SwsVector *id= sws_getIdentityVec();
2624
        sws_scaleVec(filter->chrH, -chromaSharpen);
2625
        sws_scaleVec(filter->chrV, -chromaSharpen);
2626
        sws_addVec(filter->chrH, id);
2627
        sws_addVec(filter->chrV, id);
2628
        sws_freeVec(id);
2629
    }
2630

    
2631
    if (lumaSharpen!=0.0){
2632
        SwsVector *id= sws_getIdentityVec();
2633
        sws_scaleVec(filter->lumH, -lumaSharpen);
2634
        sws_scaleVec(filter->lumV, -lumaSharpen);
2635
        sws_addVec(filter->lumH, id);
2636
        sws_addVec(filter->lumV, id);
2637
        sws_freeVec(id);
2638
    }
2639

    
2640
    if (chromaHShift != 0.0)
2641
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2642

    
2643
    if (chromaVShift != 0.0)
2644
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2645

    
2646
    sws_normalizeVec(filter->chrH, 1.0);
2647
    sws_normalizeVec(filter->chrV, 1.0);
2648
    sws_normalizeVec(filter->lumH, 1.0);
2649
    sws_normalizeVec(filter->lumV, 1.0);
2650

    
2651
    if (verbose) sws_printVec(filter->chrH);
2652
    if (verbose) sws_printVec(filter->lumH);
2653

    
2654
    return filter;
2655
}
2656

    
2657
/**
2658
 * returns a normalized gaussian curve used to filter stuff
2659
 * quality=3 is high quality, lowwer is lowwer quality
2660
 */
2661
SwsVector *sws_getGaussianVec(double variance, double quality){
2662
    const int length= (int)(variance*quality + 0.5) | 1;
2663
    int i;
2664
    double *coeff= av_malloc(length*sizeof(double));
2665
    double middle= (length-1)*0.5;
2666
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2667

    
2668
    vec->coeff= coeff;
2669
    vec->length= length;
2670

    
2671
    for (i=0; i<length; i++)
2672
    {
2673
        double dist= i-middle;
2674
        coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
2675
    }
2676

    
2677
    sws_normalizeVec(vec, 1.0);
2678

    
2679
    return vec;
2680
}
2681

    
2682
SwsVector *sws_getConstVec(double c, int length){
2683
    int i;
2684
    double *coeff= av_malloc(length*sizeof(double));
2685
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2686

    
2687
    vec->coeff= coeff;
2688
    vec->length= length;
2689

    
2690
    for (i=0; i<length; i++)
2691
        coeff[i]= c;
2692

    
2693
    return vec;
2694
}
2695

    
2696

    
2697
SwsVector *sws_getIdentityVec(void){
2698
    return sws_getConstVec(1.0, 1);
2699
}
2700

    
2701
double sws_dcVec(SwsVector *a){
2702
    int i;
2703
    double sum=0;
2704

    
2705
    for (i=0; i<a->length; i++)
2706
        sum+= a->coeff[i];
2707

    
2708
    return sum;
2709
}
2710

    
2711
void sws_scaleVec(SwsVector *a, double scalar){
2712
    int i;
2713

    
2714
    for (i=0; i<a->length; i++)
2715
        a->coeff[i]*= scalar;
2716
}
2717

    
2718
void sws_normalizeVec(SwsVector *a, double height){
2719
    sws_scaleVec(a, height/sws_dcVec(a));
2720
}
2721

    
2722
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2723
    int length= a->length + b->length - 1;
2724
    double *coeff= av_malloc(length*sizeof(double));
2725
    int i, j;
2726
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2727

    
2728
    vec->coeff= coeff;
2729
    vec->length= length;
2730

    
2731
    for (i=0; i<length; i++) coeff[i]= 0.0;
2732

    
2733
    for (i=0; i<a->length; i++)
2734
    {
2735
        for (j=0; j<b->length; j++)
2736
        {
2737
            coeff[i+j]+= a->coeff[i]*b->coeff[j];
2738
        }
2739
    }
2740

    
2741
    return vec;
2742
}
2743

    
2744
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2745
    int length= FFMAX(a->length, b->length);
2746
    double *coeff= av_malloc(length*sizeof(double));
2747
    int i;
2748
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2749

    
2750
    vec->coeff= coeff;
2751
    vec->length= length;
2752

    
2753
    for (i=0; i<length; i++) coeff[i]= 0.0;
2754

    
2755
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2756
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2757

    
2758
    return vec;
2759
}
2760

    
2761
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2762
    int length= FFMAX(a->length, b->length);
2763
    double *coeff= av_malloc(length*sizeof(double));
2764
    int i;
2765
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2766

    
2767
    vec->coeff= coeff;
2768
    vec->length= length;
2769

    
2770
    for (i=0; i<length; i++) coeff[i]= 0.0;
2771

    
2772
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2773
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2774

    
2775
    return vec;
2776
}
2777

    
2778
/* shift left / or right if "shift" is negative */
2779
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2780
    int length= a->length + FFABS(shift)*2;
2781
    double *coeff= av_malloc(length*sizeof(double));
2782
    int i;
2783
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2784

    
2785
    vec->coeff= coeff;
2786
    vec->length= length;
2787

    
2788
    for (i=0; i<length; i++) coeff[i]= 0.0;
2789

    
2790
    for (i=0; i<a->length; i++)
2791
    {
2792
        coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2793
    }
2794

    
2795
    return vec;
2796
}
2797

    
2798
void sws_shiftVec(SwsVector *a, int shift){
2799
    SwsVector *shifted= sws_getShiftedVec(a, shift);
2800
    av_free(a->coeff);
2801
    a->coeff= shifted->coeff;
2802
    a->length= shifted->length;
2803
    av_free(shifted);
2804
}
2805

    
2806
void sws_addVec(SwsVector *a, SwsVector *b){
2807
    SwsVector *sum= sws_sumVec(a, b);
2808
    av_free(a->coeff);
2809
    a->coeff= sum->coeff;
2810
    a->length= sum->length;
2811
    av_free(sum);
2812
}
2813

    
2814
void sws_subVec(SwsVector *a, SwsVector *b){
2815
    SwsVector *diff= sws_diffVec(a, b);
2816
    av_free(a->coeff);
2817
    a->coeff= diff->coeff;
2818
    a->length= diff->length;
2819
    av_free(diff);
2820
}
2821

    
2822
void sws_convVec(SwsVector *a, SwsVector *b){
2823
    SwsVector *conv= sws_getConvVec(a, b);
2824
    av_free(a->coeff);
2825
    a->coeff= conv->coeff;
2826
    a->length= conv->length;
2827
    av_free(conv);
2828
}
2829

    
2830
SwsVector *sws_cloneVec(SwsVector *a){
2831
    double *coeff= av_malloc(a->length*sizeof(double));
2832
    int i;
2833
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2834

    
2835
    vec->coeff= coeff;
2836
    vec->length= a->length;
2837

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

    
2840
    return vec;
2841
}
2842

    
2843
void sws_printVec(SwsVector *a){
2844
    int i;
2845
    double max=0;
2846
    double min=0;
2847
    double range;
2848

    
2849
    for (i=0; i<a->length; i++)
2850
        if (a->coeff[i]>max) max= a->coeff[i];
2851

    
2852
    for (i=0; i<a->length; i++)
2853
        if (a->coeff[i]<min) min= a->coeff[i];
2854

    
2855
    range= max - min;
2856

    
2857
    for (i=0; i<a->length; i++)
2858
    {
2859
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2860
        av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2861
        for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2862
        av_log(NULL, AV_LOG_DEBUG, "|\n");
2863
    }
2864
}
2865

    
2866
void sws_freeVec(SwsVector *a){
2867
    if (!a) return;
2868
    av_freep(&a->coeff);
2869
    a->length=0;
2870
    av_free(a);
2871
}
2872

    
2873
void sws_freeFilter(SwsFilter *filter){
2874
    if (!filter) return;
2875

    
2876
    if (filter->lumH) sws_freeVec(filter->lumH);
2877
    if (filter->lumV) sws_freeVec(filter->lumV);
2878
    if (filter->chrH) sws_freeVec(filter->chrH);
2879
    if (filter->chrV) sws_freeVec(filter->chrV);
2880
    av_free(filter);
2881
}
2882

    
2883

    
2884
void sws_freeContext(SwsContext *c){
2885
    int i;
2886
    if (!c) return;
2887

    
2888
    if (c->lumPixBuf)
2889
    {
2890
        for (i=0; i<c->vLumBufSize; i++)
2891
            av_freep(&c->lumPixBuf[i]);
2892
        av_freep(&c->lumPixBuf);
2893
    }
2894

    
2895
    if (c->chrPixBuf)
2896
    {
2897
        for (i=0; i<c->vChrBufSize; i++)
2898
            av_freep(&c->chrPixBuf[i]);
2899
        av_freep(&c->chrPixBuf);
2900
    }
2901

    
2902
    av_freep(&c->vLumFilter);
2903
    av_freep(&c->vChrFilter);
2904
    av_freep(&c->hLumFilter);
2905
    av_freep(&c->hChrFilter);
2906
#ifdef HAVE_ALTIVEC
2907
    av_freep(&c->vYCoeffsBank);
2908
    av_freep(&c->vCCoeffsBank);
2909
#endif
2910

    
2911
    av_freep(&c->vLumFilterPos);
2912
    av_freep(&c->vChrFilterPos);
2913
    av_freep(&c->hLumFilterPos);
2914
    av_freep(&c->hChrFilterPos);
2915

    
2916
#if defined(ARCH_X86) && defined(CONFIG_GPL)
2917
#ifdef MAP_ANONYMOUS
2918
    if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2919
    if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2920
#else
2921
    av_free(c->funnyYCode);
2922
    av_free(c->funnyUVCode);
2923
#endif
2924
    c->funnyYCode=NULL;
2925
    c->funnyUVCode=NULL;
2926
#endif /* defined(ARCH_X86) */
2927

    
2928
    av_freep(&c->lumMmx2Filter);
2929
    av_freep(&c->chrMmx2Filter);
2930
    av_freep(&c->lumMmx2FilterPos);
2931
    av_freep(&c->chrMmx2FilterPos);
2932
    av_freep(&c->yuvTable);
2933

    
2934
    av_free(c);
2935
}
2936

    
2937
/**
2938
 * Checks if context is valid or reallocs a new one instead.
2939
 * If context is NULL, just calls sws_getContext() to get a new one.
2940
 * Otherwise, checks if the parameters are the same already saved in context.
2941
 * If that is the case, returns the current context.
2942
 * Otherwise, frees context and gets a new one.
2943
 *
2944
 * Be warned that srcFilter, dstFilter are not checked, they are
2945
 * asumed to remain valid.
2946
 */
2947
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
2948
                                        int srcW, int srcH, int srcFormat,
2949
                                        int dstW, int dstH, int dstFormat, int flags,
2950
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
2951
{
2952
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
2953

    
2954
    if (!param)
2955
        param = default_param;
2956

    
2957
    if (context) {
2958
        if (context->srcW != srcW || context->srcH != srcH ||
2959
            context->srcFormat != srcFormat ||
2960
            context->dstW != dstW || context->dstH != dstH ||
2961
            context->dstFormat != dstFormat || context->flags != flags ||
2962
            context->param[0] != param[0] || context->param[1] != param[1])
2963
        {
2964
            sws_freeContext(context);
2965
            context = NULL;
2966
        }
2967
    }
2968
    if (!context) {
2969
        return sws_getContext(srcW, srcH, srcFormat,
2970
                              dstW, dstH, dstFormat, flags,
2971
                              srcFilter, dstFilter, param);
2972
    }
2973
    return context;
2974
}
2975