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