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