Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ a3e35e28

History | View | Annotate | Download (123 KB)

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

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

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

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

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

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

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

    
88
const char *swscale_configuration(void)
89
{
90
    return FFMPEG_CONFIGURATION;
91
}
92

    
93
const char *swscale_license(void)
94
{
95
#define LICENSE_PREFIX "libswscale license: "
96
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
97
}
98

    
99
#undef MOVNTQ
100
#undef PAVGB
101

    
102
//#undef HAVE_MMX2
103
//#define HAVE_AMD3DNOW
104
//#undef HAVE_MMX
105
//#undef ARCH_X86
106
#define DITHER1XBPP
107

    
108
#define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
109

    
110
#define RET 0xC3 //near return opcode for x86
111

    
112
#ifdef M_PI
113
#define PI M_PI
114
#else
115
#define PI 3.14159265358979323846
116
#endif
117

    
118
#define isSupportedIn(x)    (       \
119
           (x)==PIX_FMT_YUV420P     \
120
        || (x)==PIX_FMT_YUVA420P    \
121
        || (x)==PIX_FMT_YUYV422     \
122
        || (x)==PIX_FMT_UYVY422     \
123
        || (x)==PIX_FMT_RGB48BE     \
124
        || (x)==PIX_FMT_RGB48LE     \
125
        || (x)==PIX_FMT_RGB32       \
126
        || (x)==PIX_FMT_RGB32_1     \
127
        || (x)==PIX_FMT_BGR24       \
128
        || (x)==PIX_FMT_BGR565      \
129
        || (x)==PIX_FMT_BGR555      \
130
        || (x)==PIX_FMT_BGR32       \
131
        || (x)==PIX_FMT_BGR32_1     \
132
        || (x)==PIX_FMT_RGB24       \
133
        || (x)==PIX_FMT_RGB565      \
134
        || (x)==PIX_FMT_RGB555      \
135
        || (x)==PIX_FMT_GRAY8       \
136
        || (x)==PIX_FMT_YUV410P     \
137
        || (x)==PIX_FMT_YUV440P     \
138
        || (x)==PIX_FMT_NV12        \
139
        || (x)==PIX_FMT_NV21        \
140
        || (x)==PIX_FMT_GRAY16BE    \
141
        || (x)==PIX_FMT_GRAY16LE    \
142
        || (x)==PIX_FMT_YUV444P     \
143
        || (x)==PIX_FMT_YUV422P     \
144
        || (x)==PIX_FMT_YUV411P     \
145
        || (x)==PIX_FMT_PAL8        \
146
        || (x)==PIX_FMT_BGR8        \
147
        || (x)==PIX_FMT_RGB8        \
148
        || (x)==PIX_FMT_BGR4_BYTE   \
149
        || (x)==PIX_FMT_RGB4_BYTE   \
150
        || (x)==PIX_FMT_YUV440P     \
151
        || (x)==PIX_FMT_MONOWHITE   \
152
        || (x)==PIX_FMT_MONOBLACK   \
153
        || (x)==PIX_FMT_YUV420P16LE   \
154
        || (x)==PIX_FMT_YUV422P16LE   \
155
        || (x)==PIX_FMT_YUV444P16LE   \
156
        || (x)==PIX_FMT_YUV420P16BE   \
157
        || (x)==PIX_FMT_YUV422P16BE   \
158
        || (x)==PIX_FMT_YUV444P16BE   \
159
    )
160

    
161
int sws_isSupportedInput(enum PixelFormat pix_fmt)
162
{
163
    return isSupportedIn(pix_fmt);
164
}
165

    
166
#define isSupportedOut(x)   (       \
167
           (x)==PIX_FMT_YUV420P     \
168
        || (x)==PIX_FMT_YUVA420P    \
169
        || (x)==PIX_FMT_YUYV422     \
170
        || (x)==PIX_FMT_UYVY422     \
171
        || (x)==PIX_FMT_YUV444P     \
172
        || (x)==PIX_FMT_YUV422P     \
173
        || (x)==PIX_FMT_YUV411P     \
174
        || isRGB(x)                 \
175
        || isBGR(x)                 \
176
        || (x)==PIX_FMT_NV12        \
177
        || (x)==PIX_FMT_NV21        \
178
        || (x)==PIX_FMT_GRAY16BE    \
179
        || (x)==PIX_FMT_GRAY16LE    \
180
        || (x)==PIX_FMT_GRAY8       \
181
        || (x)==PIX_FMT_YUV410P     \
182
        || (x)==PIX_FMT_YUV440P     \
183
        || (x)==PIX_FMT_YUV420P16LE   \
184
        || (x)==PIX_FMT_YUV422P16LE   \
185
        || (x)==PIX_FMT_YUV444P16LE   \
186
        || (x)==PIX_FMT_YUV420P16BE   \
187
        || (x)==PIX_FMT_YUV422P16BE   \
188
        || (x)==PIX_FMT_YUV444P16BE   \
189
    )
190

    
191
int sws_isSupportedOutput(enum PixelFormat pix_fmt)
192
{
193
    return isSupportedOut(pix_fmt);
194
}
195

    
196
#define isPacked(x)         (       \
197
           (x)==PIX_FMT_PAL8        \
198
        || (x)==PIX_FMT_YUYV422     \
199
        || (x)==PIX_FMT_UYVY422     \
200
        || isRGB(x)                 \
201
        || isBGR(x)                 \
202
    )
203
#define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
204

    
205
#define RGB2YUV_SHIFT 15
206
#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
207
#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
208
#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
209
#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
210
#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
211
#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
212
#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
213
#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
214
#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
215

    
216
extern const int32_t ff_yuv2rgb_coeffs[8][4];
217

    
218
static const double rgb2yuv_table[8][9]={
219
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
220
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
221
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
222
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
223
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
224
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
225
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
226
    {0.701 , 0.087 , 0.212 , -0.384, 0.5  -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
227
};
228

    
229
/*
230
NOTES
231
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
232

233
TODO
234
more intelligent misalignment avoidance for the horizontal scaler
235
write special vertical cubic upscale version
236
optimize C code (YV12 / minmax)
237
add support for packed pixel YUV input & output
238
add support for Y8 output
239
optimize BGR24 & BGR32
240
add BGR4 output support
241
write special BGR->BGR scaler
242
*/
243

    
244
#if ARCH_X86 && CONFIG_GPL
245
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
246
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
247
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
248
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
249
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
250
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
251
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
252
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
253

    
254
const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
255
        0x0103010301030103LL,
256
        0x0200020002000200LL,};
257

    
258
const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
259
        0x0602060206020602LL,
260
        0x0004000400040004LL,};
261

    
262
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
263
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
264
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
265
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
266
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
267
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
268

    
269
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
270
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
271
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
272

    
273
#ifdef FAST_BGR2YV12
274
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
275
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
276
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
277
#else
278
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
279
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
280
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
281
#endif /* FAST_BGR2YV12 */
282
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
283
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
284
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
285

    
286
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
287
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
288
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
289
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
290
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
291

    
292
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
293
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
294
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
295
};
296

    
297
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
298

    
299
#endif /* ARCH_X86 && CONFIG_GPL */
300

    
301
// clipping helper table for C implementations:
302
static unsigned char clip_table[768];
303

    
304
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
305

    
306
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
307
{  1,   3,   1,   3,   1,   3,   1,   3, },
308
{  2,   0,   2,   0,   2,   0,   2,   0, },
309
};
310

    
311
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
312
{  6,   2,   6,   2,   6,   2,   6,   2, },
313
{  0,   4,   0,   4,   0,   4,   0,   4, },
314
};
315

    
316
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
317
{ 17,   9,  23,  15,  16,   8,  22,  14, },
318
{  5,  29,   3,  27,   4,  28,   2,  26, },
319
{ 21,  13,  19,  11,  20,  12,  18,  10, },
320
{  0,  24,   6,  30,   1,  25,   7,  31, },
321
{ 16,   8,  22,  14,  17,   9,  23,  15, },
322
{  4,  28,   2,  26,   5,  29,   3,  27, },
323
{ 20,  12,  18,  10,  21,  13,  19,  11, },
324
{  1,  25,   7,  31,   0,  24,   6,  30, },
325
};
326

    
327
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
328
{  0,  55,  14,  68,   3,  58,  17,  72, },
329
{ 37,  18,  50,  32,  40,  22,  54,  35, },
330
{  9,  64,   5,  59,  13,  67,   8,  63, },
331
{ 46,  27,  41,  23,  49,  31,  44,  26, },
332
{  2,  57,  16,  71,   1,  56,  15,  70, },
333
{ 39,  21,  52,  34,  38,  19,  51,  33, },
334
{ 11,  66,   7,  62,  10,  65,   6,  60, },
335
{ 48,  30,  43,  25,  47,  29,  42,  24, },
336
};
337

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

    
387
const char *sws_format_name(enum PixelFormat format)
388
{
389
    if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
390
        return av_pix_fmt_descriptors[format].name;
391
    else
392
        return "Unknown format";
393
}
394

    
395
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
396
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
397
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
398
                                                    int dstW, int chrDstW, int big_endian)
399
{
400
    //FIXME Optimize (just quickly written not optimized..)
401
    int i;
402

    
403
    for (i = 0; i < dstW; i++) {
404
        int val = 1 << 10;
405
        int j;
406

    
407
        for (j = 0; j < lumFilterSize; j++)
408
            val += lumSrc[j][i] * lumFilter[j];
409

    
410
        if (big_endian) {
411
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
412
        } else {
413
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
414
        }
415
    }
416

    
417
    if (uDest) {
418
        for (i = 0; i < chrDstW; i++) {
419
            int u = 1 << 10;
420
            int v = 1 << 10;
421
            int j;
422

    
423
            for (j = 0; j < chrFilterSize; j++) {
424
                u += chrSrc[j][i       ] * chrFilter[j];
425
                v += chrSrc[j][i + VOFW] * chrFilter[j];
426
            }
427

    
428
            if (big_endian) {
429
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
430
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
431
            } else {
432
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
433
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
434
            }
435
        }
436
    }
437

    
438
    if (CONFIG_SWSCALE_ALPHA && aDest) {
439
        for (i = 0; i < dstW; i++) {
440
            int val = 1 << 10;
441
            int j;
442

    
443
            for (j = 0; j < lumFilterSize; j++)
444
                val += alpSrc[j][i] * lumFilter[j];
445

    
446
            if (big_endian) {
447
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
448
            } else {
449
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
450
            }
451
        }
452
    }
453
}
454

    
455
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
456
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
457
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
458
                                 enum PixelFormat dstFormat)
459
{
460
    if (isBE(dstFormat)) {
461
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
462
                               chrFilter, chrSrc, chrFilterSize,
463
                               alpSrc,
464
                               dest, uDest, vDest, aDest,
465
                               dstW, chrDstW, 1);
466
    } else {
467
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
468
                               chrFilter, chrSrc, chrFilterSize,
469
                               alpSrc,
470
                               dest, uDest, vDest, aDest,
471
                               dstW, chrDstW, 0);
472
    }
473
}
474

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

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

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

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

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

    
511
            aDest[i]= av_clip_uint8(val>>19);
512
        }
513

    
514
}
515

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

    
528
        dest[i]= av_clip_uint8(val>>19);
529
    }
530

    
531
    if (!uDest)
532
        return;
533

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

    
544
            uDest[2*i]= av_clip_uint8(u>>19);
545
            uDest[2*i+1]= av_clip_uint8(v>>19);
546
        }
547
    else
548
        for (i=0; i<chrDstW; i++) {
549
            int u=1<<18;
550
            int v=1<<18;
551
            int j;
552
            for (j=0; j<chrFilterSize; j++) {
553
                u += chrSrc[j][i] * chrFilter[j];
554
                v += chrSrc[j][i + VOFW] * chrFilter[j];
555
            }
556

    
557
            uDest[2*i]= av_clip_uint8(v>>19);
558
            uDest[2*i+1]= av_clip_uint8(u>>19);
559
        }
560
}
561

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

    
596
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
597
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
598
        if ((Y1|Y2|U|V)&256) {\
599
            if (Y1>255)   Y1=255; \
600
            else if (Y1<0)Y1=0;   \
601
            if (Y2>255)   Y2=255; \
602
            else if (Y2<0)Y2=0;   \
603
            if (U>255)    U=255;  \
604
            else if (U<0) U=0;    \
605
            if (V>255)    V=255;  \
606
            else if (V<0) V=0;    \
607
        }\
608
        if (alpha && ((A1|A2)&256)) {\
609
            A1=av_clip_uint8(A1);\
610
            A2=av_clip_uint8(A2);\
611
        }
612

    
613
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
614
    for (i=0; i<dstW; i++) {\
615
        int j;\
616
        int Y = 0;\
617
        int U = -128<<19;\
618
        int V = -128<<19;\
619
        int av_unused A;\
620
        int R,G,B;\
621
        \
622
        for (j=0; j<lumFilterSize; j++) {\
623
            Y += lumSrc[j][i     ] * lumFilter[j];\
624
        }\
625
        for (j=0; j<chrFilterSize; j++) {\
626
            U += chrSrc[j][i     ] * chrFilter[j];\
627
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
628
        }\
629
        Y >>=10;\
630
        U >>=10;\
631
        V >>=10;\
632
        if (alpha) {\
633
            A = rnd;\
634
            for (j=0; j<lumFilterSize; j++)\
635
                A += alpSrc[j][i     ] * lumFilter[j];\
636
            A >>=19;\
637
            if (A&256)\
638
                A = av_clip_uint8(A);\
639
        }\
640

    
641
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
642
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
643
        Y-= c->yuv2rgb_y_offset;\
644
        Y*= c->yuv2rgb_y_coeff;\
645
        Y+= rnd;\
646
        R= Y + V*c->yuv2rgb_v2r_coeff;\
647
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
648
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
649
        if ((R|G|B)&(0xC0000000)) {\
650
            if (R>=(256<<22))   R=(256<<22)-1; \
651
            else if (R<0)R=0;   \
652
            if (G>=(256<<22))   G=(256<<22)-1; \
653
            else if (G<0)G=0;   \
654
            if (B>=(256<<22))   B=(256<<22)-1; \
655
            else if (B<0)B=0;   \
656
        }\
657

    
658

    
659
#define YSCALE_YUV_2_GRAY16_C \
660
    for (i=0; i<(dstW>>1); i++) {\
661
        int j;\
662
        int Y1 = 1<<18;\
663
        int Y2 = 1<<18;\
664
        int U  = 1<<18;\
665
        int V  = 1<<18;\
666
        \
667
        const int i2= 2*i;\
668
        \
669
        for (j=0; j<lumFilterSize; j++) {\
670
            Y1 += lumSrc[j][i2] * lumFilter[j];\
671
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
672
        }\
673
        Y1>>=11;\
674
        Y2>>=11;\
675
        if ((Y1|Y2|U|V)&65536) {\
676
            if (Y1>65535)   Y1=65535; \
677
            else if (Y1<0)Y1=0;   \
678
            if (Y2>65535)   Y2=65535; \
679
            else if (Y2<0)Y2=0;   \
680
        }
681

    
682
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
683
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
684
    r = (type *)c->table_rV[V];   \
685
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
686
    b = (type *)c->table_bU[U];   \
687

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

    
702
#define YSCALE_YUV_2_GRAY16_2_C   \
703
    for (i=0; i<(dstW>>1); i++) { \
704
        const int i2= 2*i;       \
705
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
706
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
707

    
708
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
709
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
710
    r = (type *)c->table_rV[V];\
711
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
712
    b = (type *)c->table_bU[U];\
713

    
714
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
715
    for (i=0; i<(dstW>>1); i++) {\
716
        const int i2= 2*i;\
717
        int Y1= buf0[i2  ]>>7;\
718
        int Y2= buf0[i2+1]>>7;\
719
        int U= (uvbuf1[i     ])>>7;\
720
        int V= (uvbuf1[i+VOFW])>>7;\
721
        type av_unused *r, *b, *g;\
722
        int av_unused A1, A2;\
723
        if (alpha) {\
724
            A1= abuf0[i2  ]>>7;\
725
            A2= abuf0[i2+1]>>7;\
726
        }\
727

    
728
#define YSCALE_YUV_2_GRAY16_1_C \
729
    for (i=0; i<(dstW>>1); i++) {\
730
        const int i2= 2*i;\
731
        int Y1= buf0[i2  ]<<1;\
732
        int Y2= buf0[i2+1]<<1;\
733

    
734
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
735
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
736
    r = (type *)c->table_rV[V];\
737
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
738
    b = (type *)c->table_bU[U];\
739

    
740
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
741
    for (i=0; i<(dstW>>1); i++) {\
742
        const int i2= 2*i;\
743
        int Y1= buf0[i2  ]>>7;\
744
        int Y2= buf0[i2+1]>>7;\
745
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
746
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
747
        type av_unused *r, *b, *g;\
748
        int av_unused A1, A2;\
749
        if (alpha) {\
750
            A1= abuf0[i2  ]>>7;\
751
            A2= abuf0[i2+1]>>7;\
752
        }\
753

    
754
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
755
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
756
    r = (type *)c->table_rV[V];\
757
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
758
    b = (type *)c->table_bU[U];\
759

    
760
#define YSCALE_YUV_2_MONO2_C \
761
    const uint8_t * const d128=dither_8x8_220[y&7];\
762
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
763
    for (i=0; i<dstW-7; i+=8) {\
764
        int acc;\
765
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
766
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
767
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
768
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
769
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
770
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
771
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
772
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
773
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
774
        dest++;\
775
    }\
776

    
777

    
778
#define YSCALE_YUV_2_MONOX_C \
779
    const uint8_t * const d128=dither_8x8_220[y&7];\
780
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
781
    int acc=0;\
782
    for (i=0; i<dstW-1; i+=2) {\
783
        int j;\
784
        int Y1=1<<18;\
785
        int Y2=1<<18;\
786
\
787
        for (j=0; j<lumFilterSize; j++) {\
788
            Y1 += lumSrc[j][i] * lumFilter[j];\
789
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
790
        }\
791
        Y1>>=19;\
792
        Y2>>=19;\
793
        if ((Y1|Y2)&256) {\
794
            if (Y1>255)   Y1=255;\
795
            else if (Y1<0)Y1=0;\
796
            if (Y2>255)   Y2=255;\
797
            else if (Y2<0)Y2=0;\
798
        }\
799
        acc+= acc + g[Y1+d128[(i+0)&7]];\
800
        acc+= acc + g[Y2+d128[(i+1)&7]];\
801
        if ((i&7)==6) {\
802
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
803
            dest++;\
804
        }\
805
    }
806

    
807

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

    
997

    
998
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
999
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1000
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1001
{
1002
    int i;
1003
    YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
1004
}
1005

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

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

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

    
1100
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
1101
                            uint32_t *unused)
1102
{
1103
    int i;
1104
    for (i = 0; i < width; i++) {
1105
        int r = src[i*6+0];
1106
        int g = src[i*6+2];
1107
        int b = src[i*6+4];
1108

    
1109
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1110
    }
1111
}
1112

    
1113
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1114
                             uint8_t *src1, uint8_t *src2, int width,
1115
                             uint32_t *unused)
1116
{
1117
    int i;
1118
    assert(src1==src2);
1119
    for (i = 0; i < width; i++) {
1120
        int r = src1[6*i + 0];
1121
        int g = src1[6*i + 2];
1122
        int b = src1[6*i + 4];
1123

    
1124
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1125
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1126
    }
1127
}
1128

    
1129
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1130
                                  uint8_t *src1, uint8_t *src2, int width,
1131
                                  uint32_t *unused)
1132
{
1133
    int i;
1134
    assert(src1==src2);
1135
    for (i = 0; i < width; i++) {
1136
        int r= src1[12*i + 0] + src1[12*i + 6];
1137
        int g= src1[12*i + 2] + src1[12*i + 8];
1138
        int b= src1[12*i + 4] + src1[12*i + 10];
1139

    
1140
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1141
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1142
    }
1143
}
1144

    
1145
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1146
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1147
{\
1148
    int i;\
1149
    for (i=0; i<width; i++) {\
1150
        int b= (((const type*)src)[i]>>shb)&maskb;\
1151
        int g= (((const type*)src)[i]>>shg)&maskg;\
1152
        int r= (((const type*)src)[i]>>shr)&maskr;\
1153
\
1154
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1155
    }\
1156
}
1157

    
1158
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1159
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1160
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1161
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1162
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1163
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1164

    
1165
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1166
{
1167
    int i;
1168
    for (i=0; i<width; i++) {
1169
        dst[i]= src[4*i];
1170
    }
1171
}
1172

    
1173
#define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1174
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1175
{\
1176
    int i;\
1177
    for (i=0; i<width; i++) {\
1178
        int b= (((const type*)src)[i]&maskb)>>shb;\
1179
        int g= (((const type*)src)[i]&maskg)>>shg;\
1180
        int r= (((const type*)src)[i]&maskr)>>shr;\
1181
\
1182
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1183
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1184
    }\
1185
}\
1186
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1187
{\
1188
    int i;\
1189
    for (i=0; i<width; i++) {\
1190
        int pix0= ((const type*)src)[2*i+0];\
1191
        int pix1= ((const type*)src)[2*i+1];\
1192
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1193
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1194
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1195
        g&= maskg|(2*maskg);\
1196
\
1197
        g>>=shg;\
1198
\
1199
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1200
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1201
    }\
1202
}
1203

    
1204
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1205
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1206
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0,          0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1207
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0,          0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1208
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0,          0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1209
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0,          0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1210

    
1211
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1212
{
1213
    int i;
1214
    for (i=0; i<width; i++) {
1215
        int d= src[i];
1216

    
1217
        dst[i]= pal[d] & 0xFF;
1218
    }
1219
}
1220

    
1221
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1222
                           const uint8_t *src1, const uint8_t *src2,
1223
                           long width, uint32_t *pal)
1224
{
1225
    int i;
1226
    assert(src1 == src2);
1227
    for (i=0; i<width; i++) {
1228
        int p= pal[src1[i]];
1229

    
1230
        dstU[i]= p>>8;
1231
        dstV[i]= p>>16;
1232
    }
1233
}
1234

    
1235
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1236
{
1237
    int i, j;
1238
    for (i=0; i<width/8; i++) {
1239
        int d= ~src[i];
1240
        for(j=0; j<8; j++)
1241
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1242
    }
1243
}
1244

    
1245
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1246
{
1247
    int i, j;
1248
    for (i=0; i<width/8; i++) {
1249
        int d= src[i];
1250
        for(j=0; j<8; j++)
1251
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1252
    }
1253
}
1254

    
1255

    
1256
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1257
//Plain C versions
1258
#if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1259
#define COMPILE_C
1260
#endif
1261

    
1262
#if ARCH_PPC
1263
#if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1264
#define COMPILE_ALTIVEC
1265
#endif
1266
#endif //ARCH_PPC
1267

    
1268
#if ARCH_X86
1269

    
1270
#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1271
#define COMPILE_MMX
1272
#endif
1273

    
1274
#if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1275
#define COMPILE_MMX2
1276
#endif
1277

    
1278
#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1279
#define COMPILE_3DNOW
1280
#endif
1281
#endif //ARCH_X86
1282

    
1283
#define COMPILE_TEMPLATE_MMX 0
1284
#define COMPILE_TEMPLATE_MMX2 0
1285
#define COMPILE_TEMPLATE_AMD3DNOW 0
1286
#define COMPILE_TEMPLATE_ALTIVEC 0
1287

    
1288
#ifdef COMPILE_C
1289
#define RENAME(a) a ## _C
1290
#include "swscale_template.c"
1291
#endif
1292

    
1293
#ifdef COMPILE_ALTIVEC
1294
#undef RENAME
1295
#undef COMPILE_TEMPLATE_ALTIVEC
1296
#define COMPILE_TEMPLATE_ALTIVEC 1
1297
#define RENAME(a) a ## _altivec
1298
#include "swscale_template.c"
1299
#endif
1300

    
1301
#if ARCH_X86
1302

    
1303
//MMX versions
1304
#ifdef COMPILE_MMX
1305
#undef RENAME
1306
#undef COMPILE_TEMPLATE_MMX
1307
#undef COMPILE_TEMPLATE_MMX2
1308
#undef COMPILE_TEMPLATE_AMD3DNOW
1309
#define COMPILE_TEMPLATE_MMX 1
1310
#define COMPILE_TEMPLATE_MMX2 0
1311
#define COMPILE_TEMPLATE_AMD3DNOW 0
1312
#define RENAME(a) a ## _MMX
1313
#include "swscale_template.c"
1314
#endif
1315

    
1316
//MMX2 versions
1317
#ifdef COMPILE_MMX2
1318
#undef RENAME
1319
#undef COMPILE_TEMPLATE_MMX
1320
#undef COMPILE_TEMPLATE_MMX2
1321
#undef COMPILE_TEMPLATE_AMD3DNOW
1322
#define COMPILE_TEMPLATE_MMX 1
1323
#define COMPILE_TEMPLATE_MMX2 1
1324
#define COMPILE_TEMPLATE_AMD3DNOW 0
1325
#define RENAME(a) a ## _MMX2
1326
#include "swscale_template.c"
1327
#endif
1328

    
1329
//3DNOW versions
1330
#ifdef COMPILE_3DNOW
1331
#undef RENAME
1332
#undef COMPILE_TEMPLATE_MMX
1333
#undef COMPILE_TEMPLATE_MMX2
1334
#undef COMPILE_TEMPLATE_AMD3DNOW
1335
#define COMPILE_TEMPLATE_MMX 1
1336
#define COMPILE_TEMPLATE_MMX2 0
1337
#define COMPILE_TEMPLATE_AMD3DNOW 1
1338
#define RENAME(a) a ## _3DNow
1339
#include "swscale_template.c"
1340
#endif
1341

    
1342
#endif //ARCH_X86
1343

    
1344
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1345
{
1346
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1347
    if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1348
    else           return getSplineCoeff(        0.0,
1349
                                          b+ 2.0*c + 3.0*d,
1350
                                                 c + 3.0*d,
1351
                                         -b- 3.0*c - 6.0*d,
1352
                                         dist-1.0);
1353
}
1354

    
1355
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1356
                             int srcW, int dstW, int filterAlign, int one, int flags,
1357
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1358
{
1359
    int i;
1360
    int filterSize;
1361
    int filter2Size;
1362
    int minFilterSize;
1363
    int64_t *filter=NULL;
1364
    int64_t *filter2=NULL;
1365
    const int64_t fone= 1LL<<54;
1366
    int ret= -1;
1367
#if ARCH_X86
1368
    if (flags & SWS_CPU_CAPS_MMX)
1369
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1370
#endif
1371

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

    
1375
    if (FFABS(xInc - 0x10000) <10) { // unscaled
1376
        int i;
1377
        filterSize= 1;
1378
        FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1379

    
1380
        for (i=0; i<dstW; i++) {
1381
            filter[i*filterSize]= fone;
1382
            (*filterPos)[i]=i;
1383
        }
1384

    
1385
    } else if (flags&SWS_POINT) { // lame looking point sampling mode
1386
        int i;
1387
        int xDstInSrc;
1388
        filterSize= 1;
1389
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1390

    
1391
        xDstInSrc= xInc/2 - 0x8000;
1392
        for (i=0; i<dstW; i++) {
1393
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1394

    
1395
            (*filterPos)[i]= xx;
1396
            filter[i]= fone;
1397
            xDstInSrc+= xInc;
1398
        }
1399
    } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1400
        int i;
1401
        int xDstInSrc;
1402
        filterSize= 2;
1403
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1404

    
1405
        xDstInSrc= xInc/2 - 0x8000;
1406
        for (i=0; i<dstW; i++) {
1407
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1408
            int j;
1409

    
1410
            (*filterPos)[i]= xx;
1411
            //bilinear upscale / linear interpolate / area averaging
1412
            for (j=0; j<filterSize; j++) {
1413
                int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1414
                if (coeff<0) coeff=0;
1415
                filter[i*filterSize + j]= coeff;
1416
                xx++;
1417
            }
1418
            xDstInSrc+= xInc;
1419
        }
1420
    } else {
1421
        int xDstInSrc;
1422
        int sizeFactor;
1423

    
1424
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1425
        else if (flags&SWS_X)            sizeFactor=  8;
1426
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1427
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1428
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1429
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1430
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1431
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1432
        else {
1433
            sizeFactor= 0; //GCC warning killer
1434
            assert(0);
1435
        }
1436

    
1437
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1438
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1439

    
1440
        if (filterSize > srcW-2) filterSize=srcW-2;
1441

    
1442
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1443

    
1444
        xDstInSrc= xInc - 0x10000;
1445
        for (i=0; i<dstW; i++) {
1446
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1447
            int j;
1448
            (*filterPos)[i]= xx;
1449
            for (j=0; j<filterSize; j++) {
1450
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1451
                double floatd;
1452
                int64_t coeff;
1453

    
1454
                if (xInc > 1<<16)
1455
                    d= d*dstW/srcW;
1456
                floatd= d * (1.0/(1<<30));
1457

    
1458
                if (flags & SWS_BICUBIC) {
1459
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1460
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1461
                    int64_t dd = ( d*d)>>30;
1462
                    int64_t ddd= (dd*d)>>30;
1463

    
1464
                    if      (d < 1LL<<30)
1465
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1466
                    else if (d < 1LL<<31)
1467
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1468
                    else
1469
                        coeff=0.0;
1470
                    coeff *= fone>>(30+24);
1471
                }
1472
/*                else if (flags & SWS_X) {
1473
                    double p= param ? param*0.01 : 0.3;
1474
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1475
                    coeff*= pow(2.0, - p*d*d);
1476
                }*/
1477
                else if (flags & SWS_X) {
1478
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1479
                    double c;
1480

    
1481
                    if (floatd<1.0)
1482
                        c = cos(floatd*PI);
1483
                    else
1484
                        c=-1.0;
1485
                    if (c<0.0)      c= -pow(-c, A);
1486
                    else            c=  pow( c, A);
1487
                    coeff= (c*0.5 + 0.5)*fone;
1488
                } else if (flags & SWS_AREA) {
1489
                    int64_t d2= d - (1<<29);
1490
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1491
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1492
                    else coeff=0.0;
1493
                    coeff *= fone>>(30+16);
1494
                } else if (flags & SWS_GAUSS) {
1495
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1496
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
1497
                } else if (flags & SWS_SINC) {
1498
                    coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1499
                } else if (flags & SWS_LANCZOS) {
1500
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1501
                    coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1502
                    if (floatd>p) coeff=0;
1503
                } else if (flags & SWS_BILINEAR) {
1504
                    coeff= (1<<30) - d;
1505
                    if (coeff<0) coeff=0;
1506
                    coeff *= fone >> 30;
1507
                } else if (flags & SWS_SPLINE) {
1508
                    double p=-2.196152422706632;
1509
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1510
                } else {
1511
                    coeff= 0.0; //GCC warning killer
1512
                    assert(0);
1513
                }
1514

    
1515
                filter[i*filterSize + j]= coeff;
1516
                xx++;
1517
            }
1518
            xDstInSrc+= 2*xInc;
1519
        }
1520
    }
1521

    
1522
    /* apply src & dst Filter to filter -> filter2
1523
       av_free(filter);
1524
    */
1525
    assert(filterSize>0);
1526
    filter2Size= filterSize;
1527
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1528
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1529
    assert(filter2Size>0);
1530
    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
1531

    
1532
    for (i=0; i<dstW; i++) {
1533
        int j, k;
1534

    
1535
        if(srcFilter) {
1536
            for (k=0; k<srcFilter->length; k++) {
1537
                for (j=0; j<filterSize; j++)
1538
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1539
            }
1540
        } else {
1541
            for (j=0; j<filterSize; j++)
1542
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1543
        }
1544
        //FIXME dstFilter
1545

    
1546
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1547
    }
1548
    av_freep(&filter);
1549

    
1550
    /* try to reduce the filter-size (step1 find size and shift left) */
1551
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1552
    minFilterSize= 0;
1553
    for (i=dstW-1; i>=0; i--) {
1554
        int min= filter2Size;
1555
        int j;
1556
        int64_t cutOff=0.0;
1557

    
1558
        /* get rid off near zero elements on the left by shifting left */
1559
        for (j=0; j<filter2Size; j++) {
1560
            int k;
1561
            cutOff += FFABS(filter2[i*filter2Size]);
1562

    
1563
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1564

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

    
1568
            // move filter coefficients left
1569
            for (k=1; k<filter2Size; k++)
1570
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1571
            filter2[i*filter2Size + k - 1]= 0;
1572
            (*filterPos)[i]++;
1573
        }
1574

    
1575
        cutOff=0;
1576
        /* count near zeros on the right */
1577
        for (j=filter2Size-1; j>0; j--) {
1578
            cutOff += FFABS(filter2[i*filter2Size + j]);
1579

    
1580
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1581
            min--;
1582
        }
1583

    
1584
        if (min>minFilterSize) minFilterSize= min;
1585
    }
1586

    
1587
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1588
        // we can handle the special case 4,
1589
        // so we don't want to go to the full 8
1590
        if (minFilterSize < 5)
1591
            filterAlign = 4;
1592

    
1593
        // We really don't want to waste our time
1594
        // doing useless computation, so fall back on
1595
        // the scalar C code for very small filters.
1596
        // Vectorizing is worth it only if you have a
1597
        // decent-sized vector.
1598
        if (minFilterSize < 3)
1599
            filterAlign = 1;
1600
    }
1601

    
1602
    if (flags & SWS_CPU_CAPS_MMX) {
1603
        // special case for unscaled vertical filtering
1604
        if (minFilterSize == 1 && filterAlign == 2)
1605
            filterAlign= 1;
1606
    }
1607

    
1608
    assert(minFilterSize > 0);
1609
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1610
    assert(filterSize > 0);
1611
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1612
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1613
        goto fail;
1614
    *outFilterSize= filterSize;
1615

    
1616
    if (flags&SWS_PRINT_INFO)
1617
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1618
    /* try to reduce the filter-size (step2 reduce it) */
1619
    for (i=0; i<dstW; i++) {
1620
        int j;
1621

    
1622
        for (j=0; j<filterSize; j++) {
1623
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1624
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1625
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1626
                filter[i*filterSize + j]= 0;
1627
        }
1628
    }
1629

    
1630

    
1631
    //FIXME try to align filterPos if possible
1632

    
1633
    //fix borders
1634
    for (i=0; i<dstW; i++) {
1635
        int j;
1636
        if ((*filterPos)[i] < 0) {
1637
            // move filter coefficients left to compensate for filterPos
1638
            for (j=1; j<filterSize; j++) {
1639
                int left= FFMAX(j + (*filterPos)[i], 0);
1640
                filter[i*filterSize + left] += filter[i*filterSize + j];
1641
                filter[i*filterSize + j]=0;
1642
            }
1643
            (*filterPos)[i]= 0;
1644
        }
1645

    
1646
        if ((*filterPos)[i] + filterSize > srcW) {
1647
            int shift= (*filterPos)[i] + filterSize - srcW;
1648
            // move filter coefficients right to compensate for filterPos
1649
            for (j=filterSize-2; j>=0; j--) {
1650
                int right= FFMIN(j + shift, filterSize-1);
1651
                filter[i*filterSize +right] += filter[i*filterSize +j];
1652
                filter[i*filterSize +j]=0;
1653
            }
1654
            (*filterPos)[i]= srcW - filterSize;
1655
        }
1656
    }
1657

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

    
1662
    /* normalize & store in outFilter */
1663
    for (i=0; i<dstW; i++) {
1664
        int j;
1665
        int64_t error=0;
1666
        int64_t sum=0;
1667

    
1668
        for (j=0; j<filterSize; j++) {
1669
            sum+= filter[i*filterSize + j];
1670
        }
1671
        sum= (sum + one/2)/ one;
1672
        for (j=0; j<*outFilterSize; j++) {
1673
            int64_t v= filter[i*filterSize + j] + error;
1674
            int intV= ROUNDED_DIV(v, sum);
1675
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1676
            error= v - intV*sum;
1677
        }
1678
    }
1679

    
1680
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1681
    for (i=0; i<*outFilterSize; i++) {
1682
        int j= dstW*(*outFilterSize);
1683
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1684
    }
1685

    
1686
    ret=0;
1687
fail:
1688
    av_free(filter);
1689
    av_free(filter2);
1690
    return ret;
1691
}
1692

    
1693
#ifdef COMPILE_MMX2
1694
static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1695
{
1696
    uint8_t *fragmentA;
1697
    x86_reg imm8OfPShufW1A;
1698
    x86_reg imm8OfPShufW2A;
1699
    x86_reg fragmentLengthA;
1700
    uint8_t *fragmentB;
1701
    x86_reg imm8OfPShufW1B;
1702
    x86_reg imm8OfPShufW2B;
1703
    x86_reg fragmentLengthB;
1704
    int fragmentPos;
1705

    
1706
    int xpos, i;
1707

    
1708
    // create an optimized horizontal scaling routine
1709
    /* This scaler is made of runtime-generated MMX2 code using specially
1710
     * tuned pshufw instructions. For every four output pixels, if four
1711
     * input pixels are enough for the fast bilinear scaling, then a chunk
1712
     * of fragmentB is used. If five input pixels are needed, then a chunk
1713
     * of fragmentA is used.
1714
     */
1715

    
1716
    //code fragment
1717

    
1718
    __asm__ volatile(
1719
        "jmp                         9f                 \n\t"
1720
    // Begin
1721
        "0:                                             \n\t"
1722
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1723
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1724
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1725
        "punpcklbw                %%mm7, %%mm1          \n\t"
1726
        "punpcklbw                %%mm7, %%mm0          \n\t"
1727
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1728
        "1:                                             \n\t"
1729
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1730
        "2:                                             \n\t"
1731
        "psubw                    %%mm1, %%mm0          \n\t"
1732
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1733
        "pmullw                   %%mm3, %%mm0          \n\t"
1734
        "psllw                       $7, %%mm1          \n\t"
1735
        "paddw                    %%mm1, %%mm0          \n\t"
1736

    
1737
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1738

    
1739
        "add                         $8, %%"REG_a"      \n\t"
1740
    // End
1741
        "9:                                             \n\t"
1742
//        "int $3                                         \n\t"
1743
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1744
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1745
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1746
        "dec                         %1                 \n\t"
1747
        "dec                         %2                 \n\t"
1748
        "sub                         %0, %1             \n\t"
1749
        "sub                         %0, %2             \n\t"
1750
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1751
        "sub                         %0, %3             \n\t"
1752

    
1753

    
1754
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1755
        "=r" (fragmentLengthA)
1756
    );
1757

    
1758
    __asm__ volatile(
1759
        "jmp                         9f                 \n\t"
1760
    // Begin
1761
        "0:                                             \n\t"
1762
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1763
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1764
        "punpcklbw                %%mm7, %%mm0          \n\t"
1765
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1766
        "1:                                             \n\t"
1767
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1768
        "2:                                             \n\t"
1769
        "psubw                    %%mm1, %%mm0          \n\t"
1770
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1771
        "pmullw                   %%mm3, %%mm0          \n\t"
1772
        "psllw                       $7, %%mm1          \n\t"
1773
        "paddw                    %%mm1, %%mm0          \n\t"
1774

    
1775
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1776

    
1777
        "add                         $8, %%"REG_a"      \n\t"
1778
    // End
1779
        "9:                                             \n\t"
1780
//        "int                       $3                   \n\t"
1781
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1782
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1783
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1784
        "dec                         %1                 \n\t"
1785
        "dec                         %2                 \n\t"
1786
        "sub                         %0, %1             \n\t"
1787
        "sub                         %0, %2             \n\t"
1788
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1789
        "sub                         %0, %3             \n\t"
1790

    
1791

    
1792
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1793
        "=r" (fragmentLengthB)
1794
    );
1795

    
1796
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1797
    fragmentPos=0;
1798

    
1799
    for (i=0; i<dstW/numSplits; i++) {
1800
        int xx=xpos>>16;
1801

    
1802
        if ((i&3) == 0) {
1803
            int a=0;
1804
            int b=((xpos+xInc)>>16) - xx;
1805
            int c=((xpos+xInc*2)>>16) - xx;
1806
            int d=((xpos+xInc*3)>>16) - xx;
1807
            int inc                = (d+1<4);
1808
            uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
1809
            x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
1810
            x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
1811
            x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1812
            int maxShift= 3-(d+inc);
1813
            int shift=0;
1814

    
1815
            if (filterCode) {
1816
                filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1817
                filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1818
                filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1819
                filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1820
                filterPos[i/2]= xx;
1821

    
1822
                memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1823

    
1824
                filterCode[fragmentPos + imm8OfPShufW1]=
1825
                    (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1826
                filterCode[fragmentPos + imm8OfPShufW2]=
1827
                    a | (b<<2) | (c<<4) | (d<<6);
1828

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

    
1832
                if (shift && i>=shift) {
1833
                    filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1834
                    filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1835
                    filterPos[i/2]-=shift;
1836
                }
1837
            }
1838

    
1839
            fragmentPos+= fragmentLength;
1840

    
1841
            if (filterCode)
1842
                filterCode[fragmentPos]= RET;
1843
        }
1844
        xpos+=xInc;
1845
    }
1846
    if (filterCode)
1847
        filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1848

    
1849
    return fragmentPos + 1;
1850
}
1851
#endif /* COMPILE_MMX2 */
1852

    
1853
static void globalInit(void)
1854
{
1855
    // generating tables:
1856
    int i;
1857
    for (i=0; i<768; i++) {
1858
        int c= av_clip_uint8(i-256);
1859
        clip_table[i]=c;
1860
    }
1861
}
1862

    
1863
static SwsFunc getSwsFunc(SwsContext *c)
1864
{
1865
#if CONFIG_RUNTIME_CPUDETECT
1866
    int flags = c->flags;
1867

    
1868
#if ARCH_X86 && CONFIG_GPL
1869
    // ordered per speed fastest first
1870
    if (flags & SWS_CPU_CAPS_MMX2) {
1871
        sws_init_swScale_MMX2(c);
1872
        return swScale_MMX2;
1873
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1874
        sws_init_swScale_3DNow(c);
1875
        return swScale_3DNow;
1876
    } else if (flags & SWS_CPU_CAPS_MMX) {
1877
        sws_init_swScale_MMX(c);
1878
        return swScale_MMX;
1879
    } else {
1880
        sws_init_swScale_C(c);
1881
        return swScale_C;
1882
    }
1883

    
1884
#else
1885
#if ARCH_PPC
1886
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1887
        sws_init_swScale_altivec(c);
1888
        return swScale_altivec;
1889
    } else {
1890
        sws_init_swScale_C(c);
1891
        return swScale_C;
1892
    }
1893
#endif
1894
    sws_init_swScale_C(c);
1895
    return swScale_C;
1896
#endif /* ARCH_X86 && CONFIG_GPL */
1897
#else //CONFIG_RUNTIME_CPUDETECT
1898
#if   COMPILE_TEMPLATE_MMX2
1899
    sws_init_swScale_MMX2(c);
1900
    return swScale_MMX2;
1901
#elif COMPILE_TEMPLATE_AMD3DNOW
1902
    sws_init_swScale_3DNow(c);
1903
    return swScale_3DNow;
1904
#elif COMPILE_TEMPLATE_MMX
1905
    sws_init_swScale_MMX(c);
1906
    return swScale_MMX;
1907
#elif COMPILE_TEMPLATE_ALTIVEC
1908
    sws_init_swScale_altivec(c);
1909
    return swScale_altivec;
1910
#else
1911
    sws_init_swScale_C(c);
1912
    return swScale_C;
1913
#endif
1914
#endif //!CONFIG_RUNTIME_CPUDETECT
1915
}
1916

    
1917
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1918
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1919
{
1920
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1921
    /* Copy Y plane */
1922
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1923
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1924
    else {
1925
        int i;
1926
        const uint8_t *srcPtr= src[0];
1927
        uint8_t *dstPtr= dst;
1928
        for (i=0; i<srcSliceH; i++) {
1929
            memcpy(dstPtr, srcPtr, c->srcW);
1930
            srcPtr+= srcStride[0];
1931
            dstPtr+= dstStride[0];
1932
        }
1933
    }
1934
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1935
    if (c->dstFormat == PIX_FMT_NV12)
1936
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1937
    else
1938
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1939

    
1940
    return srcSliceH;
1941
}
1942

    
1943
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1944
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1945
{
1946
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1947

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

    
1950
    return srcSliceH;
1951
}
1952

    
1953
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1954
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1955
{
1956
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1957

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

    
1960
    return srcSliceH;
1961
}
1962

    
1963
static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1964
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1965
{
1966
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1967

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

    
1970
    return srcSliceH;
1971
}
1972

    
1973
static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1974
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1975
{
1976
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1977

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

    
1980
    return srcSliceH;
1981
}
1982

    
1983
static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1984
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1985
{
1986
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1987
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1988
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1989

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

    
1992
    if (dstParam[3])
1993
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1994

    
1995
    return srcSliceH;
1996
}
1997

    
1998
static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1999
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2000
{
2001
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2002
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2003
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2004

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

    
2007
    return srcSliceH;
2008
}
2009

    
2010
static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2011
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2012
{
2013
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2014
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2015
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2016

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

    
2019
    if (dstParam[3])
2020
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2021

    
2022
    return srcSliceH;
2023
}
2024

    
2025
static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2026
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2027
{
2028
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2029
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2030
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2031

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

    
2034
    return srcSliceH;
2035
}
2036

    
2037
static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2038
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2039
{
2040
    const enum PixelFormat srcFormat= c->srcFormat;
2041
    const enum PixelFormat dstFormat= c->dstFormat;
2042
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2043
                 const uint8_t *palette)=NULL;
2044
    int i;
2045
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2046
    uint8_t *srcPtr= src[0];
2047

    
2048
    if (!usePal(srcFormat))
2049
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2050
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2051

    
2052
    switch(dstFormat) {
2053
    case PIX_FMT_RGB32  : conv = palette8topacked32; break;
2054
    case PIX_FMT_BGR32  : conv = palette8topacked32; break;
2055
    case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2056
    case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2057
    case PIX_FMT_RGB24  : conv = palette8topacked24; break;
2058
    case PIX_FMT_BGR24  : conv = palette8topacked24; break;
2059
    default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2060
                    sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2061
    }
2062

    
2063

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

    
2070
    return srcSliceH;
2071
}
2072

    
2073
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2074
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2075
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2076
{
2077
    const enum PixelFormat srcFormat= c->srcFormat;
2078
    const enum PixelFormat dstFormat= c->dstFormat;
2079
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2080
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2081
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2082
    const int dstId= fmt_depth(dstFormat) >> 2;
2083
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2084

    
2085
    /* BGR -> BGR */
2086
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
2087
       || (isRGB(srcFormat) && isRGB(dstFormat))) {
2088
        switch(srcId | (dstId<<4)) {
2089
        case 0x34: conv= rgb16to15; break;
2090
        case 0x36: conv= rgb24to15; break;
2091
        case 0x38: conv= rgb32to15; break;
2092
        case 0x43: conv= rgb15to16; break;
2093
        case 0x46: conv= rgb24to16; break;
2094
        case 0x48: conv= rgb32to16; break;
2095
        case 0x63: conv= rgb15to24; break;
2096
        case 0x64: conv= rgb16to24; break;
2097
        case 0x68: conv= rgb32to24; break;
2098
        case 0x83: conv= rgb15to32; break;
2099
        case 0x84: conv= rgb16to32; break;
2100
        case 0x86: conv= rgb24to32; break;
2101
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2102
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2103
        }
2104
    } else if (  (isBGR(srcFormat) && isRGB(dstFormat))
2105
             || (isRGB(srcFormat) && isBGR(dstFormat))) {
2106
        switch(srcId | (dstId<<4)) {
2107
        case 0x33: conv= rgb15tobgr15; break;
2108
        case 0x34: conv= rgb16tobgr15; break;
2109
        case 0x36: conv= rgb24tobgr15; break;
2110
        case 0x38: conv= rgb32tobgr15; break;
2111
        case 0x43: conv= rgb15tobgr16; break;
2112
        case 0x44: conv= rgb16tobgr16; break;
2113
        case 0x46: conv= rgb24tobgr16; break;
2114
        case 0x48: conv= rgb32tobgr16; break;
2115
        case 0x63: conv= rgb15tobgr24; break;
2116
        case 0x64: conv= rgb16tobgr24; break;
2117
        case 0x66: conv= rgb24tobgr24; break;
2118
        case 0x68: conv= rgb32tobgr24; break;
2119
        case 0x83: conv= rgb15tobgr32; break;
2120
        case 0x84: conv= rgb16tobgr32; break;
2121
        case 0x86: conv= rgb24tobgr32; break;
2122
        case 0x88: conv= rgb32tobgr32; break;
2123
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2124
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2125
        }
2126
    } else {
2127
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2128
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2129
    }
2130

    
2131
    if(conv) {
2132
        uint8_t *srcPtr= src[0];
2133
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2134
            srcPtr += ALT32_CORR;
2135

    
2136
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2137
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2138
        else {
2139
            int i;
2140
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2141

    
2142
            for (i=0; i<srcSliceH; i++) {
2143
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
2144
                srcPtr+= srcStride[0];
2145
                dstPtr+= dstStride[0];
2146
            }
2147
        }
2148
    }
2149
    return srcSliceH;
2150
}
2151

    
2152
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2153
                              int srcSliceH, uint8_t* dst[], int dstStride[])
2154
{
2155

    
2156
    rgb24toyv12(
2157
        src[0],
2158
        dst[0]+ srcSliceY    *dstStride[0],
2159
        dst[1]+(srcSliceY>>1)*dstStride[1],
2160
        dst[2]+(srcSliceY>>1)*dstStride[2],
2161
        c->srcW, srcSliceH,
2162
        dstStride[0], dstStride[1], srcStride[0]);
2163
    if (dst[3])
2164
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2165
    return srcSliceH;
2166
}
2167

    
2168
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2169
                             int srcSliceH, uint8_t* dst[], int dstStride[])
2170
{
2171
    int i;
2172

    
2173
    /* copy Y */
2174
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2175
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2176
    else {
2177
        uint8_t *srcPtr= src[0];
2178
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2179

    
2180
        for (i=0; i<srcSliceH; i++) {
2181
            memcpy(dstPtr, srcPtr, c->srcW);
2182
            srcPtr+= srcStride[0];
2183
            dstPtr+= dstStride[0];
2184
        }
2185
    }
2186

    
2187
    if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
2188
        planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2189
                 srcSliceH >> 2, srcStride[1], dstStride[1]);
2190
        planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2191
                 srcSliceH >> 2, srcStride[2], dstStride[2]);
2192
    } else {
2193
        planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2194
                 srcSliceH >> 2, srcStride[1], dstStride[2]);
2195
        planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2196
                 srcSliceH >> 2, srcStride[2], dstStride[1]);
2197
    }
2198
    if (dst[3])
2199
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2200
    return srcSliceH;
2201
}
2202

    
2203
/* unscaled copy like stuff (assumes nearly identical formats) */
2204
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2205
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2206
{
2207
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2208
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2209
    else {
2210
        int i;
2211
        uint8_t *srcPtr= src[0];
2212
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2213
        int length=0;
2214

    
2215
        /* universal length finder */
2216
        while(length+c->srcW <= FFABS(dstStride[0])
2217
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2218
        assert(length!=0);
2219

    
2220
        for (i=0; i<srcSliceH; i++) {
2221
            memcpy(dstPtr, srcPtr, length);
2222
            srcPtr+= srcStride[0];
2223
            dstPtr+= dstStride[0];
2224
        }
2225
    }
2226
    return srcSliceH;
2227
}
2228

    
2229
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2230
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2231
{
2232
    int plane, i, j;
2233
    for (plane=0; plane<4; plane++) {
2234
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2235
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2236
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2237
        uint8_t *srcPtr= src[plane];
2238
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2239

    
2240
        if (!dst[plane]) continue;
2241
        // ignore palette for GRAY8
2242
        if (plane == 1 && !dst[2]) continue;
2243
        if (!src[plane] || (plane == 1 && !src[2])) {
2244
            if(is16BPS(c->dstFormat))
2245
                length*=2;
2246
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2247
        } else {
2248
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
2249
                if (!isBE(c->srcFormat)) srcPtr++;
2250
                for (i=0; i<height; i++) {
2251
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2252
                    srcPtr+= srcStride[plane];
2253
                    dstPtr+= dstStride[plane];
2254
                }
2255
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
2256
                for (i=0; i<height; i++) {
2257
                    for (j=0; j<length; j++) {
2258
                        dstPtr[ j<<1   ] = srcPtr[j];
2259
                        dstPtr[(j<<1)+1] = srcPtr[j];
2260
                    }
2261
                    srcPtr+= srcStride[plane];
2262
                    dstPtr+= dstStride[plane];
2263
                }
2264
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2265
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
2266

    
2267
                for (i=0; i<height; i++) {
2268
                    for (j=0; j<length; j++)
2269
                        ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2270
                    srcPtr+= srcStride[plane];
2271
                    dstPtr+= dstStride[plane];
2272
                }
2273
            } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2274
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2275
            else {
2276
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2277
                    length*=2;
2278
                for (i=0; i<height; i++) {
2279
                    memcpy(dstPtr, srcPtr, length);
2280
                    srcPtr+= srcStride[plane];
2281
                    dstPtr+= dstStride[plane];
2282
                }
2283
            }
2284
        }
2285
    }
2286
    return srcSliceH;
2287
}
2288

    
2289

    
2290
static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
2291
{
2292
    *h = av_pix_fmt_descriptors[format].log2_chroma_w;
2293
    *v = av_pix_fmt_descriptors[format].log2_chroma_h;
2294
}
2295

    
2296
static uint16_t roundToInt16(int64_t f)
2297
{
2298
    int r= (f + (1<<15))>>16;
2299
         if (r<-0x7FFF) return 0x8000;
2300
    else if (r> 0x7FFF) return 0x7FFF;
2301
    else                return r;
2302
}
2303

    
2304
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
2305
{
2306
    int64_t crv =  inv_table[0];
2307
    int64_t cbu =  inv_table[1];
2308
    int64_t cgu = -inv_table[2];
2309
    int64_t cgv = -inv_table[3];
2310
    int64_t cy  = 1<<16;
2311
    int64_t oy  = 0;
2312

    
2313
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2314
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2315

    
2316
    c->brightness= brightness;
2317
    c->contrast  = contrast;
2318
    c->saturation= saturation;
2319
    c->srcRange  = srcRange;
2320
    c->dstRange  = dstRange;
2321
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2322

    
2323
    c->uOffset=   0x0400040004000400LL;
2324
    c->vOffset=   0x0400040004000400LL;
2325

    
2326
    if (!srcRange) {
2327
        cy= (cy*255) / 219;
2328
        oy= 16<<16;
2329
    } else {
2330
        crv= (crv*224) / 255;
2331
        cbu= (cbu*224) / 255;
2332
        cgu= (cgu*224) / 255;
2333
        cgv= (cgv*224) / 255;
2334
    }
2335

    
2336
    cy = (cy *contrast             )>>16;
2337
    crv= (crv*contrast * saturation)>>32;
2338
    cbu= (cbu*contrast * saturation)>>32;
2339
    cgu= (cgu*contrast * saturation)>>32;
2340
    cgv= (cgv*contrast * saturation)>>32;
2341

    
2342
    oy -= 256*brightness;
2343

    
2344
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2345
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2346
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2347
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2348
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2349
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2350

    
2351
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2352
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2353
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2354
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2355
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2356
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2357

    
2358
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2359
    //FIXME factorize
2360

    
2361
#ifdef COMPILE_ALTIVEC
2362
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2363
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2364
#endif
2365
    return 0;
2366
}
2367

    
2368
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
2369
{
2370
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2371

    
2372
    *inv_table = c->srcColorspaceTable;
2373
    *table     = c->dstColorspaceTable;
2374
    *srcRange  = c->srcRange;
2375
    *dstRange  = c->dstRange;
2376
    *brightness= c->brightness;
2377
    *contrast  = c->contrast;
2378
    *saturation= c->saturation;
2379

    
2380
    return 0;
2381
}
2382

    
2383
static int handle_jpeg(enum PixelFormat *format)
2384
{
2385
    switch (*format) {
2386
    case PIX_FMT_YUVJ420P:
2387
        *format = PIX_FMT_YUV420P;
2388
        return 1;
2389
    case PIX_FMT_YUVJ422P:
2390
        *format = PIX_FMT_YUV422P;
2391
        return 1;
2392
    case PIX_FMT_YUVJ444P:
2393
        *format = PIX_FMT_YUV444P;
2394
        return 1;
2395
    case PIX_FMT_YUVJ440P:
2396
        *format = PIX_FMT_YUV440P;
2397
        return 1;
2398
    default:
2399
        return 0;
2400
    }
2401
}
2402

    
2403
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2404
                           SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2405
{
2406

    
2407
    SwsContext *c;
2408
    int i;
2409
    int usesVFilter, usesHFilter;
2410
    int unscaled, needsDither;
2411
    int srcRange, dstRange;
2412
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2413
#if ARCH_X86
2414
    if (flags & SWS_CPU_CAPS_MMX)
2415
        __asm__ volatile("emms\n\t"::: "memory");
2416
#endif
2417

    
2418
#if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2419
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2420
#if   COMPILE_TEMPLATE_MMX2
2421
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2422
#elif COMPILE_TEMPLATE_AMD3DNOW
2423
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2424
#elif COMPILE_TEMPLATE_MMX
2425
    flags |= SWS_CPU_CAPS_MMX;
2426
#elif COMPILE_TEMPLATE_ALTIVEC
2427
    flags |= SWS_CPU_CAPS_ALTIVEC;
2428
#elif ARCH_BFIN
2429
    flags |= SWS_CPU_CAPS_BFIN;
2430
#endif
2431
#endif /* CONFIG_RUNTIME_CPUDETECT */
2432
    if (clip_table[512] != 255) globalInit();
2433
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2434

    
2435
    unscaled = (srcW == dstW && srcH == dstH);
2436
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2437
        && (fmt_depth(dstFormat))<24
2438
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2439

    
2440
    srcRange = handle_jpeg(&srcFormat);
2441
    dstRange = handle_jpeg(&dstFormat);
2442

    
2443
    if (!isSupportedIn(srcFormat)) {
2444
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2445
        return NULL;
2446
    }
2447
    if (!isSupportedOut(dstFormat)) {
2448
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2449
        return NULL;
2450
    }
2451

    
2452
    i= flags & ( SWS_POINT
2453
                |SWS_AREA
2454
                |SWS_BILINEAR
2455
                |SWS_FAST_BILINEAR
2456
                |SWS_BICUBIC
2457
                |SWS_X
2458
                |SWS_GAUSS
2459
                |SWS_LANCZOS
2460
                |SWS_SINC
2461
                |SWS_SPLINE
2462
                |SWS_BICUBLIN);
2463
    if(!i || (i & (i-1))) {
2464
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2465
        return NULL;
2466
    }
2467

    
2468
    /* sanity check */
2469
    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
2470
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2471
               srcW, srcH, dstW, dstH);
2472
        return NULL;
2473
    }
2474
    if(srcW > VOFW || dstW > VOFW) {
2475
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2476
        return NULL;
2477
    }
2478

    
2479
    if (!dstFilter) dstFilter= &dummyFilter;
2480
    if (!srcFilter) srcFilter= &dummyFilter;
2481

    
2482
    FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
2483

    
2484
    c->av_class = &sws_context_class;
2485
    c->srcW= srcW;
2486
    c->srcH= srcH;
2487
    c->dstW= dstW;
2488
    c->dstH= dstH;
2489
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2490
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2491
    c->flags= flags;
2492
    c->dstFormat= dstFormat;
2493
    c->srcFormat= srcFormat;
2494
    c->vRounder= 4* 0x0001000100010001ULL;
2495

    
2496
    usesHFilter= usesVFilter= 0;
2497
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2498
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2499
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2500
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2501
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2502
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2503
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2504
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2505

    
2506
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2507
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2508

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

    
2512
    // drop some chroma lines if the user wants it
2513
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2514
    c->chrSrcVSubSample+= c->vChrDrop;
2515

    
2516
    // drop every other pixel for chroma calculation unless user wants full chroma
2517
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2518
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2519
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2520
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2521
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2522
        c->chrSrcHSubSample=1;
2523

    
2524
    if (param) {
2525
        c->param[0] = param[0];
2526
        c->param[1] = param[1];
2527
    } else {
2528
        c->param[0] =
2529
        c->param[1] = SWS_PARAM_DEFAULT;
2530
    }
2531

    
2532
    // Note the -((-x)>>y) is so that we always round toward +inf.
2533
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2534
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2535
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2536
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2537

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

    
2540
    /* unscaled special cases */
2541
    if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat))) {
2542
        /* yv12_to_nv12 */
2543
        if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
2544
            c->swScale= PlanarToNV12Wrapper;
2545
        }
2546
        /* yuv2bgr */
2547
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2548
            && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
2549
            c->swScale= ff_yuv2rgb_get_func_ptr(c);
2550
        }
2551

    
2552
        if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2553
            c->swScale= yvu9toyv12Wrapper;
2554
        }
2555

    
2556
        /* bgr24toYV12 */
2557
        if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2558
            c->swScale= bgr24toyv12Wrapper;
2559

    
2560
        /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2561
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2562
           && (isBGR(dstFormat) || isRGB(dstFormat))
2563
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2564
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2565
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2566
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2567
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2568
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2569
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2570
           && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2571
                                             && dstFormat != PIX_FMT_RGB32_1
2572
                                             && dstFormat != PIX_FMT_BGR32_1
2573
           && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
2574
           && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
2575
           && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2576
             c->swScale= rgb2rgbWrapper;
2577

    
2578
        if ((usePal(srcFormat) && (
2579
                 dstFormat == PIX_FMT_RGB32   ||
2580
                 dstFormat == PIX_FMT_RGB32_1 ||
2581
                 dstFormat == PIX_FMT_RGB24   ||
2582
                 dstFormat == PIX_FMT_BGR32   ||
2583
                 dstFormat == PIX_FMT_BGR32_1 ||
2584
                 dstFormat == PIX_FMT_BGR24)))
2585
             c->swScale= pal2rgbWrapper;
2586

    
2587
        if (srcFormat == PIX_FMT_YUV422P) {
2588
            if (dstFormat == PIX_FMT_YUYV422)
2589
                c->swScale= YUV422PToYuy2Wrapper;
2590
            else if (dstFormat == PIX_FMT_UYVY422)
2591
                c->swScale= YUV422PToUyvyWrapper;
2592
        }
2593

    
2594
        /* LQ converters if -sws 0 or -sws 4*/
2595
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2596
            /* yv12_to_yuy2 */
2597
            if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2598
                if (dstFormat == PIX_FMT_YUYV422)
2599
                    c->swScale= PlanarToYuy2Wrapper;
2600
                else if (dstFormat == PIX_FMT_UYVY422)
2601
                    c->swScale= PlanarToUyvyWrapper;
2602
            }
2603
        }
2604
        if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2605
            c->swScale= YUYV2YUV420Wrapper;
2606
        if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2607
            c->swScale= UYVY2YUV420Wrapper;
2608
        if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2609
            c->swScale= YUYV2YUV422Wrapper;
2610
        if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2611
            c->swScale= UYVY2YUV422Wrapper;
2612

    
2613
#ifdef COMPILE_ALTIVEC
2614
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2615
            !(c->flags & SWS_BITEXACT) &&
2616
            srcFormat == PIX_FMT_YUV420P) {
2617
          // unscaled YV12 -> packed YUV, we want speed
2618
          if (dstFormat == PIX_FMT_YUYV422)
2619
              c->swScale= yv12toyuy2_unscaled_altivec;
2620
          else if (dstFormat == PIX_FMT_UYVY422)
2621
              c->swScale= yv12touyvy_unscaled_altivec;
2622
        }
2623
#endif
2624

    
2625
        /* simple copy */
2626
        if (  srcFormat == dstFormat
2627
            || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2628
            || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2629
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2630
            || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2631
            || (isGray(dstFormat) && isGray(srcFormat))
2632
            || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2633
                && c->chrDstHSubSample == c->chrSrcHSubSample
2634
                && c->chrDstVSubSample == c->chrSrcVSubSample
2635
                && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2636
                && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2637
        {
2638
            if (isPacked(c->srcFormat))
2639
                c->swScale= packedCopy;
2640
            else /* Planar YUV or gray */
2641
                c->swScale= planarCopy;
2642
        }
2643
#if ARCH_BFIN
2644
        if (flags & SWS_CPU_CAPS_BFIN)
2645
            ff_bfin_get_unscaled_swscale (c);
2646
#endif
2647

    
2648
        if (c->swScale) {
2649
            if (flags&SWS_PRINT_INFO)
2650
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2651
                       sws_format_name(srcFormat), sws_format_name(dstFormat));
2652
            return c;
2653
        }
2654
    }
2655

    
2656
    if (flags & SWS_CPU_CAPS_MMX2) {
2657
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2658
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
2659
            if (flags&SWS_PRINT_INFO)
2660
                av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2661
        }
2662
        if (usesHFilter) c->canMMX2BeUsed=0;
2663
    }
2664
    else
2665
        c->canMMX2BeUsed=0;
2666

    
2667
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2668
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2669

    
2670
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2671
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
2672
    // n-2 is the last chrominance sample available
2673
    // this is not perfect, but no one should notice the difference, the more correct variant
2674
    // would be like the vertical one, but that would require some special code for the
2675
    // first and last pixel
2676
    if (flags&SWS_FAST_BILINEAR) {
2677
        if (c->canMMX2BeUsed) {
2678
            c->lumXInc+= 20;
2679
            c->chrXInc+= 20;
2680
        }
2681
        //we don't use the x86 asm scaler if MMX is available
2682
        else if (flags & SWS_CPU_CAPS_MMX) {
2683
            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2684
            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2685
        }
2686
    }
2687

    
2688
    /* precalculate horizontal scaler filter coefficients */
2689
    {
2690
        const int filterAlign=
2691
            (flags & SWS_CPU_CAPS_MMX) ? 4 :
2692
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2693
            1;
2694

    
2695
        if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2696
                       srcW      ,       dstW, filterAlign, 1<<14,
2697
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2698
                       srcFilter->lumH, dstFilter->lumH, c->param) < 0)
2699
            goto fail;
2700
        if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2701
                       c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2702
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2703
                       srcFilter->chrH, dstFilter->chrH, c->param) < 0)
2704
            goto fail;
2705

    
2706
#if defined(COMPILE_MMX2)
2707
// can't downscale !!!
2708
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
2709
            c->lumMmx2FilterCodeSize = initMMX2HScaler(      dstW, c->lumXInc, NULL, NULL, NULL, 8);
2710
            c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2711

    
2712
#ifdef MAP_ANONYMOUS
2713
            c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2714
            c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2715
#elif HAVE_VIRTUALALLOC
2716
            c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2717
            c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2718
#else
2719
            c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2720
            c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2721
#endif
2722

    
2723
            FF_ALLOCZ_OR_GOTO(c, c->lumMmx2Filter   , (dstW        /8+8)*sizeof(int16_t), fail);
2724
            FF_ALLOCZ_OR_GOTO(c, c->chrMmx2Filter   , (c->chrDstW  /4+8)*sizeof(int16_t), fail);
2725
            FF_ALLOCZ_OR_GOTO(c, c->lumMmx2FilterPos, (dstW      /2/8+8)*sizeof(int32_t), fail);
2726
            FF_ALLOCZ_OR_GOTO(c, c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
2727

    
2728
            initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2729
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2730

    
2731
#ifdef MAP_ANONYMOUS
2732
            mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2733
            mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2734
#endif
2735
        }
2736
#endif /* defined(COMPILE_MMX2) */
2737
    } // initialize horizontal stuff
2738

    
2739

    
2740

    
2741
    /* precalculate vertical scaler filter coefficients */
2742
    {
2743
        const int filterAlign=
2744
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2745
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2746
            1;
2747

    
2748
        if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2749
                       srcH      ,        dstH, filterAlign, (1<<12),
2750
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2751
                       srcFilter->lumV, dstFilter->lumV, c->param) < 0)
2752
            goto fail;
2753
        if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2754
                       c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2755
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2756
                       srcFilter->chrV, dstFilter->chrV, c->param) < 0)
2757
            goto fail;
2758

    
2759
#ifdef COMPILE_ALTIVEC
2760
        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
2761
        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
2762

    
2763
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2764
            int j;
2765
            short *p = (short *)&c->vYCoeffsBank[i];
2766
            for (j=0;j<8;j++)
2767
                p[j] = c->vLumFilter[i];
2768
        }
2769

    
2770
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2771
            int j;
2772
            short *p = (short *)&c->vCCoeffsBank[i];
2773
            for (j=0;j<8;j++)
2774
                p[j] = c->vChrFilter[i];
2775
        }
2776
#endif
2777
    }
2778

    
2779
    // calculate buffer sizes so that they won't run out while handling these damn slices
2780
    c->vLumBufSize= c->vLumFilterSize;
2781
    c->vChrBufSize= c->vChrFilterSize;
2782
    for (i=0; i<dstH; i++) {
2783
        int chrI= i*c->chrDstH / dstH;
2784
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
2785
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2786

    
2787
        nextSlice>>= c->chrSrcVSubSample;
2788
        nextSlice<<= c->chrSrcVSubSample;
2789
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2790
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2791
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2792
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2793
    }
2794

    
2795
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2796
    // allocate several megabytes to handle all possible cases)
2797
    FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2798
    FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
2799
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2800
        FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2801
    //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)
2802
    /* align at 16 bytes for AltiVec */
2803
    for (i=0; i<c->vLumBufSize; i++) {
2804
        FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
2805
        c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2806
    }
2807
    for (i=0; i<c->vChrBufSize; i++) {
2808
        FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
2809
        c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2810
    }
2811
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2812
        for (i=0; i<c->vLumBufSize; i++) {
2813
            FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
2814
            c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2815
        }
2816

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

    
2820
    assert(2*VOFW == VOF);
2821

    
2822
    assert(c->chrDstH <= dstH);
2823

    
2824
    if (flags&SWS_PRINT_INFO) {
2825
#ifdef DITHER1XBPP
2826
        const char *dither= " dithered";
2827
#else
2828
        const char *dither= "";
2829
#endif
2830
        if (flags&SWS_FAST_BILINEAR)
2831
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2832
        else if (flags&SWS_BILINEAR)
2833
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2834
        else if (flags&SWS_BICUBIC)
2835
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2836
        else if (flags&SWS_X)
2837
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2838
        else if (flags&SWS_POINT)
2839
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2840
        else if (flags&SWS_AREA)
2841
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2842
        else if (flags&SWS_BICUBLIN)
2843
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2844
        else if (flags&SWS_GAUSS)
2845
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2846
        else if (flags&SWS_SINC)
2847
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2848
        else if (flags&SWS_LANCZOS)
2849
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2850
        else if (flags&SWS_SPLINE)
2851
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2852
        else
2853
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2854

    
2855
        if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2856
            av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2857
                   sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2858
        else
2859
            av_log(c, AV_LOG_INFO, "from %s to %s ",
2860
                   sws_format_name(srcFormat), sws_format_name(dstFormat));
2861

    
2862
        if (flags & SWS_CPU_CAPS_MMX2)
2863
            av_log(c, AV_LOG_INFO, "using MMX2\n");
2864
        else if (flags & SWS_CPU_CAPS_3DNOW)
2865
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2866
        else if (flags & SWS_CPU_CAPS_MMX)
2867
            av_log(c, AV_LOG_INFO, "using MMX\n");
2868
        else if (flags & SWS_CPU_CAPS_ALTIVEC)
2869
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
2870
        else
2871
            av_log(c, AV_LOG_INFO, "using C\n");
2872
    }
2873

    
2874
    if (flags & SWS_PRINT_INFO) {
2875
        if (flags & SWS_CPU_CAPS_MMX) {
2876
            if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2877
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2878
            else {
2879
                if (c->hLumFilterSize==4)
2880
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2881
                else if (c->hLumFilterSize==8)
2882
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2883
                else
2884
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2885

    
2886
                if (c->hChrFilterSize==4)
2887
                    av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2888
                else if (c->hChrFilterSize==8)
2889
                    av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2890
                else
2891
                    av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2892
            }
2893
        } else {
2894
#if ARCH_X86
2895
            av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2896
#else
2897
            if (flags & SWS_FAST_BILINEAR)
2898
                av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2899
            else
2900
                av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2901
#endif
2902
        }
2903
        if (isPlanarYUV(dstFormat)) {
2904
            if (c->vLumFilterSize==1)
2905
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2906
            else
2907
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2908
        } else {
2909
            if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2910
                av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2911
                       "      2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2912
            else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2913
                av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2914
            else
2915
                av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2916
        }
2917

    
2918
        if (dstFormat==PIX_FMT_BGR24)
2919
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2920
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2921
        else if (dstFormat==PIX_FMT_RGB32)
2922
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2923
        else if (dstFormat==PIX_FMT_BGR565)
2924
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2925
        else if (dstFormat==PIX_FMT_BGR555)
2926
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2927

    
2928
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2929
    }
2930
    if (flags & SWS_PRINT_INFO) {
2931
        av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2932
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2933
        av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2934
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2935
    }
2936

    
2937
    c->swScale= getSwsFunc(c);
2938
    return c;
2939

    
2940
fail:
2941
    sws_freeContext(c);
2942
    return NULL;
2943
}
2944

    
2945
static void reset_ptr(uint8_t* src[], int format)
2946
{
2947
    if(!isALPHA(format))
2948
        src[3]=NULL;
2949
    if(!isPlanarYUV(format)) {
2950
        src[3]=src[2]=NULL;
2951
        if(   format != PIX_FMT_PAL8
2952
           && format != PIX_FMT_RGB8
2953
           && format != PIX_FMT_BGR8
2954
           && format != PIX_FMT_RGB4_BYTE
2955
           && format != PIX_FMT_BGR4_BYTE
2956
          )
2957
            src[1]= NULL;
2958
    }
2959
}
2960

    
2961
/**
2962
 * swscale wrapper, so we don't need to export the SwsContext.
2963
 * Assumes planar YUV to be in YUV order instead of YVU.
2964
 */
2965
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2966
              int srcSliceH, uint8_t* dst[], int dstStride[])
2967
{
2968
    int i;
2969
    uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2970
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2971

    
2972
    // do not mess up sliceDir if we have a "trailing" 0-size slice
2973
    if (srcSliceH == 0)
2974
        return 0;
2975

    
2976
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2977
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2978
        return 0;
2979
    }
2980
    if (c->sliceDir == 0) {
2981
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2982
    }
2983

    
2984
    if (usePal(c->srcFormat)) {
2985
        for (i=0; i<256; i++) {
2986
            int p, r, g, b,y,u,v;
2987
            if(c->srcFormat == PIX_FMT_PAL8) {
2988
                p=((uint32_t*)(src[1]))[i];
2989
                r= (p>>16)&0xFF;
2990
                g= (p>> 8)&0xFF;
2991
                b=  p     &0xFF;
2992
            } else if(c->srcFormat == PIX_FMT_RGB8) {
2993
                r= (i>>5    )*36;
2994
                g= ((i>>2)&7)*36;
2995
                b= (i&3     )*85;
2996
            } else if(c->srcFormat == PIX_FMT_BGR8) {
2997
                b= (i>>6    )*85;
2998
                g= ((i>>3)&7)*36;
2999
                r= (i&7     )*36;
3000
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
3001
                r= (i>>3    )*255;
3002
                g= ((i>>1)&3)*85;
3003
                b= (i&1     )*255;
3004
            } else {
3005
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3006
                b= (i>>3    )*255;
3007
                g= ((i>>1)&3)*85;
3008
                r= (i&1     )*255;
3009
            }
3010
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3011
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3012
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3013
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
3014

    
3015

    
3016
            switch(c->dstFormat) {
3017
            case PIX_FMT_BGR32:
3018
#if !HAVE_BIGENDIAN
3019
            case PIX_FMT_RGB24:
3020
#endif
3021
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
3022
                break;
3023
            case PIX_FMT_BGR32_1:
3024
#if HAVE_BIGENDIAN
3025
            case PIX_FMT_BGR24:
3026
#endif
3027
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3028
                break;
3029
            case PIX_FMT_RGB32_1:
3030
#if HAVE_BIGENDIAN
3031
            case PIX_FMT_RGB24:
3032
#endif
3033
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3034
                break;
3035
            case PIX_FMT_RGB32:
3036
#if !HAVE_BIGENDIAN
3037
            case PIX_FMT_BGR24:
3038
#endif
3039
            default:
3040
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
3041
            }
3042
        }
3043
    }
3044

    
3045
    // copy strides, so they can safely be modified
3046
    if (c->sliceDir == 1) {
3047
        // slices go from top to bottom
3048
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3049
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3050

    
3051
        reset_ptr(src2, c->srcFormat);
3052
        reset_ptr(dst2, c->dstFormat);
3053

    
3054
        /* reset slice direction at end of frame */
3055
        if (srcSliceY + srcSliceH == c->srcH)
3056
            c->sliceDir = 0;
3057

    
3058
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3059
    } else {
3060
        // slices go from bottom to top => we flip the image internally
3061
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3062
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3063

    
3064
        src2[0] += (srcSliceH-1)*srcStride[0];
3065
        if (!usePal(c->srcFormat))
3066
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3067
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3068
        src2[3] += (srcSliceH-1)*srcStride[3];
3069
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
3070
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3071
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3072
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
3073

    
3074
        reset_ptr(src2, c->srcFormat);
3075
        reset_ptr(dst2, c->dstFormat);
3076

    
3077
        /* reset slice direction at end of frame */
3078
        if (!srcSliceY)
3079
            c->sliceDir = 0;
3080

    
3081
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3082
    }
3083
}
3084

    
3085
#if LIBSWSCALE_VERSION_MAJOR < 1
3086
int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3087
                      int srcSliceH, uint8_t* dst[], int dstStride[])
3088
{
3089
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3090
}
3091
#endif
3092

    
3093
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3094
                                float lumaSharpen, float chromaSharpen,
3095
                                float chromaHShift, float chromaVShift,
3096
                                int verbose)
3097
{
3098
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3099
    if (!filter)
3100
        return NULL;
3101

    
3102
    if (lumaGBlur!=0.0) {
3103
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3104
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3105
    } else {
3106
        filter->lumH= sws_getIdentityVec();
3107
        filter->lumV= sws_getIdentityVec();
3108
    }
3109

    
3110
    if (chromaGBlur!=0.0) {
3111
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3112
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3113
    } else {
3114
        filter->chrH= sws_getIdentityVec();
3115
        filter->chrV= sws_getIdentityVec();
3116
    }
3117

    
3118
    if (chromaSharpen!=0.0) {
3119
        SwsVector *id= sws_getIdentityVec();
3120
        sws_scaleVec(filter->chrH, -chromaSharpen);
3121
        sws_scaleVec(filter->chrV, -chromaSharpen);
3122
        sws_addVec(filter->chrH, id);
3123
        sws_addVec(filter->chrV, id);
3124
        sws_freeVec(id);
3125
    }
3126

    
3127
    if (lumaSharpen!=0.0) {
3128
        SwsVector *id= sws_getIdentityVec();
3129
        sws_scaleVec(filter->lumH, -lumaSharpen);
3130
        sws_scaleVec(filter->lumV, -lumaSharpen);
3131
        sws_addVec(filter->lumH, id);
3132
        sws_addVec(filter->lumV, id);
3133
        sws_freeVec(id);
3134
    }
3135

    
3136
    if (chromaHShift != 0.0)
3137
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3138

    
3139
    if (chromaVShift != 0.0)
3140
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3141

    
3142
    sws_normalizeVec(filter->chrH, 1.0);
3143
    sws_normalizeVec(filter->chrV, 1.0);
3144
    sws_normalizeVec(filter->lumH, 1.0);
3145
    sws_normalizeVec(filter->lumV, 1.0);
3146

    
3147
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3148
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3149

    
3150
    return filter;
3151
}
3152

    
3153
SwsVector *sws_allocVec(int length)
3154
{
3155
    SwsVector *vec = av_malloc(sizeof(SwsVector));
3156
    if (!vec)
3157
        return NULL;
3158
    vec->length = length;
3159
    vec->coeff  = av_malloc(sizeof(double) * length);
3160
    if (!vec->coeff)
3161
        av_freep(&vec);
3162
    return vec;
3163
}
3164

    
3165
SwsVector *sws_getGaussianVec(double variance, double quality)
3166
{
3167
    const int length= (int)(variance*quality + 0.5) | 1;
3168
    int i;
3169
    double middle= (length-1)*0.5;
3170
    SwsVector *vec= sws_allocVec(length);
3171

    
3172
    if (!vec)
3173
        return NULL;
3174

    
3175
    for (i=0; i<length; i++) {
3176
        double dist= i-middle;
3177
        vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3178
    }
3179

    
3180
    sws_normalizeVec(vec, 1.0);
3181

    
3182
    return vec;
3183
}
3184

    
3185
SwsVector *sws_getConstVec(double c, int length)
3186
{
3187
    int i;
3188
    SwsVector *vec= sws_allocVec(length);
3189

    
3190
    if (!vec)
3191
        return NULL;
3192

    
3193
    for (i=0; i<length; i++)
3194
        vec->coeff[i]= c;
3195

    
3196
    return vec;
3197
}
3198

    
3199

    
3200
SwsVector *sws_getIdentityVec(void)
3201
{
3202
    return sws_getConstVec(1.0, 1);
3203
}
3204

    
3205
double sws_dcVec(SwsVector *a)
3206
{
3207
    int i;
3208
    double sum=0;
3209

    
3210
    for (i=0; i<a->length; i++)
3211
        sum+= a->coeff[i];
3212

    
3213
    return sum;
3214
}
3215

    
3216
void sws_scaleVec(SwsVector *a, double scalar)
3217
{
3218
    int i;
3219

    
3220
    for (i=0; i<a->length; i++)
3221
        a->coeff[i]*= scalar;
3222
}
3223

    
3224
void sws_normalizeVec(SwsVector *a, double height)
3225
{
3226
    sws_scaleVec(a, height/sws_dcVec(a));
3227
}
3228

    
3229
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
3230
{
3231
    int length= a->length + b->length - 1;
3232
    int i, j;
3233
    SwsVector *vec= sws_getConstVec(0.0, length);
3234

    
3235
    if (!vec)
3236
        return NULL;
3237

    
3238
    for (i=0; i<a->length; i++) {
3239
        for (j=0; j<b->length; j++) {
3240
            vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
3241
        }
3242
    }
3243

    
3244
    return vec;
3245
}
3246

    
3247
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
3248
{
3249
    int length= FFMAX(a->length, b->length);
3250
    int i;
3251
    SwsVector *vec= sws_getConstVec(0.0, length);
3252

    
3253
    if (!vec)
3254
        return NULL;
3255

    
3256
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3257
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3258

    
3259
    return vec;
3260
}
3261

    
3262
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
3263
{
3264
    int length= FFMAX(a->length, b->length);
3265
    int i;
3266
    SwsVector *vec= sws_getConstVec(0.0, length);
3267

    
3268
    if (!vec)
3269
        return NULL;
3270

    
3271
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3272
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3273

    
3274
    return vec;
3275
}
3276

    
3277
/* shift left / or right if "shift" is negative */
3278
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
3279
{
3280
    int length= a->length + FFABS(shift)*2;
3281
    int i;
3282
    SwsVector *vec= sws_getConstVec(0.0, length);
3283

    
3284
    if (!vec)
3285
        return NULL;
3286

    
3287
    for (i=0; i<a->length; i++) {
3288
        vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3289
    }
3290

    
3291
    return vec;
3292
}
3293

    
3294
void sws_shiftVec(SwsVector *a, int shift)
3295
{
3296
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3297
    av_free(a->coeff);
3298
    a->coeff= shifted->coeff;
3299
    a->length= shifted->length;
3300
    av_free(shifted);
3301
}
3302

    
3303
void sws_addVec(SwsVector *a, SwsVector *b)
3304
{
3305
    SwsVector *sum= sws_sumVec(a, b);
3306
    av_free(a->coeff);
3307
    a->coeff= sum->coeff;
3308
    a->length= sum->length;
3309
    av_free(sum);
3310
}
3311

    
3312
void sws_subVec(SwsVector *a, SwsVector *b)
3313
{
3314
    SwsVector *diff= sws_diffVec(a, b);
3315
    av_free(a->coeff);
3316
    a->coeff= diff->coeff;
3317
    a->length= diff->length;
3318
    av_free(diff);
3319
}
3320

    
3321
void sws_convVec(SwsVector *a, SwsVector *b)
3322
{
3323
    SwsVector *conv= sws_getConvVec(a, b);
3324
    av_free(a->coeff);
3325
    a->coeff= conv->coeff;
3326
    a->length= conv->length;
3327
    av_free(conv);
3328
}
3329

    
3330
SwsVector *sws_cloneVec(SwsVector *a)
3331
{
3332
    int i;
3333
    SwsVector *vec= sws_allocVec(a->length);
3334

    
3335
    if (!vec)
3336
        return NULL;
3337

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

    
3340
    return vec;
3341
}
3342

    
3343
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
3344
{
3345
    int i;
3346
    double max=0;
3347
    double min=0;
3348
    double range;
3349

    
3350
    for (i=0; i<a->length; i++)
3351
        if (a->coeff[i]>max) max= a->coeff[i];
3352

    
3353
    for (i=0; i<a->length; i++)
3354
        if (a->coeff[i]<min) min= a->coeff[i];
3355

    
3356
    range= max - min;
3357

    
3358
    for (i=0; i<a->length; i++) {
3359
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3360
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3361
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
3362
        av_log(log_ctx, log_level, "|\n");
3363
    }
3364
}
3365

    
3366
#if LIBSWSCALE_VERSION_MAJOR < 1
3367
void sws_printVec(SwsVector *a)
3368
{
3369
    sws_printVec2(a, NULL, AV_LOG_DEBUG);
3370
}
3371
#endif
3372

    
3373
void sws_freeVec(SwsVector *a)
3374
{
3375
    if (!a) return;
3376
    av_freep(&a->coeff);
3377
    a->length=0;
3378
    av_free(a);
3379
}
3380

    
3381
void sws_freeFilter(SwsFilter *filter)
3382
{
3383
    if (!filter) return;
3384

    
3385
    if (filter->lumH) sws_freeVec(filter->lumH);
3386
    if (filter->lumV) sws_freeVec(filter->lumV);
3387
    if (filter->chrH) sws_freeVec(filter->chrH);
3388
    if (filter->chrV) sws_freeVec(filter->chrV);
3389
    av_free(filter);
3390
}
3391

    
3392

    
3393
void sws_freeContext(SwsContext *c)
3394
{
3395
    int i;
3396
    if (!c) return;
3397

    
3398
    if (c->lumPixBuf) {
3399
        for (i=0; i<c->vLumBufSize; i++)
3400
            av_freep(&c->lumPixBuf[i]);
3401
        av_freep(&c->lumPixBuf);
3402
    }
3403

    
3404
    if (c->chrPixBuf) {
3405
        for (i=0; i<c->vChrBufSize; i++)
3406
            av_freep(&c->chrPixBuf[i]);
3407
        av_freep(&c->chrPixBuf);
3408
    }
3409

    
3410
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
3411
        for (i=0; i<c->vLumBufSize; i++)
3412
            av_freep(&c->alpPixBuf[i]);
3413
        av_freep(&c->alpPixBuf);
3414
    }
3415

    
3416
    av_freep(&c->vLumFilter);
3417
    av_freep(&c->vChrFilter);
3418
    av_freep(&c->hLumFilter);
3419
    av_freep(&c->hChrFilter);
3420
#ifdef COMPILE_ALTIVEC
3421
    av_freep(&c->vYCoeffsBank);
3422
    av_freep(&c->vCCoeffsBank);
3423
#endif
3424

    
3425
    av_freep(&c->vLumFilterPos);
3426
    av_freep(&c->vChrFilterPos);
3427
    av_freep(&c->hLumFilterPos);
3428
    av_freep(&c->hChrFilterPos);
3429

    
3430
#if ARCH_X86 && CONFIG_GPL
3431
#ifdef MAP_ANONYMOUS
3432
    if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3433
    if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3434
#elif HAVE_VIRTUALALLOC
3435
    if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3436
    if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3437
#else
3438
    av_free(c->lumMmx2FilterCode);
3439
    av_free(c->chrMmx2FilterCode);
3440
#endif
3441
    c->lumMmx2FilterCode=NULL;
3442
    c->chrMmx2FilterCode=NULL;
3443
#endif /* ARCH_X86 && CONFIG_GPL */
3444

    
3445
    av_freep(&c->lumMmx2Filter);
3446
    av_freep(&c->chrMmx2Filter);
3447
    av_freep(&c->lumMmx2FilterPos);
3448
    av_freep(&c->chrMmx2FilterPos);
3449
    av_freep(&c->yuvTable);
3450

    
3451
    av_free(c);
3452
}
3453

    
3454
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3455
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3456
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3457
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3458
{
3459
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3460

    
3461
    if (!param)
3462
        param = default_param;
3463

    
3464
    if (context) {
3465
        if (context->srcW != srcW || context->srcH != srcH ||
3466
            context->srcFormat != srcFormat ||
3467
            context->dstW != dstW || context->dstH != dstH ||
3468
            context->dstFormat != dstFormat || context->flags != flags ||
3469
            context->param[0] != param[0] || context->param[1] != param[1])
3470
        {
3471
            sws_freeContext(context);
3472
            context = NULL;
3473
        }
3474
    }
3475
    if (!context) {
3476
        return sws_getContext(srcW, srcH, srcFormat,
3477
                              dstW, dstH, dstFormat, flags,
3478
                              srcFilter, dstFilter, param);
3479
    }
3480
    return context;
3481
}
3482