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
1327 #define COMPILE_TEMPLATE_MMX 0
1328 #define COMPILE_TEMPLATE_MMX2 0
1329 #define COMPILE_TEMPLATE_AMD3DNOW 0
1330 #define COMPILE_TEMPLATE_ALTIVEC 0
1333 #define RENAME(a) a ## _C
1334 #include "swscale_template.c"
1337 #ifdef COMPILE_ALTIVEC
1339 #undef COMPILE_TEMPLATE_ALTIVEC
1340 #define COMPILE_TEMPLATE_ALTIVEC 1
1341 #define RENAME(a) a ## _altivec
1342 #include "swscale_template.c"
1350 #undef COMPILE_TEMPLATE_MMX
1351 #undef COMPILE_TEMPLATE_MMX2
1352 #undef COMPILE_TEMPLATE_AMD3DNOW
1353 #define COMPILE_TEMPLATE_MMX 1
1354 #define COMPILE_TEMPLATE_MMX2 0
1355 #define COMPILE_TEMPLATE_AMD3DNOW 0
1356 #define RENAME(a) a ## _MMX
1357 #include "swscale_template.c"
1363 #undef COMPILE_TEMPLATE_MMX
1364 #undef COMPILE_TEMPLATE_MMX2
1365 #undef COMPILE_TEMPLATE_AMD3DNOW
1366 #define COMPILE_TEMPLATE_MMX 1
1367 #define COMPILE_TEMPLATE_MMX2 1
1368 #define COMPILE_TEMPLATE_AMD3DNOW 0
1369 #define RENAME(a) a ## _MMX2
1370 #include "swscale_template.c"
1374 #ifdef COMPILE_3DNOW
1376 #undef COMPILE_TEMPLATE_MMX
1377 #undef COMPILE_TEMPLATE_MMX2
1378 #undef COMPILE_TEMPLATE_AMD3DNOW
1379 #define COMPILE_TEMPLATE_MMX 1
1380 #define COMPILE_TEMPLATE_MMX2 0
1381 #define COMPILE_TEMPLATE_AMD3DNOW 1
1382 #define RENAME(a) a ## _3DNow
1383 #include "swscale_template.c"
1388 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1390 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1391 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1392 else return getSplineCoeff( 0.0,
1399 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1400 int srcW, int dstW, int filterAlign, int one, int flags,
1401 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1407 int64_t *filter=NULL;
1408 int64_t *filter2=NULL;
1409 const int64_t fone= 1LL<<54;
1412 if (flags & SWS_CPU_CAPS_MMX)
1413 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1416 // NOTE: the +1 is for the MMX scaler which reads over the end
1417 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1419 if (FFABS(xInc - 0x10000) <10) // unscaled
1423 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1425 for (i=0; i<dstW; i++)
1427 filter[i*filterSize]= fone;
1432 else if (flags&SWS_POINT) // lame looking point sampling mode
1437 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1439 xDstInSrc= xInc/2 - 0x8000;
1440 for (i=0; i<dstW; i++)
1442 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1444 (*filterPos)[i]= xx;
1449 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1454 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1456 xDstInSrc= xInc/2 - 0x8000;
1457 for (i=0; i<dstW; i++)
1459 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1462 (*filterPos)[i]= xx;
1463 //bilinear upscale / linear interpolate / area averaging
1464 for (j=0; j<filterSize; j++)
1466 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1467 if (coeff<0) coeff=0;
1468 filter[i*filterSize + j]= coeff;
1479 if (flags&SWS_BICUBIC) sizeFactor= 4;
1480 else if (flags&SWS_X) sizeFactor= 8;
1481 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1482 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1483 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1484 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1485 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1486 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1488 sizeFactor= 0; //GCC warning killer
1492 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1493 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1495 if (filterSize > srcW-2) filterSize=srcW-2;
1497 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1499 xDstInSrc= xInc - 0x10000;
1500 for (i=0; i<dstW; i++)
1502 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1504 (*filterPos)[i]= xx;
1505 for (j=0; j<filterSize; j++)
1507 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1513 floatd= d * (1.0/(1<<30));
1515 if (flags & SWS_BICUBIC)
1517 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1518 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1519 int64_t dd = ( d*d)>>30;
1520 int64_t ddd= (dd*d)>>30;
1523 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1524 else if (d < 1LL<<31)
1525 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1528 coeff *= fone>>(30+24);
1530 /* else if (flags & SWS_X)
1532 double p= param ? param*0.01 : 0.3;
1533 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1534 coeff*= pow(2.0, - p*d*d);
1536 else if (flags & SWS_X)
1538 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1545 if (c<0.0) c= -pow(-c, A);
1547 coeff= (c*0.5 + 0.5)*fone;
1549 else if (flags & SWS_AREA)
1551 int64_t d2= d - (1<<29);
1552 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1553 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1555 coeff *= fone>>(30+16);
1557 else if (flags & SWS_GAUSS)
1559 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1560 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1562 else if (flags & SWS_SINC)
1564 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1566 else if (flags & SWS_LANCZOS)
1568 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1569 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1570 if (floatd>p) coeff=0;
1572 else if (flags & SWS_BILINEAR)
1575 if (coeff<0) coeff=0;
1576 coeff *= fone >> 30;
1578 else if (flags & SWS_SPLINE)
1580 double p=-2.196152422706632;
1581 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1584 coeff= 0.0; //GCC warning killer
1588 filter[i*filterSize + j]= coeff;
1595 /* apply src & dst Filter to filter -> filter2
1598 assert(filterSize>0);
1599 filter2Size= filterSize;
1600 if (srcFilter) filter2Size+= srcFilter->length - 1;
1601 if (dstFilter) filter2Size+= dstFilter->length - 1;
1602 assert(filter2Size>0);
1603 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1605 for (i=0; i<dstW; i++)
1610 for (k=0; k<srcFilter->length; k++){
1611 for (j=0; j<filterSize; j++)
1612 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1615 for (j=0; j<filterSize; j++)
1616 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1620 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1624 /* try to reduce the filter-size (step1 find size and shift left) */
1625 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1627 for (i=dstW-1; i>=0; i--)
1629 int min= filter2Size;
1633 /* get rid off near zero elements on the left by shifting left */
1634 for (j=0; j<filter2Size; j++)
1637 cutOff += FFABS(filter2[i*filter2Size]);
1639 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1641 /* preserve monotonicity because the core can't handle the filter otherwise */
1642 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1644 // move filter coefficients left
1645 for (k=1; k<filter2Size; k++)
1646 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1647 filter2[i*filter2Size + k - 1]= 0;
1652 /* count near zeros on the right */
1653 for (j=filter2Size-1; j>0; j--)
1655 cutOff += FFABS(filter2[i*filter2Size + j]);
1657 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1661 if (min>minFilterSize) minFilterSize= min;
1664 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1665 // we can handle the special case 4,
1666 // so we don't want to go to the full 8
1667 if (minFilterSize < 5)
1670 // We really don't want to waste our time
1671 // doing useless computation, so fall back on
1672 // the scalar C code for very small filters.
1673 // Vectorizing is worth it only if you have a
1674 // decent-sized vector.
1675 if (minFilterSize < 3)
1679 if (flags & SWS_CPU_CAPS_MMX) {
1680 // special case for unscaled vertical filtering
1681 if (minFilterSize == 1 && filterAlign == 2)
1685 assert(minFilterSize > 0);
1686 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1687 assert(filterSize > 0);
1688 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1689 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1691 *outFilterSize= filterSize;
1693 if (flags&SWS_PRINT_INFO)
1694 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1695 /* try to reduce the filter-size (step2 reduce it) */
1696 for (i=0; i<dstW; i++)
1700 for (j=0; j<filterSize; j++)
1702 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1703 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1704 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1705 filter[i*filterSize + j]= 0;
1710 //FIXME try to align filterPos if possible
1713 for (i=0; i<dstW; i++)
1716 if ((*filterPos)[i] < 0)
1718 // move filter coefficients left to compensate for filterPos
1719 for (j=1; j<filterSize; j++)
1721 int left= FFMAX(j + (*filterPos)[i], 0);
1722 filter[i*filterSize + left] += filter[i*filterSize + j];
1723 filter[i*filterSize + j]=0;
1728 if ((*filterPos)[i] + filterSize > srcW)
1730 int shift= (*filterPos)[i] + filterSize - srcW;
1731 // move filter coefficients right to compensate for filterPos
1732 for (j=filterSize-2; j>=0; j--)
1734 int right= FFMIN(j + shift, filterSize-1);
1735 filter[i*filterSize +right] += filter[i*filterSize +j];
1736 filter[i*filterSize +j]=0;
1738 (*filterPos)[i]= srcW - filterSize;
1742 // Note the +1 is for the MMX scaler which reads over the end
1743 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1744 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1746 /* normalize & store in outFilter */
1747 for (i=0; i<dstW; i++)
1753 for (j=0; j<filterSize; j++)
1755 sum+= filter[i*filterSize + j];
1757 sum= (sum + one/2)/ one;
1758 for (j=0; j<*outFilterSize; j++)
1760 int64_t v= filter[i*filterSize + j] + error;
1761 int intV= ROUNDED_DIV(v, sum);
1762 (*outFilter)[i*(*outFilterSize) + j]= intV;
1763 error= v - intV*sum;
1767 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1768 for (i=0; i<*outFilterSize; i++)
1770 int j= dstW*(*outFilterSize);
1771 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1782 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1785 x86_reg imm8OfPShufW1A;
1786 x86_reg imm8OfPShufW2A;
1787 x86_reg fragmentLengthA;
1789 x86_reg imm8OfPShufW1B;
1790 x86_reg imm8OfPShufW2B;
1791 x86_reg fragmentLengthB;
1796 // create an optimized horizontal scaling routine
1804 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1805 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1806 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1807 "punpcklbw %%mm7, %%mm1 \n\t"
1808 "punpcklbw %%mm7, %%mm0 \n\t"
1809 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1811 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1813 "psubw %%mm1, %%mm0 \n\t"
1814 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1815 "pmullw %%mm3, %%mm0 \n\t"
1816 "psllw $7, %%mm1 \n\t"
1817 "paddw %%mm1, %%mm0 \n\t"
1819 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1821 "add $8, %%"REG_a" \n\t"
1825 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1826 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1827 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1832 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1836 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1837 "=r" (fragmentLengthA)
1844 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1845 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1846 "punpcklbw %%mm7, %%mm0 \n\t"
1847 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1849 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1851 "psubw %%mm1, %%mm0 \n\t"
1852 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1853 "pmullw %%mm3, %%mm0 \n\t"
1854 "psllw $7, %%mm1 \n\t"
1855 "paddw %%mm1, %%mm0 \n\t"
1857 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1859 "add $8, %%"REG_a" \n\t"
1863 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1864 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1865 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1870 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1874 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1875 "=r" (fragmentLengthB)
1878 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1881 for (i=0; i<dstW/numSplits; i++)
1888 int b=((xpos+xInc)>>16) - xx;
1889 int c=((xpos+xInc*2)>>16) - xx;
1890 int d=((xpos+xInc*3)>>16) - xx;
1892 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
1893 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
1894 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
1895 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1897 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1898 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1899 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1900 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1904 int maxShift= 3-(d+inc);
1907 memcpy(funnyCode + fragmentPos, fragment, fragmentLength);
1909 funnyCode[fragmentPos + imm8OfPShufW1]=
1910 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1911 funnyCode[fragmentPos + imm8OfPShufW2]=
1912 a | (b<<2) | (c<<4) | (d<<6);
1914 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1915 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1917 if (shift && i>=shift)
1919 funnyCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1920 funnyCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1921 filterPos[i/2]-=shift;
1924 fragmentPos+= fragmentLength;
1927 funnyCode[fragmentPos]= RET;
1931 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1933 #endif /* COMPILE_MMX2 */
1935 static void globalInit(void){
1936 // generating tables:
1938 for (i=0; i<768; i++){
1939 int c= av_clip_uint8(i-256);
1944 static SwsFunc getSwsFunc(SwsContext *c)
1946 #if CONFIG_RUNTIME_CPUDETECT
1947 int flags = c->flags;
1949 #if ARCH_X86 && CONFIG_GPL
1950 // ordered per speed fastest first
1951 if (flags & SWS_CPU_CAPS_MMX2) {
1952 sws_init_swScale_MMX2(c);
1953 return swScale_MMX2;
1954 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1955 sws_init_swScale_3DNow(c);
1956 return swScale_3DNow;
1957 } else if (flags & SWS_CPU_CAPS_MMX) {
1958 sws_init_swScale_MMX(c);
1961 sws_init_swScale_C(c);
1967 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1968 sws_init_swScale_altivec(c);
1969 return swScale_altivec;
1971 sws_init_swScale_C(c);
1975 sws_init_swScale_C(c);
1977 #endif /* ARCH_X86 && CONFIG_GPL */
1978 #else //CONFIG_RUNTIME_CPUDETECT
1979 #if COMPILE_TEMPLATE_MMX2
1980 sws_init_swScale_MMX2(c);
1981 return swScale_MMX2;
1982 #elif COMPILE_TEMPLATE_AMD3DNOW
1983 sws_init_swScale_3DNow(c);
1984 return swScale_3DNow;
1985 #elif COMPILE_TEMPLATE_MMX
1986 sws_init_swScale_MMX(c);
1988 #elif COMPILE_TEMPLATE_ALTIVEC
1989 sws_init_swScale_altivec(c);
1990 return swScale_altivec;
1992 sws_init_swScale_C(c);
1995 #endif //!CONFIG_RUNTIME_CPUDETECT
1998 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1999 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2000 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2002 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2003 memcpy(dst, src[0], srcSliceH*dstStride[0]);
2007 const uint8_t *srcPtr= src[0];
2008 uint8_t *dstPtr= dst;
2009 for (i=0; i<srcSliceH; i++)
2011 memcpy(dstPtr, srcPtr, c->srcW);
2012 srcPtr+= srcStride[0];
2013 dstPtr+= dstStride[0];
2016 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2017 if (c->dstFormat == PIX_FMT_NV12)
2018 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2020 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2025 static int PlanarToYuy2Wrapper(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 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2034 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2035 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2036 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2038 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2043 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2044 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2045 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2047 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2052 static int YUV422PToUyvyWrapper(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 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2061 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2062 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2063 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2064 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2065 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2067 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2070 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2075 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2076 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2077 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2078 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2079 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2081 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2086 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2087 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2088 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2089 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2090 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2092 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2095 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2100 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2101 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2102 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2103 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2104 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2106 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2111 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2112 int srcSliceH, uint8_t* dst[], int dstStride[]){
2113 const enum PixelFormat srcFormat= c->srcFormat;
2114 const enum PixelFormat dstFormat= c->dstFormat;
2115 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2116 const uint8_t *palette)=NULL;
2118 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2119 uint8_t *srcPtr= src[0];
2121 if (!usePal(srcFormat))
2122 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2123 sws_format_name(srcFormat), sws_format_name(dstFormat));
2126 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2127 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2128 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2129 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2130 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2131 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2132 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2133 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2137 for (i=0; i<srcSliceH; i++) {
2138 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2139 srcPtr+= srcStride[0];
2140 dstPtr+= dstStride[0];
2146 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2147 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2148 int srcSliceH, uint8_t* dst[], int dstStride[]){
2149 const enum PixelFormat srcFormat= c->srcFormat;
2150 const enum PixelFormat dstFormat= c->dstFormat;
2151 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2152 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2153 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2154 const int dstId= fmt_depth(dstFormat) >> 2;
2155 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2158 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2159 || (isRGB(srcFormat) && isRGB(dstFormat))){
2160 switch(srcId | (dstId<<4)){
2161 case 0x34: conv= rgb16to15; break;
2162 case 0x36: conv= rgb24to15; break;
2163 case 0x38: conv= rgb32to15; break;
2164 case 0x43: conv= rgb15to16; break;
2165 case 0x46: conv= rgb24to16; break;
2166 case 0x48: conv= rgb32to16; break;
2167 case 0x63: conv= rgb15to24; break;
2168 case 0x64: conv= rgb16to24; break;
2169 case 0x68: conv= rgb32to24; break;
2170 case 0x83: conv= rgb15to32; break;
2171 case 0x84: conv= rgb16to32; break;
2172 case 0x86: conv= rgb24to32; break;
2173 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2174 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2176 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2177 || (isRGB(srcFormat) && isBGR(dstFormat))){
2178 switch(srcId | (dstId<<4)){
2179 case 0x33: conv= rgb15tobgr15; break;
2180 case 0x34: conv= rgb16tobgr15; break;
2181 case 0x36: conv= rgb24tobgr15; break;
2182 case 0x38: conv= rgb32tobgr15; break;
2183 case 0x43: conv= rgb15tobgr16; break;
2184 case 0x44: conv= rgb16tobgr16; break;
2185 case 0x46: conv= rgb24tobgr16; break;
2186 case 0x48: conv= rgb32tobgr16; break;
2187 case 0x63: conv= rgb15tobgr24; break;
2188 case 0x64: conv= rgb16tobgr24; break;
2189 case 0x66: conv= rgb24tobgr24; break;
2190 case 0x68: conv= rgb32tobgr24; break;
2191 case 0x83: conv= rgb15tobgr32; break;
2192 case 0x84: conv= rgb16tobgr32; break;
2193 case 0x86: conv= rgb24tobgr32; break;
2194 case 0x88: conv= rgb32tobgr32; break;
2195 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2196 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2199 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2200 sws_format_name(srcFormat), sws_format_name(dstFormat));
2205 uint8_t *srcPtr= src[0];
2206 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2207 srcPtr += ALT32_CORR;
2209 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2210 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2214 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2216 for (i=0; i<srcSliceH; i++)
2218 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2219 srcPtr+= srcStride[0];
2220 dstPtr+= dstStride[0];
2227 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2228 int srcSliceH, uint8_t* dst[], int dstStride[]){
2232 dst[0]+ srcSliceY *dstStride[0],
2233 dst[1]+(srcSliceY>>1)*dstStride[1],
2234 dst[2]+(srcSliceY>>1)*dstStride[2],
2236 dstStride[0], dstStride[1], srcStride[0]);
2238 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2242 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2243 int srcSliceH, uint8_t* dst[], int dstStride[]){
2247 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2248 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2250 uint8_t *srcPtr= src[0];
2251 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2253 for (i=0; i<srcSliceH; i++)
2255 memcpy(dstPtr, srcPtr, c->srcW);
2256 srcPtr+= srcStride[0];
2257 dstPtr+= dstStride[0];
2261 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2262 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2263 srcSliceH >> 2, srcStride[1], dstStride[1]);
2264 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2265 srcSliceH >> 2, srcStride[2], dstStride[2]);
2267 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2268 srcSliceH >> 2, srcStride[1], dstStride[2]);
2269 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2270 srcSliceH >> 2, srcStride[2], dstStride[1]);
2273 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2277 /* unscaled copy like stuff (assumes nearly identical formats) */
2278 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2279 int srcSliceH, uint8_t* dst[], int dstStride[])
2281 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2282 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2286 uint8_t *srcPtr= src[0];
2287 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2290 /* universal length finder */
2291 while(length+c->srcW <= FFABS(dstStride[0])
2292 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2295 for (i=0; i<srcSliceH; i++)
2297 memcpy(dstPtr, srcPtr, length);
2298 srcPtr+= srcStride[0];
2299 dstPtr+= dstStride[0];
2305 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2306 int srcSliceH, uint8_t* dst[], int dstStride[])
2309 for (plane=0; plane<4; plane++)
2311 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2312 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2313 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2314 uint8_t *srcPtr= src[plane];
2315 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2317 if (!dst[plane]) continue;
2318 // ignore palette for GRAY8
2319 if (plane == 1 && !dst[2]) continue;
2320 if (!src[plane] || (plane == 1 && !src[2])){
2321 if(is16BPS(c->dstFormat))
2323 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2326 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2327 if (!isBE(c->srcFormat)) srcPtr++;
2328 for (i=0; i<height; i++){
2329 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2330 srcPtr+= srcStride[plane];
2331 dstPtr+= dstStride[plane];
2333 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2334 for (i=0; i<height; i++){
2335 for (j=0; j<length; j++){
2336 dstPtr[ j<<1 ] = srcPtr[j];
2337 dstPtr[(j<<1)+1] = srcPtr[j];
2339 srcPtr+= srcStride[plane];
2340 dstPtr+= dstStride[plane];
2342 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2343 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2345 for (i=0; i<height; i++){
2346 for (j=0; j<length; j++)
2347 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2348 srcPtr+= srcStride[plane];
2349 dstPtr+= dstStride[plane];
2351 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2352 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2355 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2357 for (i=0; i<height; i++)
2359 memcpy(dstPtr, srcPtr, length);
2360 srcPtr+= srcStride[plane];
2361 dstPtr+= dstStride[plane];
2370 static void getSubSampleFactors(int *h, int *v, int format){
2372 case PIX_FMT_UYVY422:
2373 case PIX_FMT_YUYV422:
2377 case PIX_FMT_YUV420P:
2378 case PIX_FMT_YUV420PLE:
2379 case PIX_FMT_YUV420PBE:
2380 case PIX_FMT_YUVA420P:
2381 case PIX_FMT_GRAY16BE:
2382 case PIX_FMT_GRAY16LE:
2383 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2389 case PIX_FMT_YUV440P:
2393 case PIX_FMT_YUV410P:
2397 case PIX_FMT_YUV444P:
2398 case PIX_FMT_YUV444PLE:
2399 case PIX_FMT_YUV444PBE:
2403 case PIX_FMT_YUV422P:
2404 case PIX_FMT_YUV422PLE:
2405 case PIX_FMT_YUV422PBE:
2409 case PIX_FMT_YUV411P:
2420 static uint16_t roundToInt16(int64_t f){
2421 int r= (f + (1<<15))>>16;
2422 if (r<-0x7FFF) return 0x8000;
2423 else if (r> 0x7FFF) return 0x7FFF;
2427 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2428 int64_t crv = inv_table[0];
2429 int64_t cbu = inv_table[1];
2430 int64_t cgu = -inv_table[2];
2431 int64_t cgv = -inv_table[3];
2435 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2436 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2438 c->brightness= brightness;
2439 c->contrast = contrast;
2440 c->saturation= saturation;
2441 c->srcRange = srcRange;
2442 c->dstRange = dstRange;
2443 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2445 c->uOffset= 0x0400040004000400LL;
2446 c->vOffset= 0x0400040004000400LL;
2452 crv= (crv*224) / 255;
2453 cbu= (cbu*224) / 255;
2454 cgu= (cgu*224) / 255;
2455 cgv= (cgv*224) / 255;
2458 cy = (cy *contrast )>>16;
2459 crv= (crv*contrast * saturation)>>32;
2460 cbu= (cbu*contrast * saturation)>>32;
2461 cgu= (cgu*contrast * saturation)>>32;
2462 cgv= (cgv*contrast * saturation)>>32;
2464 oy -= 256*brightness;
2466 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2467 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2468 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2469 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2470 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2471 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2473 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2474 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2475 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2476 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2477 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2478 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2480 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2483 #ifdef COMPILE_ALTIVEC
2484 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2485 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2490 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2491 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2493 *inv_table = c->srcColorspaceTable;
2494 *table = c->dstColorspaceTable;
2495 *srcRange = c->srcRange;
2496 *dstRange = c->dstRange;
2497 *brightness= c->brightness;
2498 *contrast = c->contrast;
2499 *saturation= c->saturation;
2504 static int handle_jpeg(enum PixelFormat *format)
2507 case PIX_FMT_YUVJ420P:
2508 *format = PIX_FMT_YUV420P;
2510 case PIX_FMT_YUVJ422P:
2511 *format = PIX_FMT_YUV422P;
2513 case PIX_FMT_YUVJ444P:
2514 *format = PIX_FMT_YUV444P;
2516 case PIX_FMT_YUVJ440P:
2517 *format = PIX_FMT_YUV440P;
2524 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2525 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2530 int usesVFilter, usesHFilter;
2531 int unscaled, needsDither;
2532 int srcRange, dstRange;
2533 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2535 if (flags & SWS_CPU_CAPS_MMX)
2536 __asm__ volatile("emms\n\t"::: "memory");
2539 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2540 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2541 #if COMPILE_TEMPLATE_MMX2
2542 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2543 #elif COMPILE_TEMPLATE_AMD3DNOW
2544 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2545 #elif COMPILE_TEMPLATE_MMX
2546 flags |= SWS_CPU_CAPS_MMX;
2547 #elif COMPILE_TEMPLATE_ALTIVEC
2548 flags |= SWS_CPU_CAPS_ALTIVEC;
2550 flags |= SWS_CPU_CAPS_BFIN;
2552 #endif /* CONFIG_RUNTIME_CPUDETECT */
2553 if (clip_table[512] != 255) globalInit();
2554 if (!rgb15to16) sws_rgb2rgb_init(flags);
2556 unscaled = (srcW == dstW && srcH == dstH);
2557 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2558 && (fmt_depth(dstFormat))<24
2559 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2561 srcRange = handle_jpeg(&srcFormat);
2562 dstRange = handle_jpeg(&dstFormat);
2564 if (!isSupportedIn(srcFormat))
2566 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2569 if (!isSupportedOut(dstFormat))
2571 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2575 i= flags & ( SWS_POINT
2586 if(!i || (i & (i-1)))
2588 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2593 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
2595 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2596 srcW, srcH, dstW, dstH);
2599 if(srcW > VOFW || dstW > VOFW){
2600 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2604 if (!dstFilter) dstFilter= &dummyFilter;
2605 if (!srcFilter) srcFilter= &dummyFilter;
2607 c= av_mallocz(sizeof(SwsContext));
2609 c->av_class = &sws_context_class;
2614 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2615 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2617 c->dstFormat= dstFormat;
2618 c->srcFormat= srcFormat;
2619 c->vRounder= 4* 0x0001000100010001ULL;
2621 usesHFilter= usesVFilter= 0;
2622 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2623 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2624 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2625 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2626 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2627 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2628 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2629 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2631 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2632 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2634 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2635 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2637 // drop some chroma lines if the user wants it
2638 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2639 c->chrSrcVSubSample+= c->vChrDrop;
2641 // drop every other pixel for chroma calculation unless user wants full chroma
2642 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2643 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2644 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2645 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2646 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2647 c->chrSrcHSubSample=1;
2650 c->param[0] = param[0];
2651 c->param[1] = param[1];
2654 c->param[1] = SWS_PARAM_DEFAULT;
2657 // Note the -((-x)>>y) is so that we always round toward +inf.
2658 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2659 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2660 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2661 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2663 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2665 /* unscaled special cases */
2666 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2669 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2671 c->swScale= PlanarToNV12Wrapper;
2674 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2675 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2677 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2680 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2682 c->swScale= yvu9toyv12Wrapper;
2686 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2687 c->swScale= bgr24toyv12Wrapper;
2689 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2690 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2691 && (isBGR(dstFormat) || isRGB(dstFormat))
2692 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2693 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2694 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2695 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2696 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2697 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2698 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2699 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2700 && dstFormat != PIX_FMT_RGB32_1
2701 && dstFormat != PIX_FMT_BGR32_1
2702 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2703 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2704 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2705 c->swScale= rgb2rgbWrapper;
2707 if ((usePal(srcFormat) && (
2708 dstFormat == PIX_FMT_RGB32 ||
2709 dstFormat == PIX_FMT_RGB32_1 ||
2710 dstFormat == PIX_FMT_RGB24 ||
2711 dstFormat == PIX_FMT_BGR32 ||
2712 dstFormat == PIX_FMT_BGR32_1 ||
2713 dstFormat == PIX_FMT_BGR24)))
2714 c->swScale= pal2rgbWrapper;
2716 if (srcFormat == PIX_FMT_YUV422P)
2718 if (dstFormat == PIX_FMT_YUYV422)
2719 c->swScale= YUV422PToYuy2Wrapper;
2720 else if (dstFormat == PIX_FMT_UYVY422)
2721 c->swScale= YUV422PToUyvyWrapper;
2724 /* LQ converters if -sws 0 or -sws 4*/
2725 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2727 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2729 if (dstFormat == PIX_FMT_YUYV422)
2730 c->swScale= PlanarToYuy2Wrapper;
2731 else if (dstFormat == PIX_FMT_UYVY422)
2732 c->swScale= PlanarToUyvyWrapper;
2735 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2736 c->swScale= YUYV2YUV420Wrapper;
2737 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2738 c->swScale= UYVY2YUV420Wrapper;
2739 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2740 c->swScale= YUYV2YUV422Wrapper;
2741 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2742 c->swScale= UYVY2YUV422Wrapper;
2744 #ifdef COMPILE_ALTIVEC
2745 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2746 !(c->flags & SWS_BITEXACT) &&
2747 srcFormat == PIX_FMT_YUV420P) {
2748 // unscaled YV12 -> packed YUV, we want speed
2749 if (dstFormat == PIX_FMT_YUYV422)
2750 c->swScale= yv12toyuy2_unscaled_altivec;
2751 else if (dstFormat == PIX_FMT_UYVY422)
2752 c->swScale= yv12touyvy_unscaled_altivec;
2757 if ( srcFormat == dstFormat
2758 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2759 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2760 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2761 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2762 || (isGray(dstFormat) && isGray(srcFormat))
2763 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2764 && c->chrDstHSubSample == c->chrSrcHSubSample
2765 && c->chrDstVSubSample == c->chrSrcVSubSample))
2767 if (isPacked(c->srcFormat))
2768 c->swScale= packedCopy;
2769 else /* Planar YUV or gray */
2770 c->swScale= planarCopy;
2773 if (flags & SWS_CPU_CAPS_BFIN)
2774 ff_bfin_get_unscaled_swscale (c);
2778 if (flags&SWS_PRINT_INFO)
2779 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2780 sws_format_name(srcFormat), sws_format_name(dstFormat));
2785 if (flags & SWS_CPU_CAPS_MMX2)
2787 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2788 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2790 if (flags&SWS_PRINT_INFO)
2791 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2793 if (usesHFilter) c->canMMX2BeUsed=0;
2798 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2799 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2801 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2802 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2803 // n-2 is the last chrominance sample available
2804 // this is not perfect, but no one should notice the difference, the more correct variant
2805 // would be like the vertical one, but that would require some special code for the
2806 // first and last pixel
2807 if (flags&SWS_FAST_BILINEAR)
2809 if (c->canMMX2BeUsed)
2814 //we don't use the x86 asm scaler if MMX is available
2815 else if (flags & SWS_CPU_CAPS_MMX)
2817 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2818 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2822 /* precalculate horizontal scaler filter coefficients */
2824 const int filterAlign=
2825 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2826 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2829 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2830 srcW , dstW, filterAlign, 1<<14,
2831 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2832 srcFilter->lumH, dstFilter->lumH, c->param);
2833 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2834 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2835 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2836 srcFilter->chrH, dstFilter->chrH, c->param);
2838 #define MAX_FUNNY_CODE_SIZE 10000
2839 #if defined(COMPILE_MMX2)
2840 // can't downscale !!!
2841 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2843 #ifdef MAP_ANONYMOUS
2844 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2845 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2846 #elif HAVE_VIRTUALALLOC
2847 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2848 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2850 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2851 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2854 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2855 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2856 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2857 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2859 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2860 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2862 #endif /* defined(COMPILE_MMX2) */
2863 } // initialize horizontal stuff
2867 /* precalculate vertical scaler filter coefficients */
2869 const int filterAlign=
2870 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2871 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2874 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2875 srcH , dstH, filterAlign, (1<<12),
2876 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2877 srcFilter->lumV, dstFilter->lumV, c->param);
2878 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2879 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2880 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2881 srcFilter->chrV, dstFilter->chrV, c->param);
2884 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2885 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2887 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2889 short *p = (short *)&c->vYCoeffsBank[i];
2891 p[j] = c->vLumFilter[i];
2894 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2896 short *p = (short *)&c->vCCoeffsBank[i];
2898 p[j] = c->vChrFilter[i];
2903 // calculate buffer sizes so that they won't run out while handling these damn slices
2904 c->vLumBufSize= c->vLumFilterSize;
2905 c->vChrBufSize= c->vChrFilterSize;
2906 for (i=0; i<dstH; i++)
2908 int chrI= i*c->chrDstH / dstH;
2909 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2910 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2912 nextSlice>>= c->chrSrcVSubSample;
2913 nextSlice<<= c->chrSrcVSubSample;
2914 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2915 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2916 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2917 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2920 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2921 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2922 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2923 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2924 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2925 //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)
2926 /* align at 16 bytes for AltiVec */
2927 for (i=0; i<c->vLumBufSize; i++)
2928 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2929 for (i=0; i<c->vChrBufSize; i++)
2930 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2931 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2932 for (i=0; i<c->vLumBufSize; i++)
2933 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2935 //try to avoid drawing green stuff between the right end and the stride end
2936 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2938 assert(2*VOFW == VOF);
2940 assert(c->chrDstH <= dstH);
2942 if (flags&SWS_PRINT_INFO)
2945 const char *dither= " dithered";
2947 const char *dither= "";
2949 if (flags&SWS_FAST_BILINEAR)
2950 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2951 else if (flags&SWS_BILINEAR)
2952 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2953 else if (flags&SWS_BICUBIC)
2954 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2955 else if (flags&SWS_X)
2956 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2957 else if (flags&SWS_POINT)
2958 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2959 else if (flags&SWS_AREA)
2960 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2961 else if (flags&SWS_BICUBLIN)
2962 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2963 else if (flags&SWS_GAUSS)
2964 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2965 else if (flags&SWS_SINC)
2966 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2967 else if (flags&SWS_LANCZOS)
2968 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2969 else if (flags&SWS_SPLINE)
2970 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2972 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2974 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2975 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2976 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2978 av_log(c, AV_LOG_INFO, "from %s to %s ",
2979 sws_format_name(srcFormat), sws_format_name(dstFormat));
2981 if (flags & SWS_CPU_CAPS_MMX2)
2982 av_log(c, AV_LOG_INFO, "using MMX2\n");
2983 else if (flags & SWS_CPU_CAPS_3DNOW)
2984 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2985 else if (flags & SWS_CPU_CAPS_MMX)
2986 av_log(c, AV_LOG_INFO, "using MMX\n");
2987 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2988 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2990 av_log(c, AV_LOG_INFO, "using C\n");
2993 if (flags & SWS_PRINT_INFO)
2995 if (flags & SWS_CPU_CAPS_MMX)
2997 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2998 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3001 if (c->hLumFilterSize==4)
3002 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3003 else if (c->hLumFilterSize==8)
3004 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3006 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3008 if (c->hChrFilterSize==4)
3009 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3010 else if (c->hChrFilterSize==8)
3011 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3013 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3019 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3021 if (flags & SWS_FAST_BILINEAR)
3022 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3024 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3027 if (isPlanarYUV(dstFormat))
3029 if (c->vLumFilterSize==1)
3030 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3032 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3036 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3037 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3038 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3039 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3040 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3042 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3045 if (dstFormat==PIX_FMT_BGR24)
3046 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3047 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3048 else if (dstFormat==PIX_FMT_RGB32)
3049 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3050 else if (dstFormat==PIX_FMT_BGR565)
3051 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3052 else if (dstFormat==PIX_FMT_BGR555)
3053 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3055 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3057 if (flags & SWS_PRINT_INFO)
3059 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3060 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3061 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3062 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3065 c->swScale= getSwsFunc(c);
3069 static void reset_ptr(uint8_t* src[], int format){
3070 if(!isALPHA(format))
3072 if(!isPlanarYUV(format)){
3074 if( format != PIX_FMT_PAL8
3075 && format != PIX_FMT_RGB8
3076 && format != PIX_FMT_BGR8
3077 && format != PIX_FMT_RGB4_BYTE
3078 && format != PIX_FMT_BGR4_BYTE
3085 * swscale wrapper, so we don't need to export the SwsContext.
3086 * Assumes planar YUV to be in YUV order instead of YVU.
3088 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3089 int srcSliceH, uint8_t* dst[], int dstStride[]){
3091 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3092 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3094 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3095 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3098 if (c->sliceDir == 0) {
3099 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3102 if (usePal(c->srcFormat)){
3103 for (i=0; i<256; i++){
3104 int p, r, g, b,y,u,v;
3105 if(c->srcFormat == PIX_FMT_PAL8){
3106 p=((uint32_t*)(src[1]))[i];
3110 }else if(c->srcFormat == PIX_FMT_RGB8){
3114 }else if(c->srcFormat == PIX_FMT_BGR8){
3118 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3123 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3128 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3129 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3130 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3131 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3134 switch(c->dstFormat) {
3139 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3141 case PIX_FMT_BGR32_1:
3145 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3147 case PIX_FMT_RGB32_1:
3151 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3158 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3163 // copy strides, so they can safely be modified
3164 if (c->sliceDir == 1) {
3165 // slices go from top to bottom
3166 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3167 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3169 reset_ptr(src2, c->srcFormat);
3170 reset_ptr(dst2, c->dstFormat);
3172 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3174 // slices go from bottom to top => we flip the image internally
3175 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3176 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3178 src2[0] += (srcSliceH-1)*srcStride[0];
3179 if (!usePal(c->srcFormat))
3180 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3181 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3182 src2[3] += (srcSliceH-1)*srcStride[3];
3183 dst2[0] += ( c->dstH -1)*dstStride[0];
3184 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3185 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3186 dst2[3] += ( c->dstH -1)*dstStride[3];
3188 reset_ptr(src2, c->srcFormat);
3189 reset_ptr(dst2, c->dstFormat);
3191 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3195 #if LIBSWSCALE_VERSION_MAJOR < 1
3196 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3197 int srcSliceH, uint8_t* dst[], int dstStride[]){
3198 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3202 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3203 float lumaSharpen, float chromaSharpen,
3204 float chromaHShift, float chromaVShift,
3207 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3209 if (lumaGBlur!=0.0){
3210 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3211 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3213 filter->lumH= sws_getIdentityVec();
3214 filter->lumV= sws_getIdentityVec();
3217 if (chromaGBlur!=0.0){
3218 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3219 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3221 filter->chrH= sws_getIdentityVec();
3222 filter->chrV= sws_getIdentityVec();
3225 if (chromaSharpen!=0.0){
3226 SwsVector *id= sws_getIdentityVec();
3227 sws_scaleVec(filter->chrH, -chromaSharpen);
3228 sws_scaleVec(filter->chrV, -chromaSharpen);
3229 sws_addVec(filter->chrH, id);
3230 sws_addVec(filter->chrV, id);
3234 if (lumaSharpen!=0.0){
3235 SwsVector *id= sws_getIdentityVec();
3236 sws_scaleVec(filter->lumH, -lumaSharpen);
3237 sws_scaleVec(filter->lumV, -lumaSharpen);
3238 sws_addVec(filter->lumH, id);
3239 sws_addVec(filter->lumV, id);
3243 if (chromaHShift != 0.0)
3244 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3246 if (chromaVShift != 0.0)
3247 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3249 sws_normalizeVec(filter->chrH, 1.0);
3250 sws_normalizeVec(filter->chrV, 1.0);
3251 sws_normalizeVec(filter->lumH, 1.0);
3252 sws_normalizeVec(filter->lumV, 1.0);
3254 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3255 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3260 SwsVector *sws_getGaussianVec(double variance, double quality){
3261 const int length= (int)(variance*quality + 0.5) | 1;
3263 double *coeff= av_malloc(length*sizeof(double));
3264 double middle= (length-1)*0.5;
3265 SwsVector *vec= av_malloc(sizeof(SwsVector));
3268 vec->length= length;
3270 for (i=0; i<length; i++)
3272 double dist= i-middle;
3273 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3276 sws_normalizeVec(vec, 1.0);
3281 SwsVector *sws_getConstVec(double c, int length){
3283 double *coeff= av_malloc(length*sizeof(double));
3284 SwsVector *vec= av_malloc(sizeof(SwsVector));
3287 vec->length= length;
3289 for (i=0; i<length; i++)
3296 SwsVector *sws_getIdentityVec(void){
3297 return sws_getConstVec(1.0, 1);
3300 double sws_dcVec(SwsVector *a){
3304 for (i=0; i<a->length; i++)
3310 void sws_scaleVec(SwsVector *a, double scalar){
3313 for (i=0; i<a->length; i++)
3314 a->coeff[i]*= scalar;
3317 void sws_normalizeVec(SwsVector *a, double height){
3318 sws_scaleVec(a, height/sws_dcVec(a));
3321 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3322 int length= a->length + b->length - 1;
3323 double *coeff= av_malloc(length*sizeof(double));
3325 SwsVector *vec= av_malloc(sizeof(SwsVector));
3328 vec->length= length;
3330 for (i=0; i<length; i++) coeff[i]= 0.0;
3332 for (i=0; i<a->length; i++)
3334 for (j=0; j<b->length; j++)
3336 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3343 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3344 int length= FFMAX(a->length, b->length);
3345 double *coeff= av_malloc(length*sizeof(double));
3347 SwsVector *vec= av_malloc(sizeof(SwsVector));
3350 vec->length= length;
3352 for (i=0; i<length; i++) coeff[i]= 0.0;
3354 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3355 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3360 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3361 int length= FFMAX(a->length, b->length);
3362 double *coeff= av_malloc(length*sizeof(double));
3364 SwsVector *vec= av_malloc(sizeof(SwsVector));
3367 vec->length= length;
3369 for (i=0; i<length; i++) coeff[i]= 0.0;
3371 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3372 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3377 /* shift left / or right if "shift" is negative */
3378 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3379 int length= a->length + FFABS(shift)*2;
3380 double *coeff= av_malloc(length*sizeof(double));
3382 SwsVector *vec= av_malloc(sizeof(SwsVector));
3385 vec->length= length;
3387 for (i=0; i<length; i++) coeff[i]= 0.0;
3389 for (i=0; i<a->length; i++)
3391 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3397 void sws_shiftVec(SwsVector *a, int shift){
3398 SwsVector *shifted= sws_getShiftedVec(a, shift);
3400 a->coeff= shifted->coeff;
3401 a->length= shifted->length;
3405 void sws_addVec(SwsVector *a, SwsVector *b){
3406 SwsVector *sum= sws_sumVec(a, b);
3408 a->coeff= sum->coeff;
3409 a->length= sum->length;
3413 void sws_subVec(SwsVector *a, SwsVector *b){
3414 SwsVector *diff= sws_diffVec(a, b);
3416 a->coeff= diff->coeff;
3417 a->length= diff->length;
3421 void sws_convVec(SwsVector *a, SwsVector *b){
3422 SwsVector *conv= sws_getConvVec(a, b);
3424 a->coeff= conv->coeff;
3425 a->length= conv->length;
3429 SwsVector *sws_cloneVec(SwsVector *a){
3430 double *coeff= av_malloc(a->length*sizeof(double));
3432 SwsVector *vec= av_malloc(sizeof(SwsVector));
3435 vec->length= a->length;
3437 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3442 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3448 for (i=0; i<a->length; i++)
3449 if (a->coeff[i]>max) max= a->coeff[i];
3451 for (i=0; i<a->length; i++)
3452 if (a->coeff[i]<min) min= a->coeff[i];
3456 for (i=0; i<a->length; i++)
3458 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3459 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3460 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3461 av_log(log_ctx, log_level, "|\n");
3465 #if LIBSWSCALE_VERSION_MAJOR < 1
3466 void sws_printVec(SwsVector *a){
3467 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3471 void sws_freeVec(SwsVector *a){
3473 av_freep(&a->coeff);
3478 void sws_freeFilter(SwsFilter *filter){
3479 if (!filter) return;
3481 if (filter->lumH) sws_freeVec(filter->lumH);
3482 if (filter->lumV) sws_freeVec(filter->lumV);
3483 if (filter->chrH) sws_freeVec(filter->chrH);
3484 if (filter->chrV) sws_freeVec(filter->chrV);
3489 void sws_freeContext(SwsContext *c){
3495 for (i=0; i<c->vLumBufSize; i++)
3496 av_freep(&c->lumPixBuf[i]);
3497 av_freep(&c->lumPixBuf);
3502 for (i=0; i<c->vChrBufSize; i++)
3503 av_freep(&c->chrPixBuf[i]);
3504 av_freep(&c->chrPixBuf);
3507 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3508 for (i=0; i<c->vLumBufSize; i++)
3509 av_freep(&c->alpPixBuf[i]);
3510 av_freep(&c->alpPixBuf);
3513 av_freep(&c->vLumFilter);
3514 av_freep(&c->vChrFilter);
3515 av_freep(&c->hLumFilter);
3516 av_freep(&c->hChrFilter);
3518 av_freep(&c->vYCoeffsBank);
3519 av_freep(&c->vCCoeffsBank);
3522 av_freep(&c->vLumFilterPos);
3523 av_freep(&c->vChrFilterPos);
3524 av_freep(&c->hLumFilterPos);
3525 av_freep(&c->hChrFilterPos);
3527 #if ARCH_X86 && CONFIG_GPL
3528 #ifdef MAP_ANONYMOUS
3529 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3530 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3531 #elif HAVE_VIRTUALALLOC
3532 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3533 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3535 av_free(c->funnyYCode );
3536 av_free(c->funnyUVCode);
3539 c->funnyUVCode=NULL;
3540 #endif /* ARCH_X86 && CONFIG_GPL */
3542 av_freep(&c->lumMmx2Filter);
3543 av_freep(&c->chrMmx2Filter);
3544 av_freep(&c->lumMmx2FilterPos);
3545 av_freep(&c->chrMmx2FilterPos);
3546 av_freep(&c->yuvTable);
3551 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3552 int srcW, int srcH, enum PixelFormat srcFormat,
3553 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3554 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3556 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3559 param = default_param;
3562 if (context->srcW != srcW || context->srcH != srcH ||
3563 context->srcFormat != srcFormat ||
3564 context->dstW != dstW || context->dstH != dstH ||
3565 context->dstFormat != dstFormat || context->flags != flags ||
3566 context->param[0] != param[0] || context->param[1] != param[1])
3568 sws_freeContext(context);
3573 return sws_getContext(srcW, srcH, srcFormat,
3574 dstW, dstH, dstFormat, flags,
3575 srcFilter, dstFilter, param);