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