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