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