Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 8cbcb1c7

History | View | Annotate | Download (125 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 "config.h"
63
#include <assert.h>
64
#if HAVE_SYS_MMAN_H
65
#include <sys/mman.h>
66
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67
#define MAP_ANONYMOUS MAP_ANON
68
#endif
69
#endif
70
#if HAVE_VIRTUALALLOC
71
#define WIN32_LEAN_AND_MEAN
72
#include <windows.h>
73
#endif
74
#include "swscale.h"
75
#include "swscale_internal.h"
76
#include "rgb2rgb.h"
77
#include "libavutil/intreadwrite.h"
78
#include "libavutil/x86_cpu.h"
79
#include "libavutil/avutil.h"
80
#include "libavutil/bswap.h"
81

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

    
87
#undef MOVNTQ
88
#undef PAVGB
89

    
90
//#undef HAVE_MMX2
91
//#define HAVE_AMD3DNOW
92
//#undef HAVE_MMX
93
//#undef ARCH_X86
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_RGB48BE     \
112
        || (x)==PIX_FMT_RGB48LE     \
113
        || (x)==PIX_FMT_RGB32       \
114
        || (x)==PIX_FMT_RGB32_1     \
115
        || (x)==PIX_FMT_BGR24       \
116
        || (x)==PIX_FMT_BGR565      \
117
        || (x)==PIX_FMT_BGR555      \
118
        || (x)==PIX_FMT_BGR32       \
119
        || (x)==PIX_FMT_BGR32_1     \
120
        || (x)==PIX_FMT_RGB24       \
121
        || (x)==PIX_FMT_RGB565      \
122
        || (x)==PIX_FMT_RGB555      \
123
        || (x)==PIX_FMT_GRAY8       \
124
        || (x)==PIX_FMT_YUV410P     \
125
        || (x)==PIX_FMT_YUV440P     \
126
        || (x)==PIX_FMT_GRAY16BE    \
127
        || (x)==PIX_FMT_GRAY16LE    \
128
        || (x)==PIX_FMT_YUV444P     \
129
        || (x)==PIX_FMT_YUV422P     \
130
        || (x)==PIX_FMT_YUV411P     \
131
        || (x)==PIX_FMT_PAL8        \
132
        || (x)==PIX_FMT_BGR8        \
133
        || (x)==PIX_FMT_RGB8        \
134
        || (x)==PIX_FMT_BGR4_BYTE   \
135
        || (x)==PIX_FMT_RGB4_BYTE   \
136
        || (x)==PIX_FMT_YUV440P     \
137
        || (x)==PIX_FMT_MONOWHITE   \
138
        || (x)==PIX_FMT_MONOBLACK   \
139
        || (x)==PIX_FMT_YUV420P16LE   \
140
        || (x)==PIX_FMT_YUV422P16LE   \
141
        || (x)==PIX_FMT_YUV444P16LE   \
142
        || (x)==PIX_FMT_YUV420P16BE   \
143
        || (x)==PIX_FMT_YUV422P16BE   \
144
        || (x)==PIX_FMT_YUV444P16BE   \
145
    )
146
#define isSupportedOut(x)   (       \
147
           (x)==PIX_FMT_YUV420P     \
148
        || (x)==PIX_FMT_YUVA420P    \
149
        || (x)==PIX_FMT_YUYV422     \
150
        || (x)==PIX_FMT_UYVY422     \
151
        || (x)==PIX_FMT_YUV444P     \
152
        || (x)==PIX_FMT_YUV422P     \
153
        || (x)==PIX_FMT_YUV411P     \
154
        || isRGB(x)                 \
155
        || isBGR(x)                 \
156
        || (x)==PIX_FMT_NV12        \
157
        || (x)==PIX_FMT_NV21        \
158
        || (x)==PIX_FMT_GRAY16BE    \
159
        || (x)==PIX_FMT_GRAY16LE    \
160
        || (x)==PIX_FMT_GRAY8       \
161
        || (x)==PIX_FMT_YUV410P     \
162
        || (x)==PIX_FMT_YUV440P     \
163
        || (x)==PIX_FMT_YUV420P16LE   \
164
        || (x)==PIX_FMT_YUV422P16LE   \
165
        || (x)==PIX_FMT_YUV444P16LE   \
166
        || (x)==PIX_FMT_YUV420P16BE   \
167
        || (x)==PIX_FMT_YUV422P16BE   \
168
        || (x)==PIX_FMT_YUV444P16BE   \
169
    )
170
#define isPacked(x)         (       \
171
           (x)==PIX_FMT_PAL8        \
172
        || (x)==PIX_FMT_YUYV422     \
173
        || (x)==PIX_FMT_UYVY422     \
174
        || isRGB(x)                 \
175
        || isBGR(x)                 \
176
    )
177
#define usePal(x)           (       \
178
           (x)==PIX_FMT_PAL8        \
179
        || (x)==PIX_FMT_BGR4_BYTE   \
180
        || (x)==PIX_FMT_RGB4_BYTE   \
181
        || (x)==PIX_FMT_BGR8        \
182
        || (x)==PIX_FMT_RGB8        \
183
    )
184

    
185
#define RGB2YUV_SHIFT 15
186
#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
187
#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
188
#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
189
#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
190
#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
191
#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
192
#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
193
#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
194
#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
195

    
196
extern const int32_t ff_yuv2rgb_coeffs[8][4];
197

    
198
static const double rgb2yuv_table[8][9]={
199
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
200
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
201
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
202
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
203
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
204
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
205
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
206
    {0.701 , 0.087 , 0.212 , -0.384, 0.5  -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
207
};
208

    
209
/*
210
NOTES
211
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
212

213
TODO
214
more intelligent misalignment avoidance for the horizontal scaler
215
write special vertical cubic upscale version
216
optimize C code (YV12 / minmax)
217
add support for packed pixel YUV input & output
218
add support for Y8 output
219
optimize BGR24 & BGR32
220
add BGR4 output support
221
write special BGR->BGR scaler
222
*/
223

    
224
#if ARCH_X86 && CONFIG_GPL
225
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
226
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
227
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
228
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
229
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
230
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
231
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
232
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
233

    
234
const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
235
        0x0103010301030103LL,
236
        0x0200020002000200LL,};
237

    
238
const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
239
        0x0602060206020602LL,
240
        0x0004000400040004LL,};
241

    
242
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
243
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
244
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
245
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
246
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
247
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
248

    
249
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
250
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
251
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
252

    
253
#ifdef FAST_BGR2YV12
254
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
255
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
256
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
257
#else
258
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
259
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
260
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
261
#endif /* FAST_BGR2YV12 */
262
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
263
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
264
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
265

    
266
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
267
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
268
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
269
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
270
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
271

    
272
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
273
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
274
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
275
};
276

    
277
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
278

    
279
#endif /* ARCH_X86 && CONFIG_GPL */
280

    
281
// clipping helper table for C implementations:
282
static unsigned char clip_table[768];
283

    
284
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
285

    
286
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
287
{  1,   3,   1,   3,   1,   3,   1,   3, },
288
{  2,   0,   2,   0,   2,   0,   2,   0, },
289
};
290

    
291
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
292
{  6,   2,   6,   2,   6,   2,   6,   2, },
293
{  0,   4,   0,   4,   0,   4,   0,   4, },
294
};
295

    
296
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
297
{ 17,   9,  23,  15,  16,   8,  22,  14, },
298
{  5,  29,   3,  27,   4,  28,   2,  26, },
299
{ 21,  13,  19,  11,  20,  12,  18,  10, },
300
{  0,  24,   6,  30,   1,  25,   7,  31, },
301
{ 16,   8,  22,  14,  17,   9,  23,  15, },
302
{  4,  28,   2,  26,   5,  29,   3,  27, },
303
{ 20,  12,  18,  10,  21,  13,  19,  11, },
304
{  1,  25,   7,  31,   0,  24,   6,  30, },
305
};
306

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

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

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

    
481
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
482
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
483
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
484
                                                    int dstW, int chrDstW, int big_endian)
485
{
486
    //FIXME Optimize (just quickly written not optimized..)
487
    int i;
488

    
489
    for (i = 0; i < dstW; i++) {
490
        int val = 1 << 10;
491
        int j;
492

    
493
        for (j = 0; j < lumFilterSize; j++)
494
            val += lumSrc[j][i] * lumFilter[j];
495

    
496
        if (big_endian) {
497
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
498
        } else {
499
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
500
        }
501
    }
502

    
503
    if (uDest) {
504
        for (i = 0; i < chrDstW; i++) {
505
            int u = 1 << 10;
506
            int v = 1 << 10;
507
            int j;
508

    
509
            for (j = 0; j < chrFilterSize; j++) {
510
                u += chrSrc[j][i       ] * chrFilter[j];
511
                v += chrSrc[j][i + VOFW] * chrFilter[j];
512
            }
513

    
514
            if (big_endian) {
515
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
516
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
517
            } else {
518
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
519
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
520
            }
521
        }
522
    }
523

    
524
    if (CONFIG_SWSCALE_ALPHA && aDest) {
525
        for (i = 0; i < dstW; i++) {
526
            int val = 1 << 10;
527
            int j;
528

    
529
            for (j = 0; j < lumFilterSize; j++)
530
                val += alpSrc[j][i] * lumFilter[j];
531

    
532
            if (big_endian) {
533
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
534
            } else {
535
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
536
            }
537
        }
538
    }
539
}
540

    
541
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
542
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
543
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
544
                                 enum PixelFormat dstFormat)
545
{
546
    if (isBE(dstFormat)) {
547
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
548
                               chrFilter, chrSrc, chrFilterSize,
549
                               alpSrc,
550
                               dest, uDest, vDest, aDest,
551
                               dstW, chrDstW, 1);
552
    } else {
553
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
554
                               chrFilter, chrSrc, chrFilterSize,
555
                               alpSrc,
556
                               dest, uDest, vDest, aDest,
557
                               dstW, chrDstW, 0);
558
    }
559
}
560

    
561
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
562
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
563
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
564
{
565
    //FIXME Optimize (just quickly written not optimized..)
566
    int i;
567
    for (i=0; i<dstW; i++) {
568
        int val=1<<18;
569
        int j;
570
        for (j=0; j<lumFilterSize; j++)
571
            val += lumSrc[j][i] * lumFilter[j];
572

    
573
        dest[i]= av_clip_uint8(val>>19);
574
    }
575

    
576
    if (uDest)
577
        for (i=0; i<chrDstW; i++) {
578
            int u=1<<18;
579
            int v=1<<18;
580
            int j;
581
            for (j=0; j<chrFilterSize; j++) {
582
                u += chrSrc[j][i] * chrFilter[j];
583
                v += chrSrc[j][i + VOFW] * chrFilter[j];
584
            }
585

    
586
            uDest[i]= av_clip_uint8(u>>19);
587
            vDest[i]= av_clip_uint8(v>>19);
588
        }
589

    
590
    if (CONFIG_SWSCALE_ALPHA && aDest)
591
        for (i=0; i<dstW; i++) {
592
            int val=1<<18;
593
            int j;
594
            for (j=0; j<lumFilterSize; j++)
595
                val += alpSrc[j][i] * lumFilter[j];
596

    
597
            aDest[i]= av_clip_uint8(val>>19);
598
        }
599

    
600
}
601

    
602
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
603
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
604
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
605
{
606
    //FIXME Optimize (just quickly written not optimized..)
607
    int i;
608
    for (i=0; i<dstW; i++) {
609
        int val=1<<18;
610
        int j;
611
        for (j=0; j<lumFilterSize; j++)
612
            val += lumSrc[j][i] * lumFilter[j];
613

    
614
        dest[i]= av_clip_uint8(val>>19);
615
    }
616

    
617
    if (!uDest)
618
        return;
619

    
620
    if (dstFormat == PIX_FMT_NV12)
621
        for (i=0; i<chrDstW; i++) {
622
            int u=1<<18;
623
            int v=1<<18;
624
            int j;
625
            for (j=0; j<chrFilterSize; j++) {
626
                u += chrSrc[j][i] * chrFilter[j];
627
                v += chrSrc[j][i + VOFW] * chrFilter[j];
628
            }
629

    
630
            uDest[2*i]= av_clip_uint8(u>>19);
631
            uDest[2*i+1]= av_clip_uint8(v>>19);
632
        }
633
    else
634
        for (i=0; i<chrDstW; i++) {
635
            int u=1<<18;
636
            int v=1<<18;
637
            int j;
638
            for (j=0; j<chrFilterSize; j++) {
639
                u += chrSrc[j][i] * chrFilter[j];
640
                v += chrSrc[j][i + VOFW] * chrFilter[j];
641
            }
642

    
643
            uDest[2*i]= av_clip_uint8(v>>19);
644
            uDest[2*i+1]= av_clip_uint8(u>>19);
645
        }
646
}
647

    
648
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
649
    for (i=0; i<(dstW>>1); i++) {\
650
        int j;\
651
        int Y1 = 1<<18;\
652
        int Y2 = 1<<18;\
653
        int U  = 1<<18;\
654
        int V  = 1<<18;\
655
        int av_unused A1, A2;\
656
        type av_unused *r, *b, *g;\
657
        const int i2= 2*i;\
658
        \
659
        for (j=0; j<lumFilterSize; j++) {\
660
            Y1 += lumSrc[j][i2] * lumFilter[j];\
661
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
662
        }\
663
        for (j=0; j<chrFilterSize; j++) {\
664
            U += chrSrc[j][i] * chrFilter[j];\
665
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
666
        }\
667
        Y1>>=19;\
668
        Y2>>=19;\
669
        U >>=19;\
670
        V >>=19;\
671
        if (alpha) {\
672
            A1 = 1<<18;\
673
            A2 = 1<<18;\
674
            for (j=0; j<lumFilterSize; j++) {\
675
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
676
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
677
            }\
678
            A1>>=19;\
679
            A2>>=19;\
680
        }\
681

    
682
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
683
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
684
        if ((Y1|Y2|U|V)&256) {\
685
            if (Y1>255)   Y1=255; \
686
            else if (Y1<0)Y1=0;   \
687
            if (Y2>255)   Y2=255; \
688
            else if (Y2<0)Y2=0;   \
689
            if (U>255)    U=255;  \
690
            else if (U<0) U=0;    \
691
            if (V>255)    V=255;  \
692
            else if (V<0) V=0;    \
693
        }\
694
        if (alpha && ((A1|A2)&256)) {\
695
            A1=av_clip_uint8(A1);\
696
            A2=av_clip_uint8(A2);\
697
        }
698

    
699
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
700
    for (i=0; i<dstW; i++) {\
701
        int j;\
702
        int Y = 0;\
703
        int U = -128<<19;\
704
        int V = -128<<19;\
705
        int av_unused A;\
706
        int R,G,B;\
707
        \
708
        for (j=0; j<lumFilterSize; j++) {\
709
            Y += lumSrc[j][i     ] * lumFilter[j];\
710
        }\
711
        for (j=0; j<chrFilterSize; j++) {\
712
            U += chrSrc[j][i     ] * chrFilter[j];\
713
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
714
        }\
715
        Y >>=10;\
716
        U >>=10;\
717
        V >>=10;\
718
        if (alpha) {\
719
            A = rnd;\
720
            for (j=0; j<lumFilterSize; j++)\
721
                A += alpSrc[j][i     ] * lumFilter[j];\
722
            A >>=19;\
723
            if (A&256)\
724
                A = av_clip_uint8(A);\
725
        }\
726

    
727
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
728
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
729
        Y-= c->yuv2rgb_y_offset;\
730
        Y*= c->yuv2rgb_y_coeff;\
731
        Y+= rnd;\
732
        R= Y + V*c->yuv2rgb_v2r_coeff;\
733
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
734
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
735
        if ((R|G|B)&(0xC0000000)) {\
736
            if (R>=(256<<22))   R=(256<<22)-1; \
737
            else if (R<0)R=0;   \
738
            if (G>=(256<<22))   G=(256<<22)-1; \
739
            else if (G<0)G=0;   \
740
            if (B>=(256<<22))   B=(256<<22)-1; \
741
            else if (B<0)B=0;   \
742
        }\
743

    
744

    
745
#define YSCALE_YUV_2_GRAY16_C \
746
    for (i=0; i<(dstW>>1); i++) {\
747
        int j;\
748
        int Y1 = 1<<18;\
749
        int Y2 = 1<<18;\
750
        int U  = 1<<18;\
751
        int V  = 1<<18;\
752
        \
753
        const int i2= 2*i;\
754
        \
755
        for (j=0; j<lumFilterSize; j++) {\
756
            Y1 += lumSrc[j][i2] * lumFilter[j];\
757
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
758
        }\
759
        Y1>>=11;\
760
        Y2>>=11;\
761
        if ((Y1|Y2|U|V)&65536) {\
762
            if (Y1>65535)   Y1=65535; \
763
            else if (Y1<0)Y1=0;   \
764
            if (Y2>65535)   Y2=65535; \
765
            else if (Y2<0)Y2=0;   \
766
        }
767

    
768
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
769
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
770
    r = (type *)c->table_rV[V];   \
771
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
772
    b = (type *)c->table_bU[U];   \
773

    
774
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
775
    for (i=0; i<(dstW>>1); i++) { \
776
        const int i2= 2*i;       \
777
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
778
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
779
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
780
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
781
        type av_unused *r, *b, *g;                                    \
782
        int av_unused A1, A2;                                         \
783
        if (alpha) {\
784
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
785
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
786
        }\
787

    
788
#define YSCALE_YUV_2_GRAY16_2_C   \
789
    for (i=0; i<(dstW>>1); i++) { \
790
        const int i2= 2*i;       \
791
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
792
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
793

    
794
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
795
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
796
    r = (type *)c->table_rV[V];\
797
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
798
    b = (type *)c->table_bU[U];\
799

    
800
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
801
    for (i=0; i<(dstW>>1); i++) {\
802
        const int i2= 2*i;\
803
        int Y1= buf0[i2  ]>>7;\
804
        int Y2= buf0[i2+1]>>7;\
805
        int U= (uvbuf1[i     ])>>7;\
806
        int V= (uvbuf1[i+VOFW])>>7;\
807
        type av_unused *r, *b, *g;\
808
        int av_unused A1, A2;\
809
        if (alpha) {\
810
            A1= abuf0[i2  ]>>7;\
811
            A2= abuf0[i2+1]>>7;\
812
        }\
813

    
814
#define YSCALE_YUV_2_GRAY16_1_C \
815
    for (i=0; i<(dstW>>1); i++) {\
816
        const int i2= 2*i;\
817
        int Y1= buf0[i2  ]<<1;\
818
        int Y2= buf0[i2+1]<<1;\
819

    
820
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
821
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
822
    r = (type *)c->table_rV[V];\
823
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
824
    b = (type *)c->table_bU[U];\
825

    
826
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
827
    for (i=0; i<(dstW>>1); i++) {\
828
        const int i2= 2*i;\
829
        int Y1= buf0[i2  ]>>7;\
830
        int Y2= buf0[i2+1]>>7;\
831
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
832
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
833
        type av_unused *r, *b, *g;\
834
        int av_unused A1, A2;\
835
        if (alpha) {\
836
            A1= abuf0[i2  ]>>7;\
837
            A2= abuf0[i2+1]>>7;\
838
        }\
839

    
840
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
841
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
842
    r = (type *)c->table_rV[V];\
843
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
844
    b = (type *)c->table_bU[U];\
845

    
846
#define YSCALE_YUV_2_MONO2_C \
847
    const uint8_t * const d128=dither_8x8_220[y&7];\
848
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
849
    for (i=0; i<dstW-7; i+=8) {\
850
        int acc;\
851
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
852
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
853
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
854
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
855
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
856
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
857
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
858
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
859
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
860
        dest++;\
861
    }\
862

    
863

    
864
#define YSCALE_YUV_2_MONOX_C \
865
    const uint8_t * const d128=dither_8x8_220[y&7];\
866
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
867
    int acc=0;\
868
    for (i=0; i<dstW-1; i+=2) {\
869
        int j;\
870
        int Y1=1<<18;\
871
        int Y2=1<<18;\
872
\
873
        for (j=0; j<lumFilterSize; j++) {\
874
            Y1 += lumSrc[j][i] * lumFilter[j];\
875
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
876
        }\
877
        Y1>>=19;\
878
        Y2>>=19;\
879
        if ((Y1|Y2)&256) {\
880
            if (Y1>255)   Y1=255;\
881
            else if (Y1<0)Y1=0;\
882
            if (Y2>255)   Y2=255;\
883
            else if (Y2<0)Y2=0;\
884
        }\
885
        acc+= acc + g[Y1+d128[(i+0)&7]];\
886
        acc+= acc + g[Y2+d128[(i+1)&7]];\
887
        if ((i&7)==6) {\
888
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
889
            dest++;\
890
        }\
891
    }
892

    
893

    
894
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
895
    switch(c->dstFormat) {\
896
    case PIX_FMT_RGB48BE:\
897
    case PIX_FMT_RGB48LE:\
898
        func(uint8_t,0)\
899
            ((uint8_t*)dest)[ 0]= r[Y1];\
900
            ((uint8_t*)dest)[ 1]= r[Y1];\
901
            ((uint8_t*)dest)[ 2]= g[Y1];\
902
            ((uint8_t*)dest)[ 3]= g[Y1];\
903
            ((uint8_t*)dest)[ 4]= b[Y1];\
904
            ((uint8_t*)dest)[ 5]= b[Y1];\
905
            ((uint8_t*)dest)[ 6]= r[Y2];\
906
            ((uint8_t*)dest)[ 7]= r[Y2];\
907
            ((uint8_t*)dest)[ 8]= g[Y2];\
908
            ((uint8_t*)dest)[ 9]= g[Y2];\
909
            ((uint8_t*)dest)[10]= b[Y2];\
910
            ((uint8_t*)dest)[11]= b[Y2];\
911
            dest+=12;\
912
        }\
913
        break;\
914
    case PIX_FMT_RGBA:\
915
    case PIX_FMT_BGRA:\
916
        if (CONFIG_SMALL) {\
917
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
918
            func(uint32_t,needAlpha)\
919
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
920
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
921
            }\
922
        } else {\
923
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
924
                func(uint32_t,1)\
925
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
926
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
927
                }\
928
            } else {\
929
                func(uint32_t,0)\
930
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
931
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
932
                }\
933
            }\
934
        }\
935
        break;\
936
    case PIX_FMT_ARGB:\
937
    case PIX_FMT_ABGR:\
938
        if (CONFIG_SMALL) {\
939
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
940
            func(uint32_t,needAlpha)\
941
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
942
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
943
            }\
944
        } else {\
945
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
946
                func(uint32_t,1)\
947
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
948
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
949
                }\
950
            } else {\
951
                func(uint32_t,0)\
952
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
953
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
954
                }\
955
            }\
956
        }                \
957
        break;\
958
    case PIX_FMT_RGB24:\
959
        func(uint8_t,0)\
960
            ((uint8_t*)dest)[0]= r[Y1];\
961
            ((uint8_t*)dest)[1]= g[Y1];\
962
            ((uint8_t*)dest)[2]= b[Y1];\
963
            ((uint8_t*)dest)[3]= r[Y2];\
964
            ((uint8_t*)dest)[4]= g[Y2];\
965
            ((uint8_t*)dest)[5]= b[Y2];\
966
            dest+=6;\
967
        }\
968
        break;\
969
    case PIX_FMT_BGR24:\
970
        func(uint8_t,0)\
971
            ((uint8_t*)dest)[0]= b[Y1];\
972
            ((uint8_t*)dest)[1]= g[Y1];\
973
            ((uint8_t*)dest)[2]= r[Y1];\
974
            ((uint8_t*)dest)[3]= b[Y2];\
975
            ((uint8_t*)dest)[4]= g[Y2];\
976
            ((uint8_t*)dest)[5]= r[Y2];\
977
            dest+=6;\
978
        }\
979
        break;\
980
    case PIX_FMT_RGB565:\
981
    case PIX_FMT_BGR565:\
982
        {\
983
            const int dr1= dither_2x2_8[y&1    ][0];\
984
            const int dg1= dither_2x2_4[y&1    ][0];\
985
            const int db1= dither_2x2_8[(y&1)^1][0];\
986
            const int dr2= dither_2x2_8[y&1    ][1];\
987
            const int dg2= dither_2x2_4[y&1    ][1];\
988
            const int db2= dither_2x2_8[(y&1)^1][1];\
989
            func(uint16_t,0)\
990
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
991
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
992
            }\
993
        }\
994
        break;\
995
    case PIX_FMT_RGB555:\
996
    case PIX_FMT_BGR555:\
997
        {\
998
            const int dr1= dither_2x2_8[y&1    ][0];\
999
            const int dg1= dither_2x2_8[y&1    ][1];\
1000
            const int db1= dither_2x2_8[(y&1)^1][0];\
1001
            const int dr2= dither_2x2_8[y&1    ][1];\
1002
            const int dg2= dither_2x2_8[y&1    ][0];\
1003
            const int db2= dither_2x2_8[(y&1)^1][1];\
1004
            func(uint16_t,0)\
1005
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
1006
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
1007
            }\
1008
        }\
1009
        break;\
1010
    case PIX_FMT_RGB8:\
1011
    case PIX_FMT_BGR8:\
1012
        {\
1013
            const uint8_t * const d64= dither_8x8_73[y&7];\
1014
            const uint8_t * const d32= dither_8x8_32[y&7];\
1015
            func(uint8_t,0)\
1016
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
1017
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
1018
            }\
1019
        }\
1020
        break;\
1021
    case PIX_FMT_RGB4:\
1022
    case PIX_FMT_BGR4:\
1023
        {\
1024
            const uint8_t * const d64= dither_8x8_73 [y&7];\
1025
            const uint8_t * const d128=dither_8x8_220[y&7];\
1026
            func(uint8_t,0)\
1027
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
1028
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
1029
            }\
1030
        }\
1031
        break;\
1032
    case PIX_FMT_RGB4_BYTE:\
1033
    case PIX_FMT_BGR4_BYTE:\
1034
        {\
1035
            const uint8_t * const d64= dither_8x8_73 [y&7];\
1036
            const uint8_t * const d128=dither_8x8_220[y&7];\
1037
            func(uint8_t,0)\
1038
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
1039
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
1040
            }\
1041
        }\
1042
        break;\
1043
    case PIX_FMT_MONOBLACK:\
1044
    case PIX_FMT_MONOWHITE:\
1045
        {\
1046
            func_monoblack\
1047
        }\
1048
        break;\
1049
    case PIX_FMT_YUYV422:\
1050
        func2\
1051
            ((uint8_t*)dest)[2*i2+0]= Y1;\
1052
            ((uint8_t*)dest)[2*i2+1]= U;\
1053
            ((uint8_t*)dest)[2*i2+2]= Y2;\
1054
            ((uint8_t*)dest)[2*i2+3]= V;\
1055
        }                \
1056
        break;\
1057
    case PIX_FMT_UYVY422:\
1058
        func2\
1059
            ((uint8_t*)dest)[2*i2+0]= U;\
1060
            ((uint8_t*)dest)[2*i2+1]= Y1;\
1061
            ((uint8_t*)dest)[2*i2+2]= V;\
1062
            ((uint8_t*)dest)[2*i2+3]= Y2;\
1063
        }                \
1064
        break;\
1065
    case PIX_FMT_GRAY16BE:\
1066
        func_g16\
1067
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1068
            ((uint8_t*)dest)[2*i2+1]= Y1;\
1069
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1070
            ((uint8_t*)dest)[2*i2+3]= Y2;\
1071
        }                \
1072
        break;\
1073
    case PIX_FMT_GRAY16LE:\
1074
        func_g16\
1075
            ((uint8_t*)dest)[2*i2+0]= Y1;\
1076
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1077
            ((uint8_t*)dest)[2*i2+2]= Y2;\
1078
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1079
        }                \
1080
        break;\
1081
    }\
1082

    
1083

    
1084
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1085
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1086
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1087
{
1088
    int i;
1089
    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)
1090
}
1091

    
1092
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1093
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1094
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1095
{
1096
    int i;
1097
    int step= fmt_depth(c->dstFormat)/8;
1098
    int aidx= 3;
1099

    
1100
    switch(c->dstFormat) {
1101
    case PIX_FMT_ARGB:
1102
        dest++;
1103
        aidx= 0;
1104
    case PIX_FMT_RGB24:
1105
        aidx--;
1106
    case PIX_FMT_RGBA:
1107
        if (CONFIG_SMALL) {
1108
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1109
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1110
                dest[aidx]= needAlpha ? A : 255;
1111
                dest[0]= R>>22;
1112
                dest[1]= G>>22;
1113
                dest[2]= B>>22;
1114
                dest+= step;
1115
            }
1116
        } else {
1117
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1118
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1119
                    dest[aidx]= A;
1120
                    dest[0]= R>>22;
1121
                    dest[1]= G>>22;
1122
                    dest[2]= B>>22;
1123
                    dest+= step;
1124
                }
1125
            } else {
1126
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1127
                    dest[aidx]= 255;
1128
                    dest[0]= R>>22;
1129
                    dest[1]= G>>22;
1130
                    dest[2]= B>>22;
1131
                    dest+= step;
1132
                }
1133
            }
1134
        }
1135
        break;
1136
    case PIX_FMT_ABGR:
1137
        dest++;
1138
        aidx= 0;
1139
    case PIX_FMT_BGR24:
1140
        aidx--;
1141
    case PIX_FMT_BGRA:
1142
        if (CONFIG_SMALL) {
1143
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1144
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1145
                dest[aidx]= needAlpha ? A : 255;
1146
                dest[0]= B>>22;
1147
                dest[1]= G>>22;
1148
                dest[2]= R>>22;
1149
                dest+= step;
1150
            }
1151
        } else {
1152
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1153
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1154
                    dest[aidx]= A;
1155
                    dest[0]= B>>22;
1156
                    dest[1]= G>>22;
1157
                    dest[2]= R>>22;
1158
                    dest+= step;
1159
                }
1160
            } else {
1161
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1162
                    dest[aidx]= 255;
1163
                    dest[0]= B>>22;
1164
                    dest[1]= G>>22;
1165
                    dest[2]= R>>22;
1166
                    dest+= step;
1167
                }
1168
            }
1169
        }
1170
        break;
1171
    default:
1172
        assert(0);
1173
    }
1174
}
1175

    
1176
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1177
{
1178
    int i;
1179
    uint8_t *ptr = plane + stride*y;
1180
    for (i=0; i<height; i++) {
1181
        memset(ptr, val, width);
1182
        ptr += stride;
1183
    }
1184
}
1185

    
1186
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1187
{
1188
    int i;
1189
    for (i = 0; i < width; i++) {
1190
        int r = src[i*6+0];
1191
        int g = src[i*6+2];
1192
        int b = src[i*6+4];
1193

    
1194
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1195
    }
1196
}
1197

    
1198
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1199
                             uint8_t *src1, uint8_t *src2, int width)
1200
{
1201
    int i;
1202
    assert(src1==src2);
1203
    for (i = 0; i < width; i++) {
1204
        int r = src1[6*i + 0];
1205
        int g = src1[6*i + 2];
1206
        int b = src1[6*i + 4];
1207

    
1208
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1209
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1210
    }
1211
}
1212

    
1213
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1214
                                  uint8_t *src1, uint8_t *src2, int width)
1215
{
1216
    int i;
1217
    assert(src1==src2);
1218
    for (i = 0; i < width; i++) {
1219
        int r= src1[12*i + 0] + src1[12*i + 6];
1220
        int g= src1[12*i + 2] + src1[12*i + 8];
1221
        int b= src1[12*i + 4] + src1[12*i + 10];
1222

    
1223
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1224
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1225
    }
1226
}
1227

    
1228
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1229
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1230
{\
1231
    int i;\
1232
    for (i=0; i<width; i++) {\
1233
        int b= (((const type*)src)[i]>>shb)&maskb;\
1234
        int g= (((const type*)src)[i]>>shg)&maskg;\
1235
        int r= (((const type*)src)[i]>>shr)&maskr;\
1236
\
1237
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1238
    }\
1239
}
1240

    
1241
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1242
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1243
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1244
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1245
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1246
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1247

    
1248
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1249
{
1250
    int i;
1251
    for (i=0; i<width; i++) {
1252
        dst[i]= src[4*i];
1253
    }
1254
}
1255

    
1256
#define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1257
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1258
{\
1259
    int i;\
1260
    for (i=0; i<width; i++) {\
1261
        int b= (((const type*)src)[i]&maskb)>>shb;\
1262
        int g= (((const type*)src)[i]&maskg)>>shg;\
1263
        int r= (((const type*)src)[i]&maskr)>>shr;\
1264
\
1265
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1266
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1267
    }\
1268
}\
1269
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1270
{\
1271
    int i;\
1272
    for (i=0; i<width; i++) {\
1273
        int pix0= ((const type*)src)[2*i+0];\
1274
        int pix1= ((const type*)src)[2*i+1];\
1275
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1276
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1277
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1278
        g&= maskg|(2*maskg);\
1279
\
1280
        g>>=shg;\
1281
\
1282
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1283
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1284
    }\
1285
}
1286

    
1287
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1288
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1289
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0,          0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1290
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0,          0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1291
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0,          0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1292
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0,          0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1293

    
1294
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1295
{
1296
    int i;
1297
    for (i=0; i<width; i++) {
1298
        int d= src[i];
1299

    
1300
        dst[i]= pal[d] & 0xFF;
1301
    }
1302
}
1303

    
1304
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1305
                           const uint8_t *src1, const uint8_t *src2,
1306
                           long width, uint32_t *pal)
1307
{
1308
    int i;
1309
    assert(src1 == src2);
1310
    for (i=0; i<width; i++) {
1311
        int p= pal[src1[i]];
1312

    
1313
        dstU[i]= p>>8;
1314
        dstV[i]= p>>16;
1315
    }
1316
}
1317

    
1318
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1319
{
1320
    int i, j;
1321
    for (i=0; i<width/8; i++) {
1322
        int d= ~src[i];
1323
        for(j=0; j<8; j++)
1324
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1325
    }
1326
}
1327

    
1328
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1329
{
1330
    int i, j;
1331
    for (i=0; i<width/8; i++) {
1332
        int d= src[i];
1333
        for(j=0; j<8; j++)
1334
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1335
    }
1336
}
1337

    
1338

    
1339
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1340
//Plain C versions
1341
#if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1342
#define COMPILE_C
1343
#endif
1344

    
1345
#if ARCH_PPC
1346
#if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1347
#define COMPILE_ALTIVEC
1348
#endif
1349
#endif //ARCH_PPC
1350

    
1351
#if ARCH_X86
1352

    
1353
#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1354
#define COMPILE_MMX
1355
#endif
1356

    
1357
#if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1358
#define COMPILE_MMX2
1359
#endif
1360

    
1361
#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1362
#define COMPILE_3DNOW
1363
#endif
1364
#endif //ARCH_X86
1365

    
1366
#define COMPILE_TEMPLATE_MMX 0
1367
#define COMPILE_TEMPLATE_MMX2 0
1368
#define COMPILE_TEMPLATE_AMD3DNOW 0
1369
#define COMPILE_TEMPLATE_ALTIVEC 0
1370

    
1371
#ifdef COMPILE_C
1372
#define RENAME(a) a ## _C
1373
#include "swscale_template.c"
1374
#endif
1375

    
1376
#ifdef COMPILE_ALTIVEC
1377
#undef RENAME
1378
#undef COMPILE_TEMPLATE_ALTIVEC
1379
#define COMPILE_TEMPLATE_ALTIVEC 1
1380
#define RENAME(a) a ## _altivec
1381
#include "swscale_template.c"
1382
#endif
1383

    
1384
#if ARCH_X86
1385

    
1386
//MMX versions
1387
#ifdef COMPILE_MMX
1388
#undef RENAME
1389
#undef COMPILE_TEMPLATE_MMX
1390
#undef COMPILE_TEMPLATE_MMX2
1391
#undef COMPILE_TEMPLATE_AMD3DNOW
1392
#define COMPILE_TEMPLATE_MMX 1
1393
#define COMPILE_TEMPLATE_MMX2 0
1394
#define COMPILE_TEMPLATE_AMD3DNOW 0
1395
#define RENAME(a) a ## _MMX
1396
#include "swscale_template.c"
1397
#endif
1398

    
1399
//MMX2 versions
1400
#ifdef COMPILE_MMX2
1401
#undef RENAME
1402
#undef COMPILE_TEMPLATE_MMX
1403
#undef COMPILE_TEMPLATE_MMX2
1404
#undef COMPILE_TEMPLATE_AMD3DNOW
1405
#define COMPILE_TEMPLATE_MMX 1
1406
#define COMPILE_TEMPLATE_MMX2 1
1407
#define COMPILE_TEMPLATE_AMD3DNOW 0
1408
#define RENAME(a) a ## _MMX2
1409
#include "swscale_template.c"
1410
#endif
1411

    
1412
//3DNOW versions
1413
#ifdef COMPILE_3DNOW
1414
#undef RENAME
1415
#undef COMPILE_TEMPLATE_MMX
1416
#undef COMPILE_TEMPLATE_MMX2
1417
#undef COMPILE_TEMPLATE_AMD3DNOW
1418
#define COMPILE_TEMPLATE_MMX 1
1419
#define COMPILE_TEMPLATE_MMX2 0
1420
#define COMPILE_TEMPLATE_AMD3DNOW 1
1421
#define RENAME(a) a ## _3DNow
1422
#include "swscale_template.c"
1423
#endif
1424

    
1425
#endif //ARCH_X86
1426

    
1427
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1428
{
1429
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1430
    if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1431
    else           return getSplineCoeff(        0.0,
1432
                                          b+ 2.0*c + 3.0*d,
1433
                                                 c + 3.0*d,
1434
                                         -b- 3.0*c - 6.0*d,
1435
                                         dist-1.0);
1436
}
1437

    
1438
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1439
                             int srcW, int dstW, int filterAlign, int one, int flags,
1440
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1441
{
1442
    int i;
1443
    int filterSize;
1444
    int filter2Size;
1445
    int minFilterSize;
1446
    int64_t *filter=NULL;
1447
    int64_t *filter2=NULL;
1448
    const int64_t fone= 1LL<<54;
1449
    int ret= -1;
1450
#if ARCH_X86
1451
    if (flags & SWS_CPU_CAPS_MMX)
1452
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1453
#endif
1454

    
1455
    // NOTE: the +1 is for the MMX scaler which reads over the end
1456
    FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
1457

    
1458
    if (FFABS(xInc - 0x10000) <10) { // unscaled
1459
        int i;
1460
        filterSize= 1;
1461
        FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1462

    
1463
        for (i=0; i<dstW; i++) {
1464
            filter[i*filterSize]= fone;
1465
            (*filterPos)[i]=i;
1466
        }
1467

    
1468
    } else if (flags&SWS_POINT) { // lame looking point sampling mode
1469
        int i;
1470
        int xDstInSrc;
1471
        filterSize= 1;
1472
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1473

    
1474
        xDstInSrc= xInc/2 - 0x8000;
1475
        for (i=0; i<dstW; i++) {
1476
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1477

    
1478
            (*filterPos)[i]= xx;
1479
            filter[i]= fone;
1480
            xDstInSrc+= xInc;
1481
        }
1482
    } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1483
        int i;
1484
        int xDstInSrc;
1485
        filterSize= 2;
1486
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1487

    
1488
        xDstInSrc= xInc/2 - 0x8000;
1489
        for (i=0; i<dstW; i++) {
1490
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1491
            int j;
1492

    
1493
            (*filterPos)[i]= xx;
1494
            //bilinear upscale / linear interpolate / area averaging
1495
            for (j=0; j<filterSize; j++) {
1496
                int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1497
                if (coeff<0) coeff=0;
1498
                filter[i*filterSize + j]= coeff;
1499
                xx++;
1500
            }
1501
            xDstInSrc+= xInc;
1502
        }
1503
    } else {
1504
        int xDstInSrc;
1505
        int sizeFactor;
1506

    
1507
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1508
        else if (flags&SWS_X)            sizeFactor=  8;
1509
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1510
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1511
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1512
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1513
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1514
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1515
        else {
1516
            sizeFactor= 0; //GCC warning killer
1517
            assert(0);
1518
        }
1519

    
1520
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1521
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1522

    
1523
        if (filterSize > srcW-2) filterSize=srcW-2;
1524

    
1525
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1526

    
1527
        xDstInSrc= xInc - 0x10000;
1528
        for (i=0; i<dstW; i++) {
1529
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1530
            int j;
1531
            (*filterPos)[i]= xx;
1532
            for (j=0; j<filterSize; j++) {
1533
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1534
                double floatd;
1535
                int64_t coeff;
1536

    
1537
                if (xInc > 1<<16)
1538
                    d= d*dstW/srcW;
1539
                floatd= d * (1.0/(1<<30));
1540

    
1541
                if (flags & SWS_BICUBIC) {
1542
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1543
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1544
                    int64_t dd = ( d*d)>>30;
1545
                    int64_t ddd= (dd*d)>>30;
1546

    
1547
                    if      (d < 1LL<<30)
1548
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1549
                    else if (d < 1LL<<31)
1550
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1551
                    else
1552
                        coeff=0.0;
1553
                    coeff *= fone>>(30+24);
1554
                }
1555
/*                else if (flags & SWS_X) {
1556
                    double p= param ? param*0.01 : 0.3;
1557
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1558
                    coeff*= pow(2.0, - p*d*d);
1559
                }*/
1560
                else if (flags & SWS_X) {
1561
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1562
                    double c;
1563

    
1564
                    if (floatd<1.0)
1565
                        c = cos(floatd*PI);
1566
                    else
1567
                        c=-1.0;
1568
                    if (c<0.0)      c= -pow(-c, A);
1569
                    else            c=  pow( c, A);
1570
                    coeff= (c*0.5 + 0.5)*fone;
1571
                } else if (flags & SWS_AREA) {
1572
                    int64_t d2= d - (1<<29);
1573
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1574
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1575
                    else coeff=0.0;
1576
                    coeff *= fone>>(30+16);
1577
                } else if (flags & SWS_GAUSS) {
1578
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1579
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
1580
                } else if (flags & SWS_SINC) {
1581
                    coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1582
                } else if (flags & SWS_LANCZOS) {
1583
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1584
                    coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1585
                    if (floatd>p) coeff=0;
1586
                } else if (flags & SWS_BILINEAR) {
1587
                    coeff= (1<<30) - d;
1588
                    if (coeff<0) coeff=0;
1589
                    coeff *= fone >> 30;
1590
                } else if (flags & SWS_SPLINE) {
1591
                    double p=-2.196152422706632;
1592
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1593
                } else {
1594
                    coeff= 0.0; //GCC warning killer
1595
                    assert(0);
1596
                }
1597

    
1598
                filter[i*filterSize + j]= coeff;
1599
                xx++;
1600
            }
1601
            xDstInSrc+= 2*xInc;
1602
        }
1603
    }
1604

    
1605
    /* apply src & dst Filter to filter -> filter2
1606
       av_free(filter);
1607
    */
1608
    assert(filterSize>0);
1609
    filter2Size= filterSize;
1610
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1611
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1612
    assert(filter2Size>0);
1613
    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
1614

    
1615
    for (i=0; i<dstW; i++) {
1616
        int j, k;
1617

    
1618
        if(srcFilter) {
1619
            for (k=0; k<srcFilter->length; k++) {
1620
                for (j=0; j<filterSize; j++)
1621
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1622
            }
1623
        } else {
1624
            for (j=0; j<filterSize; j++)
1625
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1626
        }
1627
        //FIXME dstFilter
1628

    
1629
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1630
    }
1631
    av_freep(&filter);
1632

    
1633
    /* try to reduce the filter-size (step1 find size and shift left) */
1634
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1635
    minFilterSize= 0;
1636
    for (i=dstW-1; i>=0; i--) {
1637
        int min= filter2Size;
1638
        int j;
1639
        int64_t cutOff=0.0;
1640

    
1641
        /* get rid off near zero elements on the left by shifting left */
1642
        for (j=0; j<filter2Size; j++) {
1643
            int k;
1644
            cutOff += FFABS(filter2[i*filter2Size]);
1645

    
1646
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1647

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

    
1651
            // move filter coefficients left
1652
            for (k=1; k<filter2Size; k++)
1653
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1654
            filter2[i*filter2Size + k - 1]= 0;
1655
            (*filterPos)[i]++;
1656
        }
1657

    
1658
        cutOff=0;
1659
        /* count near zeros on the right */
1660
        for (j=filter2Size-1; j>0; j--) {
1661
            cutOff += FFABS(filter2[i*filter2Size + j]);
1662

    
1663
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1664
            min--;
1665
        }
1666

    
1667
        if (min>minFilterSize) minFilterSize= min;
1668
    }
1669

    
1670
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1671
        // we can handle the special case 4,
1672
        // so we don't want to go to the full 8
1673
        if (minFilterSize < 5)
1674
            filterAlign = 4;
1675

    
1676
        // We really don't want to waste our time
1677
        // doing useless computation, so fall back on
1678
        // the scalar C code for very small filters.
1679
        // Vectorizing is worth it only if you have a
1680
        // decent-sized vector.
1681
        if (minFilterSize < 3)
1682
            filterAlign = 1;
1683
    }
1684

    
1685
    if (flags & SWS_CPU_CAPS_MMX) {
1686
        // special case for unscaled vertical filtering
1687
        if (minFilterSize == 1 && filterAlign == 2)
1688
            filterAlign= 1;
1689
    }
1690

    
1691
    assert(minFilterSize > 0);
1692
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1693
    assert(filterSize > 0);
1694
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1695
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1696
        goto fail;
1697
    *outFilterSize= filterSize;
1698

    
1699
    if (flags&SWS_PRINT_INFO)
1700
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1701
    /* try to reduce the filter-size (step2 reduce it) */
1702
    for (i=0; i<dstW; i++) {
1703
        int j;
1704

    
1705
        for (j=0; j<filterSize; j++) {
1706
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1707
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1708
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1709
                filter[i*filterSize + j]= 0;
1710
        }
1711
    }
1712

    
1713

    
1714
    //FIXME try to align filterPos if possible
1715

    
1716
    //fix borders
1717
    for (i=0; i<dstW; i++) {
1718
        int j;
1719
        if ((*filterPos)[i] < 0) {
1720
            // move filter coefficients left to compensate for filterPos
1721
            for (j=1; j<filterSize; j++) {
1722
                int left= FFMAX(j + (*filterPos)[i], 0);
1723
                filter[i*filterSize + left] += filter[i*filterSize + j];
1724
                filter[i*filterSize + j]=0;
1725
            }
1726
            (*filterPos)[i]= 0;
1727
        }
1728

    
1729
        if ((*filterPos)[i] + filterSize > srcW) {
1730
            int shift= (*filterPos)[i] + filterSize - srcW;
1731
            // move filter coefficients right to compensate for filterPos
1732
            for (j=filterSize-2; j>=0; j--) {
1733
                int right= FFMIN(j + shift, filterSize-1);
1734
                filter[i*filterSize +right] += filter[i*filterSize +j];
1735
                filter[i*filterSize +j]=0;
1736
            }
1737
            (*filterPos)[i]= srcW - filterSize;
1738
        }
1739
    }
1740

    
1741
    // Note the +1 is for the MMX scaler which reads over the end
1742
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1743
    FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
1744

    
1745
    /* normalize & store in outFilter */
1746
    for (i=0; i<dstW; i++) {
1747
        int j;
1748
        int64_t error=0;
1749
        int64_t sum=0;
1750

    
1751
        for (j=0; j<filterSize; j++) {
1752
            sum+= filter[i*filterSize + j];
1753
        }
1754
        sum= (sum + one/2)/ one;
1755
        for (j=0; j<*outFilterSize; j++) {
1756
            int64_t v= filter[i*filterSize + j] + error;
1757
            int intV= ROUNDED_DIV(v, sum);
1758
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1759
            error= v - intV*sum;
1760
        }
1761
    }
1762

    
1763
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1764
    for (i=0; i<*outFilterSize; i++) {
1765
        int j= dstW*(*outFilterSize);
1766
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1767
    }
1768

    
1769
    ret=0;
1770
fail:
1771
    av_free(filter);
1772
    av_free(filter2);
1773
    return ret;
1774
}
1775

    
1776
#ifdef COMPILE_MMX2
1777
static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1778
{
1779
    uint8_t *fragmentA;
1780
    x86_reg imm8OfPShufW1A;
1781
    x86_reg imm8OfPShufW2A;
1782
    x86_reg fragmentLengthA;
1783
    uint8_t *fragmentB;
1784
    x86_reg imm8OfPShufW1B;
1785
    x86_reg imm8OfPShufW2B;
1786
    x86_reg fragmentLengthB;
1787
    int fragmentPos;
1788

    
1789
    int xpos, i;
1790

    
1791
    // create an optimized horizontal scaling routine
1792

    
1793
    //code fragment
1794

    
1795
    __asm__ volatile(
1796
        "jmp                         9f                 \n\t"
1797
    // Begin
1798
        "0:                                             \n\t"
1799
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1800
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1801
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1802
        "punpcklbw                %%mm7, %%mm1          \n\t"
1803
        "punpcklbw                %%mm7, %%mm0          \n\t"
1804
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1805
        "1:                                             \n\t"
1806
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1807
        "2:                                             \n\t"
1808
        "psubw                    %%mm1, %%mm0          \n\t"
1809
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1810
        "pmullw                   %%mm3, %%mm0          \n\t"
1811
        "psllw                       $7, %%mm1          \n\t"
1812
        "paddw                    %%mm1, %%mm0          \n\t"
1813

    
1814
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1815

    
1816
        "add                         $8, %%"REG_a"      \n\t"
1817
    // End
1818
        "9:                                             \n\t"
1819
//        "int $3                                         \n\t"
1820
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1821
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1822
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1823
        "dec                         %1                 \n\t"
1824
        "dec                         %2                 \n\t"
1825
        "sub                         %0, %1             \n\t"
1826
        "sub                         %0, %2             \n\t"
1827
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1828
        "sub                         %0, %3             \n\t"
1829

    
1830

    
1831
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1832
        "=r" (fragmentLengthA)
1833
    );
1834

    
1835
    __asm__ volatile(
1836
        "jmp                         9f                 \n\t"
1837
    // Begin
1838
        "0:                                             \n\t"
1839
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1840
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1841
        "punpcklbw                %%mm7, %%mm0          \n\t"
1842
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1843
        "1:                                             \n\t"
1844
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1845
        "2:                                             \n\t"
1846
        "psubw                    %%mm1, %%mm0          \n\t"
1847
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1848
        "pmullw                   %%mm3, %%mm0          \n\t"
1849
        "psllw                       $7, %%mm1          \n\t"
1850
        "paddw                    %%mm1, %%mm0          \n\t"
1851

    
1852
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1853

    
1854
        "add                         $8, %%"REG_a"      \n\t"
1855
    // End
1856
        "9:                                             \n\t"
1857
//        "int                       $3                   \n\t"
1858
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1859
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1860
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1861
        "dec                         %1                 \n\t"
1862
        "dec                         %2                 \n\t"
1863
        "sub                         %0, %1             \n\t"
1864
        "sub                         %0, %2             \n\t"
1865
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1866
        "sub                         %0, %3             \n\t"
1867

    
1868

    
1869
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1870
        "=r" (fragmentLengthB)
1871
    );
1872

    
1873
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1874
    fragmentPos=0;
1875

    
1876
    for (i=0; i<dstW/numSplits; i++) {
1877
        int xx=xpos>>16;
1878

    
1879
        if ((i&3) == 0) {
1880
            int a=0;
1881
            int b=((xpos+xInc)>>16) - xx;
1882
            int c=((xpos+xInc*2)>>16) - xx;
1883
            int d=((xpos+xInc*3)>>16) - xx;
1884
            int inc                = (d+1<4);
1885
            uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
1886
            x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
1887
            x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
1888
            x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1889
            int maxShift= 3-(d+inc);
1890
            int shift=0;
1891

    
1892
            if (filterCode) {
1893
                filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1894
                filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1895
                filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1896
                filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1897
                filterPos[i/2]= xx;
1898

    
1899
                memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1900

    
1901
                filterCode[fragmentPos + imm8OfPShufW1]=
1902
                    (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1903
                filterCode[fragmentPos + imm8OfPShufW2]=
1904
                    a | (b<<2) | (c<<4) | (d<<6);
1905

    
1906
                if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1907
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1908

    
1909
                if (shift && i>=shift) {
1910
                    filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1911
                    filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1912
                    filterPos[i/2]-=shift;
1913
                }
1914
            }
1915

    
1916
            fragmentPos+= fragmentLength;
1917

    
1918
            if (filterCode)
1919
                filterCode[fragmentPos]= RET;
1920
        }
1921
        xpos+=xInc;
1922
    }
1923
    if (filterCode)
1924
        filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1925

    
1926
    return fragmentPos + 1;
1927
}
1928
#endif /* COMPILE_MMX2 */
1929

    
1930
static void globalInit(void)
1931
{
1932
    // generating tables:
1933
    int i;
1934
    for (i=0; i<768; i++) {
1935
        int c= av_clip_uint8(i-256);
1936
        clip_table[i]=c;
1937
    }
1938
}
1939

    
1940
static SwsFunc getSwsFunc(SwsContext *c)
1941
{
1942
#if CONFIG_RUNTIME_CPUDETECT
1943
    int flags = c->flags;
1944

    
1945
#if ARCH_X86 && CONFIG_GPL
1946
    // ordered per speed fastest first
1947
    if (flags & SWS_CPU_CAPS_MMX2) {
1948
        sws_init_swScale_MMX2(c);
1949
        return swScale_MMX2;
1950
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1951
        sws_init_swScale_3DNow(c);
1952
        return swScale_3DNow;
1953
    } else if (flags & SWS_CPU_CAPS_MMX) {
1954
        sws_init_swScale_MMX(c);
1955
        return swScale_MMX;
1956
    } else {
1957
        sws_init_swScale_C(c);
1958
        return swScale_C;
1959
    }
1960

    
1961
#else
1962
#if ARCH_PPC
1963
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1964
        sws_init_swScale_altivec(c);
1965
        return swScale_altivec;
1966
    } else {
1967
        sws_init_swScale_C(c);
1968
        return swScale_C;
1969
    }
1970
#endif
1971
    sws_init_swScale_C(c);
1972
    return swScale_C;
1973
#endif /* ARCH_X86 && CONFIG_GPL */
1974
#else //CONFIG_RUNTIME_CPUDETECT
1975
#if   COMPILE_TEMPLATE_MMX2
1976
    sws_init_swScale_MMX2(c);
1977
    return swScale_MMX2;
1978
#elif COMPILE_TEMPLATE_AMD3DNOW
1979
    sws_init_swScale_3DNow(c);
1980
    return swScale_3DNow;
1981
#elif COMPILE_TEMPLATE_MMX
1982
    sws_init_swScale_MMX(c);
1983
    return swScale_MMX;
1984
#elif COMPILE_TEMPLATE_ALTIVEC
1985
    sws_init_swScale_altivec(c);
1986
    return swScale_altivec;
1987
#else
1988
    sws_init_swScale_C(c);
1989
    return swScale_C;
1990
#endif
1991
#endif //!CONFIG_RUNTIME_CPUDETECT
1992
}
1993

    
1994
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1995
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1996
{
1997
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1998
    /* Copy Y plane */
1999
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2000
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
2001
    else {
2002
        int i;
2003
        const uint8_t *srcPtr= src[0];
2004
        uint8_t *dstPtr= dst;
2005
        for (i=0; i<srcSliceH; i++) {
2006
            memcpy(dstPtr, srcPtr, c->srcW);
2007
            srcPtr+= srcStride[0];
2008
            dstPtr+= dstStride[0];
2009
        }
2010
    }
2011
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2012
    if (c->dstFormat == PIX_FMT_NV12)
2013
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2014
    else
2015
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2016

    
2017
    return srcSliceH;
2018
}
2019

    
2020
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2021
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2022
{
2023
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2024

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

    
2027
    return srcSliceH;
2028
}
2029

    
2030
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2031
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2032
{
2033
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2034

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

    
2037
    return srcSliceH;
2038
}
2039

    
2040
static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2041
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
2042
{
2043
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2044

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

    
2047
    return srcSliceH;
2048
}
2049

    
2050
static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2051
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
2052
{
2053
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2054

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

    
2057
    return srcSliceH;
2058
}
2059

    
2060
static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2061
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2062
{
2063
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2064
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2065
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2066

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

    
2069
    if (dstParam[3])
2070
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2071

    
2072
    return srcSliceH;
2073
}
2074

    
2075
static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2076
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2077
{
2078
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2079
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2080
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2081

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

    
2084
    return srcSliceH;
2085
}
2086

    
2087
static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2088
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2089
{
2090
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2091
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2092
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2093

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

    
2096
    if (dstParam[3])
2097
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2098

    
2099
    return srcSliceH;
2100
}
2101

    
2102
static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2103
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2104
{
2105
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2106
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2107
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2108

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

    
2111
    return srcSliceH;
2112
}
2113

    
2114
static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2115
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2116
{
2117
    const enum PixelFormat srcFormat= c->srcFormat;
2118
    const enum PixelFormat dstFormat= c->dstFormat;
2119
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2120
                 const uint8_t *palette)=NULL;
2121
    int i;
2122
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2123
    uint8_t *srcPtr= src[0];
2124

    
2125
    if (!usePal(srcFormat))
2126
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2127
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2128

    
2129
    switch(dstFormat) {
2130
    case PIX_FMT_RGB32  : conv = palette8topacked32; break;
2131
    case PIX_FMT_BGR32  : conv = palette8topacked32; break;
2132
    case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2133
    case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2134
    case PIX_FMT_RGB24  : conv = palette8topacked24; break;
2135
    case PIX_FMT_BGR24  : conv = palette8topacked24; break;
2136
    default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2137
                    sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2138
    }
2139

    
2140

    
2141
    for (i=0; i<srcSliceH; i++) {
2142
        conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2143
        srcPtr+= srcStride[0];
2144
        dstPtr+= dstStride[0];
2145
    }
2146

    
2147
    return srcSliceH;
2148
}
2149

    
2150
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2151
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2152
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2153
{
2154
    const enum PixelFormat srcFormat= c->srcFormat;
2155
    const enum PixelFormat dstFormat= c->dstFormat;
2156
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2157
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2158
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2159
    const int dstId= fmt_depth(dstFormat) >> 2;
2160
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2161

    
2162
    /* BGR -> BGR */
2163
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
2164
       || (isRGB(srcFormat) && isRGB(dstFormat))) {
2165
        switch(srcId | (dstId<<4)) {
2166
        case 0x34: conv= rgb16to15; break;
2167
        case 0x36: conv= rgb24to15; break;
2168
        case 0x38: conv= rgb32to15; break;
2169
        case 0x43: conv= rgb15to16; break;
2170
        case 0x46: conv= rgb24to16; break;
2171
        case 0x48: conv= rgb32to16; break;
2172
        case 0x63: conv= rgb15to24; break;
2173
        case 0x64: conv= rgb16to24; break;
2174
        case 0x68: conv= rgb32to24; break;
2175
        case 0x83: conv= rgb15to32; break;
2176
        case 0x84: conv= rgb16to32; break;
2177
        case 0x86: conv= rgb24to32; break;
2178
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2179
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2180
        }
2181
    } else if (  (isBGR(srcFormat) && isRGB(dstFormat))
2182
             || (isRGB(srcFormat) && isBGR(dstFormat))) {
2183
        switch(srcId | (dstId<<4)) {
2184
        case 0x33: conv= rgb15tobgr15; break;
2185
        case 0x34: conv= rgb16tobgr15; break;
2186
        case 0x36: conv= rgb24tobgr15; break;
2187
        case 0x38: conv= rgb32tobgr15; break;
2188
        case 0x43: conv= rgb15tobgr16; break;
2189
        case 0x44: conv= rgb16tobgr16; break;
2190
        case 0x46: conv= rgb24tobgr16; break;
2191
        case 0x48: conv= rgb32tobgr16; break;
2192
        case 0x63: conv= rgb15tobgr24; break;
2193
        case 0x64: conv= rgb16tobgr24; break;
2194
        case 0x66: conv= rgb24tobgr24; break;
2195
        case 0x68: conv= rgb32tobgr24; break;
2196
        case 0x83: conv= rgb15tobgr32; break;
2197
        case 0x84: conv= rgb16tobgr32; break;
2198
        case 0x86: conv= rgb24tobgr32; break;
2199
        case 0x88: conv= rgb32tobgr32; break;
2200
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2201
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2202
        }
2203
    } else {
2204
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2205
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2206
    }
2207

    
2208
    if(conv) {
2209
        uint8_t *srcPtr= src[0];
2210
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2211
            srcPtr += ALT32_CORR;
2212

    
2213
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2214
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2215
        else {
2216
            int i;
2217
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2218

    
2219
            for (i=0; i<srcSliceH; i++) {
2220
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
2221
                srcPtr+= srcStride[0];
2222
                dstPtr+= dstStride[0];
2223
            }
2224
        }
2225
    }
2226
    return srcSliceH;
2227
}
2228

    
2229
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2230
                              int srcSliceH, uint8_t* dst[], int dstStride[])
2231
{
2232

    
2233
    rgb24toyv12(
2234
        src[0],
2235
        dst[0]+ srcSliceY    *dstStride[0],
2236
        dst[1]+(srcSliceY>>1)*dstStride[1],
2237
        dst[2]+(srcSliceY>>1)*dstStride[2],
2238
        c->srcW, srcSliceH,
2239
        dstStride[0], dstStride[1], srcStride[0]);
2240
    if (dst[3])
2241
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2242
    return srcSliceH;
2243
}
2244

    
2245
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2246
                             int srcSliceH, uint8_t* dst[], int dstStride[])
2247
{
2248
    int i;
2249

    
2250
    /* copy Y */
2251
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2252
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2253
    else {
2254
        uint8_t *srcPtr= src[0];
2255
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2256

    
2257
        for (i=0; i<srcSliceH; i++) {
2258
            memcpy(dstPtr, srcPtr, c->srcW);
2259
            srcPtr+= srcStride[0];
2260
            dstPtr+= dstStride[0];
2261
        }
2262
    }
2263

    
2264
    if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
2265
        planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2266
                 srcSliceH >> 2, srcStride[1], dstStride[1]);
2267
        planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2268
                 srcSliceH >> 2, srcStride[2], dstStride[2]);
2269
    } else {
2270
        planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2271
                 srcSliceH >> 2, srcStride[1], dstStride[2]);
2272
        planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2273
                 srcSliceH >> 2, srcStride[2], dstStride[1]);
2274
    }
2275
    if (dst[3])
2276
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2277
    return srcSliceH;
2278
}
2279

    
2280
/* unscaled copy like stuff (assumes nearly identical formats) */
2281
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2282
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2283
{
2284
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2285
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2286
    else {
2287
        int i;
2288
        uint8_t *srcPtr= src[0];
2289
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2290
        int length=0;
2291

    
2292
        /* universal length finder */
2293
        while(length+c->srcW <= FFABS(dstStride[0])
2294
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2295
        assert(length!=0);
2296

    
2297
        for (i=0; i<srcSliceH; i++) {
2298
            memcpy(dstPtr, srcPtr, length);
2299
            srcPtr+= srcStride[0];
2300
            dstPtr+= dstStride[0];
2301
        }
2302
    }
2303
    return srcSliceH;
2304
}
2305

    
2306
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2307
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2308
{
2309
    int plane, i, j;
2310
    for (plane=0; plane<4; plane++) {
2311
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2312
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2313
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2314
        uint8_t *srcPtr= src[plane];
2315
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2316

    
2317
        if (!dst[plane]) continue;
2318
        // ignore palette for GRAY8
2319
        if (plane == 1 && !dst[2]) continue;
2320
        if (!src[plane] || (plane == 1 && !src[2])) {
2321
            if(is16BPS(c->dstFormat))
2322
                length*=2;
2323
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2324
        } else {
2325
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
2326
                if (!isBE(c->srcFormat)) srcPtr++;
2327
                for (i=0; i<height; i++) {
2328
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2329
                    srcPtr+= srcStride[plane];
2330
                    dstPtr+= dstStride[plane];
2331
                }
2332
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
2333
                for (i=0; i<height; i++) {
2334
                    for (j=0; j<length; j++) {
2335
                        dstPtr[ j<<1   ] = srcPtr[j];
2336
                        dstPtr[(j<<1)+1] = srcPtr[j];
2337
                    }
2338
                    srcPtr+= srcStride[plane];
2339
                    dstPtr+= dstStride[plane];
2340
                }
2341
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2342
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
2343

    
2344
                for (i=0; i<height; i++) {
2345
                    for (j=0; j<length; j++)
2346
                        ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2347
                    srcPtr+= srcStride[plane];
2348
                    dstPtr+= dstStride[plane];
2349
                }
2350
            } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2351
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2352
            else {
2353
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2354
                    length*=2;
2355
                for (i=0; i<height; i++) {
2356
                    memcpy(dstPtr, srcPtr, length);
2357
                    srcPtr+= srcStride[plane];
2358
                    dstPtr+= dstStride[plane];
2359
                }
2360
            }
2361
        }
2362
    }
2363
    return srcSliceH;
2364
}
2365

    
2366

    
2367
static void getSubSampleFactors(int *h, int *v, int format)
2368
{
2369
    switch(format) {
2370
    case PIX_FMT_UYVY422:
2371
    case PIX_FMT_YUYV422:
2372
        *h=1;
2373
        *v=0;
2374
        break;
2375
    case PIX_FMT_YUV420P:
2376
    case PIX_FMT_YUV420P16LE:
2377
    case PIX_FMT_YUV420P16BE:
2378
    case PIX_FMT_YUVA420P:
2379
    case PIX_FMT_GRAY16BE:
2380
    case PIX_FMT_GRAY16LE:
2381
    case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2382
    case PIX_FMT_NV12:
2383
    case PIX_FMT_NV21:
2384
        *h=1;
2385
        *v=1;
2386
        break;
2387
    case PIX_FMT_YUV440P:
2388
        *h=0;
2389
        *v=1;
2390
        break;
2391
    case PIX_FMT_YUV410P:
2392
        *h=2;
2393
        *v=2;
2394
        break;
2395
    case PIX_FMT_YUV444P:
2396
    case PIX_FMT_YUV444P16LE:
2397
    case PIX_FMT_YUV444P16BE:
2398
        *h=0;
2399
        *v=0;
2400
        break;
2401
    case PIX_FMT_YUV422P:
2402
    case PIX_FMT_YUV422P16LE:
2403
    case PIX_FMT_YUV422P16BE:
2404
        *h=1;
2405
        *v=0;
2406
        break;
2407
    case PIX_FMT_YUV411P:
2408
        *h=2;
2409
        *v=0;
2410
        break;
2411
    default:
2412
        *h=0;
2413
        *v=0;
2414
        break;
2415
    }
2416
}
2417

    
2418
static uint16_t roundToInt16(int64_t f)
2419
{
2420
    int r= (f + (1<<15))>>16;
2421
         if (r<-0x7FFF) return 0x8000;
2422
    else if (r> 0x7FFF) return 0x7FFF;
2423
    else                return r;
2424
}
2425

    
2426
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
2427
{
2428
    int64_t crv =  inv_table[0];
2429
    int64_t cbu =  inv_table[1];
2430
    int64_t cgu = -inv_table[2];
2431
    int64_t cgv = -inv_table[3];
2432
    int64_t cy  = 1<<16;
2433
    int64_t oy  = 0;
2434

    
2435
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2436
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2437

    
2438
    c->brightness= brightness;
2439
    c->contrast  = contrast;
2440
    c->saturation= saturation;
2441
    c->srcRange  = srcRange;
2442
    c->dstRange  = dstRange;
2443
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2444

    
2445
    c->uOffset=   0x0400040004000400LL;
2446
    c->vOffset=   0x0400040004000400LL;
2447

    
2448
    if (!srcRange) {
2449
        cy= (cy*255) / 219;
2450
        oy= 16<<16;
2451
    } else {
2452
        crv= (crv*224) / 255;
2453
        cbu= (cbu*224) / 255;
2454
        cgu= (cgu*224) / 255;
2455
        cgv= (cgv*224) / 255;
2456
    }
2457

    
2458
    cy = (cy *contrast             )>>16;
2459
    crv= (crv*contrast * saturation)>>32;
2460
    cbu= (cbu*contrast * saturation)>>32;
2461
    cgu= (cgu*contrast * saturation)>>32;
2462
    cgv= (cgv*contrast * saturation)>>32;
2463

    
2464
    oy -= 256*brightness;
2465

    
2466
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2467
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2468
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2469
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2470
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2471
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2472

    
2473
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2474
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2475
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2476
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2477
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2478
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2479

    
2480
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2481
    //FIXME factorize
2482

    
2483
#ifdef COMPILE_ALTIVEC
2484
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2485
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2486
#endif
2487
    return 0;
2488
}
2489

    
2490
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
2491
{
2492
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2493

    
2494
    *inv_table = c->srcColorspaceTable;
2495
    *table     = c->dstColorspaceTable;
2496
    *srcRange  = c->srcRange;
2497
    *dstRange  = c->dstRange;
2498
    *brightness= c->brightness;
2499
    *contrast  = c->contrast;
2500
    *saturation= c->saturation;
2501

    
2502
    return 0;
2503
}
2504

    
2505
static int handle_jpeg(enum PixelFormat *format)
2506
{
2507
    switch (*format) {
2508
    case PIX_FMT_YUVJ420P:
2509
        *format = PIX_FMT_YUV420P;
2510
        return 1;
2511
    case PIX_FMT_YUVJ422P:
2512
        *format = PIX_FMT_YUV422P;
2513
        return 1;
2514
    case PIX_FMT_YUVJ444P:
2515
        *format = PIX_FMT_YUV444P;
2516
        return 1;
2517
    case PIX_FMT_YUVJ440P:
2518
        *format = PIX_FMT_YUV440P;
2519
        return 1;
2520
    default:
2521
        return 0;
2522
    }
2523
}
2524

    
2525
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2526
                           SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2527
{
2528

    
2529
    SwsContext *c;
2530
    int i;
2531
    int usesVFilter, usesHFilter;
2532
    int unscaled, needsDither;
2533
    int srcRange, dstRange;
2534
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2535
#if ARCH_X86
2536
    if (flags & SWS_CPU_CAPS_MMX)
2537
        __asm__ volatile("emms\n\t"::: "memory");
2538
#endif
2539

    
2540
#if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2541
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2542
#if   COMPILE_TEMPLATE_MMX2
2543
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2544
#elif COMPILE_TEMPLATE_AMD3DNOW
2545
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2546
#elif COMPILE_TEMPLATE_MMX
2547
    flags |= SWS_CPU_CAPS_MMX;
2548
#elif COMPILE_TEMPLATE_ALTIVEC
2549
    flags |= SWS_CPU_CAPS_ALTIVEC;
2550
#elif ARCH_BFIN
2551
    flags |= SWS_CPU_CAPS_BFIN;
2552
#endif
2553
#endif /* CONFIG_RUNTIME_CPUDETECT */
2554
    if (clip_table[512] != 255) globalInit();
2555
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2556

    
2557
    unscaled = (srcW == dstW && srcH == dstH);
2558
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2559
        && (fmt_depth(dstFormat))<24
2560
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2561

    
2562
    srcRange = handle_jpeg(&srcFormat);
2563
    dstRange = handle_jpeg(&dstFormat);
2564

    
2565
    if (!isSupportedIn(srcFormat)) {
2566
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2567
        return NULL;
2568
    }
2569
    if (!isSupportedOut(dstFormat)) {
2570
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2571
        return NULL;
2572
    }
2573

    
2574
    i= flags & ( SWS_POINT
2575
                |SWS_AREA
2576
                |SWS_BILINEAR
2577
                |SWS_FAST_BILINEAR
2578
                |SWS_BICUBIC
2579
                |SWS_X
2580
                |SWS_GAUSS
2581
                |SWS_LANCZOS
2582
                |SWS_SINC
2583
                |SWS_SPLINE
2584
                |SWS_BICUBLIN);
2585
    if(!i || (i & (i-1))) {
2586
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2587
        return NULL;
2588
    }
2589

    
2590
    /* sanity check */
2591
    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
2592
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2593
               srcW, srcH, dstW, dstH);
2594
        return NULL;
2595
    }
2596
    if(srcW > VOFW || dstW > VOFW) {
2597
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2598
        return NULL;
2599
    }
2600

    
2601
    if (!dstFilter) dstFilter= &dummyFilter;
2602
    if (!srcFilter) srcFilter= &dummyFilter;
2603

    
2604
    FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
2605

    
2606
    c->av_class = &sws_context_class;
2607
    c->srcW= srcW;
2608
    c->srcH= srcH;
2609
    c->dstW= dstW;
2610
    c->dstH= dstH;
2611
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2612
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2613
    c->flags= flags;
2614
    c->dstFormat= dstFormat;
2615
    c->srcFormat= srcFormat;
2616
    c->vRounder= 4* 0x0001000100010001ULL;
2617

    
2618
    usesHFilter= usesVFilter= 0;
2619
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2620
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2621
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2622
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2623
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2624
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2625
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2626
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2627

    
2628
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2629
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2630

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

    
2634
    // drop some chroma lines if the user wants it
2635
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2636
    c->chrSrcVSubSample+= c->vChrDrop;
2637

    
2638
    // drop every other pixel for chroma calculation unless user wants full chroma
2639
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2640
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2641
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2642
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2643
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2644
        c->chrSrcHSubSample=1;
2645

    
2646
    if (param) {
2647
        c->param[0] = param[0];
2648
        c->param[1] = param[1];
2649
    } else {
2650
        c->param[0] =
2651
        c->param[1] = SWS_PARAM_DEFAULT;
2652
    }
2653

    
2654
    // Note the -((-x)>>y) is so that we always round toward +inf.
2655
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2656
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2657
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2658
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2659

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

    
2662
    /* unscaled special cases */
2663
    if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat))) {
2664
        /* yv12_to_nv12 */
2665
        if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
2666
            c->swScale= PlanarToNV12Wrapper;
2667
        }
2668
        /* yuv2bgr */
2669
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2670
            && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
2671
            c->swScale= ff_yuv2rgb_get_func_ptr(c);
2672
        }
2673

    
2674
        if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2675
            c->swScale= yvu9toyv12Wrapper;
2676
        }
2677

    
2678
        /* bgr24toYV12 */
2679
        if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2680
            c->swScale= bgr24toyv12Wrapper;
2681

    
2682
        /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2683
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2684
           && (isBGR(dstFormat) || isRGB(dstFormat))
2685
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2686
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2687
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2688
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2689
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2690
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2691
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2692
           && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2693
                                             && dstFormat != PIX_FMT_RGB32_1
2694
                                             && dstFormat != PIX_FMT_BGR32_1
2695
           && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
2696
           && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
2697
           && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2698
             c->swScale= rgb2rgbWrapper;
2699

    
2700
        if ((usePal(srcFormat) && (
2701
                 dstFormat == PIX_FMT_RGB32   ||
2702
                 dstFormat == PIX_FMT_RGB32_1 ||
2703
                 dstFormat == PIX_FMT_RGB24   ||
2704
                 dstFormat == PIX_FMT_BGR32   ||
2705
                 dstFormat == PIX_FMT_BGR32_1 ||
2706
                 dstFormat == PIX_FMT_BGR24)))
2707
             c->swScale= pal2rgbWrapper;
2708

    
2709
        if (srcFormat == PIX_FMT_YUV422P) {
2710
            if (dstFormat == PIX_FMT_YUYV422)
2711
                c->swScale= YUV422PToYuy2Wrapper;
2712
            else if (dstFormat == PIX_FMT_UYVY422)
2713
                c->swScale= YUV422PToUyvyWrapper;
2714
        }
2715

    
2716
        /* LQ converters if -sws 0 or -sws 4*/
2717
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2718
            /* yv12_to_yuy2 */
2719
            if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2720
                if (dstFormat == PIX_FMT_YUYV422)
2721
                    c->swScale= PlanarToYuy2Wrapper;
2722
                else if (dstFormat == PIX_FMT_UYVY422)
2723
                    c->swScale= PlanarToUyvyWrapper;
2724
            }
2725
        }
2726
        if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2727
            c->swScale= YUYV2YUV420Wrapper;
2728
        if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2729
            c->swScale= UYVY2YUV420Wrapper;
2730
        if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2731
            c->swScale= YUYV2YUV422Wrapper;
2732
        if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2733
            c->swScale= UYVY2YUV422Wrapper;
2734

    
2735
#ifdef COMPILE_ALTIVEC
2736
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2737
            !(c->flags & SWS_BITEXACT) &&
2738
            srcFormat == PIX_FMT_YUV420P) {
2739
          // unscaled YV12 -> packed YUV, we want speed
2740
          if (dstFormat == PIX_FMT_YUYV422)
2741
              c->swScale= yv12toyuy2_unscaled_altivec;
2742
          else if (dstFormat == PIX_FMT_UYVY422)
2743
              c->swScale= yv12touyvy_unscaled_altivec;
2744
        }
2745
#endif
2746

    
2747
        /* simple copy */
2748
        if (  srcFormat == dstFormat
2749
            || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2750
            || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2751
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2752
            || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2753
            || (isGray(dstFormat) && isGray(srcFormat))
2754
            || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2755
                && c->chrDstHSubSample == c->chrSrcHSubSample
2756
                && c->chrDstVSubSample == c->chrSrcVSubSample
2757
                && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2758
                && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2759
        {
2760
            if (isPacked(c->srcFormat))
2761
                c->swScale= packedCopy;
2762
            else /* Planar YUV or gray */
2763
                c->swScale= planarCopy;
2764
        }
2765
#if ARCH_BFIN
2766
        if (flags & SWS_CPU_CAPS_BFIN)
2767
            ff_bfin_get_unscaled_swscale (c);
2768
#endif
2769

    
2770
        if (c->swScale) {
2771
            if (flags&SWS_PRINT_INFO)
2772
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2773
                       sws_format_name(srcFormat), sws_format_name(dstFormat));
2774
            return c;
2775
        }
2776
    }
2777

    
2778
    if (flags & SWS_CPU_CAPS_MMX2) {
2779
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2780
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
2781
            if (flags&SWS_PRINT_INFO)
2782
                av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2783
        }
2784
        if (usesHFilter) c->canMMX2BeUsed=0;
2785
    }
2786
    else
2787
        c->canMMX2BeUsed=0;
2788

    
2789
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2790
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2791

    
2792
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2793
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
2794
    // n-2 is the last chrominance sample available
2795
    // this is not perfect, but no one should notice the difference, the more correct variant
2796
    // would be like the vertical one, but that would require some special code for the
2797
    // first and last pixel
2798
    if (flags&SWS_FAST_BILINEAR) {
2799
        if (c->canMMX2BeUsed) {
2800
            c->lumXInc+= 20;
2801
            c->chrXInc+= 20;
2802
        }
2803
        //we don't use the x86 asm scaler if MMX is available
2804
        else if (flags & SWS_CPU_CAPS_MMX) {
2805
            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2806
            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2807
        }
2808
    }
2809

    
2810
    /* precalculate horizontal scaler filter coefficients */
2811
    {
2812
        const int filterAlign=
2813
            (flags & SWS_CPU_CAPS_MMX) ? 4 :
2814
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2815
            1;
2816

    
2817
        if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2818
                       srcW      ,       dstW, filterAlign, 1<<14,
2819
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2820
                       srcFilter->lumH, dstFilter->lumH, c->param) < 0)
2821
            goto fail;
2822
        if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2823
                       c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2824
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2825
                       srcFilter->chrH, dstFilter->chrH, c->param) < 0)
2826
            goto fail;
2827

    
2828
#if defined(COMPILE_MMX2)
2829
// can't downscale !!!
2830
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
2831
            c->lumMmx2FilterCodeSize = initMMX2HScaler(      dstW, c->lumXInc, NULL, NULL, NULL, 8);
2832
            c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2833

    
2834
#ifdef MAP_ANONYMOUS
2835
            c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2836
            c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2837
#elif HAVE_VIRTUALALLOC
2838
            c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2839
            c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2840
#else
2841
            c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2842
            c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2843
#endif
2844

    
2845
            FF_ALLOCZ_OR_GOTO(c, c->lumMmx2Filter   , (dstW        /8+8)*sizeof(int16_t), fail);
2846
            FF_ALLOCZ_OR_GOTO(c, c->chrMmx2Filter   , (c->chrDstW  /4+8)*sizeof(int16_t), fail);
2847
            FF_ALLOCZ_OR_GOTO(c, c->lumMmx2FilterPos, (dstW      /2/8+8)*sizeof(int32_t), fail);
2848
            FF_ALLOCZ_OR_GOTO(c, c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
2849

    
2850
            initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2851
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2852

    
2853
#ifdef MAP_ANONYMOUS
2854
            mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2855
            mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2856
#endif
2857
        }
2858
#endif /* defined(COMPILE_MMX2) */
2859
    } // initialize horizontal stuff
2860

    
2861

    
2862

    
2863
    /* precalculate vertical scaler filter coefficients */
2864
    {
2865
        const int filterAlign=
2866
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2867
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2868
            1;
2869

    
2870
        if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2871
                   srcH      ,        dstH, filterAlign, (1<<12),
2872
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2873
                   srcFilter->lumV, dstFilter->lumV, c->param) < 0)
2874
            goto fail;
2875
        if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2876
                   c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2877
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2878
                   srcFilter->chrV, dstFilter->chrV, c->param) < 0)
2879
            goto fail;
2880

    
2881
#ifdef COMPILE_ALTIVEC
2882
        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
2883
        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
2884

    
2885
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2886
            int j;
2887
            short *p = (short *)&c->vYCoeffsBank[i];
2888
            for (j=0;j<8;j++)
2889
                p[j] = c->vLumFilter[i];
2890
        }
2891

    
2892
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2893
            int j;
2894
            short *p = (short *)&c->vCCoeffsBank[i];
2895
            for (j=0;j<8;j++)
2896
                p[j] = c->vChrFilter[i];
2897
        }
2898
#endif
2899
    }
2900

    
2901
    // calculate buffer sizes so that they won't run out while handling these damn slices
2902
    c->vLumBufSize= c->vLumFilterSize;
2903
    c->vChrBufSize= c->vChrFilterSize;
2904
    for (i=0; i<dstH; i++) {
2905
        int chrI= i*c->chrDstH / dstH;
2906
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2907
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2908

    
2909
        nextSlice>>= c->chrSrcVSubSample;
2910
        nextSlice<<= c->chrSrcVSubSample;
2911
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2912
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2913
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2914
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2915
    }
2916

    
2917
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2918
    // allocate several megabytes to handle all possible cases)
2919
    FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2920
    FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
2921
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2922
        FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2923
    //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)
2924
    /* align at 16 bytes for AltiVec */
2925
    for (i=0; i<c->vLumBufSize; i++) {
2926
        FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
2927
        c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2928
    }
2929
    for (i=0; i<c->vChrBufSize; i++) {
2930
        FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
2931
        c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2932
    }
2933
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2934
        for (i=0; i<c->vLumBufSize; i++) {
2935
            FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
2936
            c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2937
        }
2938

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

    
2942
    assert(2*VOFW == VOF);
2943

    
2944
    assert(c->chrDstH <= dstH);
2945

    
2946
    if (flags&SWS_PRINT_INFO) {
2947
#ifdef DITHER1XBPP
2948
        const char *dither= " dithered";
2949
#else
2950
        const char *dither= "";
2951
#endif
2952
        if (flags&SWS_FAST_BILINEAR)
2953
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2954
        else if (flags&SWS_BILINEAR)
2955
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2956
        else if (flags&SWS_BICUBIC)
2957
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2958
        else if (flags&SWS_X)
2959
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2960
        else if (flags&SWS_POINT)
2961
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2962
        else if (flags&SWS_AREA)
2963
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2964
        else if (flags&SWS_BICUBLIN)
2965
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2966
        else if (flags&SWS_GAUSS)
2967
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2968
        else if (flags&SWS_SINC)
2969
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2970
        else if (flags&SWS_LANCZOS)
2971
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2972
        else if (flags&SWS_SPLINE)
2973
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2974
        else
2975
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2976

    
2977
        if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2978
            av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2979
                   sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2980
        else
2981
            av_log(c, AV_LOG_INFO, "from %s to %s ",
2982
                   sws_format_name(srcFormat), sws_format_name(dstFormat));
2983

    
2984
        if (flags & SWS_CPU_CAPS_MMX2)
2985
            av_log(c, AV_LOG_INFO, "using MMX2\n");
2986
        else if (flags & SWS_CPU_CAPS_3DNOW)
2987
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2988
        else if (flags & SWS_CPU_CAPS_MMX)
2989
            av_log(c, AV_LOG_INFO, "using MMX\n");
2990
        else if (flags & SWS_CPU_CAPS_ALTIVEC)
2991
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
2992
        else
2993
            av_log(c, AV_LOG_INFO, "using C\n");
2994
    }
2995

    
2996
    if (flags & SWS_PRINT_INFO) {
2997
        if (flags & SWS_CPU_CAPS_MMX) {
2998
            if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2999
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3000
            else {
3001
                if (c->hLumFilterSize==4)
3002
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3003
                else if (c->hLumFilterSize==8)
3004
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3005
                else
3006
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3007

    
3008
                if (c->hChrFilterSize==4)
3009
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3010
                else if (c->hChrFilterSize==8)
3011
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3012
                else
3013
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3014
            }
3015
        } else {
3016
#if ARCH_X86
3017
            av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3018
#else
3019
            if (flags & SWS_FAST_BILINEAR)
3020
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3021
            else
3022
                av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3023
#endif
3024
        }
3025
        if (isPlanarYUV(dstFormat)) {
3026
            if (c->vLumFilterSize==1)
3027
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3028
            else
3029
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3030
        } else {
3031
            if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3032
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3033
                       "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3034
            else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3035
                av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3036
            else
3037
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3038
        }
3039

    
3040
        if (dstFormat==PIX_FMT_BGR24)
3041
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3042
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3043
        else if (dstFormat==PIX_FMT_RGB32)
3044
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3045
        else if (dstFormat==PIX_FMT_BGR565)
3046
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3047
        else if (dstFormat==PIX_FMT_BGR555)
3048
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3049

    
3050
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3051
    }
3052
    if (flags & SWS_PRINT_INFO) {
3053
        av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3054
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3055
        av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3056
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3057
    }
3058

    
3059
    c->swScale= getSwsFunc(c);
3060
    return c;
3061

    
3062
fail:
3063
    sws_freeContext(c);
3064
    return NULL;
3065
}
3066

    
3067
static void reset_ptr(uint8_t* src[], int format)
3068
{
3069
    if(!isALPHA(format))
3070
        src[3]=NULL;
3071
    if(!isPlanarYUV(format)) {
3072
        src[3]=src[2]=NULL;
3073
        if(   format != PIX_FMT_PAL8
3074
           && format != PIX_FMT_RGB8
3075
           && format != PIX_FMT_BGR8
3076
           && format != PIX_FMT_RGB4_BYTE
3077
           && format != PIX_FMT_BGR4_BYTE
3078
          )
3079
            src[1]= NULL;
3080
    }
3081
}
3082

    
3083
/**
3084
 * swscale wrapper, so we don't need to export the SwsContext.
3085
 * Assumes planar YUV to be in YUV order instead of YVU.
3086
 */
3087
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3088
              int srcSliceH, uint8_t* dst[], int dstStride[])
3089
{
3090
    int i;
3091
    uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3092
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3093

    
3094
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3095
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3096
        return 0;
3097
    }
3098
    if (c->sliceDir == 0) {
3099
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3100
    }
3101

    
3102
    if (usePal(c->srcFormat)) {
3103
        for (i=0; i<256; i++) {
3104
            int p, r, g, b,y,u,v;
3105
            if(c->srcFormat == PIX_FMT_PAL8) {
3106
                p=((uint32_t*)(src[1]))[i];
3107
                r= (p>>16)&0xFF;
3108
                g= (p>> 8)&0xFF;
3109
                b=  p     &0xFF;
3110
            } else if(c->srcFormat == PIX_FMT_RGB8) {
3111
                r= (i>>5    )*36;
3112
                g= ((i>>2)&7)*36;
3113
                b= (i&3     )*85;
3114
            } else if(c->srcFormat == PIX_FMT_BGR8) {
3115
                b= (i>>6    )*85;
3116
                g= ((i>>3)&7)*36;
3117
                r= (i&7     )*36;
3118
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
3119
                r= (i>>3    )*255;
3120
                g= ((i>>1)&3)*85;
3121
                b= (i&1     )*255;
3122
            } else {
3123
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3124
                b= (i>>3    )*255;
3125
                g= ((i>>1)&3)*85;
3126
                r= (i&1     )*255;
3127
            }
3128
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3129
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3130
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3131
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
3132

    
3133

    
3134
            switch(c->dstFormat) {
3135
            case PIX_FMT_BGR32:
3136
#if !HAVE_BIGENDIAN
3137
            case PIX_FMT_RGB24:
3138
#endif
3139
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
3140
                break;
3141
            case PIX_FMT_BGR32_1:
3142
#if HAVE_BIGENDIAN
3143
            case PIX_FMT_BGR24:
3144
#endif
3145
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3146
                break;
3147
            case PIX_FMT_RGB32_1:
3148
#if HAVE_BIGENDIAN
3149
            case PIX_FMT_RGB24:
3150
#endif
3151
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3152
                break;
3153
            case PIX_FMT_RGB32:
3154
#if !HAVE_BIGENDIAN
3155
            case PIX_FMT_BGR24:
3156
#endif
3157
            default:
3158
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
3159
            }
3160
        }
3161
    }
3162

    
3163
    // copy strides, so they can safely be modified
3164
    if (c->sliceDir == 1) {
3165
        // slices go from top to bottom
3166
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3167
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3168

    
3169
        reset_ptr(src2, c->srcFormat);
3170
        reset_ptr(dst2, c->dstFormat);
3171

    
3172
        /* reset slice direction at end of frame */
3173
        if (srcSliceY + srcSliceH == c->srcH)
3174
            c->sliceDir = 0;
3175

    
3176
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3177
    } else {
3178
        // slices go from bottom to top => we flip the image internally
3179
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3180
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3181

    
3182
        src2[0] += (srcSliceH-1)*srcStride[0];
3183
        if (!usePal(c->srcFormat))
3184
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3185
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3186
        src2[3] += (srcSliceH-1)*srcStride[3];
3187
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
3188
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3189
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3190
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
3191

    
3192
        reset_ptr(src2, c->srcFormat);
3193
        reset_ptr(dst2, c->dstFormat);
3194

    
3195
        /* reset slice direction at end of frame */
3196
        if (!srcSliceY)
3197
            c->sliceDir = 0;
3198

    
3199
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3200
    }
3201
}
3202

    
3203
#if LIBSWSCALE_VERSION_MAJOR < 1
3204
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3205
                      int srcSliceH, uint8_t* dst[], int dstStride[])
3206
{
3207
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3208
}
3209
#endif
3210

    
3211
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3212
                                float lumaSharpen, float chromaSharpen,
3213
                                float chromaHShift, float chromaVShift,
3214
                                int verbose)
3215
{
3216
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3217
    if (!filter)
3218
        return NULL;
3219

    
3220
    if (lumaGBlur!=0.0) {
3221
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3222
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3223
    } else {
3224
        filter->lumH= sws_getIdentityVec();
3225
        filter->lumV= sws_getIdentityVec();
3226
    }
3227

    
3228
    if (chromaGBlur!=0.0) {
3229
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3230
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3231
    } else {
3232
        filter->chrH= sws_getIdentityVec();
3233
        filter->chrV= sws_getIdentityVec();
3234
    }
3235

    
3236
    if (chromaSharpen!=0.0) {
3237
        SwsVector *id= sws_getIdentityVec();
3238
        sws_scaleVec(filter->chrH, -chromaSharpen);
3239
        sws_scaleVec(filter->chrV, -chromaSharpen);
3240
        sws_addVec(filter->chrH, id);
3241
        sws_addVec(filter->chrV, id);
3242
        sws_freeVec(id);
3243
    }
3244

    
3245
    if (lumaSharpen!=0.0) {
3246
        SwsVector *id= sws_getIdentityVec();
3247
        sws_scaleVec(filter->lumH, -lumaSharpen);
3248
        sws_scaleVec(filter->lumV, -lumaSharpen);
3249
        sws_addVec(filter->lumH, id);
3250
        sws_addVec(filter->lumV, id);
3251
        sws_freeVec(id);
3252
    }
3253

    
3254
    if (chromaHShift != 0.0)
3255
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3256

    
3257
    if (chromaVShift != 0.0)
3258
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3259

    
3260
    sws_normalizeVec(filter->chrH, 1.0);
3261
    sws_normalizeVec(filter->chrV, 1.0);
3262
    sws_normalizeVec(filter->lumH, 1.0);
3263
    sws_normalizeVec(filter->lumV, 1.0);
3264

    
3265
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3266
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3267

    
3268
    return filter;
3269
}
3270

    
3271
SwsVector *sws_allocVec(int length)
3272
{
3273
    SwsVector *vec = av_malloc(sizeof(SwsVector));
3274
    if (!vec)
3275
        return NULL;
3276
    vec->length = length;
3277
    vec->coeff  = av_malloc(sizeof(double) * length);
3278
    if (!vec->coeff)
3279
        av_freep(&vec);
3280
    return vec;
3281
}
3282

    
3283
SwsVector *sws_getGaussianVec(double variance, double quality)
3284
{
3285
    const int length= (int)(variance*quality + 0.5) | 1;
3286
    int i;
3287
    double middle= (length-1)*0.5;
3288
    SwsVector *vec= sws_allocVec(length);
3289

    
3290
    if (!vec)
3291
        return NULL;
3292

    
3293
    for (i=0; i<length; i++) {
3294
        double dist= i-middle;
3295
        vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3296
    }
3297

    
3298
    sws_normalizeVec(vec, 1.0);
3299

    
3300
    return vec;
3301
}
3302

    
3303
SwsVector *sws_getConstVec(double c, int length)
3304
{
3305
    int i;
3306
    SwsVector *vec= sws_allocVec(length);
3307

    
3308
    if (!vec)
3309
        return NULL;
3310

    
3311
    for (i=0; i<length; i++)
3312
        vec->coeff[i]= c;
3313

    
3314
    return vec;
3315
}
3316

    
3317

    
3318
SwsVector *sws_getIdentityVec(void)
3319
{
3320
    return sws_getConstVec(1.0, 1);
3321
}
3322

    
3323
double sws_dcVec(SwsVector *a)
3324
{
3325
    int i;
3326
    double sum=0;
3327

    
3328
    for (i=0; i<a->length; i++)
3329
        sum+= a->coeff[i];
3330

    
3331
    return sum;
3332
}
3333

    
3334
void sws_scaleVec(SwsVector *a, double scalar)
3335
{
3336
    int i;
3337

    
3338
    for (i=0; i<a->length; i++)
3339
        a->coeff[i]*= scalar;
3340
}
3341

    
3342
void sws_normalizeVec(SwsVector *a, double height)
3343
{
3344
    sws_scaleVec(a, height/sws_dcVec(a));
3345
}
3346

    
3347
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
3348
{
3349
    int length= a->length + b->length - 1;
3350
    int i, j;
3351
    SwsVector *vec= sws_getConstVec(0.0, length);
3352

    
3353
    if (!vec)
3354
        return NULL;
3355

    
3356
    for (i=0; i<a->length; i++) {
3357
        for (j=0; j<b->length; j++) {
3358
            vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
3359
        }
3360
    }
3361

    
3362
    return vec;
3363
}
3364

    
3365
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
3366
{
3367
    int length= FFMAX(a->length, b->length);
3368
    int i;
3369
    SwsVector *vec= sws_getConstVec(0.0, length);
3370

    
3371
    if (!vec)
3372
        return NULL;
3373

    
3374
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3375
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3376

    
3377
    return vec;
3378
}
3379

    
3380
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
3381
{
3382
    int length= FFMAX(a->length, b->length);
3383
    int i;
3384
    SwsVector *vec= sws_getConstVec(0.0, length);
3385

    
3386
    if (!vec)
3387
        return NULL;
3388

    
3389
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3390
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3391

    
3392
    return vec;
3393
}
3394

    
3395
/* shift left / or right if "shift" is negative */
3396
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
3397
{
3398
    int length= a->length + FFABS(shift)*2;
3399
    int i;
3400
    SwsVector *vec= sws_getConstVec(0.0, length);
3401

    
3402
    if (!vec)
3403
        return NULL;
3404

    
3405
    for (i=0; i<a->length; i++) {
3406
        vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3407
    }
3408

    
3409
    return vec;
3410
}
3411

    
3412
void sws_shiftVec(SwsVector *a, int shift)
3413
{
3414
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3415
    av_free(a->coeff);
3416
    a->coeff= shifted->coeff;
3417
    a->length= shifted->length;
3418
    av_free(shifted);
3419
}
3420

    
3421
void sws_addVec(SwsVector *a, SwsVector *b)
3422
{
3423
    SwsVector *sum= sws_sumVec(a, b);
3424
    av_free(a->coeff);
3425
    a->coeff= sum->coeff;
3426
    a->length= sum->length;
3427
    av_free(sum);
3428
}
3429

    
3430
void sws_subVec(SwsVector *a, SwsVector *b)
3431
{
3432
    SwsVector *diff= sws_diffVec(a, b);
3433
    av_free(a->coeff);
3434
    a->coeff= diff->coeff;
3435
    a->length= diff->length;
3436
    av_free(diff);
3437
}
3438

    
3439
void sws_convVec(SwsVector *a, SwsVector *b)
3440
{
3441
    SwsVector *conv= sws_getConvVec(a, b);
3442
    av_free(a->coeff);
3443
    a->coeff= conv->coeff;
3444
    a->length= conv->length;
3445
    av_free(conv);
3446
}
3447

    
3448
SwsVector *sws_cloneVec(SwsVector *a)
3449
{
3450
    int i;
3451
    SwsVector *vec= sws_allocVec(a->length);
3452

    
3453
    if (!vec)
3454
        return NULL;
3455

    
3456
    for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
3457

    
3458
    return vec;
3459
}
3460

    
3461
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
3462
{
3463
    int i;
3464
    double max=0;
3465
    double min=0;
3466
    double range;
3467

    
3468
    for (i=0; i<a->length; i++)
3469
        if (a->coeff[i]>max) max= a->coeff[i];
3470

    
3471
    for (i=0; i<a->length; i++)
3472
        if (a->coeff[i]<min) min= a->coeff[i];
3473

    
3474
    range= max - min;
3475

    
3476
    for (i=0; i<a->length; i++) {
3477
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3478
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3479
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
3480
        av_log(log_ctx, log_level, "|\n");
3481
    }
3482
}
3483

    
3484
#if LIBSWSCALE_VERSION_MAJOR < 1
3485
void sws_printVec(SwsVector *a)
3486
{
3487
    sws_printVec2(a, NULL, AV_LOG_DEBUG);
3488
}
3489
#endif
3490

    
3491
void sws_freeVec(SwsVector *a)
3492
{
3493
    if (!a) return;
3494
    av_freep(&a->coeff);
3495
    a->length=0;
3496
    av_free(a);
3497
}
3498

    
3499
void sws_freeFilter(SwsFilter *filter)
3500
{
3501
    if (!filter) return;
3502

    
3503
    if (filter->lumH) sws_freeVec(filter->lumH);
3504
    if (filter->lumV) sws_freeVec(filter->lumV);
3505
    if (filter->chrH) sws_freeVec(filter->chrH);
3506
    if (filter->chrV) sws_freeVec(filter->chrV);
3507
    av_free(filter);
3508
}
3509

    
3510

    
3511
void sws_freeContext(SwsContext *c)
3512
{
3513
    int i;
3514
    if (!c) return;
3515

    
3516
    if (c->lumPixBuf) {
3517
        for (i=0; i<c->vLumBufSize; i++)
3518
            av_freep(&c->lumPixBuf[i]);
3519
        av_freep(&c->lumPixBuf);
3520
    }
3521

    
3522
    if (c->chrPixBuf) {
3523
        for (i=0; i<c->vChrBufSize; i++)
3524
            av_freep(&c->chrPixBuf[i]);
3525
        av_freep(&c->chrPixBuf);
3526
    }
3527

    
3528
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
3529
        for (i=0; i<c->vLumBufSize; i++)
3530
            av_freep(&c->alpPixBuf[i]);
3531
        av_freep(&c->alpPixBuf);
3532
    }
3533

    
3534
    av_freep(&c->vLumFilter);
3535
    av_freep(&c->vChrFilter);
3536
    av_freep(&c->hLumFilter);
3537
    av_freep(&c->hChrFilter);
3538
#ifdef COMPILE_ALTIVEC
3539
    av_freep(&c->vYCoeffsBank);
3540
    av_freep(&c->vCCoeffsBank);
3541
#endif
3542

    
3543
    av_freep(&c->vLumFilterPos);
3544
    av_freep(&c->vChrFilterPos);
3545
    av_freep(&c->hLumFilterPos);
3546
    av_freep(&c->hChrFilterPos);
3547

    
3548
#if ARCH_X86 && CONFIG_GPL
3549
#ifdef MAP_ANONYMOUS
3550
    if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3551
    if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3552
#elif HAVE_VIRTUALALLOC
3553
    if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3554
    if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3555
#else
3556
    av_free(c->lumMmx2FilterCode);
3557
    av_free(c->chrMmx2FilterCode);
3558
#endif
3559
    c->lumMmx2FilterCode=NULL;
3560
    c->chrMmx2FilterCode=NULL;
3561
#endif /* ARCH_X86 && CONFIG_GPL */
3562

    
3563
    av_freep(&c->lumMmx2Filter);
3564
    av_freep(&c->chrMmx2Filter);
3565
    av_freep(&c->lumMmx2FilterPos);
3566
    av_freep(&c->chrMmx2FilterPos);
3567
    av_freep(&c->yuvTable);
3568

    
3569
    av_free(c);
3570
}
3571

    
3572
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3573
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3574
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3575
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3576
{
3577
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3578

    
3579
    if (!param)
3580
        param = default_param;
3581

    
3582
    if (context) {
3583
        if (context->srcW != srcW || context->srcH != srcH ||
3584
            context->srcFormat != srcFormat ||
3585
            context->dstW != dstW || context->dstH != dstH ||
3586
            context->dstFormat != dstFormat || context->flags != flags ||
3587
            context->param[0] != param[0] || context->param[1] != param[1])
3588
        {
3589
            sws_freeContext(context);
3590
            context = NULL;
3591
        }
3592
    }
3593
    if (!context) {
3594
        return sws_getContext(srcW, srcH, srcFormat,
3595
                              dstW, dstH, dstFormat, flags,
3596
                              srcFilter, dstFilter, param);
3597
    }
3598
    return context;
3599
}
3600