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