Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ dbfdb288

History | View | Annotate | Download (75.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 isPacked(x)         (       \
80
           (x)==PIX_FMT_PAL8        \
81
        || (x)==PIX_FMT_YUYV422     \
82
        || (x)==PIX_FMT_UYVY422     \
83
        || (x)==PIX_FMT_GRAY8A       \
84
        || isAnyRGB(x)              \
85
    )
86

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

    
98
static const double rgb2yuv_table[8][9]={
99
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
100
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
101
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
102
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
103
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
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.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
106
    {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
107
};
108

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

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

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

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

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

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

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

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

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

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

    
177
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
178

    
179
#endif /* ARCH_X86 */
180

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
361
        dest[i]= av_clip_uint8(val>>19);
362
    }
363

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

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

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

    
385
            aDest[i]= av_clip_uint8(val>>19);
386
        }
387

    
388
}
389

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

    
402
        dest[i]= av_clip_uint8(val>>19);
403
    }
404

    
405
    if (!uDest)
406
        return;
407

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
901
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
902
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
903
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
904
{
905
    int i;
906
    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)
907
}
908

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1167
        dst[i]= pal[d] & 0xFF;
1168
    }
1169
}
1170

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

    
1180
        dstU[i]= p>>8;
1181
        dstV[i]= p>>16;
1182
    }
1183
}
1184

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

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

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

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

    
1250
#define COMPILE_TEMPLATE_MMX 0
1251
#define COMPILE_TEMPLATE_MMX2 0
1252
#define COMPILE_TEMPLATE_AMD3DNOW 0
1253
#define COMPILE_TEMPLATE_ALTIVEC 0
1254

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

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

    
1268
#if ARCH_X86
1269

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

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

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

    
1309
#endif //ARCH_X86
1310

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

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

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

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

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

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

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

    
1395
    return srcSliceH;
1396
}
1397

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

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

    
1405
    return srcSliceH;
1406
}
1407

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

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

    
1415
    return srcSliceH;
1416
}
1417

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

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

    
1425
    return srcSliceH;
1426
}
1427

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

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

    
1435
    return srcSliceH;
1436
}
1437

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

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

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

    
1450
    return srcSliceH;
1451
}
1452

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

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

    
1462
    return srcSliceH;
1463
}
1464

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

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

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

    
1477
    return srcSliceH;
1478
}
1479

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

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

    
1489
    return srcSliceH;
1490
}
1491

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

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

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

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

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

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

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

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

    
1562
    return srcSliceH;
1563
}
1564

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

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

    
1584
#define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1585

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

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

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

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

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

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

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

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

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

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

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

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

    
1733
        if (!dst[plane]) continue;
1734
        // ignore palette for GRAY8
1735
        if (plane == 1 && !dst[2]) continue;
1736
        if (!src[plane] || (plane == 1 && !src[2])) {
1737
            if(is16BPS(c->dstFormat))
1738
                length*=2;
1739
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1740
        } else {
1741
            if(isNBPS(c->srcFormat)) {
1742
                const int depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1743
                uint16_t *srcPtr2 = (uint16_t*)srcPtr;
1744

    
1745
                if (is16BPS(c->dstFormat)) {
1746
                    uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1747
                    for (i = 0; i < height; i++) {
1748
                        for (j = 0; j < length; j++)
1749
                            dstPtr2[j] = (srcPtr2[j]<<(16-depth)) | (srcPtr2[j]>>(2*depth-16));
1750
                        dstPtr2 += dstStride[plane]/2;
1751
                        srcPtr2 += srcStride[plane]/2;
1752
                    }
1753
                } else {
1754
                    // FIXME Maybe dither instead.
1755
                    for (i = 0; i < height; i++) {
1756
                        for (j = 0; j < length; j++)
1757
                            dstPtr[j] = srcPtr2[j]>>(depth-8);
1758
                        dstPtr  += dstStride[plane];
1759
                        srcPtr2 += srcStride[plane]/2;
1760
                    }
1761
                }
1762
            } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1763
                if (!isBE(c->srcFormat)) srcPtr++;
1764
                for (i=0; i<height; i++) {
1765
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1766
                    srcPtr+= srcStride[plane];
1767
                    dstPtr+= dstStride[plane];
1768
                }
1769
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1770
                for (i=0; i<height; i++) {
1771
                    for (j=0; j<length; j++) {
1772
                        dstPtr[ j<<1   ] = srcPtr[j];
1773
                        dstPtr[(j<<1)+1] = srcPtr[j];
1774
                    }
1775
                    srcPtr+= srcStride[plane];
1776
                    dstPtr+= dstStride[plane];
1777
                }
1778
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1779
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1780

    
1781
                for (i=0; i<height; i++) {
1782
                    for (j=0; j<length; j++)
1783
                        ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1784
                    srcPtr+= srcStride[plane];
1785
                    dstPtr+= dstStride[plane];
1786
                }
1787
            } else if (dstStride[plane] == srcStride[plane] &&
1788
                       srcStride[plane] > 0 && srcStride[plane] == length) {
1789
                memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1790
                       height*dstStride[plane]);
1791
            } else {
1792
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1793
                    length*=2;
1794
                for (i=0; i<height; i++) {
1795
                    memcpy(dstPtr, srcPtr, length);
1796
                    srcPtr+= srcStride[plane];
1797
                    dstPtr+= dstStride[plane];
1798
                }
1799
            }
1800
        }
1801
    }
1802
    return srcSliceH;
1803
}
1804

    
1805
int ff_hardcodedcpuflags(void)
1806
{
1807
    int flags = 0;
1808
#if   COMPILE_TEMPLATE_MMX2
1809
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1810
#elif COMPILE_TEMPLATE_AMD3DNOW
1811
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1812
#elif COMPILE_TEMPLATE_MMX
1813
    flags |= SWS_CPU_CAPS_MMX;
1814
#elif COMPILE_TEMPLATE_ALTIVEC
1815
    flags |= SWS_CPU_CAPS_ALTIVEC;
1816
#elif ARCH_BFIN
1817
    flags |= SWS_CPU_CAPS_BFIN;
1818
#endif
1819
    return flags;
1820
}
1821

    
1822
void ff_get_unscaled_swscale(SwsContext *c)
1823
{
1824
    const enum PixelFormat srcFormat = c->srcFormat;
1825
    const enum PixelFormat dstFormat = c->dstFormat;
1826
    const int flags = c->flags;
1827
    const int dstH = c->dstH;
1828
    int needsDither;
1829

    
1830
    needsDither= isAnyRGB(dstFormat)
1831
        &&  c->dstFormatBpp < 24
1832
        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1833

    
1834
    /* yv12_to_nv12 */
1835
    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1836
        c->swScale= planarToNv12Wrapper;
1837
    }
1838
    /* yuv2bgr */
1839
    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1840
        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1841
        c->swScale= ff_yuv2rgb_get_func_ptr(c);
1842
    }
1843

    
1844
    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1845
        c->swScale= yvu9ToYv12Wrapper;
1846
    }
1847

    
1848
    /* bgr24toYV12 */
1849
    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1850
        c->swScale= bgr24ToYv12Wrapper;
1851

    
1852
    /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1853
    if (   isAnyRGB(srcFormat)
1854
        && isAnyRGB(dstFormat)
1855
        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1856
        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1857
        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1858
        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1859
        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1860
        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1861
        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1862
        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1863
        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1864
        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1865
        && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1866
        && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1867
        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1868
        c->swScale= rgbToRgbWrapper;
1869

    
1870
    if ((usePal(srcFormat) && (
1871
        dstFormat == PIX_FMT_RGB32   ||
1872
        dstFormat == PIX_FMT_RGB32_1 ||
1873
        dstFormat == PIX_FMT_RGB24   ||
1874
        dstFormat == PIX_FMT_BGR32   ||
1875
        dstFormat == PIX_FMT_BGR32_1 ||
1876
        dstFormat == PIX_FMT_BGR24)))
1877
        c->swScale= palToRgbWrapper;
1878

    
1879
    if (srcFormat == PIX_FMT_YUV422P) {
1880
        if (dstFormat == PIX_FMT_YUYV422)
1881
            c->swScale= yuv422pToYuy2Wrapper;
1882
        else if (dstFormat == PIX_FMT_UYVY422)
1883
            c->swScale= yuv422pToUyvyWrapper;
1884
    }
1885

    
1886
    /* LQ converters if -sws 0 or -sws 4*/
1887
    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1888
        /* yv12_to_yuy2 */
1889
        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1890
            if (dstFormat == PIX_FMT_YUYV422)
1891
                c->swScale= planarToYuy2Wrapper;
1892
            else if (dstFormat == PIX_FMT_UYVY422)
1893
                c->swScale= planarToUyvyWrapper;
1894
        }
1895
    }
1896
    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1897
        c->swScale= yuyvToYuv420Wrapper;
1898
    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1899
        c->swScale= uyvyToYuv420Wrapper;
1900
    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1901
        c->swScale= yuyvToYuv422Wrapper;
1902
    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1903
        c->swScale= uyvyToYuv422Wrapper;
1904

    
1905
#if COMPILE_ALTIVEC
1906
    if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1907
        !(c->flags & SWS_BITEXACT) &&
1908
        srcFormat == PIX_FMT_YUV420P) {
1909
        // unscaled YV12 -> packed YUV, we want speed
1910
        if (dstFormat == PIX_FMT_YUYV422)
1911
            c->swScale= yv12toyuy2_unscaled_altivec;
1912
        else if (dstFormat == PIX_FMT_UYVY422)
1913
            c->swScale= yv12touyvy_unscaled_altivec;
1914
    }
1915
#endif
1916

    
1917
    /* simple copy */
1918
    if (  srcFormat == dstFormat
1919
        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1920
        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1921
        || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1922
        || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1923
        || (isGray(dstFormat) && isGray(srcFormat))
1924
        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1925
            && c->chrDstHSubSample == c->chrSrcHSubSample
1926
            && c->chrDstVSubSample == c->chrSrcVSubSample
1927
            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1928
            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1929
    {
1930
        if (isPacked(c->srcFormat))
1931
            c->swScale= packedCopyWrapper;
1932
        else /* Planar YUV or gray */
1933
            c->swScale= planarCopyWrapper;
1934
    }
1935
#if ARCH_BFIN
1936
    if (flags & SWS_CPU_CAPS_BFIN)
1937
        ff_bfin_get_unscaled_swscale (c);
1938
#endif
1939
}
1940

    
1941
static void reset_ptr(const uint8_t* src[], int format)
1942
{
1943
    if(!isALPHA(format))
1944
        src[3]=NULL;
1945
    if(!isPlanarYUV(format)) {
1946
        src[3]=src[2]=NULL;
1947

    
1948
        if (!usePal(format))
1949
            src[1]= NULL;
1950
    }
1951
}
1952

    
1953
static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1954
                                const int linesizes[4])
1955
{
1956
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1957
    int i;
1958

    
1959
    for (i = 0; i < 4; i++) {
1960
        int plane = desc->comp[i].plane;
1961
        if (!data[plane] || !linesizes[plane])
1962
            return 0;
1963
    }
1964

    
1965
    return 1;
1966
}
1967

    
1968
/**
1969
 * swscale wrapper, so we don't need to export the SwsContext.
1970
 * Assumes planar YUV to be in YUV order instead of YVU.
1971
 */
1972
int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1973
              int srcSliceH, uint8_t* const dst[], const int dstStride[])
1974
{
1975
    int i;
1976
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1977
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1978

    
1979
    // do not mess up sliceDir if we have a "trailing" 0-size slice
1980
    if (srcSliceH == 0)
1981
        return 0;
1982

    
1983
    if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1984
        av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1985
        return 0;
1986
    }
1987
    if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1988
        av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1989
        return 0;
1990
    }
1991

    
1992
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1993
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1994
        return 0;
1995
    }
1996
    if (c->sliceDir == 0) {
1997
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1998
    }
1999

    
2000
    if (usePal(c->srcFormat)) {
2001
        for (i=0; i<256; i++) {
2002
            int p, r, g, b, y, u, v, a = 0xff;
2003
            if(c->srcFormat == PIX_FMT_PAL8) {
2004
                p=((const uint32_t*)(src[1]))[i];
2005
                r= (p>>16)&0xFF;
2006
                g= (p>> 8)&0xFF;
2007
                b=  p     &0xFF;
2008
            } else if(c->srcFormat == PIX_FMT_RGB8) {
2009
                r= (i>>5    )*36;
2010
                g= ((i>>2)&7)*36;
2011
                b= (i&3     )*85;
2012
            } else if(c->srcFormat == PIX_FMT_BGR8) {
2013
                b= (i>>6    )*85;
2014
                g= ((i>>3)&7)*36;
2015
                r= (i&7     )*36;
2016
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
2017
                r= (i>>3    )*255;
2018
                g= ((i>>1)&3)*85;
2019
                b= (i&1     )*255;
2020
            } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
2021
                r = g = b = i;
2022
            } else {
2023
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2024
                b= (i>>3    )*255;
2025
                g= ((i>>1)&3)*85;
2026
                r= (i&1     )*255;
2027
            }
2028
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2029
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2030
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2031
            c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
2032

    
2033
            switch(c->dstFormat) {
2034
            case PIX_FMT_BGR32:
2035
#if !HAVE_BIGENDIAN
2036
            case PIX_FMT_RGB24:
2037
#endif
2038
                c->pal_rgb[i]=  r + (g<<8) + (b<<16) + (a<<24);
2039
                break;
2040
            case PIX_FMT_BGR32_1:
2041
#if HAVE_BIGENDIAN
2042
            case PIX_FMT_BGR24:
2043
#endif
2044
                c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
2045
                break;
2046
            case PIX_FMT_RGB32_1:
2047
#if HAVE_BIGENDIAN
2048
            case PIX_FMT_RGB24:
2049
#endif
2050
                c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
2051
                break;
2052
            case PIX_FMT_RGB32:
2053
#if !HAVE_BIGENDIAN
2054
            case PIX_FMT_BGR24:
2055
#endif
2056
            default:
2057
                c->pal_rgb[i]=  b + (g<<8) + (r<<16) + (a<<24);
2058
            }
2059
        }
2060
    }
2061

    
2062
    // copy strides, so they can safely be modified
2063
    if (c->sliceDir == 1) {
2064
        // slices go from top to bottom
2065
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2066
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2067

    
2068
        reset_ptr(src2, c->srcFormat);
2069
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2070

    
2071
        /* reset slice direction at end of frame */
2072
        if (srcSliceY + srcSliceH == c->srcH)
2073
            c->sliceDir = 0;
2074

    
2075
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2076
    } else {
2077
        // slices go from bottom to top => we flip the image internally
2078
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2079
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2080

    
2081
        src2[0] += (srcSliceH-1)*srcStride[0];
2082
        if (!usePal(c->srcFormat))
2083
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2084
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2085
        src2[3] += (srcSliceH-1)*srcStride[3];
2086
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
2087
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2088
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2089
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
2090

    
2091
        reset_ptr(src2, c->srcFormat);
2092
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2093

    
2094
        /* reset slice direction at end of frame */
2095
        if (!srcSliceY)
2096
            c->sliceDir = 0;
2097

    
2098
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2099
    }
2100
}
2101

    
2102
#if LIBSWSCALE_VERSION_MAJOR < 1
2103
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2104
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2105
{
2106
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2107
}
2108
#endif
2109

    
2110
/* Convert the palette to the same packed 32-bit format as the palette */
2111
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2112
{
2113
    long i;
2114

    
2115
    for (i=0; i<num_pixels; i++)
2116
        ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2117
}
2118

    
2119
/* Palette format: ABCD -> dst format: ABC */
2120
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2121
{
2122
    long i;
2123

    
2124
    for (i=0; i<num_pixels; i++) {
2125
        //FIXME slow?
2126
        dst[0]= palette[src[i]*4+0];
2127
        dst[1]= palette[src[i]*4+1];
2128
        dst[2]= palette[src[i]*4+2];
2129
        dst+= 3;
2130
    }
2131
}