4d3b893b45bf7453f9d1a1e587eaac1437110a95
[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 if (flags&SWS_BICUBIC) filterSize= 4;
1454 else if (flags&SWS_X ) filterSize= 4;
1455 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1456 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1457
1458 xDstInSrc= xInc/2 - 0x8000;
1459 for (i=0; i<dstW; i++)
1460 {
1461 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1462 int j;
1463
1464 (*filterPos)[i]= xx;
1465 //bilinear upscale / linear interpolate / area averaging
1466 for (j=0; j<filterSize; j++)
1467 {
1468 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1469 if (coeff<0) coeff=0;
1470 filter[i*filterSize + j]= coeff;
1471 xx++;
1472 }
1473 xDstInSrc+= xInc;
1474 }
1475 }
1476 else
1477 {
1478 int xDstInSrc;
1479 int sizeFactor;
1480
1481 if (flags&SWS_BICUBIC) sizeFactor= 4;
1482 else if (flags&SWS_X) sizeFactor= 8;
1483 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1484 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1485 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1486 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1487 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1488 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1489 else {
1490 sizeFactor= 0; //GCC warning killer
1491 assert(0);
1492 }
1493
1494 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1495 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1496
1497 if (filterSize > srcW-2) filterSize=srcW-2;
1498
1499 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1500
1501 xDstInSrc= xInc - 0x10000;
1502 for (i=0; i<dstW; i++)
1503 {
1504 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1505 int j;
1506 (*filterPos)[i]= xx;
1507 for (j=0; j<filterSize; j++)
1508 {
1509 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1510 double floatd;
1511 int64_t coeff;
1512
1513 if (xInc > 1<<16)
1514 d= d*dstW/srcW;
1515 floatd= d * (1.0/(1<<30));
1516
1517 if (flags & SWS_BICUBIC)
1518 {
1519 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1520 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1521 int64_t dd = ( d*d)>>30;
1522 int64_t ddd= (dd*d)>>30;
1523
1524 if (d < 1LL<<30)
1525 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1526 else if (d < 1LL<<31)
1527 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1528 else
1529 coeff=0.0;
1530 coeff *= fone>>(30+24);
1531 }
1532 /* else if (flags & SWS_X)
1533 {
1534 double p= param ? param*0.01 : 0.3;
1535 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1536 coeff*= pow(2.0, - p*d*d);
1537 }*/
1538 else if (flags & SWS_X)
1539 {
1540 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1541 double c;
1542
1543 if (floatd<1.0)
1544 c = cos(floatd*PI);
1545 else
1546 c=-1.0;
1547 if (c<0.0) c= -pow(-c, A);
1548 else c= pow( c, A);
1549 coeff= (c*0.5 + 0.5)*fone;
1550 }
1551 else if (flags & SWS_AREA)
1552 {
1553 int64_t d2= d - (1<<29);
1554 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1555 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1556 else coeff=0.0;
1557 coeff *= fone>>(30+16);
1558 }
1559 else if (flags & SWS_GAUSS)
1560 {
1561 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1562 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1563 }
1564 else if (flags & SWS_SINC)
1565 {
1566 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1567 }
1568 else if (flags & SWS_LANCZOS)
1569 {
1570 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1571 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1572 if (floatd>p) coeff=0;
1573 }
1574 else if (flags & SWS_BILINEAR)
1575 {
1576 coeff= (1<<30) - d;
1577 if (coeff<0) coeff=0;
1578 coeff *= fone >> 30;
1579 }
1580 else if (flags & SWS_SPLINE)
1581 {
1582 double p=-2.196152422706632;
1583 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1584 }
1585 else {
1586 coeff= 0.0; //GCC warning killer
1587 assert(0);
1588 }
1589
1590 filter[i*filterSize + j]= coeff;
1591 xx++;
1592 }
1593 xDstInSrc+= 2*xInc;
1594 }
1595 }
1596
1597 /* apply src & dst Filter to filter -> filter2
1598 av_free(filter);
1599 */
1600 assert(filterSize>0);
1601 filter2Size= filterSize;
1602 if (srcFilter) filter2Size+= srcFilter->length - 1;
1603 if (dstFilter) filter2Size+= dstFilter->length - 1;
1604 assert(filter2Size>0);
1605 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1606
1607 for (i=0; i<dstW; i++)
1608 {
1609 int j, k;
1610
1611 if(srcFilter){
1612 for (k=0; k<srcFilter->length; k++){
1613 for (j=0; j<filterSize; j++)
1614 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1615 }
1616 }else{
1617 for (j=0; j<filterSize; j++)
1618 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1619 }
1620 //FIXME dstFilter
1621
1622 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1623 }
1624 av_freep(&filter);
1625
1626 /* try to reduce the filter-size (step1 find size and shift left) */
1627 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1628 minFilterSize= 0;
1629 for (i=dstW-1; i>=0; i--)
1630 {
1631 int min= filter2Size;
1632 int j;
1633 int64_t cutOff=0.0;
1634
1635 /* get rid off near zero elements on the left by shifting left */
1636 for (j=0; j<filter2Size; j++)
1637 {
1638 int k;
1639 cutOff += FFABS(filter2[i*filter2Size]);
1640
1641 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1642
1643 /* preserve monotonicity because the core can't handle the filter otherwise */
1644 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1645
1646 // move filter coefficients left
1647 for (k=1; k<filter2Size; k++)
1648 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1649 filter2[i*filter2Size + k - 1]= 0;
1650 (*filterPos)[i]++;
1651 }
1652
1653 cutOff=0;
1654 /* count near zeros on the right */
1655 for (j=filter2Size-1; j>0; j--)
1656 {
1657 cutOff += FFABS(filter2[i*filter2Size + j]);
1658
1659 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1660 min--;
1661 }
1662
1663 if (min>minFilterSize) minFilterSize= min;
1664 }
1665
1666 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1667 // we can handle the special case 4,
1668 // so we don't want to go to the full 8
1669 if (minFilterSize < 5)
1670 filterAlign = 4;
1671
1672 // We really don't want to waste our time
1673 // doing useless computation, so fall back on
1674 // the scalar C code for very small filters.
1675 // Vectorizing is worth it only if you have a
1676 // decent-sized vector.
1677 if (minFilterSize < 3)
1678 filterAlign = 1;
1679 }
1680
1681 if (flags & SWS_CPU_CAPS_MMX) {
1682 // special case for unscaled vertical filtering
1683 if (minFilterSize == 1 && filterAlign == 2)
1684 filterAlign= 1;
1685 }
1686
1687 assert(minFilterSize > 0);
1688 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1689 assert(filterSize > 0);
1690 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1691 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1692 goto error;
1693 *outFilterSize= filterSize;
1694
1695 if (flags&SWS_PRINT_INFO)
1696 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1697 /* try to reduce the filter-size (step2 reduce it) */
1698 for (i=0; i<dstW; i++)
1699 {
1700 int j;
1701
1702 for (j=0; j<filterSize; j++)
1703 {
1704 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1705 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1706 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1707 filter[i*filterSize + j]= 0;
1708 }
1709 }
1710
1711
1712 //FIXME try to align filterPos if possible
1713
1714 //fix borders
1715 for (i=0; i<dstW; i++)
1716 {
1717 int j;
1718 if ((*filterPos)[i] < 0)
1719 {
1720 // move filter coefficients left to compensate for filterPos
1721 for (j=1; j<filterSize; j++)
1722 {
1723 int left= FFMAX(j + (*filterPos)[i], 0);
1724 filter[i*filterSize + left] += filter[i*filterSize + j];
1725 filter[i*filterSize + j]=0;
1726 }
1727 (*filterPos)[i]= 0;
1728 }
1729
1730 if ((*filterPos)[i] + filterSize > srcW)
1731 {
1732 int shift= (*filterPos)[i] + filterSize - srcW;
1733 // move filter coefficients right to compensate for filterPos
1734 for (j=filterSize-2; j>=0; j--)
1735 {
1736 int right= FFMIN(j + shift, filterSize-1);
1737 filter[i*filterSize +right] += filter[i*filterSize +j];
1738 filter[i*filterSize +j]=0;
1739 }
1740 (*filterPos)[i]= srcW - filterSize;
1741 }
1742 }
1743
1744 // Note the +1 is for the MMX scaler which reads over the end
1745 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1746 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1747
1748 /* normalize & store in outFilter */
1749 for (i=0; i<dstW; i++)
1750 {
1751 int j;
1752 int64_t error=0;
1753 int64_t sum=0;
1754
1755 for (j=0; j<filterSize; j++)
1756 {
1757 sum+= filter[i*filterSize + j];
1758 }
1759 sum= (sum + one/2)/ one;
1760 for (j=0; j<*outFilterSize; j++)
1761 {
1762 int64_t v= filter[i*filterSize + j] + error;
1763 int intV= ROUNDED_DIV(v, sum);
1764 (*outFilter)[i*(*outFilterSize) + j]= intV;
1765 error= v - intV*sum;
1766 }
1767 }
1768
1769 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1770 for (i=0; i<*outFilterSize; i++)
1771 {
1772 int j= dstW*(*outFilterSize);
1773 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1774 }
1775
1776 ret=0;
1777 error:
1778 av_free(filter);
1779 av_free(filter2);
1780 return ret;
1781 }
1782
1783 #ifdef COMPILE_MMX2
1784 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1785 {
1786 uint8_t *fragmentA;
1787 x86_reg imm8OfPShufW1A;
1788 x86_reg imm8OfPShufW2A;
1789 x86_reg fragmentLengthA;
1790 uint8_t *fragmentB;
1791 x86_reg imm8OfPShufW1B;
1792 x86_reg imm8OfPShufW2B;
1793 x86_reg fragmentLengthB;
1794 int fragmentPos;
1795
1796 int xpos, i;
1797
1798 // create an optimized horizontal scaling routine
1799
1800 //code fragment
1801
1802 __asm__ volatile(
1803 "jmp 9f \n\t"
1804 // Begin
1805 "0: \n\t"
1806 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1807 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1808 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1809 "punpcklbw %%mm7, %%mm1 \n\t"
1810 "punpcklbw %%mm7, %%mm0 \n\t"
1811 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1812 "1: \n\t"
1813 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1814 "2: \n\t"
1815 "psubw %%mm1, %%mm0 \n\t"
1816 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1817 "pmullw %%mm3, %%mm0 \n\t"
1818 "psllw $7, %%mm1 \n\t"
1819 "paddw %%mm1, %%mm0 \n\t"
1820
1821 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1822
1823 "add $8, %%"REG_a" \n\t"
1824 // End
1825 "9: \n\t"
1826 // "int $3 \n\t"
1827 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1828 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1829 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1830 "dec %1 \n\t"
1831 "dec %2 \n\t"
1832 "sub %0, %1 \n\t"
1833 "sub %0, %2 \n\t"
1834 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1835 "sub %0, %3 \n\t"
1836
1837
1838 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1839 "=r" (fragmentLengthA)
1840 );
1841
1842 __asm__ volatile(
1843 "jmp 9f \n\t"
1844 // Begin
1845 "0: \n\t"
1846 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1847 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1848 "punpcklbw %%mm7, %%mm0 \n\t"
1849 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1850 "1: \n\t"
1851 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1852 "2: \n\t"
1853 "psubw %%mm1, %%mm0 \n\t"
1854 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1855 "pmullw %%mm3, %%mm0 \n\t"
1856 "psllw $7, %%mm1 \n\t"
1857 "paddw %%mm1, %%mm0 \n\t"
1858
1859 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1860
1861 "add $8, %%"REG_a" \n\t"
1862 // End
1863 "9: \n\t"
1864 // "int $3 \n\t"
1865 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1866 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1867 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1868 "dec %1 \n\t"
1869 "dec %2 \n\t"
1870 "sub %0, %1 \n\t"
1871 "sub %0, %2 \n\t"
1872 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1873 "sub %0, %3 \n\t"
1874
1875
1876 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1877 "=r" (fragmentLengthB)
1878 );
1879
1880 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1881 fragmentPos=0;
1882
1883 for (i=0; i<dstW/numSplits; i++)
1884 {
1885 int xx=xpos>>16;
1886
1887 if ((i&3) == 0)
1888 {
1889 int a=0;
1890 int b=((xpos+xInc)>>16) - xx;
1891 int c=((xpos+xInc*2)>>16) - xx;
1892 int d=((xpos+xInc*3)>>16) - xx;
1893
1894 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1895 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1896 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1897 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1898 filterPos[i/2]= xx;
1899
1900 if (d+1<4)
1901 {
1902 int maxShift= 3-(d+1);
1903 int shift=0;
1904
1905 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1906
1907 funnyCode[fragmentPos + imm8OfPShufW1B]=
1908 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1909 funnyCode[fragmentPos + imm8OfPShufW2B]=
1910 a | (b<<2) | (c<<4) | (d<<6);
1911
1912 if (i+3>=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 + imm8OfPShufW1B]+= 0x55*shift;
1918 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1919 filterPos[i/2]-=shift;
1920 }
1921
1922 fragmentPos+= fragmentLengthB;
1923 }
1924 else
1925 {
1926 int maxShift= 3-d;
1927 int shift=0;
1928
1929 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1930
1931 funnyCode[fragmentPos + imm8OfPShufW1A]=
1932 funnyCode[fragmentPos + imm8OfPShufW2A]=
1933 a | (b<<2) | (c<<4) | (d<<6);
1934
1935 if (i+4>=dstW) shift=maxShift; //avoid overread
1936 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1937
1938 if (shift && i>=shift)
1939 {
1940 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1941 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1942 filterPos[i/2]-=shift;
1943 }
1944
1945 fragmentPos+= fragmentLengthA;
1946 }
1947
1948 funnyCode[fragmentPos]= RET;
1949 }
1950 xpos+=xInc;
1951 }
1952 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1953 }
1954 #endif /* COMPILE_MMX2 */
1955
1956 static void globalInit(void){
1957 // generating tables:
1958 int i;
1959 for (i=0; i<768; i++){
1960 int c= av_clip_uint8(i-256);
1961 clip_table[i]=c;
1962 }
1963 }
1964
1965 static SwsFunc getSwsFunc(SwsContext *c)
1966 {
1967 #if CONFIG_RUNTIME_CPUDETECT
1968 int flags = c->flags;
1969
1970 #if ARCH_X86 && CONFIG_GPL
1971 // ordered per speed fastest first
1972 if (flags & SWS_CPU_CAPS_MMX2) {
1973 sws_init_swScale_MMX2(c);
1974 return swScale_MMX2;
1975 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1976 sws_init_swScale_3DNow(c);
1977 return swScale_3DNow;
1978 } else if (flags & SWS_CPU_CAPS_MMX) {
1979 sws_init_swScale_MMX(c);
1980 return swScale_MMX;
1981 } else {
1982 sws_init_swScale_C(c);
1983 return swScale_C;
1984 }
1985
1986 #else
1987 #if ARCH_PPC
1988 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1989 sws_init_swScale_altivec(c);
1990 return swScale_altivec;
1991 } else {
1992 sws_init_swScale_C(c);
1993 return swScale_C;
1994 }
1995 #endif
1996 sws_init_swScale_C(c);
1997 return swScale_C;
1998 #endif /* ARCH_X86 && CONFIG_GPL */
1999 #else //CONFIG_RUNTIME_CPUDETECT
2000 #if COMPILE_TEMPLATE_MMX2
2001 sws_init_swScale_MMX2(c);
2002 return swScale_MMX2;
2003 #elif COMPILE_TEMPLATE_AMD3DNOW
2004 sws_init_swScale_3DNow(c);
2005 return swScale_3DNow;
2006 #elif COMPILE_TEMPLATE_MMX
2007 sws_init_swScale_MMX(c);
2008 return swScale_MMX;
2009 #elif COMPILE_TEMPLATE_ALTIVEC
2010 sws_init_swScale_altivec(c);
2011 return swScale_altivec;
2012 #else
2013 sws_init_swScale_C(c);
2014 return swScale_C;
2015 #endif
2016 #endif //!CONFIG_RUNTIME_CPUDETECT
2017 }
2018
2019 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2020 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2021 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2022 /* Copy Y plane */
2023 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2024 memcpy(dst, src[0], srcSliceH*dstStride[0]);
2025 else
2026 {
2027 int i;
2028 const uint8_t *srcPtr= src[0];
2029 uint8_t *dstPtr= dst;
2030 for (i=0; i<srcSliceH; i++)
2031 {
2032 memcpy(dstPtr, srcPtr, c->srcW);
2033 srcPtr+= srcStride[0];
2034 dstPtr+= dstStride[0];
2035 }
2036 }
2037 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2038 if (c->dstFormat == PIX_FMT_NV12)
2039 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2040 else
2041 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2042
2043 return srcSliceH;
2044 }
2045
2046 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2047 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2048 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2049
2050 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2051
2052 return srcSliceH;
2053 }
2054
2055 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2056 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2057 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2058
2059 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2060
2061 return srcSliceH;
2062 }
2063
2064 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2065 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2066 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2067
2068 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2069
2070 return srcSliceH;
2071 }
2072
2073 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2074 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2075 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2076
2077 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2078
2079 return srcSliceH;
2080 }
2081
2082 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2083 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2084 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2085 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2086 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2087
2088 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2089
2090 if (dstParam[3])
2091 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2092
2093 return srcSliceH;
2094 }
2095
2096 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2097 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2098 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2099 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2100 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2101
2102 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2103
2104 return srcSliceH;
2105 }
2106
2107 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2108 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2109 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2110 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2111 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2112
2113 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2114
2115 if (dstParam[3])
2116 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2117
2118 return srcSliceH;
2119 }
2120
2121 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2122 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2123 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2124 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2125 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2126
2127 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2128
2129 return srcSliceH;
2130 }
2131
2132 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2133 int srcSliceH, uint8_t* dst[], int dstStride[]){
2134 const enum PixelFormat srcFormat= c->srcFormat;
2135 const enum PixelFormat dstFormat= c->dstFormat;
2136 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2137 const uint8_t *palette)=NULL;
2138 int i;
2139 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2140 uint8_t *srcPtr= src[0];
2141
2142 if (!usePal(srcFormat))
2143 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2144 sws_format_name(srcFormat), sws_format_name(dstFormat));
2145
2146 switch(dstFormat){
2147 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2148 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2149 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2150 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2151 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2152 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2153 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2154 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2155 }
2156
2157
2158 for (i=0; i<srcSliceH; i++) {
2159 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2160 srcPtr+= srcStride[0];
2161 dstPtr+= dstStride[0];
2162 }
2163
2164 return srcSliceH;
2165 }
2166
2167 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2168 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2169 int srcSliceH, uint8_t* dst[], int dstStride[]){
2170 const enum PixelFormat srcFormat= c->srcFormat;
2171 const enum PixelFormat dstFormat= c->dstFormat;
2172 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2173 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2174 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2175 const int dstId= fmt_depth(dstFormat) >> 2;
2176 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2177
2178 /* BGR -> BGR */
2179 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2180 || (isRGB(srcFormat) && isRGB(dstFormat))){
2181 switch(srcId | (dstId<<4)){
2182 case 0x34: conv= rgb16to15; break;
2183 case 0x36: conv= rgb24to15; break;
2184 case 0x38: conv= rgb32to15; break;
2185 case 0x43: conv= rgb15to16; break;
2186 case 0x46: conv= rgb24to16; break;
2187 case 0x48: conv= rgb32to16; break;
2188 case 0x63: conv= rgb15to24; break;
2189 case 0x64: conv= rgb16to24; break;
2190 case 0x68: conv= rgb32to24; break;
2191 case 0x83: conv= rgb15to32; break;
2192 case 0x84: conv= rgb16to32; break;
2193 case 0x86: conv= rgb24to32; break;
2194 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2195 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2196 }
2197 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2198 || (isRGB(srcFormat) && isBGR(dstFormat))){
2199 switch(srcId | (dstId<<4)){
2200 case 0x33: conv= rgb15tobgr15; break;
2201 case 0x34: conv= rgb16tobgr15; break;
2202 case 0x36: conv= rgb24tobgr15; break;
2203 case 0x38: conv= rgb32tobgr15; break;
2204 case 0x43: conv= rgb15tobgr16; break;
2205 case 0x44: conv= rgb16tobgr16; break;
2206 case 0x46: conv= rgb24tobgr16; break;
2207 case 0x48: conv= rgb32tobgr16; break;
2208 case 0x63: conv= rgb15tobgr24; break;
2209 case 0x64: conv= rgb16tobgr24; break;
2210 case 0x66: conv= rgb24tobgr24; break;
2211 case 0x68: conv= rgb32tobgr24; break;
2212 case 0x83: conv= rgb15tobgr32; break;
2213 case 0x84: conv= rgb16tobgr32; break;
2214 case 0x86: conv= rgb24tobgr32; break;
2215 case 0x88: conv= rgb32tobgr32; break;
2216 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2217 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2218 }
2219 }else{
2220 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2221 sws_format_name(srcFormat), sws_format_name(dstFormat));
2222 }
2223
2224 if(conv)
2225 {
2226 uint8_t *srcPtr= src[0];
2227 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2228 srcPtr += ALT32_CORR;
2229
2230 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2231 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2232 else
2233 {
2234 int i;
2235 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2236
2237 for (i=0; i<srcSliceH; i++)
2238 {
2239 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2240 srcPtr+= srcStride[0];
2241 dstPtr+= dstStride[0];
2242 }
2243 }
2244 }
2245 return srcSliceH;
2246 }
2247
2248 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2249 int srcSliceH, uint8_t* dst[], int dstStride[]){
2250
2251 rgb24toyv12(
2252 src[0],
2253 dst[0]+ srcSliceY *dstStride[0],
2254 dst[1]+(srcSliceY>>1)*dstStride[1],
2255 dst[2]+(srcSliceY>>1)*dstStride[2],
2256 c->srcW, srcSliceH,
2257 dstStride[0], dstStride[1], srcStride[0]);
2258 if (dst[3])
2259 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2260 return srcSliceH;
2261 }
2262
2263 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2264 int srcSliceH, uint8_t* dst[], int dstStride[]){
2265 int i;
2266
2267 /* copy Y */
2268 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2269 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2270 else{
2271 uint8_t *srcPtr= src[0];
2272 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2273
2274 for (i=0; i<srcSliceH; i++)
2275 {
2276 memcpy(dstPtr, srcPtr, c->srcW);
2277 srcPtr+= srcStride[0];
2278 dstPtr+= dstStride[0];
2279 }
2280 }
2281
2282 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2283 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2284 srcSliceH >> 2, srcStride[1], dstStride[1]);
2285 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2286 srcSliceH >> 2, srcStride[2], dstStride[2]);
2287 }else{
2288 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2289 srcSliceH >> 2, srcStride[1], dstStride[2]);
2290 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2291 srcSliceH >> 2, srcStride[2], dstStride[1]);
2292 }
2293 if (dst[3])
2294 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2295 return srcSliceH;
2296 }
2297
2298 /* unscaled copy like stuff (assumes nearly identical formats) */
2299 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2300 int srcSliceH, uint8_t* dst[], int dstStride[])
2301 {
2302 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2303 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2304 else
2305 {
2306 int i;
2307 uint8_t *srcPtr= src[0];
2308 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2309 int length=0;
2310
2311 /* universal length finder */
2312 while(length+c->srcW <= FFABS(dstStride[0])
2313 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2314 assert(length!=0);
2315
2316 for (i=0; i<srcSliceH; i++)
2317 {
2318 memcpy(dstPtr, srcPtr, length);
2319 srcPtr+= srcStride[0];
2320 dstPtr+= dstStride[0];
2321 }
2322 }
2323 return srcSliceH;
2324 }
2325
2326 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2327 int srcSliceH, uint8_t* dst[], int dstStride[])
2328 {
2329 int plane, i, j;
2330 for (plane=0; plane<4; plane++)
2331 {
2332 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2333 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2334 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2335 uint8_t *srcPtr= src[plane];
2336 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2337
2338 if (!dst[plane]) continue;
2339 // ignore palette for GRAY8
2340 if (plane == 1 && !dst[2]) continue;
2341 if (!src[plane] || (plane == 1 && !src[2])){
2342 if(is16BPS(c->dstFormat))
2343 length*=2;
2344 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2345 }else
2346 {
2347 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2348 if (!isBE(c->srcFormat)) srcPtr++;
2349 for (i=0; i<height; i++){
2350 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2351 srcPtr+= srcStride[plane];
2352 dstPtr+= dstStride[plane];
2353 }
2354 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2355 for (i=0; i<height; i++){
2356 for (j=0; j<length; j++){
2357 dstPtr[ j<<1 ] = srcPtr[j];
2358 dstPtr[(j<<1)+1] = srcPtr[j];
2359 }
2360 srcPtr+= srcStride[plane];
2361 dstPtr+= dstStride[plane];
2362 }
2363 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2364 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2365
2366 for (i=0; i<height; i++){
2367 for (j=0; j<length; j++)
2368 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2369 srcPtr+= srcStride[plane];
2370 dstPtr+= dstStride[plane];
2371 }
2372 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2373 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2374 else
2375 {
2376 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2377 length*=2;
2378 for (i=0; i<height; i++)
2379 {
2380 memcpy(dstPtr, srcPtr, length);
2381 srcPtr+= srcStride[plane];
2382 dstPtr+= dstStride[plane];
2383 }
2384 }
2385 }
2386 }
2387 return srcSliceH;
2388 }
2389
2390
2391 static void getSubSampleFactors(int *h, int *v, int format){
2392 switch(format){
2393 case PIX_FMT_UYVY422:
2394 case PIX_FMT_YUYV422:
2395 *h=1;
2396 *v=0;
2397 break;
2398 case PIX_FMT_YUV420P:
2399 case PIX_FMT_YUV420PLE:
2400 case PIX_FMT_YUV420PBE:
2401 case PIX_FMT_YUVA420P:
2402 case PIX_FMT_GRAY16BE:
2403 case PIX_FMT_GRAY16LE:
2404 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2405 case PIX_FMT_NV12:
2406 case PIX_FMT_NV21:
2407 *h=1;
2408 *v=1;
2409 break;
2410 case PIX_FMT_YUV440P:
2411 *h=0;
2412 *v=1;
2413 break;
2414 case PIX_FMT_YUV410P:
2415 *h=2;
2416 *v=2;
2417 break;
2418 case PIX_FMT_YUV444P:
2419 case PIX_FMT_YUV444PLE:
2420 case PIX_FMT_YUV444PBE:
2421 *h=0;
2422 *v=0;
2423 break;
2424 case PIX_FMT_YUV422P:
2425 case PIX_FMT_YUV422PLE:
2426 case PIX_FMT_YUV422PBE:
2427 *h=1;
2428 *v=0;
2429 break;
2430 case PIX_FMT_YUV411P:
2431 *h=2;
2432 *v=0;
2433 break;
2434 default:
2435 *h=0;
2436 *v=0;
2437 break;
2438 }
2439 }
2440
2441 static uint16_t roundToInt16(int64_t f){
2442 int r= (f + (1<<15))>>16;
2443 if (r<-0x7FFF) return 0x8000;
2444 else if (r> 0x7FFF) return 0x7FFF;
2445 else return r;
2446 }
2447
2448 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2449 int64_t crv = inv_table[0];
2450 int64_t cbu = inv_table[1];
2451 int64_t cgu = -inv_table[2];
2452 int64_t cgv = -inv_table[3];
2453 int64_t cy = 1<<16;
2454 int64_t oy = 0;
2455
2456 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2457 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2458
2459 c->brightness= brightness;
2460 c->contrast = contrast;
2461 c->saturation= saturation;
2462 c->srcRange = srcRange;
2463 c->dstRange = dstRange;
2464 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2465
2466 c->uOffset= 0x0400040004000400LL;
2467 c->vOffset= 0x0400040004000400LL;
2468
2469 if (!srcRange){
2470 cy= (cy*255) / 219;
2471 oy= 16<<16;
2472 }else{
2473 crv= (crv*224) / 255;
2474 cbu= (cbu*224) / 255;
2475 cgu= (cgu*224) / 255;
2476 cgv= (cgv*224) / 255;
2477 }
2478
2479 cy = (cy *contrast )>>16;
2480 crv= (crv*contrast * saturation)>>32;
2481 cbu= (cbu*contrast * saturation)>>32;
2482 cgu= (cgu*contrast * saturation)>>32;
2483 cgv= (cgv*contrast * saturation)>>32;
2484
2485 oy -= 256*brightness;
2486
2487 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2488 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2489 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2490 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2491 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2492 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2493
2494 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2495 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2496 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2497 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2498 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2499 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2500
2501 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2502 //FIXME factorize
2503
2504 #ifdef COMPILE_ALTIVEC
2505 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2506 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2507 #endif
2508 return 0;
2509 }
2510
2511 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2512 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2513
2514 *inv_table = c->srcColorspaceTable;
2515 *table = c->dstColorspaceTable;
2516 *srcRange = c->srcRange;
2517 *dstRange = c->dstRange;
2518 *brightness= c->brightness;
2519 *contrast = c->contrast;
2520 *saturation= c->saturation;
2521
2522 return 0;
2523 }
2524
2525 static int handle_jpeg(enum PixelFormat *format)
2526 {
2527 switch (*format) {
2528 case PIX_FMT_YUVJ420P:
2529 *format = PIX_FMT_YUV420P;
2530 return 1;
2531 case PIX_FMT_YUVJ422P:
2532 *format = PIX_FMT_YUV422P;
2533 return 1;
2534 case PIX_FMT_YUVJ444P:
2535 *format = PIX_FMT_YUV444P;
2536 return 1;
2537 case PIX_FMT_YUVJ440P:
2538 *format = PIX_FMT_YUV440P;
2539 return 1;
2540 default:
2541 return 0;
2542 }
2543 }
2544
2545 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2546 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2547 {
2548
2549 SwsContext *c;
2550 int i;
2551 int usesVFilter, usesHFilter;
2552 int unscaled, needsDither;
2553 int srcRange, dstRange;
2554 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2555 #if ARCH_X86
2556 if (flags & SWS_CPU_CAPS_MMX)
2557 __asm__ volatile("emms\n\t"::: "memory");
2558 #endif
2559
2560 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2561 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2562 #if COMPILE_TEMPLATE_MMX2
2563 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2564 #elif COMPILE_TEMPLATE_AMD3DNOW
2565 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2566 #elif COMPILE_TEMPLATE_MMX
2567 flags |= SWS_CPU_CAPS_MMX;
2568 #elif COMPILE_TEMPLATE_ALTIVEC
2569 flags |= SWS_CPU_CAPS_ALTIVEC;
2570 #elif ARCH_BFIN
2571 flags |= SWS_CPU_CAPS_BFIN;
2572 #endif
2573 #endif /* CONFIG_RUNTIME_CPUDETECT */
2574 if (clip_table[512] != 255) globalInit();
2575 if (!rgb15to16) sws_rgb2rgb_init(flags);
2576
2577 unscaled = (srcW == dstW && srcH == dstH);
2578 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2579 && (fmt_depth(dstFormat))<24
2580 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2581
2582 srcRange = handle_jpeg(&srcFormat);
2583 dstRange = handle_jpeg(&dstFormat);
2584
2585 if (!isSupportedIn(srcFormat))
2586 {
2587 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2588 return NULL;
2589 }
2590 if (!isSupportedOut(dstFormat))
2591 {
2592 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2593 return NULL;
2594 }
2595
2596 i= flags & ( SWS_POINT
2597 |SWS_AREA
2598 |SWS_BILINEAR
2599 |SWS_FAST_BILINEAR
2600 |SWS_BICUBIC
2601 |SWS_X
2602 |SWS_GAUSS
2603 |SWS_LANCZOS
2604 |SWS_SINC
2605 |SWS_SPLINE
2606 |SWS_BICUBLIN);
2607 if(!i || (i & (i-1)))
2608 {
2609 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2610 return NULL;
2611 }
2612
2613 /* sanity check */
2614 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
2615 {
2616 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2617 srcW, srcH, dstW, dstH);
2618 return NULL;
2619 }
2620 if(srcW > VOFW || dstW > VOFW){
2621 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2622 return NULL;
2623 }
2624
2625 if (!dstFilter) dstFilter= &dummyFilter;
2626 if (!srcFilter) srcFilter= &dummyFilter;
2627
2628 c= av_mallocz(sizeof(SwsContext));
2629
2630 c->av_class = &sws_context_class;
2631 c->srcW= srcW;
2632 c->srcH= srcH;
2633 c->dstW= dstW;
2634 c->dstH= dstH;
2635 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2636 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2637 c->flags= flags;
2638 c->dstFormat= dstFormat;
2639 c->srcFormat= srcFormat;
2640 c->vRounder= 4* 0x0001000100010001ULL;
2641
2642 usesHFilter= usesVFilter= 0;
2643 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2644 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2645 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2646 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2647 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2648 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2649 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2650 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2651
2652 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2653 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2654
2655 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2656 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2657
2658 // drop some chroma lines if the user wants it
2659 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2660 c->chrSrcVSubSample+= c->vChrDrop;
2661
2662 // drop every other pixel for chroma calculation unless user wants full chroma
2663 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2664 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2665 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2666 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2667 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2668 c->chrSrcHSubSample=1;
2669
2670 if (param){
2671 c->param[0] = param[0];
2672 c->param[1] = param[1];
2673 }else{
2674 c->param[0] =
2675 c->param[1] = SWS_PARAM_DEFAULT;
2676 }
2677
2678 // Note the -((-x)>>y) is so that we always round toward +inf.
2679 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2680 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2681 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2682 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2683
2684 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2685
2686 /* unscaled special cases */
2687 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2688 {
2689 /* yv12_to_nv12 */
2690 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2691 {
2692 c->swScale= PlanarToNV12Wrapper;
2693 }
2694 /* yuv2bgr */
2695 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2696 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2697 {
2698 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2699 }
2700
2701 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2702 {
2703 c->swScale= yvu9toyv12Wrapper;
2704 }
2705
2706 /* bgr24toYV12 */
2707 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2708 c->swScale= bgr24toyv12Wrapper;
2709
2710 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2711 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2712 && (isBGR(dstFormat) || isRGB(dstFormat))
2713 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2714 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2715 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2716 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2717 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2718 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2719 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2720 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2721 && dstFormat != PIX_FMT_RGB32_1
2722 && dstFormat != PIX_FMT_BGR32_1
2723 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2724 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2725 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2726 c->swScale= rgb2rgbWrapper;
2727
2728 if ((usePal(srcFormat) && (
2729 dstFormat == PIX_FMT_RGB32 ||
2730 dstFormat == PIX_FMT_RGB32_1 ||
2731 dstFormat == PIX_FMT_RGB24 ||
2732 dstFormat == PIX_FMT_BGR32 ||
2733 dstFormat == PIX_FMT_BGR32_1 ||
2734 dstFormat == PIX_FMT_BGR24)))
2735 c->swScale= pal2rgbWrapper;
2736
2737 if (srcFormat == PIX_FMT_YUV422P)
2738 {
2739 if (dstFormat == PIX_FMT_YUYV422)
2740 c->swScale= YUV422PToYuy2Wrapper;
2741 else if (dstFormat == PIX_FMT_UYVY422)
2742 c->swScale= YUV422PToUyvyWrapper;
2743 }
2744
2745 /* LQ converters if -sws 0 or -sws 4*/
2746 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2747 /* yv12_to_yuy2 */
2748 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2749 {
2750 if (dstFormat == PIX_FMT_YUYV422)
2751 c->swScale= PlanarToYuy2Wrapper;
2752 else if (dstFormat == PIX_FMT_UYVY422)
2753 c->swScale= PlanarToUyvyWrapper;
2754 }
2755 }
2756 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2757 c->swScale= YUYV2YUV420Wrapper;
2758 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2759 c->swScale= UYVY2YUV420Wrapper;
2760 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2761 c->swScale= YUYV2YUV422Wrapper;
2762 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2763 c->swScale= UYVY2YUV422Wrapper;
2764
2765 #ifdef COMPILE_ALTIVEC
2766 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2767 !(c->flags & SWS_BITEXACT) &&
2768 srcFormat == PIX_FMT_YUV420P) {
2769 // unscaled YV12 -> packed YUV, we want speed
2770 if (dstFormat == PIX_FMT_YUYV422)
2771 c->swScale= yv12toyuy2_unscaled_altivec;
2772 else if (dstFormat == PIX_FMT_UYVY422)
2773 c->swScale= yv12touyvy_unscaled_altivec;
2774 }
2775 #endif
2776
2777 /* simple copy */
2778 if ( srcFormat == dstFormat
2779 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2780 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2781 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2782 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2783 || (isGray(dstFormat) && isGray(srcFormat))
2784 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2785 && c->chrDstHSubSample == c->chrSrcHSubSample
2786 && c->chrDstVSubSample == c->chrSrcVSubSample))
2787 {
2788 if (isPacked(c->srcFormat))
2789 c->swScale= packedCopy;
2790 else /* Planar YUV or gray */
2791 c->swScale= planarCopy;
2792 }
2793 #if ARCH_BFIN
2794 if (flags & SWS_CPU_CAPS_BFIN)
2795 ff_bfin_get_unscaled_swscale (c);
2796 #endif
2797
2798 if (c->swScale){
2799 if (flags&SWS_PRINT_INFO)
2800 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2801 sws_format_name(srcFormat), sws_format_name(dstFormat));
2802 return c;
2803 }
2804 }
2805
2806 if (flags & SWS_CPU_CAPS_MMX2)
2807 {
2808 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2809 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2810 {
2811 if (flags&SWS_PRINT_INFO)
2812 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2813 }
2814 if (usesHFilter) c->canMMX2BeUsed=0;
2815 }
2816 else
2817 c->canMMX2BeUsed=0;
2818
2819 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2820 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2821
2822 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2823 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2824 // n-2 is the last chrominance sample available
2825 // this is not perfect, but no one should notice the difference, the more correct variant
2826 // would be like the vertical one, but that would require some special code for the
2827 // first and last pixel
2828 if (flags&SWS_FAST_BILINEAR)
2829 {
2830 if (c->canMMX2BeUsed)
2831 {
2832 c->lumXInc+= 20;
2833 c->chrXInc+= 20;
2834 }
2835 //we don't use the x86 asm scaler if MMX is available
2836 else if (flags & SWS_CPU_CAPS_MMX)
2837 {
2838 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2839 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2840 }
2841 }
2842
2843 /* precalculate horizontal scaler filter coefficients */
2844 {
2845 const int filterAlign=
2846 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2847 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2848 1;
2849
2850 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2851 srcW , dstW, filterAlign, 1<<14,
2852 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2853 srcFilter->lumH, dstFilter->lumH, c->param);
2854 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2855 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2856 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2857 srcFilter->chrH, dstFilter->chrH, c->param);
2858
2859 #define MAX_FUNNY_CODE_SIZE 10000
2860 #if defined(COMPILE_MMX2)
2861 // can't downscale !!!
2862 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2863 {
2864 #ifdef MAP_ANONYMOUS
2865 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2866 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2867 #elif HAVE_VIRTUALALLOC
2868 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2869 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2870 #else
2871 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2872 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2873 #endif
2874
2875 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2876 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2877 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2878 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2879
2880 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2881 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2882 }
2883 #endif /* defined(COMPILE_MMX2) */
2884 } // initialize horizontal stuff
2885
2886
2887
2888 /* precalculate vertical scaler filter coefficients */
2889 {
2890 const int filterAlign=
2891 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2892 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2893 1;
2894
2895 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2896 srcH , dstH, filterAlign, (1<<12),
2897 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2898 srcFilter->lumV, dstFilter->lumV, c->param);
2899 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2900 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2901 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2902 srcFilter->chrV, dstFilter->chrV, c->param);
2903
2904 #if HAVE_ALTIVEC
2905 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2906 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2907
2908 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2909 int j;
2910 short *p = (short *)&c->vYCoeffsBank[i];
2911 for (j=0;j<8;j++)
2912 p[j] = c->vLumFilter[i];
2913 }
2914
2915 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2916 int j;
2917 short *p = (short *)&c->vCCoeffsBank[i];
2918 for (j=0;j<8;j++)
2919 p[j] = c->vChrFilter[i];
2920 }
2921 #endif
2922 }
2923
2924 // calculate buffer sizes so that they won't run out while handling these damn slices
2925 c->vLumBufSize= c->vLumFilterSize;
2926 c->vChrBufSize= c->vChrFilterSize;
2927 for (i=0; i<dstH; i++)
2928 {
2929 int chrI= i*c->chrDstH / dstH;
2930 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2931 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2932
2933 nextSlice>>= c->chrSrcVSubSample;
2934 nextSlice<<= c->chrSrcVSubSample;
2935 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2936 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2937 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2938 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2939 }
2940
2941 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2942 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2943 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2944 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2945 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2946 //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)
2947 /* align at 16 bytes for AltiVec */
2948 for (i=0; i<c->vLumBufSize; i++)
2949 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2950 for (i=0; i<c->vChrBufSize; i++)
2951 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2952 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2953 for (i=0; i<c->vLumBufSize; i++)
2954 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2955
2956 //try to avoid drawing green stuff between the right end and the stride end
2957 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2958
2959 assert(2*VOFW == VOF);
2960
2961 assert(c->chrDstH <= dstH);
2962
2963 if (flags&SWS_PRINT_INFO)
2964 {
2965 #ifdef DITHER1XBPP
2966 const char *dither= " dithered";
2967 #else
2968 const char *dither= "";
2969 #endif
2970 if (flags&SWS_FAST_BILINEAR)
2971 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2972 else if (flags&SWS_BILINEAR)
2973 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2974 else if (flags&SWS_BICUBIC)
2975 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2976 else if (flags&SWS_X)
2977 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2978 else if (flags&SWS_POINT)
2979 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2980 else if (flags&SWS_AREA)
2981 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2982 else if (flags&SWS_BICUBLIN)
2983 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2984 else if (flags&SWS_GAUSS)
2985 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2986 else if (flags&SWS_SINC)
2987 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2988 else if (flags&SWS_LANCZOS)
2989 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2990 else if (flags&SWS_SPLINE)
2991 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2992 else
2993 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2994
2995 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2996 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2997 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2998 else
2999 av_log(c, AV_LOG_INFO, "from %s to %s ",
3000 sws_format_name(srcFormat), sws_format_name(dstFormat));
3001
3002 if (flags & SWS_CPU_CAPS_MMX2)
3003 av_log(c, AV_LOG_INFO, "using MMX2\n");
3004 else if (flags & SWS_CPU_CAPS_3DNOW)
3005 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
3006 else if (flags & SWS_CPU_CAPS_MMX)
3007 av_log(c, AV_LOG_INFO, "using MMX\n");
3008 else if (flags & SWS_CPU_CAPS_ALTIVEC)
3009 av_log(c, AV_LOG_INFO, "using AltiVec\n");
3010 else
3011 av_log(c, AV_LOG_INFO, "using C\n");
3012 }
3013
3014 if (flags & SWS_PRINT_INFO)
3015 {
3016 if (flags & SWS_CPU_CAPS_MMX)
3017 {
3018 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
3019 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3020 else
3021 {
3022 if (c->hLumFilterSize==4)
3023 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3024 else if (c->hLumFilterSize==8)
3025 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3026 else
3027 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3028
3029 if (c->hChrFilterSize==4)
3030 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3031 else if (c->hChrFilterSize==8)
3032 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3033 else
3034 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3035 }
3036 }
3037 else
3038 {
3039 #if ARCH_X86
3040 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3041 #else
3042 if (flags & SWS_FAST_BILINEAR)
3043 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3044 else
3045 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3046 #endif
3047 }
3048 if (isPlanarYUV(dstFormat))
3049 {
3050 if (c->vLumFilterSize==1)
3051 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3052 else
3053 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3054 }
3055 else
3056 {
3057 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3058 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3059 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3060 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3061 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3062 else
3063 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3064 }
3065
3066 if (dstFormat==PIX_FMT_BGR24)
3067 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3068 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3069 else if (dstFormat==PIX_FMT_RGB32)
3070 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3071 else if (dstFormat==PIX_FMT_BGR565)
3072 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3073 else if (dstFormat==PIX_FMT_BGR555)
3074 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3075
3076 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3077 }
3078 if (flags & SWS_PRINT_INFO)
3079 {
3080 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3081 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3082 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3083 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3084 }
3085
3086 c->swScale= getSwsFunc(c);
3087 return c;
3088 }
3089
3090 static void reset_ptr(uint8_t* src[], int format){
3091 if(!isALPHA(format))
3092 src[3]=NULL;
3093 if(!isPlanarYUV(format)){
3094 src[3]=src[2]=NULL;
3095 if( format != PIX_FMT_PAL8
3096 && format != PIX_FMT_RGB8
3097 && format != PIX_FMT_BGR8
3098 && format != PIX_FMT_RGB4_BYTE
3099 && format != PIX_FMT_BGR4_BYTE
3100 )
3101 src[1]= NULL;
3102 }
3103 }
3104
3105 /**
3106 * swscale wrapper, so we don't need to export the SwsContext.
3107 * Assumes planar YUV to be in YUV order instead of YVU.
3108 */
3109 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3110 int srcSliceH, uint8_t* dst[], int dstStride[]){
3111 int i;
3112 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3113 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3114
3115 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3116 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3117 return 0;
3118 }
3119 if (c->sliceDir == 0) {
3120 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3121 }
3122
3123 if (usePal(c->srcFormat)){
3124 for (i=0; i<256; i++){
3125 int p, r, g, b,y,u,v;
3126 if(c->srcFormat == PIX_FMT_PAL8){
3127 p=((uint32_t*)(src[1]))[i];
3128 r= (p>>16)&0xFF;
3129 g= (p>> 8)&0xFF;
3130 b= p &0xFF;
3131 }else if(c->srcFormat == PIX_FMT_RGB8){
3132 r= (i>>5 )*36;
3133 g= ((i>>2)&7)*36;
3134 b= (i&3 )*85;
3135 }else if(c->srcFormat == PIX_FMT_BGR8){
3136 b= (i>>6 )*85;
3137 g= ((i>>3)&7)*36;
3138 r= (i&7 )*36;
3139 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3140 r= (i>>3 )*255;
3141 g= ((i>>1)&3)*85;
3142 b= (i&1 )*255;
3143 }else {
3144 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3145 b= (i>>3 )*255;
3146 g= ((i>>1)&3)*85;
3147 r= (i&1 )*255;
3148 }
3149 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3150 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3151 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3152 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3153
3154
3155 switch(c->dstFormat) {
3156 case PIX_FMT_BGR32:
3157 #if !HAVE_BIGENDIAN
3158 case PIX_FMT_RGB24:
3159 #endif
3160 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3161 break;
3162 case PIX_FMT_BGR32_1:
3163 #if HAVE_BIGENDIAN
3164 case PIX_FMT_BGR24:
3165 #endif
3166 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3167 break;
3168 case PIX_FMT_RGB32_1:
3169 #if HAVE_BIGENDIAN
3170 case PIX_FMT_RGB24:
3171 #endif
3172 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3173 break;
3174 case PIX_FMT_RGB32:
3175 #if !HAVE_BIGENDIAN
3176 case PIX_FMT_BGR24:
3177 #endif
3178 default:
3179 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3180 }
3181 }
3182 }
3183
3184 // copy strides, so they can safely be modified
3185 if (c->sliceDir == 1) {
3186 // slices go from top to bottom
3187 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3188 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3189
3190 reset_ptr(src2, c->srcFormat);
3191 reset_ptr(dst2, c->dstFormat);
3192
3193 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3194 } else {
3195 // slices go from bottom to top => we flip the image internally
3196 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3197 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3198
3199 src2[0] += (srcSliceH-1)*srcStride[0];
3200 if (!usePal(c->srcFormat))
3201 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3202 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3203 src2[3] += (srcSliceH-1)*srcStride[3];
3204 dst2[0] += ( c->dstH -1)*dstStride[0];
3205 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3206 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3207 dst2[3] += ( c->dstH -1)*dstStride[3];
3208
3209 reset_ptr(src2, c->srcFormat);
3210 reset_ptr(dst2, c->dstFormat);
3211
3212 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3213 }
3214 }
3215
3216 #if LIBSWSCALE_VERSION_MAJOR < 1
3217 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3218 int srcSliceH, uint8_t* dst[], int dstStride[]){
3219 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3220 }
3221 #endif
3222
3223 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3224 float lumaSharpen, float chromaSharpen,
3225 float chromaHShift, float chromaVShift,
3226 int verbose)
3227 {
3228 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3229
3230 if (lumaGBlur!=0.0){
3231 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3232 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3233 }else{
3234 filter->lumH= sws_getIdentityVec();
3235 filter->lumV= sws_getIdentityVec();
3236 }
3237
3238 if (chromaGBlur!=0.0){
3239 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3240 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3241 }else{
3242 filter->chrH= sws_getIdentityVec();
3243 filter->chrV= sws_getIdentityVec();
3244 }
3245
3246 if (chromaSharpen!=0.0){
3247 SwsVector *id= sws_getIdentityVec();
3248 sws_scaleVec(filter->chrH, -chromaSharpen);
3249 sws_scaleVec(filter->chrV, -chromaSharpen);
3250 sws_addVec(filter->chrH, id);
3251 sws_addVec(filter->chrV, id);
3252 sws_freeVec(id);
3253 }
3254
3255 if (lumaSharpen!=0.0){
3256 SwsVector *id= sws_getIdentityVec();
3257 sws_scaleVec(filter->lumH, -lumaSharpen);
3258 sws_scaleVec(filter->lumV, -lumaSharpen);
3259 sws_addVec(filter->lumH, id);
3260 sws_addVec(filter->lumV, id);
3261 sws_freeVec(id);
3262 }
3263
3264 if (chromaHShift != 0.0)
3265 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3266
3267 if (chromaVShift != 0.0)
3268 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3269
3270 sws_normalizeVec(filter->chrH, 1.0);
3271 sws_normalizeVec(filter->chrV, 1.0);
3272 sws_normalizeVec(filter->lumH, 1.0);
3273 sws_normalizeVec(filter->lumV, 1.0);
3274
3275 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3276 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3277
3278 return filter;
3279 }
3280
3281 SwsVector *sws_getGaussianVec(double variance, double quality){
3282 const int length= (int)(variance*quality + 0.5) | 1;
3283 int i;
3284 double *coeff= av_malloc(length*sizeof(double));
3285 double middle= (length-1)*0.5;
3286 SwsVector *vec= av_malloc(sizeof(SwsVector));
3287
3288 vec->coeff= coeff;
3289 vec->length= length;
3290
3291 for (i=0; i<length; i++)
3292 {
3293 double dist= i-middle;
3294 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3295 }
3296
3297 sws_normalizeVec(vec, 1.0);
3298
3299 return vec;
3300 }
3301
3302 SwsVector *sws_getConstVec(double c, int length){
3303 int i;
3304 double *coeff= av_malloc(length*sizeof(double));
3305 SwsVector *vec= av_malloc(sizeof(SwsVector));
3306
3307 vec->coeff= coeff;
3308 vec->length= length;
3309
3310 for (i=0; i<length; i++)
3311 coeff[i]= c;
3312
3313 return vec;
3314 }
3315
3316
3317 SwsVector *sws_getIdentityVec(void){
3318 return sws_getConstVec(1.0, 1);
3319 }
3320
3321 double sws_dcVec(SwsVector *a){
3322 int i;
3323 double sum=0;
3324
3325 for (i=0; i<a->length; i++)
3326 sum+= a->coeff[i];
3327
3328 return sum;
3329 }
3330
3331 void sws_scaleVec(SwsVector *a, double scalar){
3332 int i;
3333
3334 for (i=0; i<a->length; i++)
3335 a->coeff[i]*= scalar;
3336 }
3337
3338 void sws_normalizeVec(SwsVector *a, double height){
3339 sws_scaleVec(a, height/sws_dcVec(a));
3340 }
3341
3342 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3343 int length= a->length + b->length - 1;
3344 double *coeff= av_malloc(length*sizeof(double));
3345 int i, j;
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++)
3354 {
3355 for (j=0; j<b->length; j++)
3356 {
3357 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3358 }
3359 }
3360
3361 return vec;
3362 }
3363
3364 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3365 int length= FFMAX(a->length, b->length);
3366 double *coeff= av_malloc(length*sizeof(double));
3367 int i;
3368 SwsVector *vec= av_malloc(sizeof(SwsVector));
3369
3370 vec->coeff= coeff;
3371 vec->length= length;