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