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