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