Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 95f163b3

History | View | Annotate | Download (78.6 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
DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
270
{
271
  {   0,  1,  0,  1,  0,  1,  0,  1,},
272
  {   1,  0,  1,  0,  1,  0,  1,  0,},
273
  {   0,  1,  0,  1,  0,  1,  0,  1,},
274
  {   1,  0,  1,  0,  1,  0,  1,  0,},
275
  {   0,  1,  0,  1,  0,  1,  0,  1,},
276
  {   1,  0,  1,  0,  1,  0,  1,  0,},
277
  {   0,  1,  0,  1,  0,  1,  0,  1,},
278
  {   1,  0,  1,  0,  1,  0,  1,  0,},
279
},{
280
  {   1,  2,  1,  2,  1,  2,  1,  2,},
281
  {   3,  0,  3,  0,  3,  0,  3,  0,},
282
  {   1,  2,  1,  2,  1,  2,  1,  2,},
283
  {   3,  0,  3,  0,  3,  0,  3,  0,},
284
  {   1,  2,  1,  2,  1,  2,  1,  2,},
285
  {   3,  0,  3,  0,  3,  0,  3,  0,},
286
  {   1,  2,  1,  2,  1,  2,  1,  2,},
287
  {   3,  0,  3,  0,  3,  0,  3,  0,},
288
},{
289
  {   2,  4,  3,  5,  2,  4,  3,  5,},
290
  {   6,  0,  7,  1,  6,  0,  7,  1,},
291
  {   3,  5,  2,  4,  3,  5,  2,  4,},
292
  {   7,  1,  6,  0,  7,  1,  6,  0,},
293
  {   2,  4,  3,  5,  2,  4,  3,  5,},
294
  {   6,  0,  7,  1,  6,  0,  7,  1,},
295
  {   3,  5,  2,  4,  3,  5,  2,  4,},
296
  {   7,  1,  6,  0,  7,  1,  6,  0,},
297
},{
298
  {   4,  8,  7, 11,  4,  8,  7, 11,},
299
  {  12,  0, 15,  3, 12,  0, 15,  3,},
300
  {   6, 10,  5,  9,  6, 10,  5,  9,},
301
  {  14,  2, 13,  1, 14,  2, 13,  1,},
302
  {   4,  8,  7, 11,  4,  8,  7, 11,},
303
  {  12,  0, 15,  3, 12,  0, 15,  3,},
304
  {   6, 10,  5,  9,  6, 10,  5,  9,},
305
  {  14,  2, 13,  1, 14,  2, 13,  1,},
306
},{
307
  {   9, 17, 15, 23,  8, 16, 14, 22,},
308
  {  25,  1, 31,  7, 24,  0, 30,  6,},
309
  {  13, 21, 11, 19, 12, 20, 10, 18,},
310
  {  29,  5, 27,  3, 28,  4, 26,  2,},
311
  {   8, 16, 14, 22,  9, 17, 15, 23,},
312
  {  24,  0, 30,  6, 25,  1, 31,  7,},
313
  {  12, 20, 10, 18, 13, 21, 11, 19,},
314
  {  28,  4, 26,  2, 29,  5, 27,  3,},
315
},{
316
  {  18, 34, 30, 46, 17, 33, 29, 45,},
317
  {  50,  2, 62, 14, 49,  1, 61, 13,},
318
  {  26, 42, 22, 38, 25, 41, 21, 37,},
319
  {  58, 10, 54,  6, 57,  9, 53,  5,},
320
  {  16, 32, 28, 44, 19, 35, 31, 47,},
321
  {  48,  0, 60, 12, 51,  3, 63, 15,},
322
  {  24, 40, 20, 36, 27, 43, 23, 39,},
323
  {  56,  8, 52,  4, 59, 11, 55,  7,},
324
},{
325
  {  18, 34, 30, 46, 17, 33, 29, 45,},
326
  {  50,  2, 62, 14, 49,  1, 61, 13,},
327
  {  26, 42, 22, 38, 25, 41, 21, 37,},
328
  {  58, 10, 54,  6, 57,  9, 53,  5,},
329
  {  16, 32, 28, 44, 19, 35, 31, 47,},
330
  {  48,  0, 60, 12, 51,  3, 63, 15,},
331
  {  24, 40, 20, 36, 27, 43, 23, 39,},
332
  {  56,  8, 52,  4, 59, 11, 55,  7,},
333
},{
334
  {  36, 68, 60, 92, 34, 66, 58, 90,},
335
  { 100,  4,124, 28, 98,  2,122, 26,},
336
  {  52, 84, 44, 76, 50, 82, 42, 74,},
337
  { 116, 20,108, 12,114, 18,106, 10,},
338
  {  32, 64, 56, 88, 38, 70, 62, 94,},
339
  {  96,  0,120, 24,102,  6,126, 30,},
340
  {  48, 80, 40, 72, 54, 86, 46, 78,},
341
  { 112, 16,104,  8,118, 22,110, 14,},
342
}};
343

    
344
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
345
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
346
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
347
                                                    int dstW, int chrDstW, int big_endian)
348
{
349
    //FIXME Optimize (just quickly written not optimized..)
350
    int i;
351

    
352
    for (i = 0; i < dstW; i++) {
353
        int val = 1 << 10;
354
        int j;
355

    
356
        for (j = 0; j < lumFilterSize; j++)
357
            val += lumSrc[j][i] * lumFilter[j];
358

    
359
        if (big_endian) {
360
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
361
        } else {
362
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
363
        }
364
    }
365

    
366
    if (uDest) {
367
        for (i = 0; i < chrDstW; i++) {
368
            int u = 1 << 10;
369
            int v = 1 << 10;
370
            int j;
371

    
372
            for (j = 0; j < chrFilterSize; j++) {
373
                u += chrSrc[j][i       ] * chrFilter[j];
374
                v += chrSrc[j][i + VOFW] * chrFilter[j];
375
            }
376

    
377
            if (big_endian) {
378
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
379
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
380
            } else {
381
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
382
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
383
            }
384
        }
385
    }
386

    
387
    if (CONFIG_SWSCALE_ALPHA && aDest) {
388
        for (i = 0; i < dstW; i++) {
389
            int val = 1 << 10;
390
            int j;
391

    
392
            for (j = 0; j < lumFilterSize; j++)
393
                val += alpSrc[j][i] * lumFilter[j];
394

    
395
            if (big_endian) {
396
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
397
            } else {
398
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
399
            }
400
        }
401
    }
402
}
403

    
404
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
405
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
406
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
407
                                 enum PixelFormat dstFormat)
408
{
409
    if (isBE(dstFormat)) {
410
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
411
                               chrFilter, chrSrc, chrFilterSize,
412
                               alpSrc,
413
                               dest, uDest, vDest, aDest,
414
                               dstW, chrDstW, 1);
415
    } else {
416
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
417
                               chrFilter, chrSrc, chrFilterSize,
418
                               alpSrc,
419
                               dest, uDest, vDest, aDest,
420
                               dstW, chrDstW, 0);
421
    }
422
}
423

    
424
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
425
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
426
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
427
{
428
    //FIXME Optimize (just quickly written not optimized..)
429
    int i;
430
    for (i=0; i<dstW; i++) {
431
        int val=1<<18;
432
        int j;
433
        for (j=0; j<lumFilterSize; j++)
434
            val += lumSrc[j][i] * lumFilter[j];
435

    
436
        dest[i]= av_clip_uint8(val>>19);
437
    }
438

    
439
    if (uDest)
440
        for (i=0; i<chrDstW; i++) {
441
            int u=1<<18;
442
            int v=1<<18;
443
            int j;
444
            for (j=0; j<chrFilterSize; j++) {
445
                u += chrSrc[j][i] * chrFilter[j];
446
                v += chrSrc[j][i + VOFW] * chrFilter[j];
447
            }
448

    
449
            uDest[i]= av_clip_uint8(u>>19);
450
            vDest[i]= av_clip_uint8(v>>19);
451
        }
452

    
453
    if (CONFIG_SWSCALE_ALPHA && aDest)
454
        for (i=0; i<dstW; i++) {
455
            int val=1<<18;
456
            int j;
457
            for (j=0; j<lumFilterSize; j++)
458
                val += alpSrc[j][i] * lumFilter[j];
459

    
460
            aDest[i]= av_clip_uint8(val>>19);
461
        }
462

    
463
}
464

    
465
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
466
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
467
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
468
{
469
    //FIXME Optimize (just quickly written not optimized..)
470
    int i;
471
    for (i=0; i<dstW; i++) {
472
        int val=1<<18;
473
        int j;
474
        for (j=0; j<lumFilterSize; j++)
475
            val += lumSrc[j][i] * lumFilter[j];
476

    
477
        dest[i]= av_clip_uint8(val>>19);
478
    }
479

    
480
    if (!uDest)
481
        return;
482

    
483
    if (dstFormat == PIX_FMT_NV12)
484
        for (i=0; i<chrDstW; i++) {
485
            int u=1<<18;
486
            int v=1<<18;
487
            int j;
488
            for (j=0; j<chrFilterSize; j++) {
489
                u += chrSrc[j][i] * chrFilter[j];
490
                v += chrSrc[j][i + VOFW] * chrFilter[j];
491
            }
492

    
493
            uDest[2*i]= av_clip_uint8(u>>19);
494
            uDest[2*i+1]= av_clip_uint8(v>>19);
495
        }
496
    else
497
        for (i=0; i<chrDstW; i++) {
498
            int u=1<<18;
499
            int v=1<<18;
500
            int j;
501
            for (j=0; j<chrFilterSize; j++) {
502
                u += chrSrc[j][i] * chrFilter[j];
503
                v += chrSrc[j][i + VOFW] * chrFilter[j];
504
            }
505

    
506
            uDest[2*i]= av_clip_uint8(v>>19);
507
            uDest[2*i+1]= av_clip_uint8(u>>19);
508
        }
509
}
510

    
511
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
512
    for (i=0; i<(dstW>>1); i++) {\
513
        int j;\
514
        int Y1 = 1<<18;\
515
        int Y2 = 1<<18;\
516
        int U  = 1<<18;\
517
        int V  = 1<<18;\
518
        int av_unused A1, A2;\
519
        type av_unused *r, *b, *g;\
520
        const int i2= 2*i;\
521
        \
522
        for (j=0; j<lumFilterSize; j++) {\
523
            Y1 += lumSrc[j][i2] * lumFilter[j];\
524
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
525
        }\
526
        for (j=0; j<chrFilterSize; j++) {\
527
            U += chrSrc[j][i] * chrFilter[j];\
528
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
529
        }\
530
        Y1>>=19;\
531
        Y2>>=19;\
532
        U >>=19;\
533
        V >>=19;\
534
        if (alpha) {\
535
            A1 = 1<<18;\
536
            A2 = 1<<18;\
537
            for (j=0; j<lumFilterSize; j++) {\
538
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
539
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
540
            }\
541
            A1>>=19;\
542
            A2>>=19;\
543
        }
544

    
545
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
546
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
547
        if ((Y1|Y2|U|V)&256) {\
548
            if (Y1>255)   Y1=255; \
549
            else if (Y1<0)Y1=0;   \
550
            if (Y2>255)   Y2=255; \
551
            else if (Y2<0)Y2=0;   \
552
            if (U>255)    U=255;  \
553
            else if (U<0) U=0;    \
554
            if (V>255)    V=255;  \
555
            else if (V<0) V=0;    \
556
        }\
557
        if (alpha && ((A1|A2)&256)) {\
558
            A1=av_clip_uint8(A1);\
559
            A2=av_clip_uint8(A2);\
560
        }
561

    
562
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
563
    for (i=0; i<dstW; i++) {\
564
        int j;\
565
        int Y = 0;\
566
        int U = -128<<19;\
567
        int V = -128<<19;\
568
        int av_unused A;\
569
        int R,G,B;\
570
        \
571
        for (j=0; j<lumFilterSize; j++) {\
572
            Y += lumSrc[j][i     ] * lumFilter[j];\
573
        }\
574
        for (j=0; j<chrFilterSize; j++) {\
575
            U += chrSrc[j][i     ] * chrFilter[j];\
576
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
577
        }\
578
        Y >>=10;\
579
        U >>=10;\
580
        V >>=10;\
581
        if (alpha) {\
582
            A = rnd;\
583
            for (j=0; j<lumFilterSize; j++)\
584
                A += alpSrc[j][i     ] * lumFilter[j];\
585
            A >>=19;\
586
            if (A&256)\
587
                A = av_clip_uint8(A);\
588
        }
589

    
590
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
591
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
592
        Y-= c->yuv2rgb_y_offset;\
593
        Y*= c->yuv2rgb_y_coeff;\
594
        Y+= rnd;\
595
        R= Y + V*c->yuv2rgb_v2r_coeff;\
596
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
597
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
598
        if ((R|G|B)&(0xC0000000)) {\
599
            if (R>=(256<<22))   R=(256<<22)-1; \
600
            else if (R<0)R=0;   \
601
            if (G>=(256<<22))   G=(256<<22)-1; \
602
            else if (G<0)G=0;   \
603
            if (B>=(256<<22))   B=(256<<22)-1; \
604
            else if (B<0)B=0;   \
605
        }
606

    
607
#define YSCALE_YUV_2_GRAY16_C \
608
    for (i=0; i<(dstW>>1); i++) {\
609
        int j;\
610
        int Y1 = 1<<18;\
611
        int Y2 = 1<<18;\
612
        int U  = 1<<18;\
613
        int V  = 1<<18;\
614
        \
615
        const int i2= 2*i;\
616
        \
617
        for (j=0; j<lumFilterSize; j++) {\
618
            Y1 += lumSrc[j][i2] * lumFilter[j];\
619
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
620
        }\
621
        Y1>>=11;\
622
        Y2>>=11;\
623
        if ((Y1|Y2|U|V)&65536) {\
624
            if (Y1>65535)   Y1=65535; \
625
            else if (Y1<0)Y1=0;   \
626
            if (Y2>65535)   Y2=65535; \
627
            else if (Y2<0)Y2=0;   \
628
        }
629

    
630
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
631
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
632
    r = (type *)c->table_rV[V];   \
633
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
634
    b = (type *)c->table_bU[U];
635

    
636
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
637
    for (i=0; i<(dstW>>1); i++) { \
638
        const int i2= 2*i;       \
639
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
640
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
641
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
642
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
643
        type av_unused *r, *b, *g;                                    \
644
        int av_unused A1, A2;                                         \
645
        if (alpha) {\
646
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
647
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
648
        }
649

    
650
#define YSCALE_YUV_2_GRAY16_2_C   \
651
    for (i=0; i<(dstW>>1); i++) { \
652
        const int i2= 2*i;       \
653
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
654
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
655

    
656
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
657
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
658
    r = (type *)c->table_rV[V];\
659
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
660
    b = (type *)c->table_bU[U];
661

    
662
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
663
    for (i=0; i<(dstW>>1); i++) {\
664
        const int i2= 2*i;\
665
        int Y1= buf0[i2  ]>>7;\
666
        int Y2= buf0[i2+1]>>7;\
667
        int U= (uvbuf1[i     ])>>7;\
668
        int V= (uvbuf1[i+VOFW])>>7;\
669
        type av_unused *r, *b, *g;\
670
        int av_unused A1, A2;\
671
        if (alpha) {\
672
            A1= abuf0[i2  ]>>7;\
673
            A2= abuf0[i2+1]>>7;\
674
        }
675

    
676
#define YSCALE_YUV_2_GRAY16_1_C \
677
    for (i=0; i<(dstW>>1); i++) {\
678
        const int i2= 2*i;\
679
        int Y1= buf0[i2  ]<<1;\
680
        int Y2= buf0[i2+1]<<1;
681

    
682
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
683
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
684
    r = (type *)c->table_rV[V];\
685
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
686
    b = (type *)c->table_bU[U];
687

    
688
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
689
    for (i=0; i<(dstW>>1); i++) {\
690
        const int i2= 2*i;\
691
        int Y1= buf0[i2  ]>>7;\
692
        int Y2= buf0[i2+1]>>7;\
693
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
694
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
695
        type av_unused *r, *b, *g;\
696
        int av_unused A1, A2;\
697
        if (alpha) {\
698
            A1= abuf0[i2  ]>>7;\
699
            A2= abuf0[i2+1]>>7;\
700
        }
701

    
702
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
703
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
704
    r = (type *)c->table_rV[V];\
705
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
706
    b = (type *)c->table_bU[U];
707

    
708
#define YSCALE_YUV_2_MONO2_C \
709
    const uint8_t * const d128=dither_8x8_220[y&7];\
710
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
711
    for (i=0; i<dstW-7; i+=8) {\
712
        int acc;\
713
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
714
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
715
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
716
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
717
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
718
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
719
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
720
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
721
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
722
        dest++;\
723
    }
724

    
725
#define YSCALE_YUV_2_MONOX_C \
726
    const uint8_t * const d128=dither_8x8_220[y&7];\
727
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
728
    int acc=0;\
729
    for (i=0; i<dstW-1; i+=2) {\
730
        int j;\
731
        int Y1=1<<18;\
732
        int Y2=1<<18;\
733
\
734
        for (j=0; j<lumFilterSize; j++) {\
735
            Y1 += lumSrc[j][i] * lumFilter[j];\
736
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
737
        }\
738
        Y1>>=19;\
739
        Y2>>=19;\
740
        if ((Y1|Y2)&256) {\
741
            if (Y1>255)   Y1=255;\
742
            else if (Y1<0)Y1=0;\
743
            if (Y2>255)   Y2=255;\
744
            else if (Y2<0)Y2=0;\
745
        }\
746
        acc+= acc + g[Y1+d128[(i+0)&7]];\
747
        acc+= acc + g[Y2+d128[(i+1)&7]];\
748
        if ((i&7)==6) {\
749
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
750
            dest++;\
751
        }\
752
    }
753

    
754
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
755
    switch(c->dstFormat) {\
756
    case PIX_FMT_RGB48BE:\
757
    case PIX_FMT_RGB48LE:\
758
        func(uint8_t,0)\
759
            ((uint8_t*)dest)[ 0]= r[Y1];\
760
            ((uint8_t*)dest)[ 1]= r[Y1];\
761
            ((uint8_t*)dest)[ 2]= g[Y1];\
762
            ((uint8_t*)dest)[ 3]= g[Y1];\
763
            ((uint8_t*)dest)[ 4]= b[Y1];\
764
            ((uint8_t*)dest)[ 5]= b[Y1];\
765
            ((uint8_t*)dest)[ 6]= r[Y2];\
766
            ((uint8_t*)dest)[ 7]= r[Y2];\
767
            ((uint8_t*)dest)[ 8]= g[Y2];\
768
            ((uint8_t*)dest)[ 9]= g[Y2];\
769
            ((uint8_t*)dest)[10]= b[Y2];\
770
            ((uint8_t*)dest)[11]= b[Y2];\
771
            dest+=12;\
772
        }\
773
        break;\
774
    case PIX_FMT_BGR48BE:\
775
    case PIX_FMT_BGR48LE:\
776
        func(uint8_t,0)\
777
            ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
778
            ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
779
            ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
780
            ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
781
            ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
782
            ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
783
            dest+=12;\
784
        }\
785
        break;\
786
    case PIX_FMT_RGBA:\
787
    case PIX_FMT_BGRA:\
788
        if (CONFIG_SMALL) {\
789
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
790
            func(uint32_t,needAlpha)\
791
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
792
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
793
            }\
794
        } else {\
795
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
796
                func(uint32_t,1)\
797
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
798
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
799
                }\
800
            } else {\
801
                func(uint32_t,0)\
802
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
803
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
804
                }\
805
            }\
806
        }\
807
        break;\
808
    case PIX_FMT_ARGB:\
809
    case PIX_FMT_ABGR:\
810
        if (CONFIG_SMALL) {\
811
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
812
            func(uint32_t,needAlpha)\
813
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
814
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
815
            }\
816
        } else {\
817
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
818
                func(uint32_t,1)\
819
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
820
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
821
                }\
822
            } else {\
823
                func(uint32_t,0)\
824
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
825
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
826
                }\
827
            }\
828
        }                \
829
        break;\
830
    case PIX_FMT_RGB24:\
831
        func(uint8_t,0)\
832
            ((uint8_t*)dest)[0]= r[Y1];\
833
            ((uint8_t*)dest)[1]= g[Y1];\
834
            ((uint8_t*)dest)[2]= b[Y1];\
835
            ((uint8_t*)dest)[3]= r[Y2];\
836
            ((uint8_t*)dest)[4]= g[Y2];\
837
            ((uint8_t*)dest)[5]= b[Y2];\
838
            dest+=6;\
839
        }\
840
        break;\
841
    case PIX_FMT_BGR24:\
842
        func(uint8_t,0)\
843
            ((uint8_t*)dest)[0]= b[Y1];\
844
            ((uint8_t*)dest)[1]= g[Y1];\
845
            ((uint8_t*)dest)[2]= r[Y1];\
846
            ((uint8_t*)dest)[3]= b[Y2];\
847
            ((uint8_t*)dest)[4]= g[Y2];\
848
            ((uint8_t*)dest)[5]= r[Y2];\
849
            dest+=6;\
850
        }\
851
        break;\
852
    case PIX_FMT_RGB565BE:\
853
    case PIX_FMT_RGB565LE:\
854
    case PIX_FMT_BGR565BE:\
855
    case PIX_FMT_BGR565LE:\
856
        {\
857
            const int dr1= dither_2x2_8[y&1    ][0];\
858
            const int dg1= dither_2x2_4[y&1    ][0];\
859
            const int db1= dither_2x2_8[(y&1)^1][0];\
860
            const int dr2= dither_2x2_8[y&1    ][1];\
861
            const int dg2= dither_2x2_4[y&1    ][1];\
862
            const int db2= dither_2x2_8[(y&1)^1][1];\
863
            func(uint16_t,0)\
864
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
865
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
866
            }\
867
        }\
868
        break;\
869
    case PIX_FMT_RGB555BE:\
870
    case PIX_FMT_RGB555LE:\
871
    case PIX_FMT_BGR555BE:\
872
    case PIX_FMT_BGR555LE:\
873
        {\
874
            const int dr1= dither_2x2_8[y&1    ][0];\
875
            const int dg1= dither_2x2_8[y&1    ][1];\
876
            const int db1= dither_2x2_8[(y&1)^1][0];\
877
            const int dr2= dither_2x2_8[y&1    ][1];\
878
            const int dg2= dither_2x2_8[y&1    ][0];\
879
            const int db2= dither_2x2_8[(y&1)^1][1];\
880
            func(uint16_t,0)\
881
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
882
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
883
            }\
884
        }\
885
        break;\
886
    case PIX_FMT_RGB444BE:\
887
    case PIX_FMT_RGB444LE:\
888
    case PIX_FMT_BGR444BE:\
889
    case PIX_FMT_BGR444LE:\
890
        {\
891
            const int dr1= dither_4x4_16[y&3    ][0];\
892
            const int dg1= dither_4x4_16[y&3    ][1];\
893
            const int db1= dither_4x4_16[(y&3)^3][0];\
894
            const int dr2= dither_4x4_16[y&3    ][1];\
895
            const int dg2= dither_4x4_16[y&3    ][0];\
896
            const int db2= dither_4x4_16[(y&3)^3][1];\
897
            func(uint16_t,0)\
898
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
899
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
900
            }\
901
        }\
902
        break;\
903
    case PIX_FMT_RGB8:\
904
    case PIX_FMT_BGR8:\
905
        {\
906
            const uint8_t * const d64= dither_8x8_73[y&7];\
907
            const uint8_t * const d32= dither_8x8_32[y&7];\
908
            func(uint8_t,0)\
909
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
910
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
911
            }\
912
        }\
913
        break;\
914
    case PIX_FMT_RGB4:\
915
    case PIX_FMT_BGR4:\
916
        {\
917
            const uint8_t * const d64= dither_8x8_73 [y&7];\
918
            const uint8_t * const d128=dither_8x8_220[y&7];\
919
            func(uint8_t,0)\
920
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
921
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
922
            }\
923
        }\
924
        break;\
925
    case PIX_FMT_RGB4_BYTE:\
926
    case PIX_FMT_BGR4_BYTE:\
927
        {\
928
            const uint8_t * const d64= dither_8x8_73 [y&7];\
929
            const uint8_t * const d128=dither_8x8_220[y&7];\
930
            func(uint8_t,0)\
931
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
932
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
933
            }\
934
        }\
935
        break;\
936
    case PIX_FMT_MONOBLACK:\
937
    case PIX_FMT_MONOWHITE:\
938
        {\
939
            func_monoblack\
940
        }\
941
        break;\
942
    case PIX_FMT_YUYV422:\
943
        func2\
944
            ((uint8_t*)dest)[2*i2+0]= Y1;\
945
            ((uint8_t*)dest)[2*i2+1]= U;\
946
            ((uint8_t*)dest)[2*i2+2]= Y2;\
947
            ((uint8_t*)dest)[2*i2+3]= V;\
948
        }                \
949
        break;\
950
    case PIX_FMT_UYVY422:\
951
        func2\
952
            ((uint8_t*)dest)[2*i2+0]= U;\
953
            ((uint8_t*)dest)[2*i2+1]= Y1;\
954
            ((uint8_t*)dest)[2*i2+2]= V;\
955
            ((uint8_t*)dest)[2*i2+3]= Y2;\
956
        }                \
957
        break;\
958
    case PIX_FMT_GRAY16BE:\
959
        func_g16\
960
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
961
            ((uint8_t*)dest)[2*i2+1]= Y1;\
962
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
963
            ((uint8_t*)dest)[2*i2+3]= Y2;\
964
        }                \
965
        break;\
966
    case PIX_FMT_GRAY16LE:\
967
        func_g16\
968
            ((uint8_t*)dest)[2*i2+0]= Y1;\
969
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
970
            ((uint8_t*)dest)[2*i2+2]= Y2;\
971
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
972
        }                \
973
        break;\
974
    }
975

    
976
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
977
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
978
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
979
{
980
    int i;
981
    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)
982
}
983

    
984
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
985
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
986
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
987
{
988
    int i;
989
    int step= c->dstFormatBpp/8;
990
    int aidx= 3;
991

    
992
    switch(c->dstFormat) {
993
    case PIX_FMT_ARGB:
994
        dest++;
995
        aidx= 0;
996
    case PIX_FMT_RGB24:
997
        aidx--;
998
    case PIX_FMT_RGBA:
999
        if (CONFIG_SMALL) {
1000
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1001
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1002
                dest[aidx]= needAlpha ? A : 255;
1003
                dest[0]= R>>22;
1004
                dest[1]= G>>22;
1005
                dest[2]= B>>22;
1006
                dest+= step;
1007
            }
1008
        } else {
1009
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1010
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1011
                    dest[aidx]= A;
1012
                    dest[0]= R>>22;
1013
                    dest[1]= G>>22;
1014
                    dest[2]= B>>22;
1015
                    dest+= step;
1016
                }
1017
            } else {
1018
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1019
                    dest[aidx]= 255;
1020
                    dest[0]= R>>22;
1021
                    dest[1]= G>>22;
1022
                    dest[2]= B>>22;
1023
                    dest+= step;
1024
                }
1025
            }
1026
        }
1027
        break;
1028
    case PIX_FMT_ABGR:
1029
        dest++;
1030
        aidx= 0;
1031
    case PIX_FMT_BGR24:
1032
        aidx--;
1033
    case PIX_FMT_BGRA:
1034
        if (CONFIG_SMALL) {
1035
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1036
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1037
                dest[aidx]= needAlpha ? A : 255;
1038
                dest[0]= B>>22;
1039
                dest[1]= G>>22;
1040
                dest[2]= R>>22;
1041
                dest+= step;
1042
            }
1043
        } else {
1044
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1045
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1046
                    dest[aidx]= A;
1047
                    dest[0]= B>>22;
1048
                    dest[1]= G>>22;
1049
                    dest[2]= R>>22;
1050
                    dest+= step;
1051
                }
1052
            } else {
1053
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1054
                    dest[aidx]= 255;
1055
                    dest[0]= B>>22;
1056
                    dest[1]= G>>22;
1057
                    dest[2]= R>>22;
1058
                    dest+= step;
1059
                }
1060
            }
1061
        }
1062
        break;
1063
    default:
1064
        assert(0);
1065
    }
1066
}
1067

    
1068
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1069
{
1070
    int i;
1071
    uint8_t *ptr = plane + stride*y;
1072
    for (i=0; i<height; i++) {
1073
        memset(ptr, val, width);
1074
        ptr += stride;
1075
    }
1076
}
1077

    
1078
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
1079
                            uint32_t *unused)
1080
{
1081
    int i;
1082
    for (i = 0; i < width; i++) {
1083
        int r = src[i*6+0];
1084
        int g = src[i*6+2];
1085
        int b = src[i*6+4];
1086

    
1087
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1088
    }
1089
}
1090

    
1091
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1092
                             const uint8_t *src1, const uint8_t *src2,
1093
                             long width, uint32_t *unused)
1094
{
1095
    int i;
1096
    assert(src1==src2);
1097
    for (i = 0; i < width; i++) {
1098
        int r = src1[6*i + 0];
1099
        int g = src1[6*i + 2];
1100
        int b = src1[6*i + 4];
1101

    
1102
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1103
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1104
    }
1105
}
1106

    
1107
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1108
                                  const uint8_t *src1, const uint8_t *src2,
1109
                                  long width, uint32_t *unused)
1110
{
1111
    int i;
1112
    assert(src1==src2);
1113
    for (i = 0; i < width; i++) {
1114
        int r= src1[12*i + 0] + src1[12*i + 6];
1115
        int g= src1[12*i + 2] + src1[12*i + 8];
1116
        int b= src1[12*i + 4] + src1[12*i + 10];
1117

    
1118
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1119
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1120
    }
1121
}
1122

    
1123
static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
1124
                            uint32_t *unused)
1125
{
1126
    int i;
1127
    for (i = 0; i < width; i++) {
1128
        int b = src[i*6+0];
1129
        int g = src[i*6+2];
1130
        int r = src[i*6+4];
1131

    
1132
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1133
    }
1134
}
1135

    
1136
static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1137
                             const uint8_t *src1, const uint8_t *src2,
1138
                             long width, uint32_t *unused)
1139
{
1140
    int i;
1141
    for (i = 0; i < width; i++) {
1142
        int b = src1[6*i + 0];
1143
        int g = src1[6*i + 2];
1144
        int r = src1[6*i + 4];
1145

    
1146
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1147
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1148
    }
1149
}
1150

    
1151
static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1152
                                  const uint8_t *src1, const uint8_t *src2,
1153
                                  long width, uint32_t *unused)
1154
{
1155
    int i;
1156
    for (i = 0; i < width; i++) {
1157
        int b= src1[12*i + 0] + src1[12*i + 6];
1158
        int g= src1[12*i + 2] + src1[12*i + 8];
1159
        int r= src1[12*i + 4] + src1[12*i + 10];
1160

    
1161
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1162
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1163
    }
1164
}
1165

    
1166
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1167
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1168
{\
1169
    int i;\
1170
    for (i=0; i<width; i++) {\
1171
        int b= (((const type*)src)[i]>>shb)&maskb;\
1172
        int g= (((const type*)src)[i]>>shg)&maskg;\
1173
        int r= (((const type*)src)[i]>>shr)&maskr;\
1174
\
1175
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1176
    }\
1177
}
1178

    
1179
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1180
BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1181
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1182
BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1183
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1184
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1185
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1186
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1187

    
1188
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1189
{
1190
    int i;
1191
    for (i=0; i<width; i++) {
1192
        dst[i]= src[4*i];
1193
    }
1194
}
1195

    
1196
#define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1197
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1198
{\
1199
    int i;\
1200
    for (i=0; i<width; i++) {\
1201
        int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1202
        int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1203
        int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1204
\
1205
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1206
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1207
    }\
1208
}\
1209
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1210
{\
1211
    int i;\
1212
    for (i=0; i<width; i++) {\
1213
        int pix0= ((const type*)src)[2*i+0]>>shp;\
1214
        int pix1= ((const type*)src)[2*i+1]>>shp;\
1215
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1216
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1217
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1218
        g&= maskg|(2*maskg);\
1219
\
1220
        g>>=shg;\
1221
\
1222
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1223
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1224
    }\
1225
}
1226

    
1227
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1228
BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1229
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1230
BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1231
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1232
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1233
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1234
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1235

    
1236
static inline void palToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1237
{
1238
    int i;
1239
    for (i=0; i<width; i++) {
1240
        int d= src[i];
1241

    
1242
        dst[i]= pal[d] >> 24;
1243
    }
1244
}
1245

    
1246
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1247
{
1248
    int i;
1249
    for (i=0; i<width; i++) {
1250
        int d= src[i];
1251

    
1252
        dst[i]= pal[d] & 0xFF;
1253
    }
1254
}
1255

    
1256
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1257
                           const uint8_t *src1, const uint8_t *src2,
1258
                           long width, uint32_t *pal)
1259
{
1260
    int i;
1261
    assert(src1 == src2);
1262
    for (i=0; i<width; i++) {
1263
        int p= pal[src1[i]];
1264

    
1265
        dstU[i]= p>>8;
1266
        dstV[i]= p>>16;
1267
    }
1268
}
1269

    
1270
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1271
{
1272
    int i, j;
1273
    for (i=0; i<width/8; i++) {
1274
        int d= ~src[i];
1275
        for(j=0; j<8; j++)
1276
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1277
    }
1278
}
1279

    
1280
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1281
{
1282
    int i, j;
1283
    for (i=0; i<width/8; i++) {
1284
        int d= src[i];
1285
        for(j=0; j<8; j++)
1286
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1287
    }
1288
}
1289

    
1290
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1291
//Plain C versions
1292
#if CONFIG_RUNTIME_CPUDETECT
1293
#  define COMPILE_C 1
1294
#  if   ARCH_X86
1295
#    define COMPILE_MMX     1
1296
#    define COMPILE_MMX2    1
1297
#    define COMPILE_3DNOW   1
1298
#  elif ARCH_PPC
1299
#    define COMPILE_ALTIVEC HAVE_ALTIVEC
1300
#  endif
1301
#else /* CONFIG_RUNTIME_CPUDETECT */
1302
#  if   ARCH_X86
1303
#    if   HAVE_MMX2
1304
#      define COMPILE_MMX2  1
1305
#    elif HAVE_AMD3DNOW
1306
#      define COMPILE_3DNOW 1
1307
#    elif HAVE_MMX
1308
#      define COMPILE_MMX   1
1309
#    else
1310
#      define COMPILE_C     1
1311
#    endif
1312
#  elif ARCH_PPC && HAVE_ALTIVEC
1313
#    define COMPILE_ALTIVEC 1
1314
#  else
1315
#    define COMPILE_C       1
1316
#  endif
1317
#endif
1318

    
1319
#ifndef COMPILE_C
1320
#  define COMPILE_C 0
1321
#endif
1322
#ifndef COMPILE_MMX
1323
#  define COMPILE_MMX 0
1324
#endif
1325
#ifndef COMPILE_MMX2
1326
#  define COMPILE_MMX2 0
1327
#endif
1328
#ifndef COMPILE_3DNOW
1329
#  define COMPILE_3DNOW 0
1330
#endif
1331
#ifndef COMPILE_ALTIVEC
1332
#  define COMPILE_ALTIVEC 0
1333
#endif
1334

    
1335
#define COMPILE_TEMPLATE_MMX 0
1336
#define COMPILE_TEMPLATE_MMX2 0
1337
#define COMPILE_TEMPLATE_AMD3DNOW 0
1338
#define COMPILE_TEMPLATE_ALTIVEC 0
1339

    
1340
#if COMPILE_C
1341
#define RENAME(a) a ## _C
1342
#include "swscale_template.c"
1343
#endif
1344

    
1345
#if COMPILE_ALTIVEC
1346
#undef RENAME
1347
#undef COMPILE_TEMPLATE_ALTIVEC
1348
#define COMPILE_TEMPLATE_ALTIVEC 1
1349
#define RENAME(a) a ## _altivec
1350
#include "swscale_template.c"
1351
#endif
1352

    
1353
#if ARCH_X86
1354

    
1355
//MMX versions
1356
#if COMPILE_MMX
1357
#undef RENAME
1358
#undef COMPILE_TEMPLATE_MMX
1359
#undef COMPILE_TEMPLATE_MMX2
1360
#undef COMPILE_TEMPLATE_AMD3DNOW
1361
#define COMPILE_TEMPLATE_MMX 1
1362
#define COMPILE_TEMPLATE_MMX2 0
1363
#define COMPILE_TEMPLATE_AMD3DNOW 0
1364
#define RENAME(a) a ## _MMX
1365
#include "swscale_template.c"
1366
#endif
1367

    
1368
//MMX2 versions
1369
#if COMPILE_MMX2
1370
#undef RENAME
1371
#undef COMPILE_TEMPLATE_MMX
1372
#undef COMPILE_TEMPLATE_MMX2
1373
#undef COMPILE_TEMPLATE_AMD3DNOW
1374
#define COMPILE_TEMPLATE_MMX 1
1375
#define COMPILE_TEMPLATE_MMX2 1
1376
#define COMPILE_TEMPLATE_AMD3DNOW 0
1377
#define RENAME(a) a ## _MMX2
1378
#include "swscale_template.c"
1379
#endif
1380

    
1381
//3DNOW versions
1382
#if COMPILE_3DNOW
1383
#undef RENAME
1384
#undef COMPILE_TEMPLATE_MMX
1385
#undef COMPILE_TEMPLATE_MMX2
1386
#undef COMPILE_TEMPLATE_AMD3DNOW
1387
#define COMPILE_TEMPLATE_MMX 1
1388
#define COMPILE_TEMPLATE_MMX2 0
1389
#define COMPILE_TEMPLATE_AMD3DNOW 1
1390
#define RENAME(a) a ## _3DNow
1391
#include "swscale_template.c"
1392
#endif
1393

    
1394
#endif //ARCH_X86
1395

    
1396
SwsFunc ff_getSwsFunc(SwsContext *c)
1397
{
1398
#if CONFIG_RUNTIME_CPUDETECT
1399
    int flags = c->flags;
1400

    
1401
#if ARCH_X86
1402
    // ordered per speed fastest first
1403
    if (flags & SWS_CPU_CAPS_MMX2) {
1404
        sws_init_swScale_MMX2(c);
1405
        return swScale_MMX2;
1406
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1407
        sws_init_swScale_3DNow(c);
1408
        return swScale_3DNow;
1409
    } else if (flags & SWS_CPU_CAPS_MMX) {
1410
        sws_init_swScale_MMX(c);
1411
        return swScale_MMX;
1412
    } else {
1413
        sws_init_swScale_C(c);
1414
        return swScale_C;
1415
    }
1416

    
1417
#else
1418
#if COMPILE_ALTIVEC
1419
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1420
        sws_init_swScale_altivec(c);
1421
        return swScale_altivec;
1422
    } else {
1423
        sws_init_swScale_C(c);
1424
        return swScale_C;
1425
    }
1426
#endif
1427
    sws_init_swScale_C(c);
1428
    return swScale_C;
1429
#endif /* ARCH_X86 */
1430
#else //CONFIG_RUNTIME_CPUDETECT
1431
#if   COMPILE_TEMPLATE_MMX2
1432
    sws_init_swScale_MMX2(c);
1433
    return swScale_MMX2;
1434
#elif COMPILE_TEMPLATE_AMD3DNOW
1435
    sws_init_swScale_3DNow(c);
1436
    return swScale_3DNow;
1437
#elif COMPILE_TEMPLATE_MMX
1438
    sws_init_swScale_MMX(c);
1439
    return swScale_MMX;
1440
#elif COMPILE_TEMPLATE_ALTIVEC
1441
    sws_init_swScale_altivec(c);
1442
    return swScale_altivec;
1443
#else
1444
    sws_init_swScale_C(c);
1445
    return swScale_C;
1446
#endif
1447
#endif //!CONFIG_RUNTIME_CPUDETECT
1448
}
1449

    
1450
static void copyPlane(const uint8_t *src, int srcStride,
1451
                      int srcSliceY, int srcSliceH, int width,
1452
                      uint8_t *dst, int dstStride)
1453
{
1454
    dst += dstStride * srcSliceY;
1455
    if (dstStride == srcStride && srcStride > 0) {
1456
        memcpy(dst, src, srcSliceH * dstStride);
1457
    } else {
1458
        int i;
1459
        for (i=0; i<srcSliceH; i++) {
1460
            memcpy(dst, src, width);
1461
            src += srcStride;
1462
            dst += dstStride;
1463
        }
1464
    }
1465
}
1466

    
1467
static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1468
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1469
{
1470
    uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1471

    
1472
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1473
              dstParam[0], dstStride[0]);
1474

    
1475
    if (c->dstFormat == PIX_FMT_NV12)
1476
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1477
    else
1478
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1479

    
1480
    return srcSliceH;
1481
}
1482

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

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

    
1490
    return srcSliceH;
1491
}
1492

    
1493
static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1494
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1495
{
1496
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1497

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

    
1500
    return srcSliceH;
1501
}
1502

    
1503
static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1504
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1505
{
1506
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1507

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

    
1510
    return srcSliceH;
1511
}
1512

    
1513
static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1514
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1515
{
1516
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1517

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

    
1520
    return srcSliceH;
1521
}
1522

    
1523
static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1524
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1525
{
1526
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1527
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1528
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1529

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

    
1532
    if (dstParam[3])
1533
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1534

    
1535
    return srcSliceH;
1536
}
1537

    
1538
static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1539
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1540
{
1541
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1542
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1543
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1544

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

    
1547
    return srcSliceH;
1548
}
1549

    
1550
static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1551
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1552
{
1553
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1554
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1555
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1556

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

    
1559
    if (dstParam[3])
1560
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1561

    
1562
    return srcSliceH;
1563
}
1564

    
1565
static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1566
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1567
{
1568
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1569
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1570
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1571

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

    
1574
    return srcSliceH;
1575
}
1576

    
1577
static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1578
{
1579
    long i;
1580
    for (i=0; i<num_pixels; i++)
1581
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1582
}
1583

    
1584
static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1585
{
1586
    long i;
1587

    
1588
    for (i=0; i<num_pixels; i++)
1589
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1590
}
1591

    
1592
static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1593
{
1594
    long i;
1595

    
1596
    for (i=0; i<num_pixels; i++) {
1597
        //FIXME slow?
1598
        dst[0]= palette[src[i<<1]*4+0];
1599
        dst[1]= palette[src[i<<1]*4+1];
1600
        dst[2]= palette[src[i<<1]*4+2];
1601
        dst+= 3;
1602
    }
1603
}
1604

    
1605
static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1606
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1607
{
1608
    const enum PixelFormat srcFormat= c->srcFormat;
1609
    const enum PixelFormat dstFormat= c->dstFormat;
1610
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1611
                 const uint8_t *palette)=NULL;
1612
    int i;
1613
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1614
    const uint8_t *srcPtr= src[0];
1615

    
1616
    if (srcFormat == PIX_FMT_GRAY8A) {
1617
        switch (dstFormat) {
1618
        case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1619
        case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1620
        case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1621
        case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1622
        case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1623
        case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1624
        }
1625
    } else if (usePal(srcFormat)) {
1626
        switch (dstFormat) {
1627
        case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1628
        case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1629
        case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1630
        case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1631
        case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1632
        case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1633
        }
1634
    }
1635

    
1636
    if (!conv)
1637
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1638
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1639
    else {
1640
        for (i=0; i<srcSliceH; i++) {
1641
            conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1642
            srcPtr+= srcStride[0];
1643
            dstPtr+= dstStride[0];
1644
        }
1645
    }
1646

    
1647
    return srcSliceH;
1648
}
1649

    
1650
#define isRGBA32(x) (            \
1651
           (x) == PIX_FMT_ARGB   \
1652
        || (x) == PIX_FMT_RGBA   \
1653
        || (x) == PIX_FMT_BGRA   \
1654
        || (x) == PIX_FMT_ABGR   \
1655
        )
1656

    
1657
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1658
static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1659
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1660
{
1661
    const enum PixelFormat srcFormat= c->srcFormat;
1662
    const enum PixelFormat dstFormat= c->dstFormat;
1663
    const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1664
    const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1665
    const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1666
    const int dstId= c->dstFormatBpp >> 2;
1667
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1668

    
1669
#define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1670

    
1671
    if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1672
        if (     CONV_IS(ABGR, RGBA)
1673
              || CONV_IS(ARGB, BGRA)
1674
              || CONV_IS(BGRA, ARGB)
1675
              || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1676
        else if (CONV_IS(ABGR, ARGB)
1677
              || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1678
        else if (CONV_IS(ABGR, BGRA)
1679
              || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1680
        else if (CONV_IS(BGRA, RGBA)
1681
              || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1682
        else if (CONV_IS(BGRA, ABGR)
1683
              || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1684
    } else
1685
    /* BGR -> BGR */
1686
    if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1687
       || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1688
        switch(srcId | (dstId<<4)) {
1689
        case 0x34: conv= rgb16to15; break;
1690
        case 0x36: conv= rgb24to15; break;
1691
        case 0x38: conv= rgb32to15; break;
1692
        case 0x43: conv= rgb15to16; break;
1693
        case 0x46: conv= rgb24to16; break;
1694
        case 0x48: conv= rgb32to16; break;
1695
        case 0x63: conv= rgb15to24; break;
1696
        case 0x64: conv= rgb16to24; break;
1697
        case 0x68: conv= rgb32to24; break;
1698
        case 0x83: conv= rgb15to32; break;
1699
        case 0x84: conv= rgb16to32; break;
1700
        case 0x86: conv= rgb24to32; break;
1701
        }
1702
    } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1703
             || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1704
        switch(srcId | (dstId<<4)) {
1705
        case 0x33: conv= rgb15tobgr15; break;
1706
        case 0x34: conv= rgb16tobgr15; break;
1707
        case 0x36: conv= rgb24tobgr15; break;
1708
        case 0x38: conv= rgb32tobgr15; break;
1709
        case 0x43: conv= rgb15tobgr16; break;
1710
        case 0x44: conv= rgb16tobgr16; break;
1711
        case 0x46: conv= rgb24tobgr16; break;
1712
        case 0x48: conv= rgb32tobgr16; break;
1713
        case 0x63: conv= rgb15tobgr24; break;
1714
        case 0x64: conv= rgb16tobgr24; break;
1715
        case 0x66: conv= rgb24tobgr24; break;
1716
        case 0x68: conv= rgb32tobgr24; break;
1717
        case 0x83: conv= rgb15tobgr32; break;
1718
        case 0x84: conv= rgb16tobgr32; break;
1719
        case 0x86: conv= rgb24tobgr32; break;
1720
        }
1721
    }
1722

    
1723
    if (!conv) {
1724
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1725
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1726
    } else {
1727
        const uint8_t *srcPtr= src[0];
1728
              uint8_t *dstPtr= dst[0];
1729
        if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1730
            srcPtr += ALT32_CORR;
1731

    
1732
        if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1733
            dstPtr += ALT32_CORR;
1734

    
1735
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
1736
            conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1737
        else {
1738
            int i;
1739
            dstPtr += dstStride[0]*srcSliceY;
1740

    
1741
            for (i=0; i<srcSliceH; i++) {
1742
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
1743
                srcPtr+= srcStride[0];
1744
                dstPtr+= dstStride[0];
1745
            }
1746
        }
1747
    }
1748
    return srcSliceH;
1749
}
1750

    
1751
static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1752
                              int srcSliceH, uint8_t* dst[], int dstStride[])
1753
{
1754
    rgb24toyv12(
1755
        src[0],
1756
        dst[0]+ srcSliceY    *dstStride[0],
1757
        dst[1]+(srcSliceY>>1)*dstStride[1],
1758
        dst[2]+(srcSliceY>>1)*dstStride[2],
1759
        c->srcW, srcSliceH,
1760
        dstStride[0], dstStride[1], srcStride[0]);
1761
    if (dst[3])
1762
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1763
    return srcSliceH;
1764
}
1765

    
1766
static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1767
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1768
{
1769
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1770
              dst[0], dstStride[0]);
1771

    
1772
    planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1773
             srcSliceH >> 2, srcStride[1], dstStride[1]);
1774
    planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1775
             srcSliceH >> 2, srcStride[2], dstStride[2]);
1776
    if (dst[3])
1777
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1778
    return srcSliceH;
1779
}
1780

    
1781
/* unscaled copy like stuff (assumes nearly identical formats) */
1782
static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1783
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1784
{
1785
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1786
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1787
    else {
1788
        int i;
1789
        const uint8_t *srcPtr= src[0];
1790
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1791
        int length=0;
1792

    
1793
        /* universal length finder */
1794
        while(length+c->srcW <= FFABS(dstStride[0])
1795
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1796
        assert(length!=0);
1797

    
1798
        for (i=0; i<srcSliceH; i++) {
1799
            memcpy(dstPtr, srcPtr, length);
1800
            srcPtr+= srcStride[0];
1801
            dstPtr+= dstStride[0];
1802
        }
1803
    }
1804
    return srcSliceH;
1805
}
1806

    
1807
static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1808
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1809
{
1810
    int plane, i, j;
1811
    for (plane=0; plane<4; plane++) {
1812
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1813
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1814
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1815
        const uint8_t *srcPtr= src[plane];
1816
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1817

    
1818
        if (!dst[plane]) continue;
1819
        // ignore palette for GRAY8
1820
        if (plane == 1 && !dst[2]) continue;
1821
        if (!src[plane] || (plane == 1 && !src[2])) {
1822
            if(is16BPS(c->dstFormat))
1823
                length*=2;
1824
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1825
        } else {
1826
            if(isNBPS(c->srcFormat)) {
1827
                const int depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1828
                uint16_t *srcPtr2 = (uint16_t*)srcPtr;
1829

    
1830
                if (is16BPS(c->dstFormat)) {
1831
                    uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1832
                    for (i = 0; i < height; i++) {
1833
                        for (j = 0; j < length; j++)
1834
                            dstPtr2[j] = (srcPtr2[j]<<(16-depth)) | (srcPtr2[j]>>(2*depth-16));
1835
                        dstPtr2 += dstStride[plane]/2;
1836
                        srcPtr2 += srcStride[plane]/2;
1837
                    }
1838
                } else {
1839
                    for (i = 0; i < height; i++) {
1840
                        uint8_t *dither= dithers[depth-9][i&7];
1841
                        for (j = 0; j < length-7; j+=8){
1842
                            dstPtr[j+0] = (srcPtr2[j+0] + dither[0])>>(depth-8);
1843
                            dstPtr[j+1] = (srcPtr2[j+1] + dither[1])>>(depth-8);
1844
                            dstPtr[j+2] = (srcPtr2[j+2] + dither[2])>>(depth-8);
1845
                            dstPtr[j+3] = (srcPtr2[j+3] + dither[3])>>(depth-8);
1846
                            dstPtr[j+4] = (srcPtr2[j+4] + dither[4])>>(depth-8);
1847
                            dstPtr[j+5] = (srcPtr2[j+5] + dither[5])>>(depth-8);
1848
                            dstPtr[j+6] = (srcPtr2[j+6] + dither[6])>>(depth-8);
1849
                            dstPtr[j+7] = (srcPtr2[j+7] + dither[7])>>(depth-8);
1850
                        }
1851
                        for (; j < length; j++)
1852
                            dstPtr[j] = (srcPtr2[j] + dither[j&7])>>(depth-8);
1853
                        dstPtr  += dstStride[plane];
1854
                        srcPtr2 += srcStride[plane]/2;
1855
                    }
1856
                }
1857
            } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1858
                //FIXME add dither
1859
                if (!isBE(c->srcFormat)) srcPtr++;
1860
                for (i=0; i<height; i++) {
1861
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1862
                    srcPtr+= srcStride[plane];
1863
                    dstPtr+= dstStride[plane];
1864
                }
1865
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1866
                for (i=0; i<height; i++) {
1867
                    for (j=0; j<length; j++) {
1868
                        dstPtr[ j<<1   ] = srcPtr[j];
1869
                        dstPtr[(j<<1)+1] = srcPtr[j];
1870
                    }
1871
                    srcPtr+= srcStride[plane];
1872
                    dstPtr+= dstStride[plane];
1873
                }
1874
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1875
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1876

    
1877
                for (i=0; i<height; i++) {
1878
                    for (j=0; j<length; j++)
1879
                        ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1880
                    srcPtr+= srcStride[plane];
1881
                    dstPtr+= dstStride[plane];
1882
                }
1883
            } else if (dstStride[plane] == srcStride[plane] &&
1884
                       srcStride[plane] > 0 && srcStride[plane] == length) {
1885
                memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1886
                       height*dstStride[plane]);
1887
            } else {
1888
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1889
                    length*=2;
1890
                for (i=0; i<height; i++) {
1891
                    memcpy(dstPtr, srcPtr, length);
1892
                    srcPtr+= srcStride[plane];
1893
                    dstPtr+= dstStride[plane];
1894
                }
1895
            }
1896
        }
1897
    }
1898
    return srcSliceH;
1899
}
1900

    
1901
int ff_hardcodedcpuflags(void)
1902
{
1903
    int flags = 0;
1904
#if   COMPILE_TEMPLATE_MMX2
1905
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1906
#elif COMPILE_TEMPLATE_AMD3DNOW
1907
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1908
#elif COMPILE_TEMPLATE_MMX
1909
    flags |= SWS_CPU_CAPS_MMX;
1910
#elif COMPILE_TEMPLATE_ALTIVEC
1911
    flags |= SWS_CPU_CAPS_ALTIVEC;
1912
#elif ARCH_BFIN
1913
    flags |= SWS_CPU_CAPS_BFIN;
1914
#endif
1915
    return flags;
1916
}
1917

    
1918
void ff_get_unscaled_swscale(SwsContext *c)
1919
{
1920
    const enum PixelFormat srcFormat = c->srcFormat;
1921
    const enum PixelFormat dstFormat = c->dstFormat;
1922
    const int flags = c->flags;
1923
    const int dstH = c->dstH;
1924
    int needsDither;
1925

    
1926
    needsDither= isAnyRGB(dstFormat)
1927
        &&  c->dstFormatBpp < 24
1928
        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1929

    
1930
    /* yv12_to_nv12 */
1931
    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1932
        c->swScale= planarToNv12Wrapper;
1933
    }
1934
    /* yuv2bgr */
1935
    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1936
        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1937
        c->swScale= ff_yuv2rgb_get_func_ptr(c);
1938
    }
1939

    
1940
    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1941
        c->swScale= yvu9ToYv12Wrapper;
1942
    }
1943

    
1944
    /* bgr24toYV12 */
1945
    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1946
        c->swScale= bgr24ToYv12Wrapper;
1947

    
1948
    /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1949
    if (   isAnyRGB(srcFormat)
1950
        && isAnyRGB(dstFormat)
1951
        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1952
        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1953
        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1954
        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1955
        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1956
        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1957
        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1958
        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1959
        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1960
        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1961
        && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1962
        && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1963
        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1964
        c->swScale= rgbToRgbWrapper;
1965

    
1966
    if ((usePal(srcFormat) && (
1967
        dstFormat == PIX_FMT_RGB32   ||
1968
        dstFormat == PIX_FMT_RGB32_1 ||
1969
        dstFormat == PIX_FMT_RGB24   ||
1970
        dstFormat == PIX_FMT_BGR32   ||
1971
        dstFormat == PIX_FMT_BGR32_1 ||
1972
        dstFormat == PIX_FMT_BGR24)))
1973
        c->swScale= palToRgbWrapper;
1974

    
1975
    if (srcFormat == PIX_FMT_YUV422P) {
1976
        if (dstFormat == PIX_FMT_YUYV422)
1977
            c->swScale= yuv422pToYuy2Wrapper;
1978
        else if (dstFormat == PIX_FMT_UYVY422)
1979
            c->swScale= yuv422pToUyvyWrapper;
1980
    }
1981

    
1982
    /* LQ converters if -sws 0 or -sws 4*/
1983
    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1984
        /* yv12_to_yuy2 */
1985
        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1986
            if (dstFormat == PIX_FMT_YUYV422)
1987
                c->swScale= planarToYuy2Wrapper;
1988
            else if (dstFormat == PIX_FMT_UYVY422)
1989
                c->swScale= planarToUyvyWrapper;
1990
        }
1991
    }
1992
    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1993
        c->swScale= yuyvToYuv420Wrapper;
1994
    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1995
        c->swScale= uyvyToYuv420Wrapper;
1996
    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1997
        c->swScale= yuyvToYuv422Wrapper;
1998
    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1999
        c->swScale= uyvyToYuv422Wrapper;
2000

    
2001
#if COMPILE_ALTIVEC
2002
    if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2003
        !(c->flags & SWS_BITEXACT) &&
2004
        srcFormat == PIX_FMT_YUV420P) {
2005
        // unscaled YV12 -> packed YUV, we want speed
2006
        if (dstFormat == PIX_FMT_YUYV422)
2007
            c->swScale= yv12toyuy2_unscaled_altivec;
2008
        else if (dstFormat == PIX_FMT_UYVY422)
2009
            c->swScale= yv12touyvy_unscaled_altivec;
2010
    }
2011
#endif
2012

    
2013
    /* simple copy */
2014
    if (  srcFormat == dstFormat
2015
        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2016
        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2017
        || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2018
        || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2019
        || (isGray(dstFormat) && isGray(srcFormat))
2020
        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2021
            && c->chrDstHSubSample == c->chrSrcHSubSample
2022
            && c->chrDstVSubSample == c->chrSrcVSubSample
2023
            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2024
            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2025
    {
2026
        if (isPacked(c->srcFormat))
2027
            c->swScale= packedCopyWrapper;
2028
        else /* Planar YUV or gray */
2029
            c->swScale= planarCopyWrapper;
2030
    }
2031
#if ARCH_BFIN
2032
    if (flags & SWS_CPU_CAPS_BFIN)
2033
        ff_bfin_get_unscaled_swscale (c);
2034
#endif
2035
}
2036

    
2037
static void reset_ptr(const uint8_t* src[], int format)
2038
{
2039
    if(!isALPHA(format))
2040
        src[3]=NULL;
2041
    if(!isPlanarYUV(format)) {
2042
        src[3]=src[2]=NULL;
2043

    
2044
        if (!usePal(format))
2045
            src[1]= NULL;
2046
    }
2047
}
2048

    
2049
static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
2050
                                const int linesizes[4])
2051
{
2052
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
2053
    int i;
2054

    
2055
    for (i = 0; i < 4; i++) {
2056
        int plane = desc->comp[i].plane;
2057
        if (!data[plane] || !linesizes[plane])
2058
            return 0;
2059
    }
2060

    
2061
    return 1;
2062
}
2063

    
2064
/**
2065
 * swscale wrapper, so we don't need to export the SwsContext.
2066
 * Assumes planar YUV to be in YUV order instead of YVU.
2067
 */
2068
int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
2069
              int srcSliceH, uint8_t* const dst[], const int dstStride[])
2070
{
2071
    int i;
2072
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2073
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2074

    
2075
    // do not mess up sliceDir if we have a "trailing" 0-size slice
2076
    if (srcSliceH == 0)
2077
        return 0;
2078

    
2079
    if (!check_image_pointers(src, c->srcFormat, srcStride)) {
2080
        av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
2081
        return 0;
2082
    }
2083
    if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
2084
        av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
2085
        return 0;
2086
    }
2087

    
2088
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2089
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2090
        return 0;
2091
    }
2092
    if (c->sliceDir == 0) {
2093
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2094
    }
2095

    
2096
    if (usePal(c->srcFormat)) {
2097
        for (i=0; i<256; i++) {
2098
            int p, r, g, b, y, u, v, a = 0xff;
2099
            if(c->srcFormat == PIX_FMT_PAL8) {
2100
                p=((const uint32_t*)(src[1]))[i];
2101
                a= (p>>24)&0xFF;
2102
                r= (p>>16)&0xFF;
2103
                g= (p>> 8)&0xFF;
2104
                b=  p     &0xFF;
2105
            } else if(c->srcFormat == PIX_FMT_RGB8) {
2106
                r= (i>>5    )*36;
2107
                g= ((i>>2)&7)*36;
2108
                b= (i&3     )*85;
2109
            } else if(c->srcFormat == PIX_FMT_BGR8) {
2110
                b= (i>>6    )*85;
2111
                g= ((i>>3)&7)*36;
2112
                r= (i&7     )*36;
2113
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
2114
                r= (i>>3    )*255;
2115
                g= ((i>>1)&3)*85;
2116
                b= (i&1     )*255;
2117
            } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
2118
                r = g = b = i;
2119
            } else {
2120
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2121
                b= (i>>3    )*255;
2122
                g= ((i>>1)&3)*85;
2123
                r= (i&1     )*255;
2124
            }
2125
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2126
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2127
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2128
            c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
2129

    
2130
            switch(c->dstFormat) {
2131
            case PIX_FMT_BGR32:
2132
#if !HAVE_BIGENDIAN
2133
            case PIX_FMT_RGB24:
2134
#endif
2135
                c->pal_rgb[i]=  r + (g<<8) + (b<<16) + (a<<24);
2136
                break;
2137
            case PIX_FMT_BGR32_1:
2138
#if HAVE_BIGENDIAN
2139
            case PIX_FMT_BGR24:
2140
#endif
2141
                c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
2142
                break;
2143
            case PIX_FMT_RGB32_1:
2144
#if HAVE_BIGENDIAN
2145
            case PIX_FMT_RGB24:
2146
#endif
2147
                c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
2148
                break;
2149
            case PIX_FMT_RGB32:
2150
#if !HAVE_BIGENDIAN
2151
            case PIX_FMT_BGR24:
2152
#endif
2153
            default:
2154
                c->pal_rgb[i]=  b + (g<<8) + (r<<16) + (a<<24);
2155
            }
2156
        }
2157
    }
2158

    
2159
    // copy strides, so they can safely be modified
2160
    if (c->sliceDir == 1) {
2161
        // slices go from top to bottom
2162
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2163
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2164

    
2165
        reset_ptr(src2, c->srcFormat);
2166
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2167

    
2168
        /* reset slice direction at end of frame */
2169
        if (srcSliceY + srcSliceH == c->srcH)
2170
            c->sliceDir = 0;
2171

    
2172
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2173
    } else {
2174
        // slices go from bottom to top => we flip the image internally
2175
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2176
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2177

    
2178
        src2[0] += (srcSliceH-1)*srcStride[0];
2179
        if (!usePal(c->srcFormat))
2180
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2181
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2182
        src2[3] += (srcSliceH-1)*srcStride[3];
2183
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
2184
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2185
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2186
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
2187

    
2188
        reset_ptr(src2, c->srcFormat);
2189
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2190

    
2191
        /* reset slice direction at end of frame */
2192
        if (!srcSliceY)
2193
            c->sliceDir = 0;
2194

    
2195
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2196
    }
2197
}
2198

    
2199
#if LIBSWSCALE_VERSION_MAJOR < 1
2200
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2201
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2202
{
2203
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2204
}
2205
#endif
2206

    
2207
/* Convert the palette to the same packed 32-bit format as the palette */
2208
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2209
{
2210
    long i;
2211

    
2212
    for (i=0; i<num_pixels; i++)
2213
        ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2214
}
2215

    
2216
/* Palette format: ABCD -> dst format: ABC */
2217
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2218
{
2219
    long i;
2220

    
2221
    for (i=0; i<num_pixels; i++) {
2222
        //FIXME slow?
2223
        dst[0]= palette[src[i]*4+0];
2224
        dst[1]= palette[src[i]*4+1];
2225
        dst[2]= palette[src[i]*4+2];
2226
        dst+= 3;
2227
    }
2228
}