Indent libswscale:
[mplayer/glamo.git] / libswscale / swscale.c
blob4742f2fc048c6e9f29fe4ca45c677e433d97c66b
1 /*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
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
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
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
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
57 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
58 #include <inttypes.h>
59 #include <string.h>
60 #include <math.h>
61 #include <stdio.h>
62 #include "config.h"
63 #include <assert.h>
64 #if HAVE_SYS_MMAN_H
65 #include <sys/mman.h>
66 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67 #define MAP_ANONYMOUS MAP_ANON
68 #endif
69 #endif
70 #if HAVE_VIRTUALALLOC
71 #define WIN32_LEAN_AND_MEAN
72 #include <windows.h>
73 #endif
74 #include "swscale.h"
75 #include "swscale_internal.h"
76 #include "rgb2rgb.h"
77 #include "libavutil/intreadwrite.h"
78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/bswap.h"
81 unsigned swscale_version(void)
83 return LIBSWSCALE_VERSION_INT;
86 #undef MOVNTQ
87 #undef PAVGB
89 //#undef HAVE_MMX2
90 //#define HAVE_AMD3DNOW
91 //#undef HAVE_MMX
92 //#undef ARCH_X86
93 #define DITHER1XBPP
95 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
97 #define RET 0xC3 //near return opcode for x86
99 #ifdef M_PI
100 #define PI M_PI
101 #else
102 #define PI 3.14159265358979323846
103 #endif
105 #define isSupportedIn(x) ( \
106 (x)==PIX_FMT_YUV420P \
107 || (x)==PIX_FMT_YUVA420P \
108 || (x)==PIX_FMT_YUYV422 \
109 || (x)==PIX_FMT_UYVY422 \
110 || (x)==PIX_FMT_RGB48BE \
111 || (x)==PIX_FMT_RGB48LE \
112 || (x)==PIX_FMT_RGB32 \
113 || (x)==PIX_FMT_RGB32_1 \
114 || (x)==PIX_FMT_BGR24 \
115 || (x)==PIX_FMT_BGR565 \
116 || (x)==PIX_FMT_BGR555 \
117 || (x)==PIX_FMT_BGR32 \
118 || (x)==PIX_FMT_BGR32_1 \
119 || (x)==PIX_FMT_RGB24 \
120 || (x)==PIX_FMT_RGB565 \
121 || (x)==PIX_FMT_RGB555 \
122 || (x)==PIX_FMT_GRAY8 \
123 || (x)==PIX_FMT_YUV410P \
124 || (x)==PIX_FMT_YUV440P \
125 || (x)==PIX_FMT_GRAY16BE \
126 || (x)==PIX_FMT_GRAY16LE \
127 || (x)==PIX_FMT_YUV444P \
128 || (x)==PIX_FMT_YUV422P \
129 || (x)==PIX_FMT_YUV411P \
130 || (x)==PIX_FMT_PAL8 \
131 || (x)==PIX_FMT_BGR8 \
132 || (x)==PIX_FMT_RGB8 \
133 || (x)==PIX_FMT_BGR4_BYTE \
134 || (x)==PIX_FMT_RGB4_BYTE \
135 || (x)==PIX_FMT_YUV440P \
136 || (x)==PIX_FMT_MONOWHITE \
137 || (x)==PIX_FMT_MONOBLACK \
138 || (x)==PIX_FMT_YUV420PLE \
139 || (x)==PIX_FMT_YUV422PLE \
140 || (x)==PIX_FMT_YUV444PLE \
141 || (x)==PIX_FMT_YUV420PBE \
142 || (x)==PIX_FMT_YUV422PBE \
143 || (x)==PIX_FMT_YUV444PBE \
145 #define isSupportedOut(x) ( \
146 (x)==PIX_FMT_YUV420P \
147 || (x)==PIX_FMT_YUVA420P \
148 || (x)==PIX_FMT_YUYV422 \
149 || (x)==PIX_FMT_UYVY422 \
150 || (x)==PIX_FMT_YUV444P \
151 || (x)==PIX_FMT_YUV422P \
152 || (x)==PIX_FMT_YUV411P \
153 || isRGB(x) \
154 || isBGR(x) \
155 || (x)==PIX_FMT_NV12 \
156 || (x)==PIX_FMT_NV21 \
157 || (x)==PIX_FMT_GRAY16BE \
158 || (x)==PIX_FMT_GRAY16LE \
159 || (x)==PIX_FMT_GRAY8 \
160 || (x)==PIX_FMT_YUV410P \
161 || (x)==PIX_FMT_YUV440P \
162 || (x)==PIX_FMT_YUV420PLE \
163 || (x)==PIX_FMT_YUV422PLE \
164 || (x)==PIX_FMT_YUV444PLE \
165 || (x)==PIX_FMT_YUV420PBE \
166 || (x)==PIX_FMT_YUV422PBE \
167 || (x)==PIX_FMT_YUV444PBE \
169 #define isPacked(x) ( \
170 (x)==PIX_FMT_PAL8 \
171 || (x)==PIX_FMT_YUYV422 \
172 || (x)==PIX_FMT_UYVY422 \
173 || isRGB(x) \
174 || isBGR(x) \
176 #define usePal(x) ( \
177 (x)==PIX_FMT_PAL8 \
178 || (x)==PIX_FMT_BGR4_BYTE \
179 || (x)==PIX_FMT_RGB4_BYTE \
180 || (x)==PIX_FMT_BGR8 \
181 || (x)==PIX_FMT_RGB8 \
184 #define RGB2YUV_SHIFT 15
185 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
186 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
187 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
188 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
189 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
190 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
191 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
192 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
193 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
195 extern const int32_t ff_yuv2rgb_coeffs[8][4];
197 static const double rgb2yuv_table[8][9]={
198 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
199 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
200 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
201 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
202 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
203 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
204 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
205 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
209 NOTES
210 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
212 TODO
213 more intelligent misalignment avoidance for the horizontal scaler
214 write special vertical cubic upscale version
215 optimize C code (YV12 / minmax)
216 add support for packed pixel YUV input & output
217 add support for Y8 output
218 optimize BGR24 & BGR32
219 add BGR4 output support
220 write special BGR->BGR scaler
223 #if ARCH_X86 && CONFIG_GPL
224 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
225 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
226 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
227 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
228 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
229 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
230 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
231 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
233 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
234 0x0103010301030103LL,
235 0x0200020002000200LL,};
237 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
238 0x0602060206020602LL,
239 0x0004000400040004LL,};
241 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
242 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
243 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
244 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
245 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
246 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
248 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
250 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
252 #ifdef FAST_BGR2YV12
253 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
254 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
255 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
256 #else
257 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
258 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
259 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
260 #endif /* FAST_BGR2YV12 */
261 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
262 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
263 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
265 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
266 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
267 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
268 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
269 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
271 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
272 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
273 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
276 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
278 #endif /* ARCH_X86 && CONFIG_GPL */
280 // clipping helper table for C implementations:
281 static unsigned char clip_table[768];
283 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
285 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
286 { 1, 3, 1, 3, 1, 3, 1, 3, },
287 { 2, 0, 2, 0, 2, 0, 2, 0, },
290 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
291 { 6, 2, 6, 2, 6, 2, 6, 2, },
292 { 0, 4, 0, 4, 0, 4, 0, 4, },
295 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
296 { 17, 9, 23, 15, 16, 8, 22, 14, },
297 { 5, 29, 3, 27, 4, 28, 2, 26, },
298 { 21, 13, 19, 11, 20, 12, 18, 10, },
299 { 0, 24, 6, 30, 1, 25, 7, 31, },
300 { 16, 8, 22, 14, 17, 9, 23, 15, },
301 { 4, 28, 2, 26, 5, 29, 3, 27, },
302 { 20, 12, 18, 10, 21, 13, 19, 11, },
303 { 1, 25, 7, 31, 0, 24, 6, 30, },
306 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
307 { 0, 55, 14, 68, 3, 58, 17, 72, },
308 { 37, 18, 50, 32, 40, 22, 54, 35, },
309 { 9, 64, 5, 59, 13, 67, 8, 63, },
310 { 46, 27, 41, 23, 49, 31, 44, 26, },
311 { 2, 57, 16, 71, 1, 56, 15, 70, },
312 { 39, 21, 52, 34, 38, 19, 51, 33, },
313 { 11, 66, 7, 62, 10, 65, 6, 60, },
314 { 48, 30, 43, 25, 47, 29, 42, 24, },
317 #if 1
318 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
319 {117, 62, 158, 103, 113, 58, 155, 100, },
320 { 34, 199, 21, 186, 31, 196, 17, 182, },
321 {144, 89, 131, 76, 141, 86, 127, 72, },
322 { 0, 165, 41, 206, 10, 175, 52, 217, },
323 {110, 55, 151, 96, 120, 65, 162, 107, },
324 { 28, 193, 14, 179, 38, 203, 24, 189, },
325 {138, 83, 124, 69, 148, 93, 134, 79, },
326 { 7, 172, 48, 213, 3, 168, 45, 210, },
328 #elif 1
329 // tries to correct a gamma of 1.5
330 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
331 { 0, 143, 18, 200, 2, 156, 25, 215, },
332 { 78, 28, 125, 64, 89, 36, 138, 74, },
333 { 10, 180, 3, 161, 16, 195, 8, 175, },
334 {109, 51, 93, 38, 121, 60, 105, 47, },
335 { 1, 152, 23, 210, 0, 147, 20, 205, },
336 { 85, 33, 134, 71, 81, 30, 130, 67, },
337 { 14, 190, 6, 171, 12, 185, 5, 166, },
338 {117, 57, 101, 44, 113, 54, 97, 41, },
340 #elif 1
341 // tries to correct a gamma of 2.0
342 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
343 { 0, 124, 8, 193, 0, 140, 12, 213, },
344 { 55, 14, 104, 42, 66, 19, 119, 52, },
345 { 3, 168, 1, 145, 6, 187, 3, 162, },
346 { 86, 31, 70, 21, 99, 39, 82, 28, },
347 { 0, 134, 11, 206, 0, 129, 9, 200, },
348 { 62, 17, 114, 48, 58, 16, 109, 45, },
349 { 5, 181, 2, 157, 4, 175, 1, 151, },
350 { 95, 36, 78, 26, 90, 34, 74, 24, },
352 #else
353 // tries to correct a gamma of 2.5
354 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
355 { 0, 107, 3, 187, 0, 125, 6, 212, },
356 { 39, 7, 86, 28, 49, 11, 102, 36, },
357 { 1, 158, 0, 131, 3, 180, 1, 151, },
358 { 68, 19, 52, 12, 81, 25, 64, 17, },
359 { 0, 119, 5, 203, 0, 113, 4, 195, },
360 { 45, 9, 96, 33, 42, 8, 91, 30, },
361 { 2, 172, 1, 144, 2, 165, 0, 137, },
362 { 77, 23, 60, 15, 72, 21, 56, 14, },
364 #endif
366 const char *sws_format_name(enum PixelFormat format)
368 switch (format) {
369 case PIX_FMT_YUV420P:
370 return "yuv420p";
371 case PIX_FMT_YUVA420P:
372 return "yuva420p";
373 case PIX_FMT_YUYV422:
374 return "yuyv422";
375 case PIX_FMT_RGB24:
376 return "rgb24";
377 case PIX_FMT_BGR24:
378 return "bgr24";
379 case PIX_FMT_YUV422P:
380 return "yuv422p";
381 case PIX_FMT_YUV444P:
382 return "yuv444p";
383 case PIX_FMT_RGB32:
384 return "rgb32";
385 case PIX_FMT_YUV410P:
386 return "yuv410p";
387 case PIX_FMT_YUV411P:
388 return "yuv411p";
389 case PIX_FMT_RGB565:
390 return "rgb565";
391 case PIX_FMT_RGB555:
392 return "rgb555";
393 case PIX_FMT_GRAY16BE:
394 return "gray16be";
395 case PIX_FMT_GRAY16LE:
396 return "gray16le";
397 case PIX_FMT_GRAY8:
398 return "gray8";
399 case PIX_FMT_MONOWHITE:
400 return "mono white";
401 case PIX_FMT_MONOBLACK:
402 return "mono black";
403 case PIX_FMT_PAL8:
404 return "Palette";
405 case PIX_FMT_YUVJ420P:
406 return "yuvj420p";
407 case PIX_FMT_YUVJ422P:
408 return "yuvj422p";
409 case PIX_FMT_YUVJ444P:
410 return "yuvj444p";
411 case PIX_FMT_XVMC_MPEG2_MC:
412 return "xvmc_mpeg2_mc";
413 case PIX_FMT_XVMC_MPEG2_IDCT:
414 return "xvmc_mpeg2_idct";
415 case PIX_FMT_UYVY422:
416 return "uyvy422";
417 case PIX_FMT_UYYVYY411:
418 return "uyyvyy411";
419 case PIX_FMT_RGB32_1:
420 return "rgb32x";
421 case PIX_FMT_BGR32_1:
422 return "bgr32x";
423 case PIX_FMT_BGR32:
424 return "bgr32";
425 case PIX_FMT_BGR565:
426 return "bgr565";
427 case PIX_FMT_BGR555:
428 return "bgr555";
429 case PIX_FMT_BGR8:
430 return "bgr8";
431 case PIX_FMT_BGR4:
432 return "bgr4";
433 case PIX_FMT_BGR4_BYTE:
434 return "bgr4 byte";
435 case PIX_FMT_RGB8:
436 return "rgb8";
437 case PIX_FMT_RGB4:
438 return "rgb4";
439 case PIX_FMT_RGB4_BYTE:
440 return "rgb4 byte";
441 case PIX_FMT_RGB48BE:
442 return "rgb48be";
443 case PIX_FMT_RGB48LE:
444 return "rgb48le";
445 case PIX_FMT_NV12:
446 return "nv12";
447 case PIX_FMT_NV21:
448 return "nv21";
449 case PIX_FMT_YUV440P:
450 return "yuv440p";
451 case PIX_FMT_VDPAU_H264:
452 return "vdpau_h264";
453 case PIX_FMT_VDPAU_MPEG1:
454 return "vdpau_mpeg1";
455 case PIX_FMT_VDPAU_MPEG2:
456 return "vdpau_mpeg2";
457 case PIX_FMT_VDPAU_WMV3:
458 return "vdpau_wmv3";
459 case PIX_FMT_VDPAU_VC1:
460 return "vdpau_vc1";
461 case PIX_FMT_YUV420PLE:
462 return "yuv420ple";
463 case PIX_FMT_YUV422PLE:
464 return "yuv422ple";
465 case PIX_FMT_YUV444PLE:
466 return "yuv444ple";
467 case PIX_FMT_YUV420PBE:
468 return "yuv420pbe";
469 case PIX_FMT_YUV422PBE:
470 return "yuv422pbe";
471 case PIX_FMT_YUV444PBE:
472 return "yuv444pbe";
473 default:
474 return "Unknown format";
478 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
479 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
480 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
481 int dstW, int chrDstW, int big_endian)
483 //FIXME Optimize (just quickly written not optimized..)
484 int i;
486 for (i = 0; i < dstW; i++) {
487 int val = 1 << 10;
488 int j;
490 for (j = 0; j < lumFilterSize; j++)
491 val += lumSrc[j][i] * lumFilter[j];
493 if (big_endian) {
494 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
495 } else {
496 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
500 if (uDest) {
501 for (i = 0; i < chrDstW; i++) {
502 int u = 1 << 10;
503 int v = 1 << 10;
504 int j;
506 for (j = 0; j < chrFilterSize; j++) {
507 u += chrSrc[j][i ] * chrFilter[j];
508 v += chrSrc[j][i + VOFW] * chrFilter[j];
511 if (big_endian) {
512 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
513 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
514 } else {
515 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
516 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
521 if (CONFIG_SWSCALE_ALPHA && aDest) {
522 for (i = 0; i < dstW; i++) {
523 int val = 1 << 10;
524 int j;
526 for (j = 0; j < lumFilterSize; j++)
527 val += alpSrc[j][i] * lumFilter[j];
529 if (big_endian) {
530 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
531 } else {
532 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
538 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
539 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
540 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
541 enum PixelFormat dstFormat)
543 if (isBE(dstFormat)) {
544 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
545 chrFilter, chrSrc, chrFilterSize,
546 alpSrc,
547 dest, uDest, vDest, aDest,
548 dstW, chrDstW, 1);
549 } else {
550 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
551 chrFilter, chrSrc, chrFilterSize,
552 alpSrc,
553 dest, uDest, vDest, aDest,
554 dstW, chrDstW, 0);
558 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
559 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
560 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
562 //FIXME Optimize (just quickly written not optimized..)
563 int i;
564 for (i=0; i<dstW; i++)
566 int val=1<<18;
567 int j;
568 for (j=0; j<lumFilterSize; j++)
569 val += lumSrc[j][i] * lumFilter[j];
571 dest[i]= av_clip_uint8(val>>19);
574 if (uDest)
575 for (i=0; i<chrDstW; i++)
577 int u=1<<18;
578 int v=1<<18;
579 int j;
580 for (j=0; j<chrFilterSize; j++)
582 u += chrSrc[j][i] * chrFilter[j];
583 v += chrSrc[j][i + VOFW] * chrFilter[j];
586 uDest[i]= av_clip_uint8(u>>19);
587 vDest[i]= av_clip_uint8(v>>19);
590 if (CONFIG_SWSCALE_ALPHA && aDest)
591 for (i=0; i<dstW; i++){
592 int val=1<<18;
593 int j;
594 for (j=0; j<lumFilterSize; j++)
595 val += alpSrc[j][i] * lumFilter[j];
597 aDest[i]= av_clip_uint8(val>>19);
602 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
603 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
604 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
606 //FIXME Optimize (just quickly written not optimized..)
607 int i;
608 for (i=0; i<dstW; i++)
610 int val=1<<18;
611 int j;
612 for (j=0; j<lumFilterSize; j++)
613 val += lumSrc[j][i] * lumFilter[j];
615 dest[i]= av_clip_uint8(val>>19);
618 if (!uDest)
619 return;
621 if (dstFormat == PIX_FMT_NV12)
622 for (i=0; i<chrDstW; i++)
624 int u=1<<18;
625 int v=1<<18;
626 int j;
627 for (j=0; j<chrFilterSize; j++)
629 u += chrSrc[j][i] * chrFilter[j];
630 v += chrSrc[j][i + VOFW] * chrFilter[j];
633 uDest[2*i]= av_clip_uint8(u>>19);
634 uDest[2*i+1]= av_clip_uint8(v>>19);
636 else
637 for (i=0; i<chrDstW; i++)
639 int u=1<<18;
640 int v=1<<18;
641 int j;
642 for (j=0; j<chrFilterSize; j++)
644 u += chrSrc[j][i] * chrFilter[j];
645 v += chrSrc[j][i + VOFW] * chrFilter[j];
648 uDest[2*i]= av_clip_uint8(v>>19);
649 uDest[2*i+1]= av_clip_uint8(u>>19);
653 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
654 for (i=0; i<(dstW>>1); i++){\
655 int j;\
656 int Y1 = 1<<18;\
657 int Y2 = 1<<18;\
658 int U = 1<<18;\
659 int V = 1<<18;\
660 int av_unused A1, A2;\
661 type av_unused *r, *b, *g;\
662 const int i2= 2*i;\
664 for (j=0; j<lumFilterSize; j++)\
666 Y1 += lumSrc[j][i2] * lumFilter[j];\
667 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
669 for (j=0; j<chrFilterSize; j++)\
671 U += chrSrc[j][i] * chrFilter[j];\
672 V += chrSrc[j][i+VOFW] * chrFilter[j];\
674 Y1>>=19;\
675 Y2>>=19;\
676 U >>=19;\
677 V >>=19;\
678 if (alpha){\
679 A1 = 1<<18;\
680 A2 = 1<<18;\
681 for (j=0; j<lumFilterSize; j++){\
682 A1 += alpSrc[j][i2 ] * lumFilter[j];\
683 A2 += alpSrc[j][i2+1] * lumFilter[j];\
685 A1>>=19;\
686 A2>>=19;\
689 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
690 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
691 if ((Y1|Y2|U|V)&256)\
693 if (Y1>255) Y1=255; \
694 else if (Y1<0)Y1=0; \
695 if (Y2>255) Y2=255; \
696 else if (Y2<0)Y2=0; \
697 if (U>255) U=255; \
698 else if (U<0) U=0; \
699 if (V>255) V=255; \
700 else if (V<0) V=0; \
702 if (alpha && ((A1|A2)&256)){\
703 A1=av_clip_uint8(A1);\
704 A2=av_clip_uint8(A2);\
707 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
708 for (i=0; i<dstW; i++){\
709 int j;\
710 int Y = 0;\
711 int U = -128<<19;\
712 int V = -128<<19;\
713 int av_unused A;\
714 int R,G,B;\
716 for (j=0; j<lumFilterSize; j++){\
717 Y += lumSrc[j][i ] * lumFilter[j];\
719 for (j=0; j<chrFilterSize; j++){\
720 U += chrSrc[j][i ] * chrFilter[j];\
721 V += chrSrc[j][i+VOFW] * chrFilter[j];\
723 Y >>=10;\
724 U >>=10;\
725 V >>=10;\
726 if (alpha){\
727 A = rnd;\
728 for (j=0; j<lumFilterSize; j++)\
729 A += alpSrc[j][i ] * lumFilter[j];\
730 A >>=19;\
731 if (A&256)\
732 A = av_clip_uint8(A);\
735 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
736 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
737 Y-= c->yuv2rgb_y_offset;\
738 Y*= c->yuv2rgb_y_coeff;\
739 Y+= rnd;\
740 R= Y + V*c->yuv2rgb_v2r_coeff;\
741 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
742 B= Y + U*c->yuv2rgb_u2b_coeff;\
743 if ((R|G|B)&(0xC0000000)){\
744 if (R>=(256<<22)) R=(256<<22)-1; \
745 else if (R<0)R=0; \
746 if (G>=(256<<22)) G=(256<<22)-1; \
747 else if (G<0)G=0; \
748 if (B>=(256<<22)) B=(256<<22)-1; \
749 else if (B<0)B=0; \
753 #define YSCALE_YUV_2_GRAY16_C \
754 for (i=0; i<(dstW>>1); i++){\
755 int j;\
756 int Y1 = 1<<18;\
757 int Y2 = 1<<18;\
758 int U = 1<<18;\
759 int V = 1<<18;\
761 const int i2= 2*i;\
763 for (j=0; j<lumFilterSize; j++)\
765 Y1 += lumSrc[j][i2] * lumFilter[j];\
766 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
768 Y1>>=11;\
769 Y2>>=11;\
770 if ((Y1|Y2|U|V)&65536)\
772 if (Y1>65535) Y1=65535; \
773 else if (Y1<0)Y1=0; \
774 if (Y2>65535) Y2=65535; \
775 else if (Y2<0)Y2=0; \
778 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
779 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
780 r = (type *)c->table_rV[V]; \
781 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
782 b = (type *)c->table_bU[U]; \
784 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
785 for (i=0; i<(dstW>>1); i++){ \
786 const int i2= 2*i; \
787 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
788 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
789 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
790 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
791 type av_unused *r, *b, *g; \
792 int av_unused A1, A2; \
793 if (alpha){\
794 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
795 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
798 #define YSCALE_YUV_2_GRAY16_2_C \
799 for (i=0; i<(dstW>>1); i++){ \
800 const int i2= 2*i; \
801 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
802 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
804 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
805 YSCALE_YUV_2_PACKED2_C(type,alpha)\
806 r = (type *)c->table_rV[V];\
807 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
808 b = (type *)c->table_bU[U];\
810 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
811 for (i=0; i<(dstW>>1); i++){\
812 const int i2= 2*i;\
813 int Y1= buf0[i2 ]>>7;\
814 int Y2= buf0[i2+1]>>7;\
815 int U= (uvbuf1[i ])>>7;\
816 int V= (uvbuf1[i+VOFW])>>7;\
817 type av_unused *r, *b, *g;\
818 int av_unused A1, A2;\
819 if (alpha){\
820 A1= abuf0[i2 ]>>7;\
821 A2= abuf0[i2+1]>>7;\
824 #define YSCALE_YUV_2_GRAY16_1_C \
825 for (i=0; i<(dstW>>1); i++){\
826 const int i2= 2*i;\
827 int Y1= buf0[i2 ]<<1;\
828 int Y2= buf0[i2+1]<<1;\
830 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
831 YSCALE_YUV_2_PACKED1_C(type,alpha)\
832 r = (type *)c->table_rV[V];\
833 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
834 b = (type *)c->table_bU[U];\
836 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
837 for (i=0; i<(dstW>>1); i++){\
838 const int i2= 2*i;\
839 int Y1= buf0[i2 ]>>7;\
840 int Y2= buf0[i2+1]>>7;\
841 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
842 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
843 type av_unused *r, *b, *g;\
844 int av_unused A1, A2;\
845 if (alpha){\
846 A1= abuf0[i2 ]>>7;\
847 A2= abuf0[i2+1]>>7;\
850 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
851 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
852 r = (type *)c->table_rV[V];\
853 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
854 b = (type *)c->table_bU[U];\
856 #define YSCALE_YUV_2_MONO2_C \
857 const uint8_t * const d128=dither_8x8_220[y&7];\
858 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
859 for (i=0; i<dstW-7; i+=8){\
860 int acc;\
861 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
862 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
863 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
864 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
865 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
866 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
867 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
868 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
869 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
870 dest++;\
874 #define YSCALE_YUV_2_MONOX_C \
875 const uint8_t * const d128=dither_8x8_220[y&7];\
876 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
877 int acc=0;\
878 for (i=0; i<dstW-1; i+=2){\
879 int j;\
880 int Y1=1<<18;\
881 int Y2=1<<18;\
883 for (j=0; j<lumFilterSize; j++)\
885 Y1 += lumSrc[j][i] * lumFilter[j];\
886 Y2 += lumSrc[j][i+1] * lumFilter[j];\
888 Y1>>=19;\
889 Y2>>=19;\
890 if ((Y1|Y2)&256)\
892 if (Y1>255) Y1=255;\
893 else if (Y1<0)Y1=0;\
894 if (Y2>255) Y2=255;\
895 else if (Y2<0)Y2=0;\
897 acc+= acc + g[Y1+d128[(i+0)&7]];\
898 acc+= acc + g[Y2+d128[(i+1)&7]];\
899 if ((i&7)==6){\
900 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
901 dest++;\
906 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
907 switch(c->dstFormat)\
909 case PIX_FMT_RGB48BE:\
910 case PIX_FMT_RGB48LE:\
911 func(uint8_t,0)\
912 ((uint8_t*)dest)[ 0]= r[Y1];\
913 ((uint8_t*)dest)[ 1]= r[Y1];\
914 ((uint8_t*)dest)[ 2]= g[Y1];\
915 ((uint8_t*)dest)[ 3]= g[Y1];\
916 ((uint8_t*)dest)[ 4]= b[Y1];\
917 ((uint8_t*)dest)[ 5]= b[Y1];\
918 ((uint8_t*)dest)[ 6]= r[Y2];\
919 ((uint8_t*)dest)[ 7]= r[Y2];\
920 ((uint8_t*)dest)[ 8]= g[Y2];\
921 ((uint8_t*)dest)[ 9]= g[Y2];\
922 ((uint8_t*)dest)[10]= b[Y2];\
923 ((uint8_t*)dest)[11]= b[Y2];\
924 dest+=12;\
926 break;\
927 case PIX_FMT_RGBA:\
928 case PIX_FMT_BGRA:\
929 if (CONFIG_SMALL){\
930 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
931 func(uint32_t,needAlpha)\
932 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
933 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
935 }else{\
936 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
937 func(uint32_t,1)\
938 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
939 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
941 }else{\
942 func(uint32_t,0)\
943 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
944 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
948 break;\
949 case PIX_FMT_ARGB:\
950 case PIX_FMT_ABGR:\
951 if (CONFIG_SMALL){\
952 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
953 func(uint32_t,needAlpha)\
954 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
955 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
957 }else{\
958 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
959 func(uint32_t,1)\
960 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
961 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
963 }else{\
964 func(uint32_t,0)\
965 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
966 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
970 break;\
971 case PIX_FMT_RGB24:\
972 func(uint8_t,0)\
973 ((uint8_t*)dest)[0]= r[Y1];\
974 ((uint8_t*)dest)[1]= g[Y1];\
975 ((uint8_t*)dest)[2]= b[Y1];\
976 ((uint8_t*)dest)[3]= r[Y2];\
977 ((uint8_t*)dest)[4]= g[Y2];\
978 ((uint8_t*)dest)[5]= b[Y2];\
979 dest+=6;\
981 break;\
982 case PIX_FMT_BGR24:\
983 func(uint8_t,0)\
984 ((uint8_t*)dest)[0]= b[Y1];\
985 ((uint8_t*)dest)[1]= g[Y1];\
986 ((uint8_t*)dest)[2]= r[Y1];\
987 ((uint8_t*)dest)[3]= b[Y2];\
988 ((uint8_t*)dest)[4]= g[Y2];\
989 ((uint8_t*)dest)[5]= r[Y2];\
990 dest+=6;\
992 break;\
993 case PIX_FMT_RGB565:\
994 case PIX_FMT_BGR565:\
996 const int dr1= dither_2x2_8[y&1 ][0];\
997 const int dg1= dither_2x2_4[y&1 ][0];\
998 const int db1= dither_2x2_8[(y&1)^1][0];\
999 const int dr2= dither_2x2_8[y&1 ][1];\
1000 const int dg2= dither_2x2_4[y&1 ][1];\
1001 const int db2= dither_2x2_8[(y&1)^1][1];\
1002 func(uint16_t,0)\
1003 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
1004 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
1007 break;\
1008 case PIX_FMT_RGB555:\
1009 case PIX_FMT_BGR555:\
1011 const int dr1= dither_2x2_8[y&1 ][0];\
1012 const int dg1= dither_2x2_8[y&1 ][1];\
1013 const int db1= dither_2x2_8[(y&1)^1][0];\
1014 const int dr2= dither_2x2_8[y&1 ][1];\
1015 const int dg2= dither_2x2_8[y&1 ][0];\
1016 const int db2= dither_2x2_8[(y&1)^1][1];\
1017 func(uint16_t,0)\
1018 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
1019 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
1022 break;\
1023 case PIX_FMT_RGB8:\
1024 case PIX_FMT_BGR8:\
1026 const uint8_t * const d64= dither_8x8_73[y&7];\
1027 const uint8_t * const d32= dither_8x8_32[y&7];\
1028 func(uint8_t,0)\
1029 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
1030 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
1033 break;\
1034 case PIX_FMT_RGB4:\
1035 case PIX_FMT_BGR4:\
1037 const uint8_t * const d64= dither_8x8_73 [y&7];\
1038 const uint8_t * const d128=dither_8x8_220[y&7];\
1039 func(uint8_t,0)\
1040 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
1041 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
1044 break;\
1045 case PIX_FMT_RGB4_BYTE:\
1046 case PIX_FMT_BGR4_BYTE:\
1048 const uint8_t * const d64= dither_8x8_73 [y&7];\
1049 const uint8_t * const d128=dither_8x8_220[y&7];\
1050 func(uint8_t,0)\
1051 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
1052 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
1055 break;\
1056 case PIX_FMT_MONOBLACK:\
1057 case PIX_FMT_MONOWHITE:\
1059 func_monoblack\
1061 break;\
1062 case PIX_FMT_YUYV422:\
1063 func2\
1064 ((uint8_t*)dest)[2*i2+0]= Y1;\
1065 ((uint8_t*)dest)[2*i2+1]= U;\
1066 ((uint8_t*)dest)[2*i2+2]= Y2;\
1067 ((uint8_t*)dest)[2*i2+3]= V;\
1069 break;\
1070 case PIX_FMT_UYVY422:\
1071 func2\
1072 ((uint8_t*)dest)[2*i2+0]= U;\
1073 ((uint8_t*)dest)[2*i2+1]= Y1;\
1074 ((uint8_t*)dest)[2*i2+2]= V;\
1075 ((uint8_t*)dest)[2*i2+3]= Y2;\
1077 break;\
1078 case PIX_FMT_GRAY16BE:\
1079 func_g16\
1080 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1081 ((uint8_t*)dest)[2*i2+1]= Y1;\
1082 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1083 ((uint8_t*)dest)[2*i2+3]= Y2;\
1085 break;\
1086 case PIX_FMT_GRAY16LE:\
1087 func_g16\
1088 ((uint8_t*)dest)[2*i2+0]= Y1;\
1089 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1090 ((uint8_t*)dest)[2*i2+2]= Y2;\
1091 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1093 break;\
1097 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1098 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1099 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1101 int i;
1102 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)
1105 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1106 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1107 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1109 int i;
1110 int step= fmt_depth(c->dstFormat)/8;
1111 int aidx= 3;
1113 switch(c->dstFormat){
1114 case PIX_FMT_ARGB:
1115 dest++;
1116 aidx= 0;
1117 case PIX_FMT_RGB24:
1118 aidx--;
1119 case PIX_FMT_RGBA:
1120 if (CONFIG_SMALL){
1121 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1122 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1123 dest[aidx]= needAlpha ? A : 255;
1124 dest[0]= R>>22;
1125 dest[1]= G>>22;
1126 dest[2]= B>>22;
1127 dest+= step;
1129 }else{
1130 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1131 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1132 dest[aidx]= A;
1133 dest[0]= R>>22;
1134 dest[1]= G>>22;
1135 dest[2]= B>>22;
1136 dest+= step;
1138 }else{
1139 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1140 dest[aidx]= 255;
1141 dest[0]= R>>22;
1142 dest[1]= G>>22;
1143 dest[2]= B>>22;
1144 dest+= step;
1148 break;
1149 case PIX_FMT_ABGR:
1150 dest++;
1151 aidx= 0;
1152 case PIX_FMT_BGR24:
1153 aidx--;
1154 case PIX_FMT_BGRA:
1155 if (CONFIG_SMALL){
1156 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1157 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1158 dest[aidx]= needAlpha ? A : 255;
1159 dest[0]= B>>22;
1160 dest[1]= G>>22;
1161 dest[2]= R>>22;
1162 dest+= step;
1164 }else{
1165 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1166 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1167 dest[aidx]= A;
1168 dest[0]= B>>22;
1169 dest[1]= G>>22;
1170 dest[2]= R>>22;
1171 dest+= step;
1173 }else{
1174 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1175 dest[aidx]= 255;
1176 dest[0]= B>>22;
1177 dest[1]= G>>22;
1178 dest[2]= R>>22;
1179 dest+= step;
1183 break;
1184 default:
1185 assert(0);
1189 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1190 int i;
1191 uint8_t *ptr = plane + stride*y;
1192 for (i=0; i<height; i++){
1193 memset(ptr, val, width);
1194 ptr += stride;
1198 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1200 int i;
1201 for (i = 0; i < width; i++) {
1202 int r = src[i*6+0];
1203 int g = src[i*6+2];
1204 int b = src[i*6+4];
1206 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1210 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1211 uint8_t *src1, uint8_t *src2, int width)
1213 int i;
1214 assert(src1==src2);
1215 for (i = 0; i < width; i++) {
1216 int r = src1[6*i + 0];
1217 int g = src1[6*i + 2];
1218 int b = src1[6*i + 4];
1220 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1221 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1225 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1226 uint8_t *src1, uint8_t *src2, int width)
1228 int i;
1229 assert(src1==src2);
1230 for (i = 0; i < width; i++) {
1231 int r= src1[12*i + 0] + src1[12*i + 6];
1232 int g= src1[12*i + 2] + src1[12*i + 8];
1233 int b= src1[12*i + 4] + src1[12*i + 10];
1235 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1236 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1240 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1241 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1243 int i;\
1244 for (i=0; i<width; i++)\
1246 int b= (((const type*)src)[i]>>shb)&maskb;\
1247 int g= (((const type*)src)[i]>>shg)&maskg;\
1248 int r= (((const type*)src)[i]>>shr)&maskr;\
1250 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1254 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1255 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1256 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1257 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1258 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1259 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1261 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused){
1262 int i;
1263 for (i=0; i<width; i++){
1264 dst[i]= src[4*i];
1268 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1269 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1271 int i;\
1272 for (i=0; i<width; i++)\
1274 int b= (((const type*)src)[i]&maskb)>>shb;\
1275 int g= (((const type*)src)[i]&maskg)>>shg;\
1276 int r= (((const type*)src)[i]&maskr)>>shr;\
1278 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1279 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1282 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1284 int i;\
1285 for (i=0; i<width; i++)\
1287 int pix0= ((const type*)src)[2*i+0];\
1288 int pix1= ((const type*)src)[2*i+1];\
1289 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1290 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1291 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1292 g&= maskg|(2*maskg);\
1294 g>>=shg;\
1296 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1297 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1301 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1302 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1303 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1304 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1305 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1306 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1308 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1310 int i;
1311 for (i=0; i<width; i++)
1313 int d= src[i];
1315 dst[i]= pal[d] & 0xFF;
1319 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1320 const uint8_t *src1, const uint8_t *src2,
1321 long width, uint32_t *pal)
1323 int i;
1324 assert(src1 == src2);
1325 for (i=0; i<width; i++)
1327 int p= pal[src1[i]];
1329 dstU[i]= p>>8;
1330 dstV[i]= p>>16;
1334 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1336 int i, j;
1337 for (i=0; i<width/8; i++){
1338 int d= ~src[i];
1339 for(j=0; j<8; j++)
1340 dst[8*i+j]= ((d>>(7-j))&1)*255;
1344 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1346 int i, j;
1347 for (i=0; i<width/8; i++){
1348 int d= src[i];
1349 for(j=0; j<8; j++)
1350 dst[8*i+j]= ((d>>(7-j))&1)*255;
1355 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1356 //Plain C versions
1357 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1358 #define COMPILE_C
1359 #endif
1361 #if ARCH_PPC
1362 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1363 #define COMPILE_ALTIVEC
1364 #endif
1365 #endif //ARCH_PPC
1367 #if ARCH_X86
1369 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1370 #define COMPILE_MMX
1371 #endif
1373 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1374 #define COMPILE_MMX2
1375 #endif
1377 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1378 #define COMPILE_3DNOW
1379 #endif
1380 #endif //ARCH_X86
1382 #define COMPILE_TEMPLATE_MMX 0
1383 #define COMPILE_TEMPLATE_MMX2 0
1384 #define COMPILE_TEMPLATE_AMD3DNOW 0
1385 #define COMPILE_TEMPLATE_ALTIVEC 0
1387 #ifdef COMPILE_C
1388 #define RENAME(a) a ## _C
1389 #include "swscale_template.c"
1390 #endif
1392 #ifdef COMPILE_ALTIVEC
1393 #undef RENAME
1394 #undef COMPILE_TEMPLATE_ALTIVEC
1395 #define COMPILE_TEMPLATE_ALTIVEC 1
1396 #define RENAME(a) a ## _altivec
1397 #include "swscale_template.c"
1398 #endif
1400 #if ARCH_X86
1402 //MMX versions
1403 #ifdef COMPILE_MMX
1404 #undef RENAME
1405 #undef COMPILE_TEMPLATE_MMX
1406 #undef COMPILE_TEMPLATE_MMX2
1407 #undef COMPILE_TEMPLATE_AMD3DNOW
1408 #define COMPILE_TEMPLATE_MMX 1
1409 #define COMPILE_TEMPLATE_MMX2 0
1410 #define COMPILE_TEMPLATE_AMD3DNOW 0
1411 #define RENAME(a) a ## _MMX
1412 #include "swscale_template.c"
1413 #endif
1415 //MMX2 versions
1416 #ifdef COMPILE_MMX2
1417 #undef RENAME
1418 #undef COMPILE_TEMPLATE_MMX
1419 #undef COMPILE_TEMPLATE_MMX2
1420 #undef COMPILE_TEMPLATE_AMD3DNOW
1421 #define COMPILE_TEMPLATE_MMX 1
1422 #define COMPILE_TEMPLATE_MMX2 1
1423 #define COMPILE_TEMPLATE_AMD3DNOW 0
1424 #define RENAME(a) a ## _MMX2
1425 #include "swscale_template.c"
1426 #endif
1428 //3DNOW versions
1429 #ifdef COMPILE_3DNOW
1430 #undef RENAME
1431 #undef COMPILE_TEMPLATE_MMX
1432 #undef COMPILE_TEMPLATE_MMX2
1433 #undef COMPILE_TEMPLATE_AMD3DNOW
1434 #define COMPILE_TEMPLATE_MMX 1
1435 #define COMPILE_TEMPLATE_MMX2 0
1436 #define COMPILE_TEMPLATE_AMD3DNOW 1
1437 #define RENAME(a) a ## _3DNow
1438 #include "swscale_template.c"
1439 #endif
1441 #endif //ARCH_X86
1443 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1445 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1446 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1447 else return getSplineCoeff( 0.0,
1448 b+ 2.0*c + 3.0*d,
1449 c + 3.0*d,
1450 -b- 3.0*c - 6.0*d,
1451 dist-1.0);
1454 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1455 int srcW, int dstW, int filterAlign, int one, int flags,
1456 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1458 int i;
1459 int filterSize;
1460 int filter2Size;
1461 int minFilterSize;
1462 int64_t *filter=NULL;
1463 int64_t *filter2=NULL;
1464 const int64_t fone= 1LL<<54;
1465 int ret= -1;
1466 #if ARCH_X86
1467 if (flags & SWS_CPU_CAPS_MMX)
1468 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1469 #endif
1471 // NOTE: the +1 is for the MMX scaler which reads over the end
1472 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1474 if (FFABS(xInc - 0x10000) <10) // unscaled
1476 int i;
1477 filterSize= 1;
1478 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1480 for (i=0; i<dstW; i++)
1482 filter[i*filterSize]= fone;
1483 (*filterPos)[i]=i;
1487 else if (flags&SWS_POINT) // lame looking point sampling mode
1489 int i;
1490 int xDstInSrc;
1491 filterSize= 1;
1492 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1494 xDstInSrc= xInc/2 - 0x8000;
1495 for (i=0; i<dstW; i++)
1497 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1499 (*filterPos)[i]= xx;
1500 filter[i]= fone;
1501 xDstInSrc+= xInc;
1504 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1506 int i;
1507 int xDstInSrc;
1508 filterSize= 2;
1509 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1511 xDstInSrc= xInc/2 - 0x8000;
1512 for (i=0; i<dstW; i++)
1514 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1515 int j;
1517 (*filterPos)[i]= xx;
1518 //bilinear upscale / linear interpolate / area averaging
1519 for (j=0; j<filterSize; j++)
1521 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1522 if (coeff<0) coeff=0;
1523 filter[i*filterSize + j]= coeff;
1524 xx++;
1526 xDstInSrc+= xInc;
1529 else
1531 int xDstInSrc;
1532 int sizeFactor;
1534 if (flags&SWS_BICUBIC) sizeFactor= 4;
1535 else if (flags&SWS_X) sizeFactor= 8;
1536 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1537 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1538 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1539 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1540 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1541 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1542 else {
1543 sizeFactor= 0; //GCC warning killer
1544 assert(0);
1547 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1548 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1550 if (filterSize > srcW-2) filterSize=srcW-2;
1552 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1554 xDstInSrc= xInc - 0x10000;
1555 for (i=0; i<dstW; i++)
1557 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1558 int j;
1559 (*filterPos)[i]= xx;
1560 for (j=0; j<filterSize; j++)
1562 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1563 double floatd;
1564 int64_t coeff;
1566 if (xInc > 1<<16)
1567 d= d*dstW/srcW;
1568 floatd= d * (1.0/(1<<30));
1570 if (flags & SWS_BICUBIC)
1572 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1573 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1574 int64_t dd = ( d*d)>>30;
1575 int64_t ddd= (dd*d)>>30;
1577 if (d < 1LL<<30)
1578 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1579 else if (d < 1LL<<31)
1580 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1581 else
1582 coeff=0.0;
1583 coeff *= fone>>(30+24);
1585 /* else if (flags & SWS_X)
1587 double p= param ? param*0.01 : 0.3;
1588 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1589 coeff*= pow(2.0, - p*d*d);
1591 else if (flags & SWS_X)
1593 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1594 double c;
1596 if (floatd<1.0)
1597 c = cos(floatd*PI);
1598 else
1599 c=-1.0;
1600 if (c<0.0) c= -pow(-c, A);
1601 else c= pow( c, A);
1602 coeff= (c*0.5 + 0.5)*fone;
1604 else if (flags & SWS_AREA)
1606 int64_t d2= d - (1<<29);
1607 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1608 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1609 else coeff=0.0;
1610 coeff *= fone>>(30+16);
1612 else if (flags & SWS_GAUSS)
1614 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1615 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1617 else if (flags & SWS_SINC)
1619 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1621 else if (flags & SWS_LANCZOS)
1623 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1624 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1625 if (floatd>p) coeff=0;
1627 else if (flags & SWS_BILINEAR)
1629 coeff= (1<<30) - d;
1630 if (coeff<0) coeff=0;
1631 coeff *= fone >> 30;
1633 else if (flags & SWS_SPLINE)
1635 double p=-2.196152422706632;
1636 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1638 else {
1639 coeff= 0.0; //GCC warning killer
1640 assert(0);
1643 filter[i*filterSize + j]= coeff;
1644 xx++;
1646 xDstInSrc+= 2*xInc;
1650 /* apply src & dst Filter to filter -> filter2
1651 av_free(filter);
1653 assert(filterSize>0);
1654 filter2Size= filterSize;
1655 if (srcFilter) filter2Size+= srcFilter->length - 1;
1656 if (dstFilter) filter2Size+= dstFilter->length - 1;
1657 assert(filter2Size>0);
1658 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1660 for (i=0; i<dstW; i++)
1662 int j, k;
1664 if(srcFilter){
1665 for (k=0; k<srcFilter->length; k++){
1666 for (j=0; j<filterSize; j++)
1667 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1669 }else{
1670 for (j=0; j<filterSize; j++)
1671 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1673 //FIXME dstFilter
1675 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1677 av_freep(&filter);
1679 /* try to reduce the filter-size (step1 find size and shift left) */
1680 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1681 minFilterSize= 0;
1682 for (i=dstW-1; i>=0; i--)
1684 int min= filter2Size;
1685 int j;
1686 int64_t cutOff=0.0;
1688 /* get rid off near zero elements on the left by shifting left */
1689 for (j=0; j<filter2Size; j++)
1691 int k;
1692 cutOff += FFABS(filter2[i*filter2Size]);
1694 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1696 /* preserve monotonicity because the core can't handle the filter otherwise */
1697 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1699 // move filter coefficients left
1700 for (k=1; k<filter2Size; k++)
1701 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1702 filter2[i*filter2Size + k - 1]= 0;
1703 (*filterPos)[i]++;
1706 cutOff=0;
1707 /* count near zeros on the right */
1708 for (j=filter2Size-1; j>0; j--)
1710 cutOff += FFABS(filter2[i*filter2Size + j]);
1712 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1713 min--;
1716 if (min>minFilterSize) minFilterSize= min;
1719 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1720 // we can handle the special case 4,
1721 // so we don't want to go to the full 8
1722 if (minFilterSize < 5)
1723 filterAlign = 4;
1725 // We really don't want to waste our time
1726 // doing useless computation, so fall back on
1727 // the scalar C code for very small filters.
1728 // Vectorizing is worth it only if you have a
1729 // decent-sized vector.
1730 if (minFilterSize < 3)
1731 filterAlign = 1;
1734 if (flags & SWS_CPU_CAPS_MMX) {
1735 // special case for unscaled vertical filtering
1736 if (minFilterSize == 1 && filterAlign == 2)
1737 filterAlign= 1;
1740 assert(minFilterSize > 0);
1741 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1742 assert(filterSize > 0);
1743 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1744 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1745 goto error;
1746 *outFilterSize= filterSize;
1748 if (flags&SWS_PRINT_INFO)
1749 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1750 /* try to reduce the filter-size (step2 reduce it) */
1751 for (i=0; i<dstW; i++)
1753 int j;
1755 for (j=0; j<filterSize; j++)
1757 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1758 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1759 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1760 filter[i*filterSize + j]= 0;
1765 //FIXME try to align filterPos if possible
1767 //fix borders
1768 for (i=0; i<dstW; i++)
1770 int j;
1771 if ((*filterPos)[i] < 0)
1773 // move filter coefficients left to compensate for filterPos
1774 for (j=1; j<filterSize; j++)
1776 int left= FFMAX(j + (*filterPos)[i], 0);
1777 filter[i*filterSize + left] += filter[i*filterSize + j];
1778 filter[i*filterSize + j]=0;
1780 (*filterPos)[i]= 0;
1783 if ((*filterPos)[i] + filterSize > srcW)
1785 int shift= (*filterPos)[i] + filterSize - srcW;
1786 // move filter coefficients right to compensate for filterPos
1787 for (j=filterSize-2; j>=0; j--)
1789 int right= FFMIN(j + shift, filterSize-1);
1790 filter[i*filterSize +right] += filter[i*filterSize +j];
1791 filter[i*filterSize +j]=0;
1793 (*filterPos)[i]= srcW - filterSize;
1797 // Note the +1 is for the MMX scaler which reads over the end
1798 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1799 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1801 /* normalize & store in outFilter */
1802 for (i=0; i<dstW; i++)
1804 int j;
1805 int64_t error=0;
1806 int64_t sum=0;
1808 for (j=0; j<filterSize; j++)
1810 sum+= filter[i*filterSize + j];
1812 sum= (sum + one/2)/ one;
1813 for (j=0; j<*outFilterSize; j++)
1815 int64_t v= filter[i*filterSize + j] + error;
1816 int intV= ROUNDED_DIV(v, sum);
1817 (*outFilter)[i*(*outFilterSize) + j]= intV;
1818 error= v - intV*sum;
1822 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1823 for (i=0; i<*outFilterSize; i++)
1825 int j= dstW*(*outFilterSize);
1826 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1829 ret=0;
1830 error:
1831 av_free(filter);
1832 av_free(filter2);
1833 return ret;
1836 #ifdef COMPILE_MMX2
1837 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1839 uint8_t *fragmentA;
1840 x86_reg imm8OfPShufW1A;
1841 x86_reg imm8OfPShufW2A;
1842 x86_reg fragmentLengthA;
1843 uint8_t *fragmentB;
1844 x86_reg imm8OfPShufW1B;
1845 x86_reg imm8OfPShufW2B;
1846 x86_reg fragmentLengthB;
1847 int fragmentPos;
1849 int xpos, i;
1851 // create an optimized horizontal scaling routine
1853 //code fragment
1855 __asm__ volatile(
1856 "jmp 9f \n\t"
1857 // Begin
1858 "0: \n\t"
1859 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1860 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1861 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1862 "punpcklbw %%mm7, %%mm1 \n\t"
1863 "punpcklbw %%mm7, %%mm0 \n\t"
1864 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1865 "1: \n\t"
1866 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1867 "2: \n\t"
1868 "psubw %%mm1, %%mm0 \n\t"
1869 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1870 "pmullw %%mm3, %%mm0 \n\t"
1871 "psllw $7, %%mm1 \n\t"
1872 "paddw %%mm1, %%mm0 \n\t"
1874 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1876 "add $8, %%"REG_a" \n\t"
1877 // End
1878 "9: \n\t"
1879 // "int $3 \n\t"
1880 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1881 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1882 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1883 "dec %1 \n\t"
1884 "dec %2 \n\t"
1885 "sub %0, %1 \n\t"
1886 "sub %0, %2 \n\t"
1887 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1888 "sub %0, %3 \n\t"
1891 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1892 "=r" (fragmentLengthA)
1895 __asm__ volatile(
1896 "jmp 9f \n\t"
1897 // Begin
1898 "0: \n\t"
1899 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1900 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1901 "punpcklbw %%mm7, %%mm0 \n\t"
1902 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1903 "1: \n\t"
1904 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1905 "2: \n\t"
1906 "psubw %%mm1, %%mm0 \n\t"
1907 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1908 "pmullw %%mm3, %%mm0 \n\t"
1909 "psllw $7, %%mm1 \n\t"
1910 "paddw %%mm1, %%mm0 \n\t"
1912 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1914 "add $8, %%"REG_a" \n\t"
1915 // End
1916 "9: \n\t"
1917 // "int $3 \n\t"
1918 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1919 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1920 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1921 "dec %1 \n\t"
1922 "dec %2 \n\t"
1923 "sub %0, %1 \n\t"
1924 "sub %0, %2 \n\t"
1925 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1926 "sub %0, %3 \n\t"
1929 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1930 "=r" (fragmentLengthB)
1933 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1934 fragmentPos=0;
1936 for (i=0; i<dstW/numSplits; i++)
1938 int xx=xpos>>16;
1940 if ((i&3) == 0)
1942 int a=0;
1943 int b=((xpos+xInc)>>16) - xx;
1944 int c=((xpos+xInc*2)>>16) - xx;
1945 int d=((xpos+xInc*3)>>16) - xx;
1946 int inc = (d+1<4);
1947 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
1948 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
1949 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
1950 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1951 int maxShift= 3-(d+inc);
1952 int shift=0;
1954 if (filterCode) {
1955 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1956 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1957 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1958 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1959 filterPos[i/2]= xx;
1961 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1963 filterCode[fragmentPos + imm8OfPShufW1]=
1964 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1965 filterCode[fragmentPos + imm8OfPShufW2]=
1966 a | (b<<2) | (c<<4) | (d<<6);
1968 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1969 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1971 if (shift && i>=shift)
1973 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1974 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1975 filterPos[i/2]-=shift;
1979 fragmentPos+= fragmentLength;
1981 if (filterCode)
1982 filterCode[fragmentPos]= RET;
1984 xpos+=xInc;
1986 if (filterCode)
1987 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1989 return fragmentPos + 1;
1991 #endif /* COMPILE_MMX2 */
1993 static void globalInit(void){
1994 // generating tables:
1995 int i;
1996 for (i=0; i<768; i++){
1997 int c= av_clip_uint8(i-256);
1998 clip_table[i]=c;
2002 static SwsFunc getSwsFunc(SwsContext *c)
2004 #if CONFIG_RUNTIME_CPUDETECT
2005 int flags = c->flags;
2007 #if ARCH_X86 && CONFIG_GPL
2008 // ordered per speed fastest first
2009 if (flags & SWS_CPU_CAPS_MMX2) {
2010 sws_init_swScale_MMX2(c);
2011 return swScale_MMX2;
2012 } else if (flags & SWS_CPU_CAPS_3DNOW) {
2013 sws_init_swScale_3DNow(c);
2014 return swScale_3DNow;
2015 } else if (flags & SWS_CPU_CAPS_MMX) {
2016 sws_init_swScale_MMX(c);
2017 return swScale_MMX;
2018 } else {
2019 sws_init_swScale_C(c);
2020 return swScale_C;
2023 #else
2024 #if ARCH_PPC
2025 if (flags & SWS_CPU_CAPS_ALTIVEC) {
2026 sws_init_swScale_altivec(c);
2027 return swScale_altivec;
2028 } else {
2029 sws_init_swScale_C(c);
2030 return swScale_C;
2032 #endif
2033 sws_init_swScale_C(c);
2034 return swScale_C;
2035 #endif /* ARCH_X86 && CONFIG_GPL */
2036 #else //CONFIG_RUNTIME_CPUDETECT
2037 #if COMPILE_TEMPLATE_MMX2
2038 sws_init_swScale_MMX2(c);
2039 return swScale_MMX2;
2040 #elif COMPILE_TEMPLATE_AMD3DNOW
2041 sws_init_swScale_3DNow(c);
2042 return swScale_3DNow;
2043 #elif COMPILE_TEMPLATE_MMX
2044 sws_init_swScale_MMX(c);
2045 return swScale_MMX;
2046 #elif COMPILE_TEMPLATE_ALTIVEC
2047 sws_init_swScale_altivec(c);
2048 return swScale_altivec;
2049 #else
2050 sws_init_swScale_C(c);
2051 return swScale_C;
2052 #endif
2053 #endif //!CONFIG_RUNTIME_CPUDETECT
2056 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2057 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2058 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2059 /* Copy Y plane */
2060 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2061 memcpy(dst, src[0], srcSliceH*dstStride[0]);
2062 else
2064 int i;
2065 const uint8_t *srcPtr= src[0];
2066 uint8_t *dstPtr= dst;
2067 for (i=0; i<srcSliceH; i++)
2069 memcpy(dstPtr, srcPtr, c->srcW);
2070 srcPtr+= srcStride[0];
2071 dstPtr+= dstStride[0];
2074 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2075 if (c->dstFormat == PIX_FMT_NV12)
2076 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2077 else
2078 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2080 return srcSliceH;
2083 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2084 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2085 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2087 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2089 return srcSliceH;
2092 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2093 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2094 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2096 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2098 return srcSliceH;
2101 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2102 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2103 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2105 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2107 return srcSliceH;
2110 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2111 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2112 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2114 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2116 return srcSliceH;
2119 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2120 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2121 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2122 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2123 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2125 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2127 if (dstParam[3])
2128 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2130 return srcSliceH;
2133 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2134 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2135 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2136 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2137 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2139 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2141 return srcSliceH;
2144 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2145 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2146 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2147 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2148 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2150 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2152 if (dstParam[3])
2153 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2155 return srcSliceH;
2158 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2159 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2160 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2161 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2162 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2164 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2166 return srcSliceH;
2169 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2170 int srcSliceH, uint8_t* dst[], int dstStride[]){
2171 const enum PixelFormat srcFormat= c->srcFormat;
2172 const enum PixelFormat dstFormat= c->dstFormat;
2173 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2174 const uint8_t *palette)=NULL;
2175 int i;
2176 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2177 uint8_t *srcPtr= src[0];
2179 if (!usePal(srcFormat))
2180 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2181 sws_format_name(srcFormat), sws_format_name(dstFormat));
2183 switch(dstFormat){
2184 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2185 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2186 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2187 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2188 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2189 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2190 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2191 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2195 for (i=0; i<srcSliceH; i++) {
2196 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2197 srcPtr+= srcStride[0];
2198 dstPtr+= dstStride[0];
2201 return srcSliceH;
2204 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2205 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2206 int srcSliceH, uint8_t* dst[], int dstStride[]){
2207 const enum PixelFormat srcFormat= c->srcFormat;
2208 const enum PixelFormat dstFormat= c->dstFormat;
2209 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2210 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2211 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2212 const int dstId= fmt_depth(dstFormat) >> 2;
2213 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2215 /* BGR -> BGR */
2216 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2217 || (isRGB(srcFormat) && isRGB(dstFormat))){
2218 switch(srcId | (dstId<<4)){
2219 case 0x34: conv= rgb16to15; break;
2220 case 0x36: conv= rgb24to15; break;
2221 case 0x38: conv= rgb32to15; break;
2222 case 0x43: conv= rgb15to16; break;
2223 case 0x46: conv= rgb24to16; break;
2224 case 0x48: conv= rgb32to16; break;
2225 case 0x63: conv= rgb15to24; break;
2226 case 0x64: conv= rgb16to24; break;
2227 case 0x68: conv= rgb32to24; break;
2228 case 0x83: conv= rgb15to32; break;
2229 case 0x84: conv= rgb16to32; break;
2230 case 0x86: conv= rgb24to32; break;
2231 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2232 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2234 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2235 || (isRGB(srcFormat) && isBGR(dstFormat))){
2236 switch(srcId | (dstId<<4)){
2237 case 0x33: conv= rgb15tobgr15; break;
2238 case 0x34: conv= rgb16tobgr15; break;
2239 case 0x36: conv= rgb24tobgr15; break;
2240 case 0x38: conv= rgb32tobgr15; break;
2241 case 0x43: conv= rgb15tobgr16; break;
2242 case 0x44: conv= rgb16tobgr16; break;
2243 case 0x46: conv= rgb24tobgr16; break;
2244 case 0x48: conv= rgb32tobgr16; break;
2245 case 0x63: conv= rgb15tobgr24; break;
2246 case 0x64: conv= rgb16tobgr24; break;
2247 case 0x66: conv= rgb24tobgr24; break;
2248 case 0x68: conv= rgb32tobgr24; break;
2249 case 0x83: conv= rgb15tobgr32; break;
2250 case 0x84: conv= rgb16tobgr32; break;
2251 case 0x86: conv= rgb24tobgr32; break;
2252 case 0x88: conv= rgb32tobgr32; break;
2253 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2254 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2256 }else{
2257 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2258 sws_format_name(srcFormat), sws_format_name(dstFormat));
2261 if(conv)
2263 uint8_t *srcPtr= src[0];
2264 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2265 srcPtr += ALT32_CORR;
2267 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2268 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2269 else
2271 int i;
2272 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2274 for (i=0; i<srcSliceH; i++)
2276 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2277 srcPtr+= srcStride[0];
2278 dstPtr+= dstStride[0];
2282 return srcSliceH;
2285 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2286 int srcSliceH, uint8_t* dst[], int dstStride[]){
2288 rgb24toyv12(
2289 src[0],
2290 dst[0]+ srcSliceY *dstStride[0],
2291 dst[1]+(srcSliceY>>1)*dstStride[1],
2292 dst[2]+(srcSliceY>>1)*dstStride[2],
2293 c->srcW, srcSliceH,
2294 dstStride[0], dstStride[1], srcStride[0]);
2295 if (dst[3])
2296 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2297 return srcSliceH;
2300 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2301 int srcSliceH, uint8_t* dst[], int dstStride[]){
2302 int i;
2304 /* copy Y */
2305 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2306 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2307 else{
2308 uint8_t *srcPtr= src[0];
2309 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2311 for (i=0; i<srcSliceH; i++)
2313 memcpy(dstPtr, srcPtr, c->srcW);
2314 srcPtr+= srcStride[0];
2315 dstPtr+= dstStride[0];
2319 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2320 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2321 srcSliceH >> 2, srcStride[1], dstStride[1]);
2322 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2323 srcSliceH >> 2, srcStride[2], dstStride[2]);
2324 }else{
2325 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2326 srcSliceH >> 2, srcStride[1], dstStride[2]);
2327 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2328 srcSliceH >> 2, srcStride[2], dstStride[1]);
2330 if (dst[3])
2331 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2332 return srcSliceH;
2335 /* unscaled copy like stuff (assumes nearly identical formats) */
2336 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2337 int srcSliceH, uint8_t* dst[], int dstStride[])
2339 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2340 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2341 else
2343 int i;
2344 uint8_t *srcPtr= src[0];
2345 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2346 int length=0;
2348 /* universal length finder */
2349 while(length+c->srcW <= FFABS(dstStride[0])
2350 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2351 assert(length!=0);
2353 for (i=0; i<srcSliceH; i++)
2355 memcpy(dstPtr, srcPtr, length);
2356 srcPtr+= srcStride[0];
2357 dstPtr+= dstStride[0];
2360 return srcSliceH;
2363 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2364 int srcSliceH, uint8_t* dst[], int dstStride[])
2366 int plane, i, j;
2367 for (plane=0; plane<4; plane++)
2369 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2370 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2371 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2372 uint8_t *srcPtr= src[plane];
2373 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2375 if (!dst[plane]) continue;
2376 // ignore palette for GRAY8
2377 if (plane == 1 && !dst[2]) continue;
2378 if (!src[plane] || (plane == 1 && !src[2])){
2379 if(is16BPS(c->dstFormat))
2380 length*=2;
2381 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2382 }else
2384 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2385 if (!isBE(c->srcFormat)) srcPtr++;
2386 for (i=0; i<height; i++){
2387 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2388 srcPtr+= srcStride[plane];
2389 dstPtr+= dstStride[plane];
2391 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2392 for (i=0; i<height; i++){
2393 for (j=0; j<length; j++){
2394 dstPtr[ j<<1 ] = srcPtr[j];
2395 dstPtr[(j<<1)+1] = srcPtr[j];
2397 srcPtr+= srcStride[plane];
2398 dstPtr+= dstStride[plane];
2400 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2401 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2403 for (i=0; i<height; i++){
2404 for (j=0; j<length; j++)
2405 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2406 srcPtr+= srcStride[plane];
2407 dstPtr+= dstStride[plane];
2409 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2410 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2411 else
2413 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2414 length*=2;
2415 for (i=0; i<height; i++)
2417 memcpy(dstPtr, srcPtr, length);
2418 srcPtr+= srcStride[plane];
2419 dstPtr+= dstStride[plane];
2424 return srcSliceH;
2428 static void getSubSampleFactors(int *h, int *v, int format){
2429 switch(format){
2430 case PIX_FMT_UYVY422:
2431 case PIX_FMT_YUYV422:
2432 *h=1;
2433 *v=0;
2434 break;
2435 case PIX_FMT_YUV420P:
2436 case PIX_FMT_YUV420PLE:
2437 case PIX_FMT_YUV420PBE:
2438 case PIX_FMT_YUVA420P:
2439 case PIX_FMT_GRAY16BE:
2440 case PIX_FMT_GRAY16LE:
2441 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2442 case PIX_FMT_NV12:
2443 case PIX_FMT_NV21:
2444 *h=1;
2445 *v=1;
2446 break;
2447 case PIX_FMT_YUV440P:
2448 *h=0;
2449 *v=1;
2450 break;
2451 case PIX_FMT_YUV410P:
2452 *h=2;
2453 *v=2;
2454 break;
2455 case PIX_FMT_YUV444P:
2456 case PIX_FMT_YUV444PLE:
2457 case PIX_FMT_YUV444PBE:
2458 *h=0;
2459 *v=0;
2460 break;
2461 case PIX_FMT_YUV422P:
2462 case PIX_FMT_YUV422PLE:
2463 case PIX_FMT_YUV422PBE:
2464 *h=1;
2465 *v=0;
2466 break;
2467 case PIX_FMT_YUV411P:
2468 *h=2;
2469 *v=0;
2470 break;
2471 default:
2472 *h=0;
2473 *v=0;
2474 break;
2478 static uint16_t roundToInt16(int64_t f){
2479 int r= (f + (1<<15))>>16;
2480 if (r<-0x7FFF) return 0x8000;
2481 else if (r> 0x7FFF) return 0x7FFF;
2482 else return r;
2485 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2486 int64_t crv = inv_table[0];
2487 int64_t cbu = inv_table[1];
2488 int64_t cgu = -inv_table[2];
2489 int64_t cgv = -inv_table[3];
2490 int64_t cy = 1<<16;
2491 int64_t oy = 0;
2493 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2494 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2496 c->brightness= brightness;
2497 c->contrast = contrast;
2498 c->saturation= saturation;
2499 c->srcRange = srcRange;
2500 c->dstRange = dstRange;
2501 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2503 c->uOffset= 0x0400040004000400LL;
2504 c->vOffset= 0x0400040004000400LL;
2506 if (!srcRange){
2507 cy= (cy*255) / 219;
2508 oy= 16<<16;
2509 }else{
2510 crv= (crv*224) / 255;
2511 cbu= (cbu*224) / 255;
2512 cgu= (cgu*224) / 255;
2513 cgv= (cgv*224) / 255;
2516 cy = (cy *contrast )>>16;
2517 crv= (crv*contrast * saturation)>>32;
2518 cbu= (cbu*contrast * saturation)>>32;
2519 cgu= (cgu*contrast * saturation)>>32;
2520 cgv= (cgv*contrast * saturation)>>32;
2522 oy -= 256*brightness;
2524 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2525 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2526 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2527 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2528 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2529 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2531 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2532 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2533 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2534 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2535 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2536 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2538 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2539 //FIXME factorize
2541 #ifdef COMPILE_ALTIVEC
2542 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2543 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2544 #endif
2545 return 0;
2548 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2549 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2551 *inv_table = c->srcColorspaceTable;
2552 *table = c->dstColorspaceTable;
2553 *srcRange = c->srcRange;
2554 *dstRange = c->dstRange;
2555 *brightness= c->brightness;
2556 *contrast = c->contrast;
2557 *saturation= c->saturation;
2559 return 0;
2562 static int handle_jpeg(enum PixelFormat *format)
2564 switch (*format) {
2565 case PIX_FMT_YUVJ420P:
2566 *format = PIX_FMT_YUV420P;
2567 return 1;
2568 case PIX_FMT_YUVJ422P:
2569 *format = PIX_FMT_YUV422P;
2570 return 1;
2571 case PIX_FMT_YUVJ444P:
2572 *format = PIX_FMT_YUV444P;
2573 return 1;
2574 case PIX_FMT_YUVJ440P:
2575 *format = PIX_FMT_YUV440P;
2576 return 1;
2577 default:
2578 return 0;
2582 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2583 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2586 SwsContext *c;
2587 int i;
2588 int usesVFilter, usesHFilter;
2589 int unscaled, needsDither;
2590 int srcRange, dstRange;
2591 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2592 #if ARCH_X86
2593 if (flags & SWS_CPU_CAPS_MMX)
2594 __asm__ volatile("emms\n\t"::: "memory");
2595 #endif
2597 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2598 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2599 #if COMPILE_TEMPLATE_MMX2
2600 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2601 #elif COMPILE_TEMPLATE_AMD3DNOW
2602 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2603 #elif COMPILE_TEMPLATE_MMX
2604 flags |= SWS_CPU_CAPS_MMX;
2605 #elif COMPILE_TEMPLATE_ALTIVEC
2606 flags |= SWS_CPU_CAPS_ALTIVEC;
2607 #elif ARCH_BFIN
2608 flags |= SWS_CPU_CAPS_BFIN;
2609 #endif
2610 #endif /* CONFIG_RUNTIME_CPUDETECT */
2611 if (clip_table[512] != 255) globalInit();
2612 if (!rgb15to16) sws_rgb2rgb_init(flags);
2614 unscaled = (srcW == dstW && srcH == dstH);
2615 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2616 && (fmt_depth(dstFormat))<24
2617 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2619 srcRange = handle_jpeg(&srcFormat);
2620 dstRange = handle_jpeg(&dstFormat);
2622 if (!isSupportedIn(srcFormat))
2624 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2625 return NULL;
2627 if (!isSupportedOut(dstFormat))
2629 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2630 return NULL;
2633 i= flags & ( SWS_POINT
2634 |SWS_AREA
2635 |SWS_BILINEAR
2636 |SWS_FAST_BILINEAR
2637 |SWS_BICUBIC
2638 |SWS_X
2639 |SWS_GAUSS
2640 |SWS_LANCZOS
2641 |SWS_SINC
2642 |SWS_SPLINE
2643 |SWS_BICUBLIN);
2644 if(!i || (i & (i-1)))
2646 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2647 return NULL;
2650 /* sanity check */
2651 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
2653 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2654 srcW, srcH, dstW, dstH);
2655 return NULL;
2657 if(srcW > VOFW || dstW > VOFW){
2658 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2659 return NULL;
2662 if (!dstFilter) dstFilter= &dummyFilter;
2663 if (!srcFilter) srcFilter= &dummyFilter;
2665 c= av_mallocz(sizeof(SwsContext));
2667 c->av_class = &sws_context_class;
2668 c->srcW= srcW;
2669 c->srcH= srcH;
2670 c->dstW= dstW;
2671 c->dstH= dstH;
2672 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2673 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2674 c->flags= flags;
2675 c->dstFormat= dstFormat;
2676 c->srcFormat= srcFormat;
2677 c->vRounder= 4* 0x0001000100010001ULL;
2679 usesHFilter= usesVFilter= 0;
2680 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2681 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2682 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2683 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2684 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2685 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2686 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2687 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2689 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2690 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2692 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2693 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2695 // drop some chroma lines if the user wants it
2696 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2697 c->chrSrcVSubSample+= c->vChrDrop;
2699 // drop every other pixel for chroma calculation unless user wants full chroma
2700 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2701 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2702 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2703 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2704 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2705 c->chrSrcHSubSample=1;
2707 if (param){
2708 c->param[0] = param[0];
2709 c->param[1] = param[1];
2710 }else{
2711 c->param[0] =
2712 c->param[1] = SWS_PARAM_DEFAULT;
2715 // Note the -((-x)>>y) is so that we always round toward +inf.
2716 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2717 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2718 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2719 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2721 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2723 /* unscaled special cases */
2724 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2726 /* yv12_to_nv12 */
2727 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2729 c->swScale= PlanarToNV12Wrapper;
2731 /* yuv2bgr */
2732 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2733 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2735 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2738 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2740 c->swScale= yvu9toyv12Wrapper;
2743 /* bgr24toYV12 */
2744 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2745 c->swScale= bgr24toyv12Wrapper;
2747 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2748 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2749 && (isBGR(dstFormat) || isRGB(dstFormat))
2750 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2751 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2752 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2753 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2754 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2755 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2756 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2757 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2758 && dstFormat != PIX_FMT_RGB32_1
2759 && dstFormat != PIX_FMT_BGR32_1
2760 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2761 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2762 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2763 c->swScale= rgb2rgbWrapper;
2765 if ((usePal(srcFormat) && (
2766 dstFormat == PIX_FMT_RGB32 ||
2767 dstFormat == PIX_FMT_RGB32_1 ||
2768 dstFormat == PIX_FMT_RGB24 ||
2769 dstFormat == PIX_FMT_BGR32 ||
2770 dstFormat == PIX_FMT_BGR32_1 ||
2771 dstFormat == PIX_FMT_BGR24)))
2772 c->swScale= pal2rgbWrapper;
2774 if (srcFormat == PIX_FMT_YUV422P)
2776 if (dstFormat == PIX_FMT_YUYV422)
2777 c->swScale= YUV422PToYuy2Wrapper;
2778 else if (dstFormat == PIX_FMT_UYVY422)
2779 c->swScale= YUV422PToUyvyWrapper;
2782 /* LQ converters if -sws 0 or -sws 4*/
2783 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2784 /* yv12_to_yuy2 */
2785 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2787 if (dstFormat == PIX_FMT_YUYV422)
2788 c->swScale= PlanarToYuy2Wrapper;
2789 else if (dstFormat == PIX_FMT_UYVY422)
2790 c->swScale= PlanarToUyvyWrapper;
2793 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2794 c->swScale= YUYV2YUV420Wrapper;
2795 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2796 c->swScale= UYVY2YUV420Wrapper;
2797 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2798 c->swScale= YUYV2YUV422Wrapper;
2799 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2800 c->swScale= UYVY2YUV422Wrapper;
2802 #ifdef COMPILE_ALTIVEC
2803 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2804 !(c->flags & SWS_BITEXACT) &&
2805 srcFormat == PIX_FMT_YUV420P) {
2806 // unscaled YV12 -> packed YUV, we want speed
2807 if (dstFormat == PIX_FMT_YUYV422)
2808 c->swScale= yv12toyuy2_unscaled_altivec;
2809 else if (dstFormat == PIX_FMT_UYVY422)
2810 c->swScale= yv12touyvy_unscaled_altivec;
2812 #endif
2814 /* simple copy */
2815 if ( srcFormat == dstFormat
2816 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2817 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2818 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2819 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2820 || (isGray(dstFormat) && isGray(srcFormat))
2821 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2822 && c->chrDstHSubSample == c->chrSrcHSubSample
2823 && c->chrDstVSubSample == c->chrSrcVSubSample
2824 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2825 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2827 if (isPacked(c->srcFormat))
2828 c->swScale= packedCopy;
2829 else /* Planar YUV or gray */
2830 c->swScale= planarCopy;
2832 #if ARCH_BFIN
2833 if (flags & SWS_CPU_CAPS_BFIN)
2834 ff_bfin_get_unscaled_swscale (c);
2835 #endif
2837 if (c->swScale){
2838 if (flags&SWS_PRINT_INFO)
2839 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2840 sws_format_name(srcFormat), sws_format_name(dstFormat));
2841 return c;
2845 if (flags & SWS_CPU_CAPS_MMX2)
2847 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2848 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2850 if (flags&SWS_PRINT_INFO)
2851 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2853 if (usesHFilter) c->canMMX2BeUsed=0;
2855 else
2856 c->canMMX2BeUsed=0;
2858 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2859 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2861 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2862 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2863 // n-2 is the last chrominance sample available
2864 // this is not perfect, but no one should notice the difference, the more correct variant
2865 // would be like the vertical one, but that would require some special code for the
2866 // first and last pixel
2867 if (flags&SWS_FAST_BILINEAR)
2869 if (c->canMMX2BeUsed)
2871 c->lumXInc+= 20;
2872 c->chrXInc+= 20;
2874 //we don't use the x86 asm scaler if MMX is available
2875 else if (flags & SWS_CPU_CAPS_MMX)
2877 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2878 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2882 /* precalculate horizontal scaler filter coefficients */
2884 const int filterAlign=
2885 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2886 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2889 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2890 srcW , dstW, filterAlign, 1<<14,
2891 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2892 srcFilter->lumH, dstFilter->lumH, c->param);
2893 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2894 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2895 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2896 srcFilter->chrH, dstFilter->chrH, c->param);
2898 #if defined(COMPILE_MMX2)
2899 // can't downscale !!!
2900 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2902 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
2903 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2905 #ifdef MAP_ANONYMOUS
2906 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2907 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2908 #elif HAVE_VIRTUALALLOC
2909 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2910 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2911 #else
2912 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2913 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2914 #endif
2916 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2917 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2918 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2919 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2921 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2922 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2924 #ifdef MAP_ANONYMOUS
2925 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2926 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2927 #endif
2929 #endif /* defined(COMPILE_MMX2) */
2930 } // initialize horizontal stuff
2934 /* precalculate vertical scaler filter coefficients */
2936 const int filterAlign=
2937 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2938 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2941 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2942 srcH , dstH, filterAlign, (1<<12),
2943 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2944 srcFilter->lumV, dstFilter->lumV, c->param);
2945 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2946 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2947 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2948 srcFilter->chrV, dstFilter->chrV, c->param);
2950 #ifdef COMPILE_ALTIVEC
2951 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2952 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2954 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2955 int j;
2956 short *p = (short *)&c->vYCoeffsBank[i];
2957 for (j=0;j<8;j++)
2958 p[j] = c->vLumFilter[i];
2961 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2962 int j;
2963 short *p = (short *)&c->vCCoeffsBank[i];
2964 for (j=0;j<8;j++)
2965 p[j] = c->vChrFilter[i];
2967 #endif
2970 // calculate buffer sizes so that they won't run out while handling these damn slices
2971 c->vLumBufSize= c->vLumFilterSize;
2972 c->vChrBufSize= c->vChrFilterSize;
2973 for (i=0; i<dstH; i++)
2975 int chrI= i*c->chrDstH / dstH;
2976 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2977 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2979 nextSlice>>= c->chrSrcVSubSample;
2980 nextSlice<<= c->chrSrcVSubSample;
2981 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2982 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2983 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2984 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2987 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2988 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2989 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2990 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2991 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2992 //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)
2993 /* align at 16 bytes for AltiVec */
2994 for (i=0; i<c->vLumBufSize; i++)
2995 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2996 for (i=0; i<c->vChrBufSize; i++)
2997 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2998 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2999 for (i=0; i<c->vLumBufSize; i++)
3000 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
3002 //try to avoid drawing green stuff between the right end and the stride end
3003 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
3005 assert(2*VOFW == VOF);
3007 assert(c->chrDstH <= dstH);
3009 if (flags&SWS_PRINT_INFO)
3011 #ifdef DITHER1XBPP
3012 const char *dither= " dithered";
3013 #else
3014 const char *dither= "";
3015 #endif
3016 if (flags&SWS_FAST_BILINEAR)
3017 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
3018 else if (flags&SWS_BILINEAR)
3019 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
3020 else if (flags&SWS_BICUBIC)
3021 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
3022 else if (flags&SWS_X)
3023 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
3024 else if (flags&SWS_POINT)
3025 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
3026 else if (flags&SWS_AREA)
3027 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
3028 else if (flags&SWS_BICUBLIN)
3029 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
3030 else if (flags&SWS_GAUSS)
3031 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
3032 else if (flags&SWS_SINC)
3033 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
3034 else if (flags&SWS_LANCZOS)
3035 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
3036 else if (flags&SWS_SPLINE)
3037 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
3038 else
3039 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
3041 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
3042 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
3043 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
3044 else
3045 av_log(c, AV_LOG_INFO, "from %s to %s ",
3046 sws_format_name(srcFormat), sws_format_name(dstFormat));
3048 if (flags & SWS_CPU_CAPS_MMX2)
3049 av_log(c, AV_LOG_INFO, "using MMX2\n");
3050 else if (flags & SWS_CPU_CAPS_3DNOW)
3051 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
3052 else if (flags & SWS_CPU_CAPS_MMX)
3053 av_log(c, AV_LOG_INFO, "using MMX\n");
3054 else if (flags & SWS_CPU_CAPS_ALTIVEC)
3055 av_log(c, AV_LOG_INFO, "using AltiVec\n");
3056 else
3057 av_log(c, AV_LOG_INFO, "using C\n");
3060 if (flags & SWS_PRINT_INFO)
3062 if (flags & SWS_CPU_CAPS_MMX)
3064 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
3065 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3066 else
3068 if (c->hLumFilterSize==4)
3069 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3070 else if (c->hLumFilterSize==8)
3071 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3072 else
3073 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3075 if (c->hChrFilterSize==4)
3076 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3077 else if (c->hChrFilterSize==8)
3078 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3079 else
3080 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3083 else
3085 #if ARCH_X86
3086 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3087 #else
3088 if (flags & SWS_FAST_BILINEAR)
3089 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3090 else
3091 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3092 #endif
3094 if (isPlanarYUV(dstFormat))
3096 if (c->vLumFilterSize==1)
3097 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3098 else
3099 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3101 else
3103 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3104 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3105 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3106 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3107 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3108 else
3109 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3112 if (dstFormat==PIX_FMT_BGR24)
3113 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3114 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3115 else if (dstFormat==PIX_FMT_RGB32)
3116 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3117 else if (dstFormat==PIX_FMT_BGR565)
3118 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3119 else if (dstFormat==PIX_FMT_BGR555)
3120 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3122 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3124 if (flags & SWS_PRINT_INFO)
3126 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3127 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3128 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3129 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3132 c->swScale= getSwsFunc(c);
3133 return c;
3136 static void reset_ptr(uint8_t* src[], int format){
3137 if(!isALPHA(format))
3138 src[3]=NULL;
3139 if(!isPlanarYUV(format)){
3140 src[3]=src[2]=NULL;
3141 if( format != PIX_FMT_PAL8
3142 && format != PIX_FMT_RGB8
3143 && format != PIX_FMT_BGR8
3144 && format != PIX_FMT_RGB4_BYTE
3145 && format != PIX_FMT_BGR4_BYTE
3147 src[1]= NULL;
3152 * swscale wrapper, so we don't need to export the SwsContext.
3153 * Assumes planar YUV to be in YUV order instead of YVU.
3155 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3156 int srcSliceH, uint8_t* dst[], int dstStride[]){
3157 int i;
3158 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3159 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3161 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3162 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3163 return 0;
3165 if (c->sliceDir == 0) {
3166 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3169 if (usePal(c->srcFormat)){
3170 for (i=0; i<256; i++){
3171 int p, r, g, b,y,u,v;
3172 if(c->srcFormat == PIX_FMT_PAL8){
3173 p=((uint32_t*)(src[1]))[i];
3174 r= (p>>16)&0xFF;
3175 g= (p>> 8)&0xFF;
3176 b= p &0xFF;
3177 }else if(c->srcFormat == PIX_FMT_RGB8){
3178 r= (i>>5 )*36;
3179 g= ((i>>2)&7)*36;
3180 b= (i&3 )*85;
3181 }else if(c->srcFormat == PIX_FMT_BGR8){
3182 b= (i>>6 )*85;
3183 g= ((i>>3)&7)*36;
3184 r= (i&7 )*36;
3185 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3186 r= (i>>3 )*255;
3187 g= ((i>>1)&3)*85;
3188 b= (i&1 )*255;
3189 }else {
3190 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3191 b= (i>>3 )*255;
3192 g= ((i>>1)&3)*85;
3193 r= (i&1 )*255;
3195 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3196 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3197 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3198 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3201 switch(c->dstFormat) {
3202 case PIX_FMT_BGR32:
3203 #if !HAVE_BIGENDIAN
3204 case PIX_FMT_RGB24:
3205 #endif
3206 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3207 break;
3208 case PIX_FMT_BGR32_1:
3209 #if HAVE_BIGENDIAN
3210 case PIX_FMT_BGR24:
3211 #endif
3212 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3213 break;
3214 case PIX_FMT_RGB32_1:
3215 #if HAVE_BIGENDIAN
3216 case PIX_FMT_RGB24:
3217 #endif
3218 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3219 break;
3220 case PIX_FMT_RGB32:
3221 #if !HAVE_BIGENDIAN
3222 case PIX_FMT_BGR24:
3223 #endif
3224 default:
3225 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3230 // copy strides, so they can safely be modified
3231 if (c->sliceDir == 1) {
3232 // slices go from top to bottom
3233 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3234 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3236 reset_ptr(src2, c->srcFormat);
3237 reset_ptr(dst2, c->dstFormat);
3239 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3240 } else {
3241 // slices go from bottom to top => we flip the image internally
3242 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3243 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3245 src2[0] += (srcSliceH-1)*srcStride[0];
3246 if (!usePal(c->srcFormat))
3247 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3248 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3249 src2[3] += (srcSliceH-1)*srcStride[3];
3250 dst2[0] += ( c->dstH -1)*dstStride[0];
3251 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3252 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3253 dst2[3] += ( c->dstH -1)*dstStride[3];
3255 reset_ptr(src2, c->srcFormat);
3256 reset_ptr(dst2, c->dstFormat);
3258 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3262 #if LIBSWSCALE_VERSION_MAJOR < 1
3263 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3264 int srcSliceH, uint8_t* dst[], int dstStride[]){
3265 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3267 #endif
3269 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3270 float lumaSharpen, float chromaSharpen,
3271 float chromaHShift, float chromaVShift,
3272 int verbose)
3274 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3276 if (lumaGBlur!=0.0){
3277 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3278 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3279 }else{
3280 filter->lumH= sws_getIdentityVec();
3281 filter->lumV= sws_getIdentityVec();
3284 if (chromaGBlur!=0.0){
3285 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3286 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3287 }else{
3288 filter->chrH= sws_getIdentityVec();
3289 filter->chrV= sws_getIdentityVec();
3292 if (chromaSharpen!=0.0){
3293 SwsVector *id= sws_getIdentityVec();
3294 sws_scaleVec(filter->chrH, -chromaSharpen);
3295 sws_scaleVec(filter->chrV, -chromaSharpen);
3296 sws_addVec(filter->chrH, id);
3297 sws_addVec(filter->chrV, id);
3298 sws_freeVec(id);
3301 if (lumaSharpen!=0.0){
3302 SwsVector *id= sws_getIdentityVec();
3303 sws_scaleVec(filter->lumH, -lumaSharpen);
3304 sws_scaleVec(filter->lumV, -lumaSharpen);
3305 sws_addVec(filter->lumH, id);
3306 sws_addVec(filter->lumV, id);
3307 sws_freeVec(id);
3310 if (chromaHShift != 0.0)
3311 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3313 if (chromaVShift != 0.0)
3314 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3316 sws_normalizeVec(filter->chrH, 1.0);
3317 sws_normalizeVec(filter->chrV, 1.0);
3318 sws_normalizeVec(filter->lumH, 1.0);
3319 sws_normalizeVec(filter->lumV, 1.0);
3321 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3322 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3324 return filter;
3327 SwsVector *sws_getGaussianVec(double variance, double quality){
3328 const int length= (int)(variance*quality + 0.5) | 1;
3329 int i;
3330 double *coeff= av_malloc(length*sizeof(double));
3331 double middle= (length-1)*0.5;
3332 SwsVector *vec= av_malloc(sizeof(SwsVector));
3334 vec->coeff= coeff;
3335 vec->length= length;
3337 for (i=0; i<length; i++)
3339 double dist= i-middle;
3340 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3343 sws_normalizeVec(vec, 1.0);
3345 return vec;
3348 SwsVector *sws_getConstVec(double c, int length){
3349 int i;
3350 double *coeff= av_malloc(length*sizeof(double));
3351 SwsVector *vec= av_malloc(sizeof(SwsVector));
3353 vec->coeff= coeff;
3354 vec->length= length;
3356 for (i=0; i<length; i++)
3357 coeff[i]= c;
3359 return vec;
3363 SwsVector *sws_getIdentityVec(void){
3364 return sws_getConstVec(1.0, 1);
3367 double sws_dcVec(SwsVector *a){
3368 int i;
3369 double sum=0;
3371 for (i=0; i<a->length; i++)
3372 sum+= a->coeff[i];
3374 return sum;
3377 void sws_scaleVec(SwsVector *a, double scalar){
3378 int i;
3380 for (i=0; i<a->length; i++)
3381 a->coeff[i]*= scalar;
3384 void sws_normalizeVec(SwsVector *a, double height){
3385 sws_scaleVec(a, height/sws_dcVec(a));
3388 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3389 int length= a->length + b->length - 1;
3390 double *coeff= av_malloc(length*sizeof(double));
3391 int i, j;
3392 SwsVector *vec= av_malloc(sizeof(SwsVector));
3394 vec->coeff= coeff;
3395 vec->length= length;
3397 for (i=0; i<length; i++) coeff[i]= 0.0;
3399 for (i=0; i<a->length; i++)
3401 for (j=0; j<b->length; j++)
3403 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3407 return vec;
3410 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3411 int length= FFMAX(a->length, b->length);
3412 double *coeff= av_malloc(length*sizeof(double));
3413 int i;
3414 SwsVector *vec= av_malloc(sizeof(SwsVector));
3416 vec->coeff= coeff;
3417 vec->length= length;
3419 for (i=0; i<length; i++) coeff[i]= 0.0;
3421 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3422 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3424 return vec;
3427 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3428 int length= FFMAX(a->length, b->length);
3429 double *coeff= av_malloc(length*sizeof(double));
3430 int i;
3431 SwsVector *vec= av_malloc(sizeof(SwsVector));
3433 vec->coeff= coeff;
3434 vec->length= length;
3436 for (i=0; i<length; i++) coeff[i]= 0.0;
3438 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3439 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3441 return vec;
3444 /* shift left / or right if "shift" is negative */
3445 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3446 int length= a->length + FFABS(shift)*2;
3447 double *coeff= av_malloc(length*sizeof(double));
3448 int i;
3449 SwsVector *vec= av_malloc(sizeof(SwsVector));
3451 vec->coeff= coeff;
3452 vec->length= length;
3454 for (i=0; i<length; i++) coeff[i]= 0.0;
3456 for (i=0; i<a->length; i++)
3458 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3461 return vec;
3464 void sws_shiftVec(SwsVector *a, int shift){
3465 SwsVector *shifted= sws_getShiftedVec(a, shift);
3466 av_free(a->coeff);
3467 a->coeff= shifted->coeff;
3468 a->length= shifted->length;
3469 av_free(shifted);
3472 void sws_addVec(SwsVector *a, SwsVector *b){
3473 SwsVector *sum= sws_sumVec(a, b);
3474 av_free(a->coeff);
3475 a->coeff= sum->coeff;
3476 a->length= sum->length;
3477 av_free(sum);
3480 void sws_subVec(SwsVector *a, SwsVector *b){
3481 SwsVector *diff= sws_diffVec(a, b);
3482 av_free(a->coeff);
3483 a->coeff= diff->coeff;
3484 a->length= diff->length;
3485 av_free(diff);
3488 void sws_convVec(SwsVector *a, SwsVector *b){
3489 SwsVector *conv= sws_getConvVec(a, b);
3490 av_free(a->coeff);
3491 a->coeff= conv->coeff;
3492 a->length= conv->length;
3493 av_free(conv);
3496 SwsVector *sws_cloneVec(SwsVector *a){
3497 double *coeff= av_malloc(a->length*sizeof(double));
3498 int i;
3499 SwsVector *vec= av_malloc(sizeof(SwsVector));
3501 vec->coeff= coeff;
3502 vec->length= a->length;
3504 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3506 return vec;
3509 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3510 int i;
3511 double max=0;
3512 double min=0;
3513 double range;
3515 for (i=0; i<a->length; i++)
3516 if (a->coeff[i]>max) max= a->coeff[i];
3518 for (i=0; i<a->length; i++)
3519 if (a->coeff[i]<min) min= a->coeff[i];
3521 range= max - min;
3523 for (i=0; i<a->length; i++)
3525 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3526 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3527 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3528 av_log(log_ctx, log_level, "|\n");
3532 #if LIBSWSCALE_VERSION_MAJOR < 1
3533 void sws_printVec(SwsVector *a){
3534 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3536 #endif
3538 void sws_freeVec(SwsVector *a){
3539 if (!a) return;
3540 av_freep(&a->coeff);
3541 a->length=0;
3542 av_free(a);
3545 void sws_freeFilter(SwsFilter *filter){
3546 if (!filter) return;
3548 if (filter->lumH) sws_freeVec(filter->lumH);
3549 if (filter->lumV) sws_freeVec(filter->lumV);
3550 if (filter->chrH) sws_freeVec(filter->chrH);
3551 if (filter->chrV) sws_freeVec(filter->chrV);
3552 av_free(filter);
3556 void sws_freeContext(SwsContext *c){
3557 int i;
3558 if (!c) return;
3560 if (c->lumPixBuf)
3562 for (i=0; i<c->vLumBufSize; i++)
3563 av_freep(&c->lumPixBuf[i]);
3564 av_freep(&c->lumPixBuf);
3567 if (c->chrPixBuf)
3569 for (i=0; i<c->vChrBufSize; i++)
3570 av_freep(&c->chrPixBuf[i]);
3571 av_freep(&c->chrPixBuf);
3574 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3575 for (i=0; i<c->vLumBufSize; i++)
3576 av_freep(&c->alpPixBuf[i]);
3577 av_freep(&c->alpPixBuf);
3580 av_freep(&c->vLumFilter);
3581 av_freep(&c->vChrFilter);
3582 av_freep(&c->hLumFilter);
3583 av_freep(&c->hChrFilter);
3584 #ifdef COMPILE_ALTIVEC
3585 av_freep(&c->vYCoeffsBank);
3586 av_freep(&c->vCCoeffsBank);
3587 #endif
3589 av_freep(&c->vLumFilterPos);
3590 av_freep(&c->vChrFilterPos);
3591 av_freep(&c->hLumFilterPos);
3592 av_freep(&c->hChrFilterPos);
3594 #if ARCH_X86 && CONFIG_GPL
3595 #ifdef MAP_ANONYMOUS
3596 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3597 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3598 #elif HAVE_VIRTUALALLOC
3599 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3600 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3601 #else
3602 av_free(c->lumMmx2FilterCode);
3603 av_free(c->chrMmx2FilterCode);
3604 #endif
3605 c->lumMmx2FilterCode=NULL;
3606 c->chrMmx2FilterCode=NULL;
3607 #endif /* ARCH_X86 && CONFIG_GPL */
3609 av_freep(&c->lumMmx2Filter);
3610 av_freep(&c->chrMmx2Filter);
3611 av_freep(&c->lumMmx2FilterPos);
3612 av_freep(&c->chrMmx2FilterPos);
3613 av_freep(&c->yuvTable);
3615 av_free(c);
3618 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3619 int srcW, int srcH, enum PixelFormat srcFormat,
3620 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3621 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3623 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3625 if (!param)
3626 param = default_param;
3628 if (context) {
3629 if (context->srcW != srcW || context->srcH != srcH ||
3630 context->srcFormat != srcFormat ||
3631 context->dstW != dstW || context->dstH != dstH ||
3632 context->dstFormat != dstFormat || context->flags != flags ||
3633 context->param[0] != param[0] || context->param[1] != param[1])
3635 sws_freeContext(context);
3636 context = NULL;
3639 if (!context) {
3640 return sws_getContext(srcW, srcH, srcFormat,
3641 dstW, dstH, dstFormat, flags,
3642 srcFilter, dstFilter, param);
3644 return context;