Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ 075ec82c

History | View | Annotate | Download (117 KB)

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

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

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

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

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

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

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

    
86
#undef MOVNTQ
87
#undef PAVGB
88

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

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

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

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

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

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

    
194
extern const int32_t ff_yuv2rgb_coeffs[8][4];
195

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

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

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

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

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

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

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

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

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

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

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

    
275
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
276

    
277
#endif /* ARCH_X86 && CONFIG_GPL */
278

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

    
282
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
283

    
284
static const uint8_t  __attribute__((aligned(8))) dither_2x2_4[2][8]={
285
{  1,   3,   1,   3,   1,   3,   1,   3, },
286
{  2,   0,   2,   0,   2,   0,   2,   0, },
287
};
288

    
289
static const uint8_t  __attribute__((aligned(8))) dither_2x2_8[2][8]={
290
{  6,   2,   6,   2,   6,   2,   6,   2, },
291
{  0,   4,   0,   4,   0,   4,   0,   4, },
292
};
293

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

    
305
#if 0
306
const uint8_t  __attribute__((aligned(8))) dither_8x8_64[8][8]={
307
{  0,  48,  12,  60,   3,  51,  15,  63, },
308
{ 32,  16,  44,  28,  35,  19,  47,  31, },
309
{  8,  56,   4,  52,  11,  59,   7,  55, },
310
{ 40,  24,  36,  20,  43,  27,  39,  23, },
311
{  2,  50,  14,  62,   1,  49,  13,  61, },
312
{ 34,  18,  46,  30,  33,  17,  45,  29, },
313
{ 10,  58,   6,  54,   9,  57,   5,  53, },
314
{ 42,  26,  38,  22,  41,  25,  37,  21, },
315
};
316
#endif
317

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

    
329
#if 0
330
const uint8_t  __attribute__((aligned(8))) dither_8x8_128[8][8]={
331
{ 68,  36,  92,  60,  66,  34,  90,  58, },
332
{ 20, 116,  12, 108,  18, 114,  10, 106, },
333
{ 84,  52,  76,  44,  82,  50,  74,  42, },
334
{  0,  96,  24, 120,   6, 102,  30, 126, },
335
{ 64,  32,  88,  56,  70,  38,  94,  62, },
336
{ 16, 112,   8, 104,  22, 118,  14, 110, },
337
{ 80,  48,  72,  40,  86,  54,  78,  46, },
338
{  4, 100,  28, 124,   2,  98,  26, 122, },
339
};
340
#endif
341

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

    
391
const char *sws_format_name(enum PixelFormat format)
392
{
393
    switch (format) {
394
        case PIX_FMT_YUV420P:
395
            return "yuv420p";
396
        case PIX_FMT_YUVA420P:
397
            return "yuva420p";
398
        case PIX_FMT_YUYV422:
399
            return "yuyv422";
400
        case PIX_FMT_RGB24:
401
            return "rgb24";
402
        case PIX_FMT_BGR24:
403
            return "bgr24";
404
        case PIX_FMT_YUV422P:
405
            return "yuv422p";
406
        case PIX_FMT_YUV444P:
407
            return "yuv444p";
408
        case PIX_FMT_RGB32:
409
            return "rgb32";
410
        case PIX_FMT_YUV410P:
411
            return "yuv410p";
412
        case PIX_FMT_YUV411P:
413
            return "yuv411p";
414
        case PIX_FMT_RGB565:
415
            return "rgb565";
416
        case PIX_FMT_RGB555:
417
            return "rgb555";
418
        case PIX_FMT_GRAY16BE:
419
            return "gray16be";
420
        case PIX_FMT_GRAY16LE:
421
            return "gray16le";
422
        case PIX_FMT_GRAY8:
423
            return "gray8";
424
        case PIX_FMT_MONOWHITE:
425
            return "mono white";
426
        case PIX_FMT_MONOBLACK:
427
            return "mono black";
428
        case PIX_FMT_PAL8:
429
            return "Palette";
430
        case PIX_FMT_YUVJ420P:
431
            return "yuvj420p";
432
        case PIX_FMT_YUVJ422P:
433
            return "yuvj422p";
434
        case PIX_FMT_YUVJ444P:
435
            return "yuvj444p";
436
        case PIX_FMT_XVMC_MPEG2_MC:
437
            return "xvmc_mpeg2_mc";
438
        case PIX_FMT_XVMC_MPEG2_IDCT:
439
            return "xvmc_mpeg2_idct";
440
        case PIX_FMT_UYVY422:
441
            return "uyvy422";
442
        case PIX_FMT_UYYVYY411:
443
            return "uyyvyy411";
444
        case PIX_FMT_RGB32_1:
445
            return "rgb32x";
446
        case PIX_FMT_BGR32_1:
447
            return "bgr32x";
448
        case PIX_FMT_BGR32:
449
            return "bgr32";
450
        case PIX_FMT_BGR565:
451
            return "bgr565";
452
        case PIX_FMT_BGR555:
453
            return "bgr555";
454
        case PIX_FMT_BGR8:
455
            return "bgr8";
456
        case PIX_FMT_BGR4:
457
            return "bgr4";
458
        case PIX_FMT_BGR4_BYTE:
459
            return "bgr4 byte";
460
        case PIX_FMT_RGB8:
461
            return "rgb8";
462
        case PIX_FMT_RGB4:
463
            return "rgb4";
464
        case PIX_FMT_RGB4_BYTE:
465
            return "rgb4 byte";
466
        case PIX_FMT_RGB48BE:
467
            return "rgb48be";
468
        case PIX_FMT_RGB48LE:
469
            return "rgb48le";
470
        case PIX_FMT_NV12:
471
            return "nv12";
472
        case PIX_FMT_NV21:
473
            return "nv21";
474
        case PIX_FMT_YUV440P:
475
            return "yuv440p";
476
        case PIX_FMT_VDPAU_H264:
477
            return "vdpau_h264";
478
        case PIX_FMT_VDPAU_MPEG1:
479
            return "vdpau_mpeg1";
480
        case PIX_FMT_VDPAU_MPEG2:
481
            return "vdpau_mpeg2";
482
        case PIX_FMT_VDPAU_WMV3:
483
            return "vdpau_wmv3";
484
        case PIX_FMT_VDPAU_VC1:
485
            return "vdpau_vc1";
486
        case PIX_FMT_YUV420PLE:
487
            return "yuv420ple";
488
        case PIX_FMT_YUV422PLE:
489
            return "yuv422ple";
490
        case PIX_FMT_YUV444PLE:
491
            return "yuv444ple";
492
        case PIX_FMT_YUV420PBE:
493
            return "yuv420pbe";
494
        case PIX_FMT_YUV422PBE:
495
            return "yuv422pbe";
496
        case PIX_FMT_YUV444PBE:
497
            return "yuv444pbe";
498
        default:
499
            return "Unknown format";
500
    }
501
}
502

    
503
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
504
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
505
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
506
{
507
    //FIXME Optimize (just quickly written not optimized..)
508
    int i;
509
    for (i=0; i<dstW; i++)
510
    {
511
        int val=1<<18;
512
        int j;
513
        for (j=0; j<lumFilterSize; j++)
514
            val += lumSrc[j][i] * lumFilter[j];
515

    
516
        dest[i]= av_clip_uint8(val>>19);
517
    }
518

    
519
    if (uDest)
520
        for (i=0; i<chrDstW; i++)
521
        {
522
            int u=1<<18;
523
            int v=1<<18;
524
            int j;
525
            for (j=0; j<chrFilterSize; j++)
526
            {
527
                u += chrSrc[j][i] * chrFilter[j];
528
                v += chrSrc[j][i + VOFW] * chrFilter[j];
529
            }
530

    
531
            uDest[i]= av_clip_uint8(u>>19);
532
            vDest[i]= av_clip_uint8(v>>19);
533
        }
534

    
535
    if (CONFIG_SWSCALE_ALPHA && aDest)
536
        for (i=0; i<dstW; i++){
537
            int val=1<<18;
538
            int j;
539
            for (j=0; j<lumFilterSize; j++)
540
                val += alpSrc[j][i] * lumFilter[j];
541

    
542
            aDest[i]= av_clip_uint8(val>>19);
543
        }
544

    
545
}
546

    
547
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
548
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
549
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
550
{
551
    //FIXME Optimize (just quickly written not optimized..)
552
    int i;
553
    for (i=0; i<dstW; i++)
554
    {
555
        int val=1<<18;
556
        int j;
557
        for (j=0; j<lumFilterSize; j++)
558
            val += lumSrc[j][i] * lumFilter[j];
559

    
560
        dest[i]= av_clip_uint8(val>>19);
561
    }
562

    
563
    if (!uDest)
564
        return;
565

    
566
    if (dstFormat == PIX_FMT_NV12)
567
        for (i=0; i<chrDstW; i++)
568
        {
569
            int u=1<<18;
570
            int v=1<<18;
571
            int j;
572
            for (j=0; j<chrFilterSize; j++)
573
            {
574
                u += chrSrc[j][i] * chrFilter[j];
575
                v += chrSrc[j][i + VOFW] * chrFilter[j];
576
            }
577

    
578
            uDest[2*i]= av_clip_uint8(u>>19);
579
            uDest[2*i+1]= av_clip_uint8(v>>19);
580
        }
581
    else
582
        for (i=0; i<chrDstW; i++)
583
        {
584
            int u=1<<18;
585
            int v=1<<18;
586
            int j;
587
            for (j=0; j<chrFilterSize; j++)
588
            {
589
                u += chrSrc[j][i] * chrFilter[j];
590
                v += chrSrc[j][i + VOFW] * chrFilter[j];
591
            }
592

    
593
            uDest[2*i]= av_clip_uint8(v>>19);
594
            uDest[2*i+1]= av_clip_uint8(u>>19);
595
        }
596
}
597

    
598
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
599
    for (i=0; i<(dstW>>1); i++){\
600
        int j;\
601
        int Y1 = 1<<18;\
602
        int Y2 = 1<<18;\
603
        int U  = 1<<18;\
604
        int V  = 1<<18;\
605
        int av_unused A1, A2;\
606
        type av_unused *r, *b, *g;\
607
        const int i2= 2*i;\
608
        \
609
        for (j=0; j<lumFilterSize; j++)\
610
        {\
611
            Y1 += lumSrc[j][i2] * lumFilter[j];\
612
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
613
        }\
614
        for (j=0; j<chrFilterSize; j++)\
615
        {\
616
            U += chrSrc[j][i] * chrFilter[j];\
617
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
618
        }\
619
        Y1>>=19;\
620
        Y2>>=19;\
621
        U >>=19;\
622
        V >>=19;\
623
        if (alpha){\
624
            A1 = 1<<18;\
625
            A2 = 1<<18;\
626
            for (j=0; j<lumFilterSize; j++){\
627
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
628
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
629
            }\
630
            A1>>=19;\
631
            A2>>=19;\
632
        }\
633

    
634
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
635
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
636
        if ((Y1|Y2|U|V)&256)\
637
        {\
638
            if (Y1>255)   Y1=255; \
639
            else if (Y1<0)Y1=0;   \
640
            if (Y2>255)   Y2=255; \
641
            else if (Y2<0)Y2=0;   \
642
            if (U>255)    U=255;  \
643
            else if (U<0) U=0;    \
644
            if (V>255)    V=255;  \
645
            else if (V<0) V=0;    \
646
        }\
647
        if (alpha && ((A1|A2)&256)){\
648
            A1=av_clip_uint8(A1);\
649
            A2=av_clip_uint8(A2);\
650
        }
651

    
652
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
653
    for (i=0; i<dstW; i++){\
654
        int j;\
655
        int Y = 0;\
656
        int U = -128<<19;\
657
        int V = -128<<19;\
658
        int av_unused A;\
659
        int R,G,B;\
660
        \
661
        for (j=0; j<lumFilterSize; j++){\
662
            Y += lumSrc[j][i     ] * lumFilter[j];\
663
        }\
664
        for (j=0; j<chrFilterSize; j++){\
665
            U += chrSrc[j][i     ] * chrFilter[j];\
666
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
667
        }\
668
        Y >>=10;\
669
        U >>=10;\
670
        V >>=10;\
671
        if (alpha){\
672
            A = rnd;\
673
            for (j=0; j<lumFilterSize; j++)\
674
                A += alpSrc[j][i     ] * lumFilter[j];\
675
            A >>=19;\
676
            if (A&256)\
677
                A = av_clip_uint8(A);\
678
        }\
679

    
680
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
681
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
682
        Y-= c->yuv2rgb_y_offset;\
683
        Y*= c->yuv2rgb_y_coeff;\
684
        Y+= rnd;\
685
        R= Y + V*c->yuv2rgb_v2r_coeff;\
686
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
687
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
688
        if ((R|G|B)&(0xC0000000)){\
689
            if (R>=(256<<22))   R=(256<<22)-1; \
690
            else if (R<0)R=0;   \
691
            if (G>=(256<<22))   G=(256<<22)-1; \
692
            else if (G<0)G=0;   \
693
            if (B>=(256<<22))   B=(256<<22)-1; \
694
            else if (B<0)B=0;   \
695
        }\
696

    
697

    
698
#define YSCALE_YUV_2_GRAY16_C \
699
    for (i=0; i<(dstW>>1); i++){\
700
        int j;\
701
        int Y1 = 1<<18;\
702
        int Y2 = 1<<18;\
703
        int U  = 1<<18;\
704
        int V  = 1<<18;\
705
        \
706
        const int i2= 2*i;\
707
        \
708
        for (j=0; j<lumFilterSize; j++)\
709
        {\
710
            Y1 += lumSrc[j][i2] * lumFilter[j];\
711
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
712
        }\
713
        Y1>>=11;\
714
        Y2>>=11;\
715
        if ((Y1|Y2|U|V)&65536)\
716
        {\
717
            if (Y1>65535)   Y1=65535; \
718
            else if (Y1<0)Y1=0;   \
719
            if (Y2>65535)   Y2=65535; \
720
            else if (Y2<0)Y2=0;   \
721
        }
722

    
723
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
724
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
725
    r = (type *)c->table_rV[V];   \
726
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
727
    b = (type *)c->table_bU[U];   \
728

    
729
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
730
    for (i=0; i<(dstW>>1); i++){ \
731
        const int i2= 2*i;       \
732
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
733
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
734
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
735
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
736
        type av_unused *r, *b, *g;                                    \
737
        int av_unused A1, A2;                                         \
738
        if (alpha){\
739
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
740
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
741
        }\
742

    
743
#define YSCALE_YUV_2_GRAY16_2_C   \
744
    for (i=0; i<(dstW>>1); i++){ \
745
        const int i2= 2*i;       \
746
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
747
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;           \
748

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

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

    
769
#define YSCALE_YUV_2_GRAY16_1_C \
770
    for (i=0; i<(dstW>>1); i++){\
771
        const int i2= 2*i;\
772
        int Y1= buf0[i2  ]<<1;\
773
        int Y2= buf0[i2+1]<<1;\
774

    
775
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
776
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
777
    r = (type *)c->table_rV[V];\
778
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
779
    b = (type *)c->table_bU[U];\
780

    
781
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
782
    for (i=0; i<(dstW>>1); i++){\
783
        const int i2= 2*i;\
784
        int Y1= buf0[i2  ]>>7;\
785
        int Y2= buf0[i2+1]>>7;\
786
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
787
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
788
        type av_unused *r, *b, *g;\
789
        int av_unused A1, A2;\
790
        if (alpha){\
791
            A1= abuf0[i2  ]>>7;\
792
            A2= abuf0[i2+1]>>7;\
793
        }\
794

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

    
801
#define YSCALE_YUV_2_MONO2_C \
802
    const uint8_t * const d128=dither_8x8_220[y&7];\
803
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
804
    for (i=0; i<dstW-7; i+=8){\
805
        int acc;\
806
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
807
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
808
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
809
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
810
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
811
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
812
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
813
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
814
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
815
        dest++;\
816
    }\
817

    
818

    
819
#define YSCALE_YUV_2_MONOX_C \
820
    const uint8_t * const d128=dither_8x8_220[y&7];\
821
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
822
    int acc=0;\
823
    for (i=0; i<dstW-1; i+=2){\
824
        int j;\
825
        int Y1=1<<18;\
826
        int Y2=1<<18;\
827
\
828
        for (j=0; j<lumFilterSize; j++)\
829
        {\
830
            Y1 += lumSrc[j][i] * lumFilter[j];\
831
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
832
        }\
833
        Y1>>=19;\
834
        Y2>>=19;\
835
        if ((Y1|Y2)&256)\
836
        {\
837
            if (Y1>255)   Y1=255;\
838
            else if (Y1<0)Y1=0;\
839
            if (Y2>255)   Y2=255;\
840
            else if (Y2<0)Y2=0;\
841
        }\
842
        acc+= acc + g[Y1+d128[(i+0)&7]];\
843
        acc+= acc + g[Y2+d128[(i+1)&7]];\
844
        if ((i&7)==6){\
845
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
846
            dest++;\
847
        }\
848
    }
849

    
850

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

    
1023

    
1024
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1025
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1026
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1027
{
1028
    int i;
1029
    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)
1030
}
1031

    
1032
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1033
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1034
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1035
{
1036
    int i;
1037
    int step= fmt_depth(c->dstFormat)/8;
1038
    int aidx= 3;
1039

    
1040
    switch(c->dstFormat){
1041
    case PIX_FMT_ARGB:
1042
        dest++;
1043
        aidx= 0;
1044
    case PIX_FMT_RGB24:
1045
        aidx--;
1046
    case PIX_FMT_RGBA:
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]= R>>22;
1052
                dest[1]= G>>22;
1053
                dest[2]= B>>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]= R>>22;
1061
                    dest[1]= G>>22;
1062
                    dest[2]= B>>22;
1063
                    dest+= step;
1064
                }
1065
            }else{
1066
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1067
                    dest[aidx]= 255;
1068
                    dest[0]= R>>22;
1069
                    dest[1]= G>>22;
1070
                    dest[2]= B>>22;
1071
                    dest+= step;
1072
                }
1073
            }
1074
        }
1075
        break;
1076
    case PIX_FMT_ABGR:
1077
        dest++;
1078
        aidx= 0;
1079
    case PIX_FMT_BGR24:
1080
        aidx--;
1081
    case PIX_FMT_BGRA:
1082
        if (CONFIG_SMALL){
1083
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1084
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1085
                dest[aidx]= needAlpha ? A : 255;
1086
                dest[0]= B>>22;
1087
                dest[1]= G>>22;
1088
                dest[2]= R>>22;
1089
                dest+= step;
1090
            }
1091
        }else{
1092
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1093
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1094
                    dest[aidx]= A;
1095
                    dest[0]= B>>22;
1096
                    dest[1]= G>>22;
1097
                    dest[2]= R>>22;
1098
                    dest+= step;
1099
                }
1100
            }else{
1101
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1102
                    dest[aidx]= 255;
1103
                    dest[0]= B>>22;
1104
                    dest[1]= G>>22;
1105
                    dest[2]= R>>22;
1106
                    dest+= step;
1107
                }
1108
            }
1109
        }
1110
        break;
1111
    default:
1112
        assert(0);
1113
    }
1114
}
1115

    
1116
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1117
    int i;
1118
    uint8_t *ptr = plane + stride*y;
1119
    for (i=0; i<height; i++){
1120
        memset(ptr, val, width);
1121
        ptr += stride;
1122
    }
1123
}
1124

    
1125
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1126
//Plain C versions
1127
#if !HAVE_MMX || CONFIG_RUNTIME_CPUDETECT || !CONFIG_GPL
1128
#define COMPILE_C
1129
#endif
1130

    
1131
#if ARCH_PPC
1132
#if (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1133
#undef COMPILE_C
1134
#define COMPILE_ALTIVEC
1135
#endif
1136
#endif //ARCH_PPC
1137

    
1138
#if ARCH_X86
1139

    
1140
#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1141
#define COMPILE_MMX
1142
#endif
1143

    
1144
#if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1145
#define COMPILE_MMX2
1146
#endif
1147

    
1148
#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1149
#define COMPILE_3DNOW
1150
#endif
1151
#endif //ARCH_X86
1152

    
1153
#undef HAVE_MMX
1154
#undef HAVE_MMX2
1155
#undef HAVE_AMD3DNOW
1156
#undef HAVE_ALTIVEC
1157
#define HAVE_MMX 0
1158
#define HAVE_MMX2 0
1159
#define HAVE_AMD3DNOW 0
1160
#define HAVE_ALTIVEC 0
1161

    
1162
#ifdef COMPILE_C
1163
#define RENAME(a) a ## _C
1164
#include "swscale_template.c"
1165
#endif
1166

    
1167
#ifdef COMPILE_ALTIVEC
1168
#undef RENAME
1169
#undef HAVE_ALTIVEC
1170
#define HAVE_ALTIVEC 1
1171
#define RENAME(a) a ## _altivec
1172
#include "swscale_template.c"
1173
#endif
1174

    
1175
#if ARCH_X86
1176

    
1177
//MMX versions
1178
#ifdef COMPILE_MMX
1179
#undef RENAME
1180
#undef HAVE_MMX
1181
#undef HAVE_MMX2
1182
#undef HAVE_AMD3DNOW
1183
#define HAVE_MMX 1
1184
#define HAVE_MMX2 0
1185
#define HAVE_AMD3DNOW 0
1186
#define RENAME(a) a ## _MMX
1187
#include "swscale_template.c"
1188
#endif
1189

    
1190
//MMX2 versions
1191
#ifdef COMPILE_MMX2
1192
#undef RENAME
1193
#undef HAVE_MMX
1194
#undef HAVE_MMX2
1195
#undef HAVE_AMD3DNOW
1196
#define HAVE_MMX 1
1197
#define HAVE_MMX2 1
1198
#define HAVE_AMD3DNOW 0
1199
#define RENAME(a) a ## _MMX2
1200
#include "swscale_template.c"
1201
#endif
1202

    
1203
//3DNOW versions
1204
#ifdef COMPILE_3DNOW
1205
#undef RENAME
1206
#undef HAVE_MMX
1207
#undef HAVE_MMX2
1208
#undef HAVE_AMD3DNOW
1209
#define HAVE_MMX 1
1210
#define HAVE_MMX2 0
1211
#define HAVE_AMD3DNOW 1
1212
#define RENAME(a) a ## _3DNow
1213
#include "swscale_template.c"
1214
#endif
1215

    
1216
#endif //ARCH_X86
1217

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

    
1220
static double getSplineCoeff(double a, double b, double c, double d, double dist)
1221
{
1222
//    printf("%f %f %f %f %f\n", a,b,c,d,dist);
1223
    if (dist<=1.0)      return ((d*dist + c)*dist + b)*dist +a;
1224
    else                return getSplineCoeff(        0.0,
1225
                                             b+ 2.0*c + 3.0*d,
1226
                                                    c + 3.0*d,
1227
                                            -b- 3.0*c - 6.0*d,
1228
                                            dist-1.0);
1229
}
1230

    
1231
static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1232
                             int srcW, int dstW, int filterAlign, int one, int flags,
1233
                             SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1234
{
1235
    int i;
1236
    int filterSize;
1237
    int filter2Size;
1238
    int minFilterSize;
1239
    int64_t *filter=NULL;
1240
    int64_t *filter2=NULL;
1241
    const int64_t fone= 1LL<<54;
1242
    int ret= -1;
1243
#if ARCH_X86
1244
    if (flags & SWS_CPU_CAPS_MMX)
1245
        __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1246
#endif
1247

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

    
1251
    if (FFABS(xInc - 0x10000) <10) // unscaled
1252
    {
1253
        int i;
1254
        filterSize= 1;
1255
        filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1256

    
1257
        for (i=0; i<dstW; i++)
1258
        {
1259
            filter[i*filterSize]= fone;
1260
            (*filterPos)[i]=i;
1261
        }
1262

    
1263
    }
1264
    else if (flags&SWS_POINT) // lame looking point sampling mode
1265
    {
1266
        int i;
1267
        int xDstInSrc;
1268
        filterSize= 1;
1269
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1270

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

    
1276
            (*filterPos)[i]= xx;
1277
            filter[i]= fone;
1278
            xDstInSrc+= xInc;
1279
        }
1280
    }
1281
    else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1282
    {
1283
        int i;
1284
        int xDstInSrc;
1285
        if      (flags&SWS_BICUBIC) filterSize= 4;
1286
        else if (flags&SWS_X      ) filterSize= 4;
1287
        else                        filterSize= 2; // SWS_BILINEAR / SWS_AREA
1288
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1289

    
1290
        xDstInSrc= xInc/2 - 0x8000;
1291
        for (i=0; i<dstW; i++)
1292
        {
1293
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1294
            int j;
1295

    
1296
            (*filterPos)[i]= xx;
1297
                //bilinear upscale / linear interpolate / area averaging
1298
                for (j=0; j<filterSize; j++)
1299
                {
1300
                    int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1301
                    if (coeff<0) coeff=0;
1302
                    filter[i*filterSize + j]= coeff;
1303
                    xx++;
1304
                }
1305
            xDstInSrc+= xInc;
1306
        }
1307
    }
1308
    else
1309
    {
1310
        int xDstInSrc;
1311
        int sizeFactor;
1312

    
1313
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
1314
        else if (flags&SWS_X)            sizeFactor=  8;
1315
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
1316
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
1317
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1318
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
1319
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
1320
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
1321
        else {
1322
            sizeFactor= 0; //GCC warning killer
1323
            assert(0);
1324
        }
1325

    
1326
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
1327
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1328

    
1329
        if (filterSize > srcW-2) filterSize=srcW-2;
1330

    
1331
        filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1332

    
1333
        xDstInSrc= xInc - 0x10000;
1334
        for (i=0; i<dstW; i++)
1335
        {
1336
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1337
            int j;
1338
            (*filterPos)[i]= xx;
1339
            for (j=0; j<filterSize; j++)
1340
            {
1341
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1342
                double floatd;
1343
                int64_t coeff;
1344

    
1345
                if (xInc > 1<<16)
1346
                    d= d*dstW/srcW;
1347
                floatd= d * (1.0/(1<<30));
1348

    
1349
                if (flags & SWS_BICUBIC)
1350
                {
1351
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
1352
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1353
                    int64_t dd = ( d*d)>>30;
1354
                    int64_t ddd= (dd*d)>>30;
1355

    
1356
                    if      (d < 1LL<<30)
1357
                        coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1358
                    else if (d < 1LL<<31)
1359
                        coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1360
                    else
1361
                        coeff=0.0;
1362
                    coeff *= fone>>(30+24);
1363
                }
1364
/*                else if (flags & SWS_X)
1365
                {
1366
                    double p= param ? param*0.01 : 0.3;
1367
                    coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1368
                    coeff*= pow(2.0, - p*d*d);
1369
                }*/
1370
                else if (flags & SWS_X)
1371
                {
1372
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1373
                    double c;
1374

    
1375
                    if (floatd<1.0)
1376
                        c = cos(floatd*PI);
1377
                    else
1378
                        c=-1.0;
1379
                    if (c<0.0)      c= -pow(-c, A);
1380
                    else            c=  pow( c, A);
1381
                    coeff= (c*0.5 + 0.5)*fone;
1382
                }
1383
                else if (flags & SWS_AREA)
1384
                {
1385
                    int64_t d2= d - (1<<29);
1386
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1387
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1388
                    else coeff=0.0;
1389
                    coeff *= fone>>(30+16);
1390
                }
1391
                else if (flags & SWS_GAUSS)
1392
                {
1393
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1394
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
1395
                }
1396
                else if (flags & SWS_SINC)
1397
                {
1398
                    coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1399
                }
1400
                else if (flags & SWS_LANCZOS)
1401
                {
1402
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1403
                    coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1404
                    if (floatd>p) coeff=0;
1405
                }
1406
                else if (flags & SWS_BILINEAR)
1407
                {
1408
                    coeff= (1<<30) - d;
1409
                    if (coeff<0) coeff=0;
1410
                    coeff *= fone >> 30;
1411
                }
1412
                else if (flags & SWS_SPLINE)
1413
                {
1414
                    double p=-2.196152422706632;
1415
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1416
                }
1417
                else {
1418
                    coeff= 0.0; //GCC warning killer
1419
                    assert(0);
1420
                }
1421

    
1422
                filter[i*filterSize + j]= coeff;
1423
                xx++;
1424
            }
1425
            xDstInSrc+= 2*xInc;
1426
        }
1427
    }
1428

    
1429
    /* apply src & dst Filter to filter -> filter2
1430
       av_free(filter);
1431
    */
1432
    assert(filterSize>0);
1433
    filter2Size= filterSize;
1434
    if (srcFilter) filter2Size+= srcFilter->length - 1;
1435
    if (dstFilter) filter2Size+= dstFilter->length - 1;
1436
    assert(filter2Size>0);
1437
    filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1438

    
1439
    for (i=0; i<dstW; i++)
1440
    {
1441
        int j, k;
1442

    
1443
        if(srcFilter){
1444
            for (k=0; k<srcFilter->length; k++){
1445
                for (j=0; j<filterSize; j++)
1446
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1447
            }
1448
        }else{
1449
            for (j=0; j<filterSize; j++)
1450
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
1451
        }
1452
        //FIXME dstFilter
1453

    
1454
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1455
    }
1456
    av_freep(&filter);
1457

    
1458
    /* try to reduce the filter-size (step1 find size and shift left) */
1459
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1460
    minFilterSize= 0;
1461
    for (i=dstW-1; i>=0; i--)
1462
    {
1463
        int min= filter2Size;
1464
        int j;
1465
        int64_t cutOff=0.0;
1466

    
1467
        /* get rid off near zero elements on the left by shifting left */
1468
        for (j=0; j<filter2Size; j++)
1469
        {
1470
            int k;
1471
            cutOff += FFABS(filter2[i*filter2Size]);
1472

    
1473
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1474

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

    
1478
            // move filter coefficients left
1479
            for (k=1; k<filter2Size; k++)
1480
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1481
            filter2[i*filter2Size + k - 1]= 0;
1482
            (*filterPos)[i]++;
1483
        }
1484

    
1485
        cutOff=0;
1486
        /* count near zeros on the right */
1487
        for (j=filter2Size-1; j>0; j--)
1488
        {
1489
            cutOff += FFABS(filter2[i*filter2Size + j]);
1490

    
1491
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1492
            min--;
1493
        }
1494

    
1495
        if (min>minFilterSize) minFilterSize= min;
1496
    }
1497

    
1498
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1499
        // we can handle the special case 4,
1500
        // so we don't want to go to the full 8
1501
        if (minFilterSize < 5)
1502
            filterAlign = 4;
1503

    
1504
        // We really don't want to waste our time
1505
        // doing useless computation, so fall back on
1506
        // the scalar C code for very small filters.
1507
        // Vectorizing is worth it only if you have a
1508
        // decent-sized vector.
1509
        if (minFilterSize < 3)
1510
            filterAlign = 1;
1511
    }
1512

    
1513
    if (flags & SWS_CPU_CAPS_MMX) {
1514
        // special case for unscaled vertical filtering
1515
        if (minFilterSize == 1 && filterAlign == 2)
1516
            filterAlign= 1;
1517
    }
1518

    
1519
    assert(minFilterSize > 0);
1520
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1521
    assert(filterSize > 0);
1522
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
1523
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1524
        goto error;
1525
    *outFilterSize= filterSize;
1526

    
1527
    if (flags&SWS_PRINT_INFO)
1528
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1529
    /* try to reduce the filter-size (step2 reduce it) */
1530
    for (i=0; i<dstW; i++)
1531
    {
1532
        int j;
1533

    
1534
        for (j=0; j<filterSize; j++)
1535
        {
1536
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
1537
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
1538
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
1539
                filter[i*filterSize + j]= 0;
1540
        }
1541
    }
1542

    
1543

    
1544
    //FIXME try to align filterPos if possible
1545

    
1546
    //fix borders
1547
    for (i=0; i<dstW; i++)
1548
    {
1549
        int j;
1550
        if ((*filterPos)[i] < 0)
1551
        {
1552
            // move filter coefficients left to compensate for filterPos
1553
            for (j=1; j<filterSize; j++)
1554
            {
1555
                int left= FFMAX(j + (*filterPos)[i], 0);
1556
                filter[i*filterSize + left] += filter[i*filterSize + j];
1557
                filter[i*filterSize + j]=0;
1558
            }
1559
            (*filterPos)[i]= 0;
1560
        }
1561

    
1562
        if ((*filterPos)[i] + filterSize > srcW)
1563
        {
1564
            int shift= (*filterPos)[i] + filterSize - srcW;
1565
            // move filter coefficients right to compensate for filterPos
1566
            for (j=filterSize-2; j>=0; j--)
1567
            {
1568
                int right= FFMIN(j + shift, filterSize-1);
1569
                filter[i*filterSize +right] += filter[i*filterSize +j];
1570
                filter[i*filterSize +j]=0;
1571
            }
1572
            (*filterPos)[i]= srcW - filterSize;
1573
        }
1574
    }
1575

    
1576
    // Note the +1 is for the MMX scaler which reads over the end
1577
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1578
    *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1579

    
1580
    /* normalize & store in outFilter */
1581
    for (i=0; i<dstW; i++)
1582
    {
1583
        int j;
1584
        int64_t error=0;
1585
        int64_t sum=0;
1586

    
1587
        for (j=0; j<filterSize; j++)
1588
        {
1589
            sum+= filter[i*filterSize + j];
1590
        }
1591
        sum= (sum + one/2)/ one;
1592
        for (j=0; j<*outFilterSize; j++)
1593
        {
1594
            int64_t v= filter[i*filterSize + j] + error;
1595
            int intV= ROUNDED_DIV(v, sum);
1596
            (*outFilter)[i*(*outFilterSize) + j]= intV;
1597
            error= v - intV*sum;
1598
        }
1599
    }
1600

    
1601
    (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1602
    for (i=0; i<*outFilterSize; i++)
1603
    {
1604
        int j= dstW*(*outFilterSize);
1605
        (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1606
    }
1607

    
1608
    ret=0;
1609
error:
1610
    av_free(filter);
1611
    av_free(filter2);
1612
    return ret;
1613
}
1614

    
1615
#ifdef COMPILE_MMX2
1616
static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1617
{
1618
    uint8_t *fragmentA;
1619
    x86_reg imm8OfPShufW1A;
1620
    x86_reg imm8OfPShufW2A;
1621
    x86_reg fragmentLengthA;
1622
    uint8_t *fragmentB;
1623
    x86_reg imm8OfPShufW1B;
1624
    x86_reg imm8OfPShufW2B;
1625
    x86_reg fragmentLengthB;
1626
    int fragmentPos;
1627

    
1628
    int xpos, i;
1629

    
1630
    // create an optimized horizontal scaling routine
1631

    
1632
    //code fragment
1633

    
1634
    __asm__ volatile(
1635
        "jmp                         9f                 \n\t"
1636
    // Begin
1637
        "0:                                             \n\t"
1638
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1639
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1640
        "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
1641
        "punpcklbw                %%mm7, %%mm1          \n\t"
1642
        "punpcklbw                %%mm7, %%mm0          \n\t"
1643
        "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
1644
        "1:                                             \n\t"
1645
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1646
        "2:                                             \n\t"
1647
        "psubw                    %%mm1, %%mm0          \n\t"
1648
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1649
        "pmullw                   %%mm3, %%mm0          \n\t"
1650
        "psllw                       $7, %%mm1          \n\t"
1651
        "paddw                    %%mm1, %%mm0          \n\t"
1652

    
1653
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1654

    
1655
        "add                         $8, %%"REG_a"      \n\t"
1656
    // End
1657
        "9:                                             \n\t"
1658
//        "int $3                                         \n\t"
1659
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1660
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1661
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1662
        "dec                         %1                 \n\t"
1663
        "dec                         %2                 \n\t"
1664
        "sub                         %0, %1             \n\t"
1665
        "sub                         %0, %2             \n\t"
1666
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1667
        "sub                         %0, %3             \n\t"
1668

    
1669

    
1670
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1671
        "=r" (fragmentLengthA)
1672
    );
1673

    
1674
    __asm__ volatile(
1675
        "jmp                         9f                 \n\t"
1676
    // Begin
1677
        "0:                                             \n\t"
1678
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
1679
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
1680
        "punpcklbw                %%mm7, %%mm0          \n\t"
1681
        "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
1682
        "1:                                             \n\t"
1683
        "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
1684
        "2:                                             \n\t"
1685
        "psubw                    %%mm1, %%mm0          \n\t"
1686
        "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
1687
        "pmullw                   %%mm3, %%mm0          \n\t"
1688
        "psllw                       $7, %%mm1          \n\t"
1689
        "paddw                    %%mm1, %%mm0          \n\t"
1690

    
1691
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1692

    
1693
        "add                         $8, %%"REG_a"      \n\t"
1694
    // End
1695
        "9:                                             \n\t"
1696
//        "int                       $3                   \n\t"
1697
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
1698
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
1699
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
1700
        "dec                         %1                 \n\t"
1701
        "dec                         %2                 \n\t"
1702
        "sub                         %0, %1             \n\t"
1703
        "sub                         %0, %2             \n\t"
1704
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
1705
        "sub                         %0, %3             \n\t"
1706

    
1707

    
1708
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1709
        "=r" (fragmentLengthB)
1710
    );
1711

    
1712
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1713
    fragmentPos=0;
1714

    
1715
    for (i=0; i<dstW/numSplits; i++)
1716
    {
1717
        int xx=xpos>>16;
1718

    
1719
        if ((i&3) == 0)
1720
        {
1721
            int a=0;
1722
            int b=((xpos+xInc)>>16) - xx;
1723
            int c=((xpos+xInc*2)>>16) - xx;
1724
            int d=((xpos+xInc*3)>>16) - xx;
1725

    
1726
            filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
1727
            filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
1728
            filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1729
            filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1730
            filterPos[i/2]= xx;
1731

    
1732
            if (d+1<4)
1733
            {
1734
                int maxShift= 3-(d+1);
1735
                int shift=0;
1736

    
1737
                memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1738

    
1739
                funnyCode[fragmentPos + imm8OfPShufW1B]=
1740
                    (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1741
                funnyCode[fragmentPos + imm8OfPShufW2B]=
1742
                    a | (b<<2) | (c<<4) | (d<<6);
1743

    
1744
                if (i+3>=dstW) shift=maxShift; //avoid overread
1745
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1746

    
1747
                if (shift && i>=shift)
1748
                {
1749
                    funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1750
                    funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1751
                    filterPos[i/2]-=shift;
1752
                }
1753

    
1754
                fragmentPos+= fragmentLengthB;
1755
            }
1756
            else
1757
            {
1758
                int maxShift= 3-d;
1759
                int shift=0;
1760

    
1761
                memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1762

    
1763
                funnyCode[fragmentPos + imm8OfPShufW1A]=
1764
                funnyCode[fragmentPos + imm8OfPShufW2A]=
1765
                    a | (b<<2) | (c<<4) | (d<<6);
1766

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

    
1770
                if (shift && i>=shift)
1771
                {
1772
                    funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1773
                    funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1774
                    filterPos[i/2]-=shift;
1775
                }
1776

    
1777
                fragmentPos+= fragmentLengthA;
1778
            }
1779

    
1780
            funnyCode[fragmentPos]= RET;
1781
        }
1782
        xpos+=xInc;
1783
    }
1784
    filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1785
}
1786
#endif /* COMPILE_MMX2 */
1787

    
1788
static void globalInit(void){
1789
    // generating tables:
1790
    int i;
1791
    for (i=0; i<768; i++){
1792
        int c= av_clip_uint8(i-256);
1793
        clip_table[i]=c;
1794
    }
1795
}
1796

    
1797
static SwsFunc getSwsFunc(SwsContext *c)
1798
{
1799
#if CONFIG_RUNTIME_CPUDETECT && CONFIG_GPL
1800
    int flags = c->flags;
1801

    
1802
#if ARCH_X86
1803
    // ordered per speed fastest first
1804
    if (flags & SWS_CPU_CAPS_MMX2) {
1805
        sws_init_swScale_MMX2(c);
1806
        return swScale_MMX2;
1807
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1808
        sws_init_swScale_3DNow(c);
1809
        return swScale_3DNow;
1810
    } else if (flags & SWS_CPU_CAPS_MMX) {
1811
        sws_init_swScale_MMX(c);
1812
        return swScale_MMX;
1813
    } else {
1814
        sws_init_swScale_C(c);
1815
        return swScale_C;
1816
    }
1817

    
1818
#else
1819
#if ARCH_PPC
1820
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1821
        sws_init_swScale_altivec(c);
1822
        return swScale_altivec;
1823
    } else {
1824
        sws_init_swScale_C(c);
1825
        return swScale_C;
1826
    }
1827
#endif
1828
    sws_init_swScale_C(c);
1829
    return swScale_C;
1830
#endif /* ARCH_X86 */
1831
#else //CONFIG_RUNTIME_CPUDETECT
1832
#if   HAVE_MMX2
1833
    sws_init_swScale_MMX2(c);
1834
    return swScale_MMX2;
1835
#elif HAVE_AMD3DNOW
1836
    sws_init_swScale_3DNow(c);
1837
    return swScale_3DNow;
1838
#elif HAVE_MMX
1839
    sws_init_swScale_MMX(c);
1840
    return swScale_MMX;
1841
#elif HAVE_ALTIVEC
1842
    sws_init_swScale_altivec(c);
1843
    return swScale_altivec;
1844
#else
1845
    sws_init_swScale_C(c);
1846
    return swScale_C;
1847
#endif
1848
#endif //!CONFIG_RUNTIME_CPUDETECT
1849
}
1850

    
1851
static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1852
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1853
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1854
    /* Copy Y plane */
1855
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1856
        memcpy(dst, src[0], srcSliceH*dstStride[0]);
1857
    else
1858
    {
1859
        int i;
1860
        const uint8_t *srcPtr= src[0];
1861
        uint8_t *dstPtr= dst;
1862
        for (i=0; i<srcSliceH; i++)
1863
        {
1864
            memcpy(dstPtr, srcPtr, c->srcW);
1865
            srcPtr+= srcStride[0];
1866
            dstPtr+= dstStride[0];
1867
        }
1868
    }
1869
    dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1870
    if (c->dstFormat == PIX_FMT_NV12)
1871
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1872
    else
1873
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1874

    
1875
    return srcSliceH;
1876
}
1877

    
1878
static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1879
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1880
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1881

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

    
1884
    return srcSliceH;
1885
}
1886

    
1887
static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1888
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1889
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1890

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

    
1893
    return srcSliceH;
1894
}
1895

    
1896
static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1897
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1898
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1899

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

    
1902
    return srcSliceH;
1903
}
1904

    
1905
static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1906
                                int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1907
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1908

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

    
1911
    return srcSliceH;
1912
}
1913

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

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

    
1922
    if (dstParam[3])
1923
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1924

    
1925
    return srcSliceH;
1926
}
1927

    
1928
static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1929
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1930
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1931
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1932
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1933

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

    
1936
    return srcSliceH;
1937
}
1938

    
1939
static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1940
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1941
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1942
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1943
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1944

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

    
1947
    if (dstParam[3])
1948
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1949

    
1950
    return srcSliceH;
1951
}
1952

    
1953
static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1954
                               int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1955
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1956
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1957
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1958

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

    
1961
    return srcSliceH;
1962
}
1963

    
1964
static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1965
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
1966
    const enum PixelFormat srcFormat= c->srcFormat;
1967
    const enum PixelFormat dstFormat= c->dstFormat;
1968
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1969
                 const uint8_t *palette)=NULL;
1970
    int i;
1971
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1972
    uint8_t *srcPtr= src[0];
1973

    
1974
    if (!usePal(srcFormat))
1975
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1976
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1977

    
1978
    switch(dstFormat){
1979
    case PIX_FMT_RGB32  : conv = palette8topacked32; break;
1980
    case PIX_FMT_BGR32  : conv = palette8topacked32; break;
1981
    case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1982
    case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1983
    case PIX_FMT_RGB24  : conv = palette8topacked24; break;
1984
    case PIX_FMT_BGR24  : conv = palette8topacked24; break;
1985
    default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1986
                    sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1987
    }
1988

    
1989

    
1990
    for (i=0; i<srcSliceH; i++) {
1991
        conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1992
        srcPtr+= srcStride[0];
1993
        dstPtr+= dstStride[0];
1994
    }
1995

    
1996
    return srcSliceH;
1997
}
1998

    
1999
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2000
static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2001
                          int srcSliceH, uint8_t* dst[], int dstStride[]){
2002
    const enum PixelFormat srcFormat= c->srcFormat;
2003
    const enum PixelFormat dstFormat= c->dstFormat;
2004
    const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2005
    const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2006
    const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2007
    const int dstId= fmt_depth(dstFormat) >> 2;
2008
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2009

    
2010
    /* BGR -> BGR */
2011
    if (  (isBGR(srcFormat) && isBGR(dstFormat))
2012
       || (isRGB(srcFormat) && isRGB(dstFormat))){
2013
        switch(srcId | (dstId<<4)){
2014
        case 0x34: conv= rgb16to15; break;
2015
        case 0x36: conv= rgb24to15; break;
2016
        case 0x38: conv= rgb32to15; break;
2017
        case 0x43: conv= rgb15to16; break;
2018
        case 0x46: conv= rgb24to16; break;
2019
        case 0x48: conv= rgb32to16; break;
2020
        case 0x63: conv= rgb15to24; break;
2021
        case 0x64: conv= rgb16to24; break;
2022
        case 0x68: conv= rgb32to24; break;
2023
        case 0x83: conv= rgb15to32; break;
2024
        case 0x84: conv= rgb16to32; break;
2025
        case 0x86: conv= rgb24to32; break;
2026
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2027
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2028
        }
2029
    }else if (  (isBGR(srcFormat) && isRGB(dstFormat))
2030
             || (isRGB(srcFormat) && isBGR(dstFormat))){
2031
        switch(srcId | (dstId<<4)){
2032
        case 0x33: conv= rgb15tobgr15; break;
2033
        case 0x34: conv= rgb16tobgr15; break;
2034
        case 0x36: conv= rgb24tobgr15; break;
2035
        case 0x38: conv= rgb32tobgr15; break;
2036
        case 0x43: conv= rgb15tobgr16; break;
2037
        case 0x44: conv= rgb16tobgr16; break;
2038
        case 0x46: conv= rgb24tobgr16; break;
2039
        case 0x48: conv= rgb32tobgr16; break;
2040
        case 0x63: conv= rgb15tobgr24; break;
2041
        case 0x64: conv= rgb16tobgr24; break;
2042
        case 0x66: conv= rgb24tobgr24; break;
2043
        case 0x68: conv= rgb32tobgr24; break;
2044
        case 0x83: conv= rgb15tobgr32; break;
2045
        case 0x84: conv= rgb16tobgr32; break;
2046
        case 0x86: conv= rgb24tobgr32; break;
2047
        case 0x88: conv= rgb32tobgr32; break;
2048
        default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2049
                        sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2050
        }
2051
    }else{
2052
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2053
               sws_format_name(srcFormat), sws_format_name(dstFormat));
2054
    }
2055

    
2056
    if(conv)
2057
    {
2058
        uint8_t *srcPtr= src[0];
2059
        if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2060
            srcPtr += ALT32_CORR;
2061

    
2062
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2063
            conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2064
        else
2065
        {
2066
            int i;
2067
            uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2068

    
2069
            for (i=0; i<srcSliceH; i++)
2070
            {
2071
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
2072
                srcPtr+= srcStride[0];
2073
                dstPtr+= dstStride[0];
2074
            }
2075
        }
2076
    }
2077
    return srcSliceH;
2078
}
2079

    
2080
static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2081
                              int srcSliceH, uint8_t* dst[], int dstStride[]){
2082

    
2083
    rgb24toyv12(
2084
        src[0],
2085
        dst[0]+ srcSliceY    *dstStride[0],
2086
        dst[1]+(srcSliceY>>1)*dstStride[1],
2087
        dst[2]+(srcSliceY>>1)*dstStride[2],
2088
        c->srcW, srcSliceH,
2089
        dstStride[0], dstStride[1], srcStride[0]);
2090
    if (dst[3])
2091
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2092
    return srcSliceH;
2093
}
2094

    
2095
static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2096
                             int srcSliceH, uint8_t* dst[], int dstStride[]){
2097
    int i;
2098

    
2099
    /* copy Y */
2100
    if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2101
        memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2102
    else{
2103
        uint8_t *srcPtr= src[0];
2104
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2105

    
2106
        for (i=0; i<srcSliceH; i++)
2107
        {
2108
            memcpy(dstPtr, srcPtr, c->srcW);
2109
            srcPtr+= srcStride[0];
2110
            dstPtr+= dstStride[0];
2111
        }
2112
    }
2113

    
2114
    if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2115
        planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2116
                 srcSliceH >> 2, srcStride[1], dstStride[1]);
2117
        planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2118
                 srcSliceH >> 2, srcStride[2], dstStride[2]);
2119
    }else{
2120
        planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2121
                 srcSliceH >> 2, srcStride[1], dstStride[2]);
2122
        planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2123
                 srcSliceH >> 2, srcStride[2], dstStride[1]);
2124
    }
2125
    if (dst[3])
2126
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2127
    return srcSliceH;
2128
}
2129

    
2130
/* unscaled copy like stuff (assumes nearly identical formats) */
2131
static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2132
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2133
{
2134
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2135
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2136
    else
2137
    {
2138
        int i;
2139
        uint8_t *srcPtr= src[0];
2140
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2141
        int length=0;
2142

    
2143
        /* universal length finder */
2144
        while(length+c->srcW <= FFABS(dstStride[0])
2145
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2146
        assert(length!=0);
2147

    
2148
        for (i=0; i<srcSliceH; i++)
2149
        {
2150
            memcpy(dstPtr, srcPtr, length);
2151
            srcPtr+= srcStride[0];
2152
            dstPtr+= dstStride[0];
2153
        }
2154
    }
2155
    return srcSliceH;
2156
}
2157

    
2158
static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2159
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2160
{
2161
    int plane, i, j;
2162
    for (plane=0; plane<4; plane++)
2163
    {
2164
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
2165
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2166
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2167
        uint8_t *srcPtr= src[plane];
2168
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2169

    
2170
        if (!dst[plane]) continue;
2171
        // ignore palette for GRAY8
2172
        if (plane == 1 && !dst[2]) continue;
2173
        if (!src[plane] || (plane == 1 && !src[2])){
2174
            if(is16BPS(c->dstFormat))
2175
                length*=2;
2176
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2177
        }else
2178
        {
2179
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2180
                if (!isBE(c->srcFormat)) srcPtr++;
2181
                for (i=0; i<height; i++){
2182
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2183
                    srcPtr+= srcStride[plane];
2184
                    dstPtr+= dstStride[plane];
2185
                }
2186
            }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2187
                for (i=0; i<height; i++){
2188
                    for (j=0; j<length; j++){
2189
                        dstPtr[ j<<1   ] = srcPtr[j];
2190
                        dstPtr[(j<<1)+1] = srcPtr[j];
2191
                    }
2192
                    srcPtr+= srcStride[plane];
2193
                    dstPtr+= dstStride[plane];
2194
                }
2195
            }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2196
                  && isBE(c->srcFormat) != isBE(c->dstFormat)){
2197

    
2198
                for (i=0; i<height; i++){
2199
                    for (j=0; j<length; j++)
2200
                        ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2201
                    srcPtr+= srcStride[plane];
2202
                    dstPtr+= dstStride[plane];
2203
                }
2204
            } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2205
                memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2206
            else
2207
            {
2208
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2209
                    length*=2;
2210
                for (i=0; i<height; i++)
2211
                {
2212
                    memcpy(dstPtr, srcPtr, length);
2213
                    srcPtr+= srcStride[plane];
2214
                    dstPtr+= dstStride[plane];
2215
                }
2216
            }
2217
        }
2218
    }
2219
    return srcSliceH;
2220
}
2221

    
2222

    
2223
static void getSubSampleFactors(int *h, int *v, int format){
2224
    switch(format){
2225
    case PIX_FMT_UYVY422:
2226
    case PIX_FMT_YUYV422:
2227
        *h=1;
2228
        *v=0;
2229
        break;
2230
    case PIX_FMT_YUV420P:
2231
    case PIX_FMT_YUV420PLE:
2232
    case PIX_FMT_YUV420PBE:
2233
    case PIX_FMT_YUVA420P:
2234
    case PIX_FMT_GRAY16BE:
2235
    case PIX_FMT_GRAY16LE:
2236
    case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2237
    case PIX_FMT_NV12:
2238
    case PIX_FMT_NV21:
2239
        *h=1;
2240
        *v=1;
2241
        break;
2242
    case PIX_FMT_YUV440P:
2243
        *h=0;
2244
        *v=1;
2245
        break;
2246
    case PIX_FMT_YUV410P:
2247
        *h=2;
2248
        *v=2;
2249
        break;
2250
    case PIX_FMT_YUV444P:
2251
    case PIX_FMT_YUV444PLE:
2252
    case PIX_FMT_YUV444PBE:
2253
        *h=0;
2254
        *v=0;
2255
        break;
2256
    case PIX_FMT_YUV422P:
2257
    case PIX_FMT_YUV422PLE:
2258
    case PIX_FMT_YUV422PBE:
2259
        *h=1;
2260
        *v=0;
2261
        break;
2262
    case PIX_FMT_YUV411P:
2263
        *h=2;
2264
        *v=0;
2265
        break;
2266
    default:
2267
        *h=0;
2268
        *v=0;
2269
        break;
2270
    }
2271
}
2272

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

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

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

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

    
2298
    c->uOffset=   0x0400040004000400LL;
2299
    c->vOffset=   0x0400040004000400LL;
2300

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

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

    
2317
    oy -= 256*brightness;
2318

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

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

    
2333
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2334
    //FIXME factorize
2335

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

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

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

    
2354
    return 0;
2355
}
2356

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

    
2377
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, 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 || !CONFIG_GPL //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   HAVE_MMX2
2395
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2396
#elif HAVE_AMD3DNOW
2397
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2398
#elif HAVE_MMX
2399
    flags |= SWS_CPU_CAPS_MMX;
2400
#elif HAVE_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 (clip_table[512] != 255) globalInit();
2407
    if (!rgb15to16) sws_rgb2rgb_init(flags);
2408

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

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

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

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

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

    
2457
    if (!dstFilter) dstFilter= &dummyFilter;
2458
    if (!srcFilter) srcFilter= &dummyFilter;
2459

    
2460
    c= av_mallocz(sizeof(SwsContext));
2461

    
2462
    c->av_class = &sws_context_class;
2463
    c->srcW= srcW;
2464
    c->srcH= srcH;
2465
    c->dstW= dstW;
2466
    c->dstH= dstH;
2467
    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2468
    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2469
    c->flags= flags;
2470
    c->dstFormat= dstFormat;
2471
    c->srcFormat= srcFormat;
2472
    c->vRounder= 4* 0x0001000100010001ULL;
2473

    
2474
    usesHFilter= usesVFilter= 0;
2475
    if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2476
    if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2477
    if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2478
    if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2479
    if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2480
    if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2481
    if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2482
    if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2483

    
2484
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2485
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2486

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

    
2490
    // drop some chroma lines if the user wants it
2491
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2492
    c->chrSrcVSubSample+= c->vChrDrop;
2493

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

    
2502
    if (param){
2503
        c->param[0] = param[0];
2504
        c->param[1] = param[1];
2505
    }else{
2506
        c->param[0] =
2507
        c->param[1] = SWS_PARAM_DEFAULT;
2508
    }
2509

    
2510
    c->chrIntHSubSample= c->chrDstHSubSample;
2511
    c->chrIntVSubSample= c->chrSrcVSubSample;
2512

    
2513
    // Note the -((-x)>>y) is so that we always round toward +inf.
2514
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2515
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2516
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2517
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2518

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

    
2521
    /* unscaled special cases */
2522
    if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2523
    {
2524
        /* yv12_to_nv12 */
2525
        if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2526
        {
2527
            c->swScale= PlanarToNV12Wrapper;
2528
        }
2529
        /* yuv2bgr */
2530
        if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2531
            && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2532
        {
2533
            c->swScale= ff_yuv2rgb_get_func_ptr(c);
2534
        }
2535

    
2536
        if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2537
        {
2538
            c->swScale= yvu9toyv12Wrapper;
2539
        }
2540

    
2541
        /* bgr24toYV12 */
2542
        if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2543
            c->swScale= bgr24toyv12Wrapper;
2544

    
2545
        /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2546
        if (  (isBGR(srcFormat) || isRGB(srcFormat))
2547
           && (isBGR(dstFormat) || isRGB(dstFormat))
2548
           && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
2549
           && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
2550
           && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
2551
           && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
2552
           && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2553
           && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2554
           && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2555
           && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2556
                                             && dstFormat != PIX_FMT_RGB32_1
2557
                                             && dstFormat != PIX_FMT_BGR32_1
2558
           && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2559
             c->swScale= rgb2rgbWrapper;
2560

    
2561
        if ((usePal(srcFormat) && (
2562
                 dstFormat == PIX_FMT_RGB32   ||
2563
                 dstFormat == PIX_FMT_RGB32_1 ||
2564
                 dstFormat == PIX_FMT_RGB24   ||
2565
                 dstFormat == PIX_FMT_BGR32   ||
2566
                 dstFormat == PIX_FMT_BGR32_1 ||
2567
                 dstFormat == PIX_FMT_BGR24)))
2568
             c->swScale= pal2rgbWrapper;
2569

    
2570
        if (srcFormat == PIX_FMT_YUV422P)
2571
        {
2572
            if (dstFormat == PIX_FMT_YUYV422)
2573
                c->swScale= YUV422PToYuy2Wrapper;
2574
            else if (dstFormat == PIX_FMT_UYVY422)
2575
                c->swScale= YUV422PToUyvyWrapper;
2576
        }
2577

    
2578
        /* LQ converters if -sws 0 or -sws 4*/
2579
        if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2580
            /* yv12_to_yuy2 */
2581
            if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2582
            {
2583
                if (dstFormat == PIX_FMT_YUYV422)
2584
                    c->swScale= PlanarToYuy2Wrapper;
2585
                else if (dstFormat == PIX_FMT_UYVY422)
2586
                    c->swScale= PlanarToUyvyWrapper;
2587
            }
2588
        }
2589
        if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2590
            c->swScale= YUYV2YUV420Wrapper;
2591
        if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2592
            c->swScale= UYVY2YUV420Wrapper;
2593
        if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2594
            c->swScale= YUYV2YUV422Wrapper;
2595
        if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2596
            c->swScale= UYVY2YUV422Wrapper;
2597

    
2598
#ifdef COMPILE_ALTIVEC
2599
        if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2600
            !(c->flags & SWS_BITEXACT) &&
2601
            srcFormat == PIX_FMT_YUV420P) {
2602
          // unscaled YV12 -> packed YUV, we want speed
2603
          if (dstFormat == PIX_FMT_YUYV422)
2604
              c->swScale= yv12toyuy2_unscaled_altivec;
2605
          else if (dstFormat == PIX_FMT_UYVY422)
2606
              c->swScale= yv12touyvy_unscaled_altivec;
2607
        }
2608
#endif
2609

    
2610
        /* simple copy */
2611
        if (  srcFormat == dstFormat
2612
            || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2613
            || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2614
            || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2615
            || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2616
            || (isGray(dstFormat) && isGray(srcFormat))
2617
            || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2618
                && c->chrDstHSubSample == c->chrSrcHSubSample
2619
                && c->chrDstVSubSample == c->chrSrcVSubSample))
2620
        {
2621
            if (isPacked(c->srcFormat))
2622
                c->swScale= packedCopy;
2623
            else /* Planar YUV or gray */
2624
                c->swScale= planarCopy;
2625
        }
2626
#if ARCH_BFIN
2627
        if (flags & SWS_CPU_CAPS_BFIN)
2628
            ff_bfin_get_unscaled_swscale (c);
2629
#endif
2630

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

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

    
2652
    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2653
    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2654

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

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

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

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

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

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

    
2719

    
2720

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

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

    
2737
#if HAVE_ALTIVEC
2738
        c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2739
        c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2740

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

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

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

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

    
2774
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2775
    c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2776
    c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2777
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2778
        c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2779
    //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)
2780
    /* align at 16 bytes for AltiVec */
2781
    for (i=0; i<c->vLumBufSize; i++)
2782
        c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2783
    for (i=0; i<c->vChrBufSize; i++)
2784
        c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2785
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2786
        for (i=0; i<c->vLumBufSize; i++)
2787
            c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
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
    {
2798
#ifdef DITHER1XBPP
2799
        const char *dither= " dithered";
2800
#else
2801
        const char *dither= "";
2802
#endif
2803
        if (flags&SWS_FAST_BILINEAR)
2804
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2805
        else if (flags&SWS_BILINEAR)
2806
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2807
        else if (flags&SWS_BICUBIC)
2808
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2809
        else if (flags&SWS_X)
2810
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2811
        else if (flags&SWS_POINT)
2812
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2813
        else if (flags&SWS_AREA)
2814
            av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2815
        else if (flags&SWS_BICUBLIN)
2816
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2817
        else if (flags&SWS_GAUSS)
2818
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2819
        else if (flags&SWS_SINC)
2820
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2821
        else if (flags&SWS_LANCZOS)
2822
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2823
        else if (flags&SWS_SPLINE)
2824
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2825
        else
2826
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2827

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

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

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

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

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

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

    
2919
    c->swScale= getSwsFunc(c);
2920
    return c;
2921
}
2922

    
2923
static void reset_ptr(uint8_t* src[], int format){
2924
    if(!isALPHA(format))
2925
        src[3]=NULL;
2926
    if(!isPlanarYUV(format)){
2927
        src[3]=src[2]=NULL;
2928
        if(   format != PIX_FMT_PAL8
2929
           && format != PIX_FMT_RGB8
2930
           && format != PIX_FMT_BGR8
2931
           && format != PIX_FMT_RGB4_BYTE
2932
           && format != PIX_FMT_BGR4_BYTE
2933
          )
2934
            src[1]= NULL;
2935
    }
2936
}
2937

    
2938
/**
2939
 * swscale wrapper, so we don't need to export the SwsContext.
2940
 * Assumes planar YUV to be in YUV order instead of YVU.
2941
 */
2942
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2943
              int srcSliceH, uint8_t* dst[], int dstStride[]){
2944
    int i;
2945
    uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2946
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2947

    
2948
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2949
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2950
        return 0;
2951
    }
2952
    if (c->sliceDir == 0) {
2953
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2954
    }
2955

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

    
2987

    
2988
            switch(c->dstFormat) {
2989
            case PIX_FMT_BGR32:
2990
#ifndef WORDS_BIGENDIAN
2991
            case PIX_FMT_RGB24:
2992
#endif
2993
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2994
                break;
2995
            case PIX_FMT_BGR32_1:
2996
#ifdef  WORDS_BIGENDIAN
2997
            case PIX_FMT_BGR24:
2998
#endif
2999
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3000
                break;
3001
            case PIX_FMT_RGB32_1:
3002
#ifdef  WORDS_BIGENDIAN
3003
            case PIX_FMT_RGB24:
3004
#endif
3005
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3006
                break;
3007
            case PIX_FMT_RGB32:
3008
#ifndef WORDS_BIGENDIAN
3009
            case PIX_FMT_BGR24:
3010
#endif
3011
            default:
3012
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
3013
            }
3014
        }
3015
    }
3016

    
3017
    // copy strides, so they can safely be modified
3018
    if (c->sliceDir == 1) {
3019
        // slices go from top to bottom
3020
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3021
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3022

    
3023
        reset_ptr(src2, c->srcFormat);
3024
        reset_ptr(dst2, c->dstFormat);
3025

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

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

    
3042
        reset_ptr(src2, c->srcFormat);
3043
        reset_ptr(dst2, c->dstFormat);
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, uint8_t* src[], int srcStride[], int srcSliceY,
3051
                      int srcSliceH, uint8_t* dst[], int dstStride[]){
3052
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3053
}
3054
#endif
3055

    
3056
SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3057
                                float lumaSharpen, float chromaSharpen,
3058
                                float chromaHShift, float chromaVShift,
3059
                                int verbose)
3060
{
3061
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3062

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

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

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

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

    
3097
    if (chromaHShift != 0.0)
3098
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3099

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

    
3103
    sws_normalizeVec(filter->chrH, 1.0);
3104
    sws_normalizeVec(filter->chrV, 1.0);
3105
    sws_normalizeVec(filter->lumH, 1.0);
3106
    sws_normalizeVec(filter->lumV, 1.0);
3107

    
3108
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3109
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3110

    
3111
    return filter;
3112
}
3113

    
3114
SwsVector *sws_getGaussianVec(double variance, double quality){
3115
    const int length= (int)(variance*quality + 0.5) | 1;
3116
    int i;
3117
    double *coeff= av_malloc(length*sizeof(double));
3118
    double middle= (length-1)*0.5;
3119
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3120

    
3121
    vec->coeff= coeff;
3122
    vec->length= length;
3123

    
3124
    for (i=0; i<length; i++)
3125
    {
3126
        double dist= i-middle;
3127
        coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3128
    }
3129

    
3130
    sws_normalizeVec(vec, 1.0);
3131

    
3132
    return vec;
3133
}
3134

    
3135
SwsVector *sws_getConstVec(double c, int length){
3136
    int i;
3137
    double *coeff= av_malloc(length*sizeof(double));
3138
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3139

    
3140
    vec->coeff= coeff;
3141
    vec->length= length;
3142

    
3143
    for (i=0; i<length; i++)
3144
        coeff[i]= c;
3145

    
3146
    return vec;
3147
}
3148

    
3149

    
3150
SwsVector *sws_getIdentityVec(void){
3151
    return sws_getConstVec(1.0, 1);
3152
}
3153

    
3154
double sws_dcVec(SwsVector *a){
3155
    int i;
3156
    double sum=0;
3157

    
3158
    for (i=0; i<a->length; i++)
3159
        sum+= a->coeff[i];
3160

    
3161
    return sum;
3162
}
3163

    
3164
void sws_scaleVec(SwsVector *a, double scalar){
3165
    int i;
3166

    
3167
    for (i=0; i<a->length; i++)
3168
        a->coeff[i]*= scalar;
3169
}
3170

    
3171
void sws_normalizeVec(SwsVector *a, double height){
3172
    sws_scaleVec(a, height/sws_dcVec(a));
3173
}
3174

    
3175
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3176
    int length= a->length + b->length - 1;
3177
    double *coeff= av_malloc(length*sizeof(double));
3178
    int i, j;
3179
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3180

    
3181
    vec->coeff= coeff;
3182
    vec->length= length;
3183

    
3184
    for (i=0; i<length; i++) coeff[i]= 0.0;
3185

    
3186
    for (i=0; i<a->length; i++)
3187
    {
3188
        for (j=0; j<b->length; j++)
3189
        {
3190
            coeff[i+j]+= a->coeff[i]*b->coeff[j];
3191
        }
3192
    }
3193

    
3194
    return vec;
3195
}
3196

    
3197
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3198
    int length= FFMAX(a->length, b->length);
3199
    double *coeff= av_malloc(length*sizeof(double));
3200
    int i;
3201
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3202

    
3203
    vec->coeff= coeff;
3204
    vec->length= length;
3205

    
3206
    for (i=0; i<length; i++) coeff[i]= 0.0;
3207

    
3208
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3209
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3210

    
3211
    return vec;
3212
}
3213

    
3214
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3215
    int length= FFMAX(a->length, b->length);
3216
    double *coeff= av_malloc(length*sizeof(double));
3217
    int i;
3218
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3219

    
3220
    vec->coeff= coeff;
3221
    vec->length= length;
3222

    
3223
    for (i=0; i<length; i++) coeff[i]= 0.0;
3224

    
3225
    for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3226
    for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3227

    
3228
    return vec;
3229
}
3230

    
3231
/* shift left / or right if "shift" is negative */
3232
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3233
    int length= a->length + FFABS(shift)*2;
3234
    double *coeff= av_malloc(length*sizeof(double));
3235
    int i;
3236
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3237

    
3238
    vec->coeff= coeff;
3239
    vec->length= length;
3240

    
3241
    for (i=0; i<length; i++) coeff[i]= 0.0;
3242

    
3243
    for (i=0; i<a->length; i++)
3244
    {
3245
        coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3246
    }
3247

    
3248
    return vec;
3249
}
3250

    
3251
void sws_shiftVec(SwsVector *a, int shift){
3252
    SwsVector *shifted= sws_getShiftedVec(a, shift);
3253
    av_free(a->coeff);
3254
    a->coeff= shifted->coeff;
3255
    a->length= shifted->length;
3256
    av_free(shifted);
3257
}
3258

    
3259
void sws_addVec(SwsVector *a, SwsVector *b){
3260
    SwsVector *sum= sws_sumVec(a, b);
3261
    av_free(a->coeff);
3262
    a->coeff= sum->coeff;
3263
    a->length= sum->length;
3264
    av_free(sum);
3265
}
3266

    
3267
void sws_subVec(SwsVector *a, SwsVector *b){
3268
    SwsVector *diff= sws_diffVec(a, b);
3269
    av_free(a->coeff);
3270
    a->coeff= diff->coeff;
3271
    a->length= diff->length;
3272
    av_free(diff);
3273
}
3274

    
3275
void sws_convVec(SwsVector *a, SwsVector *b){
3276
    SwsVector *conv= sws_getConvVec(a, b);
3277
    av_free(a->coeff);
3278
    a->coeff= conv->coeff;
3279
    a->length= conv->length;
3280
    av_free(conv);
3281
}
3282

    
3283
SwsVector *sws_cloneVec(SwsVector *a){
3284
    double *coeff= av_malloc(a->length*sizeof(double));
3285
    int i;
3286
    SwsVector *vec= av_malloc(sizeof(SwsVector));
3287

    
3288
    vec->coeff= coeff;
3289
    vec->length= a->length;
3290

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

    
3293
    return vec;
3294
}
3295

    
3296
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3297
    int i;
3298
    double max=0;
3299
    double min=0;
3300
    double range;
3301

    
3302
    for (i=0; i<a->length; i++)
3303
        if (a->coeff[i]>max) max= a->coeff[i];
3304

    
3305
    for (i=0; i<a->length; i++)
3306
        if (a->coeff[i]<min) min= a->coeff[i];
3307

    
3308
    range= max - min;
3309

    
3310
    for (i=0; i<a->length; i++)
3311
    {
3312
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3313
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3314
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
3315
        av_log(log_ctx, log_level, "|\n");
3316
    }
3317
}
3318

    
3319
#if LIBSWSCALE_VERSION_MAJOR < 1
3320
void sws_printVec(SwsVector *a){
3321
    sws_printVec2(a, NULL, AV_LOG_DEBUG);
3322
}
3323
#endif
3324

    
3325
void sws_freeVec(SwsVector *a){
3326
    if (!a) return;
3327
    av_freep(&a->coeff);
3328
    a->length=0;
3329
    av_free(a);
3330
}
3331

    
3332
void sws_freeFilter(SwsFilter *filter){
3333
    if (!filter) return;
3334

    
3335
    if (filter->lumH) sws_freeVec(filter->lumH);
3336
    if (filter->lumV) sws_freeVec(filter->lumV);
3337
    if (filter->chrH) sws_freeVec(filter->chrH);
3338
    if (filter->chrV) sws_freeVec(filter->chrV);
3339
    av_free(filter);
3340
}
3341

    
3342

    
3343
void sws_freeContext(SwsContext *c){
3344
    int i;
3345
    if (!c) return;
3346

    
3347
    if (c->lumPixBuf)
3348
    {
3349
        for (i=0; i<c->vLumBufSize; i++)
3350
            av_freep(&c->lumPixBuf[i]);
3351
        av_freep(&c->lumPixBuf);
3352
    }
3353

    
3354
    if (c->chrPixBuf)
3355
    {
3356
        for (i=0; i<c->vChrBufSize; i++)
3357
            av_freep(&c->chrPixBuf[i]);
3358
        av_freep(&c->chrPixBuf);
3359
    }
3360

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

    
3367
    av_freep(&c->vLumFilter);
3368
    av_freep(&c->vChrFilter);
3369
    av_freep(&c->hLumFilter);
3370
    av_freep(&c->hChrFilter);
3371
#if HAVE_ALTIVEC
3372
    av_freep(&c->vYCoeffsBank);
3373
    av_freep(&c->vCCoeffsBank);
3374
#endif
3375

    
3376
    av_freep(&c->vLumFilterPos);
3377
    av_freep(&c->vChrFilterPos);
3378
    av_freep(&c->hLumFilterPos);
3379
    av_freep(&c->hChrFilterPos);
3380

    
3381
#if ARCH_X86 && CONFIG_GPL
3382
#ifdef MAP_ANONYMOUS
3383
    if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3384
    if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3385
#elif HAVE_VIRTUALALLOC
3386
    if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3387
    if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3388
#else
3389
    av_free(c->funnyYCode );
3390
    av_free(c->funnyUVCode);
3391
#endif
3392
    c->funnyYCode=NULL;
3393
    c->funnyUVCode=NULL;
3394
#endif /* ARCH_X86 && CONFIG_GPL */
3395

    
3396
    av_freep(&c->lumMmx2Filter);
3397
    av_freep(&c->chrMmx2Filter);
3398
    av_freep(&c->lumMmx2FilterPos);
3399
    av_freep(&c->chrMmx2FilterPos);
3400
    av_freep(&c->yuvTable);
3401

    
3402
    av_free(c);
3403
}
3404

    
3405
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3406
                                        int srcW, int srcH, enum PixelFormat srcFormat,
3407
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3408
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3409
{
3410
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3411

    
3412
    if (!param)
3413
        param = default_param;
3414

    
3415
    if (context) {
3416
        if (context->srcW != srcW || context->srcH != srcH ||
3417
            context->srcFormat != srcFormat ||
3418
            context->dstW != dstW || context->dstH != dstH ||
3419
            context->dstFormat != dstFormat || context->flags != flags ||
3420
            context->param[0] != param[0] || context->param[1] != param[1])
3421
        {
3422
            sws_freeContext(context);
3423
            context = NULL;
3424
        }
3425
    }
3426
    if (!context) {
3427
        return sws_getContext(srcW, srcH, srcFormat,
3428
                              dstW, dstH, dstFormat, flags,
3429
                              srcFilter, dstFilter, param);
3430
    }
3431
    return context;
3432
}
3433