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}
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
41 tested special converters (most are tested actually, but I did not write it down ...)
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
57 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
66 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67 #define MAP_ANONYMOUS MAP_ANON
71 #define WIN32_LEAN_AND_MEAN
75 #include "swscale_internal.h"
77 #include "libavutil/x86_cpu.h"
78 #include "libavutil/bswap.h"
80 unsigned swscale_version(void)
82 return LIBSWSCALE_VERSION_INT;
89 //#define HAVE_AMD3DNOW
94 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
96 #define RET 0xC3 //near return opcode for x86
101 #define PI 3.14159265358979323846
104 #define isSupportedIn(x) ( \
105 (x)==PIX_FMT_YUV420P \
106 || (x)==PIX_FMT_YUVA420P \
107 || (x)==PIX_FMT_YUYV422 \
108 || (x)==PIX_FMT_UYVY422 \
109 || (x)==PIX_FMT_RGB48BE \
110 || (x)==PIX_FMT_RGB48LE \
111 || (x)==PIX_FMT_RGB32 \
112 || (x)==PIX_FMT_RGB32_1 \
113 || (x)==PIX_FMT_BGR24 \
114 || (x)==PIX_FMT_BGR565 \
115 || (x)==PIX_FMT_BGR555 \
116 || (x)==PIX_FMT_BGR32 \
117 || (x)==PIX_FMT_BGR32_1 \
118 || (x)==PIX_FMT_RGB24 \
119 || (x)==PIX_FMT_RGB565 \
120 || (x)==PIX_FMT_RGB555 \
121 || (x)==PIX_FMT_GRAY8 \
122 || (x)==PIX_FMT_YUV410P \
123 || (x)==PIX_FMT_YUV440P \
124 || (x)==PIX_FMT_GRAY16BE \
125 || (x)==PIX_FMT_GRAY16LE \
126 || (x)==PIX_FMT_YUV444P \
127 || (x)==PIX_FMT_YUV422P \
128 || (x)==PIX_FMT_YUV411P \
129 || (x)==PIX_FMT_PAL8 \
130 || (x)==PIX_FMT_BGR8 \
131 || (x)==PIX_FMT_RGB8 \
132 || (x)==PIX_FMT_BGR4_BYTE \
133 || (x)==PIX_FMT_RGB4_BYTE \
134 || (x)==PIX_FMT_YUV440P \
135 || (x)==PIX_FMT_MONOWHITE \
136 || (x)==PIX_FMT_MONOBLACK \
137 || (x)==PIX_FMT_YUV420PLE \
138 || (x)==PIX_FMT_YUV422PLE \
139 || (x)==PIX_FMT_YUV444PLE \
140 || (x)==PIX_FMT_YUV420PBE \
141 || (x)==PIX_FMT_YUV422PBE \
142 || (x)==PIX_FMT_YUV444PBE \
144 #define isSupportedOut(x) ( \
145 (x)==PIX_FMT_YUV420P \
146 || (x)==PIX_FMT_YUVA420P \
147 || (x)==PIX_FMT_YUYV422 \
148 || (x)==PIX_FMT_UYVY422 \
149 || (x)==PIX_FMT_YUV444P \
150 || (x)==PIX_FMT_YUV422P \
151 || (x)==PIX_FMT_YUV411P \
154 || (x)==PIX_FMT_NV12 \
155 || (x)==PIX_FMT_NV21 \
156 || (x)==PIX_FMT_GRAY16BE \
157 || (x)==PIX_FMT_GRAY16LE \
158 || (x)==PIX_FMT_GRAY8 \
159 || (x)==PIX_FMT_YUV410P \
160 || (x)==PIX_FMT_YUV440P \
161 || (x)==PIX_FMT_YUV420PLE \
162 || (x)==PIX_FMT_YUV422PLE \
163 || (x)==PIX_FMT_YUV444PLE \
164 || (x)==PIX_FMT_YUV420PBE \
165 || (x)==PIX_FMT_YUV422PBE \
166 || (x)==PIX_FMT_YUV444PBE \
168 #define isPacked(x) ( \
170 || (x)==PIX_FMT_YUYV422 \
171 || (x)==PIX_FMT_UYVY422 \
175 #define usePal(x) ( \
177 || (x)==PIX_FMT_BGR4_BYTE \
178 || (x)==PIX_FMT_RGB4_BYTE \
179 || (x)==PIX_FMT_BGR8 \
180 || (x)==PIX_FMT_RGB8 \
183 #define RGB2YUV_SHIFT 15
184 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
185 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
186 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
187 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
188 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
189 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
190 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
191 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
192 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
194 extern const int32_t ff_yuv2rgb_coeffs[8][4];
196 static const double rgb2yuv_table[8][9]={
197 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
198 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
199 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
200 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
201 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
202 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
203 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
204 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
209 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
212 more intelligent misalignment avoidance for the horizontal scaler
213 write special vertical cubic upscale version
214 optimize C code (YV12 / minmax)
215 add support for packed pixel YUV input & output
216 add support for Y8 output
217 optimize BGR24 & BGR32
218 add BGR4 output support
219 write special BGR->BGR scaler
222 #if ARCH_X86 && CONFIG_GPL
223 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
224 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
225 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
226 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
227 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
228 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
229 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
230 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
232 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
233 0x0103010301030103LL,
234 0x0200020002000200LL,};
236 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
237 0x0602060206020602LL,
238 0x0004000400040004LL,};
240 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
241 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
242 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
243 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
244 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
245 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
247 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
248 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
252 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
253 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
254 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
256 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
257 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
258 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
259 #endif /* FAST_BGR2YV12 */
260 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
261 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
262 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
264 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
265 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
266 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
267 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
268 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
270 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
271 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
272 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
275 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
277 #endif /* ARCH_X86 && CONFIG_GPL */
279 // clipping helper table for C implementations:
280 static unsigned char clip_table[768];
282 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
284 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
285 { 1, 3, 1, 3, 1, 3, 1, 3, },
286 { 2, 0, 2, 0, 2, 0, 2, 0, },
289 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
290 { 6, 2, 6, 2, 6, 2, 6, 2, },
291 { 0, 4, 0, 4, 0, 4, 0, 4, },
294 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
295 { 17, 9, 23, 15, 16, 8, 22, 14, },
296 { 5, 29, 3, 27, 4, 28, 2, 26, },
297 { 21, 13, 19, 11, 20, 12, 18, 10, },
298 { 0, 24, 6, 30, 1, 25, 7, 31, },
299 { 16, 8, 22, 14, 17, 9, 23, 15, },
300 { 4, 28, 2, 26, 5, 29, 3, 27, },
301 { 20, 12, 18, 10, 21, 13, 19, 11, },
302 { 1, 25, 7, 31, 0, 24, 6, 30, },
306 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_64[8][8])={
307 { 0, 48, 12, 60, 3, 51, 15, 63, },
308 { 32, 16, 44, 28, 35, 19, 47, 31, },
309 { 8, 56, 4, 52, 11, 59, 7, 55, },
310 { 40, 24, 36, 20, 43, 27, 39, 23, },
311 { 2, 50, 14, 62, 1, 49, 13, 61, },
312 { 34, 18, 46, 30, 33, 17, 45, 29, },
313 { 10, 58, 6, 54, 9, 57, 5, 53, },
314 { 42, 26, 38, 22, 41, 25, 37, 21, },
318 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
319 { 0, 55, 14, 68, 3, 58, 17, 72, },
320 { 37, 18, 50, 32, 40, 22, 54, 35, },
321 { 9, 64, 5, 59, 13, 67, 8, 63, },
322 { 46, 27, 41, 23, 49, 31, 44, 26, },
323 { 2, 57, 16, 71, 1, 56, 15, 70, },
324 { 39, 21, 52, 34, 38, 19, 51, 33, },
325 { 11, 66, 7, 62, 10, 65, 6, 60, },
326 { 48, 30, 43, 25, 47, 29, 42, 24, },
330 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128[8][8])={
331 { 68, 36, 92, 60, 66, 34, 90, 58, },
332 { 20, 116, 12, 108, 18, 114, 10, 106, },
333 { 84, 52, 76, 44, 82, 50, 74, 42, },
334 { 0, 96, 24, 120, 6, 102, 30, 126, },
335 { 64, 32, 88, 56, 70, 38, 94, 62, },
336 { 16, 112, 8, 104, 22, 118, 14, 110, },
337 { 80, 48, 72, 40, 86, 54, 78, 46, },
338 { 4, 100, 28, 124, 2, 98, 26, 122, },
343 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
344 {117, 62, 158, 103, 113, 58, 155, 100, },
345 { 34, 199, 21, 186, 31, 196, 17, 182, },
346 {144, 89, 131, 76, 141, 86, 127, 72, },
347 { 0, 165, 41, 206, 10, 175, 52, 217, },
348 {110, 55, 151, 96, 120, 65, 162, 107, },
349 { 28, 193, 14, 179, 38, 203, 24, 189, },
350 {138, 83, 124, 69, 148, 93, 134, 79, },
351 { 7, 172, 48, 213, 3, 168, 45, 210, },
354 // tries to correct a gamma of 1.5
355 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
356 { 0, 143, 18, 200, 2, 156, 25, 215, },
357 { 78, 28, 125, 64, 89, 36, 138, 74, },
358 { 10, 180, 3, 161, 16, 195, 8, 175, },
359 {109, 51, 93, 38, 121, 60, 105, 47, },
360 { 1, 152, 23, 210, 0, 147, 20, 205, },
361 { 85, 33, 134, 71, 81, 30, 130, 67, },
362 { 14, 190, 6, 171, 12, 185, 5, 166, },
363 {117, 57, 101, 44, 113, 54, 97, 41, },
366 // tries to correct a gamma of 2.0
367 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
368 { 0, 124, 8, 193, 0, 140, 12, 213, },
369 { 55, 14, 104, 42, 66, 19, 119, 52, },
370 { 3, 168, 1, 145, 6, 187, 3, 162, },
371 { 86, 31, 70, 21, 99, 39, 82, 28, },
372 { 0, 134, 11, 206, 0, 129, 9, 200, },
373 { 62, 17, 114, 48, 58, 16, 109, 45, },
374 { 5, 181, 2, 157, 4, 175, 1, 151, },
375 { 95, 36, 78, 26, 90, 34, 74, 24, },
378 // tries to correct a gamma of 2.5
379 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
380 { 0, 107, 3, 187, 0, 125, 6, 212, },
381 { 39, 7, 86, 28, 49, 11, 102, 36, },
382 { 1, 158, 0, 131, 3, 180, 1, 151, },
383 { 68, 19, 52, 12, 81, 25, 64, 17, },
384 { 0, 119, 5, 203, 0, 113, 4, 195, },
385 { 45, 9, 96, 33, 42, 8, 91, 30, },
386 { 2, 172, 1, 144, 2, 165, 0, 137, },
387 { 77, 23, 60, 15, 72, 21, 56, 14, },
391 const char *sws_format_name(enum PixelFormat format)
394 case PIX_FMT_YUV420P:
396 case PIX_FMT_YUVA420P:
398 case PIX_FMT_YUYV422:
404 case PIX_FMT_YUV422P:
406 case PIX_FMT_YUV444P:
410 case PIX_FMT_YUV410P:
412 case PIX_FMT_YUV411P:
418 case PIX_FMT_GRAY16BE:
420 case PIX_FMT_GRAY16LE:
424 case PIX_FMT_MONOWHITE:
426 case PIX_FMT_MONOBLACK:
430 case PIX_FMT_YUVJ420P:
432 case PIX_FMT_YUVJ422P:
434 case PIX_FMT_YUVJ444P:
436 case PIX_FMT_XVMC_MPEG2_MC:
437 return "xvmc_mpeg2_mc";
438 case PIX_FMT_XVMC_MPEG2_IDCT:
439 return "xvmc_mpeg2_idct";
440 case PIX_FMT_UYVY422:
442 case PIX_FMT_UYYVYY411:
444 case PIX_FMT_RGB32_1:
446 case PIX_FMT_BGR32_1:
458 case PIX_FMT_BGR4_BYTE:
464 case PIX_FMT_RGB4_BYTE:
466 case PIX_FMT_RGB48BE:
468 case PIX_FMT_RGB48LE:
474 case PIX_FMT_YUV440P:
476 case PIX_FMT_VDPAU_H264:
478 case PIX_FMT_VDPAU_MPEG1:
479 return "vdpau_mpeg1";
480 case PIX_FMT_VDPAU_MPEG2:
481 return "vdpau_mpeg2";
482 case PIX_FMT_VDPAU_WMV3:
484 case PIX_FMT_VDPAU_VC1:
486 case PIX_FMT_YUV420PLE:
488 case PIX_FMT_YUV422PLE:
490 case PIX_FMT_YUV444PLE:
492 case PIX_FMT_YUV420PBE:
494 case PIX_FMT_YUV422PBE:
496 case PIX_FMT_YUV444PBE:
499 return "Unknown format";
503 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
504 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
505 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
507 //FIXME Optimize (just quickly written not optimized..)
509 for (i=0; i<dstW; i++)
513 for (j=0; j<lumFilterSize; j++)
514 val += lumSrc[j][i] * lumFilter[j];
516 dest[i]= av_clip_uint8(val>>19);
520 for (i=0; i<chrDstW; i++)
525 for (j=0; j<chrFilterSize; j++)
527 u += chrSrc[j][i] * chrFilter[j];
528 v += chrSrc[j][i + VOFW] * chrFilter[j];
531 uDest[i]= av_clip_uint8(u>>19);
532 vDest[i]= av_clip_uint8(v>>19);
535 if (CONFIG_SWSCALE_ALPHA && aDest)
536 for (i=0; i<dstW; i++){
539 for (j=0; j<lumFilterSize; j++)
540 val += alpSrc[j][i] * lumFilter[j];
542 aDest[i]= av_clip_uint8(val>>19);
547 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
548 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
549 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
551 //FIXME Optimize (just quickly written not optimized..)
553 for (i=0; i<dstW; i++)
557 for (j=0; j<lumFilterSize; j++)
558 val += lumSrc[j][i] * lumFilter[j];
560 dest[i]= av_clip_uint8(val>>19);
566 if (dstFormat == PIX_FMT_NV12)
567 for (i=0; i<chrDstW; i++)
572 for (j=0; j<chrFilterSize; j++)
574 u += chrSrc[j][i] * chrFilter[j];
575 v += chrSrc[j][i + VOFW] * chrFilter[j];
578 uDest[2*i]= av_clip_uint8(u>>19);
579 uDest[2*i+1]= av_clip_uint8(v>>19);
582 for (i=0; i<chrDstW; i++)
587 for (j=0; j<chrFilterSize; j++)
589 u += chrSrc[j][i] * chrFilter[j];
590 v += chrSrc[j][i + VOFW] * chrFilter[j];
593 uDest[2*i]= av_clip_uint8(v>>19);
594 uDest[2*i+1]= av_clip_uint8(u>>19);
598 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
599 for (i=0; i<(dstW>>1); i++){\
605 int av_unused A1, A2;\
606 type av_unused *r, *b, *g;\
609 for (j=0; j<lumFilterSize; j++)\
611 Y1 += lumSrc[j][i2] * lumFilter[j];\
612 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
614 for (j=0; j<chrFilterSize; j++)\
616 U += chrSrc[j][i] * chrFilter[j];\
617 V += chrSrc[j][i+VOFW] * chrFilter[j];\
626 for (j=0; j<lumFilterSize; j++){\
627 A1 += alpSrc[j][i2 ] * lumFilter[j];\
628 A2 += alpSrc[j][i2+1] * lumFilter[j];\
634 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
635 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
636 if ((Y1|Y2|U|V)&256)\
638 if (Y1>255) Y1=255; \
639 else if (Y1<0)Y1=0; \
640 if (Y2>255) Y2=255; \
641 else if (Y2<0)Y2=0; \
647 if (alpha && ((A1|A2)&256)){\
648 A1=av_clip_uint8(A1);\
649 A2=av_clip_uint8(A2);\
652 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
653 for (i=0; i<dstW; i++){\
661 for (j=0; j<lumFilterSize; j++){\
662 Y += lumSrc[j][i ] * lumFilter[j];\
664 for (j=0; j<chrFilterSize; j++){\
665 U += chrSrc[j][i ] * chrFilter[j];\
666 V += chrSrc[j][i+VOFW] * chrFilter[j];\
673 for (j=0; j<lumFilterSize; j++)\
674 A += alpSrc[j][i ] * lumFilter[j];\
677 A = av_clip_uint8(A);\
680 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
681 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
682 Y-= c->yuv2rgb_y_offset;\
683 Y*= c->yuv2rgb_y_coeff;\
685 R= Y + V*c->yuv2rgb_v2r_coeff;\
686 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
687 B= Y + U*c->yuv2rgb_u2b_coeff;\
688 if ((R|G|B)&(0xC0000000)){\
689 if (R>=(256<<22)) R=(256<<22)-1; \
691 if (G>=(256<<22)) G=(256<<22)-1; \
693 if (B>=(256<<22)) B=(256<<22)-1; \
698 #define YSCALE_YUV_2_GRAY16_C \
699 for (i=0; i<(dstW>>1); i++){\
708 for (j=0; j<lumFilterSize; j++)\
710 Y1 += lumSrc[j][i2] * lumFilter[j];\
711 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
715 if ((Y1|Y2|U|V)&65536)\
717 if (Y1>65535) Y1=65535; \
718 else if (Y1<0)Y1=0; \
719 if (Y2>65535) Y2=65535; \
720 else if (Y2<0)Y2=0; \
723 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
724 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
725 r = (type *)c->table_rV[V]; \
726 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
727 b = (type *)c->table_bU[U]; \
729 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
730 for (i=0; i<(dstW>>1); i++){ \
732 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
733 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
734 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
735 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
736 type av_unused *r, *b, *g; \
737 int av_unused A1, A2; \
739 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
740 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
743 #define YSCALE_YUV_2_GRAY16_2_C \
744 for (i=0; i<(dstW>>1); i++){ \
746 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
747 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
749 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
750 YSCALE_YUV_2_PACKED2_C(type,alpha)\
751 r = (type *)c->table_rV[V];\
752 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
753 b = (type *)c->table_bU[U];\
755 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
756 for (i=0; i<(dstW>>1); i++){\
758 int Y1= buf0[i2 ]>>7;\
759 int Y2= buf0[i2+1]>>7;\
760 int U= (uvbuf1[i ])>>7;\
761 int V= (uvbuf1[i+VOFW])>>7;\
762 type av_unused *r, *b, *g;\
763 int av_unused A1, A2;\
769 #define YSCALE_YUV_2_GRAY16_1_C \
770 for (i=0; i<(dstW>>1); i++){\
772 int Y1= buf0[i2 ]<<1;\
773 int Y2= buf0[i2+1]<<1;\
775 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
776 YSCALE_YUV_2_PACKED1_C(type,alpha)\
777 r = (type *)c->table_rV[V];\
778 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
779 b = (type *)c->table_bU[U];\
781 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
782 for (i=0; i<(dstW>>1); i++){\
784 int Y1= buf0[i2 ]>>7;\
785 int Y2= buf0[i2+1]>>7;\
786 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
787 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
788 type av_unused *r, *b, *g;\
789 int av_unused A1, A2;\
795 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
796 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
797 r = (type *)c->table_rV[V];\
798 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
799 b = (type *)c->table_bU[U];\
801 #define YSCALE_YUV_2_MONO2_C \
802 const uint8_t * const d128=dither_8x8_220[y&7];\
803 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
804 for (i=0; i<dstW-7; i+=8){\
806 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
807 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
808 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
809 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
810 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
811 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
812 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
813 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
814 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
819 #define YSCALE_YUV_2_MONOX_C \
820 const uint8_t * const d128=dither_8x8_220[y&7];\
821 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
823 for (i=0; i<dstW-1; i+=2){\
828 for (j=0; j<lumFilterSize; j++)\
830 Y1 += lumSrc[j][i] * lumFilter[j];\
831 Y2 += lumSrc[j][i+1] * lumFilter[j];\
842 acc+= acc + g[Y1+d128[(i+0)&7]];\
843 acc+= acc + g[Y2+d128[(i+1)&7]];\
845 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
851 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
852 switch(c->dstFormat)\
854 case PIX_FMT_RGB48BE:\
855 case PIX_FMT_RGB48LE:\
857 ((uint8_t*)dest)[ 0]= r[Y1];\
858 ((uint8_t*)dest)[ 1]= r[Y1];\
859 ((uint8_t*)dest)[ 2]= g[Y1];\
860 ((uint8_t*)dest)[ 3]= g[Y1];\
861 ((uint8_t*)dest)[ 4]= b[Y1];\
862 ((uint8_t*)dest)[ 5]= b[Y1];\
863 ((uint8_t*)dest)[ 6]= r[Y2];\
864 ((uint8_t*)dest)[ 7]= r[Y2];\
865 ((uint8_t*)dest)[ 8]= g[Y2];\
866 ((uint8_t*)dest)[ 9]= g[Y2];\
867 ((uint8_t*)dest)[10]= b[Y2];\
868 ((uint8_t*)dest)[11]= b[Y2];\
875 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
876 func(uint32_t,needAlpha)\
877 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
878 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
881 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
883 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
884 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
888 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
889 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
897 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
898 func(uint32_t,needAlpha)\
899 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
900 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
903 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
905 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
906 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
910 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
911 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
918 ((uint8_t*)dest)[0]= r[Y1];\
919 ((uint8_t*)dest)[1]= g[Y1];\
920 ((uint8_t*)dest)[2]= b[Y1];\
921 ((uint8_t*)dest)[3]= r[Y2];\
922 ((uint8_t*)dest)[4]= g[Y2];\
923 ((uint8_t*)dest)[5]= b[Y2];\
929 ((uint8_t*)dest)[0]= b[Y1];\
930 ((uint8_t*)dest)[1]= g[Y1];\
931 ((uint8_t*)dest)[2]= r[Y1];\
932 ((uint8_t*)dest)[3]= b[Y2];\
933 ((uint8_t*)dest)[4]= g[Y2];\
934 ((uint8_t*)dest)[5]= r[Y2];\
938 case PIX_FMT_RGB565:\
939 case PIX_FMT_BGR565:\
941 const int dr1= dither_2x2_8[y&1 ][0];\
942 const int dg1= dither_2x2_4[y&1 ][0];\
943 const int db1= dither_2x2_8[(y&1)^1][0];\
944 const int dr2= dither_2x2_8[y&1 ][1];\
945 const int dg2= dither_2x2_4[y&1 ][1];\
946 const int db2= dither_2x2_8[(y&1)^1][1];\
948 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
949 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
953 case PIX_FMT_RGB555:\
954 case PIX_FMT_BGR555:\
956 const int dr1= dither_2x2_8[y&1 ][0];\
957 const int dg1= dither_2x2_8[y&1 ][1];\
958 const int db1= dither_2x2_8[(y&1)^1][0];\
959 const int dr2= dither_2x2_8[y&1 ][1];\
960 const int dg2= dither_2x2_8[y&1 ][0];\
961 const int db2= dither_2x2_8[(y&1)^1][1];\
963 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
964 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
971 const uint8_t * const d64= dither_8x8_73[y&7];\
972 const uint8_t * const d32= dither_8x8_32[y&7];\
974 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
975 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
982 const uint8_t * const d64= dither_8x8_73 [y&7];\
983 const uint8_t * const d128=dither_8x8_220[y&7];\
985 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
986 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
990 case PIX_FMT_RGB4_BYTE:\
991 case PIX_FMT_BGR4_BYTE:\
993 const uint8_t * const d64= dither_8x8_73 [y&7];\
994 const uint8_t * const d128=dither_8x8_220[y&7];\
996 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
997 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
1001 case PIX_FMT_MONOBLACK:\
1002 case PIX_FMT_MONOWHITE:\
1007 case PIX_FMT_YUYV422:\
1009 ((uint8_t*)dest)[2*i2+0]= Y1;\
1010 ((uint8_t*)dest)[2*i2+1]= U;\
1011 ((uint8_t*)dest)[2*i2+2]= Y2;\
1012 ((uint8_t*)dest)[2*i2+3]= V;\
1015 case PIX_FMT_UYVY422:\
1017 ((uint8_t*)dest)[2*i2+0]= U;\
1018 ((uint8_t*)dest)[2*i2+1]= Y1;\
1019 ((uint8_t*)dest)[2*i2+2]= V;\
1020 ((uint8_t*)dest)[2*i2+3]= Y2;\
1023 case PIX_FMT_GRAY16BE:\
1025 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1026 ((uint8_t*)dest)[2*i2+1]= Y1;\
1027 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1028 ((uint8_t*)dest)[2*i2+3]= Y2;\
1031 case PIX_FMT_GRAY16LE:\
1033 ((uint8_t*)dest)[2*i2+0]= Y1;\
1034 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1035 ((uint8_t*)dest)[2*i2+2]= Y2;\
1036 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1042 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1043 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1044 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1047 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
1050 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1051 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1052 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1055 int step= fmt_depth(c->dstFormat)/8;
1058 switch(c->dstFormat){
1066 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1067 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1068 dest[aidx]= needAlpha ? A : 255;
1075 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1076 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1084 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1101 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1102 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1103 dest[aidx]= needAlpha ? A : 255;
1110 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1111 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1119 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1134 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1136 uint8_t *ptr = plane + stride*y;
1137 for (i=0; i<height; i++){
1138 memset(ptr, val, width);
1143 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1146 for (i = 0; i < width; i++) {
1151 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1155 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1156 uint8_t *src1, uint8_t *src2, int width)
1160 for (i = 0; i < width; i++) {
1161 int r = src1[6*i + 0];
1162 int g = src1[6*i + 2];
1163 int b = src1[6*i + 4];
1165 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1166 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1170 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1171 uint8_t *src1, uint8_t *src2, int width)
1175 for (i = 0; i < width; i++) {
1176 int r= src1[12*i + 0] + src1[12*i + 6];
1177 int g= src1[12*i + 2] + src1[12*i + 8];
1178 int b= src1[12*i + 4] + src1[12*i + 10];
1180 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1181 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1185 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1186 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1189 for (i=0; i<width; i++)\
1191 int b= (((const type*)src)[i]>>shb)&maskb;\
1192 int g= (((const type*)src)[i]>>shg)&maskg;\
1193 int r= (((const type*)src)[i]>>shr)&maskr;\
1195 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1199 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1200 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1201 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1202 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1203 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1204 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1206 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused){
1208 for (i=0; i<width; i++){
1213 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1214 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1217 for (i=0; i<width; i++)\
1219 int b= (((const type*)src)[i]&maskb)>>shb;\
1220 int g= (((const type*)src)[i]&maskg)>>shg;\
1221 int r= (((const type*)src)[i]&maskr)>>shr;\
1223 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1224 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1227 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1230 for (i=0; i<width; i++)\
1232 int pix0= ((const type*)src)[2*i+0];\
1233 int pix1= ((const type*)src)[2*i+1];\
1234 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1235 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1236 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1237 g&= maskg|(2*maskg);\
1241 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1242 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1246 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1247 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1248 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1249 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1250 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1251 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1253 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1256 for (i=0; i<width; i++)
1260 dst[i]= pal[d] & 0xFF;
1264 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1265 const uint8_t *src1, const uint8_t *src2,
1266 long width, uint32_t *pal)
1269 assert(src1 == src2);
1270 for (i=0; i<width; i++)
1272 int p= pal[src1[i]];
1279 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1282 for (i=0; i<width/8; i++){
1285 dst[8*i+j]= ((d>>(7-j))&1)*255;
1289 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1292 for (i=0; i<width/8; i++){
1295 dst[8*i+j]= ((d>>(7-j))&1)*255;
1300 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1302 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1307 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1308 #define COMPILE_ALTIVEC
1314 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1318 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1319 #define COMPILE_MMX2
1322 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1323 #define COMPILE_3DNOW
1329 #undef HAVE_AMD3DNOW
1333 #define HAVE_AMD3DNOW 0
1334 #define HAVE_ALTIVEC 0
1337 #define RENAME(a) a ## _C
1338 #include "swscale_template.c"
1341 #ifdef COMPILE_ALTIVEC
1344 #define HAVE_ALTIVEC 1
1345 #define RENAME(a) a ## _altivec
1346 #include "swscale_template.c"
1356 #undef HAVE_AMD3DNOW
1359 #define HAVE_AMD3DNOW 0
1360 #define RENAME(a) a ## _MMX
1361 #include "swscale_template.c"
1369 #undef HAVE_AMD3DNOW
1372 #define HAVE_AMD3DNOW 0
1373 #define RENAME(a) a ## _MMX2
1374 #include "swscale_template.c"
1378 #ifdef COMPILE_3DNOW
1382 #undef HAVE_AMD3DNOW
1385 #define HAVE_AMD3DNOW 1
1386 #define RENAME(a) a ## _3DNow
1387 #include "swscale_template.c"
1392 // minor note: the HAVE_xyz are messed up after this line so don't use them
1394 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1396 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1397 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1398 else return getSplineCoeff( 0.0,
1405 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1406 int srcW, int dstW, int filterAlign, int one, int flags,
1407 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1413 int64_t *filter=NULL;
1414 int64_t *filter2=NULL;
1415 const int64_t fone= 1LL<<54;
1418 if (flags & SWS_CPU_CAPS_MMX)
1419 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1422 // NOTE: the +1 is for the MMX scaler which reads over the end
1423 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1425 if (FFABS(xInc - 0x10000) <10) // unscaled
1429 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1431 for (i=0; i<dstW; i++)
1433 filter[i*filterSize]= fone;
1438 else if (flags&SWS_POINT) // lame looking point sampling mode
1443 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1445 xDstInSrc= xInc/2 - 0x8000;
1446 for (i=0; i<dstW; i++)
1448 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1450 (*filterPos)[i]= xx;
1455 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1459 if (flags&SWS_BICUBIC) filterSize= 4;
1460 else if (flags&SWS_X ) filterSize= 4;
1461 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1462 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1464 xDstInSrc= xInc/2 - 0x8000;
1465 for (i=0; i<dstW; i++)
1467 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1470 (*filterPos)[i]= xx;
1471 //bilinear upscale / linear interpolate / area averaging
1472 for (j=0; j<filterSize; j++)
1474 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1475 if (coeff<0) coeff=0;
1476 filter[i*filterSize + j]= coeff;
1487 if (flags&SWS_BICUBIC) sizeFactor= 4;
1488 else if (flags&SWS_X) sizeFactor= 8;
1489 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1490 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1491 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1492 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1493 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1494 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1496 sizeFactor= 0; //GCC warning killer
1500 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1501 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1503 if (filterSize > srcW-2) filterSize=srcW-2;
1505 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1507 xDstInSrc= xInc - 0x10000;
1508 for (i=0; i<dstW; i++)
1510 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1512 (*filterPos)[i]= xx;
1513 for (j=0; j<filterSize; j++)
1515 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1521 floatd= d * (1.0/(1<<30));
1523 if (flags & SWS_BICUBIC)
1525 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1526 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1527 int64_t dd = ( d*d)>>30;
1528 int64_t ddd= (dd*d)>>30;
1531 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1532 else if (d < 1LL<<31)
1533 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1536 coeff *= fone>>(30+24);
1538 /* else if (flags & SWS_X)
1540 double p= param ? param*0.01 : 0.3;
1541 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1542 coeff*= pow(2.0, - p*d*d);
1544 else if (flags & SWS_X)
1546 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1553 if (c<0.0) c= -pow(-c, A);
1555 coeff= (c*0.5 + 0.5)*fone;
1557 else if (flags & SWS_AREA)
1559 int64_t d2= d - (1<<29);
1560 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1561 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1563 coeff *= fone>>(30+16);
1565 else if (flags & SWS_GAUSS)
1567 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1568 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1570 else if (flags & SWS_SINC)
1572 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1574 else if (flags & SWS_LANCZOS)
1576 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1577 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1578 if (floatd>p) coeff=0;
1580 else if (flags & SWS_BILINEAR)
1583 if (coeff<0) coeff=0;
1584 coeff *= fone >> 30;
1586 else if (flags & SWS_SPLINE)
1588 double p=-2.196152422706632;
1589 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1592 coeff= 0.0; //GCC warning killer
1596 filter[i*filterSize + j]= coeff;
1603 /* apply src & dst Filter to filter -> filter2
1606 assert(filterSize>0);
1607 filter2Size= filterSize;
1608 if (srcFilter) filter2Size+= srcFilter->length - 1;
1609 if (dstFilter) filter2Size+= dstFilter->length - 1;
1610 assert(filter2Size>0);
1611 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1613 for (i=0; i<dstW; i++)
1618 for (k=0; k<srcFilter->length; k++){
1619 for (j=0; j<filterSize; j++)
1620 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1623 for (j=0; j<filterSize; j++)
1624 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1628 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1632 /* try to reduce the filter-size (step1 find size and shift left) */
1633 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1635 for (i=dstW-1; i>=0; i--)
1637 int min= filter2Size;
1641 /* get rid off near zero elements on the left by shifting left */
1642 for (j=0; j<filter2Size; j++)
1645 cutOff += FFABS(filter2[i*filter2Size]);
1647 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1649 /* preserve monotonicity because the core can't handle the filter otherwise */
1650 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1652 // move filter coefficients left
1653 for (k=1; k<filter2Size; k++)
1654 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1655 filter2[i*filter2Size + k - 1]= 0;
1660 /* count near zeros on the right */
1661 for (j=filter2Size-1; j>0; j--)
1663 cutOff += FFABS(filter2[i*filter2Size + j]);
1665 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1669 if (min>minFilterSize) minFilterSize= min;
1672 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1673 // we can handle the special case 4,
1674 // so we don't want to go to the full 8
1675 if (minFilterSize < 5)
1678 // We really don't want to waste our time
1679 // doing useless computation, so fall back on
1680 // the scalar C code for very small filters.
1681 // Vectorizing is worth it only if you have a
1682 // decent-sized vector.
1683 if (minFilterSize < 3)
1687 if (flags & SWS_CPU_CAPS_MMX) {
1688 // special case for unscaled vertical filtering
1689 if (minFilterSize == 1 && filterAlign == 2)
1693 assert(minFilterSize > 0);
1694 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1695 assert(filterSize > 0);
1696 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1697 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1699 *outFilterSize= filterSize;
1701 if (flags&SWS_PRINT_INFO)
1702 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1703 /* try to reduce the filter-size (step2 reduce it) */
1704 for (i=0; i<dstW; i++)
1708 for (j=0; j<filterSize; j++)
1710 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1711 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1712 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1713 filter[i*filterSize + j]= 0;
1718 //FIXME try to align filterPos if possible
1721 for (i=0; i<dstW; i++)
1724 if ((*filterPos)[i] < 0)
1726 // move filter coefficients left to compensate for filterPos
1727 for (j=1; j<filterSize; j++)
1729 int left= FFMAX(j + (*filterPos)[i], 0);
1730 filter[i*filterSize + left] += filter[i*filterSize + j];
1731 filter[i*filterSize + j]=0;
1736 if ((*filterPos)[i] + filterSize > srcW)
1738 int shift= (*filterPos)[i] + filterSize - srcW;
1739 // move filter coefficients right to compensate for filterPos
1740 for (j=filterSize-2; j>=0; j--)
1742 int right= FFMIN(j + shift, filterSize-1);
1743 filter[i*filterSize +right] += filter[i*filterSize +j];
1744 filter[i*filterSize +j]=0;
1746 (*filterPos)[i]= srcW - filterSize;
1750 // Note the +1 is for the MMX scaler which reads over the end
1751 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1752 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1754 /* normalize & store in outFilter */
1755 for (i=0; i<dstW; i++)
1761 for (j=0; j<filterSize; j++)
1763 sum+= filter[i*filterSize + j];
1765 sum= (sum + one/2)/ one;
1766 for (j=0; j<*outFilterSize; j++)
1768 int64_t v= filter[i*filterSize + j] + error;
1769 int intV= ROUNDED_DIV(v, sum);
1770 (*outFilter)[i*(*outFilterSize) + j]= intV;
1771 error= v - intV*sum;
1775 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1776 for (i=0; i<*outFilterSize; i++)
1778 int j= dstW*(*outFilterSize);
1779 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1790 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1793 x86_reg imm8OfPShufW1A;
1794 x86_reg imm8OfPShufW2A;
1795 x86_reg fragmentLengthA;
1797 x86_reg imm8OfPShufW1B;
1798 x86_reg imm8OfPShufW2B;
1799 x86_reg fragmentLengthB;
1804 // create an optimized horizontal scaling routine
1812 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1813 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1814 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1815 "punpcklbw %%mm7, %%mm1 \n\t"
1816 "punpcklbw %%mm7, %%mm0 \n\t"
1817 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1819 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1821 "psubw %%mm1, %%mm0 \n\t"
1822 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1823 "pmullw %%mm3, %%mm0 \n\t"
1824 "psllw $7, %%mm1 \n\t"
1825 "paddw %%mm1, %%mm0 \n\t"
1827 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1829 "add $8, %%"REG_a" \n\t"
1833 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1834 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1835 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1840 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1844 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1845 "=r" (fragmentLengthA)
1852 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1853 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1854 "punpcklbw %%mm7, %%mm0 \n\t"
1855 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1857 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1859 "psubw %%mm1, %%mm0 \n\t"
1860 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1861 "pmullw %%mm3, %%mm0 \n\t"
1862 "psllw $7, %%mm1 \n\t"
1863 "paddw %%mm1, %%mm0 \n\t"
1865 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1867 "add $8, %%"REG_a" \n\t"
1871 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1872 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1873 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1878 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1882 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1883 "=r" (fragmentLengthB)
1886 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1889 for (i=0; i<dstW/numSplits; i++)
1896 int b=((xpos+xInc)>>16) - xx;
1897 int c=((xpos+xInc*2)>>16) - xx;
1898 int d=((xpos+xInc*3)>>16) - xx;
1900 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1901 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1902 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1903 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1908 int maxShift= 3-(d+1);
1911 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1913 funnyCode[fragmentPos + imm8OfPShufW1B]=
1914 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1915 funnyCode[fragmentPos + imm8OfPShufW2B]=
1916 a | (b<<2) | (c<<4) | (d<<6);
1918 if (i+3>=dstW) shift=maxShift; //avoid overread
1919 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1921 if (shift && i>=shift)
1923 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1924 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1925 filterPos[i/2]-=shift;
1928 fragmentPos+= fragmentLengthB;
1935 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1937 funnyCode[fragmentPos + imm8OfPShufW1A]=
1938 funnyCode[fragmentPos + imm8OfPShufW2A]=
1939 a | (b<<2) | (c<<4) | (d<<6);
1941 if (i+4>=dstW) shift=maxShift; //avoid overread
1942 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1944 if (shift && i>=shift)
1946 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1947 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1948 filterPos[i/2]-=shift;
1951 fragmentPos+= fragmentLengthA;
1954 funnyCode[fragmentPos]= RET;
1958 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1960 #endif /* COMPILE_MMX2 */
1962 static void globalInit(void){
1963 // generating tables:
1965 for (i=0; i<768; i++){
1966 int c= av_clip_uint8(i-256);
1971 static SwsFunc getSwsFunc(SwsContext *c)
1973 #if CONFIG_RUNTIME_CPUDETECT
1974 int flags = c->flags;
1976 #if ARCH_X86 && CONFIG_GPL
1977 // ordered per speed fastest first
1978 if (flags & SWS_CPU_CAPS_MMX2) {
1979 sws_init_swScale_MMX2(c);
1980 return swScale_MMX2;
1981 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1982 sws_init_swScale_3DNow(c);
1983 return swScale_3DNow;
1984 } else if (flags & SWS_CPU_CAPS_MMX) {
1985 sws_init_swScale_MMX(c);
1988 sws_init_swScale_C(c);
1994 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1995 sws_init_swScale_altivec(c);
1996 return swScale_altivec;
1998 sws_init_swScale_C(c);
2002 sws_init_swScale_C(c);
2004 #endif /* ARCH_X86 && CONFIG_GPL */
2005 #else //CONFIG_RUNTIME_CPUDETECT
2007 sws_init_swScale_MMX2(c);
2008 return swScale_MMX2;
2010 sws_init_swScale_3DNow(c);
2011 return swScale_3DNow;
2013 sws_init_swScale_MMX(c);
2016 sws_init_swScale_altivec(c);
2017 return swScale_altivec;
2019 sws_init_swScale_C(c);
2022 #endif //!CONFIG_RUNTIME_CPUDETECT
2025 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2026 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2027 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2029 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2030 memcpy(dst, src[0], srcSliceH*dstStride[0]);
2034 const uint8_t *srcPtr= src[0];
2035 uint8_t *dstPtr= dst;
2036 for (i=0; i<srcSliceH; i++)
2038 memcpy(dstPtr, srcPtr, c->srcW);
2039 srcPtr+= srcStride[0];
2040 dstPtr+= dstStride[0];
2043 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2044 if (c->dstFormat == PIX_FMT_NV12)
2045 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2047 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2052 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2053 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2054 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2056 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2061 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2062 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2063 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2065 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2070 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2071 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2072 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2074 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2079 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2080 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2081 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2083 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2088 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2089 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2090 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2091 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2092 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2094 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2097 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2102 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2103 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2104 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2105 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2106 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2108 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2113 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2114 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2115 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2116 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2117 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2119 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2122 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2127 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2128 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2129 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2130 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2131 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2133 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2138 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2139 int srcSliceH, uint8_t* dst[], int dstStride[]){
2140 const enum PixelFormat srcFormat= c->srcFormat;
2141 const enum PixelFormat dstFormat= c->dstFormat;
2142 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2143 const uint8_t *palette)=NULL;
2145 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2146 uint8_t *srcPtr= src[0];
2148 if (!usePal(srcFormat))
2149 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2150 sws_format_name(srcFormat), sws_format_name(dstFormat));
2153 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2154 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2155 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2156 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2157 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2158 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2159 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2160 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2164 for (i=0; i<srcSliceH; i++) {
2165 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2166 srcPtr+= srcStride[0];
2167 dstPtr+= dstStride[0];
2173 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2174 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2175 int srcSliceH, uint8_t* dst[], int dstStride[]){
2176 const enum PixelFormat srcFormat= c->srcFormat;
2177 const enum PixelFormat dstFormat= c->dstFormat;
2178 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2179 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2180 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2181 const int dstId= fmt_depth(dstFormat) >> 2;
2182 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2185 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2186 || (isRGB(srcFormat) && isRGB(dstFormat))){
2187 switch(srcId | (dstId<<4)){
2188 case 0x34: conv= rgb16to15; break;
2189 case 0x36: conv= rgb24to15; break;
2190 case 0x38: conv= rgb32to15; break;
2191 case 0x43: conv= rgb15to16; break;
2192 case 0x46: conv= rgb24to16; break;
2193 case 0x48: conv= rgb32to16; break;
2194 case 0x63: conv= rgb15to24; break;
2195 case 0x64: conv= rgb16to24; break;
2196 case 0x68: conv= rgb32to24; break;
2197 case 0x83: conv= rgb15to32; break;
2198 case 0x84: conv= rgb16to32; break;
2199 case 0x86: conv= rgb24to32; break;
2200 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2201 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2203 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2204 || (isRGB(srcFormat) && isBGR(dstFormat))){
2205 switch(srcId | (dstId<<4)){
2206 case 0x33: conv= rgb15tobgr15; break;
2207 case 0x34: conv= rgb16tobgr15; break;
2208 case 0x36: conv= rgb24tobgr15; break;
2209 case 0x38: conv= rgb32tobgr15; break;
2210 case 0x43: conv= rgb15tobgr16; break;
2211 case 0x44: conv= rgb16tobgr16; break;
2212 case 0x46: conv= rgb24tobgr16; break;
2213 case 0x48: conv= rgb32tobgr16; break;
2214 case 0x63: conv= rgb15tobgr24; break;
2215 case 0x64: conv= rgb16tobgr24; break;
2216 case 0x66: conv= rgb24tobgr24; break;
2217 case 0x68: conv= rgb32tobgr24; break;
2218 case 0x83: conv= rgb15tobgr32; break;
2219 case 0x84: conv= rgb16tobgr32; break;
2220 case 0x86: conv= rgb24tobgr32; break;
2221 case 0x88: conv= rgb32tobgr32; break;
2222 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2223 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2226 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2227 sws_format_name(srcFormat), sws_format_name(dstFormat));
2232 uint8_t *srcPtr= src[0];
2233 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2234 srcPtr += ALT32_CORR;
2236 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2237 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2241 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2243 for (i=0; i<srcSliceH; i++)
2245 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2246 srcPtr+= srcStride[0];
2247 dstPtr+= dstStride[0];
2254 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2255 int srcSliceH, uint8_t* dst[], int dstStride[]){
2259 dst[0]+ srcSliceY *dstStride[0],
2260 dst[1]+(srcSliceY>>1)*dstStride[1],
2261 dst[2]+(srcSliceY>>1)*dstStride[2],
2263 dstStride[0], dstStride[1], srcStride[0]);
2265 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2269 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2270 int srcSliceH, uint8_t* dst[], int dstStride[]){
2274 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2275 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2277 uint8_t *srcPtr= src[0];
2278 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2280 for (i=0; i<srcSliceH; i++)
2282 memcpy(dstPtr, srcPtr, c->srcW);
2283 srcPtr+= srcStride[0];
2284 dstPtr+= dstStride[0];
2288 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2289 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2290 srcSliceH >> 2, srcStride[1], dstStride[1]);
2291 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2292 srcSliceH >> 2, srcStride[2], dstStride[2]);
2294 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2295 srcSliceH >> 2, srcStride[1], dstStride[2]);
2296 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2297 srcSliceH >> 2, srcStride[2], dstStride[1]);
2300 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2304 /* unscaled copy like stuff (assumes nearly identical formats) */
2305 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2306 int srcSliceH, uint8_t* dst[], int dstStride[])
2308 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2309 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2313 uint8_t *srcPtr= src[0];
2314 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2317 /* universal length finder */
2318 while(length+c->srcW <= FFABS(dstStride[0])
2319 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2322 for (i=0; i<srcSliceH; i++)
2324 memcpy(dstPtr, srcPtr, length);
2325 srcPtr+= srcStride[0];
2326 dstPtr+= dstStride[0];
2332 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2333 int srcSliceH, uint8_t* dst[], int dstStride[])
2336 for (plane=0; plane<4; plane++)
2338 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2339 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2340 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2341 uint8_t *srcPtr= src[plane];
2342 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2344 if (!dst[plane]) continue;
2345 // ignore palette for GRAY8
2346 if (plane == 1 && !dst[2]) continue;
2347 if (!src[plane] || (plane == 1 && !src[2])){
2348 if(is16BPS(c->dstFormat))
2350 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2353 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2354 if (!isBE(c->srcFormat)) srcPtr++;
2355 for (i=0; i<height; i++){
2356 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2357 srcPtr+= srcStride[plane];
2358 dstPtr+= dstStride[plane];
2360 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2361 for (i=0; i<height; i++){
2362 for (j=0; j<length; j++){
2363 dstPtr[ j<<1 ] = srcPtr[j];
2364 dstPtr[(j<<1)+1] = srcPtr[j];
2366 srcPtr+= srcStride[plane];
2367 dstPtr+= dstStride[plane];
2369 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2370 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2372 for (i=0; i<height; i++){
2373 for (j=0; j<length; j++)
2374 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2375 srcPtr+= srcStride[plane];
2376 dstPtr+= dstStride[plane];
2378 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2379 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2382 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2384 for (i=0; i<height; i++)
2386 memcpy(dstPtr, srcPtr, length);
2387 srcPtr+= srcStride[plane];
2388 dstPtr+= dstStride[plane];
2397 static void getSubSampleFactors(int *h, int *v, int format){
2399 case PIX_FMT_UYVY422:
2400 case PIX_FMT_YUYV422:
2404 case PIX_FMT_YUV420P:
2405 case PIX_FMT_YUV420PLE:
2406 case PIX_FMT_YUV420PBE:
2407 case PIX_FMT_YUVA420P:
2408 case PIX_FMT_GRAY16BE:
2409 case PIX_FMT_GRAY16LE:
2410 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2416 case PIX_FMT_YUV440P:
2420 case PIX_FMT_YUV410P:
2424 case PIX_FMT_YUV444P:
2425 case PIX_FMT_YUV444PLE:
2426 case PIX_FMT_YUV444PBE:
2430 case PIX_FMT_YUV422P:
2431 case PIX_FMT_YUV422PLE:
2432 case PIX_FMT_YUV422PBE:
2436 case PIX_FMT_YUV411P:
2447 static uint16_t roundToInt16(int64_t f){
2448 int r= (f + (1<<15))>>16;
2449 if (r<-0x7FFF) return 0x8000;
2450 else if (r> 0x7FFF) return 0x7FFF;
2454 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2455 int64_t crv = inv_table[0];
2456 int64_t cbu = inv_table[1];
2457 int64_t cgu = -inv_table[2];
2458 int64_t cgv = -inv_table[3];
2462 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2463 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2465 c->brightness= brightness;
2466 c->contrast = contrast;
2467 c->saturation= saturation;
2468 c->srcRange = srcRange;
2469 c->dstRange = dstRange;
2470 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2472 c->uOffset= 0x0400040004000400LL;
2473 c->vOffset= 0x0400040004000400LL;
2479 crv= (crv*224) / 255;
2480 cbu= (cbu*224) / 255;
2481 cgu= (cgu*224) / 255;
2482 cgv= (cgv*224) / 255;
2485 cy = (cy *contrast )>>16;
2486 crv= (crv*contrast * saturation)>>32;
2487 cbu= (cbu*contrast * saturation)>>32;
2488 cgu= (cgu*contrast * saturation)>>32;
2489 cgv= (cgv*contrast * saturation)>>32;
2491 oy -= 256*brightness;
2493 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2494 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2495 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2496 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2497 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2498 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2500 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2501 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2502 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2503 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2504 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2505 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2507 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2510 #ifdef COMPILE_ALTIVEC
2511 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2512 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2517 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2518 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2520 *inv_table = c->srcColorspaceTable;
2521 *table = c->dstColorspaceTable;
2522 *srcRange = c->srcRange;
2523 *dstRange = c->dstRange;
2524 *brightness= c->brightness;
2525 *contrast = c->contrast;
2526 *saturation= c->saturation;
2531 static int handle_jpeg(enum PixelFormat *format)
2534 case PIX_FMT_YUVJ420P:
2535 *format = PIX_FMT_YUV420P;
2537 case PIX_FMT_YUVJ422P:
2538 *format = PIX_FMT_YUV422P;
2540 case PIX_FMT_YUVJ444P:
2541 *format = PIX_FMT_YUV444P;
2543 case PIX_FMT_YUVJ440P:
2544 *format = PIX_FMT_YUV440P;
2551 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2552 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2557 int usesVFilter, usesHFilter;
2558 int unscaled, needsDither;
2559 int srcRange, dstRange;
2560 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2562 if (flags & SWS_CPU_CAPS_MMX)
2563 __asm__ volatile("emms\n\t"::: "memory");
2566 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2567 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2569 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2571 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2573 flags |= SWS_CPU_CAPS_MMX;
2575 flags |= SWS_CPU_CAPS_ALTIVEC;
2577 flags |= SWS_CPU_CAPS_BFIN;
2579 #endif /* CONFIG_RUNTIME_CPUDETECT */
2580 if (clip_table[512] != 255) globalInit();
2581 if (!rgb15to16) sws_rgb2rgb_init(flags);
2583 unscaled = (srcW == dstW && srcH == dstH);
2584 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2585 && (fmt_depth(dstFormat))<24
2586 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2588 srcRange = handle_jpeg(&srcFormat);
2589 dstRange = handle_jpeg(&dstFormat);
2591 if (!isSupportedIn(srcFormat))
2593 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2596 if (!isSupportedOut(dstFormat))
2598 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2602 i= flags & ( SWS_POINT
2613 if(!i || (i & (i-1)))
2615 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2620 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
2622 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2623 srcW, srcH, dstW, dstH);
2626 if(srcW > VOFW || dstW > VOFW){
2627 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2631 if (!dstFilter) dstFilter= &dummyFilter;
2632 if (!srcFilter) srcFilter= &dummyFilter;
2634 c= av_mallocz(sizeof(SwsContext));
2636 c->av_class = &sws_context_class;
2641 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2642 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2644 c->dstFormat= dstFormat;
2645 c->srcFormat= srcFormat;
2646 c->vRounder= 4* 0x0001000100010001ULL;
2648 usesHFilter= usesVFilter= 0;
2649 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2650 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2651 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2652 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2653 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2654 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2655 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2656 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2658 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2659 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2661 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2662 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2664 // drop some chroma lines if the user wants it
2665 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2666 c->chrSrcVSubSample+= c->vChrDrop;
2668 // drop every other pixel for chroma calculation unless user wants full chroma
2669 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2670 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2671 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2672 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2673 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2674 c->chrSrcHSubSample=1;
2677 c->param[0] = param[0];
2678 c->param[1] = param[1];
2681 c->param[1] = SWS_PARAM_DEFAULT;
2684 // Note the -((-x)>>y) is so that we always round toward +inf.
2685 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2686 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2687 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2688 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2690 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2692 /* unscaled special cases */
2693 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2696 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2698 c->swScale= PlanarToNV12Wrapper;
2701 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2702 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2704 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2707 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2709 c->swScale= yvu9toyv12Wrapper;
2713 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2714 c->swScale= bgr24toyv12Wrapper;
2716 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2717 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2718 && (isBGR(dstFormat) || isRGB(dstFormat))
2719 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2720 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2721 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2722 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2723 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2724 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2725 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2726 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2727 && dstFormat != PIX_FMT_RGB32_1
2728 && dstFormat != PIX_FMT_BGR32_1
2729 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2730 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2731 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2732 c->swScale= rgb2rgbWrapper;
2734 if ((usePal(srcFormat) && (
2735 dstFormat == PIX_FMT_RGB32 ||
2736 dstFormat == PIX_FMT_RGB32_1 ||
2737 dstFormat == PIX_FMT_RGB24 ||
2738 dstFormat == PIX_FMT_BGR32 ||
2739 dstFormat == PIX_FMT_BGR32_1 ||
2740 dstFormat == PIX_FMT_BGR24)))
2741 c->swScale= pal2rgbWrapper;
2743 if (srcFormat == PIX_FMT_YUV422P)
2745 if (dstFormat == PIX_FMT_YUYV422)
2746 c->swScale= YUV422PToYuy2Wrapper;
2747 else if (dstFormat == PIX_FMT_UYVY422)
2748 c->swScale= YUV422PToUyvyWrapper;
2751 /* LQ converters if -sws 0 or -sws 4*/
2752 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2754 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2756 if (dstFormat == PIX_FMT_YUYV422)
2757 c->swScale= PlanarToYuy2Wrapper;
2758 else if (dstFormat == PIX_FMT_UYVY422)
2759 c->swScale= PlanarToUyvyWrapper;
2762 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2763 c->swScale= YUYV2YUV420Wrapper;
2764 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2765 c->swScale= UYVY2YUV420Wrapper;
2766 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2767 c->swScale= YUYV2YUV422Wrapper;
2768 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2769 c->swScale= UYVY2YUV422Wrapper;
2771 #ifdef COMPILE_ALTIVEC
2772 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2773 !(c->flags & SWS_BITEXACT) &&
2774 srcFormat == PIX_FMT_YUV420P) {
2775 // unscaled YV12 -> packed YUV, we want speed
2776 if (dstFormat == PIX_FMT_YUYV422)
2777 c->swScale= yv12toyuy2_unscaled_altivec;
2778 else if (dstFormat == PIX_FMT_UYVY422)
2779 c->swScale= yv12touyvy_unscaled_altivec;
2784 if ( srcFormat == dstFormat
2785 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2786 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2787 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2788 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2789 || (isGray(dstFormat) && isGray(srcFormat))
2790 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2791 && c->chrDstHSubSample == c->chrSrcHSubSample
2792 && c->chrDstVSubSample == c->chrSrcVSubSample))
2794 if (isPacked(c->srcFormat))
2795 c->swScale= packedCopy;
2796 else /* Planar YUV or gray */
2797 c->swScale= planarCopy;
2800 if (flags & SWS_CPU_CAPS_BFIN)
2801 ff_bfin_get_unscaled_swscale (c);
2805 if (flags&SWS_PRINT_INFO)
2806 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2807 sws_format_name(srcFormat), sws_format_name(dstFormat));
2812 if (flags & SWS_CPU_CAPS_MMX2)
2814 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2815 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2817 if (flags&SWS_PRINT_INFO)
2818 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2820 if (usesHFilter) c->canMMX2BeUsed=0;
2825 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2826 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2828 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2829 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2830 // n-2 is the last chrominance sample available
2831 // this is not perfect, but no one should notice the difference, the more correct variant
2832 // would be like the vertical one, but that would require some special code for the
2833 // first and last pixel
2834 if (flags&SWS_FAST_BILINEAR)
2836 if (c->canMMX2BeUsed)
2841 //we don't use the x86 asm scaler if MMX is available
2842 else if (flags & SWS_CPU_CAPS_MMX)
2844 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2845 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2849 /* precalculate horizontal scaler filter coefficients */
2851 const int filterAlign=
2852 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2853 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2856 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2857 srcW , dstW, filterAlign, 1<<14,
2858 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2859 srcFilter->lumH, dstFilter->lumH, c->param);
2860 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2861 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2862 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2863 srcFilter->chrH, dstFilter->chrH, c->param);
2865 #define MAX_FUNNY_CODE_SIZE 10000
2866 #if defined(COMPILE_MMX2)
2867 // can't downscale !!!
2868 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2870 #ifdef MAP_ANONYMOUS
2871 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2872 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2873 #elif HAVE_VIRTUALALLOC
2874 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2875 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2877 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2878 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2881 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2882 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2883 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2884 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2886 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2887 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2889 #endif /* defined(COMPILE_MMX2) */
2890 } // initialize horizontal stuff
2894 /* precalculate vertical scaler filter coefficients */
2896 const int filterAlign=
2897 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2898 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2901 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2902 srcH , dstH, filterAlign, (1<<12),
2903 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2904 srcFilter->lumV, dstFilter->lumV, c->param);
2905 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2906 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2907 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2908 srcFilter->chrV, dstFilter->chrV, c->param);
2911 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2912 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2914 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2916 short *p = (short *)&c->vYCoeffsBank[i];
2918 p[j] = c->vLumFilter[i];
2921 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2923 short *p = (short *)&c->vCCoeffsBank[i];
2925 p[j] = c->vChrFilter[i];
2930 // calculate buffer sizes so that they won't run out while handling these damn slices
2931 c->vLumBufSize= c->vLumFilterSize;
2932 c->vChrBufSize= c->vChrFilterSize;
2933 for (i=0; i<dstH; i++)
2935 int chrI= i*c->chrDstH / dstH;
2936 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2937 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2939 nextSlice>>= c->chrSrcVSubSample;
2940 nextSlice<<= c->chrSrcVSubSample;
2941 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2942 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2943 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2944 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2947 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2948 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2949 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2950 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2951 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2952 //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)
2953 /* align at 16 bytes for AltiVec */
2954 for (i=0; i<c->vLumBufSize; i++)
2955 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2956 for (i=0; i<c->vChrBufSize; i++)
2957 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2958 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2959 for (i=0; i<c->vLumBufSize; i++)
2960 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2962 //try to avoid drawing green stuff between the right end and the stride end
2963 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2965 assert(2*VOFW == VOF);
2967 assert(c->chrDstH <= dstH);
2969 if (flags&SWS_PRINT_INFO)
2972 const char *dither= " dithered";
2974 const char *dither= "";
2976 if (flags&SWS_FAST_BILINEAR)
2977 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2978 else if (flags&SWS_BILINEAR)
2979 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2980 else if (flags&SWS_BICUBIC)
2981 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2982 else if (flags&SWS_X)
2983 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2984 else if (flags&SWS_POINT)
2985 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2986 else if (flags&SWS_AREA)
2987 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2988 else if (flags&SWS_BICUBLIN)
2989 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2990 else if (flags&SWS_GAUSS)
2991 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2992 else if (flags&SWS_SINC)
2993 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2994 else if (flags&SWS_LANCZOS)
2995 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2996 else if (flags&SWS_SPLINE)
2997 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2999 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
3001 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
3002 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
3003 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
3005 av_log(c, AV_LOG_INFO, "from %s to %s ",
3006 sws_format_name(srcFormat), sws_format_name(dstFormat));
3008 if (flags & SWS_CPU_CAPS_MMX2)
3009 av_log(c, AV_LOG_INFO, "using MMX2\n");
3010 else if (flags & SWS_CPU_CAPS_3DNOW)
3011 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
3012 else if (flags & SWS_CPU_CAPS_MMX)
3013 av_log(c, AV_LOG_INFO, "using MMX\n");
3014 else if (flags & SWS_CPU_CAPS_ALTIVEC)
3015 av_log(c, AV_LOG_INFO, "using AltiVec\n");
3017 av_log(c, AV_LOG_INFO, "using C\n");
3020 if (flags & SWS_PRINT_INFO)
3022 if (flags & SWS_CPU_CAPS_MMX)
3024 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
3025 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3028 if (c->hLumFilterSize==4)
3029 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3030 else if (c->hLumFilterSize==8)
3031 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3033 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3035 if (c->hChrFilterSize==4)
3036 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3037 else if (c->hChrFilterSize==8)
3038 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3040 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3046 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3048 if (flags & SWS_FAST_BILINEAR)
3049 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3051 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3054 if (isPlanarYUV(dstFormat))
3056 if (c->vLumFilterSize==1)
3057 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3059 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3063 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3064 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3065 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3066 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3067 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3069 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3072 if (dstFormat==PIX_FMT_BGR24)
3073 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3074 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3075 else if (dstFormat==PIX_FMT_RGB32)
3076 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3077 else if (dstFormat==PIX_FMT_BGR565)
3078 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3079 else if (dstFormat==PIX_FMT_BGR555)
3080 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3082 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3084 if (flags & SWS_PRINT_INFO)
3086 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3087 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3088 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3089 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3092 c->swScale= getSwsFunc(c);
3096 static void reset_ptr(uint8_t* src[], int format){
3097 if(!isALPHA(format))
3099 if(!isPlanarYUV(format)){
3101 if( format != PIX_FMT_PAL8
3102 && format != PIX_FMT_RGB8
3103 && format != PIX_FMT_BGR8
3104 && format != PIX_FMT_RGB4_BYTE
3105 && format != PIX_FMT_BGR4_BYTE
3112 * swscale wrapper, so we don't need to export the SwsContext.
3113 * Assumes planar YUV to be in YUV order instead of YVU.
3115 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3116 int srcSliceH, uint8_t* dst[], int dstStride[]){
3118 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3119 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3121 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3122 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3125 if (c->sliceDir == 0) {
3126 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3129 if (usePal(c->srcFormat)){
3130 for (i=0; i<256; i++){
3131 int p, r, g, b,y,u,v;
3132 if(c->srcFormat == PIX_FMT_PAL8){
3133 p=((uint32_t*)(src[1]))[i];
3137 }else if(c->srcFormat == PIX_FMT_RGB8){
3141 }else if(c->srcFormat == PIX_FMT_BGR8){
3145 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3150 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3155 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3156 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3157 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3158 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3161 switch(c->dstFormat) {
3166 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3168 case PIX_FMT_BGR32_1:
3172 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3174 case PIX_FMT_RGB32_1:
3178 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3185 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3190 // copy strides, so they can safely be modified
3191 if (c->sliceDir == 1) {
3192 // slices go from top to bottom
3193 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3194 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3196 reset_ptr(src2, c->srcFormat);
3197 reset_ptr(dst2, c->dstFormat);
3199 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3201 // slices go from bottom to top => we flip the image internally
3202 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3203 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3205 src2[0] += (srcSliceH-1)*srcStride[0];
3206 if (!usePal(c->srcFormat))
3207 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3208 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3209 src2[3] += (srcSliceH-1)*srcStride[3];
3210 dst2[0] += ( c->dstH -1)*dstStride[0];
3211 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3212 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3213 dst2[3] += ( c->dstH -1)*dstStride[3];
3215 reset_ptr(src2, c->srcFormat);
3216 reset_ptr(dst2, c->dstFormat);
3218 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3222 #if LIBSWSCALE_VERSION_MAJOR < 1
3223 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3224 int srcSliceH, uint8_t* dst[], int dstStride[]){
3225 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3229 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3230 float lumaSharpen, float chromaSharpen,
3231 float chromaHShift, float chromaVShift,
3234 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3236 if (lumaGBlur!=0.0){
3237 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3238 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3240 filter->lumH= sws_getIdentityVec();
3241 filter->lumV= sws_getIdentityVec();
3244 if (chromaGBlur!=0.0){
3245 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3246 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3248 filter->chrH= sws_getIdentityVec();
3249 filter->chrV= sws_getIdentityVec();
3252 if (chromaSharpen!=0.0){
3253 SwsVector *id= sws_getIdentityVec();
3254 sws_scaleVec(filter->chrH, -chromaSharpen);
3255 sws_scaleVec(filter->chrV, -chromaSharpen);
3256 sws_addVec(filter->chrH, id);
3257 sws_addVec(filter->chrV, id);
3261 if (lumaSharpen!=0.0){
3262 SwsVector *id= sws_getIdentityVec();
3263 sws_scaleVec(filter->lumH, -lumaSharpen);
3264 sws_scaleVec(filter->lumV, -lumaSharpen);
3265 sws_addVec(filter->lumH, id);
3266 sws_addVec(filter->lumV, id);
3270 if (chromaHShift != 0.0)
3271 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3273 if (chromaVShift != 0.0)
3274 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3276 sws_normalizeVec(filter->chrH, 1.0);
3277 sws_normalizeVec(filter->chrV, 1.0);
3278 sws_normalizeVec(filter->lumH, 1.0);
3279 sws_normalizeVec(filter->lumV, 1.0);
3281 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3282 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3287 SwsVector *sws_getGaussianVec(double variance, double quality){
3288 const int length= (int)(variance*quality + 0.5) | 1;
3290 double *coeff= av_malloc(length*sizeof(double));
3291 double middle= (length-1)*0.5;
3292 SwsVector *vec= av_malloc(sizeof(SwsVector));
3295 vec->length= length;
3297 for (i=0; i<length; i++)
3299 double dist= i-middle;
3300 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3303 sws_normalizeVec(vec, 1.0);
3308 SwsVector *sws_getConstVec(double c, int length){
3310 double *coeff= av_malloc(length*sizeof(double));
3311 SwsVector *vec= av_malloc(sizeof(SwsVector));
3314 vec->length= length;
3316 for (i=0; i<length; i++)
3323 SwsVector *sws_getIdentityVec(void){
3324 return sws_getConstVec(1.0, 1);
3327 double sws_dcVec(SwsVector *a){
3331 for (i=0; i<a->length; i++)
3337 void sws_scaleVec(SwsVector *a, double scalar){
3340 for (i=0; i<a->length; i++)
3341 a->coeff[i]*= scalar;
3344 void sws_normalizeVec(SwsVector *a, double height){
3345 sws_scaleVec(a, height/sws_dcVec(a));
3348 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3349 int length= a->length + b->length - 1;
3350 double *coeff= av_malloc(length*sizeof(double));
3352 SwsVector *vec= av_malloc(sizeof(SwsVector));
3355 vec->length= length;
3357 for (i=0; i<length; i++) coeff[i]= 0.0;
3359 for (i=0; i<a->length; i++)
3361 for (j=0; j<b->length; j++)
3363 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3370 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3371 int length= FFMAX(a->length, b->length);
3372 double *coeff= av_malloc(length*sizeof(double));
3374 SwsVector *vec= av_malloc(sizeof(SwsVector));
3377 vec->length= length;
3379 for (i=0; i<length; i++) coeff[i]= 0.0;
3381 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3382 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3387 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3388 int length= FFMAX(a->length, b->length);
3389 double *coeff= av_malloc(length*sizeof(double));
3391 SwsVector *vec= av_malloc(sizeof(SwsVector));
3394 vec->length= length;
3396 for (i=0; i<length; i++) coeff[i]= 0.0;
3398 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3399 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3404 /* shift left / or right if "shift" is negative */
3405 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3406 int length= a->length + FFABS(shift)*2;
3407 double *coeff= av_malloc(length*sizeof(double));
3409 SwsVector *vec= av_malloc(sizeof(SwsVector));
3412 vec->length= length;
3414 for (i=0; i<length; i++) coeff[i]= 0.0;
3416 for (i=0; i<a->length; i++)
3418 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3424 void sws_shiftVec(SwsVector *a, int shift){
3425 SwsVector *shifted= sws_getShiftedVec(a, shift);
3427 a->coeff= shifted->coeff;
3428 a->length= shifted->length;
3432 void sws_addVec(SwsVector *a, SwsVector *b){
3433 SwsVector *sum= sws_sumVec(a, b);
3435 a->coeff= sum->coeff;
3436 a->length= sum->length;
3440 void sws_subVec(SwsVector *a, SwsVector *b){
3441 SwsVector *diff= sws_diffVec(a, b);
3443 a->coeff= diff->coeff;
3444 a->length= diff->length;
3448 void sws_convVec(SwsVector *a, SwsVector *b){
3449 SwsVector *conv= sws_getConvVec(a, b);
3451 a->coeff= conv->coeff;
3452 a->length= conv->length;
3456 SwsVector *sws_cloneVec(SwsVector *a){
3457 double *coeff= av_malloc(a->length*sizeof(double));
3459 SwsVector *vec= av_malloc(sizeof(SwsVector));
3462 vec->length= a->length;
3464 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3469 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3475 for (i=0; i<a->length; i++)
3476 if (a->coeff[i]>max) max= a->coeff[i];
3478 for (i=0; i<a->length; i++)
3479 if (a->coeff[i]<min) min= a->coeff[i];
3483 for (i=0; i<a->length; i++)
3485 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3486 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3487 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3488 av_log(log_ctx, log_level, "|\n");
3492 #if LIBSWSCALE_VERSION_MAJOR < 1
3493 void sws_printVec(SwsVector *a){
3494 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3498 void sws_freeVec(SwsVector *a){
3500 av_freep(&a->coeff);
3505 void sws_freeFilter(SwsFilter *filter){
3506 if (!filter) return;
3508 if (filter->lumH) sws_freeVec(filter->lumH);
3509 if (filter->lumV) sws_freeVec(filter->lumV);
3510 if (filter->chrH) sws_freeVec(filter->chrH);
3511 if (filter->chrV) sws_freeVec(filter->chrV);
3516 void sws_freeContext(SwsContext *c){
3522 for (i=0; i<c->vLumBufSize; i++)
3523 av_freep(&c->lumPixBuf[i]);
3524 av_freep(&c->lumPixBuf);
3529 for (i=0; i<c->vChrBufSize; i++)
3530 av_freep(&c->chrPixBuf[i]);
3531 av_freep(&c->chrPixBuf);
3534 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3535 for (i=0; i<c->vLumBufSize; i++)
3536 av_freep(&c->alpPixBuf[i]);
3537 av_freep(&c->alpPixBuf);
3540 av_freep(&c->vLumFilter);
3541 av_freep(&c->vChrFilter);
3542 av_freep(&c->hLumFilter);
3543 av_freep(&c->hChrFilter);
3545 av_freep(&c->vYCoeffsBank);
3546 av_freep(&c->vCCoeffsBank);
3549 av_freep(&c->vLumFilterPos);
3550 av_freep(&c->vChrFilterPos);
3551 av_freep(&c->hLumFilterPos);
3552 av_freep(&c->hChrFilterPos);
3554 #if ARCH_X86 && CONFIG_GPL
3555 #ifdef MAP_ANONYMOUS
3556 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3557 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3558 #elif HAVE_VIRTUALALLOC
3559 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3560 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3562 av_free(c->funnyYCode );
3563 av_free(c->funnyUVCode);
3566 c->funnyUVCode=NULL;
3567 #endif /* ARCH_X86 && CONFIG_GPL */
<