Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 33a0421b

History | View | Annotate | Download (69 KB)

1
/*
2
 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav 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
 * Libav 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 Libav; 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_Y400A       \
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
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
125
{  1,   3,   1,   3,   1,   3,   1,   3, },
126
{  2,   0,   2,   0,   2,   0,   2,   0, },
127
};
128

    
129
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
130
{  6,   2,   6,   2,   6,   2,   6,   2, },
131
{  0,   4,   0,   4,   0,   4,   0,   4, },
132
};
133

    
134
DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
135
{  8,   4,  11,   7,   8,   4,  11,   7, },
136
{  2,  14,   1,  13,   2,  14,   1,  13, },
137
{ 10,   6,   9,   5,  10,   6,   9,   5, },
138
{  0,  12,   3,  15,   0,  12,   3,  15, },
139
};
140

    
141
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
142
{ 17,   9,  23,  15,  16,   8,  22,  14, },
143
{  5,  29,   3,  27,   4,  28,   2,  26, },
144
{ 21,  13,  19,  11,  20,  12,  18,  10, },
145
{  0,  24,   6,  30,   1,  25,   7,  31, },
146
{ 16,   8,  22,  14,  17,   9,  23,  15, },
147
{  4,  28,   2,  26,   5,  29,   3,  27, },
148
{ 20,  12,  18,  10,  21,  13,  19,  11, },
149
{  1,  25,   7,  31,   0,  24,   6,  30, },
150
};
151

    
152
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
153
{  0,  55,  14,  68,   3,  58,  17,  72, },
154
{ 37,  18,  50,  32,  40,  22,  54,  35, },
155
{  9,  64,   5,  59,  13,  67,   8,  63, },
156
{ 46,  27,  41,  23,  49,  31,  44,  26, },
157
{  2,  57,  16,  71,   1,  56,  15,  70, },
158
{ 39,  21,  52,  34,  38,  19,  51,  33, },
159
{ 11,  66,   7,  62,  10,  65,   6,  60, },
160
{ 48,  30,  43,  25,  47,  29,  42,  24, },
161
};
162

    
163
#if 1
164
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
165
{117,  62, 158, 103, 113,  58, 155, 100, },
166
{ 34, 199,  21, 186,  31, 196,  17, 182, },
167
{144,  89, 131,  76, 141,  86, 127,  72, },
168
{  0, 165,  41, 206,  10, 175,  52, 217, },
169
{110,  55, 151,  96, 120,  65, 162, 107, },
170
{ 28, 193,  14, 179,  38, 203,  24, 189, },
171
{138,  83, 124,  69, 148,  93, 134,  79, },
172
{  7, 172,  48, 213,   3, 168,  45, 210, },
173
};
174
#elif 1
175
// tries to correct a gamma of 1.5
176
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
177
{  0, 143,  18, 200,   2, 156,  25, 215, },
178
{ 78,  28, 125,  64,  89,  36, 138,  74, },
179
{ 10, 180,   3, 161,  16, 195,   8, 175, },
180
{109,  51,  93,  38, 121,  60, 105,  47, },
181
{  1, 152,  23, 210,   0, 147,  20, 205, },
182
{ 85,  33, 134,  71,  81,  30, 130,  67, },
183
{ 14, 190,   6, 171,  12, 185,   5, 166, },
184
{117,  57, 101,  44, 113,  54,  97,  41, },
185
};
186
#elif 1
187
// tries to correct a gamma of 2.0
188
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
189
{  0, 124,   8, 193,   0, 140,  12, 213, },
190
{ 55,  14, 104,  42,  66,  19, 119,  52, },
191
{  3, 168,   1, 145,   6, 187,   3, 162, },
192
{ 86,  31,  70,  21,  99,  39,  82,  28, },
193
{  0, 134,  11, 206,   0, 129,   9, 200, },
194
{ 62,  17, 114,  48,  58,  16, 109,  45, },
195
{  5, 181,   2, 157,   4, 175,   1, 151, },
196
{ 95,  36,  78,  26,  90,  34,  74,  24, },
197
};
198
#else
199
// tries to correct a gamma of 2.5
200
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
201
{  0, 107,   3, 187,   0, 125,   6, 212, },
202
{ 39,   7,  86,  28,  49,  11, 102,  36, },
203
{  1, 158,   0, 131,   3, 180,   1, 151, },
204
{ 68,  19,  52,  12,  81,  25,  64,  17, },
205
{  0, 119,   5, 203,   0, 113,   4, 195, },
206
{ 45,   9,  96,  33,  42,   8,  91,  30, },
207
{  2, 172,   1, 144,   2, 165,   0, 137, },
208
{ 77,  23,  60,  15,  72,  21,  56,  14, },
209
};
210
#endif
211

    
212
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
213
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
214
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
215
                                                    int dstW, int chrDstW, int big_endian)
216
{
217
    //FIXME Optimize (just quickly written not optimized..)
218
    int i;
219

    
220
    for (i = 0; i < dstW; i++) {
221
        int val = 1 << 10;
222
        int j;
223

    
224
        for (j = 0; j < lumFilterSize; j++)
225
            val += lumSrc[j][i] * lumFilter[j];
226

    
227
        if (big_endian) {
228
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
229
        } else {
230
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
231
        }
232
    }
233

    
234
    if (uDest) {
235
        for (i = 0; i < chrDstW; i++) {
236
            int u = 1 << 10;
237
            int v = 1 << 10;
238
            int j;
239

    
240
            for (j = 0; j < chrFilterSize; j++) {
241
                u += chrSrc[j][i       ] * chrFilter[j];
242
                v += chrSrc[j][i + VOFW] * chrFilter[j];
243
            }
244

    
245
            if (big_endian) {
246
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
247
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
248
            } else {
249
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
250
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
251
            }
252
        }
253
    }
254

    
255
    if (CONFIG_SWSCALE_ALPHA && aDest) {
256
        for (i = 0; i < dstW; i++) {
257
            int val = 1 << 10;
258
            int j;
259

    
260
            for (j = 0; j < lumFilterSize; j++)
261
                val += alpSrc[j][i] * lumFilter[j];
262

    
263
            if (big_endian) {
264
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
265
            } else {
266
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
267
            }
268
        }
269
    }
270
}
271

    
272
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
273
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
274
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
275
                                 enum PixelFormat dstFormat)
276
{
277
    if (isBE(dstFormat)) {
278
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
279
                               chrFilter, chrSrc, chrFilterSize,
280
                               alpSrc,
281
                               dest, uDest, vDest, aDest,
282
                               dstW, chrDstW, 1);
283
    } else {
284
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
285
                               chrFilter, chrSrc, chrFilterSize,
286
                               alpSrc,
287
                               dest, uDest, vDest, aDest,
288
                               dstW, chrDstW, 0);
289
    }
290
}
291

    
292
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
293
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
294
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
295
{
296
    //FIXME Optimize (just quickly written not optimized..)
297
    int i;
298
    for (i=0; i<dstW; i++) {
299
        int val=1<<18;
300
        int j;
301
        for (j=0; j<lumFilterSize; j++)
302
            val += lumSrc[j][i] * lumFilter[j];
303

    
304
        dest[i]= av_clip_uint8(val>>19);
305
    }
306

    
307
    if (uDest)
308
        for (i=0; i<chrDstW; i++) {
309
            int u=1<<18;
310
            int v=1<<18;
311
            int j;
312
            for (j=0; j<chrFilterSize; j++) {
313
                u += chrSrc[j][i] * chrFilter[j];
314
                v += chrSrc[j][i + VOFW] * chrFilter[j];
315
            }
316

    
317
            uDest[i]= av_clip_uint8(u>>19);
318
            vDest[i]= av_clip_uint8(v>>19);
319
        }
320

    
321
    if (CONFIG_SWSCALE_ALPHA && aDest)
322
        for (i=0; i<dstW; i++) {
323
            int val=1<<18;
324
            int j;
325
            for (j=0; j<lumFilterSize; j++)
326
                val += alpSrc[j][i] * lumFilter[j];
327

    
328
            aDest[i]= av_clip_uint8(val>>19);
329
        }
330

    
331
}
332

    
333
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
334
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
335
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
336
{
337
    //FIXME Optimize (just quickly written not optimized..)
338
    int i;
339
    for (i=0; i<dstW; i++) {
340
        int val=1<<18;
341
        int j;
342
        for (j=0; j<lumFilterSize; j++)
343
            val += lumSrc[j][i] * lumFilter[j];
344

    
345
        dest[i]= av_clip_uint8(val>>19);
346
    }
347

    
348
    if (!uDest)
349
        return;
350

    
351
    if (dstFormat == PIX_FMT_NV12)
352
        for (i=0; i<chrDstW; i++) {
353
            int u=1<<18;
354
            int v=1<<18;
355
            int j;
356
            for (j=0; j<chrFilterSize; j++) {
357
                u += chrSrc[j][i] * chrFilter[j];
358
                v += chrSrc[j][i + VOFW] * chrFilter[j];
359
            }
360

    
361
            uDest[2*i]= av_clip_uint8(u>>19);
362
            uDest[2*i+1]= av_clip_uint8(v>>19);
363
        }
364
    else
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[2*i]= av_clip_uint8(v>>19);
375
            uDest[2*i+1]= av_clip_uint8(u>>19);
376
        }
377
}
378

    
379
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
380
    for (i=0; i<(dstW>>1); i++) {\
381
        int j;\
382
        int Y1 = 1<<18;\
383
        int Y2 = 1<<18;\
384
        int U  = 1<<18;\
385
        int V  = 1<<18;\
386
        int av_unused A1, A2;\
387
        type av_unused *r, *b, *g;\
388
        const int i2= 2*i;\
389
        \
390
        for (j=0; j<lumFilterSize; j++) {\
391
            Y1 += lumSrc[j][i2] * lumFilter[j];\
392
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
393
        }\
394
        for (j=0; j<chrFilterSize; j++) {\
395
            U += chrSrc[j][i] * chrFilter[j];\
396
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
397
        }\
398
        Y1>>=19;\
399
        Y2>>=19;\
400
        U >>=19;\
401
        V >>=19;\
402
        if (alpha) {\
403
            A1 = 1<<18;\
404
            A2 = 1<<18;\
405
            for (j=0; j<lumFilterSize; j++) {\
406
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
407
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
408
            }\
409
            A1>>=19;\
410
            A2>>=19;\
411
        }
412

    
413
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
414
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
415
        if ((Y1|Y2|U|V)&256) {\
416
            if (Y1>255)   Y1=255; \
417
            else if (Y1<0)Y1=0;   \
418
            if (Y2>255)   Y2=255; \
419
            else if (Y2<0)Y2=0;   \
420
            if (U>255)    U=255;  \
421
            else if (U<0) U=0;    \
422
            if (V>255)    V=255;  \
423
            else if (V<0) V=0;    \
424
        }\
425
        if (alpha && ((A1|A2)&256)) {\
426
            A1=av_clip_uint8(A1);\
427
            A2=av_clip_uint8(A2);\
428
        }
429

    
430
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
431
    for (i=0; i<dstW; i++) {\
432
        int j;\
433
        int Y = 0;\
434
        int U = -128<<19;\
435
        int V = -128<<19;\
436
        int av_unused A;\
437
        int R,G,B;\
438
        \
439
        for (j=0; j<lumFilterSize; j++) {\
440
            Y += lumSrc[j][i     ] * lumFilter[j];\
441
        }\
442
        for (j=0; j<chrFilterSize; j++) {\
443
            U += chrSrc[j][i     ] * chrFilter[j];\
444
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
445
        }\
446
        Y >>=10;\
447
        U >>=10;\
448
        V >>=10;\
449
        if (alpha) {\
450
            A = rnd;\
451
            for (j=0; j<lumFilterSize; j++)\
452
                A += alpSrc[j][i     ] * lumFilter[j];\
453
            A >>=19;\
454
            if (A&256)\
455
                A = av_clip_uint8(A);\
456
        }
457

    
458
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
459
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
460
        Y-= c->yuv2rgb_y_offset;\
461
        Y*= c->yuv2rgb_y_coeff;\
462
        Y+= rnd;\
463
        R= Y + V*c->yuv2rgb_v2r_coeff;\
464
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
465
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
466
        if ((R|G|B)&(0xC0000000)) {\
467
            if (R>=(256<<22))   R=(256<<22)-1; \
468
            else if (R<0)R=0;   \
469
            if (G>=(256<<22))   G=(256<<22)-1; \
470
            else if (G<0)G=0;   \
471
            if (B>=(256<<22))   B=(256<<22)-1; \
472
            else if (B<0)B=0;   \
473
        }
474

    
475
#define YSCALE_YUV_2_GRAY16_C \
476
    for (i=0; i<(dstW>>1); i++) {\
477
        int j;\
478
        int Y1 = 1<<18;\
479
        int Y2 = 1<<18;\
480
        int U  = 1<<18;\
481
        int V  = 1<<18;\
482
        \
483
        const int i2= 2*i;\
484
        \
485
        for (j=0; j<lumFilterSize; j++) {\
486
            Y1 += lumSrc[j][i2] * lumFilter[j];\
487
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
488
        }\
489
        Y1>>=11;\
490
        Y2>>=11;\
491
        if ((Y1|Y2|U|V)&65536) {\
492
            if (Y1>65535)   Y1=65535; \
493
            else if (Y1<0)Y1=0;   \
494
            if (Y2>65535)   Y2=65535; \
495
            else if (Y2<0)Y2=0;   \
496
        }
497

    
498
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
499
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
500
    r = (type *)c->table_rV[V];   \
501
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
502
    b = (type *)c->table_bU[U];
503

    
504
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
505
    for (i=0; i<(dstW>>1); i++) { \
506
        const int i2= 2*i;       \
507
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
508
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
509
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
510
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
511
        type av_unused *r, *b, *g;                                    \
512
        int av_unused A1, A2;                                         \
513
        if (alpha) {\
514
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
515
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
516
        }
517

    
518
#define YSCALE_YUV_2_GRAY16_2_C   \
519
    for (i=0; i<(dstW>>1); i++) { \
520
        const int i2= 2*i;       \
521
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
522
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
523

    
524
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
525
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
526
    r = (type *)c->table_rV[V];\
527
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
528
    b = (type *)c->table_bU[U];
529

    
530
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
531
    for (i=0; i<(dstW>>1); i++) {\
532
        const int i2= 2*i;\
533
        int Y1= buf0[i2  ]>>7;\
534
        int Y2= buf0[i2+1]>>7;\
535
        int U= (uvbuf1[i     ])>>7;\
536
        int V= (uvbuf1[i+VOFW])>>7;\
537
        type av_unused *r, *b, *g;\
538
        int av_unused A1, A2;\
539
        if (alpha) {\
540
            A1= abuf0[i2  ]>>7;\
541
            A2= abuf0[i2+1]>>7;\
542
        }
543

    
544
#define YSCALE_YUV_2_GRAY16_1_C \
545
    for (i=0; i<(dstW>>1); i++) {\
546
        const int i2= 2*i;\
547
        int Y1= buf0[i2  ]<<1;\
548
        int Y2= buf0[i2+1]<<1;
549

    
550
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
551
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
552
    r = (type *)c->table_rV[V];\
553
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
554
    b = (type *)c->table_bU[U];
555

    
556
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
557
    for (i=0; i<(dstW>>1); i++) {\
558
        const int i2= 2*i;\
559
        int Y1= buf0[i2  ]>>7;\
560
        int Y2= buf0[i2+1]>>7;\
561
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
562
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
563
        type av_unused *r, *b, *g;\
564
        int av_unused A1, A2;\
565
        if (alpha) {\
566
            A1= abuf0[i2  ]>>7;\
567
            A2= abuf0[i2+1]>>7;\
568
        }
569

    
570
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
571
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
572
    r = (type *)c->table_rV[V];\
573
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
574
    b = (type *)c->table_bU[U];
575

    
576
#define YSCALE_YUV_2_MONO2_C \
577
    const uint8_t * const d128=dither_8x8_220[y&7];\
578
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
579
    for (i=0; i<dstW-7; i+=8) {\
580
        int acc;\
581
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
582
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
583
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
584
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
585
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
586
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
587
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
588
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
589
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
590
        dest++;\
591
    }
592

    
593
#define YSCALE_YUV_2_MONOX_C \
594
    const uint8_t * const d128=dither_8x8_220[y&7];\
595
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
596
    int acc=0;\
597
    for (i=0; i<dstW-1; i+=2) {\
598
        int j;\
599
        int Y1=1<<18;\
600
        int Y2=1<<18;\
601
\
602
        for (j=0; j<lumFilterSize; j++) {\
603
            Y1 += lumSrc[j][i] * lumFilter[j];\
604
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
605
        }\
606
        Y1>>=19;\
607
        Y2>>=19;\
608
        if ((Y1|Y2)&256) {\
609
            if (Y1>255)   Y1=255;\
610
            else if (Y1<0)Y1=0;\
611
            if (Y2>255)   Y2=255;\
612
            else if (Y2<0)Y2=0;\
613
        }\
614
        acc+= acc + g[Y1+d128[(i+0)&7]];\
615
        acc+= acc + g[Y2+d128[(i+1)&7]];\
616
        if ((i&7)==6) {\
617
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
618
            dest++;\
619
        }\
620
    }
621

    
622
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
623
    switch(c->dstFormat) {\
624
    case PIX_FMT_RGB48BE:\
625
    case PIX_FMT_RGB48LE:\
626
        func(uint8_t,0)\
627
            ((uint8_t*)dest)[ 0]= r[Y1];\
628
            ((uint8_t*)dest)[ 1]= r[Y1];\
629
            ((uint8_t*)dest)[ 2]= g[Y1];\
630
            ((uint8_t*)dest)[ 3]= g[Y1];\
631
            ((uint8_t*)dest)[ 4]= b[Y1];\
632
            ((uint8_t*)dest)[ 5]= b[Y1];\
633
            ((uint8_t*)dest)[ 6]= r[Y2];\
634
            ((uint8_t*)dest)[ 7]= r[Y2];\
635
            ((uint8_t*)dest)[ 8]= g[Y2];\
636
            ((uint8_t*)dest)[ 9]= g[Y2];\
637
            ((uint8_t*)dest)[10]= b[Y2];\
638
            ((uint8_t*)dest)[11]= b[Y2];\
639
            dest+=12;\
640
        }\
641
        break;\
642
    case PIX_FMT_RGBA:\
643
    case PIX_FMT_BGRA:\
644
        if (CONFIG_SMALL) {\
645
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
646
            func(uint32_t,needAlpha)\
647
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
648
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
649
            }\
650
        } else {\
651
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
652
                func(uint32_t,1)\
653
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
654
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
655
                }\
656
            } else {\
657
                func(uint32_t,0)\
658
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
659
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
660
                }\
661
            }\
662
        }\
663
        break;\
664
    case PIX_FMT_ARGB:\
665
    case PIX_FMT_ABGR:\
666
        if (CONFIG_SMALL) {\
667
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
668
            func(uint32_t,needAlpha)\
669
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
670
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
671
            }\
672
        } else {\
673
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
674
                func(uint32_t,1)\
675
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
676
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
677
                }\
678
            } else {\
679
                func(uint32_t,0)\
680
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
681
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
682
                }\
683
            }\
684
        }                \
685
        break;\
686
    case PIX_FMT_RGB24:\
687
        func(uint8_t,0)\
688
            ((uint8_t*)dest)[0]= r[Y1];\
689
            ((uint8_t*)dest)[1]= g[Y1];\
690
            ((uint8_t*)dest)[2]= b[Y1];\
691
            ((uint8_t*)dest)[3]= r[Y2];\
692
            ((uint8_t*)dest)[4]= g[Y2];\
693
            ((uint8_t*)dest)[5]= b[Y2];\
694
            dest+=6;\
695
        }\
696
        break;\
697
    case PIX_FMT_BGR24:\
698
        func(uint8_t,0)\
699
            ((uint8_t*)dest)[0]= b[Y1];\
700
            ((uint8_t*)dest)[1]= g[Y1];\
701
            ((uint8_t*)dest)[2]= r[Y1];\
702
            ((uint8_t*)dest)[3]= b[Y2];\
703
            ((uint8_t*)dest)[4]= g[Y2];\
704
            ((uint8_t*)dest)[5]= r[Y2];\
705
            dest+=6;\
706
        }\
707
        break;\
708
    case PIX_FMT_RGB565BE:\
709
    case PIX_FMT_RGB565LE:\
710
    case PIX_FMT_BGR565BE:\
711
    case PIX_FMT_BGR565LE:\
712
        {\
713
            const int dr1= dither_2x2_8[y&1    ][0];\
714
            const int dg1= dither_2x2_4[y&1    ][0];\
715
            const int db1= dither_2x2_8[(y&1)^1][0];\
716
            const int dr2= dither_2x2_8[y&1    ][1];\
717
            const int dg2= dither_2x2_4[y&1    ][1];\
718
            const int db2= dither_2x2_8[(y&1)^1][1];\
719
            func(uint16_t,0)\
720
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
721
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
722
            }\
723
        }\
724
        break;\
725
    case PIX_FMT_RGB555BE:\
726
    case PIX_FMT_RGB555LE:\
727
    case PIX_FMT_BGR555BE:\
728
    case PIX_FMT_BGR555LE:\
729
        {\
730
            const int dr1= dither_2x2_8[y&1    ][0];\
731
            const int dg1= dither_2x2_8[y&1    ][1];\
732
            const int db1= dither_2x2_8[(y&1)^1][0];\
733
            const int dr2= dither_2x2_8[y&1    ][1];\
734
            const int dg2= dither_2x2_8[y&1    ][0];\
735
            const int db2= dither_2x2_8[(y&1)^1][1];\
736
            func(uint16_t,0)\
737
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
738
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
739
            }\
740
        }\
741
        break;\
742
    case PIX_FMT_RGB444BE:\
743
    case PIX_FMT_RGB444LE:\
744
    case PIX_FMT_BGR444BE:\
745
    case PIX_FMT_BGR444LE:\
746
        {\
747
            const int dr1= dither_4x4_16[y&3    ][0];\
748
            const int dg1= dither_4x4_16[y&3    ][1];\
749
            const int db1= dither_4x4_16[(y&3)^3][0];\
750
            const int dr2= dither_4x4_16[y&3    ][1];\
751
            const int dg2= dither_4x4_16[y&3    ][0];\
752
            const int db2= dither_4x4_16[(y&3)^3][1];\
753
            func(uint16_t,0)\
754
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
755
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
756
            }\
757
        }\
758
        break;\
759
    case PIX_FMT_RGB8:\
760
    case PIX_FMT_BGR8:\
761
        {\
762
            const uint8_t * const d64= dither_8x8_73[y&7];\
763
            const uint8_t * const d32= dither_8x8_32[y&7];\
764
            func(uint8_t,0)\
765
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
766
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
767
            }\
768
        }\
769
        break;\
770
    case PIX_FMT_RGB4:\
771
    case PIX_FMT_BGR4:\
772
        {\
773
            const uint8_t * const d64= dither_8x8_73 [y&7];\
774
            const uint8_t * const d128=dither_8x8_220[y&7];\
775
            func(uint8_t,0)\
776
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
777
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
778
            }\
779
        }\
780
        break;\
781
    case PIX_FMT_RGB4_BYTE:\
782
    case PIX_FMT_BGR4_BYTE:\
783
        {\
784
            const uint8_t * const d64= dither_8x8_73 [y&7];\
785
            const uint8_t * const d128=dither_8x8_220[y&7];\
786
            func(uint8_t,0)\
787
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
788
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
789
            }\
790
        }\
791
        break;\
792
    case PIX_FMT_MONOBLACK:\
793
    case PIX_FMT_MONOWHITE:\
794
        {\
795
            func_monoblack\
796
        }\
797
        break;\
798
    case PIX_FMT_YUYV422:\
799
        func2\
800
            ((uint8_t*)dest)[2*i2+0]= Y1;\
801
            ((uint8_t*)dest)[2*i2+1]= U;\
802
            ((uint8_t*)dest)[2*i2+2]= Y2;\
803
            ((uint8_t*)dest)[2*i2+3]= V;\
804
        }                \
805
        break;\
806
    case PIX_FMT_UYVY422:\
807
        func2\
808
            ((uint8_t*)dest)[2*i2+0]= U;\
809
            ((uint8_t*)dest)[2*i2+1]= Y1;\
810
            ((uint8_t*)dest)[2*i2+2]= V;\
811
            ((uint8_t*)dest)[2*i2+3]= Y2;\
812
        }                \
813
        break;\
814
    case PIX_FMT_GRAY16BE:\
815
        func_g16\
816
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
817
            ((uint8_t*)dest)[2*i2+1]= Y1;\
818
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
819
            ((uint8_t*)dest)[2*i2+3]= Y2;\
820
        }                \
821
        break;\
822
    case PIX_FMT_GRAY16LE:\
823
        func_g16\
824
            ((uint8_t*)dest)[2*i2+0]= Y1;\
825
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
826
            ((uint8_t*)dest)[2*i2+2]= Y2;\
827
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
828
        }                \
829
        break;\
830
    }
831

    
832
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
833
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
834
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
835
{
836
    int i;
837
    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)
838
}
839

    
840
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
841
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
842
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
843
{
844
    int i;
845
    int step= c->dstFormatBpp/8;
846
    int aidx= 3;
847

    
848
    switch(c->dstFormat) {
849
    case PIX_FMT_ARGB:
850
        dest++;
851
        aidx= 0;
852
    case PIX_FMT_RGB24:
853
        aidx--;
854
    case PIX_FMT_RGBA:
855
        if (CONFIG_SMALL) {
856
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
857
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
858
                dest[aidx]= needAlpha ? A : 255;
859
                dest[0]= R>>22;
860
                dest[1]= G>>22;
861
                dest[2]= B>>22;
862
                dest+= step;
863
            }
864
        } else {
865
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
866
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
867
                    dest[aidx]= A;
868
                    dest[0]= R>>22;
869
                    dest[1]= G>>22;
870
                    dest[2]= B>>22;
871
                    dest+= step;
872
                }
873
            } else {
874
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
875
                    dest[aidx]= 255;
876
                    dest[0]= R>>22;
877
                    dest[1]= G>>22;
878
                    dest[2]= B>>22;
879
                    dest+= step;
880
                }
881
            }
882
        }
883
        break;
884
    case PIX_FMT_ABGR:
885
        dest++;
886
        aidx= 0;
887
    case PIX_FMT_BGR24:
888
        aidx--;
889
    case PIX_FMT_BGRA:
890
        if (CONFIG_SMALL) {
891
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
892
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
893
                dest[aidx]= needAlpha ? A : 255;
894
                dest[0]= B>>22;
895
                dest[1]= G>>22;
896
                dest[2]= R>>22;
897
                dest+= step;
898
            }
899
        } else {
900
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
901
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
902
                    dest[aidx]= A;
903
                    dest[0]= B>>22;
904
                    dest[1]= G>>22;
905
                    dest[2]= R>>22;
906
                    dest+= step;
907
                }
908
            } else {
909
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
910
                    dest[aidx]= 255;
911
                    dest[0]= B>>22;
912
                    dest[1]= G>>22;
913
                    dest[2]= R>>22;
914
                    dest+= step;
915
                }
916
            }
917
        }
918
        break;
919
    default:
920
        assert(0);
921
    }
922
}
923

    
924
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
925
{
926
    int i;
927
    uint8_t *ptr = plane + stride*y;
928
    for (i=0; i<height; i++) {
929
        memset(ptr, val, width);
930
        ptr += stride;
931
    }
932
}
933

    
934
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
935
                            uint32_t *unused)
936
{
937
    int i;
938
    for (i = 0; i < width; i++) {
939
        int r = src[i*6+0];
940
        int g = src[i*6+2];
941
        int b = src[i*6+4];
942

    
943
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
944
    }
945
}
946

    
947
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
948
                             const uint8_t *src1, const uint8_t *src2,
949
                             long width, uint32_t *unused)
950
{
951
    int i;
952
    assert(src1==src2);
953
    for (i = 0; i < width; i++) {
954
        int r = src1[6*i + 0];
955
        int g = src1[6*i + 2];
956
        int b = src1[6*i + 4];
957

    
958
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
959
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
960
    }
961
}
962

    
963
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
964
                                  const uint8_t *src1, const uint8_t *src2,
965
                                  long width, uint32_t *unused)
966
{
967
    int i;
968
    assert(src1==src2);
969
    for (i = 0; i < width; i++) {
970
        int r= src1[12*i + 0] + src1[12*i + 6];
971
        int g= src1[12*i + 2] + src1[12*i + 8];
972
        int b= src1[12*i + 4] + src1[12*i + 10];
973

    
974
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
975
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
976
    }
977
}
978

    
979
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
980
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
981
{\
982
    int i;\
983
    for (i=0; i<width; i++) {\
984
        int b= (((const type*)src)[i]>>shb)&maskb;\
985
        int g= (((const type*)src)[i]>>shg)&maskg;\
986
        int r= (((const type*)src)[i]>>shr)&maskr;\
987
\
988
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
989
    }\
990
}
991

    
992
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
993
BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
994
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
995
BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
996
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
997
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
998
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
999
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1000

    
1001
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1002
{
1003
    int i;
1004
    for (i=0; i<width; i++) {
1005
        dst[i]= src[4*i];
1006
    }
1007
}
1008

    
1009
#define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1010
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1011
{\
1012
    int i;\
1013
    for (i=0; i<width; i++) {\
1014
        int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1015
        int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1016
        int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1017
\
1018
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1019
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1020
    }\
1021
}\
1022
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1023
{\
1024
    int i;\
1025
    for (i=0; i<width; i++) {\
1026
        int pix0= ((const type*)src)[2*i+0]>>shp;\
1027
        int pix1= ((const type*)src)[2*i+1]>>shp;\
1028
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1029
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1030
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1031
        g&= maskg|(2*maskg);\
1032
\
1033
        g>>=shg;\
1034
\
1035
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1036
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1037
    }\
1038
}
1039

    
1040
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1041
BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1042
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1043
BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1044
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1045
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1046
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1047
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1048

    
1049
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1050
{
1051
    int i;
1052
    for (i=0; i<width; i++) {
1053
        int d= src[i];
1054

    
1055
        dst[i]= pal[d] & 0xFF;
1056
    }
1057
}
1058

    
1059
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1060
                           const uint8_t *src1, const uint8_t *src2,
1061
                           long width, uint32_t *pal)
1062
{
1063
    int i;
1064
    assert(src1 == src2);
1065
    for (i=0; i<width; i++) {
1066
        int p= pal[src1[i]];
1067

    
1068
        dstU[i]= p>>8;
1069
        dstV[i]= p>>16;
1070
    }
1071
}
1072

    
1073
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1074
{
1075
    int i, j;
1076
    for (i=0; i<width/8; i++) {
1077
        int d= ~src[i];
1078
        for(j=0; j<8; j++)
1079
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1080
    }
1081
}
1082

    
1083
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1084
{
1085
    int i, j;
1086
    for (i=0; i<width/8; i++) {
1087
        int d= src[i];
1088
        for(j=0; j<8; j++)
1089
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1090
    }
1091
}
1092

    
1093
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1094
//Plain C versions
1095
#if CONFIG_RUNTIME_CPUDETECT
1096
#  define COMPILE_C 1
1097
#  if   ARCH_X86
1098
#    define COMPILE_MMX     HAVE_MMX
1099
#    define COMPILE_MMX2    HAVE_MMX2
1100
#    define COMPILE_3DNOW   HAVE_AMD3DNOW
1101
#  elif ARCH_PPC
1102
#    define COMPILE_ALTIVEC HAVE_ALTIVEC
1103
#  endif
1104
#else /* CONFIG_RUNTIME_CPUDETECT */
1105
#  if   ARCH_X86
1106
#    if   HAVE_MMX2
1107
#      define COMPILE_MMX2  1
1108
#    elif HAVE_AMD3DNOW
1109
#      define COMPILE_3DNOW 1
1110
#    elif HAVE_MMX
1111
#      define COMPILE_MMX   1
1112
#    else
1113
#      define COMPILE_C     1
1114
#    endif
1115
#  elif ARCH_PPC && HAVE_ALTIVEC
1116
#    define COMPILE_ALTIVEC 1
1117
#  else
1118
#    define COMPILE_C       1
1119
#  endif
1120
#endif
1121

    
1122
#ifndef COMPILE_C
1123
#  define COMPILE_C 0
1124
#endif
1125
#ifndef COMPILE_MMX
1126
#  define COMPILE_MMX 0
1127
#endif
1128
#ifndef COMPILE_MMX2
1129
#  define COMPILE_MMX2 0
1130
#endif
1131
#ifndef COMPILE_3DNOW
1132
#  define COMPILE_3DNOW 0
1133
#endif
1134
#ifndef COMPILE_ALTIVEC
1135
#  define COMPILE_ALTIVEC 0
1136
#endif
1137

    
1138
#define COMPILE_TEMPLATE_MMX 0
1139
#define COMPILE_TEMPLATE_MMX2 0
1140
#define COMPILE_TEMPLATE_AMD3DNOW 0
1141
#define COMPILE_TEMPLATE_ALTIVEC 0
1142

    
1143
#include "swscale_template.c"
1144

    
1145
#if COMPILE_ALTIVEC
1146
#undef RENAME
1147
#undef COMPILE_TEMPLATE_ALTIVEC
1148
#define COMPILE_TEMPLATE_ALTIVEC 1
1149
#define RENAME(a) a ## _altivec
1150
#include "ppc/swscale_template.c"
1151
#endif
1152

    
1153
#if ARCH_X86
1154

    
1155
//MMX versions
1156
#if COMPILE_MMX
1157
#undef RENAME
1158
#undef COMPILE_TEMPLATE_MMX
1159
#undef COMPILE_TEMPLATE_MMX2
1160
#undef COMPILE_TEMPLATE_AMD3DNOW
1161
#define COMPILE_TEMPLATE_MMX 1
1162
#define COMPILE_TEMPLATE_MMX2 0
1163
#define COMPILE_TEMPLATE_AMD3DNOW 0
1164
#define RENAME(a) a ## _MMX
1165
#include "x86/swscale_template.c"
1166
#endif
1167

    
1168
//MMX2 versions
1169
#if COMPILE_MMX2
1170
#undef RENAME
1171
#undef COMPILE_TEMPLATE_MMX
1172
#undef COMPILE_TEMPLATE_MMX2
1173
#undef COMPILE_TEMPLATE_AMD3DNOW
1174
#define COMPILE_TEMPLATE_MMX 1
1175
#define COMPILE_TEMPLATE_MMX2 1
1176
#define COMPILE_TEMPLATE_AMD3DNOW 0
1177
#define RENAME(a) a ## _MMX2
1178
#include "x86/swscale_template.c"
1179
#endif
1180

    
1181
//3DNOW versions
1182
#if COMPILE_3DNOW
1183
#undef RENAME
1184
#undef COMPILE_TEMPLATE_MMX
1185
#undef COMPILE_TEMPLATE_MMX2
1186
#undef COMPILE_TEMPLATE_AMD3DNOW
1187
#define COMPILE_TEMPLATE_MMX 1
1188
#define COMPILE_TEMPLATE_MMX2 0
1189
#define COMPILE_TEMPLATE_AMD3DNOW 1
1190
#define RENAME(a) a ## _3DNow
1191
#include "x86/swscale_template.c"
1192
#endif
1193

    
1194
#endif //ARCH_X86
1195

    
1196
SwsFunc ff_getSwsFunc(SwsContext *c)
1197
{
1198
    sws_init_swScale_c(c);
1199

    
1200
#if CONFIG_RUNTIME_CPUDETECT
1201
    int flags = c->flags;
1202

    
1203
#if ARCH_X86
1204
    // ordered per speed fastest first
1205
    if (flags & SWS_CPU_CAPS_MMX2) {
1206
        sws_init_swScale_MMX2(c);
1207
        return swScale_MMX2;
1208
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1209
        sws_init_swScale_3DNow(c);
1210
        return swScale_3DNow;
1211
    } else if (flags & SWS_CPU_CAPS_MMX) {
1212
        sws_init_swScale_MMX(c);
1213
        return swScale_MMX;
1214
    }
1215

    
1216
#else
1217
#if COMPILE_ALTIVEC
1218
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1219
        sws_init_swScale_altivec(c);
1220
        return swScale_altivec;
1221
    }
1222
#endif
1223
#endif /* ARCH_X86 */
1224
#else //CONFIG_RUNTIME_CPUDETECT
1225
#if   COMPILE_TEMPLATE_MMX2
1226
    sws_init_swScale_MMX2(c);
1227
    return swScale_MMX2;
1228
#elif COMPILE_TEMPLATE_AMD3DNOW
1229
    sws_init_swScale_3DNow(c);
1230
    return swScale_3DNow;
1231
#elif COMPILE_TEMPLATE_MMX
1232
    sws_init_swScale_MMX(c);
1233
    return swScale_MMX;
1234
#elif COMPILE_TEMPLATE_ALTIVEC
1235
    sws_init_swScale_altivec(c);
1236
    return swScale_altivec;
1237
#endif
1238
#endif //!CONFIG_RUNTIME_CPUDETECT
1239

    
1240
    return swScale_c;
1241
}
1242

    
1243
static void copyPlane(const uint8_t *src, int srcStride,
1244
                      int srcSliceY, int srcSliceH, int width,
1245
                      uint8_t *dst, int dstStride)
1246
{
1247
    dst += dstStride * srcSliceY;
1248
    if (dstStride == srcStride && srcStride > 0) {
1249
        memcpy(dst, src, srcSliceH * dstStride);
1250
    } else {
1251
        int i;
1252
        for (i=0; i<srcSliceH; i++) {
1253
            memcpy(dst, src, width);
1254
            src += srcStride;
1255
            dst += dstStride;
1256
        }
1257
    }
1258
}
1259

    
1260
static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1261
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1262
{
1263
    uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1264

    
1265
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1266
              dstParam[0], dstStride[0]);
1267

    
1268
    if (c->dstFormat == PIX_FMT_NV12)
1269
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1270
    else
1271
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1272

    
1273
    return srcSliceH;
1274
}
1275

    
1276
static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1277
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1278
{
1279
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1280

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

    
1283
    return srcSliceH;
1284
}
1285

    
1286
static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1287
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1288
{
1289
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1290

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

    
1293
    return srcSliceH;
1294
}
1295

    
1296
static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1297
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1298
{
1299
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1300

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

    
1303
    return srcSliceH;
1304
}
1305

    
1306
static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1307
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1308
{
1309
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1310

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

    
1313
    return srcSliceH;
1314
}
1315

    
1316
static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1317
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1318
{
1319
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1320
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1321
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1322

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

    
1325
    if (dstParam[3])
1326
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1327

    
1328
    return srcSliceH;
1329
}
1330

    
1331
static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1332
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1333
{
1334
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1335
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1336
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1337

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

    
1340
    return srcSliceH;
1341
}
1342

    
1343
static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1344
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1345
{
1346
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1347
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1348
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1349

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

    
1352
    if (dstParam[3])
1353
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1354

    
1355
    return srcSliceH;
1356
}
1357

    
1358
static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1359
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1360
{
1361
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1362
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1363
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1364

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

    
1367
    return srcSliceH;
1368
}
1369

    
1370
static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1371
{
1372
    long i;
1373
    for (i=0; i<num_pixels; i++)
1374
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1375
}
1376

    
1377
static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1378
{
1379
    long i;
1380

    
1381
    for (i=0; i<num_pixels; i++)
1382
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1383
}
1384

    
1385
static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1386
{
1387
    long i;
1388

    
1389
    for (i=0; i<num_pixels; i++) {
1390
        //FIXME slow?
1391
        dst[0]= palette[src[i<<1]*4+0];
1392
        dst[1]= palette[src[i<<1]*4+1];
1393
        dst[2]= palette[src[i<<1]*4+2];
1394
        dst+= 3;
1395
    }
1396
}
1397

    
1398
static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1399
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1400
{
1401
    const enum PixelFormat srcFormat= c->srcFormat;
1402
    const enum PixelFormat dstFormat= c->dstFormat;
1403
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1404
                 const uint8_t *palette)=NULL;
1405
    int i;
1406
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1407
    const uint8_t *srcPtr= src[0];
1408

    
1409
    if (srcFormat == PIX_FMT_Y400A) {
1410
        switch (dstFormat) {
1411
        case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1412
        case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1413
        case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1414
        case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1415
        case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1416
        case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1417
        }
1418
    } else if (usePal(srcFormat)) {
1419
        switch (dstFormat) {
1420
        case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1421
        case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1422
        case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1423
        case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1424
        case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1425
        case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1426
        }
1427
    }
1428

    
1429
    if (!conv)
1430
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1431
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1432
    else {
1433
        for (i=0; i<srcSliceH; i++) {
1434
            conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1435
            srcPtr+= srcStride[0];
1436
            dstPtr+= dstStride[0];
1437
        }
1438
    }
1439

    
1440
    return srcSliceH;
1441
}
1442

    
1443
#define isRGBA32(x) (            \
1444
           (x) == PIX_FMT_ARGB   \
1445
        || (x) == PIX_FMT_RGBA   \
1446
        || (x) == PIX_FMT_BGRA   \
1447
        || (x) == PIX_FMT_ABGR   \
1448
        )
1449

    
1450
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1451
static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1452
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1453
{
1454
    const enum PixelFormat srcFormat= c->srcFormat;
1455
    const enum PixelFormat dstFormat= c->dstFormat;
1456
    const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1457
    const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1458
    const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1459
    const int dstId= c->dstFormatBpp >> 2;
1460
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1461

    
1462
#define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1463

    
1464
    if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1465
        if (     CONV_IS(ABGR, RGBA)
1466
              || CONV_IS(ARGB, BGRA)
1467
              || CONV_IS(BGRA, ARGB)
1468
              || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1469
        else if (CONV_IS(ABGR, ARGB)
1470
              || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1471
        else if (CONV_IS(ABGR, BGRA)
1472
              || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1473
        else if (CONV_IS(BGRA, RGBA)
1474
              || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1475
        else if (CONV_IS(BGRA, ABGR)
1476
              || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1477
    } else
1478
    /* BGR -> BGR */
1479
    if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1480
       || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1481
        switch(srcId | (dstId<<4)) {
1482
        case 0x34: conv= rgb16to15; break;
1483
        case 0x36: conv= rgb24to15; break;
1484
        case 0x38: conv= rgb32to15; break;
1485
        case 0x43: conv= rgb15to16; break;
1486
        case 0x46: conv= rgb24to16; break;
1487
        case 0x48: conv= rgb32to16; break;
1488
        case 0x63: conv= rgb15to24; break;
1489
        case 0x64: conv= rgb16to24; break;
1490
        case 0x68: conv= rgb32to24; break;
1491
        case 0x83: conv= rgb15to32; break;
1492
        case 0x84: conv= rgb16to32; break;
1493
        case 0x86: conv= rgb24to32; break;
1494
        }
1495
    } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1496
             || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1497
        switch(srcId | (dstId<<4)) {
1498
        case 0x33: conv= rgb15tobgr15; break;
1499
        case 0x34: conv= rgb16tobgr15; break;
1500
        case 0x36: conv= rgb24tobgr15; break;
1501
        case 0x38: conv= rgb32tobgr15; break;
1502
        case 0x43: conv= rgb15tobgr16; break;
1503
        case 0x44: conv= rgb16tobgr16; break;
1504
        case 0x46: conv= rgb24tobgr16; break;
1505
        case 0x48: conv= rgb32tobgr16; break;
1506
        case 0x63: conv= rgb15tobgr24; break;
1507
        case 0x64: conv= rgb16tobgr24; break;
1508
        case 0x66: conv= rgb24tobgr24; break;
1509
        case 0x68: conv= rgb32tobgr24; break;
1510
        case 0x83: conv= rgb15tobgr32; break;
1511
        case 0x84: conv= rgb16tobgr32; break;
1512
        case 0x86: conv= rgb24tobgr32; break;
1513
        }
1514
    }
1515

    
1516
    if (!conv) {
1517
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1518
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1519
    } else {
1520
        const uint8_t *srcPtr= src[0];
1521
              uint8_t *dstPtr= dst[0];
1522
        if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1523
            srcPtr += ALT32_CORR;
1524

    
1525
        if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1526
            dstPtr += ALT32_CORR;
1527

    
1528
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1529
            conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1530
        else {
1531
            int i;
1532
            dstPtr += dstStride[0]*srcSliceY;
1533

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

    
1544
static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1545
                              int srcSliceH, uint8_t* dst[], int dstStride[])
1546
{
1547
    rgb24toyv12(
1548
        src[0],
1549
        dst[0]+ srcSliceY    *dstStride[0],
1550
        dst[1]+(srcSliceY>>1)*dstStride[1],
1551
        dst[2]+(srcSliceY>>1)*dstStride[2],
1552
        c->srcW, srcSliceH,
1553
        dstStride[0], dstStride[1], srcStride[0]);
1554
    if (dst[3])
1555
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1556
    return srcSliceH;
1557
}
1558

    
1559
static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1560
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1561
{
1562
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1563
              dst[0], dstStride[0]);
1564

    
1565
    planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1566
             srcSliceH >> 2, srcStride[1], dstStride[1]);
1567
    planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1568
             srcSliceH >> 2, srcStride[2], dstStride[2]);
1569
    if (dst[3])
1570
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1571
    return srcSliceH;
1572
}
1573

    
1574
/* unscaled copy like stuff (assumes nearly identical formats) */
1575
static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1576
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1577
{
1578
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1579
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1580
    else {
1581
        int i;
1582
        const uint8_t *srcPtr= src[0];
1583
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1584
        int length=0;
1585

    
1586
        /* universal length finder */
1587
        while(length+c->srcW <= FFABS(dstStride[0])
1588
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1589
        assert(length!=0);
1590

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

    
1600
static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1601
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1602
{
1603
    int plane, i, j;
1604
    for (plane=0; plane<4; plane++) {
1605
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1606
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1607
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1608
        const uint8_t *srcPtr= src[plane];
1609
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1610

    
1611
        if (!dst[plane]) continue;
1612
        // ignore palette for GRAY8
1613
        if (plane == 1 && !dst[2]) continue;
1614
        if (!src[plane] || (plane == 1 && !src[2])) {
1615
            if(is16BPS(c->dstFormat))
1616
                length*=2;
1617
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1618
        } else {
1619
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1620
                if (!isBE(c->srcFormat)) srcPtr++;
1621
                for (i=0; i<height; i++) {
1622
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1623
                    srcPtr+= srcStride[plane];
1624
                    dstPtr+= dstStride[plane];
1625
                }
1626
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1627
                for (i=0; i<height; i++) {
1628
                    for (j=0; j<length; j++) {
1629
                        dstPtr[ j<<1   ] = srcPtr[j];
1630
                        dstPtr[(j<<1)+1] = srcPtr[j];
1631
                    }
1632
                    srcPtr+= srcStride[plane];
1633
                    dstPtr+= dstStride[plane];
1634
                }
1635
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1636
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1637

    
1638
                for (i=0; i<height; i++) {
1639
                    for (j=0; j<length; j++)
1640
                        ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1641
                    srcPtr+= srcStride[plane];
1642
                    dstPtr+= dstStride[plane];
1643
                }
1644
            } else if (dstStride[plane] == srcStride[plane] &&
1645
                       srcStride[plane] > 0 && srcStride[plane] == length) {
1646
                memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1647
                       height*dstStride[plane]);
1648
            } else {
1649
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1650
                    length*=2;
1651
                for (i=0; i<height; i++) {
1652
                    memcpy(dstPtr, srcPtr, length);
1653
                    srcPtr+= srcStride[plane];
1654
                    dstPtr+= dstStride[plane];
1655
                }
1656
            }
1657
        }
1658
    }
1659
    return srcSliceH;
1660
}
1661

    
1662
int ff_hardcodedcpuflags(void)
1663
{
1664
    int flags = 0;
1665
#if   COMPILE_TEMPLATE_MMX2
1666
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1667
#elif COMPILE_TEMPLATE_AMD3DNOW
1668
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1669
#elif COMPILE_TEMPLATE_MMX
1670
    flags |= SWS_CPU_CAPS_MMX;
1671
#elif COMPILE_TEMPLATE_ALTIVEC
1672
    flags |= SWS_CPU_CAPS_ALTIVEC;
1673
#elif ARCH_BFIN
1674
    flags |= SWS_CPU_CAPS_BFIN;
1675
#endif
1676
    return flags;
1677
}
1678

    
1679
void ff_get_unscaled_swscale(SwsContext *c)
1680
{
1681
    const enum PixelFormat srcFormat = c->srcFormat;
1682
    const enum PixelFormat dstFormat = c->dstFormat;
1683
    const int flags = c->flags;
1684
    const int dstH = c->dstH;
1685
    int needsDither;
1686

    
1687
    needsDither= isAnyRGB(dstFormat)
1688
        &&  c->dstFormatBpp < 24
1689
        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1690

    
1691
    /* yv12_to_nv12 */
1692
    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1693
        c->swScale= planarToNv12Wrapper;
1694
    }
1695
    /* yuv2bgr */
1696
    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1697
        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1698
        c->swScale= ff_yuv2rgb_get_func_ptr(c);
1699
    }
1700

    
1701
    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1702
        c->swScale= yvu9ToYv12Wrapper;
1703
    }
1704

    
1705
    /* bgr24toYV12 */
1706
    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1707
        c->swScale= bgr24ToYv12Wrapper;
1708

    
1709
    /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1710
    if (   isAnyRGB(srcFormat)
1711
        && isAnyRGB(dstFormat)
1712
        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1713
        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1714
        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1715
        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1716
        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1717
        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1718
        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1719
        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1720
        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1721
        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1722
        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1723
        c->swScale= rgbToRgbWrapper;
1724

    
1725
    if ((usePal(srcFormat) && (
1726
        dstFormat == PIX_FMT_RGB32   ||
1727
        dstFormat == PIX_FMT_RGB32_1 ||
1728
        dstFormat == PIX_FMT_RGB24   ||
1729
        dstFormat == PIX_FMT_BGR32   ||
1730
        dstFormat == PIX_FMT_BGR32_1 ||
1731
        dstFormat == PIX_FMT_BGR24)))
1732
        c->swScale= palToRgbWrapper;
1733

    
1734
    if (srcFormat == PIX_FMT_YUV422P) {
1735
        if (dstFormat == PIX_FMT_YUYV422)
1736
            c->swScale= yuv422pToYuy2Wrapper;
1737
        else if (dstFormat == PIX_FMT_UYVY422)
1738
            c->swScale= yuv422pToUyvyWrapper;
1739
    }
1740

    
1741
    /* LQ converters if -sws 0 or -sws 4*/
1742
    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1743
        /* yv12_to_yuy2 */
1744
        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1745
            if (dstFormat == PIX_FMT_YUYV422)
1746
                c->swScale= planarToYuy2Wrapper;
1747
            else if (dstFormat == PIX_FMT_UYVY422)
1748
                c->swScale= planarToUyvyWrapper;
1749
        }
1750
    }
1751
    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1752
        c->swScale= yuyvToYuv420Wrapper;
1753
    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1754
        c->swScale= uyvyToYuv420Wrapper;
1755
    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1756
        c->swScale= yuyvToYuv422Wrapper;
1757
    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1758
        c->swScale= uyvyToYuv422Wrapper;
1759

    
1760
#if COMPILE_ALTIVEC
1761
    if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1762
        !(c->flags & SWS_BITEXACT) &&
1763
        srcFormat == PIX_FMT_YUV420P) {
1764
        // unscaled YV12 -> packed YUV, we want speed
1765
        if (dstFormat == PIX_FMT_YUYV422)
1766
            c->swScale= yv12toyuy2_unscaled_altivec;
1767
        else if (dstFormat == PIX_FMT_UYVY422)
1768
            c->swScale= yv12touyvy_unscaled_altivec;
1769
    }
1770
#endif
1771

    
1772
    /* simple copy */
1773
    if (  srcFormat == dstFormat
1774
        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1775
        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1776
        || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1777
        || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1778
        || (isGray(dstFormat) && isGray(srcFormat))
1779
        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1780
            && c->chrDstHSubSample == c->chrSrcHSubSample
1781
            && c->chrDstVSubSample == c->chrSrcVSubSample
1782
            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1783
            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1784
    {
1785
        if (isPacked(c->srcFormat))
1786
            c->swScale= packedCopyWrapper;
1787
        else /* Planar YUV or gray */
1788
            c->swScale= planarCopyWrapper;
1789
    }
1790
#if ARCH_BFIN
1791
    if (flags & SWS_CPU_CAPS_BFIN)
1792
        ff_bfin_get_unscaled_swscale (c);
1793
#endif
1794
}
1795

    
1796
static void reset_ptr(const uint8_t* src[], int format)
1797
{
1798
    if(!isALPHA(format))
1799
        src[3]=NULL;
1800
    if(!isPlanarYUV(format)) {
1801
        src[3]=src[2]=NULL;
1802

    
1803
        if (!usePal(format))
1804
            src[1]= NULL;
1805
    }
1806
}
1807

    
1808
static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1809
                                const int linesizes[4])
1810
{
1811
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1812
    int i;
1813

    
1814
    for (i = 0; i < 4; i++) {
1815
        int plane = desc->comp[i].plane;
1816
        if (!data[plane] || !linesizes[plane])
1817
            return 0;
1818
    }
1819

    
1820
    return 1;
1821
}
1822

    
1823
/**
1824
 * swscale wrapper, so we don't need to export the SwsContext.
1825
 * Assumes planar YUV to be in YUV order instead of YVU.
1826
 */
1827
int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1828
              int srcSliceH, uint8_t* const dst[], const int dstStride[])
1829
{
1830
    int i;
1831
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1832
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1833

    
1834
    // do not mess up sliceDir if we have a "trailing" 0-size slice
1835
    if (srcSliceH == 0)
1836
        return 0;
1837

    
1838
    if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1839
        av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1840
        return 0;
1841
    }
1842
    if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1843
        av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1844
        return 0;
1845
    }
1846

    
1847
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1848
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1849
        return 0;
1850
    }
1851
    if (c->sliceDir == 0) {
1852
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1853
    }
1854

    
1855
    if (usePal(c->srcFormat)) {
1856
        for (i=0; i<256; i++) {
1857
            int p, r, g, b,y,u,v;
1858
            if(c->srcFormat == PIX_FMT_PAL8) {
1859
                p=((const uint32_t*)(src[1]))[i];
1860
                r= (p>>16)&0xFF;
1861
                g= (p>> 8)&0xFF;
1862
                b=  p     &0xFF;
1863
            } else if(c->srcFormat == PIX_FMT_RGB8) {
1864
                r= (i>>5    )*36;
1865
                g= ((i>>2)&7)*36;
1866
                b= (i&3     )*85;
1867
            } else if(c->srcFormat == PIX_FMT_BGR8) {
1868
                b= (i>>6    )*85;
1869
                g= ((i>>3)&7)*36;
1870
                r= (i&7     )*36;
1871
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1872
                r= (i>>3    )*255;
1873
                g= ((i>>1)&3)*85;
1874
                b= (i&1     )*255;
1875
            } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1876
                r = g = b = i;
1877
            } else {
1878
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1879
                b= (i>>3    )*255;
1880
                g= ((i>>1)&3)*85;
1881
                r= (i&1     )*255;
1882
            }
1883
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1884
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1885
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1886
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
1887

    
1888
            switch(c->dstFormat) {
1889
            case PIX_FMT_BGR32:
1890
#if !HAVE_BIGENDIAN
1891
            case PIX_FMT_RGB24:
1892
#endif
1893
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
1894
                break;
1895
            case PIX_FMT_BGR32_1:
1896
#if HAVE_BIGENDIAN
1897
            case PIX_FMT_BGR24:
1898
#endif
1899
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1900
                break;
1901
            case PIX_FMT_RGB32_1:
1902
#if HAVE_BIGENDIAN
1903
            case PIX_FMT_RGB24:
1904
#endif
1905
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1906
                break;
1907
            case PIX_FMT_RGB32:
1908
#if !HAVE_BIGENDIAN
1909
            case PIX_FMT_BGR24:
1910
#endif
1911
            default:
1912
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
1913
            }
1914
        }
1915
    }
1916

    
1917
    // copy strides, so they can safely be modified
1918
    if (c->sliceDir == 1) {
1919
        // slices go from top to bottom
1920
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1921
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1922

    
1923
        reset_ptr(src2, c->srcFormat);
1924
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
1925

    
1926
        /* reset slice direction at end of frame */
1927
        if (srcSliceY + srcSliceH == c->srcH)
1928
            c->sliceDir = 0;
1929

    
1930
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
1931
    } else {
1932
        // slices go from bottom to top => we flip the image internally
1933
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
1934
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
1935

    
1936
        src2[0] += (srcSliceH-1)*srcStride[0];
1937
        if (!usePal(c->srcFormat))
1938
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
1939
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
1940
        src2[3] += (srcSliceH-1)*srcStride[3];
1941
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
1942
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
1943
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
1944
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
1945

    
1946
        reset_ptr(src2, c->srcFormat);
1947
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
1948

    
1949
        /* reset slice direction at end of frame */
1950
        if (!srcSliceY)
1951
            c->sliceDir = 0;
1952

    
1953
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
1954
    }
1955
}
1956

    
1957
#if LIBSWSCALE_VERSION_MAJOR < 1
1958
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
1959
                      int srcSliceH, uint8_t* dst[], int dstStride[])
1960
{
1961
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
1962
}
1963
#endif
1964

    
1965
/* Convert the palette to the same packed 32-bit format as the palette */
1966
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1967
{
1968
    long i;
1969

    
1970
    for (i=0; i<num_pixels; i++)
1971
        ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
1972
}
1973

    
1974
/* Palette format: ABCD -> dst format: ABC */
1975
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1976
{
1977
    long i;
1978

    
1979
    for (i=0; i<num_pixels; i++) {
1980
        //FIXME slow?
1981
        dst[0]= palette[src[i]*4+0];
1982
        dst[1]= palette[src[i]*4+1];
1983
        dst[2]= palette[src[i]*4+2];
1984
        dst+= 3;
1985
    }
1986
}