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