Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 6858492e

History | View | Annotate | Download (115 KB)

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

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

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

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

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

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

    
81
unsigned swscale_version(void)
82
{
83
    return LIBSWSCALE_VERSION_INT;
84
}
85

    
86
#undef MOVNTQ
87
#undef PAVGB
88

    
89
//#undef HAVE_MMX2
90
//#define HAVE_AMD3DNOW
91
//#undef HAVE_MMX
92
//#undef ARCH_X86
93
//#define WORDS_BIGENDIAN
94
#define DITHER1XBPP
95

    
96
#define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
97

    
98
#define RET 0xC3 //near return opcode for x86
99

    
100
#ifdef M_PI
101
#define PI M_PI
102
#else
103
#define PI 3.14159265358979323846
104
#endif
105

    
106
#define isSupportedIn(x)    (       \
107
           (x)==PIX_FMT_YUV420P     \
108
        || (x)==PIX_FMT_YUVA420P    \
109
        || (x)==PIX_FMT_YUYV422     \
110
        || (x)==PIX_FMT_UYVY422     \
111
        || (x)==PIX_FMT_RGB32       \
112
        || (x)==PIX_FMT_RGB32_1     \
113
        || (x)==PIX_FMT_BGR24       \
114
        || (x)==PIX_FMT_BGR565      \
115
        || (x)==PIX_FMT_BGR555      \
116
        || (x)==PIX_FMT_BGR32       \
117
        || (x)==PIX_FMT_BGR32_1     \
118
        || (x)==PIX_FMT_RGB24       \
119
        || (x)==PIX_FMT_RGB565      \
120
        || (x)==PIX_FMT_RGB555      \
121
        || (x)==PIX_FMT_GRAY8       \
122
        || (x)==PIX_FMT_YUV410P     \
123
        || (x)==PIX_FMT_YUV440P     \
124
        || (x)==PIX_FMT_GRAY16BE    \
125
        || (x)==PIX_FMT_GRAY16LE    \
126
        || (x)==PIX_FMT_YUV444P     \
127
        || (x)==PIX_FMT_YUV422P     \
128
        || (x)==PIX_FMT_YUV411P     \
129
        || (x)==PIX_FMT_PAL8        \
130
        || (x)==PIX_FMT_BGR8        \
131
        || (x)==PIX_FMT_RGB8        \
132
        || (x)==PIX_FMT_BGR4_BYTE   \
133
        || (x)==PIX_FMT_RGB4_BYTE   \
134
        || (x)==PIX_FMT_YUV440P     \
135
        || (x)==PIX_FMT_MONOWHITE   \
136
        || (x)==PIX_FMT_MONOBLACK   \
137
    )
138
#define isSupportedOut(x)   (       \
139
           (x)==PIX_FMT_YUV420P     \
140
        || (x)==PIX_FMT_YUYV422     \
141
        || (x)==PIX_FMT_UYVY422     \
142
        || (x)==PIX_FMT_YUV444P     \
143
        || (x)==PIX_FMT_YUV422P     \
144
        || (x)==PIX_FMT_YUV411P     \
145
        || isRGB(x)                 \
146
        || isBGR(x)                 \
147
        || (x)==PIX_FMT_NV12        \
148
        || (x)==PIX_FMT_NV21        \
149
        || (x)==PIX_FMT_GRAY16BE    \
150
        || (x)==PIX_FMT_GRAY16LE    \
151
        || (x)==PIX_FMT_GRAY8       \
152
        || (x)==PIX_FMT_YUV410P     \
153
        || (x)==PIX_FMT_YUV440P     \
154
    )
155
#define isPacked(x)         (       \
156
           (x)==PIX_FMT_PAL8        \
157
        || (x)==PIX_FMT_YUYV422     \
158
        || (x)==PIX_FMT_UYVY422     \
159
        || isRGB(x)                 \
160
        || isBGR(x)                 \
161
    )
162
#define usePal(x)           (       \
163
           (x)==PIX_FMT_PAL8        \
164
        || (x)==PIX_FMT_BGR4_BYTE   \
165
        || (x)==PIX_FMT_RGB4_BYTE   \
166
        || (x)==PIX_FMT_BGR8        \
167
        || (x)==PIX_FMT_RGB8        \
168
    )
169

    
170
#define RGB2YUV_SHIFT 15
171
#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
172
#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
173
#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
174
#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
175
#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
176
#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
177
#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
178
#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
179
#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
180

    
181
extern const int32_t ff_yuv2rgb_coeffs[8][4];
182

    
183
static const double rgb2yuv_table[8][9]={
184
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
185
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
186
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
187
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
188
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
189
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
190
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
191
    {0.701 , 0.087 , 0.212 , -0.384, 0.5  -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
192
};
193

    
194
/*
195
NOTES
196
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
197

198
TODO
199
more intelligent misalignment avoidance for the horizontal scaler
200
write special vertical cubic upscale version
201
optimize C code (YV12 / minmax)
202
add support for packed pixel YUV input & output
203
add support for Y8 output
204
optimize BGR24 & BGR32
205
add BGR4 output support
206
write special BGR->BGR scaler
207
*/
208

    
209
#if ARCH_X86 && CONFIG_GPL
210
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
211
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
212
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
213
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
214
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
215
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
216
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
217
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
218

    
219
const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
220
        0x0103010301030103LL,
221
        0x0200020002000200LL,};
222

    
223
const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
224
        0x0602060206020602LL,
225
        0x0004000400040004LL,};
226

    
227
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
228
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
229
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
230
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
231
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
232
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
233

    
234
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
235
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
236
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
237

    
238
#ifdef FAST_BGR2YV12
239
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
240
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
241
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
242
#else
243
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
244
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
245
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
246
#endif /* FAST_BGR2YV12 */
247
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
248
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
249
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
250

    
251
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
252
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
253
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
254
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
255
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
256

    
257
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
258
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
259
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
260
};
261

    
262
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
263

    
264
#endif /* ARCH_X86 && CONFIG_GPL */
265

    
266
// clipping helper table for C implementations:
267
static unsigned char clip_table[768];
268

    
269
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
270

    
271
static const uint8_t  __attribute__((aligned(8))) dither_2x2_4[2][8]={
272
{  1,   3,   1,   3,   1,   3,   1,   3, },
273
{  2,   0,   2,   0,   2,   0,   2,   0, },
274
};
275

    
276
static const uint8_t  __attribute__((aligned(8))) dither_2x2_8[2][8]={
277
{  6,   2,   6,   2,   6,   2,   6,   2, },
278
{  0,   4,   0,   4,   0,   4,   0,   4, },
279
};
280

    
281
const uint8_t  __attribute__((aligned(8))) dither_8x8_32[8][8]={
282
{ 17,   9,  23,  15,  16,   8,  22,  14, },
283
{  5,  29,   3,  27,   4,  28,   2,  26, },
284
{ 21,  13,  19,  11,  20,  12,  18,  10, },
285
{  0,  24,   6,  30,   1,  25,   7,  31, },
286
{ 16,   8,  22,  14,  17,   9,  23,  15, },
287
{  4,  28,   2,  26,   5,  29,   3,  27, },
288
{ 20,  12,  18,  10,  21,  13,  19,  11, },
289
{  1,  25,   7,  31,   0,  24,   6,  30, },
290
};
291

    
292
#if 0
293
const uint8_t  __attribute__((aligned(8))) dither_8x8_64[8][8]={
294
{  0,  48,  12,  60,   3,  51,  15,  63, },
295
{ 32,  16,  44,  28,  35,  19,  47,  31, },
296
{  8,  56,   4,  52,  11,  59,   7,  55, },
297
{ 40,  24,  36,  20,  43,  27,  39,  23, },
298
{  2,  50,  14,  62,   1,  49,  13,  61, },
299
{ 34,  18,  46,  30,  33,  17,  45,  29, },
300
{ 10,  58,   6,  54,   9,  57,   5,  53, },
301
{ 42,  26,  38,  22,  41,  25,  37,  21, },
302
};
303
#endif
304

    
305
const uint8_t  __attribute__((aligned(8))) dither_8x8_73[8][8]={
306
{  0,  55,  14,  68,   3,  58,  17,  72, },
307
{ 37,  18,  50,  32,  40,  22,  54,  35, },
308
{  9,  64,   5,  59,  13,  67,   8,  63, },
309
{ 46,  27,  41,  23,  49,  31,  44,  26, },
310
{  2,  57,  16,  71,   1,  56,  15,  70, },
311
{ 39,  21,  52,  34,  38,  19,  51,  33, },
312
{ 11,  66,   7,  62,  10,  65,   6,  60, },
313
{ 48,  30,  43,  25,  47,  29,  42,  24, },
314
};
315

    
316
#if 0
317
const uint8_t  __attribute__((aligned(8))) dither_8x8_128[8][8]={
318
{ 68,  36,  92,  60,  66,  34,  90,  58, },
319
{ 20, 116,  12, 108,  18, 114,  10, 106, },
320
{ 84,  52,  76,  44,  82,  50,  74,  42, },
321
{  0,  96,  24, 120,   6, 102,  30, 126, },
322
{ 64,  32,  88,  56,  70,  38,  94,  62, },
323
{ 16, 112,   8, 104,  22, 118,  14, 110, },
324
{ 80,  48,  72,  40,  86,  54,  78,  46, },
325
{  4, 100,  28, 124,   2,  98,  26, 122, },
326
};
327
#endif
328

    
329
#if 1
330
const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
331
{117,  62, 158, 103, 113,  58, 155, 100, },
332
{ 34, 199,  21, 186,  31, 196,  17, 182, },
333
{144,  89, 131,  76, 141,  86, 127,  72, },
334
{  0, 165,  41, 206,  10, 175,  52, 217, },
335
{110,  55, 151,  96, 120,  65, 162, 107, },
336
{ 28, 193,  14, 179,  38, 203,  24, 189, },
337
{138,  83, 124,  69, 148,  93, 134,  79, },
338
{  7, 172,  48, 213,   3, 168,  45, 210, },
339
};
340
#elif 1
341
// tries to correct a gamma of 1.5
342
const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
343
{  0, 143,  18, 200,   2, 156,  25, 215, },
344
{ 78,  28, 125,  64,  89,  36, 138,  74, },
345
{ 10, 180,   3, 161,  16, 195,   8, 175, },
346
{109,  51,  93,  38, 121,  60, 105,  47, },
347
{  1, 152,  23, 210,   0, 147,  20, 205, },
348
{ 85,  33, 134,  71,  81,  30, 130,  67, },
349
{ 14, 190,   6, 171,  12, 185,   5, 166, },
350
{117,  57, 101,  44, 113,  54,  97,  41, },
351
};
352
#elif 1
353
// tries to correct a gamma of 2.0
354
const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
355
{  0, 124,   8, 193,   0, 140,  12, 213, },
356
{ 55,  14, 104,  42,  66,  19, 119,  52, },
357
{  3, 168,   1, 145,   6, 187,   3, 162, },
358
{ 86,  31,  70,  21,  99,  39,  82,  28, },
359
{  0, 134,  11, 206,   0, 129,   9, 200, },
360
{ 62,  17, 114,  48,  58,  16, 109,  45, },
361
{  5, 181,   2, 157,   4, 175,   1, 151, },
362
{ 95,  36,  78,  26,  90,  34,  74,  24, },
363
};
364
#else
365
// tries to correct a gamma of 2.5
366
const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
367
{  0, 107,   3, 187,   0, 125,   6, 212, },
368
{ 39,   7,  86,  28,  49,  11, 102,  36, },
369
{  1, 158,   0, 131,   3, 180,   1, 151, },
370
{ 68,  19,  52,  12,  81,  25,  64,  17, },
371
{  0, 119,   5, 203,   0, 113,   4, 195, },
372
{ 45,   9,  96,  33,  42,   8,  91,  30, },
373
{  2, 172,   1, 144,   2, 165,   0, 137, },
374
{ 77,  23,  60,  15,  72,  21,  56,  14, },
375
};
376
#endif
377

    
378
const char *sws_format_name(enum PixelFormat format)
379
{
380
    switch (format) {
381
        case PIX_FMT_YUV420P:
382
            return "yuv420p";
383
        case PIX_FMT_YUVA420P:
384
            return "yuva420p";
385
        case PIX_FMT_YUYV422:
386
            return "yuyv422";
387
        case PIX_FMT_RGB24:
388
            return "rgb24";
389
        case PIX_FMT_BGR24:
390
            return "bgr24";
391
        case PIX_FMT_YUV422P:
392
            return "yuv422p";
393
        case PIX_FMT_YUV444P:
394
            return "yuv444p";
395
        case PIX_FMT_RGB32:
396
            return "rgb32";
397
        case PIX_FMT_YUV410P:
398
            return "yuv410p";
399
        case PIX_FMT_YUV411P:
400
            return "yuv411p";
401
        case PIX_FMT_RGB565:
402
            return "rgb565";
403
        case PIX_FMT_RGB555:
404
            return "rgb555";
405
        case PIX_FMT_GRAY16BE:
406
            return "gray16be";
407
        case PIX_FMT_GRAY16LE:
408
            return "gray16le";
409
        case PIX_FMT_GRAY8:
410
            return "gray8";
411
        case PIX_FMT_MONOWHITE:
412
            return "mono white";
413
        case PIX_FMT_MONOBLACK:
414
            return "mono black";
415
        case PIX_FMT_PAL8:
416
            return "Palette";
417
        case PIX_FMT_YUVJ420P:
418
            return "yuvj420p";
419
        case PIX_FMT_YUVJ422P:
420
            return "yuvj422p";
421
        case PIX_FMT_YUVJ444P:
422
            return "yuvj444p";
423
        case PIX_FMT_XVMC_MPEG2_MC:
424
            return "xvmc_mpeg2_mc";
425
        case PIX_FMT_XVMC_MPEG2_IDCT:
426
            return "xvmc_mpeg2_idct";
427
        case PIX_FMT_UYVY422:
428
            return "uyvy422";
429
        case PIX_FMT_UYYVYY411:
430
            return "uyyvyy411";
431
        case PIX_FMT_RGB32_1:
432
            return "rgb32x";
433
        case PIX_FMT_BGR32_1:
434
            return "bgr32x";
435
        case PIX_FMT_BGR32:
436
            return "bgr32";
437
        case PIX_FMT_BGR565:
438
            return "bgr565";
439
        case PIX_FMT_BGR555:
440
            return "bgr555";
441
        case PIX_FMT_BGR8:
442
            return "bgr8";
443
        case PIX_FMT_BGR4:
444
            return "bgr4";
445
        case PIX_FMT_BGR4_BYTE:
446
            return "bgr4 byte";
447
        case PIX_FMT_RGB8:
448
            return "rgb8";
449
        case PIX_FMT_RGB4:
450
            return "rgb4";
451
        case PIX_FMT_RGB4_BYTE:
452
            return "rgb4 byte";
453
        case PIX_FMT_NV12:
454
            return "nv12";
455
        case PIX_FMT_NV21:
456
            return "nv21";
457
        case PIX_FMT_YUV440P:
458
            return "yuv440p";
459
        case PIX_FMT_VDPAU_H264:
460
            return "vdpau_h264";
461
        case PIX_FMT_VDPAU_MPEG1:
462
            return "vdpau_mpeg1";
463
        case PIX_FMT_VDPAU_MPEG2:
464
            return "vdpau_mpeg2";
465
        case PIX_FMT_VDPAU_WMV3:
466
            return "vdpau_wmv3";
467
        case PIX_FMT_VDPAU_VC1:
468
            return "vdpau_vc1";
469
        default:
470
            return "Unknown format";
471
    }
472
}
473

    
474
static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
475
                               int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
476
                               int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
477
{
478
    //FIXME Optimize (just quickly written not optimized..)
479
    int i;
480
    for (i=0; i<dstW; i++)
481
    {
482
        int val=1<<18;
483
        int j;
484
        for (j=0; j<lumFilterSize; j++)
485
            val += lumSrc[j][i] * lumFilter[j];
486

    
487
        dest[i]= av_clip_uint8(val>>19);
488
    }
489

    
490
    if (uDest)
491
        for (i=0; i<chrDstW; i++)
492
        {
493
            int u=1<<18;
494
            int v=1<<18;
495
            int j;
496
            for (j=0; j<chrFilterSize; j++)
497
            {
498
                u += chrSrc[j][i] * chrFilter[j];
499
                v += chrSrc[j][i + VOFW] * chrFilter[j];
500
            }
501

    
502
            uDest[i]= av_clip_uint8(u>>19);
503
            vDest[i]= av_clip_uint8(v>>19);
504
        }
505

    
506
    if (CONFIG_SWSCALE_ALPHA && aDest)
507
        for (i=0; i<dstW; i++){
508
            int val=1<<18;
509
            int j;
510
            for (j=0; j<lumFilterSize; j++)
511
                val += alpSrc[j][i] * lumFilter[j];
512

    
513
            aDest[i]= av_clip_uint8(val>>19);
514
        }
515

    
516
}
517

    
518
static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
519
                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
520
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
521
{
522
    //FIXME Optimize (just quickly written not optimized..)
523
    int i;
524
    for (i=0; i<dstW; i++)
525
    {
526
        int val=1<<18;
527
        int j;
528
        for (j=0; j<lumFilterSize; j++)
529
            val += lumSrc[j][i] * lumFilter[j];
530

    
531
        dest[i]= av_clip_uint8(val>>19);
532
    }
533

    
534
    if (!uDest)
535
        return;
536

    
537
    if (dstFormat == PIX_FMT_NV12)
538
        for (i=0; i<chrDstW; i++)
539
        {
540
            int u=1<<18;
541
            int v=1<<18;
542
            int j;
543
            for (j=0; j<chrFilterSize; j++)
544
            {
545
                u += chrSrc[j][i] * chrFilter[j];
546
                v += chrSrc[j][i + VOFW] * chrFilter[j];
547
            }
548

    
549
            uDest[2*i]= av_clip_uint8(u>>19);
550
            uDest[2*i+1]= av_clip_uint8(v>>19);
551
        }
552
    else
553
        for (i=0; i<chrDstW; i++)
554
        {
555
            int u=1<<18;
556
            int v=1<<18;
557
            int j;
558
            for (j=0; j<chrFilterSize; j++)
559
            {
560
                u += chrSrc[j][i] * chrFilter[j];
561
                v += chrSrc[j][i + VOFW] * chrFilter[j];
562
            }
563

    
564
            uDest[2*i]= av_clip_uint8(v>>19);
565
            uDest[2*i+1]= av_clip_uint8(u>>19);
566
        }
567
}
568

    
569
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
570
    for (i=0; i<(dstW>>1); i++){\
571
        int j;\
572
        int Y1 = 1<<18;\
573
        int Y2 = 1<<18;\
574
        int U  = 1<<18;\
575
        int V  = 1<<18;\
576
        int av_unused A1, A2;\
577
        type av_unused *r, *b, *g;\
578
        const int i2= 2*i;\
579
        \
580
        for (j=0; j<lumFilterSize; j++)\
581
        {\
582
            Y1 += lumSrc[j][i2] * lumFilter[j];\
583
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
584
        }\
585
        for (j=0; j<chrFilterSize; j++)\
586
        {\
587
            U += chrSrc[j][i] * chrFilter[j];\
588
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
589
        }\
590
        Y1>>=19;\
591
        Y2>>=19;\
592
        U >>=19;\
593
        V >>=19;\
594
        if (alpha){\
595
            A1 = 1<<18;\
596
            A2 = 1<<18;\
597
            for (j=0; j<lumFilterSize; j++){\
598
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
599
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
600
            }\
601
            A1>>=19;\
602
            A2>>=19;\
603
        }\
604

    
605
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
606
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
607
        if ((Y1|Y2|U|V)&256)\
608
        {\
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
            if (U>255)    U=255;  \
614
            else if (U<0) U=0;    \
615
            if (V>255)    V=255;  \
616
            else if (V<0) V=0;    \
617
        }\
618
        if (alpha && ((A1|A2)&256)){\
619
            A1=av_clip_uint8(A1);\
620
            A2=av_clip_uint8(A2);\
621
        }
622

    
623
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
624
    for (i=0; i<dstW; i++){\
625
        int j;\
626
        int Y = 0;\
627
        int U = -128<<19;\
628
        int V = -128<<19;\
629
        int av_unused A;\
630
        int R,G,B;\
631
        \
632
        for (j=0; j<lumFilterSize; j++){\
633
            Y += lumSrc[j][i     ] * lumFilter[j];\
634
        }\
635
        for (j=0; j<chrFilterSize; j++){\
636
            U += chrSrc[j][i     ] * chrFilter[j];\
637
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
638
        }\
639
        Y >>=10;\
640
        U >>=10;\
641
        V >>=10;\
642
        if (alpha){\
643
            A = rnd;\
644
            for (j=0; j<lumFilterSize; j++)\
645
                A += alpSrc[j][i     ] * lumFilter[j];\
646
            A >>=19;\
647
            if (A&256)\
648
                A = av_clip_uint8(A);\
649
        }\
650

    
651
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
652
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
653
        Y-= c->yuv2rgb_y_offset;\
654
        Y*= c->yuv2rgb_y_coeff;\
655
        Y+= rnd;\
656
        R= Y + V*c->yuv2rgb_v2r_coeff;\
657
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
658
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
659
        if ((R|G|B)&(0xC0000000)){\
660
            if (R>=(256<<22))   R=(256<<22)-1; \
661
            else if (R<0)R=0;   \
662
            if (G>=(256<<22))   G=(256<<22)-1; \
663
            else if (G<0)G=0;   \
664
            if (B>=(256<<22))   B=(256<<22)-1; \
665
            else if (B<0)B=0;   \
666
        }\
667

    
668

    
669
#define YSCALE_YUV_2_GRAY16_C \
670
    for (i=0; i<(dstW>>1); i++){\
671
        int j;\
672
        int Y1 = 1<<18;\
673
        int Y2 = 1<<18;\
674
        int U  = 1<<18;\
675
        int V  = 1<<18;\
676
        \
677
        const int i2= 2*i;\
678
        \
679
        for (j=0; j<lumFilterSize; j++)\
680
        {\
681
            Y1 += lumSrc[j][i2] * lumFilter[j];\
682
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
683
        }\
684
        Y1>>=11;\
685
        Y2>>=11;\
686
        if ((Y1|Y2|U|V)&65536)\
687
        {\
688
            if (Y1>65535)   Y1=65535; \
689
            else if (Y1<0)Y1=0;   \
690
            if (Y2>65535)   Y2=65535; \
691
            else if (Y2<0)Y2=0;   \
692
        }
693

    
694
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
695
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
696
    r = (type *)c->table_rV[V];   \
697
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
698
    b = (type *)c->table_bU[U];   \
699

    
700
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
701
    for (i=0; i<(dstW>>1); i++){ \
702
        const int i2= 2*i;       \
703
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
704
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
705
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
706
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
707
        type av_unused *r, *b, *g;                                    \
708
        int av_unused A1, A2;                                         \
709
        if (alpha){\
710
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
711
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
712
        }\
713

    
714
#define YSCALE_YUV_2_GRAY16_2_C   \
715
    for (i=0; i<(dstW>>1); i++){ \
716
        const int i2= 2*i;       \
717
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
718
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
719

    
720
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
721
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
722
    r = (type *)c->table_rV[V];\
723
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
724
    b = (type *)c->table_bU[U];\
725

    
726
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
727
    for (i=0; i<(dstW>>1); i++){\
728
        const int i2= 2*i;\
729
        int Y1= buf0[i2  ]>>7;\
730
        int Y2= buf0[i2+1]>>7;\
731
        int U= (uvbuf1[i     ])>>7;\
732
        int V= (uvbuf1[i+VOFW])>>7;\
733
        type av_unused *r, *b, *g;\
734
        int av_unused A1, A2;\
735
        if (alpha){\
736
            A1= abuf0[i2  ]>>7;\
737
            A2= abuf0[i2+1]>>7;\
738
        }\
739

    
740
#define YSCALE_YUV_2_GRAY16_1_C \
741
    for (i=0; i<(dstW>>1); i++){\
742
        const int i2= 2*i;\
743
        int Y1= buf0[i2  ]<<1;\
744
        int Y2= buf0[i2+1]<<1;\
745

    
746
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
747
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
748
    r = (type *)c->table_rV[V];\
749
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
750
    b = (type *)c->table_bU[U];\
751

    
752
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
753
    for (i=0; i<(dstW>>1); i++){\
754
        const int i2= 2*i;\
755
        int Y1= buf0[i2  ]>>7;\
756
        int Y2= buf0[i2+1]>>7;\
757
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
758
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
759
        type av_unused *r, *b, *g;\
760
        int av_unused A1, A2;\
761
        if (alpha){\
762
            A1= abuf0[i2  ]>>7;\
763
            A2= abuf0[i2+1]>>7;\
764
        }\
765

    
766
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
767
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
768
    r = (type *)c->table_rV[V];\
769
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
770
    b = (type *)c->table_bU[U];\
771

    
772
#define YSCALE_YUV_2_MONO2_C \
773
    const uint8_t * const d128=dither_8x8_220[y&7];\
774
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
775
    for (i=0; i<dstW-7; i+=8){\
776
        int acc;\
777
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
778
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
779
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
780
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
781
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
782
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
783
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
784
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
785
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
786
        dest++;\
787
    }\
788

    
789

    
790
#define YSCALE_YUV_2_MONOX_C \
791
    const uint8_t * const d128=dither_8x8_220[y&7];\
792
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
793
    int acc=0;\
794
    for (i=0; i<dstW-1; i+=2){\
795
        int j;\
796
        int Y1=1<<18;\
797
        int Y2=1<<18;\
798
\
799
        for (j=0; j<lumFilterSize; j++)\
800
        {\
801
            Y1 += lumSrc[j][i] * lumFilter[j];\
802
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
803
        }\
804
        Y1>>=19;\
805
        Y2>>=19;\
806
        if ((Y1|Y2)&256)\
807
        {\
808
            if (Y1>255)   Y1=255;\
809
            else if (Y1<0)Y1=0;\
810
            if (Y2>255)   Y2=255;\
811
            else if (Y2<0)Y2=0;\
812
        }\
813
        acc+= acc + g[Y1+d128[(i+0)&7]];\
814
        acc+= acc + g[Y2+d128[(i+1)&7]];\
815
        if ((i&7)==6){\
816
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
817
            dest++;\
818
        }\
819
    }
820

    
821

    
822
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
823
    switch(c->dstFormat)\
824
    {\
825
    case PIX_FMT_RGBA:\
826
    case PIX_FMT_BGRA:\
827
        if (CONFIG_SMALL){\
828
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
829
            func(uint32_t,needAlpha)\
830
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
831
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
832
            }\
833
        }else{\
834
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
835
                func(uint32_t,1)\
836
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
837
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
838
                }\
839
            }else{\
840
                func(uint32_t,0)\
841
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
842
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
843
                }\
844
            }\
845
        }\
846
        break;\
847
    case PIX_FMT_ARGB:\
848
    case PIX_FMT_ABGR:\
849
        if (CONFIG_SMALL){\
850
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
851
            func(uint32_t,needAlpha)\
852
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
853
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
854
            }\
855
        }else{\
856
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
857
                func(uint32_t,1)\
858
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
859
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
860
                }\
861
            }else{\
862
                func(uint32_t,0)\
863
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
864
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
865
                }\
866
            }\
867
        }                \
868
        break;\
869
    case PIX_FMT_RGB24:\
870
        func(uint8_t,0)\
871
            ((uint8_t*)dest)[0]= r[Y1];\
872
            ((uint8_t*)dest)[1]= g[Y1];\
873
            ((uint8_t*)dest)[2]= b[Y1];\
874
            ((uint8_t*)dest)[3]= r[Y2];\
875
            ((uint8_t*)dest)[4]= g[Y2];\
876
            ((uint8_t*)dest)[5]= b[Y2];\
877
            dest+=6;\
878
        }\
879
        break;\
880
    case PIX_FMT_BGR24:\
881
        func(uint8_t,0)\
882
            ((uint8_t*)dest)[0]= b[Y1];\
883
            ((uint8_t*)dest)[1]= g[Y1];\
884
            ((uint8_t*)dest)[2]= r[Y1];\
885
            ((uint8_t*)dest)[3]= b[Y2];\
886
            ((uint8_t*)dest)[4]= g[Y2];\
887
            ((uint8_t*)dest)[5]= r[Y2];\
888
            dest+=6;\
889
        }\
890
        break;\
891
    case PIX_FMT_RGB565:\
892
    case PIX_FMT_BGR565:\
893
        {\
894
            const int dr1= dither_2x2_8[y&1    ][0];\
895
            const int dg1= dither_2x2_4[y&1    ][0];\
896
            const int db1= dither_2x2_8[(y&1)^1][0];\
897
            const int dr2= dither_2x2_8[y&1    ][1];\
898
            const int dg2= dither_2x2_4[y&1    ][1];\
899
            const int db2= dither_2x2_8[(y&1)^1][1];\
900
            func(uint16_t,0)\
901
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
902
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
903
            }\
904
        }\
905
        break;\
906
    case PIX_FMT_RGB555:\
907
    case PIX_FMT_BGR555:\
908
        {\
909
            const int dr1= dither_2x2_8[y&1    ][0];\
910
            const int dg1= dither_2x2_8[y&1    ][1];\
911
            const int db1= dither_2x2_8[(y&1)^1][0];\
912
            const int dr2= dither_2x2_8[y&1    ][1];\
913
            const int dg2= dither_2x2_8[y&1    ][0];\
914
            const int db2= dither_2x2_8[(y&1)^1][1];\
915
            func(uint16_t,0)\
916
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
917
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
918
            }\
919
        }\
920
        break;\
921
    case PIX_FMT_RGB8:\
922
    case PIX_FMT_BGR8:\
923
        {\
924
            const uint8_t * const d64= dither_8x8_73[y&7];\
925
            const uint8_t * const d32= dither_8x8_32[y&7];\
926
            func(uint8_t,0)\
927
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
928
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
929
            }\
930
        }\
931
        break;\
932
    case PIX_FMT_RGB4:\
933
    case PIX_FMT_BGR4:\
934
        {\
935
            const uint8_t * const d64= dither_8x8_73 [y&7];\
936
            const uint8_t * const d128=dither_8x8_220[y&7];\
937
            func(uint8_t,0)\
938
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
939
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
940
            }\
941
        }\
942
        break;\
943
    case PIX_FMT_RGB4_BYTE:\
944
    case PIX_FMT_BGR4_BYTE:\
945
        {\
946
            const uint8_t * const d64= dither_8x8_73 [y&7];\
947
            const uint8_t * const d128=dither_8x8_220[y&7];\
948
            func(uint8_t,0)\
949
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
950
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
951
            }\
952
        }\
953
        break;\
954
    case PIX_FMT_MONOBLACK:\
955
    case PIX_FMT_MONOWHITE:\
956
        {\
957
            func_monoblack\
958
        }\
959
        break;\
960
    case PIX_FMT_YUYV422:\
961
        func2\
962
            ((uint8_t*)dest)[2*i2+0]= Y1;\
963
            ((uint8_t*)dest)[2*i2+1]= U;\
964
            ((uint8_t*)dest)[2*i2+2]= Y2;\
965
            ((uint8_t*)dest)[2*i2+3]= V;\
966
        }                \
967
        break;\
968
    case PIX_FMT_UYVY422:\
969
        func2\
970
            ((uint8_t*)dest)[2*i2+0]= U;\
971
            ((uint8_t*)dest)[2*i2+1]= Y1;\
972
            ((uint8_t*)dest)[2*i2+2]= V;\
973
            ((uint8_t*)dest)[2*i2+3]= Y2;\
974
        }                \
975
        break;\
976
    case PIX_FMT_GRAY16BE:\
977
        func_g16\
978
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
979
            ((uint8_t*)dest)[2*i2+1]= Y1;\
980
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
981
            ((uint8_t*)dest)[2*i2+3]= Y2;\
982
        }                \
983
        break;\
984
    case PIX_FMT_GRAY16LE:\
985
        func_g16\
986
            ((uint8_t*)dest)[2*i2+0]= Y1;\
987
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
988
            ((uint8_t*)dest)[2*i2+2]= Y2;\
989
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
990
        }                \
991
        break;\
992
    }\
993

    
994

    
995
static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
996
                                  int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
997
                                  int16_t **alpSrc, uint8_t *dest, int dstW, int y)
998
{
999
    int i;
1000
    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)
1001
}
1002

    
1003
static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
1004
                                    int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
1005
                                    int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1006
{
1007
    int i;
1008
    int step= fmt_depth(c->dstFormat)/8;
1009
    int aidx= 3;
1010

    
1011
    switch(c->dstFormat){
1012
    case PIX_FMT_ARGB:
1013
        dest++;
1014
        aidx= 0;
1015
    case PIX_FMT_RGB24:
1016
        aidx--;
1017
    case PIX_FMT_RGBA:
1018
        if (CONFIG_SMALL){
1019
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1020
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1021
                dest[aidx]= needAlpha ? A : 255;
1022
                dest[0]= R>>22;
1023
                dest[1]= G>>22;
1024
                dest[2]= B>>22;
1025
                dest+= step;
1026
            }
1027
        }else{
1028
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1029
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1030
                    dest[aidx]= A;
1031
                    dest[0]= R>>22;
1032
                    dest[1]= G>>22;
1033
                    dest[2]= B>>22;
1034
                    dest+= step;
1035
                }
1036
            }else{
1037
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1038
                    dest[aidx]= 255;
1039
                    dest[0]= R>>22;
1040
                    dest[1]= G>>22;
1041
                    dest[2]= B>>22;
1042
                    dest+= step;
1043
                }
1044
            }
1045
        }
1046
        break;
1047
    case PIX_FMT_ABGR:
1048
        dest++;
1049
        aidx= 0;
1050
    case PIX_FMT_BGR24:
1051
        aidx--;
1052
    case PIX_FMT_BGRA:
1053
        if (CONFIG_SMALL){
1054
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1055
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1056
                dest[aidx]= needAlpha ? A : 255;
1057
                dest[0]= B>>22;
1058
                dest[1]= G>>22;
1059
                dest[2]= R>>22;
1060
                dest+= step;
1061
            }
1062
        }else{
1063
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1064
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1065
                    dest[aidx]= A;
1066
                    dest[0]= B>>22;
1067
                    dest[1]= G>>22;
1068
                    dest[2]= R>>22;
1069
                    dest+= step;
1070
                }
1071
            }else{
1072
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1073
                    dest[aidx]= 255;
1074
                    dest[0]= B>>22;
1075
                    dest[1]= G>>22;
1076
                    dest[2]= R>>22;
1077
                    dest+= step;
1078
                }
1079
            }
1080
        }
1081
        break;
1082
    default:
1083
        assert(0);
1084
    }
1085
}
1086

    
1087
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1088
    int i;
1089
    uint8_t *ptr = plane + stride*y;
1090
    for (i=0; i<height; i++){
1091
        memset(ptr, val, width);
1092
        ptr += stride;
1093
    }
1094
}
1095

    
1096
//Note: we have C, X86, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1097
//Plain C versions
1098
#if !HAVE_MMX || defined (RUNTIME_CPUDETECT) || !CONFIG_GPL
1099
#define COMPILE_C
1100
#endif
1101

    
1102
#if ARCH_PPC
1103
#if (HAVE_ALTIVEC || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1104
#undef COMPILE_C
1105
#define COMPILE_ALTIVEC
1106
#endif
1107
#endif //ARCH_PPC
1108

    
1109
#if ARCH_X86
1110

    
1111
#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1112
#define COMPILE_MMX
1113
#endif
1114

    
1115
#if (HAVE_MMX2 || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1116
#define COMPILE_MMX2
1117
#endif
1118

    
1119
#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1120
#define COMPILE_3DNOW
1121
#endif
1122
#endif //ARCH_X86
1123

    
1124
#undef HAVE_MMX
1125
#undef HAVE_MMX2
1126
#undef HAVE_AMD3DNOW
1127
#undef HAVE_ALTIVEC
1128
#define HAVE_MMX 0
1129
#define HAVE_MMX2 0
1130
#define HAVE_AMD3DNOW 0
1131
#define HAVE_ALTIVEC 0
1132

    
1133
#ifdef COMPILE_C
1134
#define RENAME(a) a ## _C
1135
#include "swscale_template.c"
1136
#endif
1137

    
1138
#ifdef COMPILE_ALTIVEC
1139
#undef RENAME
1140
#undef HAVE_ALTIVEC
1141
#define HAVE_ALTIVEC 1
1142
#define RENAME(a) a ## _altivec
1143
#include "swscale_template.c"
1144
#endif
1145

    
1146
#if ARCH_X86
1147

    
1148
//x86 versions
1149
/*
1150
#undef RENAME
1151
#undef HAVE_MMX
1152
#undef HAVE_MMX2
1153
#undef HAVE_AMD3DNOW
1154
#define ARCH_X86
1155
#define RENAME(a) a ## _X86
1156
#include "swscale_template.c"
1157
*/
1158
//MMX versions
1159
#ifdef COMPILE_MMX
1160
#undef RENAME
1161
#undef HAVE_MMX
1162
#undef HAVE_MMX2
1163
#undef HAVE_AMD3DNOW
1164
#define HAVE_MMX 1
1165
#define HAVE_MMX2 0
1166
#define HAVE_AMD3DNOW 0
1167
#define RENAME(a) a ## _MMX
1168
#include "swscale_template.c"
1169
#endif
1170

    
1171
//MMX2 versions
1172
#ifdef COMPILE_MMX2
1173
#undef RENAME
1174
#undef HAVE_MMX
1175
#undef HAVE_MMX2
1176
#undef HAVE_AMD3DNOW
1177
#define HAVE_MMX 1
1178
#define HAVE_MMX2 1
1179
#define HAVE_AMD3DNOW 0
1180
#define RENAME(a) a ## _MMX2
1181
#include "swscale_template.c"
1182
#endif
1183

    
1184
//3DNOW versions
1185
#ifdef COMPILE_3DNOW
1186
#undef RENAME
1187
#undef HAVE_MMX
1188
#undef HAVE_MMX2
1189
#undef HAVE_AMD3DNOW
1190
#define HAVE_MMX 1
1191
#define HAVE_MMX2 0
1192
#define HAVE_AMD3DNOW 1
1193
#define RENAME(a) a ## _3DNow
1194
#include "swscale_template.c"
1195
#endif
1196

    
1197
#endif //ARCH_X86
1198

    
1199
// minor note: the HAVE_xyz are messed up after this line so don't use them
1200

    
1201
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1202
{
1203
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1204
    if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
1205
    else                return getSplineCoeff(        0.0,
1206
                                             b+ 2.0*c + 3.0*d,
1207
                                                    c + 3.0*d,
1208
                                            -b- 3.0*c - 6.0*d,
1209
                                            dist-1.0);
1210
}
1211

    
1212
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1213
                             int srcW, int dstW, int filterAlign, int one, int flags,
1214
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1215
{
1216
    int i;
1217
    int filterSize;
1218
    int filter2Size;
1219
    int minFilterSize;
1220
    int64_t *filter=NULL;
1221
    int64_t *filter2=NULL;
1222
    const int64_t fone= 1LL<<54;
1223
    int ret= -1;
1224
#if ARCH_X86
1225
    if (flags & SWS_CPU_CAPS_MMX)
1226
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1227
#endif
1228

    
1229
    // NOTE: the +1 is for the MMX scaler which reads over the end
1230
    *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1231

    
1232
    if (FFABS(xInc - 0x10000) <10) // unscaled
1233
    {
1234
        int i;
1235
        filterSize= 1;
1236
        filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1237

    
1238
        for (i=0; i<dstW; i++)
1239
        {
1240
            filter[i*filterSize]= fone;
1241
            (*filterPos)[i]=i;
1242
        }
1243

    
1244
    }
1245
    else if (flags&SWS_POINT) // lame looking point sampling mode
1246
    {
1247
        int i;
1248
        int xDstInSrc;
1249
        filterSize= 1;
1250
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1251

    
1252
        xDstInSrc= xInc/2 - 0x8000;
1253
        for (i=0; i<dstW; i++)
1254
        {
1255
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1256

    
1257
            (*filterPos)[i]= xx;
1258
            filter[i]= fone;
1259
            xDstInSrc+= xInc;
1260
        }
1261
    }
1262
    else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1263
    {
1264
        int i;
1265
        int xDstInSrc;
1266
        if      (flags&SWS_BICUBIC) filterSize= 4;
1267
        else if (flags&SWS_X      ) filterSize= 4;
1268
        else                        filterSize= 2; // SWS_BILINEAR / SWS_AREA
1269
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1270

    
1271
        xDstInSrc= xInc/2 - 0x8000;
1272
        for (i=0; i<dstW; i++)
1273
        {
1274
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1275
            int j;
1276

    
1277
            (*filterPos)[i]= xx;
1278
                //bilinear upscale / linear interpolate / area averaging
1279
                for (j=0; j<filterSize; j++)
1280
                {
1281
                    int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1282
                    if (coeff<0) coeff=0;
1283
                    filter[i*filterSize + j]= coeff;
1284
                    xx++;
1285
                }
1286
            xDstInSrc+= xInc;
1287
        }
1288
    }
1289
    else
1290
    {
1291
        int xDstInSrc;
1292
        int sizeFactor;
1293

    
1294
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1295
        else if (flags&SWS_X)            sizeFactor=  8;
1296
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1297
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1298
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1299
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1300
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1301
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1302
        else {
1303
            sizeFactor= 0; //GCC warning killer
1304
            assert(0);
1305
        }
1306

    
1307
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1308
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1309

    
1310
        if (filterSize > srcW-2) filterSize=srcW-2;
1311

    
1312
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1313

    
1314
        xDstInSrc= xInc - 0x10000;
1315
        for (i=0; i<dstW; i++)
1316
        {
1317
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1318
            int j;
1319
            (*filterPos)[i]= xx;
1320
            for (j=0; j<filterSize; j++)
1321
            {
1322
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1323
                double floatd;
1324
                int64_t coeff;
1325

    
1326
                if (xInc > 1<<16)
1327
                    d= d*dstW/srcW;
1328
                floatd= d * (1.0/(1<<30));
1329

    
1330
                if (flags & SWS_BICUBIC)
1331
                {
1332
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1333
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1334
                    int64_t dd = ( d*d)>>30;
1335
                    int64_t ddd= (dd*d)>>30;
1336

    
1337
                    if      (d < 1LL<<30)
1338
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1339
                    else if (d < 1LL<<31)
1340
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1341
                    else
1342
                        coeff=0.0;
1343
                    coeff *= fone>>(30+24);
1344
                }
1345
/*                else if (flags & SWS_X)
1346
                {
1347
                    double p= param ? param*0.01 : 0.3;
1348
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1349
                    coeff*= pow(2.0, - p*d*d);
1350
                }*/
1351
                else if (flags & SWS_X)
1352
                {
1353
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1354
                    double c;
1355

    
1356
                    if (floatd<1.0)
1357
                        c = cos(floatd*PI);
1358
                    else
1359
                        c=-1.0;
1360
                    if (c<0.0)      c= -pow(-c, A);
1361
                    else            c=  pow( c, A);
1362
                    coeff= (c*0.5 + 0.5)*fone;
1363
                }
1364
                else if (flags & SWS_AREA)
1365
                {
1366
                    int64_t d2= d - (1<<29);
1367
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1368
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1369
                    else coeff=0.0;
1370
                    coeff *= fone>>(30+16);
1371
                }
1372
                else if (flags & SWS_GAUSS)
1373
                {
1374
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1375
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
1376
                }
1377
                else if (flags & SWS_SINC)
1378
                {
1379
                    coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1380
                }
1381
                else if (flags & SWS_LANCZOS)
1382
                {
1383
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1384
                    coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1385
                    if (floatd>p) coeff=0;
1386
                }
1387
                else if (flags & SWS_BILINEAR)
1388
                {
1389
                    coeff= (1<<30) - d;
1390
                    if (coeff<0) coeff=0;
1391
                    coeff *= fone >> 30;
1392
                }
1393
                else if (flags & SWS_SPLINE)
1394
                {
1395
                    double p=-2.196152422706632;
1396
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1397
                }
1398
                else {
1399
                    coeff= 0.0; //GCC warning killer
1400
                    assert(0);
1401
                }
1402

    
1403
                filter[i*filterSize + j]= coeff;
1404
                xx++;
1405
            }
1406
            xDstInSrc+= 2*xInc;
1407
        }
1408
    }
1409

    
1410
    /* apply src & dst Filter to filter -> filter2
1411
       av_free(filter);
1412
    */
1413
    assert(filterSize>0);
1414
    filter2Size= filterSize;
1415
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1416
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1417
    assert(filter2Size>0);
1418
    filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1419

    
1420
    for (i=0; i<dstW; i++)
1421
    {
1422
        int j, k;
1423

    
1424
        if(srcFilter){
1425
            for (k=0; k<srcFilter->length; k++){
1426
                for (j=0; j<filterSize; j++)
1427
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1428
            }
1429
        }else{
1430
            for (j=0; j<filterSize; j++)
1431
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1432
        }
1433
        //FIXME dstFilter
1434

    
1435
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1436
    }
1437
    av_freep(&filter);
1438

    
1439
    /* try to reduce the filter-size (step1 find size and shift left) */
1440
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1441
    minFilterSize= 0;
1442
    for (i=dstW-1; i>=0; i--)
1443
    {
1444
        int min= filter2Size;
1445
        int j;
1446
        int64_t cutOff=0.0;
1447

    
1448
        /* get rid off near zero elements on the left by shifting left */
1449
        for (j=0; j<filter2Size; j++)
1450
        {
1451
            int k;
1452
            cutOff += FFABS(filter2[i*filter2Size]);
1453

    
1454
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1455

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

    
1459
            // move filter coefficients left
1460
            for (k=1; k<filter2Size; k++)
1461
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1462
            filter2[i*filter2Size + k - 1]= 0;
1463
            (*filterPos)[i]++;
1464
        }
1465

    
1466
        cutOff=0;
1467
        /* count near zeros on the right */
1468
        for (j=filter2Size-1; j>0; j--)
1469
        {
1470
            cutOff += FFABS(filter2[i*filter2Size + j]);
1471

    
1472
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1473
            min--;
1474
        }
1475

    
1476
        if (min>minFilterSize) minFilterSize= min;
1477
    }
1478

    
1479
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1480
        // we can handle the special case 4,
1481
        // so we don't want to go to the full 8
1482
        if (minFilterSize < 5)
1483
            filterAlign = 4;
1484

    
1485
        // We really don't want to waste our time
1486
        // doing useless computation, so fall back on
1487
        // the scalar C code for very small filters.
1488
        // Vectorizing is worth it only if you have a
1489
        // decent-sized vector.
1490
        if (minFilterSize < 3)
1491
            filterAlign = 1;
1492
    }
1493

    
1494
    if (flags & SWS_CPU_CAPS_MMX) {
1495
        // special case for unscaled vertical filtering
1496
        if (minFilterSize == 1 && filterAlign == 2)
1497
            filterAlign= 1;
1498
    }
1499

    
1500
    assert(minFilterSize > 0);
1501
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1502
    assert(filterSize > 0);
1503
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1504
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1505
        goto error;
1506
    *outFilterSize= filterSize;
1507

    
1508
    if (flags&SWS_PRINT_INFO)
1509
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1510
    /* try to reduce the filter-size (step2 reduce it) */
1511
    for (i=0; i<dstW; i++)
1512
    {
1513
        int j;
1514

    
1515
        for (j=0; j<filterSize; j++)
1516
        {
1517
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1518
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1519
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1520
                filter[i*filterSize + j]= 0;
1521
        }
1522
    }
1523

    
1524

    
1525
    //FIXME try to align filterPos if possible
1526

    
1527
    //fix borders
1528
    for (i=0; i<dstW; i++)
1529
    {
1530
        int j;
1531
        if ((*filterPos)[i] < 0)
1532
        {
1533
            // move filter coefficients left to compensate for filterPos
1534
            for (j=1; j<filterSize; j++)
1535
            {
1536
                int left= FFMAX(j + (*filterPos)[i], 0);
1537
                filter[i*filterSize + left] += filter[i*filterSize + j];
1538
                filter[i*filterSize + j]=0;
1539
            }
1540
            (*filterPos)[i]= 0;
1541
        }
1542

    
1543
        if ((*filterPos)[i] + filterSize > srcW)
1544
        {
1545
            int shift= (*filterPos)[i] + filterSize - srcW;
1546
            // move filter coefficients right to compensate for filterPos
1547
            for (j=filterSize-2; j>=0; j--)
1548
            {
1549
                int right= FFMIN(j + shift, filterSize-1);
1550
                filter[i*filterSize +right] += filter[i*filterSize +j];
1551
                filter[i*filterSize +j]=0;
1552
            }
1553
            (*filterPos)[i]= srcW - filterSize;
1554
        }
1555
    }
1556

    
1557
    // Note the +1 is for the MMX scaler which reads over the end
1558
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1559
    *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1560

    
1561
    /* normalize & store in outFilter */
1562
    for (i=0; i<dstW; i++)
1563
    {
1564
        int j;
1565
        int64_t error=0;
1566
        int64_t sum=0;
1567

    
1568
        for (j=0; j<filterSize; j++)
1569
        {
1570
            sum+= filter[i*filterSize + j];
1571
        }
1572
        sum= (sum + one/2)/ one;
1573
        for (j=0; j<*outFilterSize; j++)
1574
        {
1575
            int64_t v= filter[i*filterSize + j] + error;
1576
            int intV= ROUNDED_DIV(v, sum);
1577
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1578
            error= v - intV*sum;
1579
        }
1580
    }
1581

    
1582
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1583
    for (i=0; i<*outFilterSize; i++)
1584
    {
1585
        int j= dstW*(*outFilterSize);
1586
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1587
    }
1588

    
1589
    ret=0;
1590
error:
1591
    av_free(filter);
1592
    av_free(filter2);
1593
    return ret;
1594
}
1595

    
1596
#ifdef COMPILE_MMX2
1597
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1598
{
1599
    uint8_t *fragmentA;
1600
    x86_reg imm8OfPShufW1A;
1601
    x86_reg imm8OfPShufW2A;
1602
    x86_reg fragmentLengthA;
1603
    uint8_t *fragmentB;
1604
    x86_reg imm8OfPShufW1B;
1605
    x86_reg imm8OfPShufW2B;
1606
    x86_reg fragmentLengthB;
1607
    int fragmentPos;
1608

    
1609
    int xpos, i;
1610

    
1611
    // create an optimized horizontal scaling routine
1612

    
1613
    //code fragment
1614

    
1615
    __asm__ volatile(
1616
        "jmp                         9f                 \n\t"
1617
    // Begin
1618
        "0:                                             \n\t"
1619
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1620
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1621
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1622
        "punpcklbw                %%mm7, %%mm1          \n\t"
1623
        "punpcklbw                %%mm7, %%mm0          \n\t"
1624
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1625
        "1:                                             \n\t"
1626
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1627
        "2:                                             \n\t"
1628
        "psubw                    %%mm1, %%mm0          \n\t"
1629
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1630
        "pmullw                   %%mm3, %%mm0          \n\t"
1631
        "psllw                       $7, %%mm1          \n\t"
1632
        "paddw                    %%mm1, %%mm0          \n\t"
1633

    
1634
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1635

    
1636
        "add                         $8, %%"REG_a"      \n\t"
1637
    // End
1638
        "9:                                             \n\t"
1639
//        "int $3                                         \n\t"
1640
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1641
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1642
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1643
        "dec                         %1                 \n\t"
1644
        "dec                         %2                 \n\t"
1645
        "sub                         %0, %1             \n\t"
1646
        "sub                         %0, %2             \n\t"
1647
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1648
        "sub                         %0, %3             \n\t"
1649

    
1650

    
1651
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1652
        "=r" (fragmentLengthA)
1653
    );
1654

    
1655
    __asm__ volatile(
1656
        "jmp                         9f                 \n\t"
1657
    // Begin
1658
        "0:                                             \n\t"
1659
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1660
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1661
        "punpcklbw                %%mm7, %%mm0          \n\t"
1662
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1663
        "1:                                             \n\t"
1664
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1665
        "2:                                             \n\t"
1666
        "psubw                    %%mm1, %%mm0          \n\t"
1667
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1668
        "pmullw                   %%mm3, %%mm0          \n\t"
1669
        "psllw                       $7, %%mm1          \n\t"
1670
        "paddw                    %%mm1, %%mm0          \n\t"
1671

    
1672
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1673

    
1674
        "add                         $8, %%"REG_a"      \n\t"
1675
    // End
1676
        "9:                                             \n\t"
1677
//        "int                       $3                   \n\t"
1678
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1679
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1680
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1681
        "dec                         %1                 \n\t"
1682
        "dec                         %2                 \n\t"
1683
        "sub                         %0, %1             \n\t"
1684
        "sub                         %0, %2             \n\t"
1685
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1686
        "sub                         %0, %3             \n\t"
1687

    
1688

    
1689
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1690
        "=r" (fragmentLengthB)
1691
    );
1692

    
1693
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1694
    fragmentPos=0;
1695

    
1696
    for (i=0; i<dstW/numSplits; i++)
1697
    {
1698
        int xx=xpos>>16;
1699

    
1700
        if ((i&3) == 0)
1701
        {
1702
            int a=0;
1703
            int b=((xpos+xInc)>>16) - xx;
1704
            int c=((xpos+xInc*2)>>16) - xx;
1705
            int d=((xpos+xInc*3)>>16) - xx;
1706

    
1707
            filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1708
            filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1709
            filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1710
            filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1711
            filterPos[i/2]= xx;
1712

    
1713
            if (d+1<4)
1714
            {
1715
                int maxShift= 3-(d+1);
1716
                int shift=0;
1717

    
1718
                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1719

    
1720
                funnyCode[fragmentPos + imm8OfPShufW1B]=
1721
                    (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1722
                funnyCode[fragmentPos + imm8OfPShufW2B]=
1723
                    a | (b<<2) | (c<<4) | (d<<6);
1724

    
1725
                if (i+3>=dstW) shift=maxShift; //avoid overread
1726
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1727

    
1728
                if (shift && i>=shift)
1729
                {
1730
                    funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1731
                    funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1732
                    filterPos[i/2]-=shift;
1733
                }
1734

    
1735
                fragmentPos+= fragmentLengthB;
1736
            }
1737
            else
1738
            {
1739
                int maxShift= 3-d;
1740
                int shift=0;
1741

    
1742
                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1743

    
1744
                funnyCode[fragmentPos + imm8OfPShufW1A]=
1745
                funnyCode[fragmentPos + imm8OfPShufW2A]=
1746
                    a | (b<<2) | (c<<4) | (d<<6);
1747

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

    
1751
                if (shift && i>=shift)
1752
                {
1753
                    funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1754
                    funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1755
                    filterPos[i/2]-=shift;
1756
                }
1757

    
1758
                fragmentPos+= fragmentLengthA;
1759
            }
1760

    
1761
            funnyCode[fragmentPos]= RET;
1762
        }
1763
        xpos+=xInc;
1764
    }
1765
    filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1766
}
1767
#endif /* COMPILE_MMX2 */
1768

    
1769
static void globalInit(void){
1770
    // generating tables:
1771
    int i;
1772
    for (i=0; i<768; i++){
1773
        int c= av_clip_uint8(i-256);
1774
        clip_table[i]=c;
1775
    }
1776
}
1777

    
1778
static SwsFunc getSwsFunc(int flags){
1779

    
1780
#if defined(RUNTIME_CPUDETECT) && CONFIG_GPL
1781
#if ARCH_X86
1782
    // ordered per speed fastest first
1783
    if (flags & SWS_CPU_CAPS_MMX2)
1784
        return swScale_MMX2;
1785
    else if (flags & SWS_CPU_CAPS_3DNOW)
1786
        return swScale_3DNow;
1787
    else if (flags & SWS_CPU_CAPS_MMX)
1788
        return swScale_MMX;
1789
    else
1790
        return swScale_C;
1791

    
1792
#else
1793
#if ARCH_PPC
1794
    if (flags & SWS_CPU_CAPS_ALTIVEC)
1795
        return swScale_altivec;
1796
    else
1797
        return swScale_C;
1798
#endif
1799
    return swScale_C;
1800
#endif /* ARCH_X86 */
1801
#else //RUNTIME_CPUDETECT
1802
#if   HAVE_MMX2
1803
    return swScale_MMX2;
1804
#elif HAVE_AMD3DNOW
1805
    return swScale_3DNow;
1806
#elif HAVE_MMX
1807
    return swScale_MMX;
1808
#elif HAVE_ALTIVEC
1809
    return swScale_altivec;
1810
#else
1811
    return swScale_C;
1812
#endif
1813
#endif //!RUNTIME_CPUDETECT
1814
}
1815

    
1816
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1817
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1818
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1819
    /* Copy Y plane */
1820
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1821
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1822
    else
1823
    {
1824
        int i;
1825
        uint8_t *srcPtr= src[0];
1826
        uint8_t *dstPtr= dst;
1827
        for (i=0; i<srcSliceH; i++)
1828
        {
1829
            memcpy(dstPtr, srcPtr, c->srcW);
1830
            srcPtr+= srcStride[0];
1831
            dstPtr+= dstStride[0];
1832
        }
1833
    }
1834
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1835
    if (c->dstFormat == PIX_FMT_NV12)
1836
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1837
    else
1838
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1839

    
1840
    return srcSliceH;
1841
}
1842

    
1843
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1844
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1845
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1846

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

    
1849
    return srcSliceH;
1850
}
1851

    
1852
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1853
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1854
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1855

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

    
1858
    return srcSliceH;
1859
}
1860

    
1861
static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1862
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1863
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1864

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

    
1867
    return srcSliceH;
1868
}
1869

    
1870
static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1871
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1872
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1873

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

    
1876
    return srcSliceH;
1877
}
1878

    
1879
static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1880
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1881
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1882
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1883
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1884

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

    
1887
    return srcSliceH;
1888
}
1889

    
1890
static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1891
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1892
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1893
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1894
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1895

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

    
1898
    return srcSliceH;
1899
}
1900

    
1901
static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1902
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1903
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1904
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1905
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1906

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

    
1909
    return srcSliceH;
1910
}
1911

    
1912
static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1913
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1914
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1915
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1916
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1917

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

    
1920
    return srcSliceH;
1921
}
1922

    
1923
static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1924
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1925
    const enum PixelFormat srcFormat= c->srcFormat;
1926
    const enum PixelFormat dstFormat= c->dstFormat;
1927
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1928
                 const uint8_t *palette)=NULL;
1929
    int i;
1930
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1931
    uint8_t *srcPtr= src[0];
1932

    
1933
    if (!usePal(srcFormat))
1934
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1935
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1936

    
1937
    switch(dstFormat){
1938
    case PIX_FMT_RGB32  : conv = palette8topacked32; break;
1939
    case PIX_FMT_BGR32  : conv = palette8topacked32; break;
1940
    case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1941
    case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1942
    case PIX_FMT_RGB24  : conv = palette8topacked24; break;
1943
    case PIX_FMT_BGR24  : conv = palette8topacked24; break;
1944
    default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1945
                    sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1946
    }
1947

    
1948

    
1949
    for (i=0; i<srcSliceH; i++) {
1950
        conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1951
        srcPtr+= srcStride[0];
1952
        dstPtr+= dstStride[0];
1953
    }
1954

    
1955
    return srcSliceH;
1956
}
1957

    
1958
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1959
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1960
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1961
    const enum PixelFormat srcFormat= c->srcFormat;
1962
    const enum PixelFormat dstFormat= c->dstFormat;
1963
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1964
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1965
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1966
    const int dstId= fmt_depth(dstFormat) >> 2;
1967
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1968

    
1969
    /* BGR -> BGR */
1970
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
1971
       || (isRGB(srcFormat) && isRGB(dstFormat))){
1972
        switch(srcId | (dstId<<4)){
1973
        case 0x34: conv= rgb16to15; break;
1974
        case 0x36: conv= rgb24to15; break;
1975
        case 0x38: conv= rgb32to15; break;
1976
        case 0x43: conv= rgb15to16; break;
1977
        case 0x46: conv= rgb24to16; break;
1978
        case 0x48: conv= rgb32to16; break;
1979
        case 0x63: conv= rgb15to24; break;
1980
        case 0x64: conv= rgb16to24; break;
1981
        case 0x68: conv= rgb32to24; break;
1982
        case 0x83: conv= rgb15to32; break;
1983
        case 0x84: conv= rgb16to32; break;
1984
        case 0x86: conv= rgb24to32; break;
1985
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1986
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1987
        }
1988
    }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
1989
             || (isRGB(srcFormat) && isBGR(dstFormat))){
1990
        switch(srcId | (dstId<<4)){
1991
        case 0x33: conv= rgb15tobgr15; break;
1992
        case 0x34: conv= rgb16tobgr15; break;
1993
        case 0x36: conv= rgb24tobgr15; break;
1994
        case 0x38: conv= rgb32tobgr15; break;
1995
        case 0x43: conv= rgb15tobgr16; break;
1996
        case 0x44: conv= rgb16tobgr16; break;
1997
        case 0x46: conv= rgb24tobgr16; break;
1998
        case 0x48: conv= rgb32tobgr16; break;
1999
        case 0x63: conv= rgb15tobgr24; break;
2000
        case 0x64: conv= rgb16tobgr24; break;
2001
        case 0x66: conv= rgb24tobgr24; break;
2002
        case 0x68: conv= rgb32tobgr24; break;
2003
        case 0x83: conv= rgb15tobgr32; break;
2004
        case 0x84: conv= rgb16tobgr32; break;
2005
        case 0x86: conv= rgb24tobgr32; break;
2006
        case 0x88: conv= rgb32tobgr32; break;
2007
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2008
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2009
        }
2010
    }else{
2011
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2012
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2013
    }
2014

    
2015
    if(conv)
2016
    {
2017
        uint8_t *srcPtr= src[0];
2018
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2019
            srcPtr += ALT32_CORR;
2020

    
2021
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2022
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2023
        else
2024
        {
2025
            int i;
2026
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2027

    
2028
            for (i=0; i<srcSliceH; i++)
2029
            {
2030
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
2031
                srcPtr+= srcStride[0];
2032
                dstPtr+= dstStride[0];
2033
            }
2034
        }
2035
    }
2036
    return srcSliceH;
2037
}
2038

    
2039
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2040
                              int srcSliceH, uint8_t* dst[], int dstStride[]){
2041

    
2042
    rgb24toyv12(
2043
        src[0],
2044
        dst[0]+ srcSliceY    *dstStride[0],
2045
        dst[1]+(srcSliceY>>1)*dstStride[1],
2046
        dst[2]+(srcSliceY>>1)*dstStride[2],
2047
        c->srcW, srcSliceH,
2048
        dstStride[0], dstStride[1], srcStride[0]);
2049
    return srcSliceH;
2050
}
2051

    
2052
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2053
                             int srcSliceH, uint8_t* dst[], int dstStride[]){
2054
    int i;
2055

    
2056
    /* copy Y */
2057
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2058
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2059
    else{
2060
        uint8_t *srcPtr= src[0];
2061
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2062

    
2063
        for (i=0; i<srcSliceH; i++)
2064
        {
2065
            memcpy(dstPtr, srcPtr, c->srcW);
2066
            srcPtr+= srcStride[0];
2067
            dstPtr+= dstStride[0];
2068
        }
2069
    }
2070

    
2071
    if (c->dstFormat==PIX_FMT_YUV420P){
2072
        planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
2073
        planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
2074
    }else{
2075
        planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
2076
        planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
2077
    }
2078
    return srcSliceH;
2079
}
2080

    
2081
/* unscaled copy like stuff (assumes nearly identical formats) */
2082
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2083
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2084
{
2085
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2086
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2087
    else
2088
    {
2089
        int i;
2090
        uint8_t *srcPtr= src[0];
2091
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2092
        int length=0;
2093

    
2094
        /* universal length finder */
2095
        while(length+c->srcW <= FFABS(dstStride[0])
2096
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2097
        assert(length!=0);
2098

    
2099
        for (i=0; i<srcSliceH; i++)
2100
        {
2101
            memcpy(dstPtr, srcPtr, length);
2102
            srcPtr+= srcStride[0];
2103
            dstPtr+= dstStride[0];
2104
        }
2105
    }
2106
    return srcSliceH;
2107
}
2108

    
2109
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2110
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2111
{
2112
    int plane;
2113
    for (plane=0; plane<3; plane++)
2114
    {
2115
        int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2116
        int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2117
        int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2118

    
2119
        if (dst[plane] && !src[plane])
2120
                fillPlane(dst[plane], dstStride[plane], length, height, y, 128);
2121
        else
2122
        {
2123
            if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2124
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2125
            else
2126
            {
2127
                int i;
2128
                uint8_t *srcPtr= src[plane];
2129
                uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2130
                for (i=0; i<height; i++)
2131
                {
2132
                    memcpy(dstPtr, srcPtr, length);
2133
                    srcPtr+= srcStride[plane];
2134
                    dstPtr+= dstStride[plane];
2135
                }
2136
            }
2137
        }
2138
    }
2139
    return srcSliceH;
2140
}
2141

    
2142
static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2143
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
2144

    
2145
    int length= c->srcW;
2146
    int y=      srcSliceY;
2147
    int height= srcSliceH;
2148
    int i, j;
2149
    uint8_t *srcPtr= src[0];
2150
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2151

    
2152
    if (!isGray(c->dstFormat)){
2153
        int height= -((-srcSliceH)>>c->chrDstVSubSample);
2154
        memset(dst[1], 128, dstStride[1]*height);
2155
        memset(dst[2], 128, dstStride[2]*height);
2156
    }
2157
    if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
2158
    for (i=0; i<height; i++)
2159
    {
2160
        for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2161
        srcPtr+= srcStride[0];
2162
        dstPtr+= dstStride[0];
2163
    }
2164
    return srcSliceH;
2165
}
2166

    
2167
static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2168
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
2169

    
2170
    int length= c->srcW;
2171
    int y=      srcSliceY;
2172
    int height= srcSliceH;
2173
    int i, j;
2174
    uint8_t *srcPtr= src[0];
2175
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2176
    for (i=0; i<height; i++)
2177
    {
2178
        for (j=0; j<length; j++)
2179
        {
2180
            dstPtr[j<<1] = srcPtr[j];
2181
            dstPtr[(j<<1)+1] = srcPtr[j];
2182
        }
2183
        srcPtr+= srcStride[0];
2184
        dstPtr+= dstStride[0];
2185
    }
2186
    return srcSliceH;
2187
}
2188

    
2189
static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2190
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
2191

    
2192
    int length= c->srcW;
2193
    int y=      srcSliceY;
2194
    int height= srcSliceH;
2195
    int i, j;
2196
    uint16_t *srcPtr= (uint16_t*)src[0];
2197
    uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
2198
    for (i=0; i<height; i++)
2199
    {
2200
        for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
2201
        srcPtr+= srcStride[0]/2;
2202
        dstPtr+= dstStride[0]/2;
2203
    }
2204
    return srcSliceH;
2205
}
2206

    
2207

    
2208
static void getSubSampleFactors(int *h, int *v, int format){
2209
    switch(format){
2210
    case PIX_FMT_UYVY422:
2211
    case PIX_FMT_YUYV422:
2212
        *h=1;
2213
        *v=0;
2214
        break;
2215
    case PIX_FMT_YUV420P:
2216
    case PIX_FMT_YUVA420P:
2217
    case PIX_FMT_GRAY16BE:
2218
    case PIX_FMT_GRAY16LE:
2219
    case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2220
    case PIX_FMT_NV12:
2221
    case PIX_FMT_NV21:
2222
        *h=1;
2223
        *v=1;
2224
        break;
2225
    case PIX_FMT_YUV440P:
2226
        *h=0;
2227
        *v=1;
2228
        break;
2229
    case PIX_FMT_YUV410P:
2230
        *h=2;
2231
        *v=2;
2232
        break;
2233
    case PIX_FMT_YUV444P:
2234
        *h=0;
2235
        *v=0;
2236
        break;
2237
    case PIX_FMT_YUV422P:
2238
        *h=1;
2239
        *v=0;
2240
        break;
2241
    case PIX_FMT_YUV411P:
2242
        *h=2;
2243
        *v=0;
2244
        break;
2245
    default:
2246
        *h=0;
2247
        *v=0;
2248
        break;
2249
    }
2250
}
2251

    
2252
static uint16_t roundToInt16(int64_t f){
2253
    int r= (f + (1<<15))>>16;
2254
         if (r<-0x7FFF) return 0x8000;
2255
    else if (r> 0x7FFF) return 0x7FFF;
2256
    else                return r;
2257
}
2258

    
2259
/**
2260
 * @param inv_table the yuv2rgb coefficients, normally ff_yuv2rgb_coeffs[x]
2261
 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
2262
 * @return -1 if not supported
2263
 */
2264
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2265
    int64_t crv =  inv_table[0];
2266
    int64_t cbu =  inv_table[1];
2267
    int64_t cgu = -inv_table[2];
2268
    int64_t cgv = -inv_table[3];
2269
    int64_t cy  = 1<<16;
2270
    int64_t oy  = 0;
2271

    
2272
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2273
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2274

    
2275
    c->brightness= brightness;
2276
    c->contrast  = contrast;
2277
    c->saturation= saturation;
2278
    c->srcRange  = srcRange;
2279
    c->dstRange  = dstRange;
2280
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
2281

    
2282
    c->uOffset=   0x0400040004000400LL;
2283
    c->vOffset=   0x0400040004000400LL;
2284

    
2285
    if (!srcRange){
2286
        cy= (cy*255) / 219;
2287
        oy= 16<<16;
2288
    }else{
2289
        crv= (crv*224) / 255;
2290
        cbu= (cbu*224) / 255;
2291
        cgu= (cgu*224) / 255;
2292
        cgv= (cgv*224) / 255;
2293
    }
2294

    
2295
    cy = (cy *contrast             )>>16;
2296
    crv= (crv*contrast * saturation)>>32;
2297
    cbu= (cbu*contrast * saturation)>>32;
2298
    cgu= (cgu*contrast * saturation)>>32;
2299
    cgv= (cgv*contrast * saturation)>>32;
2300

    
2301
    oy -= 256*brightness;
2302

    
2303
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2304
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2305
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2306
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2307
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2308
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2309

    
2310
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2311
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2312
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2313
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2314
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2315
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2316

    
2317
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2318
    //FIXME factorize
2319

    
2320
#ifdef COMPILE_ALTIVEC
2321
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2322
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2323
#endif
2324
    return 0;
2325
}
2326

    
2327
/**
2328
 * @return -1 if not supported
2329
 */
2330
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2331
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2332

    
2333
    *inv_table = c->srcColorspaceTable;
2334
    *table     = c->dstColorspaceTable;
2335
    *srcRange  = c->srcRange;
2336
    *dstRange  = c->dstRange;
2337
    *brightness= c->brightness;
2338
    *contrast  = c->contrast;
2339
    *saturation= c->saturation;
2340

    
2341
    return 0;
2342
}
2343

    
2344
static int handle_jpeg(enum PixelFormat *format)
2345
{
2346
    switch (*format) {
2347
        case PIX_FMT_YUVJ420P:
2348
            *format = PIX_FMT_YUV420P;
2349
            return 1;
2350
        case PIX_FMT_YUVJ422P:
2351
            *format = PIX_FMT_YUV422P;
2352
            return 1;
2353
        case PIX_FMT_YUVJ444P:
2354
            *format = PIX_FMT_YUV444P;
2355
            return 1;
2356
        case PIX_FMT_YUVJ440P:
2357
            *format = PIX_FMT_YUV440P;
2358
            return 1;
2359
        default:
2360
            return 0;
2361
    }
2362
}
2363

    
2364
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2365
                           SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2366

    
2367
    SwsContext *c;
2368
    int i;
2369
    int usesVFilter, usesHFilter;
2370
    int unscaled, needsDither;
2371
    int srcRange, dstRange;
2372
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2373
#if ARCH_X86
2374
    if (flags & SWS_CPU_CAPS_MMX)
2375
        __asm__ volatile("emms\n\t"::: "memory");
2376
#endif
2377

    
2378
#if !defined(RUNTIME_CPUDETECT) || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
2379
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2380
#if   HAVE_MMX2
2381
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2382
#elif HAVE_AMD3DNOW
2383
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2384
#elif HAVE_MMX
2385
    flags |= SWS_CPU_CAPS_MMX;
2386
#elif HAVE_ALTIVEC
2387
    flags |= SWS_CPU_CAPS_ALTIVEC;
2388
#elif ARCH_BFIN
2389
    flags |= SWS_CPU_CAPS_BFIN;
2390
#endif
2391
#endif /* RUNTIME_CPUDETECT */
2392
    if (clip_table[512] != 255) globalInit();
2393
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2394

    
2395
    unscaled = (srcW == dstW && srcH == dstH);
2396
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2397
        && (fmt_depth(dstFormat))<24
2398
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2399

    
2400
    srcRange = handle_jpeg(&srcFormat);
2401
    dstRange = handle_jpeg(&dstFormat);
2402

    
2403
    if (!isSupportedIn(srcFormat))
2404
    {
2405
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2406
        return NULL;
2407
    }
2408
    if (!isSupportedOut(dstFormat))
2409
    {
2410
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2411
        return NULL;
2412
    }
2413

    
2414
    i= flags & ( SWS_POINT
2415
                |SWS_AREA
2416
                |SWS_BILINEAR
2417
                |SWS_FAST_BILINEAR
2418
                |SWS_BICUBIC
2419
                |SWS_X
2420
                |SWS_GAUSS
2421
                |SWS_LANCZOS
2422
                |SWS_SINC
2423
                |SWS_SPLINE
2424
                |SWS_BICUBLIN);
2425
    if(!i || (i & (i-1)))
2426
    {
2427
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2428
        return NULL;
2429
    }
2430

    
2431
    /* sanity check */
2432
    if (srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
2433
    {
2434
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2435
               srcW, srcH, dstW, dstH);
2436
        return NULL;
2437
    }
2438
    if(srcW > VOFW || dstW > VOFW){
2439
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2440
        return NULL;
2441
    }
2442

    
2443
    if (!dstFilter) dstFilter= &dummyFilter;
2444
    if (!srcFilter) srcFilter= &dummyFilter;
2445

    
2446
    c= av_mallocz(sizeof(SwsContext));
2447

    
2448
    c->av_class = &sws_context_class;
2449
    c->srcW= srcW;
2450
    c->srcH= srcH;
2451
    c->dstW= dstW;
2452
    c->dstH= dstH;
2453
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2454
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2455
    c->flags= flags;
2456
    c->dstFormat= dstFormat;
2457
    c->srcFormat= srcFormat;
2458
    c->vRounder= 4* 0x0001000100010001ULL;
2459

    
2460
    usesHFilter= usesVFilter= 0;
2461
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2462
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2463
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2464
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2465
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2466
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2467
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2468
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2469

    
2470
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2471
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2472

    
2473
    // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2474
    if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2475

    
2476
    // drop some chroma lines if the user wants it
2477
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2478
    c->chrSrcVSubSample+= c->vChrDrop;
2479

    
2480
    // drop every other pixel for chroma calculation unless user wants full chroma
2481
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2482
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2483
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2484
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2485
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2486
        c->chrSrcHSubSample=1;
2487

    
2488
    if (param){
2489
        c->param[0] = param[0];
2490
        c->param[1] = param[1];
2491
    }else{
2492
        c->param[0] =
2493
        c->param[1] = SWS_PARAM_DEFAULT;
2494
    }
2495

    
2496
    c->chrIntHSubSample= c->chrDstHSubSample;
2497
    c->chrIntVSubSample= c->chrSrcVSubSample;
2498

    
2499
    // Note the -((-x)>>y) is so that we always round toward +inf.
2500
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2501
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2502
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2503
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2504

    
2505
    sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2506

    
2507
    /* unscaled special cases */
2508
    if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2509
    {
2510
        /* yv12_to_nv12 */
2511
        if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2512
        {
2513
            c->swScale= PlanarToNV12Wrapper;
2514
        }
2515
        /* yuv2bgr */
2516
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2517
            && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2518
        {
2519
            c->swScale= ff_yuv2rgb_get_func_ptr(c);
2520
        }
2521

    
2522
        if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
2523
        {
2524
            c->swScale= yvu9toyv12Wrapper;
2525
        }
2526

    
2527
        /* bgr24toYV12 */
2528
        if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
2529
            c->swScale= bgr24toyv12Wrapper;
2530

    
2531
        /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2532
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2533
           && (isBGR(dstFormat) || isRGB(dstFormat))
2534
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2535
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2536
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2537
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2538
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2539
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2540
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2541
           && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2542
                                             && dstFormat != PIX_FMT_RGB32_1
2543
                                             && dstFormat != PIX_FMT_BGR32_1
2544
           && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2545
             c->swScale= rgb2rgbWrapper;
2546

    
2547
        if ((usePal(srcFormat) && (
2548
                 dstFormat == PIX_FMT_RGB32   ||
2549
                 dstFormat == PIX_FMT_RGB32_1 ||
2550
                 dstFormat == PIX_FMT_RGB24   ||
2551
                 dstFormat == PIX_FMT_BGR32   ||
2552
                 dstFormat == PIX_FMT_BGR32_1 ||
2553
                 dstFormat == PIX_FMT_BGR24)))
2554
             c->swScale= pal2rgbWrapper;
2555

    
2556
        if (srcFormat == PIX_FMT_YUV422P)
2557
        {
2558
            if (dstFormat == PIX_FMT_YUYV422)
2559
                c->swScale= YUV422PToYuy2Wrapper;
2560
            else if (dstFormat == PIX_FMT_UYVY422)
2561
                c->swScale= YUV422PToUyvyWrapper;
2562
        }
2563

    
2564
        /* LQ converters if -sws 0 or -sws 4*/
2565
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2566
            /* yv12_to_yuy2 */
2567
            if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2568
            {
2569
                if (dstFormat == PIX_FMT_YUYV422)
2570
                    c->swScale= PlanarToYuy2Wrapper;
2571
                else if (dstFormat == PIX_FMT_UYVY422)
2572
                    c->swScale= PlanarToUyvyWrapper;
2573
            }
2574

    
2575
            if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV420P)
2576
                c->swScale= YUYV2YUV420Wrapper;
2577
            if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV420P)
2578
                c->swScale= UYVY2YUV420Wrapper;
2579
        }
2580
        if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2581
            c->swScale= YUYV2YUV422Wrapper;
2582
        if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2583
            c->swScale= UYVY2YUV422Wrapper;
2584

    
2585
#ifdef COMPILE_ALTIVEC
2586
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2587
            !(c->flags & SWS_BITEXACT) &&
2588
            srcFormat == PIX_FMT_YUV420P) {
2589
          // unscaled YV12 -> packed YUV, we want speed
2590
          if (dstFormat == PIX_FMT_YUYV422)
2591
              c->swScale= yv12toyuy2_unscaled_altivec;
2592
          else if (dstFormat == PIX_FMT_UYVY422)
2593
              c->swScale= yv12touyvy_unscaled_altivec;
2594
        }
2595
#endif
2596

    
2597
        /* simple copy */
2598
        if (  srcFormat == dstFormat
2599
            || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2600
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2601
            || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2602
        {
2603
            if (isPacked(c->srcFormat))
2604
                c->swScale= packedCopy;
2605
            else /* Planar YUV or gray */
2606
                c->swScale= planarCopy;
2607
        }
2608

    
2609
        /* gray16{le,be} conversions */
2610
        if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2611
        {
2612
            c->swScale= gray16togray;
2613
        }
2614
        if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2615
        {
2616
            c->swScale= graytogray16;
2617
        }
2618
        if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2619
        {
2620
            c->swScale= gray16swap;
2621
        }
2622

    
2623
#if ARCH_BFIN
2624
        if (flags & SWS_CPU_CAPS_BFIN)
2625
            ff_bfin_get_unscaled_swscale (c);
2626
#endif
2627

    
2628
        if (c->swScale){
2629
            if (flags&SWS_PRINT_INFO)
2630
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2631
                                sws_format_name(srcFormat), sws_format_name(dstFormat));
2632
            return c;
2633
        }
2634
    }
2635

    
2636
    if (flags & SWS_CPU_CAPS_MMX2)
2637
    {
2638
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2639
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2640
        {
2641
            if (flags&SWS_PRINT_INFO)
2642
                av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2643
        }
2644
        if (usesHFilter) c->canMMX2BeUsed=0;
2645
    }
2646
    else
2647
        c->canMMX2BeUsed=0;
2648

    
2649
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2650
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2651

    
2652
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2653
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
2654
    // n-2 is the last chrominance sample available
2655
    // this is not perfect, but no one should notice the difference, the more correct variant
2656
    // would be like the vertical one, but that would require some special code for the
2657
    // first and last pixel
2658
    if (flags&SWS_FAST_BILINEAR)
2659
    {
2660
        if (c->canMMX2BeUsed)
2661
        {
2662
            c->lumXInc+= 20;
2663
            c->chrXInc+= 20;
2664
        }
2665
        //we don't use the x86 asm scaler if MMX is available
2666
        else if (flags & SWS_CPU_CAPS_MMX)
2667
        {
2668
            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2669
            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2670
        }
2671
    }
2672

    
2673
    /* precalculate horizontal scaler filter coefficients */
2674
    {
2675
        const int filterAlign=
2676
            (flags & SWS_CPU_CAPS_MMX) ? 4 :
2677
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2678
            1;
2679

    
2680
        initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2681
                   srcW      ,       dstW, filterAlign, 1<<14,
2682
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2683
                   srcFilter->lumH, dstFilter->lumH, c->param);
2684
        initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2685
                   c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2686
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2687
                   srcFilter->chrH, dstFilter->chrH, c->param);
2688

    
2689
#define MAX_FUNNY_CODE_SIZE 10000
2690
#if defined(COMPILE_MMX2)
2691
// can't downscale !!!
2692
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2693
        {
2694
#ifdef MAP_ANONYMOUS
2695
            c->funnyYCode  = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2696
            c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2697
#elif HAVE_VIRTUALALLOC
2698
            c->funnyYCode  = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2699
            c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2700
#else
2701
            c->funnyYCode  = av_malloc(MAX_FUNNY_CODE_SIZE);
2702
            c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2703
#endif
2704

    
2705
            c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2706
            c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2707
            c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2708
            c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2709

    
2710
            initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2711
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2712
        }
2713
#endif /* defined(COMPILE_MMX2) */
2714
    } // initialize horizontal stuff
2715

    
2716

    
2717

    
2718
    /* precalculate vertical scaler filter coefficients */
2719
    {
2720
        const int filterAlign=
2721
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2722
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2723
            1;
2724

    
2725
        initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2726
                   srcH      ,        dstH, filterAlign, (1<<12),
2727
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2728
                   srcFilter->lumV, dstFilter->lumV, c->param);
2729
        initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2730
                   c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2731
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2732
                   srcFilter->chrV, dstFilter->chrV, c->param);
2733

    
2734
#if HAVE_ALTIVEC
2735
        c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2736
        c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2737

    
2738
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2739
            int j;
2740
            short *p = (short *)&c->vYCoeffsBank[i];
2741
            for (j=0;j<8;j++)
2742
                p[j] = c->vLumFilter[i];
2743
        }
2744

    
2745
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2746
            int j;
2747
            short *p = (short *)&c->vCCoeffsBank[i];
2748
            for (j=0;j<8;j++)
2749
                p[j] = c->vChrFilter[i];
2750
        }
2751
#endif
2752
    }
2753

    
2754
    // calculate buffer sizes so that they won't run out while handling these damn slices
2755
    c->vLumBufSize= c->vLumFilterSize;
2756
    c->vChrBufSize= c->vChrFilterSize;
2757
    for (i=0; i<dstH; i++)
2758
    {
2759
        int chrI= i*c->chrDstH / dstH;
2760
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2761
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2762

    
2763
        nextSlice>>= c->chrSrcVSubSample;
2764
        nextSlice<<= c->chrSrcVSubSample;
2765
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2766
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2767
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2768
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2769
    }
2770

    
2771
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2772
    c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2773
    c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2774
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2775
        c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2776
    //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
2777
    /* align at 16 bytes for AltiVec */
2778
    for (i=0; i<c->vLumBufSize; i++)
2779
        c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2780
    for (i=0; i<c->vChrBufSize; i++)
2781
        c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2782
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2783
        for (i=0; i<c->vLumBufSize; i++)
2784
            c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2785

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

    
2789
    assert(2*VOFW == VOF);
2790

    
2791
    assert(c->chrDstH <= dstH);
2792

    
2793
    if (flags&SWS_PRINT_INFO)
2794
    {
2795
#ifdef DITHER1XBPP
2796
        const char *dither= " dithered";
2797
#else
2798
        const char *dither= "";
2799
#endif
2800
        if (flags&SWS_FAST_BILINEAR)
2801
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2802
        else if (flags&SWS_BILINEAR)
2803
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2804
        else if (flags&SWS_BICUBIC)
2805
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2806
        else if (flags&SWS_X)
2807
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2808
        else if (flags&SWS_POINT)
2809
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2810
        else if (flags&SWS_AREA)
2811
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2812
        else if (flags&SWS_BICUBLIN)
2813
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2814
        else if (flags&SWS_GAUSS)
2815
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2816
        else if (flags&SWS_SINC)
2817
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2818
        else if (flags&SWS_LANCZOS)
2819
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2820
        else if (flags&SWS_SPLINE)
2821
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2822
        else
2823
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2824

    
2825
        if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2826
            av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2827
                   sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2828
        else
2829
            av_log(c, AV_LOG_INFO, "from %s to %s ",
2830
                   sws_format_name(srcFormat), sws_format_name(dstFormat));
2831

    
2832
        if (flags & SWS_CPU_CAPS_MMX2)
2833
            av_log(c, AV_LOG_INFO, "using MMX2\n");
2834
        else if (flags & SWS_CPU_CAPS_3DNOW)
2835
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2836
        else if (flags & SWS_CPU_CAPS_MMX)
2837
            av_log(c, AV_LOG_INFO, "using MMX\n");
2838
        else if (flags & SWS_CPU_CAPS_ALTIVEC)
2839
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
2840
        else
2841
            av_log(c, AV_LOG_INFO, "using C\n");
2842
    }
2843

    
2844
    if (flags & SWS_PRINT_INFO)
2845
    {
2846
        if (flags & SWS_CPU_CAPS_MMX)
2847
        {
2848
            if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2849
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2850
            else
2851
            {
2852
                if (c->hLumFilterSize==4)
2853
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2854
                else if (c->hLumFilterSize==8)
2855
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2856
                else
2857
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2858

    
2859
                if (c->hChrFilterSize==4)
2860
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2861
                else if (c->hChrFilterSize==8)
2862
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2863
                else
2864
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2865
            }
2866
        }
2867
        else
2868
        {
2869
#if ARCH_X86
2870
            av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2871
#else
2872
            if (flags & SWS_FAST_BILINEAR)
2873
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2874
            else
2875
                av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2876
#endif
2877
        }
2878
        if (isPlanarYUV(dstFormat))
2879
        {
2880
            if (c->vLumFilterSize==1)
2881
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2882
            else
2883
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2884
        }
2885
        else
2886
        {
2887
            if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2888
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2889
                       "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2890
            else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2891
                av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2892
            else
2893
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2894
        }
2895

    
2896
        if (dstFormat==PIX_FMT_BGR24)
2897
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2898
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2899
        else if (dstFormat==PIX_FMT_RGB32)
2900
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2901
        else if (dstFormat==PIX_FMT_BGR565)
2902
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2903
        else if (dstFormat==PIX_FMT_BGR555)
2904
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2905

    
2906
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2907
    }
2908
    if (flags & SWS_PRINT_INFO)
2909
    {
2910
        av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2911
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2912
        av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2913
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2914
    }
2915

    
2916
    c->swScale= getSwsFunc(flags);
2917
    return c;
2918
}
2919

    
2920
/**
2921
 * swscale wrapper, so we don't need to export the SwsContext.
2922
 * Assumes planar YUV to be in YUV order instead of YVU.
2923
 */
2924
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2925
              int srcSliceH, uint8_t* dst[], int dstStride[]){
2926
    int i;
2927
    uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2928

    
2929
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2930
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2931
        return 0;
2932
    }
2933
    if (c->sliceDir == 0) {
2934
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2935
    }
2936

    
2937
    if (usePal(c->srcFormat)){
2938
        for (i=0; i<256; i++){
2939
            int p, r, g, b,y,u,v;
2940
            if(c->srcFormat == PIX_FMT_PAL8){
2941
                p=((uint32_t*)(src[1]))[i];
2942
                r= (p>>16)&0xFF;
2943
                g= (p>> 8)&0xFF;
2944
                b=  p     &0xFF;
2945
            }else if(c->srcFormat == PIX_FMT_RGB8){
2946
                r= (i>>5    )*36;
2947
                g= ((i>>2)&7)*36;
2948
                b= (i&3     )*85;
2949
            }else if(c->srcFormat == PIX_FMT_BGR8){
2950
                b= (i>>6    )*85;
2951
                g= ((i>>3)&7)*36;
2952
                r= (i&7     )*36;
2953
            }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2954
                r= (i>>3    )*255;
2955
                g= ((i>>1)&3)*85;
2956
                b= (i&1     )*255;
2957
            }else {
2958
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2959
                b= (i>>3    )*255;
2960
                g= ((i>>1)&3)*85;
2961
                r= (i&1     )*255;
2962
            }
2963
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2964
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2965
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2966
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
2967

    
2968

    
2969
            switch(c->dstFormat) {
2970
            case PIX_FMT_BGR32:
2971
#ifndef WORDS_BIGENDIAN
2972
            case PIX_FMT_RGB24:
2973
#endif
2974
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2975
                break;
2976
            case PIX_FMT_BGR32_1:
2977
#ifdef  WORDS_BIGENDIAN
2978
            case PIX_FMT_BGR24:
2979
#endif
2980
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2981
                break;
2982
            case PIX_FMT_RGB32_1:
2983
#ifdef  WORDS_BIGENDIAN
2984
            case PIX_FMT_RGB24:
2985
#endif
2986
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2987
                break;
2988
            case PIX_FMT_RGB32:
2989
#ifndef WORDS_BIGENDIAN
2990
            case PIX_FMT_BGR24:
2991
#endif
2992
            default:
2993
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
2994
            }
2995
        }
2996
    }
2997

    
2998
    // copy strides, so they can safely be modified
2999
    if (c->sliceDir == 1) {
3000
        // slices go from top to bottom
3001
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3002
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3003
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
3004
    } else {
3005
        // slices go from bottom to top => we flip the image internally
3006
        uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
3007
                           dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
3008
                           dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2],
3009
                           dst[3] + (c->dstH-1)*dstStride[3]};
3010
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3011
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3012

    
3013
        src2[0] += (srcSliceH-1)*srcStride[0];
3014
        if (!usePal(c->srcFormat))
3015
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3016
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3017
        src2[3] += (srcSliceH-1)*srcStride[3];
3018

    
3019
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3020
    }
3021
}
3022

    
3023
#if LIBSWSCALE_VERSION_MAJOR < 1
3024
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3025
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
3026
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3027
}
3028
#endif
3029

    
3030
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3031
                                float lumaSharpen, float chromaSharpen,
3032
                                float chromaHShift, float chromaVShift,
3033
                                int verbose)
3034
{
3035
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3036

    
3037
    if (lumaGBlur!=0.0){
3038
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3039
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3040
    }else{
3041
        filter->lumH= sws_getIdentityVec();
3042
        filter->lumV= sws_getIdentityVec();
3043
    }
3044

    
3045
    if (chromaGBlur!=0.0){
3046
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3047
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3048
    }else{
3049
        filter->chrH= sws_getIdentityVec();
3050
        filter->chrV= sws_getIdentityVec();
3051
    }
3052

    
3053
    if (chromaSharpen!=0.0){
3054
        SwsVector *id= sws_getIdentityVec();
3055
        sws_scaleVec(filter->chrH, -chromaSharpen);
3056
        sws_scaleVec(filter->chrV, -chromaSharpen);
3057
        sws_addVec(filter->chrH, id);
3058
        sws_addVec(filter->chrV, id);
3059
        sws_freeVec(id);
3060
    }
3061

    
3062
    if (lumaSharpen!=0.0){
3063
        SwsVector *id= sws_getIdentityVec();
3064
        sws_scaleVec(filter->lumH, -lumaSharpen);
3065
        sws_scaleVec(filter->lumV, -lumaSharpen);
3066
        sws_addVec(filter->lumH, id);
3067
        sws_addVec(filter->lumV, id);
3068
        sws_freeVec(id);
3069
    }
3070

    
3071
    if (chromaHShift != 0.0)
3072
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3073

    
3074
    if (chromaVShift != 0.0)
3075
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3076

    
3077
    sws_normalizeVec(filter->chrH, 1.0);
3078
    sws_normalizeVec(filter->chrV, 1.0);
3079
    sws_normalizeVec(filter->lumH, 1.0);
3080
    sws_normalizeVec(filter->lumV, 1.0);
3081

    
3082
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3083
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3084

    
3085
    return filter;
3086
}
3087

    
3088
SwsVector *sws_getGaussianVec(double variance, double quality){
3089
    const int length= (int)(variance*quality + 0.5) | 1;
3090
    int i;
3091
    double *coeff= av_malloc(length*sizeof(double));
3092
    double middle= (length-1)*0.5;
3093
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3094

    
3095
    vec->coeff= coeff;
3096
    vec->length= length;
3097

    
3098
    for (i=0; i<length; i++)
3099
    {
3100
        double dist= i-middle;
3101
        coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3102
    }
3103

    
3104
    sws_normalizeVec(vec, 1.0);
3105

    
3106
    return vec;
3107
}
3108

    
3109
SwsVector *sws_getConstVec(double c, int length){
3110
    int i;
3111
    double *coeff= av_malloc(length*sizeof(double));
3112
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3113

    
3114
    vec->coeff= coeff;
3115
    vec->length= length;
3116

    
3117
    for (i=0; i<length; i++)
3118
        coeff[i]= c;
3119

    
3120
    return vec;
3121
}
3122

    
3123

    
3124
SwsVector *sws_getIdentityVec(void){
3125
    return sws_getConstVec(1.0, 1);
3126
}
3127

    
3128
double sws_dcVec(SwsVector *a){
3129
    int i;
3130
    double sum=0;
3131

    
3132
    for (i=0; i<a->length; i++)
3133
        sum+= a->coeff[i];
3134

    
3135
    return sum;
3136
}
3137

    
3138
void sws_scaleVec(SwsVector *a, double scalar){
3139
    int i;
3140

    
3141
    for (i=0; i<a->length; i++)
3142
        a->coeff[i]*= scalar;
3143
}
3144

    
3145
void sws_normalizeVec(SwsVector *a, double height){
3146
    sws_scaleVec(a, height/sws_dcVec(a));
3147
}
3148

    
3149
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3150
    int length= a->length + b->length - 1;
3151
    double *coeff= av_malloc(length*sizeof(double));
3152
    int i, j;
3153
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3154

    
3155
    vec->coeff= coeff;
3156
    vec->length= length;
3157

    
3158
    for (i=0; i<length; i++) coeff[i]= 0.0;
3159

    
3160
    for (i=0; i<a->length; i++)
3161
    {
3162
        for (j=0; j<b->length; j++)
3163
        {
3164
            coeff[i+j]+= a->coeff[i]*b->coeff[j];
3165
        }
3166
    }
3167

    
3168
    return vec;
3169
}
3170

    
3171
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3172
    int length= FFMAX(a->length, b->length);
3173
    double *coeff= av_malloc(length*sizeof(double));
3174
    int i;
3175
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3176

    
3177
    vec->coeff= coeff;
3178
    vec->length= length;
3179

    
3180
    for (i=0; i<length; i++) coeff[i]= 0.0;
3181

    
3182
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3183
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3184

    
3185
    return vec;
3186
}
3187

    
3188
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3189
    int length= FFMAX(a->length, b->length);
3190
    double *coeff= av_malloc(length*sizeof(double));
3191
    int i;
3192
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3193

    
3194
    vec->coeff= coeff;
3195
    vec->length= length;
3196

    
3197
    for (i=0; i<length; i++) coeff[i]= 0.0;
3198

    
3199
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3200
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3201

    
3202
    return vec;
3203
}
3204

    
3205
/* shift left / or right if "shift" is negative */
3206
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3207
    int length= a->length + FFABS(shift)*2;
3208
    double *coeff= av_malloc(length*sizeof(double));
3209
    int i;
3210
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3211

    
3212
    vec->coeff= coeff;
3213
    vec->length= length;
3214

    
3215
    for (i=0; i<length; i++) coeff[i]= 0.0;
3216

    
3217
    for (i=0; i<a->length; i++)
3218
    {
3219
        coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3220
    }
3221

    
3222
    return vec;
3223
}
3224

    
3225
void sws_shiftVec(SwsVector *a, int shift){
3226
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3227
    av_free(a->coeff);
3228
    a->coeff= shifted->coeff;
3229
    a->length= shifted->length;
3230
    av_free(shifted);
3231
}
3232

    
3233
void sws_addVec(SwsVector *a, SwsVector *b){
3234
    SwsVector *sum= sws_sumVec(a, b);
3235
    av_free(a->coeff);
3236
    a->coeff= sum->coeff;
3237
    a->length= sum->length;
3238
    av_free(sum);
3239
}
3240

    
3241
void sws_subVec(SwsVector *a, SwsVector *b){
3242
    SwsVector *diff= sws_diffVec(a, b);
3243
    av_free(a->coeff);
3244
    a->coeff= diff->coeff;
3245
    a->length= diff->length;
3246
    av_free(diff);
3247
}
3248

    
3249
void sws_convVec(SwsVector *a, SwsVector *b){
3250
    SwsVector *conv= sws_getConvVec(a, b);
3251
    av_free(a->coeff);
3252
    a->coeff= conv->coeff;
3253
    a->length= conv->length;
3254
    av_free(conv);
3255
}
3256

    
3257
SwsVector *sws_cloneVec(SwsVector *a){
3258
    double *coeff= av_malloc(a->length*sizeof(double));
3259
    int i;
3260
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3261

    
3262
    vec->coeff= coeff;
3263
    vec->length= a->length;
3264

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

    
3267
    return vec;
3268
}
3269

    
3270
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3271
    int i;
3272
    double max=0;
3273
    double min=0;
3274
    double range;
3275

    
3276
    for (i=0; i<a->length; i++)
3277
        if (a->coeff[i]>max) max= a->coeff[i];
3278

    
3279
    for (i=0; i<a->length; i++)
3280
        if (a->coeff[i]<min) min= a->coeff[i];
3281

    
3282
    range= max - min;
3283

    
3284
    for (i=0; i<a->length; i++)
3285
    {
3286
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3287
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3288
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
3289
        av_log(log_ctx, log_level, "|\n");
3290
    }
3291
}
3292

    
3293
#if LIBSWSCALE_VERSION_MAJOR < 1
3294
void sws_printVec(SwsVector *a){
3295
    sws_printVec2(a, NULL, AV_LOG_DEBUG);
3296
}
3297
#endif
3298

    
3299
void sws_freeVec(SwsVector *a){
3300
    if (!a) return;
3301
    av_freep(&a->coeff);
3302
    a->length=0;
3303
    av_free(a);
3304
}
3305

    
3306
void sws_freeFilter(SwsFilter *filter){
3307
    if (!filter) return;
3308

    
3309
    if (filter->lumH) sws_freeVec(filter->lumH);
3310
    if (filter->lumV) sws_freeVec(filter->lumV);
3311
    if (filter->chrH) sws_freeVec(filter->chrH);
3312
    if (filter->chrV) sws_freeVec(filter->chrV);
3313
    av_free(filter);
3314
}
3315

    
3316

    
3317
void sws_freeContext(SwsContext *c){
3318
    int i;
3319
    if (!c) return;
3320

    
3321
    if (c->lumPixBuf)
3322
    {
3323
        for (i=0; i<c->vLumBufSize; i++)
3324
            av_freep(&c->lumPixBuf[i]);
3325
        av_freep(&c->lumPixBuf);
3326
    }
3327

    
3328
    if (c->chrPixBuf)
3329
    {
3330
        for (i=0; i<c->vChrBufSize; i++)
3331
            av_freep(&c->chrPixBuf[i]);
3332
        av_freep(&c->chrPixBuf);
3333
    }
3334

    
3335
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3336
        for (i=0; i<c->vLumBufSize; i++)
3337
            av_freep(&c->alpPixBuf[i]);
3338
        av_freep(&c->alpPixBuf);
3339
    }
3340

    
3341
    av_freep(&c->vLumFilter);
3342
    av_freep(&c->vChrFilter);
3343
    av_freep(&c->hLumFilter);
3344
    av_freep(&c->hChrFilter);
3345
#if HAVE_ALTIVEC
3346
    av_freep(&c->vYCoeffsBank);
3347
    av_freep(&c->vCCoeffsBank);
3348
#endif
3349

    
3350
    av_freep(&c->vLumFilterPos);
3351
    av_freep(&c->vChrFilterPos);
3352
    av_freep(&c->hLumFilterPos);
3353
    av_freep(&c->hChrFilterPos);
3354

    
3355
#if ARCH_X86 && CONFIG_GPL
3356
#ifdef MAP_ANONYMOUS
3357
    if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3358
    if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3359
#elif HAVE_VIRTUALALLOC
3360
    if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3361
    if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3362
#else
3363
    av_free(c->funnyYCode );
3364
    av_free(c->funnyUVCode);
3365
#endif
3366
    c->funnyYCode=NULL;
3367
    c->funnyUVCode=NULL;
3368
#endif /* ARCH_X86 && CONFIG_GPL */
3369

    
3370
    av_freep(&c->lumMmx2Filter);
3371
    av_freep(&c->chrMmx2Filter);
3372
    av_freep(&c->lumMmx2FilterPos);
3373
    av_freep(&c->chrMmx2FilterPos);
3374
    av_freep(&c->yuvTable);
3375

    
3376
    av_free(c);
3377
}
3378

    
3379
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3380
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3381
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3382
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
3383
{
3384
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3385

    
3386
    if (!param)
3387
        param = default_param;
3388

    
3389
    if (context) {
3390
        if (context->srcW != srcW || context->srcH != srcH ||
3391
            context->srcFormat != srcFormat ||
3392
            context->dstW != dstW || context->dstH != dstH ||
3393
            context->dstFormat != dstFormat || context->flags != flags ||
3394
            context->param[0] != param[0] || context->param[1] != param[1])
3395
        {
3396
            sws_freeContext(context);
3397
            context = NULL;
3398
        }
3399
    }
3400
    if (!context) {
3401
        return sws_getContext(srcW, srcH, srcFormat,
3402
                              dstW, dstH, dstFormat, flags,
3403
                              srcFilter, dstFilter, param);
3404
    }
3405
    return context;
3406
}
3407