Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 95c8bb03

History | View | Annotate | Download (74.1 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
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (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 GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License 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

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

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

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

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

    
54
#include <inttypes.h>
55
#include <string.h>
56
#include <math.h>
57
#include <stdio.h>
58
#include "config.h"
59
#include <assert.h>
60
#include "swscale.h"
61
#include "swscale_internal.h"
62
#include "rgb2rgb.h"
63
#include "libavutil/intreadwrite.h"
64
#include "libavutil/x86_cpu.h"
65
#include "libavutil/avutil.h"
66
#include "libavutil/mathematics.h"
67
#include "libavutil/bswap.h"
68
#include "libavutil/pixdesc.h"
69

    
70
#undef MOVNTQ
71
#undef PAVGB
72

    
73
//#undef HAVE_MMX2
74
//#define HAVE_AMD3DNOW
75
//#undef HAVE_MMX
76
//#undef ARCH_X86
77
#define DITHER1XBPP
78

    
79
#define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
80

    
81
#define isPacked(x)         (       \
82
           (x)==PIX_FMT_PAL8        \
83
        || (x)==PIX_FMT_YUYV422     \
84
        || (x)==PIX_FMT_UYVY422     \
85
        || (x)==PIX_FMT_Y400A       \
86
        || isAnyRGB(x)              \
87
    )
88

    
89
#define RGB2YUV_SHIFT 15
90
#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
91
#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
92
#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
93
#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
94
#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
95
#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
96
#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
97
#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
98
#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
99

    
100
static const double rgb2yuv_table[8][9]={
101
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
102
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
103
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
104
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
105
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
106
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
107
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
108
    {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
109
};
110

    
111
/*
112
NOTES
113
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
114

115
TODO
116
more intelligent misalignment avoidance for the horizontal scaler
117
write special vertical cubic upscale version
118
optimize C code (YV12 / minmax)
119
add support for packed pixel YUV input & output
120
add support for Y8 output
121
optimize BGR24 & BGR32
122
add BGR4 output support
123
write special BGR->BGR scaler
124
*/
125

    
126
#if ARCH_X86
127
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
128
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
129
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
130
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
131
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
132
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
133
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
134
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
135

    
136
const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
137
        0x0103010301030103LL,
138
        0x0200020002000200LL,};
139

    
140
const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
141
        0x0602060206020602LL,
142
        0x0004000400040004LL,};
143

    
144
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
145
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
146
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
147
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
148
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
149
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
150

    
151
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
152
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
153
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
154

    
155
#ifdef FAST_BGR2YV12
156
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
157
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
158
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
159
#else
160
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
161
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
162
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
163
#endif /* FAST_BGR2YV12 */
164
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
165
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
166
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
167

    
168
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
169
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
170
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
171
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
172
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
173

    
174
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
175
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
176
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
177
};
178

    
179
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
180

    
181
#endif /* ARCH_X86 */
182

    
183
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
184
{  1,   3,   1,   3,   1,   3,   1,   3, },
185
{  2,   0,   2,   0,   2,   0,   2,   0, },
186
};
187

    
188
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
189
{  6,   2,   6,   2,   6,   2,   6,   2, },
190
{  0,   4,   0,   4,   0,   4,   0,   4, },
191
};
192

    
193
DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
194
{  8,   4,  11,   7,   8,   4,  11,   7, },
195
{  2,  14,   1,  13,   2,  14,   1,  13, },
196
{ 10,   6,   9,   5,  10,   6,   9,   5, },
197
{  0,  12,   3,  15,   0,  12,   3,  15, },
198
};
199

    
200
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
201
{ 17,   9,  23,  15,  16,   8,  22,  14, },
202
{  5,  29,   3,  27,   4,  28,   2,  26, },
203
{ 21,  13,  19,  11,  20,  12,  18,  10, },
204
{  0,  24,   6,  30,   1,  25,   7,  31, },
205
{ 16,   8,  22,  14,  17,   9,  23,  15, },
206
{  4,  28,   2,  26,   5,  29,   3,  27, },
207
{ 20,  12,  18,  10,  21,  13,  19,  11, },
208
{  1,  25,   7,  31,   0,  24,   6,  30, },
209
};
210

    
211
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
212
{  0,  55,  14,  68,   3,  58,  17,  72, },
213
{ 37,  18,  50,  32,  40,  22,  54,  35, },
214
{  9,  64,   5,  59,  13,  67,   8,  63, },
215
{ 46,  27,  41,  23,  49,  31,  44,  26, },
216
{  2,  57,  16,  71,   1,  56,  15,  70, },
217
{ 39,  21,  52,  34,  38,  19,  51,  33, },
218
{ 11,  66,   7,  62,  10,  65,   6,  60, },
219
{ 48,  30,  43,  25,  47,  29,  42,  24, },
220
};
221

    
222
#if 1
223
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
224
{117,  62, 158, 103, 113,  58, 155, 100, },
225
{ 34, 199,  21, 186,  31, 196,  17, 182, },
226
{144,  89, 131,  76, 141,  86, 127,  72, },
227
{  0, 165,  41, 206,  10, 175,  52, 217, },
228
{110,  55, 151,  96, 120,  65, 162, 107, },
229
{ 28, 193,  14, 179,  38, 203,  24, 189, },
230
{138,  83, 124,  69, 148,  93, 134,  79, },
231
{  7, 172,  48, 213,   3, 168,  45, 210, },
232
};
233
#elif 1
234
// tries to correct a gamma of 1.5
235
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
236
{  0, 143,  18, 200,   2, 156,  25, 215, },
237
{ 78,  28, 125,  64,  89,  36, 138,  74, },
238
{ 10, 180,   3, 161,  16, 195,   8, 175, },
239
{109,  51,  93,  38, 121,  60, 105,  47, },
240
{  1, 152,  23, 210,   0, 147,  20, 205, },
241
{ 85,  33, 134,  71,  81,  30, 130,  67, },
242
{ 14, 190,   6, 171,  12, 185,   5, 166, },
243
{117,  57, 101,  44, 113,  54,  97,  41, },
244
};
245
#elif 1
246
// tries to correct a gamma of 2.0
247
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
248
{  0, 124,   8, 193,   0, 140,  12, 213, },
249
{ 55,  14, 104,  42,  66,  19, 119,  52, },
250
{  3, 168,   1, 145,   6, 187,   3, 162, },
251
{ 86,  31,  70,  21,  99,  39,  82,  28, },
252
{  0, 134,  11, 206,   0, 129,   9, 200, },
253
{ 62,  17, 114,  48,  58,  16, 109,  45, },
254
{  5, 181,   2, 157,   4, 175,   1, 151, },
255
{ 95,  36,  78,  26,  90,  34,  74,  24, },
256
};
257
#else
258
// tries to correct a gamma of 2.5
259
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
260
{  0, 107,   3, 187,   0, 125,   6, 212, },
261
{ 39,   7,  86,  28,  49,  11, 102,  36, },
262
{  1, 158,   0, 131,   3, 180,   1, 151, },
263
{ 68,  19,  52,  12,  81,  25,  64,  17, },
264
{  0, 119,   5, 203,   0, 113,   4, 195, },
265
{ 45,   9,  96,  33,  42,   8,  91,  30, },
266
{  2, 172,   1, 144,   2, 165,   0, 137, },
267
{ 77,  23,  60,  15,  72,  21,  56,  14, },
268
};
269
#endif
270

    
271
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
272
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
273
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
274
                                                    int dstW, int chrDstW, int big_endian)
275
{
276
    //FIXME Optimize (just quickly written not optimized..)
277
    int i;
278

    
279
    for (i = 0; i < dstW; i++) {
280
        int val = 1 << 10;
281
        int j;
282

    
283
        for (j = 0; j < lumFilterSize; j++)
284
            val += lumSrc[j][i] * lumFilter[j];
285

    
286
        if (big_endian) {
287
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
288
        } else {
289
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
290
        }
291
    }
292

    
293
    if (uDest) {
294
        for (i = 0; i < chrDstW; i++) {
295
            int u = 1 << 10;
296
            int v = 1 << 10;
297
            int j;
298

    
299
            for (j = 0; j < chrFilterSize; j++) {
300
                u += chrSrc[j][i       ] * chrFilter[j];
301
                v += chrSrc[j][i + VOFW] * chrFilter[j];
302
            }
303

    
304
            if (big_endian) {
305
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
306
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
307
            } else {
308
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
309
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
310
            }
311
        }
312
    }
313

    
314
    if (CONFIG_SWSCALE_ALPHA && aDest) {
315
        for (i = 0; i < dstW; i++) {
316
            int val = 1 << 10;
317
            int j;
318

    
319
            for (j = 0; j < lumFilterSize; j++)
320
                val += alpSrc[j][i] * lumFilter[j];
321

    
322
            if (big_endian) {
323
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
324
            } else {
325
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
326
            }
327
        }
328
    }
329
}
330

    
331
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
332
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
333
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
334
                                 enum PixelFormat dstFormat)
335
{
336
    if (isBE(dstFormat)) {
337
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
338
                               chrFilter, chrSrc, chrFilterSize,
339
                               alpSrc,
340
                               dest, uDest, vDest, aDest,
341
                               dstW, chrDstW, 1);
342
    } else {
343
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
344
                               chrFilter, chrSrc, chrFilterSize,
345
                               alpSrc,
346
                               dest, uDest, vDest, aDest,
347
                               dstW, chrDstW, 0);
348
    }
349
}
350

    
351
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
352
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
353
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
354
{
355
    //FIXME Optimize (just quickly written not optimized..)
356
    int i;
357
    for (i=0; i<dstW; i++) {
358
        int val=1<<18;
359
        int j;
360
        for (j=0; j<lumFilterSize; j++)
361
            val += lumSrc[j][i] * lumFilter[j];
362

    
363
        dest[i]= av_clip_uint8(val>>19);
364
    }
365

    
366
    if (uDest)
367
        for (i=0; i<chrDstW; i++) {
368
            int u=1<<18;
369
            int v=1<<18;
370
            int j;
371
            for (j=0; j<chrFilterSize; j++) {
372
                u += chrSrc[j][i] * chrFilter[j];
373
                v += chrSrc[j][i + VOFW] * chrFilter[j];
374
            }
375

    
376
            uDest[i]= av_clip_uint8(u>>19);
377
            vDest[i]= av_clip_uint8(v>>19);
378
        }
379

    
380
    if (CONFIG_SWSCALE_ALPHA && aDest)
381
        for (i=0; i<dstW; i++) {
382
            int val=1<<18;
383
            int j;
384
            for (j=0; j<lumFilterSize; j++)
385
                val += alpSrc[j][i] * lumFilter[j];
386

    
387
            aDest[i]= av_clip_uint8(val>>19);
388
        }
389

    
390
}
391

    
392
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
393
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
394
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
395
{
396
    //FIXME Optimize (just quickly written not optimized..)
397
    int i;
398
    for (i=0; i<dstW; i++) {
399
        int val=1<<18;
400
        int j;
401
        for (j=0; j<lumFilterSize; j++)
402
            val += lumSrc[j][i] * lumFilter[j];
403

    
404
        dest[i]= av_clip_uint8(val>>19);
405
    }
406

    
407
    if (!uDest)
408
        return;
409

    
410
    if (dstFormat == PIX_FMT_NV12)
411
        for (i=0; i<chrDstW; i++) {
412
            int u=1<<18;
413
            int v=1<<18;
414
            int j;
415
            for (j=0; j<chrFilterSize; j++) {
416
                u += chrSrc[j][i] * chrFilter[j];
417
                v += chrSrc[j][i + VOFW] * chrFilter[j];
418
            }
419

    
420
            uDest[2*i]= av_clip_uint8(u>>19);
421
            uDest[2*i+1]= av_clip_uint8(v>>19);
422
        }
423
    else
424
        for (i=0; i<chrDstW; i++) {
425
            int u=1<<18;
426
            int v=1<<18;
427
            int j;
428
            for (j=0; j<chrFilterSize; j++) {
429
                u += chrSrc[j][i] * chrFilter[j];
430
                v += chrSrc[j][i + VOFW] * chrFilter[j];
431
            }
432

    
433
            uDest[2*i]= av_clip_uint8(v>>19);
434
            uDest[2*i+1]= av_clip_uint8(u>>19);
435
        }
436
}
437

    
438
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
439
    for (i=0; i<(dstW>>1); i++) {\
440
        int j;\
441
        int Y1 = 1<<18;\
442
        int Y2 = 1<<18;\
443
        int U  = 1<<18;\
444
        int V  = 1<<18;\
445
        int av_unused A1, A2;\
446
        type av_unused *r, *b, *g;\
447
        const int i2= 2*i;\
448
        \
449
        for (j=0; j<lumFilterSize; j++) {\
450
            Y1 += lumSrc[j][i2] * lumFilter[j];\
451
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
452
        }\
453
        for (j=0; j<chrFilterSize; j++) {\
454
            U += chrSrc[j][i] * chrFilter[j];\
455
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
456
        }\
457
        Y1>>=19;\
458
        Y2>>=19;\
459
        U >>=19;\
460
        V >>=19;\
461
        if (alpha) {\
462
            A1 = 1<<18;\
463
            A2 = 1<<18;\
464
            for (j=0; j<lumFilterSize; j++) {\
465
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
466
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
467
            }\
468
            A1>>=19;\
469
            A2>>=19;\
470
        }
471

    
472
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
473
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
474
        if ((Y1|Y2|U|V)&256) {\
475
            if (Y1>255)   Y1=255; \
476
            else if (Y1<0)Y1=0;   \
477
            if (Y2>255)   Y2=255; \
478
            else if (Y2<0)Y2=0;   \
479
            if (U>255)    U=255;  \
480
            else if (U<0) U=0;    \
481
            if (V>255)    V=255;  \
482
            else if (V<0) V=0;    \
483
        }\
484
        if (alpha && ((A1|A2)&256)) {\
485
            A1=av_clip_uint8(A1);\
486
            A2=av_clip_uint8(A2);\
487
        }
488

    
489
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
490
    for (i=0; i<dstW; i++) {\
491
        int j;\
492
        int Y = 0;\
493
        int U = -128<<19;\
494
        int V = -128<<19;\
495
        int av_unused A;\
496
        int R,G,B;\
497
        \
498
        for (j=0; j<lumFilterSize; j++) {\
499
            Y += lumSrc[j][i     ] * lumFilter[j];\
500
        }\
501
        for (j=0; j<chrFilterSize; j++) {\
502
            U += chrSrc[j][i     ] * chrFilter[j];\
503
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
504
        }\
505
        Y >>=10;\
506
        U >>=10;\
507
        V >>=10;\
508
        if (alpha) {\
509
            A = rnd;\
510
            for (j=0; j<lumFilterSize; j++)\
511
                A += alpSrc[j][i     ] * lumFilter[j];\
512
            A >>=19;\
513
            if (A&256)\
514
                A = av_clip_uint8(A);\
515
        }
516

    
517
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
518
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
519
        Y-= c->yuv2rgb_y_offset;\
520
        Y*= c->yuv2rgb_y_coeff;\
521
        Y+= rnd;\
522
        R= Y + V*c->yuv2rgb_v2r_coeff;\
523
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
524
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
525
        if ((R|G|B)&(0xC0000000)) {\
526
            if (R>=(256<<22))   R=(256<<22)-1; \
527
            else if (R<0)R=0;   \
528
            if (G>=(256<<22))   G=(256<<22)-1; \
529
            else if (G<0)G=0;   \
530
            if (B>=(256<<22))   B=(256<<22)-1; \
531
            else if (B<0)B=0;   \
532
        }
533

    
534
#define YSCALE_YUV_2_GRAY16_C \
535
    for (i=0; i<(dstW>>1); i++) {\
536
        int j;\
537
        int Y1 = 1<<18;\
538
        int Y2 = 1<<18;\
539
        int U  = 1<<18;\
540
        int V  = 1<<18;\
541
        \
542
        const int i2= 2*i;\
543
        \
544
        for (j=0; j<lumFilterSize; j++) {\
545
            Y1 += lumSrc[j][i2] * lumFilter[j];\
546
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
547
        }\
548
        Y1>>=11;\
549
        Y2>>=11;\
550
        if ((Y1|Y2|U|V)&65536) {\
551
            if (Y1>65535)   Y1=65535; \
552
            else if (Y1<0)Y1=0;   \
553
            if (Y2>65535)   Y2=65535; \
554
            else if (Y2<0)Y2=0;   \
555
        }
556

    
557
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
558
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
559
    r = (type *)c->table_rV[V];   \
560
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
561
    b = (type *)c->table_bU[U];
562

    
563
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
564
    for (i=0; i<(dstW>>1); i++) { \
565
        const int i2= 2*i;       \
566
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
567
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
568
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
569
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
570
        type av_unused *r, *b, *g;                                    \
571
        int av_unused A1, A2;                                         \
572
        if (alpha) {\
573
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
574
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
575
        }
576

    
577
#define YSCALE_YUV_2_GRAY16_2_C   \
578
    for (i=0; i<(dstW>>1); i++) { \
579
        const int i2= 2*i;       \
580
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
581
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
582

    
583
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
584
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
585
    r = (type *)c->table_rV[V];\
586
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
587
    b = (type *)c->table_bU[U];
588

    
589
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
590
    for (i=0; i<(dstW>>1); i++) {\
591
        const int i2= 2*i;\
592
        int Y1= buf0[i2  ]>>7;\
593
        int Y2= buf0[i2+1]>>7;\
594
        int U= (uvbuf1[i     ])>>7;\
595
        int V= (uvbuf1[i+VOFW])>>7;\
596
        type av_unused *r, *b, *g;\
597
        int av_unused A1, A2;\
598
        if (alpha) {\
599
            A1= abuf0[i2  ]>>7;\
600
            A2= abuf0[i2+1]>>7;\
601
        }
602

    
603
#define YSCALE_YUV_2_GRAY16_1_C \
604
    for (i=0; i<(dstW>>1); i++) {\
605
        const int i2= 2*i;\
606
        int Y1= buf0[i2  ]<<1;\
607
        int Y2= buf0[i2+1]<<1;
608

    
609
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
610
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
611
    r = (type *)c->table_rV[V];\
612
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
613
    b = (type *)c->table_bU[U];
614

    
615
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
616
    for (i=0; i<(dstW>>1); i++) {\
617
        const int i2= 2*i;\
618
        int Y1= buf0[i2  ]>>7;\
619
        int Y2= buf0[i2+1]>>7;\
620
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
621
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
622
        type av_unused *r, *b, *g;\
623
        int av_unused A1, A2;\
624
        if (alpha) {\
625
            A1= abuf0[i2  ]>>7;\
626
            A2= abuf0[i2+1]>>7;\
627
        }
628

    
629
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
630
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
631
    r = (type *)c->table_rV[V];\
632
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
633
    b = (type *)c->table_bU[U];
634

    
635
#define YSCALE_YUV_2_MONO2_C \
636
    const uint8_t * const d128=dither_8x8_220[y&7];\
637
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
638
    for (i=0; i<dstW-7; i+=8) {\
639
        int acc;\
640
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
641
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
642
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
643
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
644
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
645
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
646
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
647
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
648
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
649
        dest++;\
650
    }
651

    
652
#define YSCALE_YUV_2_MONOX_C \
653
    const uint8_t * const d128=dither_8x8_220[y&7];\
654
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
655
    int acc=0;\
656
    for (i=0; i<dstW-1; i+=2) {\
657
        int j;\
658
        int Y1=1<<18;\
659
        int Y2=1<<18;\
660
\
661
        for (j=0; j<lumFilterSize; j++) {\
662
            Y1 += lumSrc[j][i] * lumFilter[j];\
663
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
664
        }\
665
        Y1>>=19;\
666
        Y2>>=19;\
667
        if ((Y1|Y2)&256) {\
668
            if (Y1>255)   Y1=255;\
669
            else if (Y1<0)Y1=0;\
670
            if (Y2>255)   Y2=255;\
671
            else if (Y2<0)Y2=0;\
672
        }\
673
        acc+= acc + g[Y1+d128[(i+0)&7]];\
674
        acc+= acc + g[Y2+d128[(i+1)&7]];\
675
        if ((i&7)==6) {\
676
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
677
            dest++;\
678
        }\
679
    }
680

    
681
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
682
    switch(c->dstFormat) {\
683
    case PIX_FMT_RGB48BE:\
684
    case PIX_FMT_RGB48LE:\
685
        func(uint8_t,0)\
686
            ((uint8_t*)dest)[ 0]= r[Y1];\
687
            ((uint8_t*)dest)[ 1]= r[Y1];\
688
            ((uint8_t*)dest)[ 2]= g[Y1];\
689
            ((uint8_t*)dest)[ 3]= g[Y1];\
690
            ((uint8_t*)dest)[ 4]= b[Y1];\
691
            ((uint8_t*)dest)[ 5]= b[Y1];\
692
            ((uint8_t*)dest)[ 6]= r[Y2];\
693
            ((uint8_t*)dest)[ 7]= r[Y2];\
694
            ((uint8_t*)dest)[ 8]= g[Y2];\
695
            ((uint8_t*)dest)[ 9]= g[Y2];\
696
            ((uint8_t*)dest)[10]= b[Y2];\
697
            ((uint8_t*)dest)[11]= b[Y2];\
698
            dest+=12;\
699
        }\
700
        break;\
701
    case PIX_FMT_BGR48BE:\
702
    case PIX_FMT_BGR48LE:\
703
        func(uint8_t,0)\
704
            ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
705
            ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
706
            ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
707
            ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
708
            ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
709
            ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
710
            dest+=12;\
711
        }\
712
        break;\
713
    case PIX_FMT_RGBA:\
714
    case PIX_FMT_BGRA:\
715
        if (CONFIG_SMALL) {\
716
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
717
            func(uint32_t,needAlpha)\
718
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
719
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
720
            }\
721
        } else {\
722
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
723
                func(uint32_t,1)\
724
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
725
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
726
                }\
727
            } else {\
728
                func(uint32_t,0)\
729
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
730
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
731
                }\
732
            }\
733
        }\
734
        break;\
735
    case PIX_FMT_ARGB:\
736
    case PIX_FMT_ABGR:\
737
        if (CONFIG_SMALL) {\
738
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
739
            func(uint32_t,needAlpha)\
740
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
741
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
742
            }\
743
        } else {\
744
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
745
                func(uint32_t,1)\
746
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
747
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
748
                }\
749
            } else {\
750
                func(uint32_t,0)\
751
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
752
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
753
                }\
754
            }\
755
        }                \
756
        break;\
757
    case PIX_FMT_RGB24:\
758
        func(uint8_t,0)\
759
            ((uint8_t*)dest)[0]= r[Y1];\
760
            ((uint8_t*)dest)[1]= g[Y1];\
761
            ((uint8_t*)dest)[2]= b[Y1];\
762
            ((uint8_t*)dest)[3]= r[Y2];\
763
            ((uint8_t*)dest)[4]= g[Y2];\
764
            ((uint8_t*)dest)[5]= b[Y2];\
765
            dest+=6;\
766
        }\
767
        break;\
768
    case PIX_FMT_BGR24:\
769
        func(uint8_t,0)\
770
            ((uint8_t*)dest)[0]= b[Y1];\
771
            ((uint8_t*)dest)[1]= g[Y1];\
772
            ((uint8_t*)dest)[2]= r[Y1];\
773
            ((uint8_t*)dest)[3]= b[Y2];\
774
            ((uint8_t*)dest)[4]= g[Y2];\
775
            ((uint8_t*)dest)[5]= r[Y2];\
776
            dest+=6;\
777
        }\
778
        break;\
779
    case PIX_FMT_RGB565BE:\
780
    case PIX_FMT_RGB565LE:\
781
    case PIX_FMT_BGR565BE:\
782
    case PIX_FMT_BGR565LE:\
783
        {\
784
            const int dr1= dither_2x2_8[y&1    ][0];\
785
            const int dg1= dither_2x2_4[y&1    ][0];\
786
            const int db1= dither_2x2_8[(y&1)^1][0];\
787
            const int dr2= dither_2x2_8[y&1    ][1];\
788
            const int dg2= dither_2x2_4[y&1    ][1];\
789
            const int db2= dither_2x2_8[(y&1)^1][1];\
790
            func(uint16_t,0)\
791
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
792
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
793
            }\
794
        }\
795
        break;\
796
    case PIX_FMT_RGB555BE:\
797
    case PIX_FMT_RGB555LE:\
798
    case PIX_FMT_BGR555BE:\
799
    case PIX_FMT_BGR555LE:\
800
        {\
801
            const int dr1= dither_2x2_8[y&1    ][0];\
802
            const int dg1= dither_2x2_8[y&1    ][1];\
803
            const int db1= dither_2x2_8[(y&1)^1][0];\
804
            const int dr2= dither_2x2_8[y&1    ][1];\
805
            const int dg2= dither_2x2_8[y&1    ][0];\
806
            const int db2= dither_2x2_8[(y&1)^1][1];\
807
            func(uint16_t,0)\
808
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
809
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
810
            }\
811
        }\
812
        break;\
813
    case PIX_FMT_RGB444BE:\
814
    case PIX_FMT_RGB444LE:\
815
    case PIX_FMT_BGR444BE:\
816
    case PIX_FMT_BGR444LE:\
817
        {\
818
            const int dr1= dither_4x4_16[y&3    ][0];\
819
            const int dg1= dither_4x4_16[y&3    ][1];\
820
            const int db1= dither_4x4_16[(y&3)^3][0];\
821
            const int dr2= dither_4x4_16[y&3    ][1];\
822
            const int dg2= dither_4x4_16[y&3    ][0];\
823
            const int db2= dither_4x4_16[(y&3)^3][1];\
824
            func(uint16_t,0)\
825
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
826
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
827
            }\
828
        }\
829
        break;\
830
    case PIX_FMT_RGB8:\
831
    case PIX_FMT_BGR8:\
832
        {\
833
            const uint8_t * const d64= dither_8x8_73[y&7];\
834
            const uint8_t * const d32= dither_8x8_32[y&7];\
835
            func(uint8_t,0)\
836
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
837
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
838
            }\
839
        }\
840
        break;\
841
    case PIX_FMT_RGB4:\
842
    case PIX_FMT_BGR4:\
843
        {\
844
            const uint8_t * const d64= dither_8x8_73 [y&7];\
845
            const uint8_t * const d128=dither_8x8_220[y&7];\
846
            func(uint8_t,0)\
847
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
848
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
849
            }\
850
        }\
851
        break;\
852
    case PIX_FMT_RGB4_BYTE:\
853
    case PIX_FMT_BGR4_BYTE:\
854
        {\
855
            const uint8_t * const d64= dither_8x8_73 [y&7];\
856
            const uint8_t * const d128=dither_8x8_220[y&7];\
857
            func(uint8_t,0)\
858
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
859
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
860
            }\
861
        }\
862
        break;\
863
    case PIX_FMT_MONOBLACK:\
864
    case PIX_FMT_MONOWHITE:\
865
        {\
866
            func_monoblack\
867
        }\
868
        break;\
869
    case PIX_FMT_YUYV422:\
870
        func2\
871
            ((uint8_t*)dest)[2*i2+0]= Y1;\
872
            ((uint8_t*)dest)[2*i2+1]= U;\
873
            ((uint8_t*)dest)[2*i2+2]= Y2;\
874
            ((uint8_t*)dest)[2*i2+3]= V;\
875
        }                \
876
        break;\
877
    case PIX_FMT_UYVY422:\
878
        func2\
879
            ((uint8_t*)dest)[2*i2+0]= U;\
880
            ((uint8_t*)dest)[2*i2+1]= Y1;\
881
            ((uint8_t*)dest)[2*i2+2]= V;\
882
            ((uint8_t*)dest)[2*i2+3]= Y2;\
883
        }                \
884
        break;\
885
    case PIX_FMT_GRAY16BE:\
886
        func_g16\
887
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
888
            ((uint8_t*)dest)[2*i2+1]= Y1;\
889
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
890
            ((uint8_t*)dest)[2*i2+3]= Y2;\
891
        }                \
892
        break;\
893
    case PIX_FMT_GRAY16LE:\
894
        func_g16\
895
            ((uint8_t*)dest)[2*i2+0]= Y1;\
896
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
897
            ((uint8_t*)dest)[2*i2+2]= Y2;\
898
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
899
        }                \
900
        break;\
901
    }
902

    
903
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
904
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
905
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
906
{
907
    int i;
908
    YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
909
}
910

    
911
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
912
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
913
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
914
{
915
    int i;
916
    int step= c->dstFormatBpp/8;
917
    int aidx= 3;
918

    
919
    switch(c->dstFormat) {
920
    case PIX_FMT_ARGB:
921
        dest++;
922
        aidx= 0;
923
    case PIX_FMT_RGB24:
924
        aidx--;
925
    case PIX_FMT_RGBA:
926
        if (CONFIG_SMALL) {
927
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
928
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
929
                dest[aidx]= needAlpha ? A : 255;
930
                dest[0]= R>>22;
931
                dest[1]= G>>22;
932
                dest[2]= B>>22;
933
                dest+= step;
934
            }
935
        } else {
936
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
937
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
938
                    dest[aidx]= A;
939
                    dest[0]= R>>22;
940
                    dest[1]= G>>22;
941
                    dest[2]= B>>22;
942
                    dest+= step;
943
                }
944
            } else {
945
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
946
                    dest[aidx]= 255;
947
                    dest[0]= R>>22;
948
                    dest[1]= G>>22;
949
                    dest[2]= B>>22;
950
                    dest+= step;
951
                }
952
            }
953
        }
954
        break;
955
    case PIX_FMT_ABGR:
956
        dest++;
957
        aidx= 0;
958
    case PIX_FMT_BGR24:
959
        aidx--;
960
    case PIX_FMT_BGRA:
961
        if (CONFIG_SMALL) {
962
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
963
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
964
                dest[aidx]= needAlpha ? A : 255;
965
                dest[0]= B>>22;
966
                dest[1]= G>>22;
967
                dest[2]= R>>22;
968
                dest+= step;
969
            }
970
        } else {
971
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
972
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
973
                    dest[aidx]= A;
974
                    dest[0]= B>>22;
975
                    dest[1]= G>>22;
976
                    dest[2]= R>>22;
977
                    dest+= step;
978
                }
979
            } else {
980
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
981
                    dest[aidx]= 255;
982
                    dest[0]= B>>22;
983
                    dest[1]= G>>22;
984
                    dest[2]= R>>22;
985
                    dest+= step;
986
                }
987
            }
988
        }
989
        break;
990
    default:
991
        assert(0);
992
    }
993
}
994

    
995
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
996
{
997
    int i;
998
    uint8_t *ptr = plane + stride*y;
999
    for (i=0; i<height; i++) {
1000
        memset(ptr, val, width);
1001
        ptr += stride;
1002
    }
1003
}
1004

    
1005
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
1006
                            uint32_t *unused)
1007
{
1008
    int i;
1009
    for (i = 0; i < width; i++) {
1010
        int r = src[i*6+0];
1011
        int g = src[i*6+2];
1012
        int b = src[i*6+4];
1013

    
1014
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1015
    }
1016
}
1017

    
1018
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1019
                             const uint8_t *src1, const uint8_t *src2,
1020
                             long width, uint32_t *unused)
1021
{
1022
    int i;
1023
    assert(src1==src2);
1024
    for (i = 0; i < width; i++) {
1025
        int r = src1[6*i + 0];
1026
        int g = src1[6*i + 2];
1027
        int b = src1[6*i + 4];
1028

    
1029
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1030
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1031
    }
1032
}
1033

    
1034
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1035
                                  const uint8_t *src1, const uint8_t *src2,
1036
                                  long width, uint32_t *unused)
1037
{
1038
    int i;
1039
    assert(src1==src2);
1040
    for (i = 0; i < width; i++) {
1041
        int r= src1[12*i + 0] + src1[12*i + 6];
1042
        int g= src1[12*i + 2] + src1[12*i + 8];
1043
        int b= src1[12*i + 4] + src1[12*i + 10];
1044

    
1045
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1046
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1047
    }
1048
}
1049

    
1050
static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
1051
                            uint32_t *unused)
1052
{
1053
    int i;
1054
    for (i = 0; i < width; i++) {
1055
        int b = src[i*6+0];
1056
        int g = src[i*6+2];
1057
        int r = src[i*6+4];
1058

    
1059
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1060
    }
1061
}
1062

    
1063
static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1064
                             const uint8_t *src1, const uint8_t *src2,
1065
                             long width, uint32_t *unused)
1066
{
1067
    int i;
1068
    for (i = 0; i < width; i++) {
1069
        int b = src1[6*i + 0];
1070
        int g = src1[6*i + 2];
1071
        int r = src1[6*i + 4];
1072

    
1073
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1074
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1075
    }
1076
}
1077

    
1078
static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1079
                                  const uint8_t *src1, const uint8_t *src2,
1080
                                  long width, uint32_t *unused)
1081
{
1082
    int i;
1083
    for (i = 0; i < width; i++) {
1084
        int b= src1[12*i + 0] + src1[12*i + 6];
1085
        int g= src1[12*i + 2] + src1[12*i + 8];
1086
        int r= src1[12*i + 4] + src1[12*i + 10];
1087

    
1088
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1089
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1090
    }
1091
}
1092

    
1093
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1094
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1095
{\
1096
    int i;\
1097
    for (i=0; i<width; i++) {\
1098
        int b= (((const type*)src)[i]>>shb)&maskb;\
1099
        int g= (((const type*)src)[i]>>shg)&maskg;\
1100
        int r= (((const type*)src)[i]>>shr)&maskr;\
1101
\
1102
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1103
    }\
1104
}
1105

    
1106
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1107
BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1108
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1109
BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1110
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1111
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1112
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1113
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1114

    
1115
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1116
{
1117
    int i;
1118
    for (i=0; i<width; i++) {
1119
        dst[i]= src[4*i];
1120
    }
1121
}
1122

    
1123
#define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1124
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1125
{\
1126
    int i;\
1127
    for (i=0; i<width; i++) {\
1128
        int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1129
        int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1130
        int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1131
\
1132
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1133
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1134
    }\
1135
}\
1136
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1137
{\
1138
    int i;\
1139
    for (i=0; i<width; i++) {\
1140
        int pix0= ((const type*)src)[2*i+0]>>shp;\
1141
        int pix1= ((const type*)src)[2*i+1]>>shp;\
1142
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1143
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1144
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1145
        g&= maskg|(2*maskg);\
1146
\
1147
        g>>=shg;\
1148
\
1149
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1150
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1151
    }\
1152
}
1153

    
1154
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1155
BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1156
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1157
BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1158
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1159
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1160
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1161
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1162

    
1163
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1164
{
1165
    int i;
1166
    for (i=0; i<width; i++) {
1167
        int d= src[i];
1168

    
1169
        dst[i]= pal[d] & 0xFF;
1170
    }
1171
}
1172

    
1173
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1174
                           const uint8_t *src1, const uint8_t *src2,
1175
                           long width, uint32_t *pal)
1176
{
1177
    int i;
1178
    assert(src1 == src2);
1179
    for (i=0; i<width; i++) {
1180
        int p= pal[src1[i]];
1181

    
1182
        dstU[i]= p>>8;
1183
        dstV[i]= p>>16;
1184
    }
1185
}
1186

    
1187
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1188
{
1189
    int i, j;
1190
    for (i=0; i<width/8; i++) {
1191
        int d= ~src[i];
1192
        for(j=0; j<8; j++)
1193
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1194
    }
1195
}
1196

    
1197
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1198
{
1199
    int i, j;
1200
    for (i=0; i<width/8; i++) {
1201
        int d= src[i];
1202
        for(j=0; j<8; j++)
1203
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1204
    }
1205
}
1206

    
1207
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1208
//Plain C versions
1209
#if CONFIG_RUNTIME_CPUDETECT
1210
#  define COMPILE_C 1
1211
#  if   ARCH_X86
1212
#    define COMPILE_MMX     1
1213
#    define COMPILE_MMX2    1
1214
#    define COMPILE_3DNOW   1
1215
#  elif ARCH_PPC
1216
#    define COMPILE_ALTIVEC HAVE_ALTIVEC
1217
#  endif
1218
#else /* CONFIG_RUNTIME_CPUDETECT */
1219
#  if   ARCH_X86
1220
#    if   HAVE_MMX2
1221
#      define COMPILE_MMX2  1
1222
#    elif HAVE_AMD3DNOW
1223
#      define COMPILE_3DNOW 1
1224
#    elif HAVE_MMX
1225
#      define COMPILE_MMX   1
1226
#    else
1227
#      define COMPILE_C     1
1228
#    endif
1229
#  elif ARCH_PPC && HAVE_ALTIVEC
1230
#    define COMPILE_ALTIVEC 1
1231
#  else
1232
#    define COMPILE_C       1
1233
#  endif
1234
#endif
1235

    
1236
#ifndef COMPILE_C
1237
#  define COMPILE_C 0
1238
#endif
1239
#ifndef COMPILE_MMX
1240
#  define COMPILE_MMX 0
1241
#endif
1242
#ifndef COMPILE_MMX2
1243
#  define COMPILE_MMX2 0
1244
#endif
1245
#ifndef COMPILE_3DNOW
1246
#  define COMPILE_3DNOW 0
1247
#endif
1248
#ifndef COMPILE_ALTIVEC
1249
#  define COMPILE_ALTIVEC 0
1250
#endif
1251

    
1252
#define COMPILE_TEMPLATE_MMX 0
1253
#define COMPILE_TEMPLATE_MMX2 0
1254
#define COMPILE_TEMPLATE_AMD3DNOW 0
1255
#define COMPILE_TEMPLATE_ALTIVEC 0
1256

    
1257
#if COMPILE_C
1258
#define RENAME(a) a ## _C
1259
#include "swscale_template.c"
1260
#endif
1261

    
1262
#if COMPILE_ALTIVEC
1263
#undef RENAME
1264
#undef COMPILE_TEMPLATE_ALTIVEC
1265
#define COMPILE_TEMPLATE_ALTIVEC 1
1266
#define RENAME(a) a ## _altivec
1267
#include "swscale_template.c"
1268
#endif
1269

    
1270
#if ARCH_X86
1271

    
1272
//MMX versions
1273
#if COMPILE_MMX
1274
#undef RENAME
1275
#undef COMPILE_TEMPLATE_MMX
1276
#undef COMPILE_TEMPLATE_MMX2
1277
#undef COMPILE_TEMPLATE_AMD3DNOW
1278
#define COMPILE_TEMPLATE_MMX 1
1279
#define COMPILE_TEMPLATE_MMX2 0
1280
#define COMPILE_TEMPLATE_AMD3DNOW 0
1281
#define RENAME(a) a ## _MMX
1282
#include "swscale_template.c"
1283
#endif
1284

    
1285
//MMX2 versions
1286
#if COMPILE_MMX2
1287
#undef RENAME
1288
#undef COMPILE_TEMPLATE_MMX
1289
#undef COMPILE_TEMPLATE_MMX2
1290
#undef COMPILE_TEMPLATE_AMD3DNOW
1291
#define COMPILE_TEMPLATE_MMX 1
1292
#define COMPILE_TEMPLATE_MMX2 1
1293
#define COMPILE_TEMPLATE_AMD3DNOW 0
1294
#define RENAME(a) a ## _MMX2
1295
#include "swscale_template.c"
1296
#endif
1297

    
1298
//3DNOW versions
1299
#if COMPILE_3DNOW
1300
#undef RENAME
1301
#undef COMPILE_TEMPLATE_MMX
1302
#undef COMPILE_TEMPLATE_MMX2
1303
#undef COMPILE_TEMPLATE_AMD3DNOW
1304
#define COMPILE_TEMPLATE_MMX 1
1305
#define COMPILE_TEMPLATE_MMX2 0
1306
#define COMPILE_TEMPLATE_AMD3DNOW 1
1307
#define RENAME(a) a ## _3DNow
1308
#include "swscale_template.c"
1309
#endif
1310

    
1311
#endif //ARCH_X86
1312

    
1313
SwsFunc ff_getSwsFunc(SwsContext *c)
1314
{
1315
#if CONFIG_RUNTIME_CPUDETECT
1316
    int flags = c->flags;
1317

    
1318
#if ARCH_X86
1319
    // ordered per speed fastest first
1320
    if (flags & SWS_CPU_CAPS_MMX2) {
1321
        sws_init_swScale_MMX2(c);
1322
        return swScale_MMX2;
1323
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1324
        sws_init_swScale_3DNow(c);
1325
        return swScale_3DNow;
1326
    } else if (flags & SWS_CPU_CAPS_MMX) {
1327
        sws_init_swScale_MMX(c);
1328
        return swScale_MMX;
1329
    } else {
1330
        sws_init_swScale_C(c);
1331
        return swScale_C;
1332
    }
1333

    
1334
#else
1335
#if COMPILE_ALTIVEC
1336
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1337
        sws_init_swScale_altivec(c);
1338
        return swScale_altivec;
1339
    } else {
1340
        sws_init_swScale_C(c);
1341
        return swScale_C;
1342
    }
1343
#endif
1344
    sws_init_swScale_C(c);
1345
    return swScale_C;
1346
#endif /* ARCH_X86 */
1347
#else //CONFIG_RUNTIME_CPUDETECT
1348
#if   COMPILE_TEMPLATE_MMX2
1349
    sws_init_swScale_MMX2(c);
1350
    return swScale_MMX2;
1351
#elif COMPILE_TEMPLATE_AMD3DNOW
1352
    sws_init_swScale_3DNow(c);
1353
    return swScale_3DNow;
1354
#elif COMPILE_TEMPLATE_MMX
1355
    sws_init_swScale_MMX(c);
1356
    return swScale_MMX;
1357
#elif COMPILE_TEMPLATE_ALTIVEC
1358
    sws_init_swScale_altivec(c);
1359
    return swScale_altivec;
1360
#else
1361
    sws_init_swScale_C(c);
1362
    return swScale_C;
1363
#endif
1364
#endif //!CONFIG_RUNTIME_CPUDETECT
1365
}
1366

    
1367
static void copyPlane(const uint8_t *src, int srcStride,
1368
                      int srcSliceY, int srcSliceH, int width,
1369
                      uint8_t *dst, int dstStride)
1370
{
1371
    dst += dstStride * srcSliceY;
1372
    if (dstStride == srcStride && srcStride > 0) {
1373
        memcpy(dst, src, srcSliceH * dstStride);
1374
    } else {
1375
        int i;
1376
        for (i=0; i<srcSliceH; i++) {
1377
            memcpy(dst, src, width);
1378
            src += srcStride;
1379
            dst += dstStride;
1380
        }
1381
    }
1382
}
1383

    
1384
static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1385
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1386
{
1387
    uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1388

    
1389
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1390
              dstParam[0], dstStride[0]);
1391

    
1392
    if (c->dstFormat == PIX_FMT_NV12)
1393
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1394
    else
1395
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1396

    
1397
    return srcSliceH;
1398
}
1399

    
1400
static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1401
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1402
{
1403
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1404

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

    
1407
    return srcSliceH;
1408
}
1409

    
1410
static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1411
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1412
{
1413
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1414

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

    
1417
    return srcSliceH;
1418
}
1419

    
1420
static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1421
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1422
{
1423
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1424

    
1425
    yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1426

    
1427
    return srcSliceH;
1428
}
1429

    
1430
static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1431
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1432
{
1433
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1434

    
1435
    yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1436

    
1437
    return srcSliceH;
1438
}
1439

    
1440
static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1441
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1442
{
1443
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1444
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1445
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1446

    
1447
    yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1448

    
1449
    if (dstParam[3])
1450
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1451

    
1452
    return srcSliceH;
1453
}
1454

    
1455
static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1456
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1457
{
1458
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1459
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1460
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1461

    
1462
    yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1463

    
1464
    return srcSliceH;
1465
}
1466

    
1467
static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1468
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1469
{
1470
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1471
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1472
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1473

    
1474
    uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1475

    
1476
    if (dstParam[3])
1477
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1478

    
1479
    return srcSliceH;
1480
}
1481

    
1482
static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1483
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1484
{
1485
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1486
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1487
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1488

    
1489
    uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1490

    
1491
    return srcSliceH;
1492
}
1493

    
1494
static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1495
{
1496
    long i;
1497
    for (i=0; i<num_pixels; i++)
1498
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1499
}
1500

    
1501
static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1502
{
1503
    long i;
1504

    
1505
    for (i=0; i<num_pixels; i++)
1506
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1507
}
1508

    
1509
static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1510
{
1511
    long i;
1512

    
1513
    for (i=0; i<num_pixels; i++) {
1514
        //FIXME slow?
1515
        dst[0]= palette[src[i<<1]*4+0];
1516
        dst[1]= palette[src[i<<1]*4+1];
1517
        dst[2]= palette[src[i<<1]*4+2];
1518
        dst+= 3;
1519
    }
1520
}
1521

    
1522
static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1523
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1524
{
1525
    const enum PixelFormat srcFormat= c->srcFormat;
1526
    const enum PixelFormat dstFormat= c->dstFormat;
1527
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1528
                 const uint8_t *palette)=NULL;
1529
    int i;
1530
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1531
    const uint8_t *srcPtr= src[0];
1532

    
1533
    if (srcFormat == PIX_FMT_Y400A) {
1534
        switch (dstFormat) {
1535
        case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1536
        case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1537
        case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1538
        case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1539
        case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1540
        case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1541
        }
1542
    } else if (usePal(srcFormat)) {
1543
        switch (dstFormat) {
1544
        case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1545
        case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1546
        case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1547
        case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1548
        case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1549
        case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1550
        }
1551
    }
1552

    
1553
    if (!conv)
1554
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1555
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1556
    else {
1557
        for (i=0; i<srcSliceH; i++) {
1558
            conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1559
            srcPtr+= srcStride[0];
1560
            dstPtr+= dstStride[0];
1561
        }
1562
    }
1563

    
1564
    return srcSliceH;
1565
}
1566

    
1567
#define isRGBA32(x) (            \
1568
           (x) == PIX_FMT_ARGB   \
1569
        || (x) == PIX_FMT_RGBA   \
1570
        || (x) == PIX_FMT_BGRA   \
1571
        || (x) == PIX_FMT_ABGR   \
1572
        )
1573

    
1574
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1575
static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1576
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1577
{
1578
    const enum PixelFormat srcFormat= c->srcFormat;
1579
    const enum PixelFormat dstFormat= c->dstFormat;
1580
    const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1581
    const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1582
    const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1583
    const int dstId= c->dstFormatBpp >> 2;
1584
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1585

    
1586
#define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1587

    
1588
    if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1589
        if (     CONV_IS(ABGR, RGBA)
1590
              || CONV_IS(ARGB, BGRA)
1591
              || CONV_IS(BGRA, ARGB)
1592
              || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1593
        else if (CONV_IS(ABGR, ARGB)
1594
              || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1595
        else if (CONV_IS(ABGR, BGRA)
1596
              || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1597
        else if (CONV_IS(BGRA, RGBA)
1598
              || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1599
        else if (CONV_IS(BGRA, ABGR)
1600
              || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1601
    } else
1602
    /* BGR -> BGR */
1603
    if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1604
       || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1605
        switch(srcId | (dstId<<4)) {
1606
        case 0x34: conv= rgb16to15; break;
1607
        case 0x36: conv= rgb24to15; break;
1608
        case 0x38: conv= rgb32to15; break;
1609
        case 0x43: conv= rgb15to16; break;
1610
        case 0x46: conv= rgb24to16; break;
1611
        case 0x48: conv= rgb32to16; break;
1612
        case 0x63: conv= rgb15to24; break;
1613
        case 0x64: conv= rgb16to24; break;
1614
        case 0x68: conv= rgb32to24; break;
1615
        case 0x83: conv= rgb15to32; break;
1616
        case 0x84: conv= rgb16to32; break;
1617
        case 0x86: conv= rgb24to32; break;
1618
        }
1619
    } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1620
             || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1621
        switch(srcId | (dstId<<4)) {
1622
        case 0x33: conv= rgb15tobgr15; break;
1623
        case 0x34: conv= rgb16tobgr15; break;
1624
        case 0x36: conv= rgb24tobgr15; break;
1625
        case 0x38: conv= rgb32tobgr15; break;
1626
        case 0x43: conv= rgb15tobgr16; break;
1627
        case 0x44: conv= rgb16tobgr16; break;
1628
        case 0x46: conv= rgb24tobgr16; break;
1629
        case 0x48: conv= rgb32tobgr16; break;
1630
        case 0x63: conv= rgb15tobgr24; break;
1631
        case 0x64: conv= rgb16tobgr24; break;
1632
        case 0x66: conv= rgb24tobgr24; break;
1633
        case 0x68: conv= rgb32tobgr24; break;
1634
        case 0x83: conv= rgb15tobgr32; break;
1635
        case 0x84: conv= rgb16tobgr32; break;
1636
        case 0x86: conv= rgb24tobgr32; break;
1637
        }
1638
    }
1639

    
1640
    if (!conv) {
1641
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1642
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1643
    } else {
1644
        const uint8_t *srcPtr= src[0];
1645
              uint8_t *dstPtr= dst[0];
1646
        if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1647
            srcPtr += ALT32_CORR;
1648

    
1649
        if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1650
            dstPtr += ALT32_CORR;
1651

    
1652
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1653
            conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1654
        else {
1655
            int i;
1656
            dstPtr += dstStride[0]*srcSliceY;
1657

    
1658
            for (i=0; i<srcSliceH; i++) {
1659
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
1660
                srcPtr+= srcStride[0];
1661
                dstPtr+= dstStride[0];
1662
            }
1663
        }
1664
    }
1665
    return srcSliceH;
1666
}
1667

    
1668
static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1669
                              int srcSliceH, uint8_t* dst[], int dstStride[])
1670
{
1671
    rgb24toyv12(
1672
        src[0],
1673
        dst[0]+ srcSliceY    *dstStride[0],
1674
        dst[1]+(srcSliceY>>1)*dstStride[1],
1675
        dst[2]+(srcSliceY>>1)*dstStride[2],
1676
        c->srcW, srcSliceH,
1677
        dstStride[0], dstStride[1], srcStride[0]);
1678
    if (dst[3])
1679
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1680
    return srcSliceH;
1681
}
1682

    
1683
static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1684
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1685
{
1686
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1687
              dst[0], dstStride[0]);
1688

    
1689
    planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1690
             srcSliceH >> 2, srcStride[1], dstStride[1]);
1691
    planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1692
             srcSliceH >> 2, srcStride[2], dstStride[2]);
1693
    if (dst[3])
1694
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1695
    return srcSliceH;
1696
}
1697

    
1698
/* unscaled copy like stuff (assumes nearly identical formats) */
1699
static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1700
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1701
{
1702
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1703
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1704
    else {
1705
        int i;
1706
        const uint8_t *srcPtr= src[0];
1707
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1708
        int length=0;
1709

    
1710
        /* universal length finder */
1711
        while(length+c->srcW <= FFABS(dstStride[0])
1712
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1713
        assert(length!=0);
1714

    
1715
        for (i=0; i<srcSliceH; i++) {
1716
            memcpy(dstPtr, srcPtr, length);
1717
            srcPtr+= srcStride[0];
1718
            dstPtr+= dstStride[0];
1719
        }
1720
    }
1721
    return srcSliceH;
1722
}
1723

    
1724
static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1725
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1726
{
1727
    int plane, i, j;
1728
    for (plane=0; plane<4; plane++) {
1729
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1730
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1731
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1732
        const uint8_t *srcPtr= src[plane];
1733
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1734

    
1735
        if (!dst[plane]) continue;
1736
        // ignore palette for GRAY8
1737
        if (plane == 1 && !dst[2]) continue;
1738
        if (!src[plane] || (plane == 1 && !src[2])) {
1739
            if(is16BPS(c->dstFormat))
1740
                length*=2;
1741
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1742
        } else {
1743
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1744
                if (!isBE(c->srcFormat)) srcPtr++;
1745
                for (i=0; i<height; i++) {
1746
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1747
                    srcPtr+= srcStride[plane];
1748
                    dstPtr+= dstStride[plane];
1749
                }
1750
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1751
                for (i=0; i<height; i++) {
1752
                    for (j=0; j<length; j++) {
1753
                        dstPtr[ j<<1   ] = srcPtr[j];
1754
                        dstPtr[(j<<1)+1] = srcPtr[j];
1755
                    }
1756
                    srcPtr+= srcStride[plane];
1757
                    dstPtr+= dstStride[plane];
1758
                }
1759
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1760
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1761

    
1762
                for (i=0; i<height; i++) {
1763
                    for (j=0; j<length; j++)
1764
                        ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1765
                    srcPtr+= srcStride[plane];
1766
                    dstPtr+= dstStride[plane];
1767
                }
1768
            } else if (dstStride[plane] == srcStride[plane] &&
1769
                       srcStride[plane] > 0 && srcStride[plane] == length) {
1770
                memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1771
                       height*dstStride[plane]);
1772
            } else {
1773
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1774
                    length*=2;
1775
                for (i=0; i<height; i++) {
1776
                    memcpy(dstPtr, srcPtr, length);
1777
                    srcPtr+= srcStride[plane];
1778
                    dstPtr+= dstStride[plane];
1779
                }
1780
            }
1781
        }
1782
    }
1783
    return srcSliceH;
1784
}
1785

    
1786
int ff_hardcodedcpuflags(void)
1787
{
1788
    int flags = 0;
1789
#if   COMPILE_TEMPLATE_MMX2
1790
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1791
#elif COMPILE_TEMPLATE_AMD3DNOW
1792
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1793
#elif COMPILE_TEMPLATE_MMX
1794
    flags |= SWS_CPU_CAPS_MMX;
1795
#elif COMPILE_TEMPLATE_ALTIVEC
1796
    flags |= SWS_CPU_CAPS_ALTIVEC;
1797
#elif ARCH_BFIN
1798
    flags |= SWS_CPU_CAPS_BFIN;
1799
#endif
1800
    return flags;
1801
}
1802

    
1803
void ff_get_unscaled_swscale(SwsContext *c)
1804
{
1805
    const enum PixelFormat srcFormat = c->srcFormat;
1806
    const enum PixelFormat dstFormat = c->dstFormat;
1807
    const int flags = c->flags;
1808
    const int dstH = c->dstH;
1809
    int needsDither;
1810

    
1811
    needsDither= isAnyRGB(dstFormat)
1812
        &&  c->dstFormatBpp < 24
1813
        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1814

    
1815
    /* yv12_to_nv12 */
1816
    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1817
        c->swScale= planarToNv12Wrapper;
1818
    }
1819
    /* yuv2bgr */
1820
    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1821
        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1822
        c->swScale= ff_yuv2rgb_get_func_ptr(c);
1823
    }
1824

    
1825
    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1826
        c->swScale= yvu9ToYv12Wrapper;
1827
    }
1828

    
1829
    /* bgr24toYV12 */
1830
    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1831
        c->swScale= bgr24ToYv12Wrapper;
1832

    
1833
    /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1834
    if (   isAnyRGB(srcFormat)
1835
        && isAnyRGB(dstFormat)
1836
        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1837
        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1838
        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1839
        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1840
        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1841
        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1842
        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1843
        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1844
        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1845
        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1846
        && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1847
        && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1848
        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1849
        c->swScale= rgbToRgbWrapper;
1850

    
1851
    if ((usePal(srcFormat) && (
1852
        dstFormat == PIX_FMT_RGB32   ||
1853
        dstFormat == PIX_FMT_RGB32_1 ||
1854
        dstFormat == PIX_FMT_RGB24   ||
1855
        dstFormat == PIX_FMT_BGR32   ||
1856
        dstFormat == PIX_FMT_BGR32_1 ||
1857
        dstFormat == PIX_FMT_BGR24)))
1858
        c->swScale= palToRgbWrapper;
1859

    
1860
    if (srcFormat == PIX_FMT_YUV422P) {
1861
        if (dstFormat == PIX_FMT_YUYV422)
1862
            c->swScale= yuv422pToYuy2Wrapper;
1863
        else if (dstFormat == PIX_FMT_UYVY422)
1864
            c->swScale= yuv422pToUyvyWrapper;
1865
    }
1866

    
1867
    /* LQ converters if -sws 0 or -sws 4*/
1868
    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1869
        /* yv12_to_yuy2 */
1870
        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1871
            if (dstFormat == PIX_FMT_YUYV422)
1872
                c->swScale= planarToYuy2Wrapper;
1873
            else if (dstFormat == PIX_FMT_UYVY422)
1874
                c->swScale= planarToUyvyWrapper;
1875
        }
1876
    }
1877
    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1878
        c->swScale= yuyvToYuv420Wrapper;
1879
    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1880
        c->swScale= uyvyToYuv420Wrapper;
1881
    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1882
        c->swScale= yuyvToYuv422Wrapper;
1883
    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1884
        c->swScale= uyvyToYuv422Wrapper;
1885

    
1886
#if COMPILE_ALTIVEC
1887
    if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1888
        !(c->flags & SWS_BITEXACT) &&
1889
        srcFormat == PIX_FMT_YUV420P) {
1890
        // unscaled YV12 -> packed YUV, we want speed
1891
        if (dstFormat == PIX_FMT_YUYV422)
1892
            c->swScale= yv12toyuy2_unscaled_altivec;
1893
        else if (dstFormat == PIX_FMT_UYVY422)
1894
            c->swScale= yv12touyvy_unscaled_altivec;
1895
    }
1896
#endif
1897

    
1898
    /* simple copy */
1899
    if (  srcFormat == dstFormat
1900
        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1901
        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1902
        || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1903
        || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1904
        || (isGray(dstFormat) && isGray(srcFormat))
1905
        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1906
            && c->chrDstHSubSample == c->chrSrcHSubSample
1907
            && c->chrDstVSubSample == c->chrSrcVSubSample
1908
            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1909
            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1910
    {
1911
        if (isPacked(c->srcFormat))
1912
            c->swScale= packedCopyWrapper;
1913
        else /* Planar YUV or gray */
1914
            c->swScale= planarCopyWrapper;
1915
    }
1916
#if ARCH_BFIN
1917
    if (flags & SWS_CPU_CAPS_BFIN)
1918
        ff_bfin_get_unscaled_swscale (c);
1919
#endif
1920
}
1921

    
1922
static void reset_ptr(const uint8_t* src[], int format)
1923
{
1924
    if(!isALPHA(format))
1925
        src[3]=NULL;
1926
    if(!isPlanarYUV(format)) {
1927
        src[3]=src[2]=NULL;
1928

    
1929
        if (!usePal(format))
1930
            src[1]= NULL;
1931
    }
1932
}
1933

    
1934
static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1935
                                const int linesizes[4])
1936
{
1937
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1938
    int i;
1939

    
1940
    for (i = 0; i < 4; i++) {
1941
        int plane = desc->comp[i].plane;
1942
        if (!data[plane] || !linesizes[plane])
1943
            return 0;
1944
    }
1945

    
1946
    return 1;
1947
}
1948

    
1949
/**
1950
 * swscale wrapper, so we don't need to export the SwsContext.
1951
 * Assumes planar YUV to be in YUV order instead of YVU.
1952
 */
1953
int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1954
              int srcSliceH, uint8_t* const dst[], const int dstStride[])
1955
{
1956
    int i;
1957
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1958
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1959

    
1960
    // do not mess up sliceDir if we have a "trailing" 0-size slice
1961
    if (srcSliceH == 0)
1962
        return 0;
1963

    
1964
    if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1965
        av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1966
        return 0;
1967
    }
1968
    if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1969
        av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1970
        return 0;
1971
    }
1972

    
1973
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1974
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1975
        return 0;
1976
    }
1977
    if (c->sliceDir == 0) {
1978
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1979
    }
1980

    
1981
    if (usePal(c->srcFormat)) {
1982
        for (i=0; i<256; i++) {
1983
            int p, r, g, b,y,u,v;
1984
            if(c->srcFormat == PIX_FMT_PAL8) {
1985
                p=((const uint32_t*)(src[1]))[i];
1986
                r= (p>>16)&0xFF;
1987
                g= (p>> 8)&0xFF;
1988
                b=  p     &0xFF;
1989
            } else if(c->srcFormat == PIX_FMT_RGB8) {
1990
                r= (i>>5    )*36;
1991
                g= ((i>>2)&7)*36;
1992
                b= (i&3     )*85;
1993
            } else if(c->srcFormat == PIX_FMT_BGR8) {
1994
                b= (i>>6    )*85;
1995
                g= ((i>>3)&7)*36;
1996
                r= (i&7     )*36;
1997
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1998
                r= (i>>3    )*255;
1999
                g= ((i>>1)&3)*85;
2000
                b= (i&1     )*255;
2001
            } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
2002
                r = g = b = i;
2003
            } else {
2004
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2005
                b= (i>>3    )*255;
2006
                g= ((i>>1)&3)*85;
2007
                r= (i&1     )*255;
2008
            }
2009
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2010
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2011
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2012
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
2013

    
2014
            switch(c->dstFormat) {
2015
            case PIX_FMT_BGR32:
2016
#if !HAVE_BIGENDIAN
2017
            case PIX_FMT_RGB24:
2018
#endif
2019
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2020
                break;
2021
            case PIX_FMT_BGR32_1:
2022
#if HAVE_BIGENDIAN
2023
            case PIX_FMT_BGR24:
2024
#endif
2025
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2026
                break;
2027
            case PIX_FMT_RGB32_1:
2028
#if HAVE_BIGENDIAN
2029
            case PIX_FMT_RGB24:
2030
#endif
2031
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2032
                break;
2033
            case PIX_FMT_RGB32:
2034
#if !HAVE_BIGENDIAN
2035
            case PIX_FMT_BGR24:
2036
#endif
2037
            default:
2038
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
2039
            }
2040
        }
2041
    }
2042

    
2043
    // copy strides, so they can safely be modified
2044
    if (c->sliceDir == 1) {
2045
        // slices go from top to bottom
2046
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2047
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2048

    
2049
        reset_ptr(src2, c->srcFormat);
2050
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2051

    
2052
        /* reset slice direction at end of frame */
2053
        if (srcSliceY + srcSliceH == c->srcH)
2054
            c->sliceDir = 0;
2055

    
2056
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2057
    } else {
2058
        // slices go from bottom to top => we flip the image internally
2059
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2060
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2061

    
2062
        src2[0] += (srcSliceH-1)*srcStride[0];
2063
        if (!usePal(c->srcFormat))
2064
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2065
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2066
        src2[3] += (srcSliceH-1)*srcStride[3];
2067
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
2068
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2069
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2070
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
2071

    
2072
        reset_ptr(src2, c->srcFormat);
2073
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2074

    
2075
        /* reset slice direction at end of frame */
2076
        if (!srcSliceY)
2077
            c->sliceDir = 0;
2078

    
2079
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2080
    }
2081
}
2082

    
2083
#if LIBSWSCALE_VERSION_MAJOR < 1
2084
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2085
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2086
{
2087
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2088
}
2089
#endif
2090

    
2091
/* Convert the palette to the same packed 32-bit format as the palette */
2092
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2093
{
2094
    long i;
2095

    
2096
    for (i=0; i<num_pixels; i++)
2097
        ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2098
}
2099

    
2100
/* Palette format: ABCD -> dst format: ABC */
2101
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2102
{
2103
    long i;
2104

    
2105
    for (i=0; i<num_pixels; i++) {
2106
        //FIXME slow?
2107
        dst[0]= palette[src[i]*4+0];
2108
        dst[1]= palette[src[i]*4+1];
2109
        dst[2]= palette[src[i]*4+2];
2110
        dst+= 3;
2111
    }
2112
}