Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 0e21dcdb

History | View | Annotate | Download (122 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
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
302
{  1,   3,   1,   3,   1,   3,   1,   3, },
303
{  2,   0,   2,   0,   2,   0,   2,   0, },
304
};
305

    
306
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
307
{  6,   2,   6,   2,   6,   2,   6,   2, },
308
{  0,   4,   0,   4,   0,   4,   0,   4, },
309
};
310

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

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

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

    
382
const char *sws_format_name(enum PixelFormat format)
383
{
384
    if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
385
        return av_pix_fmt_descriptors[format].name;
386
    else
387
        return "Unknown format";
388
}
389

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

    
398
    for (i = 0; i < dstW; i++) {
399
        int val = 1 << 10;
400
        int j;
401

    
402
        for (j = 0; j < lumFilterSize; j++)
403
            val += lumSrc[j][i] * lumFilter[j];
404

    
405
        if (big_endian) {
406
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
407
        } else {
408
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
409
        }
410
    }
411

    
412
    if (uDest) {
413
        for (i = 0; i < chrDstW; i++) {
414
            int u = 1 << 10;
415
            int v = 1 << 10;
416
            int j;
417

    
418
            for (j = 0; j < chrFilterSize; j++) {
419
                u += chrSrc[j][i       ] * chrFilter[j];
420
                v += chrSrc[j][i + VOFW] * chrFilter[j];
421
            }
422

    
423
            if (big_endian) {
424
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
425
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
426
            } else {
427
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
428
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
429
            }
430
        }
431
    }
432

    
433
    if (CONFIG_SWSCALE_ALPHA && aDest) {
434
        for (i = 0; i < dstW; i++) {
435
            int val = 1 << 10;
436
            int j;
437

    
438
            for (j = 0; j < lumFilterSize; j++)
439
                val += alpSrc[j][i] * lumFilter[j];
440

    
441
            if (big_endian) {
442
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
443
            } else {
444
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
445
            }
446
        }
447
    }
448
}
449

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

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

    
482
        dest[i]= av_clip_uint8(val>>19);
483
    }
484

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

    
495
            uDest[i]= av_clip_uint8(u>>19);
496
            vDest[i]= av_clip_uint8(v>>19);
497
        }
498

    
499
    if (CONFIG_SWSCALE_ALPHA && aDest)
500
        for (i=0; i<dstW; i++) {
501
            int val=1<<18;
502
            int j;
503
            for (j=0; j<lumFilterSize; j++)
504
                val += alpSrc[j][i] * lumFilter[j];
505

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

    
509
}
510

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

    
523
        dest[i]= av_clip_uint8(val>>19);
524
    }
525

    
526
    if (!uDest)
527
        return;
528

    
529
    if (dstFormat == PIX_FMT_NV12)
530
        for (i=0; i<chrDstW; i++) {
531
            int u=1<<18;
532
            int v=1<<18;
533
            int j;
534
            for (j=0; j<chrFilterSize; j++) {
535
                u += chrSrc[j][i] * chrFilter[j];
536
                v += chrSrc[j][i + VOFW] * chrFilter[j];
537
            }
538

    
539
            uDest[2*i]= av_clip_uint8(u>>19);
540
            uDest[2*i+1]= av_clip_uint8(v>>19);
541
        }
542
    else
543
        for (i=0; i<chrDstW; i++) {
544
            int u=1<<18;
545
            int v=1<<18;
546
            int j;
547
            for (j=0; j<chrFilterSize; j++) {
548
                u += chrSrc[j][i] * chrFilter[j];
549
                v += chrSrc[j][i + VOFW] * chrFilter[j];
550
            }
551

    
552
            uDest[2*i]= av_clip_uint8(v>>19);
553
            uDest[2*i+1]= av_clip_uint8(u>>19);
554
        }
555
}
556

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

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

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

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

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

    
676
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
677
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
678
    r = (type *)c->table_rV[V];   \
679
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
680
    b = (type *)c->table_bU[U];
681

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

    
696
#define YSCALE_YUV_2_GRAY16_2_C   \
697
    for (i=0; i<(dstW>>1); i++) { \
698
        const int i2= 2*i;       \
699
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
700
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
701

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

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

    
722
#define YSCALE_YUV_2_GRAY16_1_C \
723
    for (i=0; i<(dstW>>1); i++) {\
724
        const int i2= 2*i;\
725
        int Y1= buf0[i2  ]<<1;\
726
        int Y2= buf0[i2+1]<<1;
727

    
728
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
729
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
730
    r = (type *)c->table_rV[V];\
731
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
732
    b = (type *)c->table_bU[U];
733

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

    
748
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
749
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
750
    r = (type *)c->table_rV[V];\
751
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
752
    b = (type *)c->table_bU[U];
753

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

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

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

    
989
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
990
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
991
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
992
{
993
    int i;
994
    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)
995
}
996

    
997
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
998
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
999
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1000
{
1001
    int i;
1002
    int step= fmt_depth(c->dstFormat)/8;
1003
    int aidx= 3;
1004

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

    
1081
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1082
{
1083
    int i;
1084
    uint8_t *ptr = plane + stride*y;
1085
    for (i=0; i<height; i++) {
1086
        memset(ptr, val, width);
1087
        ptr += stride;
1088
    }
1089
}
1090

    
1091
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
1092
                            uint32_t *unused)
1093
{
1094
    int i;
1095
    for (i = 0; i < width; i++) {
1096
        int r = src[i*6+0];
1097
        int g = src[i*6+2];
1098
        int b = src[i*6+4];
1099

    
1100
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1101
    }
1102
}
1103

    
1104
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1105
                             const uint8_t *src1, const uint8_t *src2,
1106
                             int width, uint32_t *unused)
1107
{
1108
    int i;
1109
    assert(src1==src2);
1110
    for (i = 0; i < width; i++) {
1111
        int r = src1[6*i + 0];
1112
        int g = src1[6*i + 2];
1113
        int b = src1[6*i + 4];
1114

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

    
1120
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1121
                                  const uint8_t *src1, const uint8_t *src2,
1122
                                  int width, uint32_t *unused)
1123
{
1124
    int i;
1125
    assert(src1==src2);
1126
    for (i = 0; i < width; i++) {
1127
        int r= src1[12*i + 0] + src1[12*i + 6];
1128
        int g= src1[12*i + 2] + src1[12*i + 8];
1129
        int b= src1[12*i + 4] + src1[12*i + 10];
1130

    
1131
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1132
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1133
    }
1134
}
1135

    
1136
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1137
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1138
{\
1139
    int i;\
1140
    for (i=0; i<width; i++) {\
1141
        int b= (((const type*)src)[i]>>shb)&maskb;\
1142
        int g= (((const type*)src)[i]>>shg)&maskg;\
1143
        int r= (((const type*)src)[i]>>shr)&maskr;\
1144
\
1145
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1146
    }\
1147
}
1148

    
1149
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1150
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1151
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1152
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1153
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1154
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1155

    
1156
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1157
{
1158
    int i;
1159
    for (i=0; i<width; i++) {
1160
        dst[i]= src[4*i];
1161
    }
1162
}
1163

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

    
1195
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1196
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1197
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0,          0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1198
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0,          0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1199
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0,          0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1200
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0,          0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1201

    
1202
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1203
{
1204
    int i;
1205
    for (i=0; i<width; i++) {
1206
        int d= src[i];
1207

    
1208
        dst[i]= pal[d] & 0xFF;
1209
    }
1210
}
1211

    
1212
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1213
                           const uint8_t *src1, const uint8_t *src2,
1214
                           long width, uint32_t *pal)
1215
{
1216
    int i;
1217
    assert(src1 == src2);
1218
    for (i=0; i<width; i++) {
1219
        int p= pal[src1[i]];
1220

    
1221
        dstU[i]= p>>8;
1222
        dstV[i]= p>>16;
1223
    }
1224
}
1225

    
1226
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1227
{
1228
    int i, j;
1229
    for (i=0; i<width/8; i++) {
1230
        int d= ~src[i];
1231
        for(j=0; j<8; j++)
1232
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1233
    }
1234
}
1235

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

    
1246
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1247
//Plain C versions
1248
#if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1249
#define COMPILE_C
1250
#endif
1251

    
1252
#if ARCH_PPC
1253
#if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1254
#define COMPILE_ALTIVEC
1255
#endif
1256
#endif //ARCH_PPC
1257

    
1258
#if ARCH_X86
1259

    
1260
#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1261
#define COMPILE_MMX
1262
#endif
1263

    
1264
#if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1265
#define COMPILE_MMX2
1266
#endif
1267

    
1268
#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1269
#define COMPILE_3DNOW
1270
#endif
1271
#endif //ARCH_X86
1272

    
1273
#define COMPILE_TEMPLATE_MMX 0
1274
#define COMPILE_TEMPLATE_MMX2 0
1275
#define COMPILE_TEMPLATE_AMD3DNOW 0
1276
#define COMPILE_TEMPLATE_ALTIVEC 0
1277

    
1278
#ifdef COMPILE_C
1279
#define RENAME(a) a ## _C
1280
#include "swscale_template.c"
1281
#endif
1282

    
1283
#ifdef COMPILE_ALTIVEC
1284
#undef RENAME
1285
#undef COMPILE_TEMPLATE_ALTIVEC
1286
#define COMPILE_TEMPLATE_ALTIVEC 1
1287
#define RENAME(a) a ## _altivec
1288
#include "swscale_template.c"
1289
#endif
1290

    
1291
#if ARCH_X86
1292

    
1293
//MMX versions
1294
#ifdef COMPILE_MMX
1295
#undef RENAME
1296
#undef COMPILE_TEMPLATE_MMX
1297
#undef COMPILE_TEMPLATE_MMX2
1298
#undef COMPILE_TEMPLATE_AMD3DNOW
1299
#define COMPILE_TEMPLATE_MMX 1
1300
#define COMPILE_TEMPLATE_MMX2 0
1301
#define COMPILE_TEMPLATE_AMD3DNOW 0
1302
#define RENAME(a) a ## _MMX
1303
#include "swscale_template.c"
1304
#endif
1305

    
1306
//MMX2 versions
1307
#ifdef COMPILE_MMX2
1308
#undef RENAME
1309
#undef COMPILE_TEMPLATE_MMX
1310
#undef COMPILE_TEMPLATE_MMX2
1311
#undef COMPILE_TEMPLATE_AMD3DNOW
1312
#define COMPILE_TEMPLATE_MMX 1
1313
#define COMPILE_TEMPLATE_MMX2 1
1314
#define COMPILE_TEMPLATE_AMD3DNOW 0
1315
#define RENAME(a) a ## _MMX2
1316
#include "swscale_template.c"
1317
#endif
1318

    
1319
//3DNOW versions
1320
#ifdef COMPILE_3DNOW
1321
#undef RENAME
1322
#undef COMPILE_TEMPLATE_MMX
1323
#undef COMPILE_TEMPLATE_MMX2
1324
#undef COMPILE_TEMPLATE_AMD3DNOW
1325
#define COMPILE_TEMPLATE_MMX 1
1326
#define COMPILE_TEMPLATE_MMX2 0
1327
#define COMPILE_TEMPLATE_AMD3DNOW 1
1328
#define RENAME(a) a ## _3DNow
1329
#include "swscale_template.c"
1330
#endif
1331

    
1332
#endif //ARCH_X86
1333

    
1334
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1335
{
1336
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1337
    if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1338
    else           return getSplineCoeff(        0.0,
1339
                                          b+ 2.0*c + 3.0*d,
1340
                                                 c + 3.0*d,
1341
                                         -b- 3.0*c - 6.0*d,
1342
                                         dist-1.0);
1343
}
1344

    
1345
static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1346
                      int srcW, int dstW, int filterAlign, int one, int flags,
1347
                      SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1348
{
1349
    int i;
1350
    int filterSize;
1351
    int filter2Size;
1352
    int minFilterSize;
1353
    int64_t *filter=NULL;
1354
    int64_t *filter2=NULL;
1355
    const int64_t fone= 1LL<<54;
1356
    int ret= -1;
1357
#if ARCH_X86
1358
    if (flags & SWS_CPU_CAPS_MMX)
1359
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1360
#endif
1361

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

    
1365
    if (FFABS(xInc - 0x10000) <10) { // unscaled
1366
        int i;
1367
        filterSize= 1;
1368
        FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1369

    
1370
        for (i=0; i<dstW; i++) {
1371
            filter[i*filterSize]= fone;
1372
            (*filterPos)[i]=i;
1373
        }
1374

    
1375
    } else if (flags&SWS_POINT) { // lame looking point sampling mode
1376
        int i;
1377
        int xDstInSrc;
1378
        filterSize= 1;
1379
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1380

    
1381
        xDstInSrc= xInc/2 - 0x8000;
1382
        for (i=0; i<dstW; i++) {
1383
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1384

    
1385
            (*filterPos)[i]= xx;
1386
            filter[i]= fone;
1387
            xDstInSrc+= xInc;
1388
        }
1389
    } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1390
        int i;
1391
        int xDstInSrc;
1392
        filterSize= 2;
1393
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1394

    
1395
        xDstInSrc= xInc/2 - 0x8000;
1396
        for (i=0; i<dstW; i++) {
1397
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1398
            int j;
1399

    
1400
            (*filterPos)[i]= xx;
1401
            //bilinear upscale / linear interpolate / area averaging
1402
            for (j=0; j<filterSize; j++) {
1403
                int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1404
                if (coeff<0) coeff=0;
1405
                filter[i*filterSize + j]= coeff;
1406
                xx++;
1407
            }
1408
            xDstInSrc+= xInc;
1409
        }
1410
    } else {
1411
        int xDstInSrc;
1412
        int sizeFactor;
1413

    
1414
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1415
        else if (flags&SWS_X)            sizeFactor=  8;
1416
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1417
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1418
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1419
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1420
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1421
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1422
        else {
1423
            sizeFactor= 0; //GCC warning killer
1424
            assert(0);
1425
        }
1426

    
1427
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1428
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1429

    
1430
        if (filterSize > srcW-2) filterSize=srcW-2;
1431

    
1432
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1433

    
1434
        xDstInSrc= xInc - 0x10000;
1435
        for (i=0; i<dstW; i++) {
1436
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1437
            int j;
1438
            (*filterPos)[i]= xx;
1439
            for (j=0; j<filterSize; j++) {
1440
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1441
                double floatd;
1442
                int64_t coeff;
1443

    
1444
                if (xInc > 1<<16)
1445
                    d= d*dstW/srcW;
1446
                floatd= d * (1.0/(1<<30));
1447

    
1448
                if (flags & SWS_BICUBIC) {
1449
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1450
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1451
                    int64_t dd = ( d*d)>>30;
1452
                    int64_t ddd= (dd*d)>>30;
1453

    
1454
                    if      (d < 1LL<<30)
1455
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1456
                    else if (d < 1LL<<31)
1457
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1458
                    else
1459
                        coeff=0.0;
1460
                    coeff *= fone>>(30+24);
1461
                }
1462
/*                else if (flags & SWS_X) {
1463
                    double p= param ? param*0.01 : 0.3;
1464
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1465
                    coeff*= pow(2.0, - p*d*d);
1466
                }*/
1467
                else if (flags & SWS_X) {
1468
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1469
                    double c;
1470

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

    
1505
                filter[i*filterSize + j]= coeff;
1506
                xx++;
1507
            }
1508
            xDstInSrc+= 2*xInc;
1509
        }
1510
    }
1511

    
1512
    /* apply src & dst Filter to filter -> filter2
1513
       av_free(filter);
1514
    */
1515
    assert(filterSize>0);
1516
    filter2Size= filterSize;
1517
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1518
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1519
    assert(filter2Size>0);
1520
    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
1521

    
1522
    for (i=0; i<dstW; i++) {
1523
        int j, k;
1524

    
1525
        if(srcFilter) {
1526
            for (k=0; k<srcFilter->length; k++) {
1527
                for (j=0; j<filterSize; j++)
1528
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1529
            }
1530
        } else {
1531
            for (j=0; j<filterSize; j++)
1532
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1533
        }
1534
        //FIXME dstFilter
1535

    
1536
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1537
    }
1538
    av_freep(&filter);
1539

    
1540
    /* try to reduce the filter-size (step1 find size and shift left) */
1541
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1542
    minFilterSize= 0;
1543
    for (i=dstW-1; i>=0; i--) {
1544
        int min= filter2Size;
1545
        int j;
1546
        int64_t cutOff=0.0;
1547

    
1548
        /* get rid of near zero elements on the left by shifting left */
1549
        for (j=0; j<filter2Size; j++) {
1550
            int k;
1551
            cutOff += FFABS(filter2[i*filter2Size]);
1552

    
1553
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1554

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

    
1558
            // move filter coefficients left
1559
            for (k=1; k<filter2Size; k++)
1560
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1561
            filter2[i*filter2Size + k - 1]= 0;
1562
            (*filterPos)[i]++;
1563
        }
1564

    
1565
        cutOff=0;
1566
        /* count near zeros on the right */
1567
        for (j=filter2Size-1; j>0; j--) {
1568
            cutOff += FFABS(filter2[i*filter2Size + j]);
1569

    
1570
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1571
            min--;
1572
        }
1573

    
1574
        if (min>minFilterSize) minFilterSize= min;
1575
    }
1576

    
1577
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1578
        // we can handle the special case 4,
1579
        // so we don't want to go to the full 8
1580
        if (minFilterSize < 5)
1581
            filterAlign = 4;
1582

    
1583
        // We really don't want to waste our time
1584
        // doing useless computation, so fall back on
1585
        // the scalar C code for very small filters.
1586
        // Vectorizing is worth it only if you have a
1587
        // decent-sized vector.
1588
        if (minFilterSize < 3)
1589
            filterAlign = 1;
1590
    }
1591

    
1592
    if (flags & SWS_CPU_CAPS_MMX) {
1593
        // special case for unscaled vertical filtering
1594
        if (minFilterSize == 1 && filterAlign == 2)
1595
            filterAlign= 1;
1596
    }
1597

    
1598
    assert(minFilterSize > 0);
1599
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1600
    assert(filterSize > 0);
1601
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1602
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1603
        goto fail;
1604
    *outFilterSize= filterSize;
1605

    
1606
    if (flags&SWS_PRINT_INFO)
1607
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1608
    /* try to reduce the filter-size (step2 reduce it) */
1609
    for (i=0; i<dstW; i++) {
1610
        int j;
1611

    
1612
        for (j=0; j<filterSize; j++) {
1613
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1614
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1615
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1616
                filter[i*filterSize + j]= 0;
1617
        }
1618
    }
1619

    
1620
    //FIXME try to align filterPos if possible
1621

    
1622
    //fix borders
1623
    for (i=0; i<dstW; i++) {
1624
        int j;
1625
        if ((*filterPos)[i] < 0) {
1626
            // move filter coefficients left to compensate for filterPos
1627
            for (j=1; j<filterSize; j++) {
1628
                int left= FFMAX(j + (*filterPos)[i], 0);
1629
                filter[i*filterSize + left] += filter[i*filterSize + j];
1630
                filter[i*filterSize + j]=0;
1631
            }
1632
            (*filterPos)[i]= 0;
1633
        }
1634

    
1635
        if ((*filterPos)[i] + filterSize > srcW) {
1636
            int shift= (*filterPos)[i] + filterSize - srcW;
1637
            // move filter coefficients right to compensate for filterPos
1638
            for (j=filterSize-2; j>=0; j--) {
1639
                int right= FFMIN(j + shift, filterSize-1);
1640
                filter[i*filterSize +right] += filter[i*filterSize +j];
1641
                filter[i*filterSize +j]=0;
1642
            }
1643
            (*filterPos)[i]= srcW - filterSize;
1644
        }
1645
    }
1646

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

    
1651
    /* normalize & store in outFilter */
1652
    for (i=0; i<dstW; i++) {
1653
        int j;
1654
        int64_t error=0;
1655
        int64_t sum=0;
1656

    
1657
        for (j=0; j<filterSize; j++) {
1658
            sum+= filter[i*filterSize + j];
1659
        }
1660
        sum= (sum + one/2)/ one;
1661
        for (j=0; j<*outFilterSize; j++) {
1662
            int64_t v= filter[i*filterSize + j] + error;
1663
            int intV= ROUNDED_DIV(v, sum);
1664
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1665
            error= v - intV*sum;
1666
        }
1667
    }
1668

    
1669
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1670
    for (i=0; i<*outFilterSize; i++) {
1671
        int j= dstW*(*outFilterSize);
1672
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1673
    }
1674

    
1675
    ret=0;
1676
fail:
1677
    av_free(filter);
1678
    av_free(filter2);
1679
    return ret;
1680
}
1681

    
1682
#ifdef COMPILE_MMX2
1683
static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1684
{
1685
    uint8_t *fragmentA;
1686
    x86_reg imm8OfPShufW1A;
1687
    x86_reg imm8OfPShufW2A;
1688
    x86_reg fragmentLengthA;
1689
    uint8_t *fragmentB;
1690
    x86_reg imm8OfPShufW1B;
1691
    x86_reg imm8OfPShufW2B;
1692
    x86_reg fragmentLengthB;
1693
    int fragmentPos;
1694

    
1695
    int xpos, i;
1696

    
1697
    // create an optimized horizontal scaling routine
1698
    /* This scaler is made of runtime-generated MMX2 code using specially
1699
     * tuned pshufw instructions. For every four output pixels, if four
1700
     * input pixels are enough for the fast bilinear scaling, then a chunk
1701
     * of fragmentB is used. If five input pixels are needed, then a chunk
1702
     * of fragmentA is used.
1703
     */
1704

    
1705
    //code fragment
1706

    
1707
    __asm__ volatile(
1708
        "jmp                         9f                 \n\t"
1709
    // Begin
1710
        "0:                                             \n\t"
1711
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1712
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1713
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1714
        "punpcklbw                %%mm7, %%mm1          \n\t"
1715
        "punpcklbw                %%mm7, %%mm0          \n\t"
1716
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1717
        "1:                                             \n\t"
1718
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1719
        "2:                                             \n\t"
1720
        "psubw                    %%mm1, %%mm0          \n\t"
1721
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1722
        "pmullw                   %%mm3, %%mm0          \n\t"
1723
        "psllw                       $7, %%mm1          \n\t"
1724
        "paddw                    %%mm1, %%mm0          \n\t"
1725

    
1726
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1727

    
1728
        "add                         $8, %%"REG_a"      \n\t"
1729
    // End
1730
        "9:                                             \n\t"
1731
//        "int $3                                         \n\t"
1732
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1733
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1734
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1735
        "dec                         %1                 \n\t"
1736
        "dec                         %2                 \n\t"
1737
        "sub                         %0, %1             \n\t"
1738
        "sub                         %0, %2             \n\t"
1739
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1740
        "sub                         %0, %3             \n\t"
1741

    
1742

    
1743
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1744
        "=r" (fragmentLengthA)
1745
    );
1746

    
1747
    __asm__ volatile(
1748
        "jmp                         9f                 \n\t"
1749
    // Begin
1750
        "0:                                             \n\t"
1751
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1752
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1753
        "punpcklbw                %%mm7, %%mm0          \n\t"
1754
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1755
        "1:                                             \n\t"
1756
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1757
        "2:                                             \n\t"
1758
        "psubw                    %%mm1, %%mm0          \n\t"
1759
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1760
        "pmullw                   %%mm3, %%mm0          \n\t"
1761
        "psllw                       $7, %%mm1          \n\t"
1762
        "paddw                    %%mm1, %%mm0          \n\t"
1763

    
1764
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1765

    
1766
        "add                         $8, %%"REG_a"      \n\t"
1767
    // End
1768
        "9:                                             \n\t"
1769
//        "int                       $3                   \n\t"
1770
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1771
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1772
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1773
        "dec                         %1                 \n\t"
1774
        "dec                         %2                 \n\t"
1775
        "sub                         %0, %1             \n\t"
1776
        "sub                         %0, %2             \n\t"
1777
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1778
        "sub                         %0, %3             \n\t"
1779

    
1780

    
1781
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1782
        "=r" (fragmentLengthB)
1783
    );
1784

    
1785
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1786
    fragmentPos=0;
1787

    
1788
    for (i=0; i<dstW/numSplits; i++) {
1789
        int xx=xpos>>16;
1790

    
1791
        if ((i&3) == 0) {
1792
            int a=0;
1793
            int b=((xpos+xInc)>>16) - xx;
1794
            int c=((xpos+xInc*2)>>16) - xx;
1795
            int d=((xpos+xInc*3)>>16) - xx;
1796
            int inc                = (d+1<4);
1797
            uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
1798
            x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
1799
            x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
1800
            x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1801
            int maxShift= 3-(d+inc);
1802
            int shift=0;
1803

    
1804
            if (filterCode) {
1805
                filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1806
                filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1807
                filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1808
                filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1809
                filterPos[i/2]= xx;
1810

    
1811
                memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1812

    
1813
                filterCode[fragmentPos + imm8OfPShufW1]=
1814
                    (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1815
                filterCode[fragmentPos + imm8OfPShufW2]=
1816
                    a | (b<<2) | (c<<4) | (d<<6);
1817

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

    
1821
                if (shift && i>=shift) {
1822
                    filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1823
                    filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1824
                    filterPos[i/2]-=shift;
1825
                }
1826
            }
1827

    
1828
            fragmentPos+= fragmentLength;
1829

    
1830
            if (filterCode)
1831
                filterCode[fragmentPos]= RET;
1832
        }
1833
        xpos+=xInc;
1834
    }
1835
    if (filterCode)
1836
        filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1837

    
1838
    return fragmentPos + 1;
1839
}
1840
#endif /* COMPILE_MMX2 */
1841

    
1842
static SwsFunc getSwsFunc(SwsContext *c)
1843
{
1844
#if CONFIG_RUNTIME_CPUDETECT
1845
    int flags = c->flags;
1846

    
1847
#if ARCH_X86 && CONFIG_GPL
1848
    // ordered per speed fastest first
1849
    if (flags & SWS_CPU_CAPS_MMX2) {
1850
        sws_init_swScale_MMX2(c);
1851
        return swScale_MMX2;
1852
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1853
        sws_init_swScale_3DNow(c);
1854
        return swScale_3DNow;
1855
    } else if (flags & SWS_CPU_CAPS_MMX) {
1856
        sws_init_swScale_MMX(c);
1857
        return swScale_MMX;
1858
    } else {
1859
        sws_init_swScale_C(c);
1860
        return swScale_C;
1861
    }
1862

    
1863
#else
1864
#if ARCH_PPC
1865
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1866
        sws_init_swScale_altivec(c);
1867
        return swScale_altivec;
1868
    } else {
1869
        sws_init_swScale_C(c);
1870
        return swScale_C;
1871
    }
1872
#endif
1873
    sws_init_swScale_C(c);
1874
    return swScale_C;
1875
#endif /* ARCH_X86 && CONFIG_GPL */
1876
#else //CONFIG_RUNTIME_CPUDETECT
1877
#if   COMPILE_TEMPLATE_MMX2
1878
    sws_init_swScale_MMX2(c);
1879
    return swScale_MMX2;
1880
#elif COMPILE_TEMPLATE_AMD3DNOW
1881
    sws_init_swScale_3DNow(c);
1882
    return swScale_3DNow;
1883
#elif COMPILE_TEMPLATE_MMX
1884
    sws_init_swScale_MMX(c);
1885
    return swScale_MMX;
1886
#elif COMPILE_TEMPLATE_ALTIVEC
1887
    sws_init_swScale_altivec(c);
1888
    return swScale_altivec;
1889
#else
1890
    sws_init_swScale_C(c);
1891
    return swScale_C;
1892
#endif
1893
#endif //!CONFIG_RUNTIME_CPUDETECT
1894
}
1895

    
1896
static int PlanarToNV12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1897
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1898
{
1899
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1900
    /* Copy Y plane */
1901
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1902
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1903
    else {
1904
        int i;
1905
        const uint8_t *srcPtr= src[0];
1906
        uint8_t *dstPtr= dst;
1907
        for (i=0; i<srcSliceH; i++) {
1908
            memcpy(dstPtr, srcPtr, c->srcW);
1909
            srcPtr+= srcStride[0];
1910
            dstPtr+= dstStride[0];
1911
        }
1912
    }
1913
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1914
    if (c->dstFormat == PIX_FMT_NV12)
1915
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1916
    else
1917
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1918

    
1919
    return srcSliceH;
1920
}
1921

    
1922
static int PlanarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1923
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1924
{
1925
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1926

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

    
1929
    return srcSliceH;
1930
}
1931

    
1932
static int PlanarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1933
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1934
{
1935
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1936

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

    
1939
    return srcSliceH;
1940
}
1941

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

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

    
1949
    return srcSliceH;
1950
}
1951

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

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

    
1959
    return srcSliceH;
1960
}
1961

    
1962
static int YUYV2YUV420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1963
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1964
{
1965
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1966
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1967
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1968

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

    
1971
    if (dstParam[3])
1972
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1973

    
1974
    return srcSliceH;
1975
}
1976

    
1977
static int YUYV2YUV422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1978
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1979
{
1980
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1981
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1982
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1983

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

    
1986
    return srcSliceH;
1987
}
1988

    
1989
static int UYVY2YUV420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1990
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1991
{
1992
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1993
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1994
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1995

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

    
1998
    if (dstParam[3])
1999
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2000

    
2001
    return srcSliceH;
2002
}
2003

    
2004
static int UYVY2YUV422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2005
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
2006
{
2007
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2008
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2009
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2010

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

    
2013
    return srcSliceH;
2014
}
2015

    
2016
static int pal2rgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2017
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2018
{
2019
    const enum PixelFormat srcFormat= c->srcFormat;
2020
    const enum PixelFormat dstFormat= c->dstFormat;
2021
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2022
                 const uint8_t *palette)=NULL;
2023
    int i;
2024
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2025
    const uint8_t *srcPtr= src[0];
2026

    
2027
    if (usePal(srcFormat)) {
2028
        switch (dstFormat) {
2029
        case PIX_FMT_RGB32  : conv = palette8topacked32; break;
2030
        case PIX_FMT_BGR32  : conv = palette8topacked32; break;
2031
        case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2032
        case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2033
        case PIX_FMT_RGB24  : conv = palette8topacked24; break;
2034
        case PIX_FMT_BGR24  : conv = palette8topacked24; break;
2035
        }
2036
    }
2037

    
2038
    if (!conv)
2039
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2040
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2041
    else {
2042
        for (i=0; i<srcSliceH; i++) {
2043
            conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2044
            srcPtr+= srcStride[0];
2045
            dstPtr+= dstStride[0];
2046
        }
2047
    }
2048

    
2049
    return srcSliceH;
2050
}
2051

    
2052
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2053
static int rgb2rgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2054
                          int srcSliceH, uint8_t* dst[], int dstStride[])
2055
{
2056
    const enum PixelFormat srcFormat= c->srcFormat;
2057
    const enum PixelFormat dstFormat= c->dstFormat;
2058
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2059
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2060
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2061
    const int dstId= fmt_depth(dstFormat) >> 2;
2062
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2063

    
2064
    /* BGR -> BGR */
2065
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
2066
       || (isRGB(srcFormat) && isRGB(dstFormat))) {
2067
        switch(srcId | (dstId<<4)) {
2068
        case 0x34: conv= rgb16to15; break;
2069
        case 0x36: conv= rgb24to15; break;
2070
        case 0x38: conv= rgb32to15; break;
2071
        case 0x43: conv= rgb15to16; break;
2072
        case 0x46: conv= rgb24to16; break;
2073
        case 0x48: conv= rgb32to16; break;
2074
        case 0x63: conv= rgb15to24; break;
2075
        case 0x64: conv= rgb16to24; break;
2076
        case 0x68: conv= rgb32to24; break;
2077
        case 0x83: conv= rgb15to32; break;
2078
        case 0x84: conv= rgb16to32; break;
2079
        case 0x86: conv= rgb24to32; break;
2080
        }
2081
    } else if (  (isBGR(srcFormat) && isRGB(dstFormat))
2082
             || (isRGB(srcFormat) && isBGR(dstFormat))) {
2083
        switch(srcId | (dstId<<4)) {
2084
        case 0x33: conv= rgb15tobgr15; break;
2085
        case 0x34: conv= rgb16tobgr15; break;
2086
        case 0x36: conv= rgb24tobgr15; break;
2087
        case 0x38: conv= rgb32tobgr15; break;
2088
        case 0x43: conv= rgb15tobgr16; break;
2089
        case 0x44: conv= rgb16tobgr16; break;
2090
        case 0x46: conv= rgb24tobgr16; break;
2091
        case 0x48: conv= rgb32tobgr16; break;
2092
        case 0x63: conv= rgb15tobgr24; break;
2093
        case 0x64: conv= rgb16tobgr24; break;
2094
        case 0x66: conv= rgb24tobgr24; break;
2095
        case 0x68: conv= rgb32tobgr24; break;
2096
        case 0x83: conv= rgb15tobgr32; break;
2097
        case 0x84: conv= rgb16tobgr32; break;
2098
        case 0x86: conv= rgb24tobgr32; break;
2099
        case 0x88: conv= rgb32tobgr32; break;
2100
        }
2101
    }
2102

    
2103
    if (!conv) {
2104
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2105
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2106
    } else {
2107
        const uint8_t *srcPtr= src[0];
2108
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2109
            srcPtr += ALT32_CORR;
2110

    
2111
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2112
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2113
        else {
2114
            int i;
2115
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2116

    
2117
            for (i=0; i<srcSliceH; i++) {
2118
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
2119
                srcPtr+= srcStride[0];
2120
                dstPtr+= dstStride[0];
2121
            }
2122
        }
2123
    }
2124
    return srcSliceH;
2125
}
2126

    
2127
static int bgr24toyv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2128
                              int srcSliceH, uint8_t* dst[], int dstStride[])
2129
{
2130
    rgb24toyv12(
2131
        src[0],
2132
        dst[0]+ srcSliceY    *dstStride[0],
2133
        dst[1]+(srcSliceY>>1)*dstStride[1],
2134
        dst[2]+(srcSliceY>>1)*dstStride[2],
2135
        c->srcW, srcSliceH,
2136
        dstStride[0], dstStride[1], srcStride[0]);
2137
    if (dst[3])
2138
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2139
    return srcSliceH;
2140
}
2141

    
2142
static int yvu9toyv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2143
                             int srcSliceH, uint8_t* dst[], int dstStride[])
2144
{
2145
    int i;
2146

    
2147
    /* copy Y */
2148
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2149
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2150
    else {
2151
        const uint8_t *srcPtr= src[0];
2152
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2153

    
2154
        for (i=0; i<srcSliceH; i++) {
2155
            memcpy(dstPtr, srcPtr, c->srcW);
2156
            srcPtr+= srcStride[0];
2157
            dstPtr+= dstStride[0];
2158
        }
2159
    }
2160

    
2161
    if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
2162
        planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2163
                 srcSliceH >> 2, srcStride[1], dstStride[1]);
2164
        planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2165
                 srcSliceH >> 2, srcStride[2], dstStride[2]);
2166
    } else {
2167
        planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2168
                 srcSliceH >> 2, srcStride[1], dstStride[2]);
2169
        planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2170
                 srcSliceH >> 2, srcStride[2], dstStride[1]);
2171
    }
2172
    if (dst[3])
2173
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2174
    return srcSliceH;
2175
}
2176

    
2177
/* unscaled copy like stuff (assumes nearly identical formats) */
2178
static int packedCopy(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2179
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2180
{
2181
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2182
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2183
    else {
2184
        int i;
2185
        const uint8_t *srcPtr= src[0];
2186
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2187
        int length=0;
2188

    
2189
        /* universal length finder */
2190
        while(length+c->srcW <= FFABS(dstStride[0])
2191
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2192
        assert(length!=0);
2193

    
2194
        for (i=0; i<srcSliceH; i++) {
2195
            memcpy(dstPtr, srcPtr, length);
2196
            srcPtr+= srcStride[0];
2197
            dstPtr+= dstStride[0];
2198
        }
2199
    }
2200
    return srcSliceH;
2201
}
2202

    
2203
static int planarCopy(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
2204
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2205
{
2206
    int plane, i, j;
2207
    for (plane=0; plane<4; plane++) {
2208
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2209
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2210
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2211
        const uint8_t *srcPtr= src[plane];
2212
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2213

    
2214
        if (!dst[plane]) continue;
2215
        // ignore palette for GRAY8
2216
        if (plane == 1 && !dst[2]) continue;
2217
        if (!src[plane] || (plane == 1 && !src[2])) {
2218
            if(is16BPS(c->dstFormat))
2219
                length*=2;
2220
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2221
        } else {
2222
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
2223
                if (!isBE(c->srcFormat)) srcPtr++;
2224
                for (i=0; i<height; i++) {
2225
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2226
                    srcPtr+= srcStride[plane];
2227
                    dstPtr+= dstStride[plane];
2228
                }
2229
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
2230
                for (i=0; i<height; i++) {
2231
                    for (j=0; j<length; j++) {
2232
                        dstPtr[ j<<1   ] = srcPtr[j];
2233
                        dstPtr[(j<<1)+1] = srcPtr[j];
2234
                    }
2235
                    srcPtr+= srcStride[plane];
2236
                    dstPtr+= dstStride[plane];
2237
                }
2238
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2239
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
2240

    
2241
                for (i=0; i<height; i++) {
2242
                    for (j=0; j<length; j++)
2243
                        ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
2244
                    srcPtr+= srcStride[plane];
2245
                    dstPtr+= dstStride[plane];
2246
                }
2247
            } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2248
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2249
            else {
2250
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2251
                    length*=2;
2252
                for (i=0; i<height; i++) {
2253
                    memcpy(dstPtr, srcPtr, length);
2254
                    srcPtr+= srcStride[plane];
2255
                    dstPtr+= dstStride[plane];
2256
                }
2257
            }
2258
        }
2259
    }
2260
    return srcSliceH;
2261
}
2262

    
2263
static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
2264
{
2265
    *h = av_pix_fmt_descriptors[format].log2_chroma_w;
2266
    *v = av_pix_fmt_descriptors[format].log2_chroma_h;
2267
}
2268

    
2269
static uint16_t roundToInt16(int64_t f)
2270
{
2271
    int r= (f + (1<<15))>>16;
2272
         if (r<-0x7FFF) return 0x8000;
2273
    else if (r> 0x7FFF) return 0x7FFF;
2274
    else                return r;
2275
}
2276

    
2277
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
2278
{
2279
    int64_t crv =  inv_table[0];
2280
    int64_t cbu =  inv_table[1];
2281
    int64_t cgu = -inv_table[2];
2282
    int64_t cgv = -inv_table[3];
2283
    int64_t cy  = 1<<16;
2284
    int64_t oy  = 0;
2285

    
2286
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2287
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
2288

    
2289
    c->brightness= brightness;
2290
    c->contrast  = contrast;
2291
    c->saturation= saturation;
2292
    c->srcRange  = srcRange;
2293
    c->dstRange  = dstRange;
2294
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2295

    
2296
    c->uOffset=   0x0400040004000400LL;
2297
    c->vOffset=   0x0400040004000400LL;
2298

    
2299
    if (!srcRange) {
2300
        cy= (cy*255) / 219;
2301
        oy= 16<<16;
2302
    } else {
2303
        crv= (crv*224) / 255;
2304
        cbu= (cbu*224) / 255;
2305
        cgu= (cgu*224) / 255;
2306
        cgv= (cgv*224) / 255;
2307
    }
2308

    
2309
    cy = (cy *contrast             )>>16;
2310
    crv= (crv*contrast * saturation)>>32;
2311
    cbu= (cbu*contrast * saturation)>>32;
2312
    cgu= (cgu*contrast * saturation)>>32;
2313
    cgv= (cgv*contrast * saturation)>>32;
2314

    
2315
    oy -= 256*brightness;
2316

    
2317
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
2318
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
2319
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2320
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2321
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2322
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
2323

    
2324
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
2325
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2326
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2327
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2328
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2329
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2330

    
2331
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2332
    //FIXME factorize
2333

    
2334
#ifdef COMPILE_ALTIVEC
2335
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2336
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2337
#endif
2338
    return 0;
2339
}
2340

    
2341
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
2342
{
2343
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2344

    
2345
    *inv_table = c->srcColorspaceTable;
2346
    *table     = c->dstColorspaceTable;
2347
    *srcRange  = c->srcRange;
2348
    *dstRange  = c->dstRange;
2349
    *brightness= c->brightness;
2350
    *contrast  = c->contrast;
2351
    *saturation= c->saturation;
2352

    
2353
    return 0;
2354
}
2355

    
2356
static int handle_jpeg(enum PixelFormat *format)
2357
{
2358
    switch (*format) {
2359
    case PIX_FMT_YUVJ420P:
2360
        *format = PIX_FMT_YUV420P;
2361
        return 1;
2362
    case PIX_FMT_YUVJ422P:
2363
        *format = PIX_FMT_YUV422P;
2364
        return 1;
2365
    case PIX_FMT_YUVJ444P:
2366
        *format = PIX_FMT_YUV444P;
2367
        return 1;
2368
    case PIX_FMT_YUVJ440P:
2369
        *format = PIX_FMT_YUV440P;
2370
        return 1;
2371
    default:
2372
        return 0;
2373
    }
2374
}
2375

    
2376
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
2377
                           int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2378
                           SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2379
{
2380

    
2381
    SwsContext *c;
2382
    int i;
2383
    int usesVFilter, usesHFilter;
2384
    int unscaled, needsDither;
2385
    int srcRange, dstRange;
2386
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2387
#if ARCH_X86
2388
    if (flags & SWS_CPU_CAPS_MMX)
2389
        __asm__ volatile("emms\n\t"::: "memory");
2390
#endif
2391

    
2392
#if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2393
    flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2394
#if   COMPILE_TEMPLATE_MMX2
2395
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2396
#elif COMPILE_TEMPLATE_AMD3DNOW
2397
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2398
#elif COMPILE_TEMPLATE_MMX
2399
    flags |= SWS_CPU_CAPS_MMX;
2400
#elif COMPILE_TEMPLATE_ALTIVEC
2401
    flags |= SWS_CPU_CAPS_ALTIVEC;
2402
#elif ARCH_BFIN
2403
    flags |= SWS_CPU_CAPS_BFIN;
2404
#endif
2405
#endif /* CONFIG_RUNTIME_CPUDETECT */
2406
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2407

    
2408
    unscaled = (srcW == dstW && srcH == dstH);
2409
    needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2410
        && (fmt_depth(dstFormat))<24
2411
        && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2412

    
2413
    srcRange = handle_jpeg(&srcFormat);
2414
    dstRange = handle_jpeg(&dstFormat);
2415

    
2416
    if (!isSupportedIn(srcFormat)) {
2417
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2418
        return NULL;
2419
    }
2420
    if (!isSupportedOut(dstFormat)) {
2421
        av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2422
        return NULL;
2423
    }
2424

    
2425
    i= flags & ( SWS_POINT
2426
                |SWS_AREA
2427
                |SWS_BILINEAR
2428
                |SWS_FAST_BILINEAR
2429
                |SWS_BICUBIC
2430
                |SWS_X
2431
                |SWS_GAUSS
2432
                |SWS_LANCZOS
2433
                |SWS_SINC
2434
                |SWS_SPLINE
2435
                |SWS_BICUBLIN);
2436
    if(!i || (i & (i-1))) {
2437
        av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2438
        return NULL;
2439
    }
2440

    
2441
    /* sanity check */
2442
    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
2443
        av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2444
               srcW, srcH, dstW, dstH);
2445
        return NULL;
2446
    }
2447
    if(srcW > VOFW || dstW > VOFW) {
2448
        av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2449
        return NULL;
2450
    }
2451

    
2452
    if (!dstFilter) dstFilter= &dummyFilter;
2453
    if (!srcFilter) srcFilter= &dummyFilter;
2454

    
2455
    FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
2456

    
2457
    c->av_class = &sws_context_class;
2458
    c->srcW= srcW;
2459
    c->srcH= srcH;
2460
    c->dstW= dstW;
2461
    c->dstH= dstH;
2462
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2463
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2464
    c->flags= flags;
2465
    c->dstFormat= dstFormat;
2466
    c->srcFormat= srcFormat;
2467
    c->vRounder= 4* 0x0001000100010001ULL;
2468

    
2469
    usesHFilter= usesVFilter= 0;
2470
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2471
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2472
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2473
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2474
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2475
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2476
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2477
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2478

    
2479
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2480
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2481

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

    
2485
    // drop some chroma lines if the user wants it
2486
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2487
    c->chrSrcVSubSample+= c->vChrDrop;
2488

    
2489
    // drop every other pixel for chroma calculation unless user wants full chroma
2490
    if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2491
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
2492
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
2493
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2494
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2495
        c->chrSrcHSubSample=1;
2496

    
2497
    if (param) {
2498
        c->param[0] = param[0];
2499
        c->param[1] = param[1];
2500
    } else {
2501
        c->param[0] =
2502
        c->param[1] = SWS_PARAM_DEFAULT;
2503
    }
2504

    
2505
    // Note the -((-x)>>y) is so that we always round toward +inf.
2506
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2507
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2508
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2509
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2510

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

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

    
2525
        if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2526
            c->swScale= yvu9toyv12Wrapper;
2527
        }
2528

    
2529
        /* bgr24toYV12 */
2530
        if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2531
            c->swScale= bgr24toyv12Wrapper;
2532

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

    
2551
        if ((usePal(srcFormat) && (
2552
                 dstFormat == PIX_FMT_RGB32   ||
2553
                 dstFormat == PIX_FMT_RGB32_1 ||
2554
                 dstFormat == PIX_FMT_RGB24   ||
2555
                 dstFormat == PIX_FMT_BGR32   ||
2556
                 dstFormat == PIX_FMT_BGR32_1 ||
2557
                 dstFormat == PIX_FMT_BGR24)))
2558
             c->swScale= pal2rgbWrapper;
2559

    
2560
        if (srcFormat == PIX_FMT_YUV422P) {
2561
            if (dstFormat == PIX_FMT_YUYV422)
2562
                c->swScale= YUV422PToYuy2Wrapper;
2563
            else if (dstFormat == PIX_FMT_UYVY422)
2564
                c->swScale= YUV422PToUyvyWrapper;
2565
        }
2566

    
2567
        /* LQ converters if -sws 0 or -sws 4*/
2568
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2569
            /* yv12_to_yuy2 */
2570
            if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2571
                if (dstFormat == PIX_FMT_YUYV422)
2572
                    c->swScale= PlanarToYuy2Wrapper;
2573
                else if (dstFormat == PIX_FMT_UYVY422)
2574
                    c->swScale= PlanarToUyvyWrapper;
2575
            }
2576
        }
2577
        if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2578
            c->swScale= YUYV2YUV420Wrapper;
2579
        if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2580
            c->swScale= UYVY2YUV420Wrapper;
2581
        if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2582
            c->swScale= YUYV2YUV422Wrapper;
2583
        if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2584
            c->swScale= UYVY2YUV422Wrapper;
2585

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

    
2598
        /* simple copy */
2599
        if (  srcFormat == dstFormat
2600
            || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2601
            || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2602
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2603
            || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2604
            || (isGray(dstFormat) && isGray(srcFormat))
2605
            || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2606
                && c->chrDstHSubSample == c->chrSrcHSubSample
2607
                && c->chrDstVSubSample == c->chrSrcVSubSample
2608
                && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2609
                && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2610
        {
2611
            if (isPacked(c->srcFormat))
2612
                c->swScale= packedCopy;
2613
            else /* Planar YUV or gray */
2614
                c->swScale= planarCopy;
2615
        }
2616
#if ARCH_BFIN
2617
        if (flags & SWS_CPU_CAPS_BFIN)
2618
            ff_bfin_get_unscaled_swscale (c);
2619
#endif
2620

    
2621
        if (c->swScale) {
2622
            if (flags&SWS_PRINT_INFO)
2623
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2624
                       sws_format_name(srcFormat), sws_format_name(dstFormat));
2625
            return c;
2626
        }
2627
    }
2628

    
2629
    if (flags & SWS_CPU_CAPS_MMX2) {
2630
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2631
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
2632
            if (flags&SWS_PRINT_INFO)
2633
                av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2634
        }
2635
        if (usesHFilter) c->canMMX2BeUsed=0;
2636
    }
2637
    else
2638
        c->canMMX2BeUsed=0;
2639

    
2640
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2641
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2642

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

    
2661
    /* precalculate horizontal scaler filter coefficients */
2662
    {
2663
#if defined(COMPILE_MMX2)
2664
// can't downscale !!!
2665
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
2666
            c->lumMmx2FilterCodeSize = initMMX2HScaler(      dstW, c->lumXInc, NULL, NULL, NULL, 8);
2667
            c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2668

    
2669
#ifdef MAP_ANONYMOUS
2670
            c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2671
            c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2672
#elif HAVE_VIRTUALALLOC
2673
            c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2674
            c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2675
#else
2676
            c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2677
            c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2678
#endif
2679

    
2680
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilter   , (dstW        /8+8)*sizeof(int16_t), fail);
2681
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilter   , (c->chrDstW  /4+8)*sizeof(int16_t), fail);
2682
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW      /2/8+8)*sizeof(int32_t), fail);
2683
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
2684

    
2685
            initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
2686
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
2687

    
2688
#ifdef MAP_ANONYMOUS
2689
            mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2690
            mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2691
#endif
2692
        } else
2693
#endif /* defined(COMPILE_MMX2) */
2694
        {
2695
            const int filterAlign=
2696
                (flags & SWS_CPU_CAPS_MMX) ? 4 :
2697
                (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2698
                1;
2699

    
2700
            if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2701
                           srcW      ,       dstW, filterAlign, 1<<14,
2702
                           (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2703
                           srcFilter->lumH, dstFilter->lumH, c->param) < 0)
2704
                goto fail;
2705
            if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2706
                           c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2707
                           (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2708
                           srcFilter->chrH, dstFilter->chrH, c->param) < 0)
2709
                goto fail;
2710
        }
2711
    } // initialize horizontal stuff
2712

    
2713
    /* precalculate vertical scaler filter coefficients */
2714
    {
2715
        const int filterAlign=
2716
            (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2717
            (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2718
            1;
2719

    
2720
        if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2721
                       srcH      ,        dstH, filterAlign, (1<<12),
2722
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags,
2723
                       srcFilter->lumV, dstFilter->lumV, c->param) < 0)
2724
            goto fail;
2725
        if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2726
                       c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2727
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2728
                       srcFilter->chrV, dstFilter->chrV, c->param) < 0)
2729
            goto fail;
2730

    
2731
#ifdef COMPILE_ALTIVEC
2732
        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
2733
        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
2734

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

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

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

    
2759
        nextSlice>>= c->chrSrcVSubSample;
2760
        nextSlice<<= c->chrSrcVSubSample;
2761
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
2762
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2763
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2764
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2765
    }
2766

    
2767
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2768
    // allocate several megabytes to handle all possible cases)
2769
    FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2770
    FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
2771
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2772
        FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2773
    //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)
2774
    /* align at 16 bytes for AltiVec */
2775
    for (i=0; i<c->vLumBufSize; i++) {
2776
        FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
2777
        c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2778
    }
2779
    for (i=0; i<c->vChrBufSize; i++) {
2780
        FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
2781
        c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2782
    }
2783
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2784
        for (i=0; i<c->vLumBufSize; i++) {
2785
            FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
2786
            c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2787
        }
2788

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

    
2792
    assert(2*VOFW == VOF);
2793

    
2794
    assert(c->chrDstH <= dstH);
2795

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

    
2822
        av_log(c, AV_LOG_INFO, "from %s to %s%s ",
2823
               sws_format_name(srcFormat),
2824
#ifdef DITHER1XBPP
2825
               dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ? "dithered " : "",
2826
#else
2827
               "",
2828
#endif
2829
               sws_format_name(dstFormat));
2830

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

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

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

    
2887
        if (dstFormat==PIX_FMT_BGR24)
2888
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2889
                   (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2890
        else if (dstFormat==PIX_FMT_RGB32)
2891
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2892
        else if (dstFormat==PIX_FMT_BGR565)
2893
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2894
        else if (dstFormat==PIX_FMT_BGR555)
2895
            av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2896

    
2897
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2898
    }
2899
    if (flags & SWS_PRINT_INFO) {
2900
        av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2901
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2902
        av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2903
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2904
    }
2905

    
2906
    c->swScale= getSwsFunc(c);
2907
    return c;
2908

    
2909
fail:
2910
    sws_freeContext(c);
2911
    return NULL;
2912
}
2913

    
2914
static void reset_ptr(const uint8_t* src[], int format)
2915
{
2916
    if(!isALPHA(format))
2917
        src[3]=NULL;
2918
    if(!isPlanarYUV(format)) {
2919
        src[3]=src[2]=NULL;
2920

    
2921
        if (!usePal(format))
2922
            src[1]= NULL;
2923
    }
2924
}
2925

    
2926
/**
2927
 * swscale wrapper, so we don't need to export the SwsContext.
2928
 * Assumes planar YUV to be in YUV order instead of YVU.
2929
 */
2930
int sws_scale(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2931
              int srcSliceH, uint8_t* dst[], int dstStride[])
2932
{
2933
    int i;
2934
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2935
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2936

    
2937
    // do not mess up sliceDir if we have a "trailing" 0-size slice
2938
    if (srcSliceH == 0)
2939
        return 0;
2940

    
2941
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2942
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2943
        return 0;
2944
    }
2945
    if (c->sliceDir == 0) {
2946
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2947
    }
2948

    
2949
    if (usePal(c->srcFormat)) {
2950
        for (i=0; i<256; i++) {
2951
            int p, r, g, b,y,u,v;
2952
            if(c->srcFormat == PIX_FMT_PAL8) {
2953
                p=((const uint32_t*)(src[1]))[i];
2954
                r= (p>>16)&0xFF;
2955
                g= (p>> 8)&0xFF;
2956
                b=  p     &0xFF;
2957
            } else if(c->srcFormat == PIX_FMT_RGB8) {
2958
                r= (i>>5    )*36;
2959
                g= ((i>>2)&7)*36;
2960
                b= (i&3     )*85;
2961
            } else if(c->srcFormat == PIX_FMT_BGR8) {
2962
                b= (i>>6    )*85;
2963
                g= ((i>>3)&7)*36;
2964
                r= (i&7     )*36;
2965
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
2966
                r= (i>>3    )*255;
2967
                g= ((i>>1)&3)*85;
2968
                b= (i&1     )*255;
2969
            } else {
2970
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2971
                b= (i>>3    )*255;
2972
                g= ((i>>1)&3)*85;
2973
                r= (i&1     )*255;
2974
            }
2975
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2976
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2977
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2978
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
2979

    
2980
            switch(c->dstFormat) {
2981
            case PIX_FMT_BGR32:
2982
#if !HAVE_BIGENDIAN
2983
            case PIX_FMT_RGB24:
2984
#endif
2985
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2986
                break;
2987
            case PIX_FMT_BGR32_1:
2988
#if HAVE_BIGENDIAN
2989
            case PIX_FMT_BGR24:
2990
#endif
2991
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2992
                break;
2993
            case PIX_FMT_RGB32_1:
2994
#if HAVE_BIGENDIAN
2995
            case PIX_FMT_RGB24:
2996
#endif
2997
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2998
                break;
2999
            case PIX_FMT_RGB32:
3000
#if !HAVE_BIGENDIAN
3001
            case PIX_FMT_BGR24:
3002
#endif
3003
            default:
3004
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
3005
            }
3006
        }
3007
    }
3008

    
3009
    // copy strides, so they can safely be modified
3010
    if (c->sliceDir == 1) {
3011
        // slices go from top to bottom
3012
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3013
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3014

    
3015
        reset_ptr(src2, c->srcFormat);
3016
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
3017

    
3018
        /* reset slice direction at end of frame */
3019
        if (srcSliceY + srcSliceH == c->srcH)
3020
            c->sliceDir = 0;
3021

    
3022
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3023
    } else {
3024
        // slices go from bottom to top => we flip the image internally
3025
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3026
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3027

    
3028
        src2[0] += (srcSliceH-1)*srcStride[0];
3029
        if (!usePal(c->srcFormat))
3030
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3031
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3032
        src2[3] += (srcSliceH-1)*srcStride[3];
3033
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
3034
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3035
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3036
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
3037

    
3038
        reset_ptr(src2, c->srcFormat);
3039
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
3040

    
3041
        /* reset slice direction at end of frame */
3042
        if (!srcSliceY)
3043
            c->sliceDir = 0;
3044

    
3045
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3046
    }
3047
}
3048

    
3049
#if LIBSWSCALE_VERSION_MAJOR < 1
3050
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
3051
                      int srcSliceH, uint8_t* dst[], int dstStride[])
3052
{
3053
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3054
}
3055
#endif
3056

    
3057
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3058
                                float lumaSharpen, float chromaSharpen,
3059
                                float chromaHShift, float chromaVShift,
3060
                                int verbose)
3061
{
3062
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3063
    if (!filter)
3064
        return NULL;
3065

    
3066
    if (lumaGBlur!=0.0) {
3067
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3068
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3069
    } else {
3070
        filter->lumH= sws_getIdentityVec();
3071
        filter->lumV= sws_getIdentityVec();
3072
    }
3073

    
3074
    if (chromaGBlur!=0.0) {
3075
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3076
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3077
    } else {
3078
        filter->chrH= sws_getIdentityVec();
3079
        filter->chrV= sws_getIdentityVec();
3080
    }
3081

    
3082
    if (chromaSharpen!=0.0) {
3083
        SwsVector *id= sws_getIdentityVec();
3084
        sws_scaleVec(filter->chrH, -chromaSharpen);
3085
        sws_scaleVec(filter->chrV, -chromaSharpen);
3086
        sws_addVec(filter->chrH, id);
3087
        sws_addVec(filter->chrV, id);
3088
        sws_freeVec(id);
3089
    }
3090

    
3091
    if (lumaSharpen!=0.0) {
3092
        SwsVector *id= sws_getIdentityVec();
3093
        sws_scaleVec(filter->lumH, -lumaSharpen);
3094
        sws_scaleVec(filter->lumV, -lumaSharpen);
3095
        sws_addVec(filter->lumH, id);
3096
        sws_addVec(filter->lumV, id);
3097
        sws_freeVec(id);
3098
    }
3099

    
3100
    if (chromaHShift != 0.0)
3101
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3102

    
3103
    if (chromaVShift != 0.0)
3104
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3105

    
3106
    sws_normalizeVec(filter->chrH, 1.0);
3107
    sws_normalizeVec(filter->chrV, 1.0);
3108
    sws_normalizeVec(filter->lumH, 1.0);
3109
    sws_normalizeVec(filter->lumV, 1.0);
3110

    
3111
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3112
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3113

    
3114
    return filter;
3115
}
3116

    
3117
SwsVector *sws_allocVec(int length)
3118
{
3119
    SwsVector *vec = av_malloc(sizeof(SwsVector));
3120
    if (!vec)
3121
        return NULL;
3122
    vec->length = length;
3123
    vec->coeff  = av_malloc(sizeof(double) * length);
3124
    if (!vec->coeff)
3125
        av_freep(&vec);
3126
    return vec;
3127
}
3128

    
3129
SwsVector *sws_getGaussianVec(double variance, double quality)
3130
{
3131
    const int length= (int)(variance*quality + 0.5) | 1;
3132
    int i;
3133
    double middle= (length-1)*0.5;
3134
    SwsVector *vec= sws_allocVec(length);
3135

    
3136
    if (!vec)
3137
        return NULL;
3138

    
3139
    for (i=0; i<length; i++) {
3140
        double dist= i-middle;
3141
        vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3142
    }
3143

    
3144
    sws_normalizeVec(vec, 1.0);
3145

    
3146
    return vec;
3147
}
3148

    
3149
SwsVector *sws_getConstVec(double c, int length)
3150
{
3151
    int i;
3152
    SwsVector *vec= sws_allocVec(length);
3153

    
3154
    if (!vec)
3155
        return NULL;
3156

    
3157
    for (i=0; i<length; i++)
3158
        vec->coeff[i]= c;
3159

    
3160
    return vec;
3161
}
3162

    
3163
SwsVector *sws_getIdentityVec(void)
3164
{
3165
    return sws_getConstVec(1.0, 1);
3166
}
3167

    
3168
double sws_dcVec(SwsVector *a)
3169
{
3170
    int i;
3171
    double sum=0;
3172

    
3173
    for (i=0; i<a->length; i++)
3174
        sum+= a->coeff[i];
3175

    
3176
    return sum;
3177
}
3178

    
3179
void sws_scaleVec(SwsVector *a, double scalar)
3180
{
3181
    int i;
3182

    
3183
    for (i=0; i<a->length; i++)
3184
        a->coeff[i]*= scalar;
3185
}
3186

    
3187
void sws_normalizeVec(SwsVector *a, double height)
3188
{
3189
    sws_scaleVec(a, height/sws_dcVec(a));
3190
}
3191

    
3192
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
3193
{
3194
    int length= a->length + b->length - 1;
3195
    int i, j;
3196
    SwsVector *vec= sws_getConstVec(0.0, length);
3197

    
3198
    if (!vec)
3199
        return NULL;
3200

    
3201
    for (i=0; i<a->length; i++) {
3202
        for (j=0; j<b->length; j++) {
3203
            vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
3204
        }
3205
    }
3206

    
3207
    return vec;
3208
}
3209

    
3210
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
3211
{
3212
    int length= FFMAX(a->length, b->length);
3213
    int i;
3214
    SwsVector *vec= sws_getConstVec(0.0, length);
3215

    
3216
    if (!vec)
3217
        return NULL;
3218

    
3219
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3220
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3221

    
3222
    return vec;
3223
}
3224

    
3225
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
3226
{
3227
    int length= FFMAX(a->length, b->length);
3228
    int i;
3229
    SwsVector *vec= sws_getConstVec(0.0, length);
3230

    
3231
    if (!vec)
3232
        return NULL;
3233

    
3234
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3235
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3236

    
3237
    return vec;
3238
}
3239

    
3240
/* shift left / or right if "shift" is negative */
3241
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
3242
{
3243
    int length= a->length + FFABS(shift)*2;
3244
    int i;
3245
    SwsVector *vec= sws_getConstVec(0.0, length);
3246

    
3247
    if (!vec)
3248
        return NULL;
3249

    
3250
    for (i=0; i<a->length; i++) {
3251
        vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3252
    }
3253

    
3254
    return vec;
3255
}
3256

    
3257
void sws_shiftVec(SwsVector *a, int shift)
3258
{
3259
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3260
    av_free(a->coeff);
3261
    a->coeff= shifted->coeff;
3262
    a->length= shifted->length;
3263
    av_free(shifted);
3264
}
3265

    
3266
void sws_addVec(SwsVector *a, SwsVector *b)
3267
{
3268
    SwsVector *sum= sws_sumVec(a, b);
3269
    av_free(a->coeff);
3270
    a->coeff= sum->coeff;
3271
    a->length= sum->length;
3272
    av_free(sum);
3273
}
3274

    
3275
void sws_subVec(SwsVector *a, SwsVector *b)
3276
{
3277
    SwsVector *diff= sws_diffVec(a, b);
3278
    av_free(a->coeff);
3279
    a->coeff= diff->coeff;
3280
    a->length= diff->length;
3281
    av_free(diff);
3282
}
3283

    
3284
void sws_convVec(SwsVector *a, SwsVector *b)
3285
{
3286
    SwsVector *conv= sws_getConvVec(a, b);
3287
    av_free(a->coeff);
3288
    a->coeff= conv->coeff;
3289
    a->length= conv->length;
3290
    av_free(conv);
3291
}
3292

    
3293
SwsVector *sws_cloneVec(SwsVector *a)
3294
{
3295
    int i;
3296
    SwsVector *vec= sws_allocVec(a->length);
3297

    
3298
    if (!vec)
3299
        return NULL;
3300

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

    
3303
    return vec;
3304
}
3305

    
3306
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
3307
{
3308
    int i;
3309
    double max=0;
3310
    double min=0;
3311
    double range;
3312

    
3313
    for (i=0; i<a->length; i++)
3314
        if (a->coeff[i]>max) max= a->coeff[i];
3315

    
3316
    for (i=0; i<a->length; i++)
3317
        if (a->coeff[i]<min) min= a->coeff[i];
3318

    
3319
    range= max - min;
3320

    
3321
    for (i=0; i<a->length; i++) {
3322
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3323
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3324
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
3325
        av_log(log_ctx, log_level, "|\n");
3326
    }
3327
}
3328

    
3329
#if LIBSWSCALE_VERSION_MAJOR < 1
3330
void sws_printVec(SwsVector *a)
3331
{
3332
    sws_printVec2(a, NULL, AV_LOG_DEBUG);
3333
}
3334
#endif
3335

    
3336
void sws_freeVec(SwsVector *a)
3337
{
3338
    if (!a) return;
3339
    av_freep(&a->coeff);
3340
    a->length=0;
3341
    av_free(a);
3342
}
3343

    
3344
void sws_freeFilter(SwsFilter *filter)
3345
{
3346
    if (!filter) return;
3347

    
3348
    if (filter->lumH) sws_freeVec(filter->lumH);
3349
    if (filter->lumV) sws_freeVec(filter->lumV);
3350
    if (filter->chrH) sws_freeVec(filter->chrH);
3351
    if (filter->chrV) sws_freeVec(filter->chrV);
3352
    av_free(filter);
3353
}
3354

    
3355
void sws_freeContext(SwsContext *c)
3356
{
3357
    int i;
3358
    if (!c) return;
3359

    
3360
    if (c->lumPixBuf) {
3361
        for (i=0; i<c->vLumBufSize; i++)
3362
            av_freep(&c->lumPixBuf[i]);
3363
        av_freep(&c->lumPixBuf);
3364
    }
3365

    
3366
    if (c->chrPixBuf) {
3367
        for (i=0; i<c->vChrBufSize; i++)
3368
            av_freep(&c->chrPixBuf[i]);
3369
        av_freep(&c->chrPixBuf);
3370
    }
3371

    
3372
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
3373
        for (i=0; i<c->vLumBufSize; i++)
3374
            av_freep(&c->alpPixBuf[i]);
3375
        av_freep(&c->alpPixBuf);
3376
    }
3377

    
3378
    av_freep(&c->vLumFilter);
3379
    av_freep(&c->vChrFilter);
3380
    av_freep(&c->hLumFilter);
3381
    av_freep(&c->hChrFilter);
3382
#ifdef COMPILE_ALTIVEC
3383
    av_freep(&c->vYCoeffsBank);
3384
    av_freep(&c->vCCoeffsBank);
3385
#endif
3386

    
3387
    av_freep(&c->vLumFilterPos);
3388
    av_freep(&c->vChrFilterPos);
3389
    av_freep(&c->hLumFilterPos);
3390
    av_freep(&c->hChrFilterPos);
3391

    
3392
#if ARCH_X86 && CONFIG_GPL
3393
#ifdef MAP_ANONYMOUS
3394
    if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3395
    if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3396
#elif HAVE_VIRTUALALLOC
3397
    if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3398
    if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3399
#else
3400
    av_free(c->lumMmx2FilterCode);
3401
    av_free(c->chrMmx2FilterCode);
3402
#endif
3403
    c->lumMmx2FilterCode=NULL;
3404
    c->chrMmx2FilterCode=NULL;
3405
#endif /* ARCH_X86 && CONFIG_GPL */
3406

    
3407
    av_freep(&c->yuvTable);
3408

    
3409
    av_free(c);
3410
}
3411

    
3412
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3413
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3414
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3415
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3416
{
3417
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3418

    
3419
    if (!param)
3420
        param = default_param;
3421

    
3422
    if (context) {
3423
        if (context->srcW != srcW || context->srcH != srcH ||
3424
            context->srcFormat != srcFormat ||
3425
            context->dstW != dstW || context->dstH != dstH ||
3426
            context->dstFormat != dstFormat || context->flags != flags ||
3427
            context->param[0] != param[0] || context->param[1] != param[1])
3428
        {
3429
            sws_freeContext(context);
3430
            context = NULL;
3431
        }
3432
    }
3433
    if (!context) {
3434
        return sws_getContext(srcW, srcH, srcFormat,
3435
                              dstW, dstH, dstFormat, flags,
3436
                              srcFilter, dstFilter, param);
3437
    }
3438
    return context;
3439
}
3440