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