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