Add YUVA420P -> RGBA/BGRA/ARGB/ABGR unscaled converters
[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
f6cf4ed0 1992 if (dst[plane] && !src[plane])
d4da3e47 1993 fillPlane(dst[plane], dstStride[plane], length, height, y, 128);
bc5a0444
LB
1994 else
1995 {
1996 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1997 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
221b804f
DB
1998 else
1999 {
bc5a0444
LB
2000 int i;
2001 uint8_t *srcPtr= src[plane];
2002 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2003 for (i=0; i<height; i++)
221b804f 2004 {
bc5a0444
LB
2005 memcpy(dstPtr, srcPtr, length);
2006 srcPtr+= srcStride[plane];
2007 dstPtr+= dstStride[plane];
221b804f
DB
2008 }
2009 }
2010 }
bc5a0444 2011 }
221b804f 2012 return srcSliceH;
37079906 2013}
28bf81c9 2014
4884b9e5 2015static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2016 int srcSliceH, uint8_t* dst[], int dstStride[]){
2017
2018 int length= c->srcW;
2019 int y= srcSliceY;
2020 int height= srcSliceH;
2021 int i, j;
2022 uint8_t *srcPtr= src[0];
2023 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2024
2025 if (!isGray(c->dstFormat)){
2026 int height= -((-srcSliceH)>>c->chrDstVSubSample);
2027 memset(dst[1], 128, dstStride[1]*height);
2028 memset(dst[2], 128, dstStride[2]*height);
2029 }
2030 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
2031 for (i=0; i<height; i++)
2032 {
2033 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2034 srcPtr+= srcStride[0];
2035 dstPtr+= dstStride[0];
2036 }
2037 return srcSliceH;
4884b9e5
KS
2038}
2039
2040static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2041 int srcSliceH, uint8_t* dst[], int dstStride[]){
2042
2043 int length= c->srcW;
2044 int y= srcSliceY;
2045 int height= srcSliceH;
2046 int i, j;
2047 uint8_t *srcPtr= src[0];
2048 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2049 for (i=0; i<height; i++)
2050 {
2051 for (j=0; j<length; j++)
2052 {
2053 dstPtr[j<<1] = srcPtr[j];
2054 dstPtr[(j<<1)+1] = srcPtr[j];
2055 }
2056 srcPtr+= srcStride[0];
2057 dstPtr+= dstStride[0];
2058 }
2059 return srcSliceH;
4884b9e5
KS
2060}
2061
2062static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2063 int srcSliceH, uint8_t* dst[], int dstStride[]){
2064
2065 int length= c->srcW;
2066 int y= srcSliceY;
2067 int height= srcSliceH;
2068 int i, j;
73d046e2 2069 uint16_t *srcPtr= (uint16_t*)src[0];
b8b015f4 2070 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
221b804f
DB
2071 for (i=0; i<height; i++)
2072 {
2073 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
2074 srcPtr+= srcStride[0]/2;
2075 dstPtr+= dstStride[0]/2;
2076 }
2077 return srcSliceH;
4884b9e5
KS
2078}
2079
2080
c7a810cc 2081static void getSubSampleFactors(int *h, int *v, int format){
221b804f
DB
2082 switch(format){
2083 case PIX_FMT_UYVY422:
2084 case PIX_FMT_YUYV422:
2085 *h=1;
2086 *v=0;
2087 break;
2088 case PIX_FMT_YUV420P:
79973335 2089 case PIX_FMT_YUVA420P:
221b804f
DB
2090 case PIX_FMT_GRAY16BE:
2091 case PIX_FMT_GRAY16LE:
2092 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2093 case PIX_FMT_NV12:
2094 case PIX_FMT_NV21:
2095 *h=1;
2096 *v=1;
2097 break;
9ba7fe6d
2098 case PIX_FMT_YUV440P:
2099 *h=0;
2100 *v=1;
2101 break;
221b804f
DB
2102 case PIX_FMT_YUV410P:
2103 *h=2;
2104 *v=2;
2105 break;
2106 case PIX_FMT_YUV444P:
2107 *h=0;
2108 *v=0;
2109 break;
2110 case PIX_FMT_YUV422P:
2111 *h=1;
2112 *v=0;
2113 break;
2114 case PIX_FMT_YUV411P:
2115 *h=2;
2116 *v=0;
2117 break;
2118 default:
2119 *h=0;
2120 *v=0;
2121 break;
2122 }
c7a810cc
MN
2123}
2124
5427e242 2125static uint16_t roundToInt16(int64_t f){
221b804f
DB
2126 int r= (f + (1<<15))>>16;
2127 if (r<-0x7FFF) return 0x8000;
2128 else if (r> 0x7FFF) return 0x7FFF;
2129 else return r;
0481412a
MN
2130}
2131
2132/**
fa58ba15 2133 * @param inv_table the yuv2rgb coefficients, normally ff_yuv2rgb_coeffs[x]
86bdf3fd 2134 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
5427e242 2135 * @return -1 if not supported
0481412a 2136 */
5427e242 2137int 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
2138 int64_t crv = inv_table[0];
2139 int64_t cbu = inv_table[1];
2140 int64_t cgu = -inv_table[2];
2141 int64_t cgv = -inv_table[3];
2142 int64_t cy = 1<<16;
2143 int64_t oy = 0;
2144
221b804f
DB
2145 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2146 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2147
2148 c->brightness= brightness;
2149 c->contrast = contrast;
2150 c->saturation= saturation;
2151 c->srcRange = srcRange;
2152 c->dstRange = dstRange;
6bc0c792 2153 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
221b804f
DB
2154
2155 c->uOffset= 0x0400040004000400LL;
2156 c->vOffset= 0x0400040004000400LL;
2157
2158 if (!srcRange){
2159 cy= (cy*255) / 219;
2160 oy= 16<<16;
2161 }else{
2162 crv= (crv*224) / 255;
2163 cbu= (cbu*224) / 255;
2164 cgu= (cgu*224) / 255;
2165 cgv= (cgv*224) / 255;
2166 }
0481412a 2167
221b804f
DB
2168 cy = (cy *contrast )>>16;
2169 crv= (crv*contrast * saturation)>>32;
2170 cbu= (cbu*contrast * saturation)>>32;
2171 cgu= (cgu*contrast * saturation)>>32;
2172 cgv= (cgv*contrast * saturation)>>32;
0481412a 2173
221b804f 2174 oy -= 256*brightness;
0481412a 2175
221b804f
DB
2176 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2177 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2178 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2179 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2180 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2181 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
5427e242 2182
43c16478
MN
2183 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2184 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2185 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2186 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2187 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2188 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
f0faee4c 2189
780daf2b 2190 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
221b804f 2191 //FIXME factorize
a31de956 2192
6634d0ef 2193#ifdef COMPILE_ALTIVEC
221b804f 2194 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
780daf2b 2195 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
6a4970ab 2196#endif
221b804f 2197 return 0;
5427e242
MN
2198}
2199
2200/**
2201 * @return -1 if not supported
2202 */
2203int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
221b804f 2204 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
5427e242 2205
221b804f
DB
2206 *inv_table = c->srcColorspaceTable;
2207 *table = c->dstColorspaceTable;
2208 *srcRange = c->srcRange;
2209 *dstRange = c->dstRange;
2210 *brightness= c->brightness;
2211 *contrast = c->contrast;
2212 *saturation= c->saturation;
6a4970ab 2213
221b804f 2214 return 0;
0481412a
MN
2215}
2216
13394e8c 2217static int handle_jpeg(enum PixelFormat *format)
44cdb423 2218{
221b804f
DB
2219 switch (*format) {
2220 case PIX_FMT_YUVJ420P:
2221 *format = PIX_FMT_YUV420P;
2222 return 1;
2223 case PIX_FMT_YUVJ422P:
2224 *format = PIX_FMT_YUV422P;
2225 return 1;
2226 case PIX_FMT_YUVJ444P:
2227 *format = PIX_FMT_YUV444P;
2228 return 1;
9ba7fe6d
2229 case PIX_FMT_YUVJ440P:
2230 *format = PIX_FMT_YUV440P;
2231 return 1;
221b804f
DB
2232 default:
2233 return 0;
2234 }
44cdb423
LA
2235}
2236
58e4b706 2237SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
221b804f
DB
2238 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2239
2240 SwsContext *c;
2241 int i;
2242 int usesVFilter, usesHFilter;
2243 int unscaled, needsDither;
2244 int srcRange, dstRange;
2245 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
b63f641e 2246#if ARCH_X86
221b804f 2247 if (flags & SWS_CPU_CAPS_MMX)
7ad6469e 2248 __asm__ volatile("emms\n\t"::: "memory");
5cebb24b 2249#endif
516b1f82 2250
b63f641e 2251#if !defined(RUNTIME_CPUDETECT) || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
d3f3eea9 2252 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
b63f641e 2253#if HAVE_MMX2
221b804f 2254 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
f4406ec1 2255#elif HAVE_AMD3DNOW
221b804f 2256 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
b63f641e 2257#elif HAVE_MMX
221b804f 2258 flags |= SWS_CPU_CAPS_MMX;
b63f641e 2259#elif HAVE_ALTIVEC
221b804f 2260 flags |= SWS_CPU_CAPS_ALTIVEC;
b63f641e 2261#elif ARCH_BFIN
d3f3eea9 2262 flags |= SWS_CPU_CAPS_BFIN;
516b1f82 2263#endif
69796008 2264#endif /* RUNTIME_CPUDETECT */
221b804f 2265 if (clip_table[512] != 255) globalInit();
1b0a4572 2266 if (!rgb15to16) sws_rgb2rgb_init(flags);
221b804f
DB
2267
2268 unscaled = (srcW == dstW && srcH == dstH);
2269 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2270 && (fmt_depth(dstFormat))<24
2271 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2272
2273 srcRange = handle_jpeg(&srcFormat);
2274 dstRange = handle_jpeg(&dstFormat);
2275
2276 if (!isSupportedIn(srcFormat))
2277 {
0d6fd5ec 2278 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
221b804f
DB
2279 return NULL;
2280 }
2281 if (!isSupportedOut(dstFormat))
2282 {
0d6fd5ec 2283 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
221b804f
DB
2284 return NULL;
2285 }
2286
010c00bc
MN
2287 i= flags & ( SWS_POINT
2288 |SWS_AREA
6afc7c19 2289 |SWS_BILINEAR
010c00bc
MN
2290 |SWS_FAST_BILINEAR
2291 |SWS_BICUBIC
2292 |SWS_X
2293 |SWS_GAUSS
2294 |SWS_LANCZOS
2295 |SWS_SINC
2296 |SWS_SPLINE
2297 |SWS_BICUBLIN);
2298 if(!i || (i & (i-1)))
2299 {
f40c7dbb 2300 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
010c00bc
MN
2301 return NULL;
2302 }
010c00bc 2303
221b804f
DB
2304 /* sanity check */
2305 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
2306 {
2307 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2308 srcW, srcH, dstW, dstH);
2309 return NULL;
2310 }
8b2fce0d 2311 if(srcW > VOFW || dstW > VOFW){
f40c7dbb 2312 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
8b2fce0d
MN
2313 return NULL;
2314 }
221b804f
DB
2315
2316 if (!dstFilter) dstFilter= &dummyFilter;
2317 if (!srcFilter) srcFilter= &dummyFilter;
2318
2319 c= av_mallocz(sizeof(SwsContext));
2320
2321 c->av_class = &sws_context_class;
2322 c->srcW= srcW;
2323 c->srcH= srcH;
2324 c->dstW= dstW;
2325 c->dstH= dstH;
2326 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2327 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2328 c->flags= flags;
2329 c->dstFormat= dstFormat;
2330 c->srcFormat= srcFormat;
2331 c->vRounder= 4* 0x0001000100010001ULL;
2332
2333 usesHFilter= usesVFilter= 0;
1b0a4572
BF
2334 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2335 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2336 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2337 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2338 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2339 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2340 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2341 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
221b804f
DB
2342
2343 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2344 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2345
f40c7dbb 2346 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
221b804f
DB
2347 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2348
2349 // drop some chroma lines if the user wants it
2350 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2351 c->chrSrcVSubSample+= c->vChrDrop;
2352
f40c7dbb 2353 // drop every other pixel for chroma calculation unless user wants full chroma
221b804f
DB
2354 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2355 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2356 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
dfb09bd1 2357 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2f60f629 2358 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2359 c->chrSrcHSubSample=1;
2360
2361 if (param){
2362 c->param[0] = param[0];
2363 c->param[1] = param[1];
2364 }else{
2365 c->param[0] =
2366 c->param[1] = SWS_PARAM_DEFAULT;
2367 }
2368
2369 c->chrIntHSubSample= c->chrDstHSubSample;
2370 c->chrIntVSubSample= c->chrSrcVSubSample;
2371
2372 // Note the -((-x)>>y) is so that we always round toward +inf.
2373 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2374 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2375 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2376 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2377
fa58ba15 2378 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
221b804f 2379
f40c7dbb 2380 /* unscaled special cases */
6bc0c792 2381 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
221b804f
DB
2382 {
2383 /* yv12_to_nv12 */
2c897342 2384 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
221b804f
DB
2385 {
2386 c->swScale= PlanarToNV12Wrapper;
2387 }
221b804f 2388 /* yuv2bgr */
2c897342 2389 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
9b0d44ef 2390 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
221b804f 2391 {
780daf2b 2392 c->swScale= ff_yuv2rgb_get_func_ptr(c);
221b804f 2393 }
6a4970ab 2394
84c12535 2395 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
221b804f
DB
2396 {
2397 c->swScale= yvu9toyv12Wrapper;
2398 }
2399
2400 /* bgr24toYV12 */
08857704 2401 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
221b804f
DB
2402 c->swScale= bgr24toyv12Wrapper;
2403
f40c7dbb 2404 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
221b804f
DB
2405 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2406 && (isBGR(dstFormat) || isRGB(dstFormat))
2407 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2408 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2409 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2410 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2411 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2412 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2413 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
ec1bca2a 2414 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
9990e426
MN
2415 && dstFormat != PIX_FMT_RGB32_1
2416 && dstFormat != PIX_FMT_BGR32_1
736143c8 2417 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2418 c->swScale= rgb2rgbWrapper;
2419
49004617 2420 if ((usePal(srcFormat) && (
76e9fd01 2421 dstFormat == PIX_FMT_RGB32 ||
522ce957 2422 dstFormat == PIX_FMT_RGB32_1 ||
76e9fd01
VS
2423 dstFormat == PIX_FMT_RGB24 ||
2424 dstFormat == PIX_FMT_BGR32 ||
522ce957 2425 dstFormat == PIX_FMT_BGR32_1 ||
49004617
VS
2426 dstFormat == PIX_FMT_BGR24)))
2427 c->swScale= pal2rgbWrapper;
2428
a6100f39
BC
2429 if (srcFormat == PIX_FMT_YUV422P)
2430 {
2431 if (dstFormat == PIX_FMT_YUYV422)
2432 c->swScale= YUV422PToYuy2Wrapper;
2433 else if (dstFormat == PIX_FMT_UYVY422)
2434 c->swScale= YUV422PToUyvyWrapper;
2435 }
2436
221b804f
DB
2437 /* LQ converters if -sws 0 or -sws 4*/
2438 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
221b804f 2439 /* yv12_to_yuy2 */
2c897342 2440 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
221b804f
DB
2441 {
2442 if (dstFormat == PIX_FMT_YUYV422)
2443 c->swScale= PlanarToYuy2Wrapper;
3b38f1c6 2444 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2445 c->swScale= PlanarToUyvyWrapper;
2446 }
0411072e
MN
2447
2448 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV420P)
2449 c->swScale= YUYV2YUV420Wrapper;
2450 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV420P)
2451 c->swScale= UYVY2YUV420Wrapper;
221b804f 2452 }
0411072e
MN
2453 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2454 c->swScale= YUYV2YUV422Wrapper;
2455 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2456 c->swScale= UYVY2YUV422Wrapper;
ec22603f 2457
6634d0ef 2458#ifdef COMPILE_ALTIVEC
221b804f 2459 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
12794f73 2460 !(c->flags & SWS_BITEXACT) &&
3b38f1c6 2461 srcFormat == PIX_FMT_YUV420P) {
221b804f
DB
2462 // unscaled YV12 -> packed YUV, we want speed
2463 if (dstFormat == PIX_FMT_YUYV422)
2464 c->swScale= yv12toyuy2_unscaled_altivec;
3b38f1c6 2465 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2466 c->swScale= yv12touyvy_unscaled_altivec;
2467 }
b71cf33c
RD
2468#endif
2469
221b804f
DB
2470 /* simple copy */
2471 if ( srcFormat == dstFormat
2c897342 2472 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
221b804f 2473 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
e5091488 2474 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
221b804f 2475 {
2d35ae56
LB
2476 if (isPacked(c->srcFormat))
2477 c->swScale= packedCopy;
2478 else /* Planar YUV or gray */
2479 c->swScale= planarCopy;
221b804f
DB
2480 }
2481
2482 /* gray16{le,be} conversions */
2483 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2484 {
2485 c->swScale= gray16togray;
2486 }
2487 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2488 {
2489 c->swScale= graytogray16;
2490 }
2491 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2492 {
2493 c->swScale= gray16swap;
2494 }
2495
b63f641e 2496#if ARCH_BFIN
1ebbfe15
MH
2497 if (flags & SWS_CPU_CAPS_BFIN)
2498 ff_bfin_get_unscaled_swscale (c);
2499#endif
2500
221b804f
DB
2501 if (c->swScale){
2502 if (flags&SWS_PRINT_INFO)
4b0c30b7 2503 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
221b804f
DB
2504 sws_format_name(srcFormat), sws_format_name(dstFormat));
2505 return c;
2506 }
2507 }
2508
2509 if (flags & SWS_CPU_CAPS_MMX2)
2510 {
2511 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2512 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2513 {
2514 if (flags&SWS_PRINT_INFO)
f40c7dbb 2515 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
221b804f
DB
2516 }
2517 if (usesHFilter) c->canMMX2BeUsed=0;
2518 }
2519 else
2520 c->canMMX2BeUsed=0;
2521
2522 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2523 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2524
2525 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2526 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2527 // n-2 is the last chrominance sample available
86bdf3fd 2528 // this is not perfect, but no one should notice the difference, the more correct variant
221b804f
DB
2529 // would be like the vertical one, but that would require some special code for the
2530 // first and last pixel
2531 if (flags&SWS_FAST_BILINEAR)
2532 {
2533 if (c->canMMX2BeUsed)
2534 {
2535 c->lumXInc+= 20;
2536 c->chrXInc+= 20;
2537 }
f40c7dbb 2538 //we don't use the x86 asm scaler if MMX is available
221b804f
DB
2539 else if (flags & SWS_CPU_CAPS_MMX)
2540 {
2541 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2542 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2543 }
2544 }
2545
2546 /* precalculate horizontal scaler filter coefficients */
2547 {
2548 const int filterAlign=
2549 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2550 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2551 1;
2552
2553 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2554 srcW , dstW, filterAlign, 1<<14,
2555 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2556 srcFilter->lumH, dstFilter->lumH, c->param);
2557 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2558 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2559 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2560 srcFilter->chrH, dstFilter->chrH, c->param);
28bf81c9 2561
dbdae6ec 2562#define MAX_FUNNY_CODE_SIZE 10000
17c613ef 2563#if defined(COMPILE_MMX2)
77a416e8 2564// can't downscale !!!
221b804f
DB
2565 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2566 {
113ef149 2567#ifdef MAP_ANONYMOUS
16a3717f
RP
2568 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2569 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
dd35beb2
RP
2570#elif HAVE_VIRTUALALLOC
2571 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2572 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
38d5c282 2573#else
16a3717f 2574 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
221b804f 2575 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
38d5c282
AJ
2576#endif
2577
221b804f
DB
2578 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2579 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2580 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2581 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
b7dc6f66 2582
221b804f
DB
2583 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2584 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2585 }
17c613ef 2586#endif /* defined(COMPILE_MMX2) */
f40c7dbb 2587 } // initialize horizontal stuff
28bf81c9
MN
2588
2589
2590
221b804f
DB
2591 /* precalculate vertical scaler filter coefficients */
2592 {
2593 const int filterAlign=
2594 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2595 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2596 1;
8c266f0c 2597
221b804f 2598 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
5fe4aad7 2599 srcH , dstH, filterAlign, (1<<12),
221b804f
DB
2600 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2601 srcFilter->lumV, dstFilter->lumV, c->param);
2602 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
5fe4aad7 2603 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
221b804f
DB
2604 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2605 srcFilter->chrV, dstFilter->chrV, c->param);
d33d485e 2606
b63f641e 2607#if HAVE_ALTIVEC
221b804f
DB
2608 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2609 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2610
2611 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2612 int j;
2613 short *p = (short *)&c->vYCoeffsBank[i];
2614 for (j=0;j<8;j++)
2615 p[j] = c->vLumFilter[i];
2616 }
2617
2618 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2619 int j;
2620 short *p = (short *)&c->vCCoeffsBank[i];
2621 for (j=0;j<8;j++)
2622 p[j] = c->vChrFilter[i];
2623 }
d33d485e 2624#endif
221b804f
DB
2625 }
2626
f40c7dbb 2627 // calculate buffer sizes so that they won't run out while handling these damn slices
221b804f
DB
2628 c->vLumBufSize= c->vLumFilterSize;
2629 c->vChrBufSize= c->vChrFilterSize;
2630 for (i=0; i<dstH; i++)
2631 {
2632 int chrI= i*c->chrDstH / dstH;
2633 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2634 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2635
2636 nextSlice>>= c->chrSrcVSubSample;
2637 nextSlice<<= c->chrSrcVSubSample;
2638 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
e5091488 2639 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
221b804f
DB
2640 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2641 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2642 }
2643
2644 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2645 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2646 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
f40c7dbb 2647 //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
2648 /* align at 16 bytes for AltiVec */
2649 for (i=0; i<c->vLumBufSize; i++)
8b2fce0d 2650 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
221b804f 2651 for (i=0; i<c->vChrBufSize; i++)
8b2fce0d 2652 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
221b804f
DB
2653
2654 //try to avoid drawing green stuff between the right end and the stride end
8b2fce0d
MN
2655 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2656
5a352b14 2657 assert(2*VOFW == VOF);
221b804f 2658
fcc402b1 2659 assert(c->chrDstH <= dstH);
221b804f
DB
2660
2661 if (flags&SWS_PRINT_INFO)
2662 {
28bf81c9 2663#ifdef DITHER1XBPP
3e62b7e3 2664 const char *dither= " dithered";
5521b193 2665#else
3e62b7e3 2666 const char *dither= "";
28bf81c9 2667#endif
221b804f 2668 if (flags&SWS_FAST_BILINEAR)
4b0c30b7 2669 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
221b804f 2670 else if (flags&SWS_BILINEAR)
4b0c30b7 2671 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
221b804f 2672 else if (flags&SWS_BICUBIC)
4b0c30b7 2673 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
221b804f 2674 else if (flags&SWS_X)
4b0c30b7 2675 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
221b804f 2676 else if (flags&SWS_POINT)
4b0c30b7 2677 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
221b804f 2678 else if (flags&SWS_AREA)
4b0c30b7 2679 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
221b804f 2680 else if (flags&SWS_BICUBLIN)
4b0c30b7 2681 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
221b804f 2682 else if (flags&SWS_GAUSS)
4b0c30b7 2683 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
221b804f 2684 else if (flags&SWS_SINC)
4b0c30b7 2685 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
221b804f 2686 else if (flags&SWS_LANCZOS)
4b0c30b7 2687 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
221b804f 2688 else if (flags&SWS_SPLINE)
4b0c30b7 2689 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
221b804f 2690 else
4b0c30b7 2691 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
221b804f
DB
2692
2693 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2694 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2695 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2696 else
2697 av_log(c, AV_LOG_INFO, "from %s to %s ",
2698 sws_format_name(srcFormat), sws_format_name(dstFormat));
2699
2700 if (flags & SWS_CPU_CAPS_MMX2)
2701 av_log(c, AV_LOG_INFO, "using MMX2\n");
2702 else if (flags & SWS_CPU_CAPS_3DNOW)
2703 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2704 else if (flags & SWS_CPU_CAPS_MMX)
2705 av_log(c, AV_LOG_INFO, "using MMX\n");
2706 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2707 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2708 else
2709 av_log(c, AV_LOG_INFO, "using C\n");
2710 }
2711
2712 if (flags & SWS_PRINT_INFO)
2713 {
2714 if (flags & SWS_CPU_CAPS_MMX)
2715 {
2716 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
4b0c30b7 2717 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
221b804f
DB
2718 else
2719 {
2720 if (c->hLumFilterSize==4)
4b0c30b7 2721 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2722 else if (c->hLumFilterSize==8)
4b0c30b7 2723 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2724 else
4b0c30b7 2725 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
221b804f
DB
2726
2727 if (c->hChrFilterSize==4)
4b0c30b7 2728 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2729 else if (c->hChrFilterSize==8)
4b0c30b7 2730 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2731 else
4b0c30b7 2732 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
221b804f
DB
2733 }
2734 }
2735 else
2736 {
b63f641e 2737#if ARCH_X86
f40c7dbb 2738 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
28bf81c9 2739#else
221b804f 2740 if (flags & SWS_FAST_BILINEAR)
4b0c30b7 2741 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
221b804f 2742 else
4b0c30b7 2743 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
28bf81c9 2744#endif
221b804f
DB
2745 }
2746 if (isPlanarYUV(dstFormat))
2747 {
2748 if (c->vLumFilterSize==1)
4b0c30b7 2749 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 2750 else
4b0c30b7 2751 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
2752 }
2753 else
2754 {
2755 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
4b0c30b7
BC
2756 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2757 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2758 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
4b0c30b7 2759 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 2760 else
4b0c30b7 2761 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
2762 }
2763
2764 if (dstFormat==PIX_FMT_BGR24)
f40c7dbb 2765 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
221b804f
DB
2766 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2767 else if (dstFormat==PIX_FMT_RGB32)
f40c7dbb 2768 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2769 else if (dstFormat==PIX_FMT_BGR565)
f40c7dbb 2770 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2771 else if (dstFormat==PIX_FMT_BGR555)
f40c7dbb 2772 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2773
4b0c30b7 2774 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
221b804f
DB
2775 }
2776 if (flags & SWS_PRINT_INFO)
2777 {
f40c7dbb 2778 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f 2779 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
f40c7dbb 2780 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f
DB
2781 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2782 }
2783
2784 c->swScale= getSwsFunc(flags);
2785 return c;
28bf81c9
MN
2786}
2787
2788/**
56b69633 2789 * swscale wrapper, so we don't need to export the SwsContext.
f40c7dbb 2790 * Assumes planar YUV to be in YUV order instead of YVU.
fccb9b2b 2791 */
703b56fb 2792int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2793 int srcSliceH, uint8_t* dst[], int dstStride[]){
2794 int i;
6208d676 2795 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
14623020 2796
221b804f 2797 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3f0bc115 2798 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
221b804f
DB
2799 return 0;
2800 }
2801 if (c->sliceDir == 0) {
2802 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2803 }
2804
49004617 2805 if (usePal(c->srcFormat)){
221b804f 2806 for (i=0; i<256; i++){
14623020
MN
2807 int p, r, g, b,y,u,v;
2808 if(c->srcFormat == PIX_FMT_PAL8){
2809 p=((uint32_t*)(src[1]))[i];
2810 r= (p>>16)&0xFF;
2811 g= (p>> 8)&0xFF;
2812 b= p &0xFF;
2813 }else if(c->srcFormat == PIX_FMT_RGB8){
2814 r= (i>>5 )*36;
2815 g= ((i>>2)&7)*36;
2816 b= (i&3 )*85;
2817 }else if(c->srcFormat == PIX_FMT_BGR8){
2818 b= (i>>6 )*85;
2819 g= ((i>>3)&7)*36;
2820 r= (i&7 )*36;
2821 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2822 r= (i>>3 )*255;
2823 g= ((i>>1)&3)*85;
2824 b= (i&1 )*255;
75add6fb
VS
2825 }else {
2826 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
14623020
MN
2827 b= (i>>3 )*255;
2828 g= ((i>>1)&3)*85;
2829 r= (i&1 )*255;
2830 }
cbcb408f
MN
2831 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2832 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2833 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
49004617 2834 c->pal_yuv[i]= y + (u<<8) + (v<<16);
522ce957
VS
2835
2836
2837 switch(c->dstFormat) {
2838 case PIX_FMT_BGR32:
2839#ifndef WORDS_BIGENDIAN
2840 case PIX_FMT_RGB24:
2841#endif
2842 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2843 break;
2844 case PIX_FMT_BGR32_1:
2845#ifdef WORDS_BIGENDIAN
2846 case PIX_FMT_BGR24:
2847#endif
2848 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2849 break;
2850 case PIX_FMT_RGB32_1:
2851#ifdef WORDS_BIGENDIAN
2852 case PIX_FMT_RGB24:
2853#endif
2854 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2855 break;
2856 case PIX_FMT_RGB32:
2857#ifndef WORDS_BIGENDIAN
2858 case PIX_FMT_BGR24:
2859#endif
2860 default:
2861 c->pal_rgb[i]= b + (g<<8) + (r<<16);
2862 }
21c08a3f 2863 }
221b804f 2864 }
21c08a3f 2865
221b804f
DB
2866 // copy strides, so they can safely be modified
2867 if (c->sliceDir == 1) {
2868 // slices go from top to bottom
6208d676
CS
2869 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2870 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
221b804f
DB
2871 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2872 } else {
2873 // slices go from bottom to top => we flip the image internally
2874 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2875 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
6208d676
CS
2876 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2],
2877 dst[3] + (c->dstH-1)*dstStride[3]};
2878 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2879 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
221b804f
DB
2880
2881 src2[0] += (srcSliceH-1)*srcStride[0];
49004617 2882 if (!usePal(c->srcFormat))
221b804f
DB
2883 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2884 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
6208d676 2885 src2[3] += (srcSliceH-1)*srcStride[3];
221b804f
DB
2886
2887 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2888 }
fccb9b2b
MN
2889}
2890
c099766f 2891#if LIBSWSCALE_VERSION_MAJOR < 1
703b56fb 2892int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2893 int srcSliceH, uint8_t* dst[], int dstStride[]){
2894 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
d4e24275 2895}
c099766f 2896#endif
d4e24275 2897
6a4970ab 2898SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
221b804f
DB
2899 float lumaSharpen, float chromaSharpen,
2900 float chromaHShift, float chromaVShift,
2901 int verbose)
e21206a8 2902{
221b804f
DB
2903 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2904
2905 if (lumaGBlur!=0.0){
2906 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2907 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2908 }else{
2909 filter->lumH= sws_getIdentityVec();
2910 filter->lumV= sws_getIdentityVec();
2911 }
2912
2913 if (chromaGBlur!=0.0){
2914 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2915 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2916 }else{
2917 filter->chrH= sws_getIdentityVec();
2918 filter->chrV= sws_getIdentityVec();
2919 }
2920
2921 if (chromaSharpen!=0.0){
2922 SwsVector *id= sws_getIdentityVec();
2923 sws_scaleVec(filter->chrH, -chromaSharpen);
2924 sws_scaleVec(filter->chrV, -chromaSharpen);
2925 sws_addVec(filter->chrH, id);
2926 sws_addVec(filter->chrV, id);
2927 sws_freeVec(id);
2928 }
2929
2930 if (lumaSharpen!=0.0){
2931 SwsVector *id= sws_getIdentityVec();
2932 sws_scaleVec(filter->lumH, -lumaSharpen);
2933 sws_scaleVec(filter->lumV, -lumaSharpen);
2934 sws_addVec(filter->lumH, id);
2935 sws_addVec(filter->lumV, id);
2936 sws_freeVec(id);
2937 }
2938
2939 if (chromaHShift != 0.0)
2940 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2941
2942 if (chromaVShift != 0.0)
2943 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2944
2945 sws_normalizeVec(filter->chrH, 1.0);
2946 sws_normalizeVec(filter->chrV, 1.0);
2947 sws_normalizeVec(filter->lumH, 1.0);
2948 sws_normalizeVec(filter->lumV, 1.0);
2949
e14ff1e6
SS
2950 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2951 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
221b804f
DB
2952
2953 return filter;
e21206a8
MN
2954}
2955
d4e24275 2956SwsVector *sws_getGaussianVec(double variance, double quality){
221b804f
DB
2957 const int length= (int)(variance*quality + 0.5) | 1;
2958 int i;
2959 double *coeff= av_malloc(length*sizeof(double));
2960 double middle= (length-1)*0.5;
2961 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2962
221b804f
DB
2963 vec->coeff= coeff;
2964 vec->length= length;
28bf81c9 2965
221b804f
DB
2966 for (i=0; i<length; i++)
2967 {
2968 double dist= i-middle;
e5091488 2969 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
221b804f 2970 }
28bf81c9 2971
221b804f 2972 sws_normalizeVec(vec, 1.0);
c7f822d9 2973
221b804f 2974 return vec;
28bf81c9
MN
2975}
2976
d4e24275 2977SwsVector *sws_getConstVec(double c, int length){
221b804f
DB
2978 int i;
2979 double *coeff= av_malloc(length*sizeof(double));
2980 SwsVector *vec= av_malloc(sizeof(SwsVector));
5521b193 2981
221b804f
DB
2982 vec->coeff= coeff;
2983 vec->length= length;
5521b193 2984
221b804f
DB
2985 for (i=0; i<length; i++)
2986 coeff[i]= c;
5521b193 2987
221b804f 2988 return vec;
5521b193
MN
2989}
2990
2991
d4e24275 2992SwsVector *sws_getIdentityVec(void){
221b804f 2993 return sws_getConstVec(1.0, 1);
c7f822d9
MN
2994}
2995
2e728364 2996double sws_dcVec(SwsVector *a){
221b804f
DB
2997 int i;
2998 double sum=0;
28bf81c9 2999
221b804f
DB
3000 for (i=0; i<a->length; i++)
3001 sum+= a->coeff[i];
28bf81c9 3002
221b804f 3003 return sum;
28bf81c9
MN
3004}
3005
d4e24275 3006void sws_scaleVec(SwsVector *a, double scalar){
221b804f 3007 int i;
c7f822d9 3008
221b804f
DB
3009 for (i=0; i<a->length; i++)
3010 a->coeff[i]*= scalar;
c7f822d9
MN
3011}
3012
2e728364 3013void sws_normalizeVec(SwsVector *a, double height){
221b804f 3014 sws_scaleVec(a, height/sws_dcVec(a));
2e728364
MN
3015}
3016
d4e24275 3017static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
221b804f
DB
3018 int length= a->length + b->length - 1;
3019 double *coeff= av_malloc(length*sizeof(double));
3020 int i, j;
3021 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3022
221b804f
DB
3023 vec->coeff= coeff;
3024 vec->length= length;
28bf81c9 3025
221b804f 3026 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 3027
221b804f
DB
3028 for (i=0; i<a->length; i++)
3029 {
3030 for (j=0; j<b->length; j++)
3031 {
3032 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3033 }
3034 }
28bf81c9 3035
221b804f 3036 return vec;
28bf81c9
MN
3037}
3038
d4e24275 3039static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
221b804f
DB
3040 int length= FFMAX(a->length, b->length);
3041 double *coeff= av_malloc(length*sizeof(double));
3042 int i;
3043 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3044
221b804f
DB
3045 vec->coeff= coeff;
3046 vec->length= length;
28bf81c9 3047
221b804f 3048 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 3049
221b804f
DB
3050 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3051 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
c7f822d9 3052
221b804f 3053 return vec;
28bf81c9 3054}
c7f822d9 3055
d4e24275 3056static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
221b804f
DB
3057 int length= FFMAX(a->length, b->length);
3058 double *coeff= av_malloc(length*sizeof(double));
3059 int i;
3060 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3061
221b804f
DB
3062 vec->coeff= coeff;
3063 vec->length= length;
c7f822d9 3064
221b804f 3065 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 3066
221b804f
DB
3067 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3068 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
c7f822d9 3069
221b804f 3070 return vec;
c7f822d9
MN
3071}
3072
3073/* shift left / or right if "shift" is negative */
d4e24275 3074static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
221b804f
DB
3075 int length= a->length + FFABS(shift)*2;
3076 double *coeff= av_malloc(length*sizeof(double));
3077 int i;
3078 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 3079
221b804f
DB
3080 vec->coeff= coeff;
3081 vec->length= length;
c7f822d9 3082
221b804f 3083 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 3084
221b804f
DB
3085 for (i=0; i<a->length; i++)
3086 {
3087 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3088 }
c7f822d9 3089
221b804f 3090 return vec;
c7f822d9
MN
3091}
3092
d4e24275 3093void sws_shiftVec(SwsVector *a, int shift){
221b804f
DB
3094 SwsVector *shifted= sws_getShiftedVec(a, shift);
3095 av_free(a->coeff);
3096 a->coeff= shifted->coeff;
3097 a->length= shifted->length;
3098 av_free(shifted);
5cebb24b
MN
3099}
3100
d4e24275 3101void sws_addVec(SwsVector *a, SwsVector *b){
221b804f
DB
3102 SwsVector *sum= sws_sumVec(a, b);
3103 av_free(a->coeff);
3104 a->coeff= sum->coeff;
3105 a->length= sum->length;
3106 av_free(sum);
5cebb24b
MN
3107}
3108
d4e24275 3109void sws_subVec(SwsVector *a, SwsVector *b){
221b804f
DB
3110 SwsVector *diff= sws_diffVec(a, b);
3111 av_free(a->coeff);
3112 a->coeff= diff->coeff;
3113 a->length= diff->length;
3114 av_free(diff);
5cebb24b
MN
3115}
3116
d4e24275 3117void sws_convVec(SwsVector *a, SwsVector *b){
221b804f
DB
3118 SwsVector *conv= sws_getConvVec(a, b);
3119 av_free(a->coeff);
3120 a->coeff= conv->coeff;
3121 a->length= conv->length;
3122 av_free(conv);
5cebb24b
MN
3123}
3124
d4e24275 3125SwsVector *sws_cloneVec(SwsVector *a){
221b804f
DB
3126 double *coeff= av_malloc(a->length*sizeof(double));
3127 int i;
3128 SwsVector *vec= av_malloc(sizeof(SwsVector));
5cebb24b 3129
221b804f
DB
3130 vec->coeff= coeff;
3131 vec->length= a->length;
5cebb24b 3132
221b804f 3133 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
5cebb24b 3134
221b804f 3135 return vec;
5cebb24b
MN
3136}
3137
26b40a65 3138void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
221b804f
DB
3139 int i;
3140 double max=0;
3141 double min=0;
3142 double range;
3143
3144 for (i=0; i<a->length; i++)
3145 if (a->coeff[i]>max) max= a->coeff[i];
3146
3147 for (i=0; i<a->length; i++)
3148 if (a->coeff[i]<min) min= a->coeff[i];
3149
3150 range= max - min;
3151
3152 for (i=0; i<a->length; i++)
3153 {
3154 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
26b40a65
SS
3155 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3156 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3157 av_log(log_ctx, log_level, "|\n");
221b804f 3158 }
c7f822d9
MN
3159}
3160
26b40a65
SS
3161#if LIBSWSCALE_VERSION_MAJOR < 1
3162void sws_printVec(SwsVector *a){
3163 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3164}
3165#endif
3166
d4e24275 3167void sws_freeVec(SwsVector *a){
221b804f 3168 if (!a) return;
47b7382d 3169 av_freep(&a->coeff);
221b804f
DB
3170 a->length=0;
3171 av_free(a);
c7f822d9
MN
3172}
3173
e21206a8 3174void sws_freeFilter(SwsFilter *filter){
221b804f 3175 if (!filter) return;
e21206a8 3176
221b804f
DB
3177 if (filter->lumH) sws_freeVec(filter->lumH);
3178 if (filter->lumV) sws_freeVec(filter->lumV);
3179 if (filter->chrH) sws_freeVec(filter->chrH);
3180 if (filter->chrV) sws_freeVec(filter->chrV);
3181 av_free(filter);
e21206a8
MN
3182}
3183
3184
d4e24275 3185void sws_freeContext(SwsContext *c){
221b804f
DB
3186 int i;
3187 if (!c) return;
3188
3189 if (c->lumPixBuf)
3190 {
3191 for (i=0; i<c->vLumBufSize; i++)
47b7382d
MN
3192 av_freep(&c->lumPixBuf[i]);
3193 av_freep(&c->lumPixBuf);
221b804f
DB
3194 }
3195
3196 if (c->chrPixBuf)
3197 {
3198 for (i=0; i<c->vChrBufSize; i++)
47b7382d
MN
3199 av_freep(&c->chrPixBuf[i]);
3200 av_freep(&c->chrPixBuf);
221b804f
DB
3201 }
3202
47b7382d
MN
3203 av_freep(&c->vLumFilter);
3204 av_freep(&c->vChrFilter);
3205 av_freep(&c->hLumFilter);
3206 av_freep(&c->hChrFilter);
b63f641e 3207#if HAVE_ALTIVEC
47b7382d
MN
3208 av_freep(&c->vYCoeffsBank);
3209 av_freep(&c->vCCoeffsBank);
d33d485e 3210#endif
c7f822d9 3211
47b7382d
MN
3212 av_freep(&c->vLumFilterPos);
3213 av_freep(&c->vChrFilterPos);
3214 av_freep(&c->hLumFilterPos);
3215 av_freep(&c->hChrFilterPos);
c7f822d9 3216
b63f641e 3217#if ARCH_X86 && CONFIG_GPL
0ce5275d 3218#ifdef MAP_ANONYMOUS
16a3717f 3219 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
221b804f 3220 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
dd35beb2
RP
3221#elif HAVE_VIRTUALALLOC
3222 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3223 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
38d5c282 3224#else
16a3717f 3225 av_free(c->funnyYCode );
221b804f 3226 av_free(c->funnyUVCode);
38d5c282 3227#endif
221b804f
DB
3228 c->funnyYCode=NULL;
3229 c->funnyUVCode=NULL;
7a24ec50 3230#endif /* ARCH_X86 && CONFIG_GPL */
38d5c282 3231
47b7382d
MN
3232 av_freep(&c->lumMmx2Filter);
3233 av_freep(&c->chrMmx2Filter);
3234 av_freep(&c->lumMmx2FilterPos);
3235 av_freep(&c->chrMmx2FilterPos);
3236 av_freep(&c->yuvTable);
221b804f
DB
3237
3238 av_free(c);
c7f822d9
MN
3239}
3240
22e46959 3241struct SwsContext *sws_getCachedContext(struct SwsContext *context,
58e4b706
CEH
3242 int srcW, int srcH, enum PixelFormat srcFormat,
3243 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
221b804f 3244 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
22e46959 3245{
337a2a45
KMH
3246 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3247
3248 if (!param)
3249 param = default_param;
3250
1b0a4572 3251 if (context) {
b8db4e22
BF
3252 if (context->srcW != srcW || context->srcH != srcH ||
3253 context->srcFormat != srcFormat ||
3254 context->dstW != dstW || context->dstH != dstH ||
3255 context->dstFormat != dstFormat || context->flags != flags ||
3256 context->param[0] != param[0] || context->param[1] != param[1])
22e46959
VP
3257 {
3258 sws_freeContext(context);
3259 context = NULL;
3260 }
3261 }
1b0a4572 3262 if (!context) {
22e46959 3263 return sws_getContext(srcW, srcH, srcFormat,
221b804f
DB
3264 dstW, dstH, dstFormat, flags,
3265 srcFilter, dstFilter, param);
22e46959
VP
3266 }
3267 return context;
3268}
3269