Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ cb82a073

History | View | Annotate | Download (107 KB)

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

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

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

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

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

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

    
77
unsigned swscale_version(void)
78
{
79
    return LIBSWSCALE_VERSION_INT;
80
}
81

    
82
#undef MOVNTQ
83
#undef PAVGB
84

    
85
//#undef HAVE_MMX2
86
//#define HAVE_3DNOW
87
//#undef HAVE_MMX
88
//#undef ARCH_X86
89
//#define WORDS_BIGENDIAN
90
#define DITHER1XBPP
91

    
92
#define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
93

    
94
#define RET 0xC3 //near return opcode for X86
95

    
96
#ifdef M_PI
97
#define PI M_PI
98
#else
99
#define PI 3.14159265358979323846
100
#endif
101

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

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

    
177
extern const int32_t Inverse_Table_6_9[8][4];
178

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

    
190
/*
191
NOTES
192
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
193

194
TODO
195
more intelligent misalignment avoidance for the horizontal scaler
196
write special vertical cubic upscale version
197
Optimize C code (yv12 / minmax)
198
add support for packed pixel yuv input & output
199
add support for Y8 output
200
optimize bgr24 & bgr32
201
add BGR4 output support
202
write special BGR->BGR scaler
203
*/
204

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

    
215
const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
216
        0x0103010301030103LL,
217
        0x0200020002000200LL,};
218

    
219
const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
220
        0x0602060206020602LL,
221
        0x0004000400040004LL,};
222

    
223
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
224
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
225
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
226
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
227
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
228
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
229

    
230
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
231
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
232
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
233

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

    
247
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
248
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
249
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
250
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
251
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
252

    
253
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
254
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
255
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
256
};
257

    
258
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
259

    
260
#endif /* defined(ARCH_X86) */
261

    
262
// clipping helper table for C implementations:
263
static unsigned char clip_table[768];
264

    
265
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
266

    
267
static const uint8_t  __attribute__((aligned(8))) dither_2x2_4[2][8]={
268
{  1,   3,   1,   3,   1,   3,   1,   3, },
269
{  2,   0,   2,   0,   2,   0,   2,   0, },
270
};
271

    
272
static const uint8_t  __attribute__((aligned(8))) dither_2x2_8[2][8]={
273
{  6,   2,   6,   2,   6,   2,   6,   2, },
274
{  0,   4,   0,   4,   0,   4,   0,   4, },
275
};
276

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

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

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

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

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

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

    
460
static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
461
                               int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
462
                               uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
463
{
464
    //FIXME Optimize (just quickly writen not opti..)
465
    int i;
466
    for (i=0; i<dstW; i++)
467
    {
468
        int val=1<<18;
469
        int j;
470
        for (j=0; j<lumFilterSize; j++)
471
            val += lumSrc[j][i] * lumFilter[j];
472

    
473
        dest[i]= av_clip_uint8(val>>19);
474
    }
475

    
476
    if (uDest)
477
        for (i=0; i<chrDstW; i++)
478
        {
479
            int u=1<<18;
480
            int v=1<<18;
481
            int j;
482
            for (j=0; j<chrFilterSize; j++)
483
            {
484
                u += chrSrc[j][i] * chrFilter[j];
485
                v += chrSrc[j][i + VOFW] * chrFilter[j];
486
            }
487

    
488
            uDest[i]= av_clip_uint8(u>>19);
489
            vDest[i]= av_clip_uint8(v>>19);
490
        }
491
}
492

    
493
static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
494
                                int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
495
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
496
{
497
    //FIXME Optimize (just quickly writen not opti..)
498
    int i;
499
    for (i=0; i<dstW; i++)
500
    {
501
        int val=1<<18;
502
        int j;
503
        for (j=0; j<lumFilterSize; j++)
504
            val += lumSrc[j][i] * lumFilter[j];
505

    
506
        dest[i]= av_clip_uint8(val>>19);
507
    }
508

    
509
    if (!uDest)
510
        return;
511

    
512
    if (dstFormat == PIX_FMT_NV12)
513
        for (i=0; i<chrDstW; i++)
514
        {
515
            int u=1<<18;
516
            int v=1<<18;
517
            int j;
518
            for (j=0; j<chrFilterSize; j++)
519
            {
520
                u += chrSrc[j][i] * chrFilter[j];
521
                v += chrSrc[j][i + VOFW] * chrFilter[j];
522
            }
523

    
524
            uDest[2*i]= av_clip_uint8(u>>19);
525
            uDest[2*i+1]= av_clip_uint8(v>>19);
526
        }
527
    else
528
        for (i=0; i<chrDstW; i++)
529
        {
530
            int u=1<<18;
531
            int v=1<<18;
532
            int j;
533
            for (j=0; j<chrFilterSize; j++)
534
            {
535
                u += chrSrc[j][i] * chrFilter[j];
536
                v += chrSrc[j][i + VOFW] * chrFilter[j];
537
            }
538

    
539
            uDest[2*i]= av_clip_uint8(v>>19);
540
            uDest[2*i+1]= av_clip_uint8(u>>19);
541
        }
542
}
543

    
544
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type) \
545
    for (i=0; i<(dstW>>1); i++){\
546
        int j;\
547
        int Y1 = 1<<18;\
548
        int Y2 = 1<<18;\
549
        int U  = 1<<18;\
550
        int V  = 1<<18;\
551
        type av_unused *r, *b, *g;\
552
        const int i2= 2*i;\
553
        \
554
        for (j=0; j<lumFilterSize; j++)\
555
        {\
556
            Y1 += lumSrc[j][i2] * lumFilter[j];\
557
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
558
        }\
559
        for (j=0; j<chrFilterSize; j++)\
560
        {\
561
            U += chrSrc[j][i] * chrFilter[j];\
562
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
563
        }\
564
        Y1>>=19;\
565
        Y2>>=19;\
566
        U >>=19;\
567
        V >>=19;\
568

    
569
#define YSCALE_YUV_2_PACKEDX_C(type) \
570
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type)\
571
        if ((Y1|Y2|U|V)&256)\
572
        {\
573
            if (Y1>255)   Y1=255; \
574
            else if (Y1<0)Y1=0;   \
575
            if (Y2>255)   Y2=255; \
576
            else if (Y2<0)Y2=0;   \
577
            if (U>255)    U=255;  \
578
            else if (U<0) U=0;    \
579
            if (V>255)    V=255;  \
580
            else if (V<0) V=0;    \
581
        }
582

    
583
#define YSCALE_YUV_2_PACKEDX_FULL_C \
584
    for (i=0; i<dstW; i++){\
585
        int j;\
586
        int Y = 0;\
587
        int U = -128<<19;\
588
        int V = -128<<19;\
589
        int R,G,B;\
590
        \
591
        for (j=0; j<lumFilterSize; j++){\
592
            Y += lumSrc[j][i     ] * lumFilter[j];\
593
        }\
594
        for (j=0; j<chrFilterSize; j++){\
595
            U += chrSrc[j][i     ] * chrFilter[j];\
596
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
597
        }\
598
        Y >>=10;\
599
        U >>=10;\
600
        V >>=10;\
601

    
602
#define YSCALE_YUV_2_RGBX_FULL_C(rnd) \
603
    YSCALE_YUV_2_PACKEDX_FULL_C\
604
        Y-= c->yuv2rgb_y_offset;\
605
        Y*= c->yuv2rgb_y_coeff;\
606
        Y+= rnd;\
607
        R= Y + V*c->yuv2rgb_v2r_coeff;\
608
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
609
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
610
        if ((R|G|B)&(0xC0000000)){\
611
            if (R>=(256<<22))   R=(256<<22)-1; \
612
            else if (R<0)R=0;   \
613
            if (G>=(256<<22))   G=(256<<22)-1; \
614
            else if (G<0)G=0;   \
615
            if (B>=(256<<22))   B=(256<<22)-1; \
616
            else if (B<0)B=0;   \
617
        }\
618

    
619

    
620
#define YSCALE_YUV_2_GRAY16_C \
621
    for (i=0; i<(dstW>>1); i++){\
622
        int j;\
623
        int Y1 = 1<<18;\
624
        int Y2 = 1<<18;\
625
        int U  = 1<<18;\
626
        int V  = 1<<18;\
627
        \
628
        const int i2= 2*i;\
629
        \
630
        for (j=0; j<lumFilterSize; j++)\
631
        {\
632
            Y1 += lumSrc[j][i2] * lumFilter[j];\
633
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
634
        }\
635
        Y1>>=11;\
636
        Y2>>=11;\
637
        if ((Y1|Y2|U|V)&65536)\
638
        {\
639
            if (Y1>65535)   Y1=65535; \
640
            else if (Y1<0)Y1=0;   \
641
            if (Y2>65535)   Y2=65535; \
642
            else if (Y2<0)Y2=0;   \
643
        }
644

    
645
#define YSCALE_YUV_2_RGBX_C(type) \
646
    YSCALE_YUV_2_PACKEDX_C(type)  /* FIXME fix tables so that cliping is not needed and then use _NOCLIP*/\
647
    r = (type *)c->table_rV[V];   \
648
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
649
    b = (type *)c->table_bU[U];   \
650

    
651
#define YSCALE_YUV_2_PACKED2_C   \
652
    for (i=0; i<(dstW>>1); i++){ \
653
        const int i2= 2*i;       \
654
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
655
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
656
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
657
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
658

    
659
#define YSCALE_YUV_2_GRAY16_2_C   \
660
    for (i=0; i<(dstW>>1); i++){ \
661
        const int i2= 2*i;       \
662
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
663
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
664

    
665
#define YSCALE_YUV_2_RGB2_C(type) \
666
    YSCALE_YUV_2_PACKED2_C\
667
    type *r, *b, *g;\
668
    r = (type *)c->table_rV[V];\
669
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
670
    b = (type *)c->table_bU[U];\
671

    
672
#define YSCALE_YUV_2_PACKED1_C \
673
    for (i=0; i<(dstW>>1); i++){\
674
        const int i2= 2*i;\
675
        int Y1= buf0[i2  ]>>7;\
676
        int Y2= buf0[i2+1]>>7;\
677
        int U= (uvbuf1[i     ])>>7;\
678
        int V= (uvbuf1[i+VOFW])>>7;\
679

    
680
#define YSCALE_YUV_2_GRAY16_1_C \
681
    for (i=0; i<(dstW>>1); i++){\
682
        const int i2= 2*i;\
683
        int Y1= buf0[i2  ]<<1;\
684
        int Y2= buf0[i2+1]<<1;\
685

    
686
#define YSCALE_YUV_2_RGB1_C(type) \
687
    YSCALE_YUV_2_PACKED1_C\
688
    type *r, *b, *g;\
689
    r = (type *)c->table_rV[V];\
690
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
691
    b = (type *)c->table_bU[U];\
692

    
693
#define YSCALE_YUV_2_PACKED1B_C \
694
    for (i=0; i<(dstW>>1); i++){\
695
        const int i2= 2*i;\
696
        int Y1= buf0[i2  ]>>7;\
697
        int Y2= buf0[i2+1]>>7;\
698
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
699
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
700

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

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

    
725

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

    
757

    
758
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
759
    switch(c->dstFormat)\
760
    {\
761
    case PIX_FMT_RGB32:\
762
    case PIX_FMT_BGR32:\
763
    case PIX_FMT_RGB32_1:\
764
    case PIX_FMT_BGR32_1:\
765
        func(uint32_t)\
766
            ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
767
            ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
768
        }                \
769
        break;\
770
    case PIX_FMT_RGB24:\
771
        func(uint8_t)\
772
            ((uint8_t*)dest)[0]= r[Y1];\
773
            ((uint8_t*)dest)[1]= g[Y1];\
774
            ((uint8_t*)dest)[2]= b[Y1];\
775
            ((uint8_t*)dest)[3]= r[Y2];\
776
            ((uint8_t*)dest)[4]= g[Y2];\
777
            ((uint8_t*)dest)[5]= b[Y2];\
778
            dest+=6;\
779
        }\
780
        break;\
781
    case PIX_FMT_BGR24:\
782
        func(uint8_t)\
783
            ((uint8_t*)dest)[0]= b[Y1];\
784
            ((uint8_t*)dest)[1]= g[Y1];\
785
            ((uint8_t*)dest)[2]= r[Y1];\
786
            ((uint8_t*)dest)[3]= b[Y2];\
787
            ((uint8_t*)dest)[4]= g[Y2];\
788
            ((uint8_t*)dest)[5]= r[Y2];\
789
            dest+=6;\
790
        }\
791
        break;\
792
    case PIX_FMT_RGB565:\
793
    case PIX_FMT_BGR565:\
794
        {\
795
            const int dr1= dither_2x2_8[y&1    ][0];\
796
            const int dg1= dither_2x2_4[y&1    ][0];\
797
            const int db1= dither_2x2_8[(y&1)^1][0];\
798
            const int dr2= dither_2x2_8[y&1    ][1];\
799
            const int dg2= dither_2x2_4[y&1    ][1];\
800
            const int db2= dither_2x2_8[(y&1)^1][1];\
801
            func(uint16_t)\
802
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
803
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
804
            }\
805
        }\
806
        break;\
807
    case PIX_FMT_RGB555:\
808
    case PIX_FMT_BGR555:\
809
        {\
810
            const int dr1= dither_2x2_8[y&1    ][0];\
811
            const int dg1= dither_2x2_8[y&1    ][1];\
812
            const int db1= dither_2x2_8[(y&1)^1][0];\
813
            const int dr2= dither_2x2_8[y&1    ][1];\
814
            const int dg2= dither_2x2_8[y&1    ][0];\
815
            const int db2= dither_2x2_8[(y&1)^1][1];\
816
            func(uint16_t)\
817
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
818
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
819
            }\
820
        }\
821
        break;\
822
    case PIX_FMT_RGB8:\
823
    case PIX_FMT_BGR8:\
824
        {\
825
            const uint8_t * const d64= dither_8x8_73[y&7];\
826
            const uint8_t * const d32= dither_8x8_32[y&7];\
827
            func(uint8_t)\
828
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
829
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
830
            }\
831
        }\
832
        break;\
833
    case PIX_FMT_RGB4:\
834
    case PIX_FMT_BGR4:\
835
        {\
836
            const uint8_t * const d64= dither_8x8_73 [y&7];\
837
            const uint8_t * const d128=dither_8x8_220[y&7];\
838
            func(uint8_t)\
839
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
840
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
841
            }\
842
        }\
843
        break;\
844
    case PIX_FMT_RGB4_BYTE:\
845
    case PIX_FMT_BGR4_BYTE:\
846
        {\
847
            const uint8_t * const d64= dither_8x8_73 [y&7];\
848
            const uint8_t * const d128=dither_8x8_220[y&7];\
849
            func(uint8_t)\
850
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
851
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
852
            }\
853
        }\
854
        break;\
855
    case PIX_FMT_MONOBLACK:\
856
    case PIX_FMT_MONOWHITE:\
857
        {\
858
            func_monoblack\
859
        }\
860
        break;\
861
    case PIX_FMT_YUYV422:\
862
        func2\
863
            ((uint8_t*)dest)[2*i2+0]= Y1;\
864
            ((uint8_t*)dest)[2*i2+1]= U;\
865
            ((uint8_t*)dest)[2*i2+2]= Y2;\
866
            ((uint8_t*)dest)[2*i2+3]= V;\
867
        }                \
868
        break;\
869
    case PIX_FMT_UYVY422:\
870
        func2\
871
            ((uint8_t*)dest)[2*i2+0]= U;\
872
            ((uint8_t*)dest)[2*i2+1]= Y1;\
873
            ((uint8_t*)dest)[2*i2+2]= V;\
874
            ((uint8_t*)dest)[2*i2+3]= Y2;\
875
        }                \
876
        break;\
877
    case PIX_FMT_GRAY16BE:\
878
        func_g16\
879
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
880
            ((uint8_t*)dest)[2*i2+1]= Y1;\
881
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
882
            ((uint8_t*)dest)[2*i2+3]= Y2;\
883
        }                \
884
        break;\
885
    case PIX_FMT_GRAY16LE:\
886
        func_g16\
887
            ((uint8_t*)dest)[2*i2+0]= Y1;\
888
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
889
            ((uint8_t*)dest)[2*i2+2]= Y2;\
890
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
891
        }                \
892
        break;\
893
    }\
894

    
895

    
896
static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
897
                                  int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
898
                                  uint8_t *dest, int dstW, int y)
899
{
900
    int i;
901
    YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
902
}
903

    
904
static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
905
                                    int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
906
                                    uint8_t *dest, int dstW, int y)
907
{
908
    int i;
909
    int step= fmt_depth(c->dstFormat)/8;
910
    int aidx= 3;
911

    
912
    switch(c->dstFormat){
913
    case PIX_FMT_ARGB:
914
        dest++;
915
        aidx= 0;
916
    case PIX_FMT_RGB24:
917
        aidx--;
918
    case PIX_FMT_RGBA:
919
        YSCALE_YUV_2_RGBX_FULL_C(1<<21)
920
            dest[aidx]= 0;
921
            dest[0]= R>>22;
922
            dest[1]= G>>22;
923
            dest[2]= B>>22;
924
            dest+= step;
925
        }
926
        break;
927
    case PIX_FMT_ABGR:
928
        dest++;
929
        aidx= 0;
930
    case PIX_FMT_BGR24:
931
        aidx--;
932
    case PIX_FMT_BGRA:
933
        YSCALE_YUV_2_RGBX_FULL_C(1<<21)
934
            dest[aidx]= 0;
935
            dest[0]= B>>22;
936
            dest[1]= G>>22;
937
            dest[2]= R>>22;
938
            dest+= step;
939
        }
940
        break;
941
    default:
942
        assert(0);
943
    }
944
}
945

    
946
//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
947
//Plain C versions
948
#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
949
#define COMPILE_C
950
#endif
951

    
952
#ifdef ARCH_PPC
953
#if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
954
#define COMPILE_ALTIVEC
955
#endif //HAVE_ALTIVEC
956
#endif //ARCH_PPC
957

    
958
#if defined(ARCH_X86)
959

    
960
#if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
961
#define COMPILE_MMX
962
#endif
963

    
964
#if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
965
#define COMPILE_MMX2
966
#endif
967

    
968
#if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
969
#define COMPILE_3DNOW
970
#endif
971
#endif //ARCH_X86 || ARCH_X86_64
972

    
973
#undef HAVE_MMX
974
#undef HAVE_MMX2
975
#undef HAVE_3DNOW
976

    
977
#ifdef COMPILE_C
978
#undef HAVE_MMX
979
#undef HAVE_MMX2
980
#undef HAVE_3DNOW
981
#undef HAVE_ALTIVEC
982
#define RENAME(a) a ## _C
983
#include "swscale_template.c"
984
#endif
985

    
986
#ifdef COMPILE_ALTIVEC
987
#undef RENAME
988
#define HAVE_ALTIVEC
989
#define RENAME(a) a ## _altivec
990
#include "swscale_template.c"
991
#endif
992

    
993
#if defined(ARCH_X86)
994

    
995
//X86 versions
996
/*
997
#undef RENAME
998
#undef HAVE_MMX
999
#undef HAVE_MMX2
1000
#undef HAVE_3DNOW
1001
#define ARCH_X86
1002
#define RENAME(a) a ## _X86
1003
#include "swscale_template.c"
1004
*/
1005
//MMX versions
1006
#ifdef COMPILE_MMX
1007
#undef RENAME
1008
#define HAVE_MMX
1009
#undef HAVE_MMX2
1010
#undef HAVE_3DNOW
1011
#define RENAME(a) a ## _MMX
1012
#include "swscale_template.c"
1013
#endif
1014

    
1015
//MMX2 versions
1016
#ifdef COMPILE_MMX2
1017
#undef RENAME
1018
#define HAVE_MMX
1019
#define HAVE_MMX2
1020
#undef HAVE_3DNOW
1021
#define RENAME(a) a ## _MMX2
1022
#include "swscale_template.c"
1023
#endif
1024

    
1025
//3DNOW versions
1026
#ifdef COMPILE_3DNOW
1027
#undef RENAME
1028
#define HAVE_MMX
1029
#undef HAVE_MMX2
1030
#define HAVE_3DNOW
1031
#define RENAME(a) a ## _3DNow
1032
#include "swscale_template.c"
1033
#endif
1034

    
1035
#endif //ARCH_X86 || ARCH_X86_64
1036

    
1037
// minor note: the HAVE_xyz is messed up after that line so don't use it
1038

    
1039
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1040
{
1041
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1042
    if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
1043
    else                return getSplineCoeff(        0.0,
1044
                                             b+ 2.0*c + 3.0*d,
1045
                                                    c + 3.0*d,
1046
                                            -b- 3.0*c - 6.0*d,
1047
                                            dist-1.0);
1048
}
1049

    
1050
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1051
                             int srcW, int dstW, int filterAlign, int one, int flags,
1052
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1053
{
1054
    int i;
1055
    int filterSize;
1056
    int filter2Size;
1057
    int minFilterSize;
1058
    int64_t *filter=NULL;
1059
    int64_t *filter2=NULL;
1060
    const int64_t fone= 1LL<<54;
1061
    int ret= -1;
1062
#if defined(ARCH_X86)
1063
    if (flags & SWS_CPU_CAPS_MMX)
1064
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1065
#endif
1066

    
1067
    // Note the +1 is for the MMXscaler which reads over the end
1068
    *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1069

    
1070
    if (FFABS(xInc - 0x10000) <10) // unscaled
1071
    {
1072
        int i;
1073
        filterSize= 1;
1074
        filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1075

    
1076
        for (i=0; i<dstW; i++)
1077
        {
1078
            filter[i*filterSize]= fone;
1079
            (*filterPos)[i]=i;
1080
        }
1081

    
1082
    }
1083
    else if (flags&SWS_POINT) // lame looking point sampling mode
1084
    {
1085
        int i;
1086
        int xDstInSrc;
1087
        filterSize= 1;
1088
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1089

    
1090
        xDstInSrc= xInc/2 - 0x8000;
1091
        for (i=0; i<dstW; i++)
1092
        {
1093
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1094

    
1095
            (*filterPos)[i]= xx;
1096
            filter[i]= fone;
1097
            xDstInSrc+= xInc;
1098
        }
1099
    }
1100
    else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1101
    {
1102
        int i;
1103
        int xDstInSrc;
1104
        if      (flags&SWS_BICUBIC) filterSize= 4;
1105
        else if (flags&SWS_X      ) filterSize= 4;
1106
        else                        filterSize= 2; // SWS_BILINEAR / SWS_AREA
1107
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1108

    
1109
        xDstInSrc= xInc/2 - 0x8000;
1110
        for (i=0; i<dstW; i++)
1111
        {
1112
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1113
            int j;
1114

    
1115
            (*filterPos)[i]= xx;
1116
                //Bilinear upscale / linear interpolate / Area averaging
1117
                for (j=0; j<filterSize; j++)
1118
                {
1119
                    int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1120
                    if (coeff<0) coeff=0;
1121
                    filter[i*filterSize + j]= coeff;
1122
                    xx++;
1123
                }
1124
            xDstInSrc+= xInc;
1125
        }
1126
    }
1127
    else
1128
    {
1129
        int xDstInSrc;
1130
        int sizeFactor;
1131

    
1132
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1133
        else if (flags&SWS_X)            sizeFactor=  8;
1134
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1135
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1136
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1137
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1138
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1139
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1140
        else {
1141
            sizeFactor= 0; //GCC warning killer
1142
            assert(0);
1143
        }
1144

    
1145
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1146
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1147

    
1148
        if (filterSize > srcW-2) filterSize=srcW-2;
1149

    
1150
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1151

    
1152
        xDstInSrc= xInc - 0x10000;
1153
        for (i=0; i<dstW; i++)
1154
        {
1155
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1156
            int j;
1157
            (*filterPos)[i]= xx;
1158
            for (j=0; j<filterSize; j++)
1159
            {
1160
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1161
                double floatd;
1162
                int64_t coeff;
1163

    
1164
                if (xInc > 1<<16)
1165
                    d= d*dstW/srcW;
1166
                floatd= d * (1.0/(1<<30));
1167

    
1168
                if (flags & SWS_BICUBIC)
1169
                {
1170
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1171
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1172
                    int64_t dd = ( d*d)>>30;
1173
                    int64_t ddd= (dd*d)>>30;
1174

    
1175
                    if      (d < 1LL<<30)
1176
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1177
                    else if (d < 1LL<<31)
1178
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1179
                    else
1180
                        coeff=0.0;
1181
                    coeff *= fone>>(30+24);
1182
                }
1183
/*                else if (flags & SWS_X)
1184
                {
1185
                    double p= param ? param*0.01 : 0.3;
1186
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1187
                    coeff*= pow(2.0, - p*d*d);
1188
                }*/
1189
                else if (flags & SWS_X)
1190
                {
1191
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1192
                    double c;
1193

    
1194
                    if (floatd<1.0)
1195
                        c = cos(floatd*PI);
1196
                    else
1197
                        c=-1.0;
1198
                    if (c<0.0)      c= -pow(-c, A);
1199
                    else            c=  pow( c, A);
1200
                    coeff= (c*0.5 + 0.5)*fone;
1201
                }
1202
                else if (flags & SWS_AREA)
1203
                {
1204
                    int64_t d2= d - (1<<29);
1205
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1206
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1207
                    else coeff=0.0;
1208
                    coeff *= fone>>(30+16);
1209
                }
1210
                else if (flags & SWS_GAUSS)
1211
                {
1212
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1213
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
1214
                }
1215
                else if (flags & SWS_SINC)
1216
                {
1217
                    coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1218
                }
1219
                else if (flags & SWS_LANCZOS)
1220
                {
1221
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1222
                    coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1223
                    if (floatd>p) coeff=0;
1224
                }
1225
                else if (flags & SWS_BILINEAR)
1226
                {
1227
                    coeff= (1<<30) - d;
1228
                    if (coeff<0) coeff=0;
1229
                    coeff *= fone >> 30;
1230
                }
1231
                else if (flags & SWS_SPLINE)
1232
                {
1233
                    double p=-2.196152422706632;
1234
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1235
                }
1236
                else {
1237
                    coeff= 0.0; //GCC warning killer
1238
                    assert(0);
1239
                }
1240

    
1241
                filter[i*filterSize + j]= coeff;
1242
                xx++;
1243
            }
1244
            xDstInSrc+= 2*xInc;
1245
        }
1246
    }
1247

    
1248
    /* apply src & dst Filter to filter -> filter2
1249
       av_free(filter);
1250
    */
1251
    assert(filterSize>0);
1252
    filter2Size= filterSize;
1253
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1254
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1255
    assert(filter2Size>0);
1256
    filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1257

    
1258
    for (i=0; i<dstW; i++)
1259
    {
1260
        int j, k;
1261

    
1262
        if(srcFilter){
1263
            for (k=0; k<srcFilter->length; k++){
1264
                for (j=0; j<filterSize; j++)
1265
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1266
            }
1267
        }else{
1268
            for (j=0; j<filterSize; j++)
1269
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1270
        }
1271
        //FIXME dstFilter
1272

    
1273
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1274
    }
1275
    av_freep(&filter);
1276

    
1277
    /* try to reduce the filter-size (step1 find size and shift left) */
1278
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1279
    minFilterSize= 0;
1280
    for (i=dstW-1; i>=0; i--)
1281
    {
1282
        int min= filter2Size;
1283
        int j;
1284
        int64_t cutOff=0.0;
1285

    
1286
        /* get rid off near zero elements on the left by shifting left */
1287
        for (j=0; j<filter2Size; j++)
1288
        {
1289
            int k;
1290
            cutOff += FFABS(filter2[i*filter2Size]);
1291

    
1292
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1293

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

    
1297
            // Move filter coeffs left
1298
            for (k=1; k<filter2Size; k++)
1299
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1300
            filter2[i*filter2Size + k - 1]= 0;
1301
            (*filterPos)[i]++;
1302
        }
1303

    
1304
        cutOff=0;
1305
        /* count near zeros on the right */
1306
        for (j=filter2Size-1; j>0; j--)
1307
        {
1308
            cutOff += FFABS(filter2[i*filter2Size + j]);
1309

    
1310
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1311
            min--;
1312
        }
1313

    
1314
        if (min>minFilterSize) minFilterSize= min;
1315
    }
1316

    
1317
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1318
        // we can handle the special case 4,
1319
        // so we don't want to go to the full 8
1320
        if (minFilterSize < 5)
1321
            filterAlign = 4;
1322

    
1323
        // we really don't want to waste our time
1324
        // doing useless computation, so fall-back on
1325
        // the scalar C code for very small filter.
1326
        // vectorizing is worth it only if you have
1327
        // decent-sized vector.
1328
        if (minFilterSize < 3)
1329
            filterAlign = 1;
1330
    }
1331

    
1332
    if (flags & SWS_CPU_CAPS_MMX) {
1333
        // special case for unscaled vertical filtering
1334
        if (minFilterSize == 1 && filterAlign == 2)
1335
            filterAlign= 1;
1336
    }
1337

    
1338
    assert(minFilterSize > 0);
1339
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1340
    assert(filterSize > 0);
1341
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1342
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1343
        goto error;
1344
    *outFilterSize= filterSize;
1345

    
1346
    if (flags&SWS_PRINT_INFO)
1347
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1348
    /* try to reduce the filter-size (step2 reduce it) */
1349
    for (i=0; i<dstW; i++)
1350
    {
1351
        int j;
1352

    
1353
        for (j=0; j<filterSize; j++)
1354
        {
1355
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1356
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1357
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1358
                filter[i*filterSize + j]= 0;
1359
        }
1360
    }
1361

    
1362

    
1363
    //FIXME try to align filterpos if possible
1364

    
1365
    //fix borders
1366
    for (i=0; i<dstW; i++)
1367
    {
1368
        int j;
1369
        if ((*filterPos)[i] < 0)
1370
        {
1371
            // Move filter coeffs left to compensate for filterPos
1372
            for (j=1; j<filterSize; j++)
1373
            {
1374
                int left= FFMAX(j + (*filterPos)[i], 0);
1375
                filter[i*filterSize + left] += filter[i*filterSize + j];
1376
                filter[i*filterSize + j]=0;
1377
            }
1378
            (*filterPos)[i]= 0;
1379
        }
1380

    
1381
        if ((*filterPos)[i] + filterSize > srcW)
1382
        {
1383
            int shift= (*filterPos)[i] + filterSize - srcW;
1384
            // Move filter coeffs right to compensate for filterPos
1385
            for (j=filterSize-2; j>=0; j--)
1386
            {
1387
                int right= FFMIN(j + shift, filterSize-1);
1388
                filter[i*filterSize +right] += filter[i*filterSize +j];
1389
                filter[i*filterSize +j]=0;
1390
            }
1391
            (*filterPos)[i]= srcW - filterSize;
1392
        }
1393
    }
1394

    
1395
    // Note the +1 is for the MMXscaler which reads over the end
1396
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1397
    *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1398

    
1399
    /* Normalize & Store in outFilter */
1400
    for (i=0; i<dstW; i++)
1401
    {
1402
        int j;
1403
        int64_t error=0;
1404
        int64_t sum=0;
1405

    
1406
        for (j=0; j<filterSize; j++)
1407
        {
1408
            sum+= filter[i*filterSize + j];
1409
        }
1410
        sum= (sum + one/2)/ one;
1411
        for (j=0; j<*outFilterSize; j++)
1412
        {
1413
            int64_t v= filter[i*filterSize + j] + error;
1414
            int intV= ROUNDED_DIV(v, sum);
1415
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1416
            error= v - intV*sum;
1417
        }
1418
    }
1419

    
1420
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1421
    for (i=0; i<*outFilterSize; i++)
1422
    {
1423
        int j= dstW*(*outFilterSize);
1424
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1425
    }
1426

    
1427
    ret=0;
1428
error:
1429
    av_free(filter);
1430
    av_free(filter2);
1431
    return ret;
1432
}
1433

    
1434
#ifdef COMPILE_MMX2
1435
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1436
{
1437
    uint8_t *fragmentA;
1438
    long imm8OfPShufW1A;
1439
    long imm8OfPShufW2A;
1440
    long fragmentLengthA;
1441
    uint8_t *fragmentB;
1442
    long imm8OfPShufW1B;
1443
    long imm8OfPShufW2B;
1444
    long fragmentLengthB;
1445
    int fragmentPos;
1446

    
1447
    int xpos, i;
1448

    
1449
    // create an optimized horizontal scaling routine
1450

    
1451
    //code fragment
1452

    
1453
    __asm__ volatile(
1454
        "jmp                         9f                 \n\t"
1455
    // Begin
1456
        "0:                                             \n\t"
1457
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1458
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1459
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1460
        "punpcklbw                %%mm7, %%mm1          \n\t"
1461
        "punpcklbw                %%mm7, %%mm0          \n\t"
1462
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1463
        "1:                                             \n\t"
1464
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1465
        "2:                                             \n\t"
1466
        "psubw                    %%mm1, %%mm0          \n\t"
1467
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1468
        "pmullw                   %%mm3, %%mm0          \n\t"
1469
        "psllw                       $7, %%mm1          \n\t"
1470
        "paddw                    %%mm1, %%mm0          \n\t"
1471

    
1472
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1473

    
1474
        "add                         $8, %%"REG_a"      \n\t"
1475
    // End
1476
        "9:                                             \n\t"
1477
//        "int $3                                         \n\t"
1478
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1479
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1480
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1481
        "dec                         %1                 \n\t"
1482
        "dec                         %2                 \n\t"
1483
        "sub                         %0, %1             \n\t"
1484
        "sub                         %0, %2             \n\t"
1485
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1486
        "sub                         %0, %3             \n\t"
1487

    
1488

    
1489
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1490
        "=r" (fragmentLengthA)
1491
    );
1492

    
1493
    __asm__ volatile(
1494
        "jmp                         9f                 \n\t"
1495
    // Begin
1496
        "0:                                             \n\t"
1497
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1498
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1499
        "punpcklbw                %%mm7, %%mm0          \n\t"
1500
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1501
        "1:                                             \n\t"
1502
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1503
        "2:                                             \n\t"
1504
        "psubw                    %%mm1, %%mm0          \n\t"
1505
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1506
        "pmullw                   %%mm3, %%mm0          \n\t"
1507
        "psllw                       $7, %%mm1          \n\t"
1508
        "paddw                    %%mm1, %%mm0          \n\t"
1509

    
1510
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1511

    
1512
        "add                         $8, %%"REG_a"      \n\t"
1513
    // End
1514
        "9:                                             \n\t"
1515
//        "int                       $3                   \n\t"
1516
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1517
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1518
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1519
        "dec                         %1                 \n\t"
1520
        "dec                         %2                 \n\t"
1521
        "sub                         %0, %1             \n\t"
1522
        "sub                         %0, %2             \n\t"
1523
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1524
        "sub                         %0, %3             \n\t"
1525

    
1526

    
1527
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1528
        "=r" (fragmentLengthB)
1529
    );
1530

    
1531
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1532
    fragmentPos=0;
1533

    
1534
    for (i=0; i<dstW/numSplits; i++)
1535
    {
1536
        int xx=xpos>>16;
1537

    
1538
        if ((i&3) == 0)
1539
        {
1540
            int a=0;
1541
            int b=((xpos+xInc)>>16) - xx;
1542
            int c=((xpos+xInc*2)>>16) - xx;
1543
            int d=((xpos+xInc*3)>>16) - xx;
1544

    
1545
            filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1546
            filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1547
            filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1548
            filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1549
            filterPos[i/2]= xx;
1550

    
1551
            if (d+1<4)
1552
            {
1553
                int maxShift= 3-(d+1);
1554
                int shift=0;
1555

    
1556
                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1557

    
1558
                funnyCode[fragmentPos + imm8OfPShufW1B]=
1559
                    (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1560
                funnyCode[fragmentPos + imm8OfPShufW2B]=
1561
                    a | (b<<2) | (c<<4) | (d<<6);
1562

    
1563
                if (i+3>=dstW) shift=maxShift; //avoid overread
1564
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1565

    
1566
                if (shift && i>=shift)
1567
                {
1568
                    funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1569
                    funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1570
                    filterPos[i/2]-=shift;
1571
                }
1572

    
1573
                fragmentPos+= fragmentLengthB;
1574
            }
1575
            else
1576
            {
1577
                int maxShift= 3-d;
1578
                int shift=0;
1579

    
1580
                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1581

    
1582
                funnyCode[fragmentPos + imm8OfPShufW1A]=
1583
                funnyCode[fragmentPos + imm8OfPShufW2A]=
1584
                    a | (b<<2) | (c<<4) | (d<<6);
1585

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

    
1589
                if (shift && i>=shift)
1590
                {
1591
                    funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1592
                    funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1593
                    filterPos[i/2]-=shift;
1594
                }
1595

    
1596
                fragmentPos+= fragmentLengthA;
1597
            }
1598

    
1599
            funnyCode[fragmentPos]= RET;
1600
        }
1601
        xpos+=xInc;
1602
    }
1603
    filterPos[i/2]= xpos>>16; // needed to jump to the next part
1604
}
1605
#endif /* COMPILE_MMX2 */
1606

    
1607
static void globalInit(void){
1608
    // generating tables:
1609
    int i;
1610
    for (i=0; i<768; i++){
1611
        int c= av_clip_uint8(i-256);
1612
        clip_table[i]=c;
1613
    }
1614
}
1615

    
1616
static SwsFunc getSwsFunc(int flags){
1617

    
1618
#if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1619
#if defined(ARCH_X86)
1620
    // ordered per speed fastest first
1621
    if (flags & SWS_CPU_CAPS_MMX2)
1622
        return swScale_MMX2;
1623
    else if (flags & SWS_CPU_CAPS_3DNOW)
1624
        return swScale_3DNow;
1625
    else if (flags & SWS_CPU_CAPS_MMX)
1626
        return swScale_MMX;
1627
    else
1628
        return swScale_C;
1629

    
1630
#else
1631
#ifdef ARCH_PPC
1632
    if (flags & SWS_CPU_CAPS_ALTIVEC)
1633
        return swScale_altivec;
1634
    else
1635
        return swScale_C;
1636
#endif
1637
    return swScale_C;
1638
#endif /* defined(ARCH_X86) */
1639
#else //RUNTIME_CPUDETECT
1640
#ifdef HAVE_MMX2
1641
    return swScale_MMX2;
1642
#elif defined (HAVE_3DNOW)
1643
    return swScale_3DNow;
1644
#elif defined (HAVE_MMX)
1645
    return swScale_MMX;
1646
#elif defined (HAVE_ALTIVEC)
1647
    return swScale_altivec;
1648
#else
1649
    return swScale_C;
1650
#endif
1651
#endif //!RUNTIME_CPUDETECT
1652
}
1653

    
1654
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1655
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1656
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1657
    /* Copy Y plane */
1658
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1659
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1660
    else
1661
    {
1662
        int i;
1663
        uint8_t *srcPtr= src[0];
1664
        uint8_t *dstPtr= dst;
1665
        for (i=0; i<srcSliceH; i++)
1666
        {
1667
            memcpy(dstPtr, srcPtr, c->srcW);
1668
            srcPtr+= srcStride[0];
1669
            dstPtr+= dstStride[0];
1670
        }
1671
    }
1672
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1673
    if (c->dstFormat == PIX_FMT_NV12)
1674
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1675
    else
1676
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1677

    
1678
    return srcSliceH;
1679
}
1680

    
1681
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1682
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1683
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1684

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

    
1687
    return srcSliceH;
1688
}
1689

    
1690
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1691
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1692
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1693

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

    
1696
    return srcSliceH;
1697
}
1698

    
1699
static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1700
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1701
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1702

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

    
1705
    return srcSliceH;
1706
}
1707

    
1708
static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1709
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1710
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1711

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

    
1714
    return srcSliceH;
1715
}
1716

    
1717
static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1718
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1719
    const enum PixelFormat srcFormat= c->srcFormat;
1720
    const enum PixelFormat dstFormat= c->dstFormat;
1721
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1722
                 const uint8_t *palette)=NULL;
1723
    int i;
1724
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1725
    uint8_t *srcPtr= src[0];
1726

    
1727
    if (!usePal(srcFormat))
1728
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1729
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1730

    
1731
    switch(dstFormat){
1732
    case PIX_FMT_RGB32  : conv = palette8topacked32; break;
1733
    case PIX_FMT_BGR32  : conv = palette8topacked32; break;
1734
    case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1735
    case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1736
    case PIX_FMT_RGB24  : conv = palette8topacked24; break;
1737
    case PIX_FMT_BGR24  : conv = palette8topacked24; break;
1738
    default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1739
                    sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1740
    }
1741

    
1742

    
1743
    for (i=0; i<srcSliceH; i++) {
1744
        conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1745
        srcPtr+= srcStride[0];
1746
        dstPtr+= dstStride[0];
1747
    }
1748

    
1749
    return srcSliceH;
1750
}
1751

    
1752
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1753
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1754
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1755
    const enum PixelFormat srcFormat= c->srcFormat;
1756
    const enum PixelFormat dstFormat= c->dstFormat;
1757
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1758
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1759
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1760
    const int dstId= fmt_depth(dstFormat) >> 2;
1761
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1762

    
1763
    /* BGR -> BGR */
1764
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
1765
       || (isRGB(srcFormat) && isRGB(dstFormat))){
1766
        switch(srcId | (dstId<<4)){
1767
        case 0x34: conv= rgb16to15; break;
1768
        case 0x36: conv= rgb24to15; break;
1769
        case 0x38: conv= rgb32to15; break;
1770
        case 0x43: conv= rgb15to16; break;
1771
        case 0x46: conv= rgb24to16; break;
1772
        case 0x48: conv= rgb32to16; break;
1773
        case 0x63: conv= rgb15to24; break;
1774
        case 0x64: conv= rgb16to24; break;
1775
        case 0x68: conv= rgb32to24; break;
1776
        case 0x83: conv= rgb15to32; break;
1777
        case 0x84: conv= rgb16to32; break;
1778
        case 0x86: conv= rgb24to32; break;
1779
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1780
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1781
        }
1782
    }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
1783
             || (isRGB(srcFormat) && isBGR(dstFormat))){
1784
        switch(srcId | (dstId<<4)){
1785
        case 0x33: conv= rgb15tobgr15; break;
1786
        case 0x34: conv= rgb16tobgr15; break;
1787
        case 0x36: conv= rgb24tobgr15; break;
1788
        case 0x38: conv= rgb32tobgr15; break;
1789
        case 0x43: conv= rgb15tobgr16; break;
1790
        case 0x44: conv= rgb16tobgr16; break;
1791
        case 0x46: conv= rgb24tobgr16; break;
1792
        case 0x48: conv= rgb32tobgr16; break;
1793
        case 0x63: conv= rgb15tobgr24; break;
1794
        case 0x64: conv= rgb16tobgr24; break;
1795
        case 0x66: conv= rgb24tobgr24; break;
1796
        case 0x68: conv= rgb32tobgr24; break;
1797
        case 0x83: conv= rgb15tobgr32; break;
1798
        case 0x84: conv= rgb16tobgr32; break;
1799
        case 0x86: conv= rgb24tobgr32; break;
1800
        case 0x88: conv= rgb32tobgr32; break;
1801
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1802
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1803
        }
1804
    }else{
1805
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1806
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1807
    }
1808

    
1809
    if(conv)
1810
    {
1811
        uint8_t *srcPtr= src[0];
1812
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1813
            srcPtr += ALT32_CORR;
1814

    
1815
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1816
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1817
        else
1818
        {
1819
            int i;
1820
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1821

    
1822
            for (i=0; i<srcSliceH; i++)
1823
            {
1824
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
1825
                srcPtr+= srcStride[0];
1826
                dstPtr+= dstStride[0];
1827
            }
1828
        }
1829
    }
1830
    return srcSliceH;
1831
}
1832

    
1833
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1834
                              int srcSliceH, uint8_t* dst[], int dstStride[]){
1835

    
1836
    rgb24toyv12(
1837
        src[0],
1838
        dst[0]+ srcSliceY    *dstStride[0],
1839
        dst[1]+(srcSliceY>>1)*dstStride[1],
1840
        dst[2]+(srcSliceY>>1)*dstStride[2],
1841
        c->srcW, srcSliceH,
1842
        dstStride[0], dstStride[1], srcStride[0]);
1843
    return srcSliceH;
1844
}
1845

    
1846
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1847
                             int srcSliceH, uint8_t* dst[], int dstStride[]){
1848
    int i;
1849

    
1850
    /* copy Y */
1851
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1852
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1853
    else{
1854
        uint8_t *srcPtr= src[0];
1855
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1856

    
1857
        for (i=0; i<srcSliceH; i++)
1858
        {
1859
            memcpy(dstPtr, srcPtr, c->srcW);
1860
            srcPtr+= srcStride[0];
1861
            dstPtr+= dstStride[0];
1862
        }
1863
    }
1864

    
1865
    if (c->dstFormat==PIX_FMT_YUV420P){
1866
        planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1867
        planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1868
    }else{
1869
        planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1870
        planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1871
    }
1872
    return srcSliceH;
1873
}
1874

    
1875
/* unscaled copy like stuff (assumes nearly identical formats) */
1876
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1877
                      int srcSliceH, uint8_t* dst[], int dstStride[])
1878
{
1879
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1880
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1881
    else
1882
    {
1883
        int i;
1884
        uint8_t *srcPtr= src[0];
1885
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1886
        int length=0;
1887

    
1888
        /* universal length finder */
1889
        while(length+c->srcW <= FFABS(dstStride[0])
1890
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1891
        assert(length!=0);
1892

    
1893
        for (i=0; i<srcSliceH; i++)
1894
        {
1895
            memcpy(dstPtr, srcPtr, length);
1896
            srcPtr+= srcStride[0];
1897
            dstPtr+= dstStride[0];
1898
        }
1899
    }
1900
    return srcSliceH;
1901
}
1902

    
1903
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1904
                      int srcSliceH, uint8_t* dst[], int dstStride[])
1905
{
1906
    int plane;
1907
    for (plane=0; plane<3; plane++)
1908
    {
1909
        int length= plane==0 ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1910
        int y=      plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1911
        int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1912

    
1913
        if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1914
        {
1915
            if (!isGray(c->dstFormat))
1916
                memset(dst[plane], 128, dstStride[plane]*height);
1917
        }
1918
        else
1919
        {
1920
            if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1921
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1922
            else
1923
            {
1924
                int i;
1925
                uint8_t *srcPtr= src[plane];
1926
                uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1927
                for (i=0; i<height; i++)
1928
                {
1929
                    memcpy(dstPtr, srcPtr, length);
1930
                    srcPtr+= srcStride[plane];
1931
                    dstPtr+= dstStride[plane];
1932
                }
1933
            }
1934
        }
1935
    }
1936
    return srcSliceH;
1937
}
1938

    
1939
static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1940
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
1941

    
1942
    int length= c->srcW;
1943
    int y=      srcSliceY;
1944
    int height= srcSliceH;
1945
    int i, j;
1946
    uint8_t *srcPtr= src[0];
1947
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1948

    
1949
    if (!isGray(c->dstFormat)){
1950
        int height= -((-srcSliceH)>>c->chrDstVSubSample);
1951
        memset(dst[1], 128, dstStride[1]*height);
1952
        memset(dst[2], 128, dstStride[2]*height);
1953
    }
1954
    if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1955
    for (i=0; i<height; i++)
1956
    {
1957
        for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1958
        srcPtr+= srcStride[0];
1959
        dstPtr+= dstStride[0];
1960
    }
1961
    return srcSliceH;
1962
}
1963

    
1964
static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1965
                        int srcSliceH, uint8_t* dst[], int dstStride[]){
1966

    
1967
    int length= c->srcW;
1968
    int y=      srcSliceY;
1969
    int height= srcSliceH;
1970
    int i, j;
1971
    uint8_t *srcPtr= src[0];
1972
    uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1973
    for (i=0; i<height; i++)
1974
    {
1975
        for (j=0; j<length; j++)
1976
        {
1977
            dstPtr[j<<1] = srcPtr[j];
1978
            dstPtr[(j<<1)+1] = srcPtr[j];
1979
        }
1980
        srcPtr+= srcStride[0];
1981
        dstPtr+= dstStride[0];
1982
    }
1983
    return srcSliceH;
1984
}
1985

    
1986
static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1987
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
1988

    
1989
    int length= c->srcW;
1990
    int y=      srcSliceY;
1991
    int height= srcSliceH;
1992
    int i, j;
1993
    uint16_t *srcPtr= (uint16_t*)src[0];
1994
    uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
1995
    for (i=0; i<height; i++)
1996
    {
1997
        for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1998
        srcPtr+= srcStride[0]/2;
1999
        dstPtr+= dstStride[0]/2;
2000
    }
2001
    return srcSliceH;
2002
}
2003

    
2004

    
2005
static void getSubSampleFactors(int *h, int *v, int format){
2006
    switch(format){
2007
    case PIX_FMT_UYVY422:
2008
    case PIX_FMT_YUYV422:
2009
        *h=1;
2010
        *v=0;
2011
        break;
2012
    case PIX_FMT_YUV420P:
2013
    case PIX_FMT_YUVA420P:
2014
    case PIX_FMT_GRAY16BE:
2015
    case PIX_FMT_GRAY16LE:
2016
    case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2017
    case PIX_FMT_NV12:
2018
    case PIX_FMT_NV21:
2019
        *h=1;
2020
        *v=1;
2021
        break;
2022
    case PIX_FMT_YUV440P:
2023
        *h=0;
2024
        *v=1;
2025
        break;
2026
    case PIX_FMT_YUV410P:
2027
        *h=2;
2028
        *v=2;
2029
        break;
2030
    case PIX_FMT_YUV444P:
2031
        *h=0;
2032
        *v=0;
2033
        break;
2034
    case PIX_FMT_YUV422P:
2035
        *h=1;
2036
        *v=0;
2037
        break;
2038
    case PIX_FMT_YUV411P:
2039
        *h=2;
2040
        *v=0;
2041
        break;
2042
    default:
2043
        *h=0;
2044
        *v=0;
2045
        break;
2046
    }
2047
}
2048

    
2049
static uint16_t roundToInt16(int64_t f){
2050
    int r= (f + (1<<15))>>16;
2051
         if (r<-0x7FFF) return 0x8000;
2052
    else if (r> 0x7FFF) return 0x7FFF;
2053
    else                return r;
2054
}
2055

    
2056
/**
2057
 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
2058
 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
2059
 * @return -1 if not supported
2060
 */
2061
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2062
    int64_t crv =  inv_table[0];
2063
    int64_t cbu =  inv_table[1];
2064
    int64_t cgu = -inv_table[2];
2065
    int64_t cgv = -inv_table[3];
2066
    int64_t cy  = 1<<16;
2067
    int64_t oy  = 0;
2068

    
2069
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2070
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2071

    
2072
    c->brightness= brightness;
2073
    c->contrast  = contrast;
2074
    c->saturation= saturation;
2075
    c->srcRange  = srcRange;
2076
    c->dstRange  = dstRange;
2077
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
2078

    
2079
    c->uOffset=   0x0400040004000400LL;
2080
    c->vOffset=   0x0400040004000400LL;
2081

    
2082
    if (!srcRange){
2083
        cy= (cy*255) / 219;
2084
        oy= 16<<16;
2085
    }else{
2086
        crv= (crv*224) / 255;
2087
        cbu= (cbu*224) / 255;
2088
        cgu= (cgu*224) / 255;
2089
        cgv= (cgv*224) / 255;
2090
    }
2091

    
2092
    cy = (cy *contrast             )>>16;
2093
    crv= (crv*contrast * saturation)>>32;
2094
    cbu= (cbu*contrast * saturation)>>32;
2095
    cgu= (cgu*contrast * saturation)>>32;
2096
    cgv= (cgv*contrast * saturation)>>32;
2097

    
2098
    oy -= 256*brightness;
2099

    
2100
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2101
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2102
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2103
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2104
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2105
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2106

    
2107
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2108
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2109
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2110
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2111
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2112
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2113

    
2114
    yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2115
    //FIXME factorize
2116

    
2117
#ifdef COMPILE_ALTIVEC
2118
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2119
        yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
2120
#endif
2121
    return 0;
2122
}
2123

    
2124
/**
2125
 * @return -1 if not supported
2126
 */
2127
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2128
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2129

    
2130
    *inv_table = c->srcColorspaceTable;
2131
    *table     = c->dstColorspaceTable;
2132
    *srcRange  = c->srcRange;
2133
    *dstRange  = c->dstRange;
2134
    *brightness= c->brightness;
2135
    *contrast  = c->contrast;
2136
    *saturation= c->saturation;
2137

    
2138
    return 0;
2139
}
2140

    
2141
static int handle_jpeg(enum PixelFormat *format)
2142
{
2143
    switch (*format) {
2144
        case PIX_FMT_YUVJ420P:
2145
            *format = PIX_FMT_YUV420P;
2146
            return 1;
2147
        case PIX_FMT_YUVJ422P:
2148
            *format = PIX_FMT_YUV422P;
2149
            return 1;
2150
        case PIX_FMT_YUVJ444P:
2151
            *format = PIX_FMT_YUV444P;
2152
            return 1;
2153
        case PIX_FMT_YUVJ440P:
2154
            *format = PIX_FMT_YUV440P;
2155
            return 1;
2156
        default:
2157
            return 0;
2158
    }
2159
}
2160

    
2161
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2162
                           SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2163

    
2164
    SwsContext *c;
2165
    int i;
2166
    int usesVFilter, usesHFilter;
2167
    int unscaled, needsDither;
2168
    int srcRange, dstRange;
2169
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2170
#if defined(ARCH_X86)
2171
    if (flags & SWS_CPU_CAPS_MMX)
2172
        __asm__ volatile("emms\n\t"::: "memory");
2173
#endif
2174

    
2175
#if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
2176
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2177
#ifdef HAVE_MMX2
2178
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2179
#elif defined (HAVE_3DNOW)
2180
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2181
#elif defined (HAVE_MMX)
2182
    flags |= SWS_CPU_CAPS_MMX;
2183
#elif defined (HAVE_ALTIVEC)
2184
    flags |= SWS_CPU_CAPS_ALTIVEC;
2185
#elif defined (ARCH_BFIN)
2186
    flags |= SWS_CPU_CAPS_BFIN;
2187
#endif
2188
#endif /* RUNTIME_CPUDETECT */
2189
    if (clip_table[512] != 255) globalInit();
2190
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2191

    
2192
    unscaled = (srcW == dstW && srcH == dstH);
2193
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2194
        && (fmt_depth(dstFormat))<24
2195
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2196

    
2197
    srcRange = handle_jpeg(&srcFormat);
2198
    dstRange = handle_jpeg(&dstFormat);
2199

    
2200
    if (!isSupportedIn(srcFormat))
2201
    {
2202
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2203
        return NULL;
2204
    }
2205
    if (!isSupportedOut(dstFormat))
2206
    {
2207
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2208
        return NULL;
2209
    }
2210

    
2211
    i= flags & ( SWS_POINT
2212
                |SWS_AREA
2213
                |SWS_BILINEAR
2214
                |SWS_FAST_BILINEAR
2215
                |SWS_BICUBIC
2216
                |SWS_X
2217
                |SWS_GAUSS
2218
                |SWS_LANCZOS
2219
                |SWS_SINC
2220
                |SWS_SPLINE
2221
                |SWS_BICUBLIN);
2222
    if(!i || (i & (i-1)))
2223
    {
2224
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be choosen\n");
2225
        return NULL;
2226
    }
2227

    
2228
    /* sanity check */
2229
    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
2230
    {
2231
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2232
               srcW, srcH, dstW, dstH);
2233
        return NULL;
2234
    }
2235
    if(srcW > VOFW || dstW > VOFW){
2236
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2237
        return NULL;
2238
    }
2239

    
2240
    if (!dstFilter) dstFilter= &dummyFilter;
2241
    if (!srcFilter) srcFilter= &dummyFilter;
2242

    
2243
    c= av_mallocz(sizeof(SwsContext));
2244

    
2245
    c->av_class = &sws_context_class;
2246
    c->srcW= srcW;
2247
    c->srcH= srcH;
2248
    c->dstW= dstW;
2249
    c->dstH= dstH;
2250
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2251
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2252
    c->flags= flags;
2253
    c->dstFormat= dstFormat;
2254
    c->srcFormat= srcFormat;
2255
    c->vRounder= 4* 0x0001000100010001ULL;
2256

    
2257
    usesHFilter= usesVFilter= 0;
2258
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2259
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2260
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2261
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2262
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2263
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2264
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2265
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2266

    
2267
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2268
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2269

    
2270
    // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2271
    if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2272

    
2273
    // drop some chroma lines if the user wants it
2274
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2275
    c->chrSrcVSubSample+= c->vChrDrop;
2276

    
2277
    // drop every 2. pixel for chroma calculation unless user wants full chroma
2278
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2279
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2280
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2281
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2282
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2283
        c->chrSrcHSubSample=1;
2284

    
2285
    if (param){
2286
        c->param[0] = param[0];
2287
        c->param[1] = param[1];
2288
    }else{
2289
        c->param[0] =
2290
        c->param[1] = SWS_PARAM_DEFAULT;
2291
    }
2292

    
2293
    c->chrIntHSubSample= c->chrDstHSubSample;
2294
    c->chrIntVSubSample= c->chrSrcVSubSample;
2295

    
2296
    // Note the -((-x)>>y) is so that we always round toward +inf.
2297
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2298
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2299
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2300
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2301

    
2302
    sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2303

    
2304
    /* unscaled special Cases */
2305
    if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2306
    {
2307
        /* yv12_to_nv12 */
2308
        if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2309
        {
2310
            c->swScale= PlanarToNV12Wrapper;
2311
        }
2312
#ifdef CONFIG_GPL
2313
        /* yuv2bgr */
2314
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat))
2315
            && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2316
        {
2317
            c->swScale= yuv2rgb_get_func_ptr(c);
2318
        }
2319
#endif
2320

    
2321
        if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
2322
        {
2323
            c->swScale= yvu9toyv12Wrapper;
2324
        }
2325

    
2326
        /* bgr24toYV12 */
2327
        if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
2328
            c->swScale= bgr24toyv12Wrapper;
2329

    
2330
        /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2331
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2332
           && (isBGR(dstFormat) || isRGB(dstFormat))
2333
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2334
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2335
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2336
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2337
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2338
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2339
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2340
           && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2341
                                             && dstFormat != PIX_FMT_RGB32_1
2342
                                             && dstFormat != PIX_FMT_BGR32_1
2343
           && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2344
             c->swScale= rgb2rgbWrapper;
2345

    
2346
        if ((usePal(srcFormat) && (
2347
                 dstFormat == PIX_FMT_RGB32   ||
2348
                 dstFormat == PIX_FMT_RGB32_1 ||
2349
                 dstFormat == PIX_FMT_RGB24   ||
2350
                 dstFormat == PIX_FMT_BGR32   ||
2351
                 dstFormat == PIX_FMT_BGR32_1 ||
2352
                 dstFormat == PIX_FMT_BGR24)))
2353
             c->swScale= pal2rgbWrapper;
2354

    
2355
        if (srcFormat == PIX_FMT_YUV422P)
2356
        {
2357
            if (dstFormat == PIX_FMT_YUYV422)
2358
                c->swScale= YUV422PToYuy2Wrapper;
2359
            else if (dstFormat == PIX_FMT_UYVY422)
2360
                c->swScale= YUV422PToUyvyWrapper;
2361
        }
2362

    
2363
        /* LQ converters if -sws 0 or -sws 4*/
2364
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2365
            /* yv12_to_yuy2 */
2366
            if (srcFormat == PIX_FMT_YUV420P)
2367
            {
2368
                if (dstFormat == PIX_FMT_YUYV422)
2369
                    c->swScale= PlanarToYuy2Wrapper;
2370
                else if (dstFormat == PIX_FMT_UYVY422)
2371
                    c->swScale= PlanarToUyvyWrapper;
2372
            }
2373
        }
2374

    
2375
#ifdef COMPILE_ALTIVEC
2376
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2377
            srcFormat == PIX_FMT_YUV420P) {
2378
          // unscaled YV12 -> packed YUV, we want speed
2379
          if (dstFormat == PIX_FMT_YUYV422)
2380
              c->swScale= yv12toyuy2_unscaled_altivec;
2381
          else if (dstFormat == PIX_FMT_UYVY422)
2382
              c->swScale= yv12touyvy_unscaled_altivec;
2383
        }
2384
#endif
2385

    
2386
        /* simple copy */
2387
        if (  srcFormat == dstFormat
2388
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2389
            || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2390
        {
2391
            if (isPacked(c->srcFormat))
2392
                c->swScale= packedCopy;
2393
            else /* Planar YUV or gray */
2394
                c->swScale= planarCopy;
2395
        }
2396

    
2397
        /* gray16{le,be} conversions */
2398
        if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2399
        {
2400
            c->swScale= gray16togray;
2401
        }
2402
        if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2403
        {
2404
            c->swScale= graytogray16;
2405
        }
2406
        if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2407
        {
2408
            c->swScale= gray16swap;
2409
        }
2410

    
2411
#ifdef ARCH_BFIN
2412
        if (flags & SWS_CPU_CAPS_BFIN)
2413
            ff_bfin_get_unscaled_swscale (c);
2414
#endif
2415

    
2416
        if (c->swScale){
2417
            if (flags&SWS_PRINT_INFO)
2418
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2419
                                sws_format_name(srcFormat), sws_format_name(dstFormat));
2420
            return c;
2421
        }
2422
    }
2423

    
2424
    if (flags & SWS_CPU_CAPS_MMX2)
2425
    {
2426
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2427
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2428
        {
2429
            if (flags&SWS_PRINT_INFO)
2430
                av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
2431
        }
2432
        if (usesHFilter) c->canMMX2BeUsed=0;
2433
    }
2434
    else
2435
        c->canMMX2BeUsed=0;
2436

    
2437
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2438
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2439

    
2440
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2441
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
2442
    // n-2 is the last chrominance sample available
2443
    // this is not perfect, but no one should notice the difference, the more correct variant
2444
    // would be like the vertical one, but that would require some special code for the
2445
    // first and last pixel
2446
    if (flags&SWS_FAST_BILINEAR)
2447
    {
2448
        if (c->canMMX2BeUsed)
2449
        {
2450
            c->lumXInc+= 20;
2451
            c->chrXInc+= 20;
2452
        }
2453
        //we don't use the x86asm scaler if mmx is available
2454
        else if (flags & SWS_CPU_CAPS_MMX)
2455
        {
2456
            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2457
            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2458
        }
2459
    }
2460

    
2461
    /* precalculate horizontal scaler filter coefficients */
2462
    {
2463
        const int filterAlign=
2464
            (flags & SWS_CPU_CAPS_MMX) ? 4 :
2465
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2466
            1;
2467

    
2468
        initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2469
                   srcW      ,       dstW, filterAlign, 1<<14,
2470
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2471
                   srcFilter->lumH, dstFilter->lumH, c->param);
2472
        initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2473
                   c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2474
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2475
                   srcFilter->chrH, dstFilter->chrH, c->param);
2476

    
2477
#define MAX_FUNNY_CODE_SIZE 10000
2478
#if defined(COMPILE_MMX2)
2479
// can't downscale !!!
2480
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2481
        {
2482
#ifdef MAP_ANONYMOUS
2483
            c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2484
            c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2485
#else
2486
            c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2487
            c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2488
#endif
2489

    
2490
            c->lumMmx2Filter   = av_malloc((dstW        /8+8)*sizeof(int16_t));
2491
            c->chrMmx2Filter   = av_malloc((c->chrDstW  /4+8)*sizeof(int16_t));
2492
            c->lumMmx2FilterPos= av_malloc((dstW      /2/8+8)*sizeof(int32_t));
2493
            c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2494

    
2495
            initMMX2HScaler(      dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2496
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2497
        }
2498
#endif /* defined(COMPILE_MMX2) */
2499
    } // Init Horizontal stuff
2500

    
2501

    
2502

    
2503
    /* precalculate vertical scaler filter coefficients */
2504
    {
2505
        const int filterAlign=
2506
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2507
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2508
            1;
2509

    
2510
        initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2511
                   srcH      ,        dstH, filterAlign, (1<<12),
2512
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2513
                   srcFilter->lumV, dstFilter->lumV, c->param);
2514
        initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2515
                   c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2516
                   (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2517
                   srcFilter->chrV, dstFilter->chrV, c->param);
2518

    
2519
#ifdef HAVE_ALTIVEC
2520
        c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2521
        c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2522

    
2523
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2524
            int j;
2525
            short *p = (short *)&c->vYCoeffsBank[i];
2526
            for (j=0;j<8;j++)
2527
                p[j] = c->vLumFilter[i];
2528
        }
2529

    
2530
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2531
            int j;
2532
            short *p = (short *)&c->vCCoeffsBank[i];
2533
            for (j=0;j<8;j++)
2534
                p[j] = c->vChrFilter[i];
2535
        }
2536
#endif
2537
    }
2538

    
2539
    // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2540
    c->vLumBufSize= c->vLumFilterSize;
2541
    c->vChrBufSize= c->vChrFilterSize;
2542
    for (i=0; i<dstH; i++)
2543
    {
2544
        int chrI= i*c->chrDstH / dstH;
2545
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2546
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2547

    
2548
        nextSlice>>= c->chrSrcVSubSample;
2549
        nextSlice<<= c->chrSrcVSubSample;
2550
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2551
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2552
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2553
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2554
    }
2555

    
2556
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2557
    c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2558
    c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2559
    //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)
2560
    /* align at 16 bytes for AltiVec */
2561
    for (i=0; i<c->vLumBufSize; i++)
2562
        c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2563
    for (i=0; i<c->vChrBufSize; i++)
2564
        c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2565

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

    
2569
    assert(2*VOFW == VOF);
2570

    
2571
    assert(c->chrDstH <= dstH);
2572

    
2573
    if (flags&SWS_PRINT_INFO)
2574
    {
2575
#ifdef DITHER1XBPP
2576
        const char *dither= " dithered";
2577
#else
2578
        const char *dither= "";
2579
#endif
2580
        if (flags&SWS_FAST_BILINEAR)
2581
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2582
        else if (flags&SWS_BILINEAR)
2583
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2584
        else if (flags&SWS_BICUBIC)
2585
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2586
        else if (flags&SWS_X)
2587
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2588
        else if (flags&SWS_POINT)
2589
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2590
        else if (flags&SWS_AREA)
2591
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2592
        else if (flags&SWS_BICUBLIN)
2593
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2594
        else if (flags&SWS_GAUSS)
2595
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2596
        else if (flags&SWS_SINC)
2597
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2598
        else if (flags&SWS_LANCZOS)
2599
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2600
        else if (flags&SWS_SPLINE)
2601
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2602
        else
2603
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2604

    
2605
        if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2606
            av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2607
                   sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2608
        else
2609
            av_log(c, AV_LOG_INFO, "from %s to %s ",
2610
                   sws_format_name(srcFormat), sws_format_name(dstFormat));
2611

    
2612
        if (flags & SWS_CPU_CAPS_MMX2)
2613
            av_log(c, AV_LOG_INFO, "using MMX2\n");
2614
        else if (flags & SWS_CPU_CAPS_3DNOW)
2615
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2616
        else if (flags & SWS_CPU_CAPS_MMX)
2617
            av_log(c, AV_LOG_INFO, "using MMX\n");
2618
        else if (flags & SWS_CPU_CAPS_ALTIVEC)
2619
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
2620
        else
2621
            av_log(c, AV_LOG_INFO, "using C\n");
2622
    }
2623

    
2624
    if (flags & SWS_PRINT_INFO)
2625
    {
2626
        if (flags & SWS_CPU_CAPS_MMX)
2627
        {
2628
            if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2629
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2630
            else
2631
            {
2632
                if (c->hLumFilterSize==4)
2633
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2634
                else if (c->hLumFilterSize==8)
2635
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2636
                else
2637
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2638

    
2639
                if (c->hChrFilterSize==4)
2640
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2641
                else if (c->hChrFilterSize==8)
2642
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2643
                else
2644
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2645
            }
2646
        }
2647
        else
2648
        {
2649
#if defined(ARCH_X86)
2650
            av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
2651
#else
2652
            if (flags & SWS_FAST_BILINEAR)
2653
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2654
            else
2655
                av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2656
#endif
2657
        }
2658
        if (isPlanarYUV(dstFormat))
2659
        {
2660
            if (c->vLumFilterSize==1)
2661
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2662
            else
2663
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2664
        }
2665
        else
2666
        {
2667
            if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2668
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2669
                       "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2670
            else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2671
                av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2672
            else
2673
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2674
        }
2675

    
2676
        if (dstFormat==PIX_FMT_BGR24)
2677
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
2678
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2679
        else if (dstFormat==PIX_FMT_RGB32)
2680
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2681
        else if (dstFormat==PIX_FMT_BGR565)
2682
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2683
        else if (dstFormat==PIX_FMT_BGR555)
2684
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2685

    
2686
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2687
    }
2688
    if (flags & SWS_PRINT_INFO)
2689
    {
2690
        av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2691
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2692
        av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2693
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2694
    }
2695

    
2696
    c->swScale= getSwsFunc(flags);
2697
    return c;
2698
}
2699

    
2700
/**
2701
 * swscale wrapper, so we don't need to export the SwsContext.
2702
 * assumes planar YUV to be in YUV order instead of YVU
2703
 */
2704
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2705
              int srcSliceH, uint8_t* dst[], int dstStride[]){
2706
    int i;
2707
    uint8_t* src2[4]= {src[0], src[1], src[2]};
2708

    
2709
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2710
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2711
        return 0;
2712
    }
2713
    if (c->sliceDir == 0) {
2714
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2715
    }
2716

    
2717
    if (usePal(c->srcFormat)){
2718
        for (i=0; i<256; i++){
2719
            int p, r, g, b,y,u,v;
2720
            if(c->srcFormat == PIX_FMT_PAL8){
2721
                p=((uint32_t*)(src[1]))[i];
2722
                r= (p>>16)&0xFF;
2723
                g= (p>> 8)&0xFF;
2724
                b=  p     &0xFF;
2725
            }else if(c->srcFormat == PIX_FMT_RGB8){
2726
                r= (i>>5    )*36;
2727
                g= ((i>>2)&7)*36;
2728
                b= (i&3     )*85;
2729
            }else if(c->srcFormat == PIX_FMT_BGR8){
2730
                b= (i>>6    )*85;
2731
                g= ((i>>3)&7)*36;
2732
                r= (i&7     )*36;
2733
            }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2734
                r= (i>>3    )*255;
2735
                g= ((i>>1)&3)*85;
2736
                b= (i&1     )*255;
2737
            }else {
2738
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2739
                b= (i>>3    )*255;
2740
                g= ((i>>1)&3)*85;
2741
                r= (i&1     )*255;
2742
            }
2743
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2744
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2745
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2746
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
2747

    
2748

    
2749
            switch(c->dstFormat) {
2750
            case PIX_FMT_BGR32:
2751
#ifndef WORDS_BIGENDIAN
2752
            case PIX_FMT_RGB24:
2753
#endif
2754
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2755
                break;
2756
            case PIX_FMT_BGR32_1:
2757
#ifdef  WORDS_BIGENDIAN
2758
            case PIX_FMT_BGR24:
2759
#endif
2760
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2761
                break;
2762
            case PIX_FMT_RGB32_1:
2763
#ifdef  WORDS_BIGENDIAN
2764
            case PIX_FMT_RGB24:
2765
#endif
2766
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2767
                break;
2768
            case PIX_FMT_RGB32:
2769
#ifndef WORDS_BIGENDIAN
2770
            case PIX_FMT_BGR24:
2771
#endif
2772
            default:
2773
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
2774
            }
2775
        }
2776
    }
2777

    
2778
    // copy strides, so they can safely be modified
2779
    if (c->sliceDir == 1) {
2780
        // slices go from top to bottom
2781
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2782
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2783
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2784
    } else {
2785
        // slices go from bottom to top => we flip the image internally
2786
        uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2787
                           dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2788
                           dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2789
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2790
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2791

    
2792
        src2[0] += (srcSliceH-1)*srcStride[0];
2793
        if (!usePal(c->srcFormat))
2794
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2795
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2796

    
2797
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2798
    }
2799
}
2800

    
2801
/**
2802
 * swscale wrapper, so we don't need to export the SwsContext
2803
 */
2804
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2805
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
2806
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2807
}
2808

    
2809
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2810
                                float lumaSharpen, float chromaSharpen,
2811
                                float chromaHShift, float chromaVShift,
2812
                                int verbose)
2813
{
2814
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2815

    
2816
    if (lumaGBlur!=0.0){
2817
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2818
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2819
    }else{
2820
        filter->lumH= sws_getIdentityVec();
2821
        filter->lumV= sws_getIdentityVec();
2822
    }
2823

    
2824
    if (chromaGBlur!=0.0){
2825
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2826
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2827
    }else{
2828
        filter->chrH= sws_getIdentityVec();
2829
        filter->chrV= sws_getIdentityVec();
2830
    }
2831

    
2832
    if (chromaSharpen!=0.0){
2833
        SwsVector *id= sws_getIdentityVec();
2834
        sws_scaleVec(filter->chrH, -chromaSharpen);
2835
        sws_scaleVec(filter->chrV, -chromaSharpen);
2836
        sws_addVec(filter->chrH, id);
2837
        sws_addVec(filter->chrV, id);
2838
        sws_freeVec(id);
2839
    }
2840

    
2841
    if (lumaSharpen!=0.0){
2842
        SwsVector *id= sws_getIdentityVec();
2843
        sws_scaleVec(filter->lumH, -lumaSharpen);
2844
        sws_scaleVec(filter->lumV, -lumaSharpen);
2845
        sws_addVec(filter->lumH, id);
2846
        sws_addVec(filter->lumV, id);
2847
        sws_freeVec(id);
2848
    }
2849

    
2850
    if (chromaHShift != 0.0)
2851
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2852

    
2853
    if (chromaVShift != 0.0)
2854
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2855

    
2856
    sws_normalizeVec(filter->chrH, 1.0);
2857
    sws_normalizeVec(filter->chrV, 1.0);
2858
    sws_normalizeVec(filter->lumH, 1.0);
2859
    sws_normalizeVec(filter->lumV, 1.0);
2860

    
2861
    if (verbose) sws_printVec(filter->chrH);
2862
    if (verbose) sws_printVec(filter->lumH);
2863

    
2864
    return filter;
2865
}
2866

    
2867
/**
2868
 * returns a normalized gaussian curve used to filter stuff
2869
 * quality=3 is high quality, lowwer is lowwer quality
2870
 */
2871
SwsVector *sws_getGaussianVec(double variance, double quality){
2872
    const int length= (int)(variance*quality + 0.5) | 1;
2873
    int i;
2874
    double *coeff= av_malloc(length*sizeof(double));
2875
    double middle= (length-1)*0.5;
2876
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2877

    
2878
    vec->coeff= coeff;
2879
    vec->length= length;
2880

    
2881
    for (i=0; i<length; i++)
2882
    {
2883
        double dist= i-middle;
2884
        coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
2885
    }
2886

    
2887
    sws_normalizeVec(vec, 1.0);
2888

    
2889
    return vec;
2890
}
2891

    
2892
SwsVector *sws_getConstVec(double c, int length){
2893
    int i;
2894
    double *coeff= av_malloc(length*sizeof(double));
2895
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2896

    
2897
    vec->coeff= coeff;
2898
    vec->length= length;
2899

    
2900
    for (i=0; i<length; i++)
2901
        coeff[i]= c;
2902

    
2903
    return vec;
2904
}
2905

    
2906

    
2907
SwsVector *sws_getIdentityVec(void){
2908
    return sws_getConstVec(1.0, 1);
2909
}
2910

    
2911
double sws_dcVec(SwsVector *a){
2912
    int i;
2913
    double sum=0;
2914

    
2915
    for (i=0; i<a->length; i++)
2916
        sum+= a->coeff[i];
2917

    
2918
    return sum;
2919
}
2920

    
2921
void sws_scaleVec(SwsVector *a, double scalar){
2922
    int i;
2923

    
2924
    for (i=0; i<a->length; i++)
2925
        a->coeff[i]*= scalar;
2926
}
2927

    
2928
void sws_normalizeVec(SwsVector *a, double height){
2929
    sws_scaleVec(a, height/sws_dcVec(a));
2930
}
2931

    
2932
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2933
    int length= a->length + b->length - 1;
2934
    double *coeff= av_malloc(length*sizeof(double));
2935
    int i, j;
2936
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2937

    
2938
    vec->coeff= coeff;
2939
    vec->length= length;
2940

    
2941
    for (i=0; i<length; i++) coeff[i]= 0.0;
2942

    
2943
    for (i=0; i<a->length; i++)
2944
    {
2945
        for (j=0; j<b->length; j++)
2946
        {
2947
            coeff[i+j]+= a->coeff[i]*b->coeff[j];
2948
        }
2949
    }
2950

    
2951
    return vec;
2952
}
2953

    
2954
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2955
    int length= FFMAX(a->length, b->length);
2956
    double *coeff= av_malloc(length*sizeof(double));
2957
    int i;
2958
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2959

    
2960
    vec->coeff= coeff;
2961
    vec->length= length;
2962

    
2963
    for (i=0; i<length; i++) coeff[i]= 0.0;
2964

    
2965
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2966
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2967

    
2968
    return vec;
2969
}
2970

    
2971
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2972
    int length= FFMAX(a->length, b->length);
2973
    double *coeff= av_malloc(length*sizeof(double));
2974
    int i;
2975
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2976

    
2977
    vec->coeff= coeff;
2978
    vec->length= length;
2979

    
2980
    for (i=0; i<length; i++) coeff[i]= 0.0;
2981

    
2982
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2983
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2984

    
2985
    return vec;
2986
}
2987

    
2988
/* shift left / or right if "shift" is negative */
2989
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2990
    int length= a->length + FFABS(shift)*2;
2991
    double *coeff= av_malloc(length*sizeof(double));
2992
    int i;
2993
    SwsVector *vec= av_malloc(sizeof(SwsVector));
2994

    
2995
    vec->coeff= coeff;
2996
    vec->length= length;
2997

    
2998
    for (i=0; i<length; i++) coeff[i]= 0.0;
2999

    
3000
    for (i=0; i<a->length; i++)
3001
    {
3002
        coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3003
    }
3004

    
3005
    return vec;
3006
}
3007

    
3008
void sws_shiftVec(SwsVector *a, int shift){
3009
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3010
    av_free(a->coeff);
3011
    a->coeff= shifted->coeff;
3012
    a->length= shifted->length;
3013
    av_free(shifted);
3014
}
3015

    
3016
void sws_addVec(SwsVector *a, SwsVector *b){
3017
    SwsVector *sum= sws_sumVec(a, b);
3018
    av_free(a->coeff);
3019
    a->coeff= sum->coeff;
3020
    a->length= sum->length;
3021
    av_free(sum);
3022
}
3023

    
3024
void sws_subVec(SwsVector *a, SwsVector *b){
3025
    SwsVector *diff= sws_diffVec(a, b);
3026
    av_free(a->coeff);
3027
    a->coeff= diff->coeff;
3028
    a->length= diff->length;
3029
    av_free(diff);
3030
}
3031

    
3032
void sws_convVec(SwsVector *a, SwsVector *b){
3033
    SwsVector *conv= sws_getConvVec(a, b);
3034
    av_free(a->coeff);
3035
    a->coeff= conv->coeff;
3036
    a->length= conv->length;
3037
    av_free(conv);
3038
}
3039

    
3040
SwsVector *sws_cloneVec(SwsVector *a){
3041
    double *coeff= av_malloc(a->length*sizeof(double));
3042
    int i;
3043
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3044

    
3045
    vec->coeff= coeff;
3046
    vec->length= a->length;
3047

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

    
3050
    return vec;
3051
}
3052

    
3053
void sws_printVec(SwsVector *a){
3054
    int i;
3055
    double max=0;
3056
    double min=0;
3057
    double range;
3058

    
3059
    for (i=0; i<a->length; i++)
3060
        if (a->coeff[i]>max) max= a->coeff[i];
3061

    
3062
    for (i=0; i<a->length; i++)
3063
        if (a->coeff[i]<min) min= a->coeff[i];
3064

    
3065
    range= max - min;
3066

    
3067
    for (i=0; i<a->length; i++)
3068
    {
3069
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3070
        av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
3071
        for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
3072
        av_log(NULL, AV_LOG_DEBUG, "|\n");
3073
    }
3074
}
3075

    
3076
void sws_freeVec(SwsVector *a){
3077
    if (!a) return;
3078
    av_freep(&a->coeff);
3079
    a->length=0;
3080
    av_free(a);
3081
}
3082

    
3083
void sws_freeFilter(SwsFilter *filter){
3084
    if (!filter) return;
3085

    
3086
    if (filter->lumH) sws_freeVec(filter->lumH);
3087
    if (filter->lumV) sws_freeVec(filter->lumV);
3088
    if (filter->chrH) sws_freeVec(filter->chrH);
3089
    if (filter->chrV) sws_freeVec(filter->chrV);
3090
    av_free(filter);
3091
}
3092

    
3093

    
3094
void sws_freeContext(SwsContext *c){
3095
    int i;
3096
    if (!c) return;
3097

    
3098
    if (c->lumPixBuf)
3099
    {
3100
        for (i=0; i<c->vLumBufSize; i++)
3101
            av_freep(&c->lumPixBuf[i]);
3102
        av_freep(&c->lumPixBuf);
3103
    }
3104

    
3105
    if (c->chrPixBuf)
3106
    {
3107
        for (i=0; i<c->vChrBufSize; i++)
3108
            av_freep(&c->chrPixBuf[i]);
3109
        av_freep(&c->chrPixBuf);
3110
    }
3111

    
3112
    av_freep(&c->vLumFilter);
3113
    av_freep(&c->vChrFilter);
3114
    av_freep(&c->hLumFilter);
3115
    av_freep(&c->hChrFilter);
3116
#ifdef HAVE_ALTIVEC
3117
    av_freep(&c->vYCoeffsBank);
3118
    av_freep(&c->vCCoeffsBank);
3119
#endif
3120

    
3121
    av_freep(&c->vLumFilterPos);
3122
    av_freep(&c->vChrFilterPos);
3123
    av_freep(&c->hLumFilterPos);
3124
    av_freep(&c->hChrFilterPos);
3125

    
3126
#if defined(ARCH_X86) && defined(CONFIG_GPL)
3127
#ifdef MAP_ANONYMOUS
3128
    if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
3129
    if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3130
#else
3131
    av_free(c->funnyYCode);
3132
    av_free(c->funnyUVCode);
3133
#endif
3134
    c->funnyYCode=NULL;
3135
    c->funnyUVCode=NULL;
3136
#endif /* defined(ARCH_X86) */
3137

    
3138
    av_freep(&c->lumMmx2Filter);
3139
    av_freep(&c->chrMmx2Filter);
3140
    av_freep(&c->lumMmx2FilterPos);
3141
    av_freep(&c->chrMmx2FilterPos);
3142
    av_freep(&c->yuvTable);
3143

    
3144
    av_free(c);
3145
}
3146

    
3147
/**
3148
 * Checks if context is valid or reallocs a new one instead.
3149
 * If context is NULL, just calls sws_getContext() to get a new one.
3150
 * Otherwise, checks if the parameters are the same already saved in context.
3151
 * If that is the case, returns the current context.
3152
 * Otherwise, frees context and gets a new one.
3153
 *
3154
 * Be warned that srcFilter, dstFilter are not checked, they are
3155
 * asumed to remain valid.
3156
 */
3157
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3158
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3159
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3160
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
3161
{
3162
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3163

    
3164
    if (!param)
3165
        param = default_param;
3166

    
3167
    if (context) {
3168
        if (context->srcW != srcW || context->srcH != srcH ||
3169
            context->srcFormat != srcFormat ||
3170
            context->dstW != dstW || context->dstH != dstH ||
3171
            context->dstFormat != dstFormat || context->flags != flags ||
3172
            context->param[0] != param[0] || context->param[1] != param[1])
3173
        {
3174
            sws_freeContext(context);
3175
            context = NULL;
3176
        }
3177
    }
3178
    if (!context) {
3179
        return sws_getContext(srcW, srcH, srcFormat,
3180
                              dstW, dstH, dstFormat, flags,
3181
                              srcFilter, dstFilter, param);
3182
    }
3183
    return context;
3184
}
3185