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