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