Allocate executable memory with VirtualAlloc() in Windows.
[libav.git] / libswscale / swscale.c
CommitLineData
fe8054c0 1/*
d026b45e
DB
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
b19bcbaa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d026b45e 19 *
807e0c66
LA
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
d026b45e 22 */
783e9cc9 23
28bf81c9 24/*
9990e426 25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
caeaabe7 26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
e09d12f4 27 {BGR,RGB}{1,4,8,15,16} support dithering
6a4970ab 28
e09d12f4
MN
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
b935781b
MN
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
4bb3fa5e 37 BGR15 -> BGR16
b935781b
MN
38*/
39
6a4970ab 40/*
a6f6b237 41tested special converters (most are tested actually, but I did not write it down ...)
e09d12f4 42 YV12 -> BGR16
b935781b 43 YV12 -> YV12
4bb3fa5e 44 BGR15 -> BGR16
1e1c4fe9 45 BGR16 -> BGR16
e09d12f4 46 YVU9 -> YV12
b935781b
MN
47
48untested special converters
f40c7dbb 49 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
1e1c4fe9
MN
50 YV12/I420 -> YV12/I420
51 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
b935781b
MN
52 BGR24 -> BGR32 & RGB24 -> RGB32
53 BGR32 -> BGR24 & RGB32 -> RGB24
ec22603f 54 BGR24 -> YV12
28bf81c9
MN
55*/
56
d63a2cb1 57#define _SVID_SOURCE //needed for MAP_ANONYMOUS
d3f41512 58#include <inttypes.h>
dda87e9f 59#include <string.h>
077ea8a7 60#include <math.h>
c1b0bfb4 61#include <stdio.h>
171d7d78 62#include <unistd.h>
b2d374c9 63#include "config.h"
81b7c056 64#include <assert.h>
b63f641e 65#if HAVE_SYS_MMAN_H
38d5c282 66#include <sys/mman.h>
113ef149
RD
67#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
68#define MAP_ANONYMOUS MAP_ANON
69#endif
38d5c282 70#endif
dd35beb2
RP
71#if HAVE_VIRTUALALLOC
72#define WIN32_LEAN_AND_MEAN
73#include <windows.h>
74#endif
d604bab9 75#include "swscale.h"
5427e242 76#include "swscale_internal.h"
37079906 77#include "rgb2rgb.h"
83da2c6f
DB
78#include "libavutil/x86_cpu.h"
79#include "libavutil/bswap.h"
0d9f3d85 80
b3e03fa7
SS
81unsigned swscale_version(void)
82{
83 return LIBSWSCALE_VERSION_INT;
84}
85
541c4eb9 86#undef MOVNTQ
7d7f78b5 87#undef PAVGB
d3f41512 88
783e9cc9 89//#undef HAVE_MMX2
f4406ec1 90//#define HAVE_AMD3DNOW
d3f41512 91//#undef HAVE_MMX
783e9cc9 92//#undef ARCH_X86
2ba1bff0 93//#define WORDS_BIGENDIAN
d604bab9 94#define DITHER1XBPP
d3f41512 95
f40c7dbb 96#define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
ac6a2e45 97
f40c7dbb 98#define RET 0xC3 //near return opcode for x86
c1b0bfb4 99
28bf81c9
MN
100#ifdef M_PI
101#define PI M_PI
102#else
103#define PI 3.14159265358979323846
104#endif
c1b0bfb4 105
9d9de37d
IP
106#define isSupportedIn(x) ( \
107 (x)==PIX_FMT_YUV420P \
79973335 108 || (x)==PIX_FMT_YUVA420P \
9d9de37d
IP
109 || (x)==PIX_FMT_YUYV422 \
110 || (x)==PIX_FMT_UYVY422 \
111 || (x)==PIX_FMT_RGB32 \
9990e426 112 || (x)==PIX_FMT_RGB32_1 \
9d9de37d
IP
113 || (x)==PIX_FMT_BGR24 \
114 || (x)==PIX_FMT_BGR565 \
115 || (x)==PIX_FMT_BGR555 \
116 || (x)==PIX_FMT_BGR32 \
9990e426 117 || (x)==PIX_FMT_BGR32_1 \
9d9de37d
IP
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 \
6c80eb16 123 || (x)==PIX_FMT_YUV440P \
9d9de37d
IP
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 \
9ba7fe6d 134 || (x)==PIX_FMT_YUV440P \
3d05e078
MN
135 || (x)==PIX_FMT_MONOWHITE \
136 || (x)==PIX_FMT_MONOBLACK \
9d9de37d
IP
137 )
138#define isSupportedOut(x) ( \
139 (x)==PIX_FMT_YUV420P \
140 || (x)==PIX_FMT_YUYV422 \
141 || (x)==PIX_FMT_UYVY422 \
142 || (x)==PIX_FMT_YUV444P \
143 || (x)==PIX_FMT_YUV422P \
144 || (x)==PIX_FMT_YUV411P \
145 || isRGB(x) \
146 || isBGR(x) \
147 || (x)==PIX_FMT_NV12 \
148 || (x)==PIX_FMT_NV21 \
149 || (x)==PIX_FMT_GRAY16BE \
150 || (x)==PIX_FMT_GRAY16LE \
151 || (x)==PIX_FMT_GRAY8 \
152 || (x)==PIX_FMT_YUV410P \
6c80eb16 153 || (x)==PIX_FMT_YUV440P \
9d9de37d
IP
154 )
155#define isPacked(x) ( \
156 (x)==PIX_FMT_PAL8 \
157 || (x)==PIX_FMT_YUYV422 \
158 || (x)==PIX_FMT_UYVY422 \
159 || isRGB(x) \
160 || isBGR(x) \
161 )
49004617
VS
162#define usePal(x) ( \
163 (x)==PIX_FMT_PAL8 \
164 || (x)==PIX_FMT_BGR4_BYTE \
165 || (x)==PIX_FMT_RGB4_BYTE \
166 || (x)==PIX_FMT_BGR8 \
167 || (x)==PIX_FMT_RGB8 \
168 )
6ff0ad6b 169
6b79dbce 170#define RGB2YUV_SHIFT 15
7b5d7b9e
MN
171#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
172#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
173#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
174#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
175#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
176#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
177#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
178#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
179#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
6c7506de 180
fa58ba15 181extern const int32_t ff_yuv2rgb_coeffs[8][4];
0481412a 182
0f5d4aa8
MN
183static const double rgb2yuv_table[8][9]={
184 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
185 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
186 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
187 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
188 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
189 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
190 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
191 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
192};
193
783e9cc9
MN
194/*
195NOTES
d604bab9 196Special versions: fast Y 1:1 scaling (no interpolation in y direction)
31190492 197
783e9cc9 198TODO
bd7c6fd5 199more intelligent misalignment avoidance for the horizontal scaler
c1b0bfb4 200write special vertical cubic upscale version
f40c7dbb
DB
201optimize C code (YV12 / minmax)
202add support for packed pixel YUV input & output
6ff0ad6b 203add support for Y8 output
f40c7dbb 204optimize BGR24 & BGR32
ff7ba856 205add BGR4 output support
1e621b18 206write special BGR->BGR scaler
783e9cc9 207*/
31190492 208
b63f641e 209#if ARCH_X86 && CONFIG_GPL
d334c7c2
RD
210DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
211DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
212DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
213DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
214DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
215DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
216DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
217DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
d604bab9 218
0cb25594 219const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
221b804f
DB
220 0x0103010301030103LL,
221 0x0200020002000200LL,};
d8fa3c54 222
0cb25594 223const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
221b804f
DB
224 0x0602060206020602LL,
225 0x0004000400040004LL,};
d604bab9 226
d334c7c2
RD
227DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
228DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
229DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
230DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
231DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
232DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
d604bab9 233
5802683a
RD
234DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
235DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
236DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
99d2cb72 237
ac6a2e45 238#ifdef FAST_BGR2YV12
5802683a
RD
239DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
240DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
241DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
ac6a2e45 242#else
5802683a
RD
243DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
244DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
245DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
69796008 246#endif /* FAST_BGR2YV12 */
5802683a
RD
247DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
248DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
249DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
dfb09bd1 250
b5c44b1a
DP
251DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
252DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
253DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
254DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
255DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
dfb09bd1 256
b5c44b1a 257DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
dfb09bd1
MN
258 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
259 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
260};
261
b5c44b1a 262DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
dfb09bd1 263
7a24ec50 264#endif /* ARCH_X86 && CONFIG_GPL */
783e9cc9
MN
265
266// clipping helper table for C implementations:
267static unsigned char clip_table[768];
268
d4e24275 269static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
6a4970ab 270
d0b69b28 271static const uint8_t __attribute__((aligned(8))) dither_2x2_4[2][8]={
45e18be8
MN
272{ 1, 3, 1, 3, 1, 3, 1, 3, },
273{ 2, 0, 2, 0, 2, 0, 2, 0, },
274};
275
d0b69b28 276static const uint8_t __attribute__((aligned(8))) dither_2x2_8[2][8]={
45e18be8
MN
277{ 6, 2, 6, 2, 6, 2, 6, 2, },
278{ 0, 4, 0, 4, 0, 4, 0, 4, },
279};
280
281const uint8_t __attribute__((aligned(8))) dither_8x8_32[8][8]={
282{ 17, 9, 23, 15, 16, 8, 22, 14, },
283{ 5, 29, 3, 27, 4, 28, 2, 26, },
284{ 21, 13, 19, 11, 20, 12, 18, 10, },
285{ 0, 24, 6, 30, 1, 25, 7, 31, },
286{ 16, 8, 22, 14, 17, 9, 23, 15, },
287{ 4, 28, 2, 26, 5, 29, 3, 27, },
288{ 20, 12, 18, 10, 21, 13, 19, 11, },
289{ 1, 25, 7, 31, 0, 24, 6, 30, },
290};
291
292#if 0
293const uint8_t __attribute__((aligned(8))) dither_8x8_64[8][8]={
294{ 0, 48, 12, 60, 3, 51, 15, 63, },
295{ 32, 16, 44, 28, 35, 19, 47, 31, },
296{ 8, 56, 4, 52, 11, 59, 7, 55, },
297{ 40, 24, 36, 20, 43, 27, 39, 23, },
298{ 2, 50, 14, 62, 1, 49, 13, 61, },
299{ 34, 18, 46, 30, 33, 17, 45, 29, },
300{ 10, 58, 6, 54, 9, 57, 5, 53, },
301{ 42, 26, 38, 22, 41, 25, 37, 21, },
302};
303#endif
304
305const uint8_t __attribute__((aligned(8))) dither_8x8_73[8][8]={
306{ 0, 55, 14, 68, 3, 58, 17, 72, },
307{ 37, 18, 50, 32, 40, 22, 54, 35, },
308{ 9, 64, 5, 59, 13, 67, 8, 63, },
309{ 46, 27, 41, 23, 49, 31, 44, 26, },
310{ 2, 57, 16, 71, 1, 56, 15, 70, },
311{ 39, 21, 52, 34, 38, 19, 51, 33, },
312{ 11, 66, 7, 62, 10, 65, 6, 60, },
313{ 48, 30, 43, 25, 47, 29, 42, 24, },
314};
315
316#if 0
317const uint8_t __attribute__((aligned(8))) dither_8x8_128[8][8]={
318{ 68, 36, 92, 60, 66, 34, 90, 58, },
319{ 20, 116, 12, 108, 18, 114, 10, 106, },
320{ 84, 52, 76, 44, 82, 50, 74, 42, },
321{ 0, 96, 24, 120, 6, 102, 30, 126, },
322{ 64, 32, 88, 56, 70, 38, 94, 62, },
323{ 16, 112, 8, 104, 22, 118, 14, 110, },
324{ 80, 48, 72, 40, 86, 54, 78, 46, },
325{ 4, 100, 28, 124, 2, 98, 26, 122, },
326};
327#endif
328
329#if 1
330const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
331{117, 62, 158, 103, 113, 58, 155, 100, },
332{ 34, 199, 21, 186, 31, 196, 17, 182, },
333{144, 89, 131, 76, 141, 86, 127, 72, },
334{ 0, 165, 41, 206, 10, 175, 52, 217, },
335{110, 55, 151, 96, 120, 65, 162, 107, },
336{ 28, 193, 14, 179, 38, 203, 24, 189, },
337{138, 83, 124, 69, 148, 93, 134, 79, },
338{ 7, 172, 48, 213, 3, 168, 45, 210, },
339};
340#elif 1
341// tries to correct a gamma of 1.5
342const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
343{ 0, 143, 18, 200, 2, 156, 25, 215, },
344{ 78, 28, 125, 64, 89, 36, 138, 74, },
345{ 10, 180, 3, 161, 16, 195, 8, 175, },
346{109, 51, 93, 38, 121, 60, 105, 47, },
347{ 1, 152, 23, 210, 0, 147, 20, 205, },
348{ 85, 33, 134, 71, 81, 30, 130, 67, },
349{ 14, 190, 6, 171, 12, 185, 5, 166, },
350{117, 57, 101, 44, 113, 54, 97, 41, },
351};
352#elif 1
353// tries to correct a gamma of 2.0
354const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
355{ 0, 124, 8, 193, 0, 140, 12, 213, },
356{ 55, 14, 104, 42, 66, 19, 119, 52, },
357{ 3, 168, 1, 145, 6, 187, 3, 162, },
358{ 86, 31, 70, 21, 99, 39, 82, 28, },
359{ 0, 134, 11, 206, 0, 129, 9, 200, },
360{ 62, 17, 114, 48, 58, 16, 109, 45, },
361{ 5, 181, 2, 157, 4, 175, 1, 151, },
362{ 95, 36, 78, 26, 90, 34, 74, 24, },
363};
364#else
365// tries to correct a gamma of 2.5
366const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
367{ 0, 107, 3, 187, 0, 125, 6, 212, },
368{ 39, 7, 86, 28, 49, 11, 102, 36, },
369{ 1, 158, 0, 131, 3, 180, 1, 151, },
370{ 68, 19, 52, 12, 81, 25, 64, 17, },
371{ 0, 119, 5, 203, 0, 113, 4, 195, },
372{ 45, 9, 96, 33, 42, 8, 91, 30, },
373{ 2, 172, 1, 144, 2, 165, 0, 137, },
374{ 77, 23, 60, 15, 72, 21, 56, 14, },
375};
376#endif
5cebb24b 377
8055ede6 378const char *sws_format_name(enum PixelFormat format)
94c4def2 379{
e9e12f0e
LA
380 switch (format) {
381 case PIX_FMT_YUV420P:
382 return "yuv420p";
79973335
AJ
383 case PIX_FMT_YUVA420P:
384 return "yuva420p";
e9e12f0e
LA
385 case PIX_FMT_YUYV422:
386 return "yuyv422";
387 case PIX_FMT_RGB24:
388 return "rgb24";
389 case PIX_FMT_BGR24:
390 return "bgr24";
391 case PIX_FMT_YUV422P:
392 return "yuv422p";
393 case PIX_FMT_YUV444P:
394 return "yuv444p";
395 case PIX_FMT_RGB32:
396 return "rgb32";
397 case PIX_FMT_YUV410P:
398 return "yuv410p";
399 case PIX_FMT_YUV411P:
400 return "yuv411p";
401 case PIX_FMT_RGB565:
402 return "rgb565";
403 case PIX_FMT_RGB555:
404 return "rgb555";
4884b9e5
KS
405 case PIX_FMT_GRAY16BE:
406 return "gray16be";
407 case PIX_FMT_GRAY16LE:
408 return "gray16le";
e9e12f0e
LA
409 case PIX_FMT_GRAY8:
410 return "gray8";
411 case PIX_FMT_MONOWHITE:
412 return "mono white";
413 case PIX_FMT_MONOBLACK:
414 return "mono black";
415 case PIX_FMT_PAL8:
416 return "Palette";
417 case PIX_FMT_YUVJ420P:
418 return "yuvj420p";
419 case PIX_FMT_YUVJ422P:
420 return "yuvj422p";
421 case PIX_FMT_YUVJ444P:
422 return "yuvj444p";
423 case PIX_FMT_XVMC_MPEG2_MC:
424 return "xvmc_mpeg2_mc";
425 case PIX_FMT_XVMC_MPEG2_IDCT:
426 return "xvmc_mpeg2_idct";
427 case PIX_FMT_UYVY422:
428 return "uyvy422";
429 case PIX_FMT_UYYVYY411:
430 return "uyyvyy411";
431 case PIX_FMT_RGB32_1:
432 return "rgb32x";
433 case PIX_FMT_BGR32_1:
434 return "bgr32x";
435 case PIX_FMT_BGR32:
436 return "bgr32";
437 case PIX_FMT_BGR565:
438 return "bgr565";
439 case PIX_FMT_BGR555:
440 return "bgr555";
441 case PIX_FMT_BGR8:
442 return "bgr8";
443 case PIX_FMT_BGR4:
444 return "bgr4";
445 case PIX_FMT_BGR4_BYTE:
446 return "bgr4 byte";
447 case PIX_FMT_RGB8:
448 return "rgb8";
449 case PIX_FMT_RGB4:
450 return "rgb4";
451 case PIX_FMT_RGB4_BYTE:
452 return "rgb4 byte";
453 case PIX_FMT_NV12:
454 return "nv12";
455 case PIX_FMT_NV21:
456 return "nv21";
9ba7fe6d
457 case PIX_FMT_YUV440P:
458 return "yuv440p";
420169e5
CEH
459 case PIX_FMT_VDPAU_H264:
460 return "vdpau_h264";
4e2b4876
NC
461 case PIX_FMT_VDPAU_MPEG1:
462 return "vdpau_mpeg1";
463 case PIX_FMT_VDPAU_MPEG2:
464 return "vdpau_mpeg2";
0ab80395
CEH
465 case PIX_FMT_VDPAU_WMV3:
466 return "vdpau_wmv3";
467 case PIX_FMT_VDPAU_VC1:
468 return "vdpau_vc1";
e9e12f0e
LA
469 default:
470 return "Unknown format";
471 }
94c4def2
LA
472}
473
5859233b 474static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
475 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
476 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
e3d2500f 477{
f40c7dbb 478 //FIXME Optimize (just quickly written not optimized..)
221b804f
DB
479 int i;
480 for (i=0; i<dstW; i++)
481 {
482 int val=1<<18;
483 int j;
484 for (j=0; j<lumFilterSize; j++)
485 val += lumSrc[j][i] * lumFilter[j];
486
487 dest[i]= av_clip_uint8(val>>19);
488 }
489
1b0a4572 490 if (uDest)
221b804f
DB
491 for (i=0; i<chrDstW; i++)
492 {
493 int u=1<<18;
494 int v=1<<18;
495 int j;
496 for (j=0; j<chrFilterSize; j++)
497 {
498 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 499 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
500 }
501
502 uDest[i]= av_clip_uint8(u>>19);
503 vDest[i]= av_clip_uint8(v>>19);
504 }
e3d2500f
MN
505}
506
6118e52e 507static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
508 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
509 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
6118e52e 510{
f40c7dbb 511 //FIXME Optimize (just quickly written not optimized..)
221b804f
DB
512 int i;
513 for (i=0; i<dstW; i++)
514 {
515 int val=1<<18;
516 int j;
517 for (j=0; j<lumFilterSize; j++)
518 val += lumSrc[j][i] * lumFilter[j];
519
520 dest[i]= av_clip_uint8(val>>19);
521 }
522
1b0a4572 523 if (!uDest)
221b804f
DB
524 return;
525
526 if (dstFormat == PIX_FMT_NV12)
527 for (i=0; i<chrDstW; i++)
528 {
529 int u=1<<18;
530 int v=1<<18;
531 int j;
532 for (j=0; j<chrFilterSize; j++)
533 {
534 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 535 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
536 }
537
538 uDest[2*i]= av_clip_uint8(u>>19);
539 uDest[2*i+1]= av_clip_uint8(v>>19);
540 }
541 else
542 for (i=0; i<chrDstW; i++)
543 {
544 int u=1<<18;
545 int v=1<<18;
546 int j;
547 for (j=0; j<chrFilterSize; j++)
548 {
549 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 550 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
551 }
552
553 uDest[2*i]= av_clip_uint8(v>>19);
554 uDest[2*i+1]= av_clip_uint8(u>>19);
555 }
6118e52e 556}
46de8b73 557
bdf397ba 558#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type) \
221b804f
DB
559 for (i=0; i<(dstW>>1); i++){\
560 int j;\
561 int Y1 = 1<<18;\
562 int Y2 = 1<<18;\
563 int U = 1<<18;\
564 int V = 1<<18;\
2db27aad 565 type av_unused *r, *b, *g;\
221b804f
DB
566 const int i2= 2*i;\
567 \
568 for (j=0; j<lumFilterSize; j++)\
569 {\
570 Y1 += lumSrc[j][i2] * lumFilter[j];\
571 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
572 }\
573 for (j=0; j<chrFilterSize; j++)\
574 {\
575 U += chrSrc[j][i] * chrFilter[j];\
8b2fce0d 576 V += chrSrc[j][i+VOFW] * chrFilter[j];\
221b804f
DB
577 }\
578 Y1>>=19;\
579 Y2>>=19;\
580 U >>=19;\
581 V >>=19;\
bdf397ba
MN
582
583#define YSCALE_YUV_2_PACKEDX_C(type) \
584 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type)\
221b804f
DB
585 if ((Y1|Y2|U|V)&256)\
586 {\
587 if (Y1>255) Y1=255; \
588 else if (Y1<0)Y1=0; \
589 if (Y2>255) Y2=255; \
590 else if (Y2<0)Y2=0; \
591 if (U>255) U=255; \
592 else if (U<0) U=0; \
593 if (V>255) V=255; \
594 else if (V<0) V=0; \
595 }
6a4970ab 596
f0faee4c
MN
597#define YSCALE_YUV_2_PACKEDX_FULL_C \
598 for (i=0; i<dstW; i++){\
599 int j;\
600 int Y = 0;\
601 int U = -128<<19;\
602 int V = -128<<19;\
603 int R,G,B;\
604 \
605 for (j=0; j<lumFilterSize; j++){\
606 Y += lumSrc[j][i ] * lumFilter[j];\
607 }\
608 for (j=0; j<chrFilterSize; j++){\
609 U += chrSrc[j][i ] * chrFilter[j];\
610 V += chrSrc[j][i+VOFW] * chrFilter[j];\
611 }\
612 Y >>=10;\
613 U >>=10;\
614 V >>=10;\
615
616#define YSCALE_YUV_2_RGBX_FULL_C(rnd) \
617 YSCALE_YUV_2_PACKEDX_FULL_C\
43c16478
MN
618 Y-= c->yuv2rgb_y_offset;\
619 Y*= c->yuv2rgb_y_coeff;\
f0faee4c 620 Y+= rnd;\
43c16478
MN
621 R= Y + V*c->yuv2rgb_v2r_coeff;\
622 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
623 B= Y + U*c->yuv2rgb_u2b_coeff;\
f0faee4c
MN
624 if ((R|G|B)&(0xC0000000)){\
625 if (R>=(256<<22)) R=(256<<22)-1; \
626 else if (R<0)R=0; \
627 if (G>=(256<<22)) G=(256<<22)-1; \
628 else if (G<0)G=0; \
629 if (B>=(256<<22)) B=(256<<22)-1; \
630 else if (B<0)B=0; \
631 }\
632
633
e69bd294 634#define YSCALE_YUV_2_GRAY16_C \
b0880d5d
MN
635 for (i=0; i<(dstW>>1); i++){\
636 int j;\
637 int Y1 = 1<<18;\
638 int Y2 = 1<<18;\
639 int U = 1<<18;\
640 int V = 1<<18;\
e69bd294 641 \
b0880d5d
MN
642 const int i2= 2*i;\
643 \
644 for (j=0; j<lumFilterSize; j++)\
645 {\
646 Y1 += lumSrc[j][i2] * lumFilter[j];\
647 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
648 }\
649 Y1>>=11;\
650 Y2>>=11;\
651 if ((Y1|Y2|U|V)&65536)\
652 {\
653 if (Y1>65535) Y1=65535; \
654 else if (Y1<0)Y1=0; \
655 if (Y2>65535) Y2=65535; \
656 else if (Y2<0)Y2=0; \
657 }
658
46de8b73 659#define YSCALE_YUV_2_RGBX_C(type) \
f40c7dbb 660 YSCALE_YUV_2_PACKEDX_C(type) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
221b804f
DB
661 r = (type *)c->table_rV[V]; \
662 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
663 b = (type *)c->table_bU[U]; \
664
665#define YSCALE_YUV_2_PACKED2_C \
666 for (i=0; i<(dstW>>1); i++){ \
667 const int i2= 2*i; \
668 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
669 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
670 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
8b2fce0d 671 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
46de8b73 672
b0880d5d
MN
673#define YSCALE_YUV_2_GRAY16_2_C \
674 for (i=0; i<(dstW>>1); i++){ \
675 const int i2= 2*i; \
676 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
677 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
678
46de8b73 679#define YSCALE_YUV_2_RGB2_C(type) \
221b804f
DB
680 YSCALE_YUV_2_PACKED2_C\
681 type *r, *b, *g;\
682 r = (type *)c->table_rV[V];\
683 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
684 b = (type *)c->table_bU[U];\
cf7d1c1a 685
25593e29 686#define YSCALE_YUV_2_PACKED1_C \
221b804f
DB
687 for (i=0; i<(dstW>>1); i++){\
688 const int i2= 2*i;\
689 int Y1= buf0[i2 ]>>7;\
690 int Y2= buf0[i2+1]>>7;\
691 int U= (uvbuf1[i ])>>7;\
8b2fce0d 692 int V= (uvbuf1[i+VOFW])>>7;\
46de8b73 693
b0880d5d
MN
694#define YSCALE_YUV_2_GRAY16_1_C \
695 for (i=0; i<(dstW>>1); i++){\
696 const int i2= 2*i;\
697 int Y1= buf0[i2 ]<<1;\
698 int Y2= buf0[i2+1]<<1;\
699
46de8b73 700#define YSCALE_YUV_2_RGB1_C(type) \
221b804f
DB
701 YSCALE_YUV_2_PACKED1_C\
702 type *r, *b, *g;\
703 r = (type *)c->table_rV[V];\
704 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
705 b = (type *)c->table_bU[U];\
cf7d1c1a 706
25593e29 707#define YSCALE_YUV_2_PACKED1B_C \
221b804f
DB
708 for (i=0; i<(dstW>>1); i++){\
709 const int i2= 2*i;\
710 int Y1= buf0[i2 ]>>7;\
711 int Y2= buf0[i2+1]>>7;\
712 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
8b2fce0d 713 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
46de8b73
MN
714
715#define YSCALE_YUV_2_RGB1B_C(type) \
221b804f
DB
716 YSCALE_YUV_2_PACKED1B_C\
717 type *r, *b, *g;\
718 r = (type *)c->table_rV[V];\
719 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
720 b = (type *)c->table_bU[U];\
cf7d1c1a 721
ec1bca2a 722#define YSCALE_YUV_2_MONO2_C \
e69bd294
MN
723 const uint8_t * const d128=dither_8x8_220[y&7];\
724 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
725 for (i=0; i<dstW-7; i+=8){\
726 int acc;\
727 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
728 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
729 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
730 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
731 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
732 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
733 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
734 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
ec1bca2a 735 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
e69bd294
MN
736 dest++;\
737 }\
738
739
ec1bca2a 740#define YSCALE_YUV_2_MONOX_C \
e69bd294
MN
741 const uint8_t * const d128=dither_8x8_220[y&7];\
742 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
743 int acc=0;\
744 for (i=0; i<dstW-1; i+=2){\
745 int j;\
746 int Y1=1<<18;\
747 int Y2=1<<18;\
748\
749 for (j=0; j<lumFilterSize; j++)\
750 {\
751 Y1 += lumSrc[j][i] * lumFilter[j];\
752 Y2 += lumSrc[j][i+1] * lumFilter[j];\
753 }\
754 Y1>>=19;\
755 Y2>>=19;\
756 if ((Y1|Y2)&256)\
757 {\
758 if (Y1>255) Y1=255;\
759 else if (Y1<0)Y1=0;\
760 if (Y2>255) Y2=255;\
761 else if (Y2<0)Y2=0;\
762 }\
763 acc+= acc + g[Y1+d128[(i+0)&7]];\
764 acc+= acc + g[Y2+d128[(i+1)&7]];\
765 if ((i&7)==6){\
ec1bca2a 766 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
e69bd294
MN
767 dest++;\
768 }\
769 }
770
771
772#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
221b804f
DB
773 switch(c->dstFormat)\
774 {\
775 case PIX_FMT_RGB32:\
776 case PIX_FMT_BGR32:\
9990e426
MN
777 case PIX_FMT_RGB32_1:\
778 case PIX_FMT_BGR32_1:\
221b804f
DB
779 func(uint32_t)\
780 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
781 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
782 } \
783 break;\
784 case PIX_FMT_RGB24:\
785 func(uint8_t)\
786 ((uint8_t*)dest)[0]= r[Y1];\
787 ((uint8_t*)dest)[1]= g[Y1];\
788 ((uint8_t*)dest)[2]= b[Y1];\
789 ((uint8_t*)dest)[3]= r[Y2];\
790 ((uint8_t*)dest)[4]= g[Y2];\
791 ((uint8_t*)dest)[5]= b[Y2];\
792 dest+=6;\
793 }\
794 break;\
795 case PIX_FMT_BGR24:\
796 func(uint8_t)\
797 ((uint8_t*)dest)[0]= b[Y1];\
798 ((uint8_t*)dest)[1]= g[Y1];\
799 ((uint8_t*)dest)[2]= r[Y1];\
800 ((uint8_t*)dest)[3]= b[Y2];\
801 ((uint8_t*)dest)[4]= g[Y2];\
802 ((uint8_t*)dest)[5]= r[Y2];\
803 dest+=6;\
804 }\
805 break;\
806 case PIX_FMT_RGB565:\
807 case PIX_FMT_BGR565:\
808 {\
809 const int dr1= dither_2x2_8[y&1 ][0];\
810 const int dg1= dither_2x2_4[y&1 ][0];\
811 const int db1= dither_2x2_8[(y&1)^1][0];\
812 const int dr2= dither_2x2_8[y&1 ][1];\
813 const int dg2= dither_2x2_4[y&1 ][1];\
814 const int db2= dither_2x2_8[(y&1)^1][1];\
815 func(uint16_t)\
816 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
817 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
818 }\
819 }\
820 break;\
821 case PIX_FMT_RGB555:\
822 case PIX_FMT_BGR555:\
823 {\
824 const int dr1= dither_2x2_8[y&1 ][0];\
825 const int dg1= dither_2x2_8[y&1 ][1];\
826 const int db1= dither_2x2_8[(y&1)^1][0];\
827 const int dr2= dither_2x2_8[y&1 ][1];\
828 const int dg2= dither_2x2_8[y&1 ][0];\
829 const int db2= dither_2x2_8[(y&1)^1][1];\
830 func(uint16_t)\
831 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
832 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
833 }\
834 }\
835 break;\
836 case PIX_FMT_RGB8:\
837 case PIX_FMT_BGR8:\
838 {\
839 const uint8_t * const d64= dither_8x8_73[y&7];\
840 const uint8_t * const d32= dither_8x8_32[y&7];\
841 func(uint8_t)\
842 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
843 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
844 }\
845 }\
846 break;\
847 case PIX_FMT_RGB4:\
848 case PIX_FMT_BGR4:\
849 {\
850 const uint8_t * const d64= dither_8x8_73 [y&7];\
851 const uint8_t * const d128=dither_8x8_220[y&7];\
852 func(uint8_t)\
853 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
854 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
855 }\
856 }\
857 break;\
858 case PIX_FMT_RGB4_BYTE:\
859 case PIX_FMT_BGR4_BYTE:\
860 {\
861 const uint8_t * const d64= dither_8x8_73 [y&7];\
862 const uint8_t * const d128=dither_8x8_220[y&7];\
863 func(uint8_t)\
864 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
865 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
866 }\
867 }\
868 break;\
869 case PIX_FMT_MONOBLACK:\
ec1bca2a 870 case PIX_FMT_MONOWHITE:\
221b804f 871 {\
e69bd294 872 func_monoblack\
221b804f
DB
873 }\
874 break;\
875 case PIX_FMT_YUYV422:\
876 func2\
877 ((uint8_t*)dest)[2*i2+0]= Y1;\
878 ((uint8_t*)dest)[2*i2+1]= U;\
879 ((uint8_t*)dest)[2*i2+2]= Y2;\
880 ((uint8_t*)dest)[2*i2+3]= V;\
881 } \
882 break;\
883 case PIX_FMT_UYVY422:\
884 func2\
885 ((uint8_t*)dest)[2*i2+0]= U;\
886 ((uint8_t*)dest)[2*i2+1]= Y1;\
887 ((uint8_t*)dest)[2*i2+2]= V;\
888 ((uint8_t*)dest)[2*i2+3]= Y2;\
889 } \
890 break;\
b0880d5d
MN
891 case PIX_FMT_GRAY16BE:\
892 func_g16\
893 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
894 ((uint8_t*)dest)[2*i2+1]= Y1;\
895 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
896 ((uint8_t*)dest)[2*i2+3]= Y2;\
897 } \
898 break;\
899 case PIX_FMT_GRAY16LE:\
900 func_g16\
901 ((uint8_t*)dest)[2*i2+0]= Y1;\
902 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
903 ((uint8_t*)dest)[2*i2+2]= Y2;\
904 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
905 } \
906 break;\
221b804f 907 }\
cf7d1c1a
MN
908
909
25593e29 910static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
911 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
912 uint8_t *dest, int dstW, int y)
e3d2500f 913{
221b804f 914 int i;
ec1bca2a 915 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
e3d2500f
MN
916}
917
f0faee4c
MN
918static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
919 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
920 uint8_t *dest, int dstW, int y)
921{
922 int i;
923 int step= fmt_depth(c->dstFormat)/8;
d616c8ae 924 int aidx= 3;
f0faee4c
MN
925
926 switch(c->dstFormat){
927 case PIX_FMT_ARGB:
928 dest++;
a3398feb 929 aidx= 0;
f0faee4c 930 case PIX_FMT_RGB24:
d616c8ae 931 aidx--;
f0faee4c
MN
932 case PIX_FMT_RGBA:
933 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
f8a138be 934 dest[aidx]= 255;
f0faee4c
MN
935 dest[0]= R>>22;
936 dest[1]= G>>22;
937 dest[2]= B>>22;
f0faee4c
MN
938 dest+= step;
939 }
940 break;
941 case PIX_FMT_ABGR:
942 dest++;
a3398feb 943 aidx= 0;
f0faee4c 944 case PIX_FMT_BGR24:
d616c8ae 945 aidx--;
f0faee4c
MN
946 case PIX_FMT_BGRA:
947 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
f8a138be 948 dest[aidx]= 255;
f0faee4c
MN
949 dest[0]= B>>22;
950 dest[1]= G>>22;
951 dest[2]= R>>22;
f0faee4c
MN
952 dest+= step;
953 }
954 break;
955 default:
956 assert(0);
957 }
958}
e3d2500f 959
d4da3e47
CS
960static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
961 int i;
962 uint8_t *ptr = plane + stride*y;
963 for (i=0; i<height; i++){
964 memset(ptr, val, width);
965 ptr += stride;
966 }
967}
968
f40c7dbb 969//Note: we have C, X86, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
7630f2e0 970//Plain C versions
b63f641e 971#if !HAVE_MMX || defined (RUNTIME_CPUDETECT) || !CONFIG_GPL
726a959a
MN
972#define COMPILE_C
973#endif
974
b63f641e
AJ
975#if ARCH_PPC
976#if (HAVE_ALTIVEC || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
dfe44a85 977#undef COMPILE_C
a2faa401 978#define COMPILE_ALTIVEC
7a24ec50 979#endif
cb82a073 980#endif //ARCH_PPC
a2faa401 981
b63f641e 982#if ARCH_X86
726a959a 983
f4406ec1 984#if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
726a959a
MN
985#define COMPILE_MMX
986#endif
987
b63f641e 988#if (HAVE_MMX2 || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
726a959a
MN
989#define COMPILE_MMX2
990#endif
991
f4406ec1 992#if ((HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
726a959a
MN
993#define COMPILE_3DNOW
994#endif
7a24ec50 995#endif //ARCH_X86
726a959a
MN
996
997#undef HAVE_MMX
998#undef HAVE_MMX2
f4406ec1 999#undef HAVE_AMD3DNOW
b63f641e
AJ
1000#undef HAVE_ALTIVEC
1001#define HAVE_MMX 0
1002#define HAVE_MMX2 0
f4406ec1 1003#define HAVE_AMD3DNOW 0
b63f641e 1004#define HAVE_ALTIVEC 0
726a959a
MN
1005
1006#ifdef COMPILE_C
7630f2e0
MN
1007#define RENAME(a) a ## _C
1008#include "swscale_template.c"
726a959a 1009#endif
397c035e 1010
a2faa401
RD
1011#ifdef COMPILE_ALTIVEC
1012#undef RENAME
b63f641e
AJ
1013#undef HAVE_ALTIVEC
1014#define HAVE_ALTIVEC 1
a2faa401
RD
1015#define RENAME(a) a ## _altivec
1016#include "swscale_template.c"
1017#endif
a2faa401 1018
b63f641e 1019#if ARCH_X86
397c035e 1020
f40c7dbb 1021//x86 versions
7630f2e0
MN
1022/*
1023#undef RENAME
1024#undef HAVE_MMX
1025#undef HAVE_MMX2
f4406ec1 1026#undef HAVE_AMD3DNOW
7630f2e0
MN
1027#define ARCH_X86
1028#define RENAME(a) a ## _X86
1029#include "swscale_template.c"
1faf0867 1030*/
7630f2e0 1031//MMX versions
726a959a 1032#ifdef COMPILE_MMX
7630f2e0 1033#undef RENAME
b63f641e 1034#undef HAVE_MMX
7630f2e0 1035#undef HAVE_MMX2
f4406ec1 1036#undef HAVE_AMD3DNOW
b63f641e
AJ
1037#define HAVE_MMX 1
1038#define HAVE_MMX2 0
f4406ec1 1039#define HAVE_AMD3DNOW 0
7630f2e0
MN
1040#define RENAME(a) a ## _MMX
1041#include "swscale_template.c"
726a959a 1042#endif
7630f2e0
MN
1043
1044//MMX2 versions
726a959a 1045#ifdef COMPILE_MMX2
7630f2e0 1046#undef RENAME
b63f641e
AJ
1047#undef HAVE_MMX
1048#undef HAVE_MMX2
f4406ec1 1049#undef HAVE_AMD3DNOW
b63f641e
AJ
1050#define HAVE_MMX 1
1051#define HAVE_MMX2 1
f4406ec1 1052#define HAVE_AMD3DNOW 0
7630f2e0
MN
1053#define RENAME(a) a ## _MMX2
1054#include "swscale_template.c"
726a959a 1055#endif
7630f2e0
MN
1056
1057//3DNOW versions
726a959a 1058#ifdef COMPILE_3DNOW
7630f2e0 1059#undef RENAME
b63f641e 1060#undef HAVE_MMX
7630f2e0 1061#undef HAVE_MMX2
f4406ec1 1062#undef HAVE_AMD3DNOW
b63f641e
AJ
1063#define HAVE_MMX 1
1064#define HAVE_MMX2 0
f4406ec1 1065#define HAVE_AMD3DNOW 1
7630f2e0
MN
1066#define RENAME(a) a ## _3DNow
1067#include "swscale_template.c"
726a959a 1068#endif
7630f2e0 1069
7a24ec50 1070#endif //ARCH_X86
7630f2e0 1071
f40c7dbb 1072// minor note: the HAVE_xyz are messed up after this line so don't use them
d604bab9 1073
a86c461c
MN
1074static double getSplineCoeff(double a, double b, double c, double d, double dist)
1075{
221b804f
DB
1076// printf("%f %f %f %f %f\n", a,b,c,d,dist);
1077 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1078 else return getSplineCoeff( 0.0,
1079 b+ 2.0*c + 3.0*d,
1080 c + 3.0*d,
1081 -b- 3.0*c - 6.0*d,
1082 dist-1.0);
a86c461c 1083}
6c7506de 1084
bca11e75 1085static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
221b804f
DB
1086 int srcW, int dstW, int filterAlign, int one, int flags,
1087 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
28bf81c9 1088{
221b804f
DB
1089 int i;
1090 int filterSize;
1091 int filter2Size;
1092 int minFilterSize;
a64a062f
MN
1093 int64_t *filter=NULL;
1094 int64_t *filter2=NULL;
1095 const int64_t fone= 1LL<<54;
091d3bdc 1096 int ret= -1;
b63f641e 1097#if ARCH_X86
221b804f 1098 if (flags & SWS_CPU_CAPS_MMX)
7ad6469e 1099 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
726a959a 1100#endif
31190492 1101
f40c7dbb 1102 // NOTE: the +1 is for the MMX scaler which reads over the end
221b804f
DB
1103 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1104
1105 if (FFABS(xInc - 0x10000) <10) // unscaled
1106 {
1107 int i;
1108 filterSize= 1;
8588e148 1109 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
221b804f
DB
1110
1111 for (i=0; i<dstW; i++)
1112 {
a64a062f 1113 filter[i*filterSize]= fone;
221b804f
DB
1114 (*filterPos)[i]=i;
1115 }
1116
1117 }
1118 else if (flags&SWS_POINT) // lame looking point sampling mode
1119 {
1120 int i;
1121 int xDstInSrc;
1122 filterSize= 1;
8588e148 1123 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
221b804f
DB
1124
1125 xDstInSrc= xInc/2 - 0x8000;
1126 for (i=0; i<dstW; i++)
1127 {
1128 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1129
1130 (*filterPos)[i]= xx;
a64a062f 1131 filter[i]= fone;
221b804f
DB
1132 xDstInSrc+= xInc;
1133 }
1134 }
1135 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1136 {
1137 int i;
1138 int xDstInSrc;
1139 if (flags&SWS_BICUBIC) filterSize= 4;
1140 else if (flags&SWS_X ) filterSize= 4;
1141 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
8588e148 1142 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
221b804f
DB
1143
1144 xDstInSrc= xInc/2 - 0x8000;
1145 for (i=0; i<dstW; i++)
1146 {
1147 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1148 int j;
1149
1150 (*filterPos)[i]= xx;
f40c7dbb 1151 //bilinear upscale / linear interpolate / area averaging
221b804f
DB
1152 for (j=0; j<filterSize; j++)
1153 {
a64a062f 1154 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
221b804f
DB
1155 if (coeff<0) coeff=0;
1156 filter[i*filterSize + j]= coeff;
1157 xx++;
1158 }
1159 xDstInSrc+= xInc;
1160 }
1161 }
1162 else
1163 {
a64a062f
MN
1164 int xDstInSrc;
1165 int sizeFactor;
1166
1167 if (flags&SWS_BICUBIC) sizeFactor= 4;
1168 else if (flags&SWS_X) sizeFactor= 8;
1169 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1170 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1171 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1172 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1173 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1174 else if (flags&SWS_BILINEAR) sizeFactor= 2;
221b804f 1175 else {
a64a062f 1176 sizeFactor= 0; //GCC warning killer
fcc402b1 1177 assert(0);
221b804f
DB
1178 }
1179
a64a062f
MN
1180 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1181 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
221b804f 1182
221b804f
DB
1183 if (filterSize > srcW-2) filterSize=srcW-2;
1184
8588e148 1185 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
221b804f 1186
a64a062f 1187 xDstInSrc= xInc - 0x10000;
221b804f
DB
1188 for (i=0; i<dstW; i++)
1189 {
a64a062f 1190 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
221b804f
DB
1191 int j;
1192 (*filterPos)[i]= xx;
1193 for (j=0; j<filterSize; j++)
1194 {
a64a062f
MN
1195 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1196 double floatd;
1197 int64_t coeff;
1198
1199 if (xInc > 1<<16)
1200 d= d*dstW/srcW;
1201 floatd= d * (1.0/(1<<30));
1202
221b804f
DB
1203 if (flags & SWS_BICUBIC)
1204 {
a64a062f
MN
1205 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1206 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1207 int64_t dd = ( d*d)>>30;
1208 int64_t ddd= (dd*d)>>30;
1209
1210 if (d < 1LL<<30)
1211 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1212 else if (d < 1LL<<31)
1213 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
221b804f
DB
1214 else
1215 coeff=0.0;
a64a062f 1216 coeff *= fone>>(30+24);
221b804f
DB
1217 }
1218/* else if (flags & SWS_X)
1219 {
1220 double p= param ? param*0.01 : 0.3;
1221 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1222 coeff*= pow(2.0, - p*d*d);
1223 }*/
1224 else if (flags & SWS_X)
1225 {
1226 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
a64a062f 1227 double c;
221b804f 1228
a64a062f
MN
1229 if (floatd<1.0)
1230 c = cos(floatd*PI);
221b804f 1231 else
a64a062f
MN
1232 c=-1.0;
1233 if (c<0.0) c= -pow(-c, A);
1234 else c= pow( c, A);
1235 coeff= (c*0.5 + 0.5)*fone;
221b804f
DB
1236 }
1237 else if (flags & SWS_AREA)
1238 {
a64a062f
MN
1239 int64_t d2= d - (1<<29);
1240 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1241 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
221b804f 1242 else coeff=0.0;
a64a062f 1243 coeff *= fone>>(30+16);
221b804f
DB
1244 }
1245 else if (flags & SWS_GAUSS)
1246 {
1247 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
a64a062f 1248 coeff = (pow(2.0, - p*floatd*floatd))*fone;
221b804f
DB
1249 }
1250 else if (flags & SWS_SINC)
1251 {
a64a062f 1252 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
221b804f
DB
1253 }
1254 else if (flags & SWS_LANCZOS)
1255 {
1256 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
a64a062f
MN
1257 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1258 if (floatd>p) coeff=0;
221b804f
DB
1259 }
1260 else if (flags & SWS_BILINEAR)
1261 {
a64a062f 1262 coeff= (1<<30) - d;
221b804f 1263 if (coeff<0) coeff=0;
a64a062f 1264 coeff *= fone >> 30;
221b804f
DB
1265 }
1266 else if (flags & SWS_SPLINE)
1267 {
1268 double p=-2.196152422706632;
f830d824 1269 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
221b804f
DB
1270 }
1271 else {
1272 coeff= 0.0; //GCC warning killer
fcc402b1 1273 assert(0);
221b804f
DB
1274 }
1275
1276 filter[i*filterSize + j]= coeff;
1277 xx++;
1278 }
a64a062f 1279 xDstInSrc+= 2*xInc;
221b804f
DB
1280 }
1281 }
1282
1283 /* apply src & dst Filter to filter -> filter2
1284 av_free(filter);
1285 */
fcc402b1 1286 assert(filterSize>0);
221b804f
DB
1287 filter2Size= filterSize;
1288 if (srcFilter) filter2Size+= srcFilter->length - 1;
1289 if (dstFilter) filter2Size+= dstFilter->length - 1;
fcc402b1 1290 assert(filter2Size>0);
a64a062f 1291 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
221b804f
DB
1292
1293 for (i=0; i<dstW; i++)
1294 {
a64a062f 1295 int j, k;
221b804f 1296
a64a062f
MN
1297 if(srcFilter){
1298 for (k=0; k<srcFilter->length; k++){
1299 for (j=0; j<filterSize; j++)
1300 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1301 }
1302 }else{
1303 for (j=0; j<filterSize; j++)
1304 filter2[i*filter2Size + j]= filter[i*filterSize + j];
221b804f 1305 }
a64a062f 1306 //FIXME dstFilter
221b804f
DB
1307
1308 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
221b804f 1309 }
47b7382d 1310 av_freep(&filter);
221b804f
DB
1311
1312 /* try to reduce the filter-size (step1 find size and shift left) */
86bdf3fd 1313 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
221b804f
DB
1314 minFilterSize= 0;
1315 for (i=dstW-1; i>=0; i--)
1316 {
1317 int min= filter2Size;
1318 int j;
a64a062f 1319 int64_t cutOff=0.0;
221b804f
DB
1320
1321 /* get rid off near zero elements on the left by shifting left */
1322 for (j=0; j<filter2Size; j++)
1323 {
1324 int k;
1325 cutOff += FFABS(filter2[i*filter2Size]);
1326
a64a062f 1327 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
221b804f 1328
86bdf3fd 1329 /* preserve monotonicity because the core can't handle the filter otherwise */
221b804f
DB
1330 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1331
f40c7dbb 1332 // move filter coefficients left
221b804f
DB
1333 for (k=1; k<filter2Size; k++)
1334 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
a64a062f 1335 filter2[i*filter2Size + k - 1]= 0;
221b804f
DB
1336 (*filterPos)[i]++;
1337 }
1338
a64a062f 1339 cutOff=0;
221b804f
DB
1340 /* count near zeros on the right */
1341 for (j=filter2Size-1; j>0; j--)
1342 {
1343 cutOff += FFABS(filter2[i*filter2Size + j]);
1344
a64a062f 1345 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
221b804f
DB
1346 min--;
1347 }
1348
1349 if (min>minFilterSize) minFilterSize= min;
1350 }
1351
1352 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1353 // we can handle the special case 4,
1354 // so we don't want to go to the full 8
1355 if (minFilterSize < 5)
8c266f0c
RD
1356 filterAlign = 4;
1357
f40c7dbb
DB
1358 // We really don't want to waste our time
1359 // doing useless computation, so fall back on
1360 // the scalar C code for very small filters.
1361 // Vectorizing is worth it only if you have a
221b804f
DB
1362 // decent-sized vector.
1363 if (minFilterSize < 3)
8c266f0c 1364 filterAlign = 1;
221b804f
DB
1365 }
1366
1367 if (flags & SWS_CPU_CAPS_MMX) {
1368 // special case for unscaled vertical filtering
1369 if (minFilterSize == 1 && filterAlign == 2)
1370 filterAlign= 1;
1371 }
1372
fcc402b1 1373 assert(minFilterSize > 0);
221b804f 1374 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
fcc402b1 1375 assert(filterSize > 0);
8588e148 1376 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1625216e 1377 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
091d3bdc 1378 goto error;
221b804f
DB
1379 *outFilterSize= filterSize;
1380
1381 if (flags&SWS_PRINT_INFO)
1382 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1383 /* try to reduce the filter-size (step2 reduce it) */
1384 for (i=0; i<dstW; i++)
1385 {
1386 int j;
1387
1388 for (j=0; j<filterSize; j++)
1389 {
a64a062f 1390 if (j>=filter2Size) filter[i*filterSize + j]= 0;
221b804f 1391 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
88bc5a64 1392 if((flags & SWS_BITEXACT) && j>=minFilterSize)
a64a062f 1393 filter[i*filterSize + j]= 0;
8c266f0c 1394 }
221b804f 1395 }
221b804f
DB
1396
1397
f40c7dbb 1398 //FIXME try to align filterPos if possible
8c266f0c 1399
221b804f
DB
1400 //fix borders
1401 for (i=0; i<dstW; i++)
1402 {
1403 int j;
1404 if ((*filterPos)[i] < 0)
1405 {
f40c7dbb 1406 // move filter coefficients left to compensate for filterPos
221b804f
DB
1407 for (j=1; j<filterSize; j++)
1408 {
1409 int left= FFMAX(j + (*filterPos)[i], 0);
1410 filter[i*filterSize + left] += filter[i*filterSize + j];
1411 filter[i*filterSize + j]=0;
1412 }
1413 (*filterPos)[i]= 0;
bca11e75
MN
1414 }
1415
221b804f
DB
1416 if ((*filterPos)[i] + filterSize > srcW)
1417 {
1418 int shift= (*filterPos)[i] + filterSize - srcW;
f40c7dbb 1419 // move filter coefficients right to compensate for filterPos
221b804f
DB
1420 for (j=filterSize-2; j>=0; j--)
1421 {
1422 int right= FFMIN(j + shift, filterSize-1);
1423 filter[i*filterSize +right] += filter[i*filterSize +j];
1424 filter[i*filterSize +j]=0;
1425 }
1426 (*filterPos)[i]= srcW - filterSize;
1427 }
1428 }
1429
f40c7dbb 1430 // Note the +1 is for the MMX scaler which reads over the end
221b804f
DB
1431 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1432 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1433
f40c7dbb 1434 /* normalize & store in outFilter */
221b804f
DB
1435 for (i=0; i<dstW; i++)
1436 {
1437 int j;
a64a062f
MN
1438 int64_t error=0;
1439 int64_t sum=0;
221b804f
DB
1440
1441 for (j=0; j<filterSize; j++)
1442 {
1443 sum+= filter[i*filterSize + j];
1444 }
a64a062f 1445 sum= (sum + one/2)/ one;
221b804f
DB
1446 for (j=0; j<*outFilterSize; j++)
1447 {
a64a062f
MN
1448 int64_t v= filter[i*filterSize + j] + error;
1449 int intV= ROUNDED_DIV(v, sum);
221b804f 1450 (*outFilter)[i*(*outFilterSize) + j]= intV;
a64a062f 1451 error= v - intV*sum;
221b804f
DB
1452 }
1453 }
1454
1455 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1456 for (i=0; i<*outFilterSize; i++)
1457 {
1458 int j= dstW*(*outFilterSize);
1459 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1460 }
1461
091d3bdc
MN
1462 ret=0;
1463error:
221b804f 1464 av_free(filter);
091d3bdc
MN
1465 av_free(filter2);
1466 return ret;
7630f2e0 1467}
31190492 1468
17c613ef 1469#ifdef COMPILE_MMX2
b7dc6f66 1470static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
28bf81c9 1471{
221b804f 1472 uint8_t *fragmentA;
d0ce212a
RP
1473 x86_reg imm8OfPShufW1A;
1474 x86_reg imm8OfPShufW2A;
1475 x86_reg fragmentLengthA;
221b804f 1476 uint8_t *fragmentB;
d0ce212a
RP
1477 x86_reg imm8OfPShufW1B;
1478 x86_reg imm8OfPShufW2B;
1479 x86_reg fragmentLengthB;
221b804f
DB
1480 int fragmentPos;
1481
1482 int xpos, i;
1483
1484 // create an optimized horizontal scaling routine
1485
1486 //code fragment
1487
7ad6469e 1488 __asm__ volatile(
221b804f
DB
1489 "jmp 9f \n\t"
1490 // Begin
1491 "0: \n\t"
1492 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1493 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1494 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1495 "punpcklbw %%mm7, %%mm1 \n\t"
1496 "punpcklbw %%mm7, %%mm0 \n\t"
1497 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1498 "1: \n\t"
1499 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1500 "2: \n\t"
1501 "psubw %%mm1, %%mm0 \n\t"
1502 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1503 "pmullw %%mm3, %%mm0 \n\t"
1504 "psllw $7, %%mm1 \n\t"
1505 "paddw %%mm1, %%mm0 \n\t"
1506
1507 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1508
1509 "add $8, %%"REG_a" \n\t"
1510 // End
1511 "9: \n\t"
1512// "int $3 \n\t"
86593486
AS
1513 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1514 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1515 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
221b804f
DB
1516 "dec %1 \n\t"
1517 "dec %2 \n\t"
1518 "sub %0, %1 \n\t"
1519 "sub %0, %2 \n\t"
86593486 1520 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
221b804f
DB
1521 "sub %0, %3 \n\t"
1522
1523
1524 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1525 "=r" (fragmentLengthA)
1526 );
1527
7ad6469e 1528 __asm__ volatile(
221b804f
DB
1529 "jmp 9f \n\t"
1530 // Begin
1531 "0: \n\t"
1532 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1533 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1534 "punpcklbw %%mm7, %%mm0 \n\t"
1535 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1536 "1: \n\t"
1537 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1538 "2: \n\t"
1539 "psubw %%mm1, %%mm0 \n\t"
1540 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1541 "pmullw %%mm3, %%mm0 \n\t"
1542 "psllw $7, %%mm1 \n\t"
1543 "paddw %%mm1, %%mm0 \n\t"
1544
1545 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1546
1547 "add $8, %%"REG_a" \n\t"
1548 // End
1549 "9: \n\t"
1550// "int $3 \n\t"
86593486
AS
1551 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1552 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1553 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
221b804f
DB
1554 "dec %1 \n\t"
1555 "dec %2 \n\t"
1556 "sub %0, %1 \n\t"
1557 "sub %0, %2 \n\t"
86593486 1558 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
221b804f
DB
1559 "sub %0, %3 \n\t"
1560
1561
1562 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1563 "=r" (fragmentLengthB)
1564 );
1565
1566 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1567 fragmentPos=0;
1568
1569 for (i=0; i<dstW/numSplits; i++)
1570 {
1571 int xx=xpos>>16;
1572
1573 if ((i&3) == 0)
1574 {
1575 int a=0;
1576 int b=((xpos+xInc)>>16) - xx;
1577 int c=((xpos+xInc*2)>>16) - xx;
1578 int d=((xpos+xInc*3)>>16) - xx;
1579
1580 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1581 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1582 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1583 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1584 filterPos[i/2]= xx;
1585
1586 if (d+1<4)
1587 {
1588 int maxShift= 3-(d+1);
1589 int shift=0;
1590
1591 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1592
1593 funnyCode[fragmentPos + imm8OfPShufW1B]=
1594 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1595 funnyCode[fragmentPos + imm8OfPShufW2B]=
1596 a | (b<<2) | (c<<4) | (d<<6);
1597
1598 if (i+3>=dstW) shift=maxShift; //avoid overread
1599 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1600
1601 if (shift && i>=shift)
1602 {
1603 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1604 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1605 filterPos[i/2]-=shift;
1606 }
1607
1608 fragmentPos+= fragmentLengthB;
1609 }
1610 else
1611 {
1612 int maxShift= 3-d;
1613 int shift=0;
1614
1615 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1616
1617 funnyCode[fragmentPos + imm8OfPShufW1A]=
1618 funnyCode[fragmentPos + imm8OfPShufW2A]=
1619 a | (b<<2) | (c<<4) | (d<<6);
1620
1621 if (i+4>=dstW) shift=maxShift; //avoid overread
1622 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1623
1624 if (shift && i>=shift)
1625 {
1626 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1627 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1628 filterPos[i/2]-=shift;
1629 }
1630
1631 fragmentPos+= fragmentLengthA;
1632 }
1633
1634 funnyCode[fragmentPos]= RET;
1635 }
1636 xpos+=xInc;
1637 }
c662e788 1638 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
28bf81c9 1639}
17c613ef 1640#endif /* COMPILE_MMX2 */
28bf81c9 1641
9b2283cc 1642static void globalInit(void){
31190492
A
1643 // generating tables:
1644 int i;
221b804f
DB
1645 for (i=0; i<768; i++){
1646 int c= av_clip_uint8(i-256);
1647 clip_table[i]=c;
b18ea156 1648 }
516b1f82 1649}
c1b0bfb4 1650
516b1f82 1651static SwsFunc getSwsFunc(int flags){
6a4970ab 1652
b63f641e
AJ
1653#if defined(RUNTIME_CPUDETECT) && CONFIG_GPL
1654#if ARCH_X86
c14731d8 1655 // ordered per speed fastest first
221b804f
DB
1656 if (flags & SWS_CPU_CAPS_MMX2)
1657 return swScale_MMX2;
1658 else if (flags & SWS_CPU_CAPS_3DNOW)
1659 return swScale_3DNow;
1660 else if (flags & SWS_CPU_CAPS_MMX)
1661 return swScale_MMX;
1662 else
1663 return swScale_C;
28bf81c9
MN
1664
1665#else
b63f641e 1666#if ARCH_PPC
221b804f
DB
1667 if (flags & SWS_CPU_CAPS_ALTIVEC)
1668 return swScale_altivec;
1669 else
1670 return swScale_C;
a2faa401 1671#endif
221b804f 1672 return swScale_C;
b63f641e 1673#endif /* ARCH_X86 */
28bf81c9 1674#else //RUNTIME_CPUDETECT
b63f641e 1675#if HAVE_MMX2
221b804f 1676 return swScale_MMX2;
f4406ec1 1677#elif HAVE_AMD3DNOW
221b804f 1678 return swScale_3DNow;
b63f641e 1679#elif HAVE_MMX
221b804f 1680 return swScale_MMX;
b63f641e 1681#elif HAVE_ALTIVEC
221b804f 1682 return swScale_altivec;
28bf81c9 1683#else
221b804f 1684 return swScale_C;
28bf81c9
MN
1685#endif
1686#endif //!RUNTIME_CPUDETECT
31190492 1687}
7630f2e0 1688
d4e24275 1689static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1690 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1691 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1692 /* Copy Y plane */
1693 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1694 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1695 else
1696 {
1697 int i;
1698 uint8_t *srcPtr= src[0];
1699 uint8_t *dstPtr= dst;
1700 for (i=0; i<srcSliceH; i++)
1701 {
1702 memcpy(dstPtr, srcPtr, c->srcW);
1703 srcPtr+= srcStride[0];
1704 dstPtr+= dstStride[0];
1705 }
1706 }
1707 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1708 if (c->dstFormat == PIX_FMT_NV12)
30c48a0a 1709 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
221b804f 1710 else
30c48a0a 1711 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
221b804f
DB
1712
1713 return srcSliceH;
0d9f3d85
A
1714}
1715
d4e24275 1716static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1717 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1718 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
0d9f3d85 1719
30c48a0a 1720 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
fccb9b2b 1721
221b804f 1722 return srcSliceH;
0d9f3d85
A
1723}
1724
caeaabe7 1725static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1726 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1727 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
caeaabe7 1728
30c48a0a 1729 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
caeaabe7 1730
221b804f 1731 return srcSliceH;
caeaabe7
AB
1732}
1733
a6100f39
BC
1734static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1735 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1736 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1737
1738 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1739
1740 return srcSliceH;
1741}
1742
1743static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1744 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1745 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1746
1747 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1748
1749 return srcSliceH;
1750}
1751
0411072e
MN
1752static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1753 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1754 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1755 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1756 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1757
1758 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1759
1760 return srcSliceH;
1761}
1762
1763static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1764 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1765 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
72ef3dd7
MN
1766 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1767 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
0411072e
MN
1768
1769 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1770
1771 return srcSliceH;
1772}
1773
1774static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1775 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1776 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1777 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1778 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1779
1780 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1781
1782 return srcSliceH;
1783}
1784
1785static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1786 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1787 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
72ef3dd7
MN
1788 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1789 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
0411072e
MN
1790
1791 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1792
1793 return srcSliceH;
1794}
1795
49004617
VS
1796static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1797 int srcSliceH, uint8_t* dst[], int dstStride[]){
f5a2c981
CEH
1798 const enum PixelFormat srcFormat= c->srcFormat;
1799 const enum PixelFormat dstFormat= c->dstFormat;
49004617
VS
1800 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1801 const uint8_t *palette)=NULL;
1802 int i;
1803 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1804 uint8_t *srcPtr= src[0];
1805
1806 if (!usePal(srcFormat))
1807 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1808 sws_format_name(srcFormat), sws_format_name(dstFormat));
1809
1810 switch(dstFormat){
522ce957
VS
1811 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1812 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1813 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1814 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1815 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1816 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
49004617
VS
1817 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1818 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1819 }
1820
1821
1822 for (i=0; i<srcSliceH; i++) {
65f65c30 1823 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
49004617
VS
1824 srcPtr+= srcStride[0];
1825 dstPtr+= dstStride[0];
1826 }
1827
1828 return srcSliceH;
1829}
1830
9990e426 1831/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
d4e24275 1832static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f 1833 int srcSliceH, uint8_t* dst[], int dstStride[]){
58e4b706
CEH
1834 const enum PixelFormat srcFormat= c->srcFormat;
1835 const enum PixelFormat dstFormat= c->dstFormat;
221b804f
DB
1836 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1837 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1838 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1839 const int dstId= fmt_depth(dstFormat) >> 2;
1840 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1841
1842 /* BGR -> BGR */
1843 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1844 || (isRGB(srcFormat) && isRGB(dstFormat))){
1845 switch(srcId | (dstId<<4)){
1846 case 0x34: conv= rgb16to15; break;
1847 case 0x36: conv= rgb24to15; break;
1848 case 0x38: conv= rgb32to15; break;
1849 case 0x43: conv= rgb15to16; break;
1850 case 0x46: conv= rgb24to16; break;
1851 case 0x48: conv= rgb32to16; break;
1852 case 0x63: conv= rgb15to24; break;
1853 case 0x64: conv= rgb16to24; break;
1854 case 0x68: conv= rgb32to24; break;
1855 case 0x83: conv= rgb15to32; break;
1856 case 0x84: conv= rgb16to32; break;
1857 case 0x86: conv= rgb24to32; break;
3f0bc115 1858 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1859 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1860 }
1861 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1862 || (isRGB(srcFormat) && isBGR(dstFormat))){
1863 switch(srcId | (dstId<<4)){
1864 case 0x33: conv= rgb15tobgr15; break;
1865 case 0x34: conv= rgb16tobgr15; break;
1866 case 0x36: conv= rgb24tobgr15; break;
1867 case 0x38: conv= rgb32tobgr15; break;
1868 case 0x43: conv= rgb15tobgr16; break;
1869 case 0x44: conv= rgb16tobgr16; break;
1870 case 0x46: conv= rgb24tobgr16; break;
1871 case 0x48: conv= rgb32tobgr16; break;
1872 case 0x63: conv= rgb15tobgr24; break;
1873 case 0x64: conv= rgb16tobgr24; break;
1874 case 0x66: conv= rgb24tobgr24; break;
1875 case 0x68: conv= rgb32tobgr24; break;
1876 case 0x83: conv= rgb15tobgr32; break;
1877 case 0x84: conv= rgb16tobgr32; break;
1878 case 0x86: conv= rgb24tobgr32; break;
1879 case 0x88: conv= rgb32tobgr32; break;
3f0bc115 1880 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1881 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1882 }
1883 }else{
3f0bc115 1884 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1885 sws_format_name(srcFormat), sws_format_name(dstFormat));
1886 }
1887
068b0f4f
BF
1888 if(conv)
1889 {
9990e426
MN
1890 uint8_t *srcPtr= src[0];
1891 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1892 srcPtr += ALT32_CORR;
1893
5efaf000 1894 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
9990e426 1895 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
c4ca31d0 1896 else
221b804f 1897 {
c4ca31d0 1898 int i;
c4ca31d0
BF
1899 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1900
1901 for (i=0; i<srcSliceH; i++)
1902 {
1903 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1904 srcPtr+= srcStride[0];
1905 dstPtr+= dstStride[0];
1906 }
221b804f
DB
1907 }
1908 }
1909 return srcSliceH;
0d9f3d85
A
1910}
1911
d4e24275 1912static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1913 int srcSliceH, uint8_t* dst[], int dstStride[]){
1914
1915 rgb24toyv12(
1916 src[0],
1917 dst[0]+ srcSliceY *dstStride[0],
1918 dst[1]+(srcSliceY>>1)*dstStride[1],
1919 dst[2]+(srcSliceY>>1)*dstStride[2],
1920 c->srcW, srcSliceH,
1921 dstStride[0], dstStride[1], srcStride[0]);
1922 return srcSliceH;
ec22603f
MN
1923}
1924
d4e24275 1925static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1926 int srcSliceH, uint8_t* dst[], int dstStride[]){
1927 int i;
1928
1929 /* copy Y */
1930 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1931 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1932 else{
1933 uint8_t *srcPtr= src[0];
1934 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1935
1936 for (i=0; i<srcSliceH; i++)
1937 {
1938 memcpy(dstPtr, srcPtr, c->srcW);
1939 srcPtr+= srcStride[0];
1940 dstPtr+= dstStride[0];
1941 }
1942 }
1943
1944 if (c->dstFormat==PIX_FMT_YUV420P){
1945 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1946 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1947 }else{
1948 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1949 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1950 }
1951 return srcSliceH;
b241cbf2
MN
1952}
1953
b6654a54 1954/* unscaled copy like stuff (assumes nearly identical formats) */
2d35ae56 1955static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
bc5a0444
LB
1956 int srcSliceH, uint8_t* dst[], int dstStride[])
1957{
1958 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1959 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1960 else
1961 {
1962 int i;
1963 uint8_t *srcPtr= src[0];
1964 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1965 int length=0;
221b804f 1966
bc5a0444
LB
1967 /* universal length finder */
1968 while(length+c->srcW <= FFABS(dstStride[0])
1969 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
fcc402b1 1970 assert(length!=0);
2d35ae56 1971
bc5a0444 1972 for (i=0; i<srcSliceH; i++)
221b804f 1973 {
bc5a0444
LB
1974 memcpy(dstPtr, srcPtr, length);
1975 srcPtr+= srcStride[0];
1976 dstPtr+= dstStride[0];
221b804f 1977 }
bc5a0444 1978 }
2d35ae56
LB
1979 return srcSliceH;
1980}
bc5a0444 1981
2d35ae56
LB
1982static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1983 int srcSliceH, uint8_t* dst[], int dstStride[])
1984{
bc5a0444
LB
1985 int plane;
1986 for (plane=0; plane<3; plane++)
1987 {
1988 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1989 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1990 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2d35ae56 1991
bc5a0444 1992 if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
221b804f 1993 {
d4da3e47
CS
1994 if (!isGray(c->dstFormat))
1995 fillPlane(dst[plane], dstStride[plane], length, height, y, 128);
bc5a0444
LB
1996 }
1997 else
1998 {
1999 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2000 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
221b804f
DB
2001 else
2002 {
bc5a0444
LB
2003 int i;
2004 uint8_t *srcPtr= src[plane];
2005 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2006 for (i=0; i<height; i++)
221b804f 2007 {
bc5a0444
LB
2008 memcpy(dstPtr, srcPtr, length);
2009 srcPtr+= srcStride[plane];
2010 dstPtr+= dstStride[plane];
221b804f
DB
2011 }
2012 }
2013 }
bc5a0444 2014 }
221b804f 2015 return srcSliceH;
37079906 2016}
28bf81c9 2017
4884b9e5 2018static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2019 int srcSliceH, uint8_t* dst[], int dstStride[]){
2020
2021 int length= c->srcW;
2022 int y= srcSliceY;
2023 int height= srcSliceH;
2024 int i, j;
2025 uint8_t *srcPtr= src[0];
2026 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2027
2028 if (!isGray(c->dstFormat)){
2029 int height= -((-srcSliceH)>>c->chrDstVSubSample);
2030 memset(dst[1], 128, dstStride[1]*height);
2031 memset(dst[2], 128, dstStride[2]*height);
2032 }
2033 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
2034 for (i=0; i<height; i++)
2035 {
2036 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2037 srcPtr+= srcStride[0];
2038 dstPtr+= dstStride[0];
2039 }
2040 return srcSliceH;
4884b9e5
KS
2041}
2042
2043static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2044 int srcSliceH, uint8_t* dst[], int dstStride[]){
2045
2046 int length= c->srcW;
2047 int y= srcSliceY;
2048 int height= srcSliceH;
2049 int i, j;
2050 uint8_t *srcPtr= src[0];
2051 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2052 for (i=0; i<height; i++)
2053 {
2054 for (j=0; j<length; j++)
2055 {
2056 dstPtr[j<<1] = srcPtr[j];
2057 dstPtr[(j<<1)+1] = srcPtr[j];
2058 }
2059 srcPtr+= srcStride[0];
2060 dstPtr+= dstStride[0];
2061 }
2062 return srcSliceH;
4884b9e5
KS
2063}
2064
2065static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2066 int srcSliceH, uint8_t* dst[], int dstStride[]){
2067
2068 int length= c->srcW;
2069 int y= srcSliceY;
2070 int height= srcSliceH;
2071 int i, j;
73d046e2 2072 uint16_t *srcPtr= (uint16_t*)src[0];
b8b015f4 2073 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
221b804f
DB
2074 for (i=0; i<height; i++)
2075 {
2076 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
2077 srcPtr+= srcStride[0]/2;
2078 dstPtr+= dstStride[0]/2;
2079 }
2080 return srcSliceH;
4884b9e5
KS
2081}
2082
2083
c7a810cc 2084static void getSubSampleFactors(int *h, int *v, int format){
221b804f
DB
2085 switch(format){
2086 case PIX_FMT_UYVY422:
2087 case PIX_FMT_YUYV422:
2088 *h=1;
2089 *v=0;
2090 break;
2091 case PIX_FMT_YUV420P:
79973335 2092 case PIX_FMT_YUVA420P:
221b804f
DB
2093 case PIX_FMT_GRAY16BE:
2094 case PIX_FMT_GRAY16LE:
2095 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2096 case PIX_FMT_NV12:
2097 case PIX_FMT_NV21:
2098 *h=1;
2099 *v=1;
2100 break;
9ba7fe6d
2101 case PIX_FMT_YUV440P:
2102 *h=0;
2103 *v=1;
2104 break;
221b804f
DB
2105 case PIX_FMT_YUV410P:
2106 *h=2;
2107 *v=2;
2108 break;
2109 case PIX_FMT_YUV444P:
2110 *h=0;
2111 *v=0;
2112 break;
2113 case PIX_FMT_YUV422P:
2114 *h=1;
2115 *v=0;
2116 break;
2117 case PIX_FMT_YUV411P:
2118 *h=2;
2119 *v=0;
2120 break;
2121 default:
2122 *h=0;
2123 *v=0;
2124 break;
2125 }
c7a810cc
MN
2126}
2127
5427e242 2128static uint16_t roundToInt16(int64_t f){
221b804f
DB
2129 int r= (f + (1<<15))>>16;
2130 if (r<-0x7FFF) return 0x8000;
2131 else if (r> 0x7FFF) return 0x7FFF;
2132 else return r;
0481412a
MN
2133}
2134
2135/**
fa58ba15 2136 * @param inv_table the yuv2rgb coefficients, normally ff_yuv2rgb_coeffs[x]
86bdf3fd 2137 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
5427e242 2138 * @return -1 if not supported
0481412a 2139 */
5427e242 2140int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
221b804f
DB
2141 int64_t crv = inv_table[0];
2142 int64_t cbu = inv_table[1];
2143 int64_t cgu = -inv_table[2];
2144 int64_t cgv = -inv_table[3];
2145 int64_t cy = 1<<16;
2146 int64_t oy = 0;
2147
221b804f
DB
2148 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2149 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2150
2151 c->brightness= brightness;
2152 c->contrast = contrast;
2153 c->saturation= saturation;
2154 c->srcRange = srcRange;
2155 c->dstRange = dstRange;
6bc0c792 2156 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
221b804f
DB
2157
2158 c->uOffset= 0x0400040004000400LL;
2159 c->vOffset= 0x0400040004000400LL;
2160
2161 if (!srcRange){
2162 cy= (cy*255) / 219;
2163 oy= 16<<16;
2164 }else{
2165 crv= (crv*224) / 255;
2166 cbu= (cbu*224) / 255;
2167 cgu= (cgu*224) / 255;
2168 cgv= (cgv*224) / 255;
2169 }
0481412a 2170
221b804f
DB
2171 cy = (cy *contrast )>>16;
2172 crv= (crv*contrast * saturation)>>32;
2173 cbu= (cbu*contrast * saturation)>>32;
2174 cgu= (cgu*contrast * saturation)>>32;
2175 cgv= (cgv*contrast * saturation)>>32;
0481412a 2176
221b804f 2177 oy -= 256*brightness;
0481412a 2178
221b804f
DB
2179 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2180 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2181 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2182 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2183 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2184 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
5427e242 2185
43c16478
MN
2186 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2187 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2188 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2189 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2190 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2191 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
f0faee4c 2192
780daf2b 2193 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
221b804f 2194 //FIXME factorize
a31de956 2195
6634d0ef 2196#ifdef COMPILE_ALTIVEC
221b804f 2197 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
780daf2b 2198 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
6a4970ab 2199#endif
221b804f 2200 return 0;
5427e242
MN
2201}
2202
2203/**
2204 * @return -1 if not supported
2205 */
2206int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
221b804f 2207 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
5427e242 2208
221b804f
DB
2209 *inv_table = c->srcColorspaceTable;
2210 *table = c->dstColorspaceTable;
2211 *srcRange = c->srcRange;
2212 *dstRange = c->dstRange;
2213 *brightness= c->brightness;
2214 *contrast = c->contrast;
2215 *saturation= c->saturation;
6a4970ab 2216
221b804f 2217 return 0;
0481412a
MN
2218}
2219
13394e8c 2220static int handle_jpeg(enum PixelFormat *format)
44cdb423 2221{
221b804f
DB
2222 switch (*format) {
2223 case PIX_FMT_YUVJ420P:
2224 *format = PIX_FMT_YUV420P;
2225 return 1;
2226 case PIX_FMT_YUVJ422P:
2227 *format = PIX_FMT_YUV422P;
2228 return 1;
2229 case PIX_FMT_YUVJ444P:
2230 *format = PIX_FMT_YUV444P;
2231 return 1;
9ba7fe6d
2232 case PIX_FMT_YUVJ440P:
2233 *format = PIX_FMT_YUV440P;
2234 return 1;
221b804f
DB
2235 default:
2236 return 0;
2237 }
44cdb423
LA
2238}
2239
58e4b706 2240SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
221b804f
DB
2241 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2242
2243 SwsContext *c;
2244 int i;
2245 int usesVFilter, usesHFilter;
2246 int unscaled, needsDither;
2247 int srcRange, dstRange;
2248 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
b63f641e 2249#if ARCH_X86
221b804f 2250 if (flags & SWS_CPU_CAPS_MMX)
7ad6469e 2251 __asm__ volatile("emms\n\t"::: "memory");
5cebb24b 2252#endif
516b1f82 2253
b63f641e 2254#if !defined(RUNTIME_CPUDETECT) || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
d3f3eea9 2255 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
b63f641e 2256#if HAVE_MMX2
221b804f 2257 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
f4406ec1 2258#elif HAVE_AMD3DNOW
221b804f 2259 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
b63f641e 2260#elif HAVE_MMX
221b804f 2261 flags |= SWS_CPU_CAPS_MMX;
b63f641e 2262#elif HAVE_ALTIVEC
221b804f 2263 flags |= SWS_CPU_CAPS_ALTIVEC;
b63f641e 2264#elif ARCH_BFIN
d3f3eea9 2265 flags |= SWS_CPU_CAPS_BFIN;
516b1f82 2266#endif
69796008 2267#endif /* RUNTIME_CPUDETECT */
221b804f 2268 if (clip_table[512] != 255) globalInit();
1b0a4572 2269 if (!rgb15to16) sws_rgb2rgb_init(flags);
221b804f
DB
2270
2271 unscaled = (srcW == dstW && srcH == dstH);
2272 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2273 && (fmt_depth(dstFormat))<24
2274 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2275
2276 srcRange = handle_jpeg(&srcFormat);
2277 dstRange = handle_jpeg(&dstFormat);
2278
2279 if (!isSupportedIn(srcFormat))
2280 {
0d6fd5ec 2281 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
221b804f
DB
2282 return NULL;
2283 }
2284 if (!isSupportedOut(dstFormat))
2285 {
0d6fd5ec 2286 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
221b804f
DB
2287 return NULL;
2288 }
2289
010c00bc
MN
2290 i= flags & ( SWS_POINT
2291 |SWS_AREA
6afc7c19 2292 |SWS_BILINEAR
010c00bc
MN
2293 |SWS_FAST_BILINEAR
2294 |SWS_BICUBIC
2295 |SWS_X
2296 |SWS_GAUSS
2297 |SWS_LANCZOS
2298 |SWS_SINC
2299 |SWS_SPLINE
2300 |SWS_BICUBLIN);
2301 if(!i || (i & (i-1)))
2302 {
f40c7dbb 2303 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
010c00bc
MN
2304 return NULL;
2305 }
010c00bc 2306
221b804f
DB
2307 /* sanity check */
2308 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
2309 {
2310 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2311 srcW, srcH, dstW, dstH);
2312 return NULL;
2313 }
8b2fce0d 2314 if(srcW > VOFW || dstW > VOFW){
f40c7dbb 2315 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
8b2fce0d
MN
2316 return NULL;
2317 }
221b804f
DB
2318
2319 if (!dstFilter) dstFilter= &dummyFilter;
2320 if (!srcFilter) srcFilter= &dummyFilter;
2321
2322 c= av_mallocz(sizeof(SwsContext));
2323
2324 c->av_class = &sws_context_class;
2325 c->srcW= srcW;
2326 c->srcH= srcH;
2327 c->dstW= dstW;
2328 c->dstH= dstH;
2329 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2330 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2331 c->flags= flags;
2332 c->dstFormat= dstFormat;
2333 c->srcFormat= srcFormat;
2334 c->vRounder= 4* 0x0001000100010001ULL;
2335
2336 usesHFilter= usesVFilter= 0;
1b0a4572
BF
2337 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2338 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2339 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2340 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2341 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2342 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2343 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2344 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
221b804f
DB
2345
2346 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2347 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2348
f40c7dbb 2349 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
221b804f
DB
2350 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2351
2352 // drop some chroma lines if the user wants it
2353 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2354 c->chrSrcVSubSample+= c->vChrDrop;
2355
f40c7dbb 2356 // drop every other pixel for chroma calculation unless user wants full chroma
221b804f
DB
2357 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2358 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2359 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
dfb09bd1 2360 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2f60f629 2361 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2362 c->chrSrcHSubSample=1;
2363
2364 if (param){
2365 c->param[0] = param[0];
2366 c->param[1] = param[1];
2367 }else{
2368 c->param[0] =
2369 c->param[1] = SWS_PARAM_DEFAULT;
2370 }
2371
2372 c->chrIntHSubSample= c->chrDstHSubSample;
2373 c->chrIntVSubSample= c->chrSrcVSubSample;
2374
2375 // Note the -((-x)>>y) is so that we always round toward +inf.
2376 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2377 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2378 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2379 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2380
fa58ba15 2381 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
221b804f 2382
f40c7dbb 2383 /* unscaled special cases */
6bc0c792 2384 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
221b804f
DB
2385 {
2386 /* yv12_to_nv12 */
2c897342 2387 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
221b804f
DB
2388 {
2389 c->swScale= PlanarToNV12Wrapper;
2390 }
221b804f 2391 /* yuv2bgr */
2c897342 2392 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
9b0d44ef 2393 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
221b804f 2394 {
780daf2b 2395 c->swScale= ff_yuv2rgb_get_func_ptr(c);
221b804f 2396 }
6a4970ab 2397
84c12535 2398 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
221b804f
DB
2399 {
2400 c->swScale= yvu9toyv12Wrapper;
2401 }
2402
2403 /* bgr24toYV12 */
08857704 2404 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
221b804f
DB
2405 c->swScale= bgr24toyv12Wrapper;
2406
f40c7dbb 2407 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
221b804f
DB
2408 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2409 && (isBGR(dstFormat) || isRGB(dstFormat))
2410 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2411 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2412 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2413 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2414 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2415 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2416 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
ec1bca2a 2417 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
9990e426
MN
2418 && dstFormat != PIX_FMT_RGB32_1
2419 && dstFormat != PIX_FMT_BGR32_1
736143c8 2420 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2421 c->swScale= rgb2rgbWrapper;
2422
49004617 2423 if ((usePal(srcFormat) && (
76e9fd01 2424 dstFormat == PIX_FMT_RGB32 ||
522ce957 2425 dstFormat == PIX_FMT_RGB32_1 ||
76e9fd01
VS
2426 dstFormat == PIX_FMT_RGB24 ||
2427 dstFormat == PIX_FMT_BGR32 ||
522ce957 2428 dstFormat == PIX_FMT_BGR32_1 ||
49004617
VS
2429 dstFormat == PIX_FMT_BGR24)))
2430 c->swScale= pal2rgbWrapper;
2431
a6100f39
BC
2432 if (srcFormat == PIX_FMT_YUV422P)
2433 {
2434 if (dstFormat == PIX_FMT_YUYV422)
2435 c->swScale= YUV422PToYuy2Wrapper;
2436 else if (dstFormat == PIX_FMT_UYVY422)
2437 c->swScale= YUV422PToUyvyWrapper;
2438 }
2439
221b804f
DB
2440 /* LQ converters if -sws 0 or -sws 4*/
2441 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
221b804f 2442 /* yv12_to_yuy2 */
2c897342 2443 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
221b804f
DB
2444 {
2445 if (dstFormat == PIX_FMT_YUYV422)
2446 c->swScale= PlanarToYuy2Wrapper;
3b38f1c6 2447 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2448 c->swScale= PlanarToUyvyWrapper;
2449 }
0411072e
MN
2450
2451 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV420P)
2452 c->swScale= YUYV2YUV420Wrapper;
2453 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV420P)
2454 c->swScale= UYVY2YUV420Wrapper;
221b804f 2455 }
0411072e
MN
2456 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2457 c->swScale= YUYV2YUV422Wrapper;
2458 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2459 c->swScale= UYVY2YUV422Wrapper;
ec22603f 2460
6634d0ef 2461#ifdef COMPILE_ALTIVEC
221b804f 2462 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
12794f73 2463 !(c->flags & SWS_BITEXACT) &&
3b38f1c6 2464 srcFormat == PIX_FMT_YUV420P) {
221b804f
DB
2465 // unscaled YV12 -> packed YUV, we want speed
2466 if (dstFormat == PIX_FMT_YUYV422)
2467 c->swScale= yv12toyuy2_unscaled_altivec;
3b38f1c6 2468 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2469 c->swScale= yv12touyvy_unscaled_altivec;
2470 }
b71cf33c
RD
2471#endif
2472
221b804f
DB
2473 /* simple copy */
2474 if ( srcFormat == dstFormat
2c897342 2475 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
221b804f 2476 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
e5091488 2477 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
221b804f 2478 {
2d35ae56
LB
2479 if (isPacked(c->srcFormat))
2480 c->swScale= packedCopy;
2481 else /* Planar YUV or gray */
2482 c->swScale= planarCopy;
221b804f
DB
2483 }
2484
2485 /* gray16{le,be} conversions */
2486 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2487 {
2488 c->swScale= gray16togray;
2489 }
2490 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2491 {
2492 c->swScale= graytogray16;
2493 }
2494 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2495 {
2496 c->swScale= gray16swap;
2497 }
2498
b63f641e 2499#if ARCH_BFIN
1ebbfe15
MH
2500 if (flags & SWS_CPU_CAPS_BFIN)
2501 ff_bfin_get_unscaled_swscale (c);
2502#endif
2503
221b804f
DB
2504 if (c->swScale){
2505 if (flags&SWS_PRINT_INFO)
4b0c30b7 2506 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
221b804f
DB
2507 sws_format_name(srcFormat), sws_format_name(dstFormat));
2508 return c;
2509 }
2510 }
2511
2512 if (flags & SWS_CPU_CAPS_MMX2)
2513 {
2514 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2515 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2516 {
2517 if (flags&SWS_PRINT_INFO)
f40c7dbb 2518 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
221b804f
DB
2519 }
2520 if (usesHFilter) c->canMMX2BeUsed=0;
2521 }
2522 else
2523 c->canMMX2BeUsed=0;
2524
2525 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2526 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2527
2528 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2529 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2530 // n-2 is the last chrominance sample available
86bdf3fd 2531 // this is not perfect, but no one should notice the difference, the more correct variant
221b804f
DB
2532 // would be like the vertical one, but that would require some special code for the
2533 // first and last pixel
2534 if (flags&SWS_FAST_BILINEAR)
2535 {
2536 if (c->canMMX2BeUsed)
2537 {
2538 c->lumXInc+= 20;
2539 c->chrXInc+= 20;
2540 }
f40c7dbb 2541 //we don't use the x86 asm scaler if MMX is available
221b804f
DB
2542 else if (flags & SWS_CPU_CAPS_MMX)
2543 {
2544 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2545 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2546 }
2547 }
2548
2549 /* precalculate horizontal scaler filter coefficients */
2550 {
2551 const int filterAlign=
2552 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2553 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2554 1;
2555
2556 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2557 srcW , dstW, filterAlign, 1<<14,
2558 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2559 srcFilter->lumH, dstFilter->lumH, c->param);
2560 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2561 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2562 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2563 srcFilter->chrH, dstFilter->chrH, c->param);
28bf81c9 2564
dbdae6ec 2565#define MAX_FUNNY_CODE_SIZE 10000
17c613ef 2566#if defined(COMPILE_MMX2)
77a416e8 2567// can't downscale !!!
221b804f
DB
2568 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2569 {
113ef149 2570#ifdef MAP_ANONYMOUS
16a3717f
RP
2571 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2572 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
dd35beb2
RP
2573#elif HAVE_VIRTUALALLOC
2574 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2575 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
38d5c282 2576#else
16a3717f 2577 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
221b804f 2578 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
38d5c282
AJ
2579#endif
2580
221b804f
DB
2581 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2582 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2583 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2584 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
b7dc6f66 2585
221b804f
DB
2586 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2587 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2588 }
17c613ef 2589#endif /* defined(COMPILE_MMX2) */
f40c7dbb 2590 } // initialize horizontal stuff
28bf81c9
MN
2591
2592
2593
221b804f
DB
2594 /* precalculate vertical scaler filter coefficients */
2595 {
2596 const int filterAlign=
2597 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2598 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2599 1;
8c266f0c 2600
221b804f 2601 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
5fe4aad7 2602 srcH , dstH, filterAlign, (1<<12),
221b804f
DB
2603 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2604 srcFilter->lumV, dstFilter->lumV, c->param);
2605 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
5fe4aad7 2606 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
221b804f
DB
2607 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2608 srcFilter->chrV, dstFilter->chrV, c->param);
d33d485e 2609
b63f641e 2610#if HAVE_ALTIVEC
221b804f
DB
2611 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2612 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2613
2614 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2615 int j;
2616 short *p = (short *)&c->vYCoeffsBank[i];
2617 for (j=0;j<8;j++)
2618 p[j] = c->vLumFilter[i];
2619 }
2620
2621 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2622 int j;
2623 short *p = (short *)&c->vCCoeffsBank[i];
2624 for (j=0;j<8;j++)
2625 p[j] = c->vChrFilter[i];
2626 }
d33d485e 2627#endif
221b804f
DB
2628 }
2629
f40c7dbb 2630 // calculate buffer sizes so that they won't run out while handling these damn slices
221b804f
DB
2631 c->vLumBufSize= c->vLumFilterSize;
2632 c->vChrBufSize= c->vChrFilterSize;
2633 for (i=0; i<dstH; i++)
2634 {
2635 int chrI= i*c->chrDstH / dstH;
2636 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2637 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2638
2639 nextSlice>>= c->chrSrcVSubSample;
2640 nextSlice<<= c->chrSrcVSubSample;
2641 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
e5091488 2642 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
221b804f
DB
2643 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2644 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2645 }
2646
2647 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2648 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2649 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
f40c7dbb 2650 //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)
221b804f
DB
2651 /* align at 16 bytes for AltiVec */
2652 for (i=0; i<c->vLumBufSize; i++)
8b2fce0d 2653 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
221b804f 2654 for (i=0; i<c->vChrBufSize; i++)
8b2fce0d 2655 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
221b804f
DB
2656
2657 //try to avoid drawing green stuff between the right end and the stride end
8b2fce0d
MN
2658 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2659
5a352b14 2660 assert(2*VOFW == VOF);
221b804f 2661
fcc402b1 2662 assert(c->chrDstH <= dstH);
221b804f
DB
2663
2664 if (flags&SWS_PRINT_INFO)
2665 {
28bf81c9 2666#ifdef DITHER1XBPP
3e62b7e3 2667 const char *dither= " dithered";
5521b193 2668#else
3e62b7e3 2669 const char *dither= "";
28bf81c9 2670#endif
221b804f 2671 if (flags&SWS_FAST_BILINEAR)
4b0c30b7 2672 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
221b804f 2673 else if (flags&SWS_BILINEAR)
4b0c30b7 2674 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
221b804f 2675 else if (flags&SWS_BICUBIC)
4b0c30b7 2676 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
221b804f 2677 else if (flags&SWS_X)
4b0c30b7 2678 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
221b804f 2679 else if (flags&SWS_POINT)
4b0c30b7 2680 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
221b804f 2681 else if (flags&SWS_AREA)
4b0c30b7 2682 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
221b804f 2683 else if (flags&SWS_BICUBLIN)
4b0c30b7 2684 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
221b804f 2685 else if (flags&SWS_GAUSS)
4b0c30b7 2686 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
221b804f 2687 else if (flags&SWS_SINC)
4b0c30b7 2688 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
221b804f 2689 else if (flags&SWS_LANCZOS)
4b0c30b7 2690 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
221b804f 2691 else if (flags&SWS_SPLINE)
4b0c30b7 2692 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
221b804f 2693 else
4b0c30b7 2694 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
221b804f
DB
2695
2696 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2697 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2698 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2699 else
2700 av_log(c, AV_LOG_INFO, "from %s to %s ",
2701 sws_format_name(srcFormat), sws_format_name(dstFormat));
2702
2703 if (flags & SWS_CPU_CAPS_MMX2)
2704 av_log(c, AV_LOG_INFO, "using MMX2\n");
2705 else if (flags & SWS_CPU_CAPS_3DNOW)
2706 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2707 else if (flags & SWS_CPU_CAPS_MMX)
2708 av_log(c, AV_LOG_INFO, "using MMX\n");
2709 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2710 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2711 else
2712 av_log(c, AV_LOG_INFO, "using C\n");
2713 }
2714
2715 if (flags & SWS_PRINT_INFO)
2716 {
2717 if (flags & SWS_CPU_CAPS_MMX)
2718 {
2719 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
4b0c30b7 2720 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
221b804f
DB
2721 else
2722 {
2723 if (c->hLumFilterSize==4)
4b0c30b7 2724 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2725 else if (c->hLumFilterSize==8)
4b0c30b7 2726 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2727 else
4b0c30b7 2728 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
221b804f
DB
2729
2730 if (c->hChrFilterSize==4)
4b0c30b7 2731 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2732 else if (c->hChrFilterSize==8)
4b0c30b7 2733 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2734 else
4b0c30b7 2735 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
221b804f
DB
2736 }
2737 }
2738 else
2739 {
b63f641e 2740#if ARCH_X86
f40c7dbb 2741 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
28bf81c9 2742#else
221b804f 2743 if (flags & SWS_FAST_BILINEAR)
4b0c30b7 2744 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
221b804f 2745 else
4b0c30b7 2746 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
28bf81c9 2747#endif
221b804f
DB
2748 }
2749 if (isPlanarYUV(dstFormat))
2750 {
2751 if (c->vLumFilterSize==1)
4b0c30b7 2752 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2753 else
4b0c30b7 2754 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f
DB
2755 }
2756 else
2757 {
2758 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
4b0c30b7
BC
2759 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2760 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2761 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
4b0c30b7 2762 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2763 else
4b0c30b7 2764 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f
DB
2765 }
2766
2767 if (dstFormat==PIX_FMT_BGR24)
f40c7dbb 2768 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
221b804f
DB
2769 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2770 else if (dstFormat==PIX_FMT_RGB32)
f40c7dbb 2771 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2772 else if (dstFormat==PIX_FMT_BGR565)
f40c7dbb 2773 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2774 else if (dstFormat==PIX_FMT_BGR555)
f40c7dbb 2775 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2776
4b0c30b7 2777 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
221b804f
DB
2778 }
2779 if (flags & SWS_PRINT_INFO)
2780 {
f40c7dbb 2781 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f 2782 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
f40c7dbb 2783 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f
DB
2784 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2785 }
2786
2787 c->swScale= getSwsFunc(flags);
2788 return c;
28bf81c9
MN
2789}
2790
2791/**
56b69633 2792 * swscale wrapper, so we don't need to export the SwsContext.
f40c7dbb 2793 * Assumes planar YUV to be in YUV order instead of YVU.
fccb9b2b 2794 */
703b56fb 2795int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2796 int srcSliceH, uint8_t* dst[], int dstStride[]){
2797 int i;
6208d676 2798 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
14623020 2799
221b804f 2800 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3f0bc115 2801 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
221b804f
DB
2802 return 0;
2803 }
2804 if (c->sliceDir == 0) {
2805 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2806 }
2807
49004617 2808 if (usePal(c->srcFormat)){
221b804f 2809 for (i=0; i<256; i++){
14623020
MN
2810 int p, r, g, b,y,u,v;
2811 if(c->srcFormat == PIX_FMT_PAL8){
2812 p=((uint32_t*)(src[1]))[i];
2813 r= (p>>16)&0xFF;
2814 g= (p>> 8)&0xFF;
2815 b= p &0xFF;
2816 }else if(c->srcFormat == PIX_FMT_RGB8){
2817 r= (i>>5 )*36;
2818 g= ((i>>2)&7)*36;
2819 b= (i&3 )*85;
2820 }else if(c->srcFormat == PIX_FMT_BGR8){
2821 b= (i>>6 )*85;
2822 g= ((i>>3)&7)*36;
2823 r= (i&7 )*36;
2824 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2825 r= (i>>3 )*255;
2826 g= ((i>>1)&3)*85;
2827 b= (i&1 )*255;
75add6fb
VS
2828 }else {
2829 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
14623020
MN
2830 b= (i>>3 )*255;
2831 g= ((i>>1)&3)*85;
2832 r= (i&1 )*255;
2833 }
cbcb408f
MN
2834 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2835 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2836 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
49004617 2837 c->pal_yuv[i]= y + (u<<8) + (v<<16);
522ce957
VS
2838
2839
2840 switch(c->dstFormat) {
2841 case PIX_FMT_BGR32:
2842#ifndef WORDS_BIGENDIAN
2843 case PIX_FMT_RGB24:
2844#endif
2845 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2846 break;
2847 case PIX_FMT_BGR32_1:
2848#ifdef WORDS_BIGENDIAN
2849 case PIX_FMT_BGR24:
2850#endif
2851 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2852 break;
2853 case PIX_FMT_RGB32_1:
2854#ifdef WORDS_BIGENDIAN
2855 case PIX_FMT_RGB24:
2856#endif
2857 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2858 break;
2859 case PIX_FMT_RGB32:
2860#ifndef WORDS_BIGENDIAN
2861 case PIX_FMT_BGR24:
2862#endif
2863 default:
2864 c->pal_rgb[i]= b + (g<<8) + (r<<16);
2865 }
21c08a3f 2866 }
221b804f 2867 }
21c08a3f 2868
221b804f
DB
2869 // copy strides, so they can safely be modified
2870 if (c->sliceDir == 1) {
2871 // slices go from top to bottom
6208d676
CS
2872 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2873 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
221b804f
DB
2874 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2875 } else {
2876 // slices go from bottom to top => we flip the image internally
2877 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2878 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
6208d676
CS
2879 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2],
2880 dst[3] + (c->dstH-1)*dstStride[3]};
2881 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2882 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
221b804f
DB
2883
2884 src2[0] += (srcSliceH-1)*srcStride[0];
49004617 2885 if (!usePal(c->srcFormat))
221b804f
DB
2886 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2887 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
6208d676 2888 src2[3] += (srcSliceH-1)*srcStride[3];
221b804f
DB
2889
2890 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2891 }
fccb9b2b
MN
2892}
2893
c099766f 2894#if LIBSWSCALE_VERSION_MAJOR < 1
703b56fb 2895int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2896 int srcSliceH, uint8_t* dst[], int dstStride[]){
2897 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
d4e24275 2898}
c099766f 2899#endif
d4e24275 2900
6a4970ab 2901SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
221b804f
DB
2902 float lumaSharpen, float chromaSharpen,
2903 float chromaHShift, float chromaVShift,
2904 int verbose)
e21206a8 2905{
221b804f
DB
2906 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2907
2908 if (lumaGBlur!=0.0){
2909 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2910 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2911 }else{
2912 filter->lumH= sws_getIdentityVec();
2913 filter->lumV= sws_getIdentityVec();
2914 }
2915
2916 if (chromaGBlur!=0.0){
2917 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2918 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2919 }else{
2920 filter->chrH= sws_getIdentityVec();
2921 filter->chrV= sws_getIdentityVec();
2922 }
2923
2924 if (chromaSharpen!=0.0){
2925 SwsVector *id= sws_getIdentityVec();
2926 sws_scaleVec(filter->chrH, -chromaSharpen);
2927 sws_scaleVec(filter->chrV, -chromaSharpen);
2928 sws_addVec(filter->chrH, id);
2929 sws_addVec(filter->chrV, id);
2930 sws_freeVec(id);
2931 }
2932
2933 if (lumaSharpen!=0.0){
2934 SwsVector *id= sws_getIdentityVec();
2935 sws_scaleVec(filter->lumH, -lumaSharpen);
2936 sws_scaleVec(filter->lumV, -lumaSharpen);
2937 sws_addVec(filter->lumH, id);
2938 sws_addVec(filter->lumV, id);
2939 sws_freeVec(id);
2940 }
2941
2942 if (chromaHShift != 0.0)
2943 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2944
2945 if (chromaVShift != 0.0)
2946 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2947
2948 sws_normalizeVec(filter->chrH, 1.0);
2949 sws_normalizeVec(filter->chrV, 1.0);
2950 sws_normalizeVec(filter->lumH, 1.0);
2951 sws_normalizeVec(filter->lumV, 1.0);
2952
e14ff1e6
SS
2953 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2954 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
221b804f
DB
2955
2956 return filter;
e21206a8
MN
2957}
2958
d4e24275 2959SwsVector *sws_getGaussianVec(double variance, double quality){
221b804f
DB
2960 const int length= (int)(variance*quality + 0.5) | 1;
2961 int i;
2962 double *coeff= av_malloc(length*sizeof(double));
2963 double middle= (length-1)*0.5;
2964 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2965
221b804f
DB
2966 vec->coeff= coeff;
2967 vec->length= length;
28bf81c9 2968
221b804f
DB
2969 for (i=0; i<length; i++)
2970 {
2971 double dist= i-middle;
e5091488 2972 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
221b804f 2973 }
28bf81c9 2974
221b804f 2975 sws_normalizeVec(vec, 1.0);
c7f822d9 2976
221b804f 2977 return vec;
28bf81c9
MN
2978}
2979
d4e24275 2980SwsVector *sws_getConstVec(double c, int length){
221b804f
DB
2981 int i;
2982 double *coeff= av_malloc(length*sizeof(double));
2983 SwsVector *vec= av_malloc(sizeof(SwsVector));
5521b193 2984
221b804f
DB
2985 vec->coeff= coeff;
2986 vec->length= length;
5521b193 2987
221b804f
DB
2988 for (i=0; i<length; i++)
2989 coeff[i]= c;
5521b193 2990
221b804f 2991 return vec;
5521b193
MN
2992}
2993
2994
d4e24275 2995SwsVector *sws_getIdentityVec(void){
221b804f 2996 return sws_getConstVec(1.0, 1);
c7f822d9
MN
2997}
2998
2e728364 2999double sws_dcVec(SwsVector *a){
221b804f
DB
3000 int i;
3001 double sum=0;
28bf81c9 3002
221b804f
DB
3003 for (i=0; i<a->length; i++)
3004 sum+= a->coeff[i];
28bf81c9 3005
221b804f 3006 return sum;
28bf81c9
MN
3007}
3008
d4e24275 3009void sws_scaleVec(SwsVector *a, double scalar){
221b804f 3010 int i;
c7f822d9 3011
221b804f
DB
3012 for (i=0; i<a->length; i++)
3013 a->coeff[i]*= scalar;
c7f822d9
MN
3014}
3015
2e728364 3016void sws_normalizeVec(SwsVector *a, double height){
221b804f 3017 sws_scaleVec(a, height/sws_dcVec(a));
2e728364
MN
3018}
3019
d4e24275 3020static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
221b804f
DB
3021 int length= a->length + b->length - 1;
3022 double *coeff= av_malloc(length*sizeof(double));
3023 int i, j;
3024 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3025
221b804f
DB
3026 vec->coeff= coeff;
3027 vec->length= length;
28bf81c9 3028
221b804f 3029 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 3030
221b804f
DB
3031 for (i=0; i<a->length; i++)
3032 {
3033 for (j=0; j<b->length; j++)
3034 {
3035 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3036 }
3037 }
28bf81c9 3038
221b804f 3039 return vec;
28bf81c9
MN
3040}
3041
d4e24275 3042static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
221b804f
DB
3043 int length= FFMAX(a->length, b->length);
3044 double *coeff= av_malloc(length*sizeof(double));
3045 int i;
3046 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3047
221b804f
DB
3048 vec->coeff= coeff;
3049 vec->length= length;
28bf81c9 3050
221b804f 3051 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 3052
221b804f
DB
3053 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3054 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
c7f822d9 3055
221b804f 3056 return vec;
28bf81c9 3057}
c7f822d9 3058
d4e24275 3059static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
221b804f
DB
3060 int length= FFMAX(a->length, b->length);
3061 double *coeff= av_malloc(length*sizeof(double));
3062 int i;
3063 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3064
221b804f
DB
3065 vec->coeff= coeff;
3066 vec->length= length;
c7f822d9 3067
221b804f 3068 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 3069
221b804f
DB
3070 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3071 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
c7f822d9 3072
221b804f 3073 return vec;
c7f822d9
MN
3074}
3075
3076/* shift left / or right if "shift" is negative */
d4e24275 3077static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
221b804f
DB
3078 int length= a->length + FFABS(shift)*2;
3079 double *coeff= av_malloc(length*sizeof(double));
3080 int i;
3081 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3082
221b804f
DB
3083 vec->coeff= coeff;
3084 vec->length= length;
c7f822d9 3085
221b804f 3086 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 3087
221b804f
DB
3088 for (i=0; i<a->length; i++)
3089 {
3090 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3091 }
c7f822d9 3092
221b804f 3093 return vec;
c7f822d9
MN
3094}
3095
d4e24275 3096void sws_shiftVec(SwsVector *a, int shift){
221b804f
DB
3097 SwsVector *shifted= sws_getShiftedVec(a, shift);
3098 av_free(a->coeff);
3099 a->coeff= shifted->coeff;
3100 a->length= shifted->length;
3101 av_free(shifted);
5cebb24b
MN
3102}
3103
d4e24275 3104void sws_addVec(SwsVector *a, SwsVector *b){
221b804f
DB
3105 SwsVector *sum= sws_sumVec(a, b);
3106 av_free(a->coeff);
3107 a->coeff= sum->coeff;
3108 a->length= sum->length;
3109 av_free(sum);
5cebb24b
MN
3110}
3111
d4e24275 3112void sws_subVec(SwsVector *a, SwsVector *b){
221b804f
DB
3113 SwsVector *diff= sws_diffVec(a, b);
3114 av_free(a->coeff);
3115 a->coeff= diff->coeff;
3116 a->length= diff->length;
3117 av_free(diff);
5cebb24b
MN
3118}
3119
d4e24275 3120void sws_convVec(SwsVector *a, SwsVector *b){
221b804f
DB
3121 SwsVector *conv= sws_getConvVec(a, b);
3122 av_free(a->coeff);
3123 a->coeff= conv->coeff;
3124 a->length= conv->length;
3125 av_free(conv);
5cebb24b
MN
3126}
3127
d4e24275 3128SwsVector *sws_cloneVec(SwsVector *a){
221b804f
DB
3129 double *coeff= av_malloc(a->length*sizeof(double));
3130 int i;
3131 SwsVector *vec= av_malloc(sizeof(SwsVector));
5cebb24b 3132
221b804f
DB
3133 vec->coeff= coeff;
3134 vec->length= a->length;
5cebb24b 3135
221b804f 3136 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
5cebb24b 3137
221b804f 3138 return vec;
5cebb24b
MN
3139}
3140
26b40a65 3141void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
221b804f
DB
3142 int i;
3143 double max=0;
3144 double min=0;
3145 double range;
3146
3147 for (i=0; i<a->length; i++)
3148 if (a->coeff[i]>max) max= a->coeff[i];
3149
3150 for (i=0; i<a->length; i++)
3151 if (a->coeff[i]<min) min= a->coeff[i];
3152
3153 range= max - min;
3154
3155 for (i=0; i<a->length; i++)
3156 {
3157 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
26b40a65
SS
3158 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3159 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3160 av_log(log_ctx, log_level, "|\n");
221b804f 3161 }
c7f822d9
MN
3162}
3163
26b40a65
SS
3164#if LIBSWSCALE_VERSION_MAJOR < 1
3165void sws_printVec(SwsVector *a){
3166 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3167}
3168#endif
3169
d4e24275 3170void sws_freeVec(SwsVector *a){
221b804f 3171 if (!a) return;
47b7382d 3172 av_freep(&a->coeff);
221b804f
DB
3173 a->length=0;
3174 av_free(a);
c7f822d9
MN
3175}
3176
e21206a8 3177void sws_freeFilter(SwsFilter *filter){
221b804f 3178 if (!filter) return;
e21206a8 3179
221b804f
DB
3180 if (filter->lumH) sws_freeVec(filter->lumH);
3181 if (filter->lumV) sws_freeVec(filter->lumV);
3182 if (filter->chrH) sws_freeVec(filter->chrH);
3183 if (filter->chrV) sws_freeVec(filter->chrV);
3184 av_free(filter);
e21206a8
MN
3185}
3186
3187
d4e24275 3188void sws_freeContext(SwsContext *c){
221b804f
DB
3189 int i;
3190 if (!c) return;
3191
3192 if (c->lumPixBuf)
3193 {
3194 for (i=0; i<c->vLumBufSize; i++)
47b7382d
MN
3195 av_freep(&c->lumPixBuf[i]);
3196 av_freep(&c->lumPixBuf);
221b804f
DB
3197 }
3198
3199 if (c->chrPixBuf)
3200 {
3201 for (i=0; i<c->vChrBufSize; i++)
47b7382d
MN
3202 av_freep(&c->chrPixBuf[i]);
3203 av_freep(&c->chrPixBuf);
221b804f
DB
3204 }
3205
47b7382d
MN
3206 av_freep(&c->vLumFilter);
3207 av_freep(&c->vChrFilter);
3208 av_freep(&c->hLumFilter);
3209 av_freep(&c->hChrFilter);
b63f641e 3210#if HAVE_ALTIVEC
47b7382d
MN
3211 av_freep(&c->vYCoeffsBank);
3212 av_freep(&c->vCCoeffsBank);
d33d485e 3213#endif
c7f822d9 3214
47b7382d
MN
3215 av_freep(&c->vLumFilterPos);
3216 av_freep(&c->vChrFilterPos);
3217 av_freep(&c->hLumFilterPos);
3218 av_freep(&c->hChrFilterPos);
c7f822d9 3219
b63f641e 3220#if ARCH_X86 && CONFIG_GPL
0ce5275d 3221#ifdef MAP_ANONYMOUS
16a3717f 3222 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
221b804f 3223 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
dd35beb2
RP
3224#elif HAVE_VIRTUALALLOC
3225 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3226 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
38d5c282 3227#else
16a3717f 3228 av_free(c->funnyYCode );
221b804f 3229 av_free(c->funnyUVCode);
38d5c282 3230#endif
221b804f
DB
3231 c->funnyYCode=NULL;
3232 c->funnyUVCode=NULL;
7a24ec50 3233#endif /* ARCH_X86 && CONFIG_GPL */
38d5c282 3234
47b7382d
MN
3235 av_freep(&c->lumMmx2Filter);
3236 av_freep(&c->chrMmx2Filter);
3237 av_freep(&c->lumMmx2FilterPos);
3238 av_freep(&c->chrMmx2FilterPos);
3239 av_freep(&c->yuvTable);
221b804f
DB
3240
3241 av_free(c);
c7f822d9
MN
3242}
3243
22e46959 3244struct SwsContext *sws_getCachedContext(struct SwsContext *context,
58e4b706
CEH
3245 int srcW, int srcH, enum PixelFormat srcFormat,
3246 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
221b804f 3247 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
22e46959 3248{
337a2a45
KMH
3249 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3250
3251 if (!param)
3252 param = default_param;
3253
1b0a4572 3254 if (context) {
b8db4e22
BF
3255 if (context->srcW != srcW || context->srcH != srcH ||
3256 context->srcFormat != srcFormat ||
3257 context->dstW != dstW || context->dstH != dstH ||
3258 context->dstFormat != dstFormat || context->flags != flags ||
3259 context->param[0] != param[0] || context->param[1] != param[1])
22e46959
VP
3260 {
3261 sws_freeContext(context);
3262 context = NULL;
3263 }
3264 }
1b0a4572 3265 if (!context) {
22e46959 3266 return sws_getContext(srcW, srcH, srcFormat,
221b804f
DB
3267 dstW, dstH, dstFormat, flags,
3268 srcFilter, dstFilter, param);
22e46959
VP
3269 }
3270 return context;
3271}
3272