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