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, },
305 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
306 { 0, 55, 14, 68, 3, 58, 17, 72, },
307 { 37, 18, 50, 32, 40, 22, 54, 35, },
308 { 9, 64, 5, 59, 13, 67, 8, 63, },
309 { 46, 27, 41, 23, 49, 31, 44, 26, },
310 { 2, 57, 16, 71, 1, 56, 15, 70, },
311 { 39, 21, 52, 34, 38, 19, 51, 33, },
312 { 11, 66, 7, 62, 10, 65, 6, 60, },
313 { 48, 30, 43, 25, 47, 29, 42, 24, },
317 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
318 {117, 62, 158, 103, 113, 58, 155, 100, },
319 { 34, 199, 21, 186, 31, 196, 17, 182, },
320 {144, 89, 131, 76, 141, 86, 127, 72, },
321 { 0, 165, 41, 206, 10, 175, 52, 217, },
322 {110, 55, 151, 96, 120, 65, 162, 107, },
323 { 28, 193, 14, 179, 38, 203, 24, 189, },
324 {138, 83, 124, 69, 148, 93, 134, 79, },
325 { 7, 172, 48, 213, 3, 168, 45, 210, },
328 // tries to correct a gamma of 1.5
329 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
330 { 0, 143, 18, 200, 2, 156, 25, 215, },
331 { 78, 28, 125, 64, 89, 36, 138, 74, },
332 { 10, 180, 3, 161, 16, 195, 8, 175, },
333 {109, 51, 93, 38, 121, 60, 105, 47, },
334 { 1, 152, 23, 210, 0, 147, 20, 205, },
335 { 85, 33, 134, 71, 81, 30, 130, 67, },
336 { 14, 190, 6, 171, 12, 185, 5, 166, },
337 {117, 57, 101, 44, 113, 54, 97, 41, },
340 // tries to correct a gamma of 2.0
341 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
342 { 0, 124, 8, 193, 0, 140, 12, 213, },
343 { 55, 14, 104, 42, 66, 19, 119, 52, },
344 { 3, 168, 1, 145, 6, 187, 3, 162, },
345 { 86, 31, 70, 21, 99, 39, 82, 28, },
346 { 0, 134, 11, 206, 0, 129, 9, 200, },
347 { 62, 17, 114, 48, 58, 16, 109, 45, },
348 { 5, 181, 2, 157, 4, 175, 1, 151, },
349 { 95, 36, 78, 26, 90, 34, 74, 24, },
352 // tries to correct a gamma of 2.5
353 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
354 { 0, 107, 3, 187, 0, 125, 6, 212, },
355 { 39, 7, 86, 28, 49, 11, 102, 36, },
356 { 1, 158, 0, 131, 3, 180, 1, 151, },
357 { 68, 19, 52, 12, 81, 25, 64, 17, },
358 { 0, 119, 5, 203, 0, 113, 4, 195, },
359 { 45, 9, 96, 33, 42, 8, 91, 30, },
360 { 2, 172, 1, 144, 2, 165, 0, 137, },
361 { 77, 23, 60, 15, 72, 21, 56, 14, },
365 const char *sws_format_name(enum PixelFormat format)
368 case PIX_FMT_YUV420P:
370 case PIX_FMT_YUVA420P:
372 case PIX_FMT_YUYV422:
378 case PIX_FMT_YUV422P:
380 case PIX_FMT_YUV444P:
384 case PIX_FMT_YUV410P:
386 case PIX_FMT_YUV411P:
392 case PIX_FMT_GRAY16BE:
394 case PIX_FMT_GRAY16LE:
398 case PIX_FMT_MONOWHITE:
400 case PIX_FMT_MONOBLACK:
404 case PIX_FMT_YUVJ420P:
406 case PIX_FMT_YUVJ422P:
408 case PIX_FMT_YUVJ444P:
410 case PIX_FMT_XVMC_MPEG2_MC:
411 return "xvmc_mpeg2_mc";
412 case PIX_FMT_XVMC_MPEG2_IDCT:
413 return "xvmc_mpeg2_idct";
414 case PIX_FMT_UYVY422:
416 case PIX_FMT_UYYVYY411:
418 case PIX_FMT_RGB32_1:
420 case PIX_FMT_BGR32_1:
432 case PIX_FMT_BGR4_BYTE:
438 case PIX_FMT_RGB4_BYTE:
440 case PIX_FMT_RGB48BE:
442 case PIX_FMT_RGB48LE:
448 case PIX_FMT_YUV440P:
450 case PIX_FMT_VDPAU_H264:
452 case PIX_FMT_VDPAU_MPEG1:
453 return "vdpau_mpeg1";
454 case PIX_FMT_VDPAU_MPEG2:
455 return "vdpau_mpeg2";
456 case PIX_FMT_VDPAU_WMV3:
458 case PIX_FMT_VDPAU_VC1:
460 case PIX_FMT_YUV420PLE:
462 case PIX_FMT_YUV422PLE:
464 case PIX_FMT_YUV444PLE:
466 case PIX_FMT_YUV420PBE:
468 case PIX_FMT_YUV422PBE:
470 case PIX_FMT_YUV444PBE:
473 return "Unknown format";
477 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
478 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
479 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
481 //FIXME Optimize (just quickly written not optimized..)
483 for (i=0; i<dstW; i++)
487 for (j=0; j<lumFilterSize; j++)
488 val += lumSrc[j][i] * lumFilter[j];
490 dest[i]= av_clip_uint8(val>>19);
494 for (i=0; i<chrDstW; i++)
499 for (j=0; j<chrFilterSize; j++)
501 u += chrSrc[j][i] * chrFilter[j];
502 v += chrSrc[j][i + VOFW] * chrFilter[j];
505 uDest[i]= av_clip_uint8(u>>19);
506 vDest[i]= av_clip_uint8(v>>19);
509 if (CONFIG_SWSCALE_ALPHA && aDest)
510 for (i=0; i<dstW; i++){
513 for (j=0; j<lumFilterSize; j++)
514 val += alpSrc[j][i] * lumFilter[j];
516 aDest[i]= av_clip_uint8(val>>19);
521 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
522 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
523 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
525 //FIXME Optimize (just quickly written not optimized..)
527 for (i=0; i<dstW; i++)
531 for (j=0; j<lumFilterSize; j++)
532 val += lumSrc[j][i] * lumFilter[j];
534 dest[i]= av_clip_uint8(val>>19);
540 if (dstFormat == PIX_FMT_NV12)
541 for (i=0; i<chrDstW; i++)
546 for (j=0; j<chrFilterSize; j++)
548 u += chrSrc[j][i] * chrFilter[j];
549 v += chrSrc[j][i + VOFW] * chrFilter[j];
552 uDest[2*i]= av_clip_uint8(u>>19);
553 uDest[2*i+1]= av_clip_uint8(v>>19);
556 for (i=0; i<chrDstW; i++)
561 for (j=0; j<chrFilterSize; j++)
563 u += chrSrc[j][i] * chrFilter[j];
564 v += chrSrc[j][i + VOFW] * chrFilter[j];
567 uDest[2*i]= av_clip_uint8(v>>19);
568 uDest[2*i+1]= av_clip_uint8(u>>19);
572 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
573 for (i=0; i<(dstW>>1); i++){\
579 int av_unused A1, A2;\
580 type av_unused *r, *b, *g;\
583 for (j=0; j<lumFilterSize; j++)\
585 Y1 += lumSrc[j][i2] * lumFilter[j];\
586 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
588 for (j=0; j<chrFilterSize; j++)\
590 U += chrSrc[j][i] * chrFilter[j];\
591 V += chrSrc[j][i+VOFW] * chrFilter[j];\
600 for (j=0; j<lumFilterSize; j++){\
601 A1 += alpSrc[j][i2 ] * lumFilter[j];\
602 A2 += alpSrc[j][i2+1] * lumFilter[j];\
608 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
609 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
610 if ((Y1|Y2|U|V)&256)\
612 if (Y1>255) Y1=255; \
613 else if (Y1<0)Y1=0; \
614 if (Y2>255) Y2=255; \
615 else if (Y2<0)Y2=0; \
621 if (alpha && ((A1|A2)&256)){\
622 A1=av_clip_uint8(A1);\
623 A2=av_clip_uint8(A2);\
626 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
627 for (i=0; i<dstW; i++){\
635 for (j=0; j<lumFilterSize; j++){\
636 Y += lumSrc[j][i ] * lumFilter[j];\
638 for (j=0; j<chrFilterSize; j++){\
639 U += chrSrc[j][i ] * chrFilter[j];\
640 V += chrSrc[j][i+VOFW] * chrFilter[j];\
647 for (j=0; j<lumFilterSize; j++)\
648 A += alpSrc[j][i ] * lumFilter[j];\
651 A = av_clip_uint8(A);\
654 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
655 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
656 Y-= c->yuv2rgb_y_offset;\
657 Y*= c->yuv2rgb_y_coeff;\
659 R= Y + V*c->yuv2rgb_v2r_coeff;\
660 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
661 B= Y + U*c->yuv2rgb_u2b_coeff;\
662 if ((R|G|B)&(0xC0000000)){\
663 if (R>=(256<<22)) R=(256<<22)-1; \
665 if (G>=(256<<22)) G=(256<<22)-1; \
667 if (B>=(256<<22)) B=(256<<22)-1; \
672 #define YSCALE_YUV_2_GRAY16_C \
673 for (i=0; i<(dstW>>1); i++){\
682 for (j=0; j<lumFilterSize; j++)\
684 Y1 += lumSrc[j][i2] * lumFilter[j];\
685 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
689 if ((Y1|Y2|U|V)&65536)\
691 if (Y1>65535) Y1=65535; \
692 else if (Y1<0)Y1=0; \
693 if (Y2>65535) Y2=65535; \
694 else if (Y2<0)Y2=0; \
697 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
698 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
699 r = (type *)c->table_rV[V]; \
700 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
701 b = (type *)c->table_bU[U]; \
703 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
704 for (i=0; i<(dstW>>1); i++){ \
706 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
707 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
708 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
709 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
710 type av_unused *r, *b, *g; \
711 int av_unused A1, A2; \
713 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
714 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
717 #define YSCALE_YUV_2_GRAY16_2_C \
718 for (i=0; i<(dstW>>1); i++){ \
720 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
721 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
723 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
724 YSCALE_YUV_2_PACKED2_C(type,alpha)\
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_PACKED1_C(type,alpha) \
730 for (i=0; i<(dstW>>1); i++){\
732 int Y1= buf0[i2 ]>>7;\
733 int Y2= buf0[i2+1]>>7;\
734 int U= (uvbuf1[i ])>>7;\
735 int V= (uvbuf1[i+VOFW])>>7;\
736 type av_unused *r, *b, *g;\
737 int av_unused A1, A2;\
743 #define YSCALE_YUV_2_GRAY16_1_C \
744 for (i=0; i<(dstW>>1); i++){\
746 int Y1= buf0[i2 ]<<1;\
747 int Y2= buf0[i2+1]<<1;\
749 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
750 YSCALE_YUV_2_PACKED1_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_PACKED1B_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= (uvbuf0[i ] + uvbuf1[i ])>>8;\
761 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
762 type av_unused *r, *b, *g;\
763 int av_unused A1, A2;\
769 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
770 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
771 r = (type *)c->table_rV[V];\
772 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
773 b = (type *)c->table_bU[U];\
775 #define YSCALE_YUV_2_MONO2_C \
776 const uint8_t * const d128=dither_8x8_220[y&7];\
777 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
778 for (i=0; i<dstW-7; i+=8){\
780 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
781 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
782 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
783 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
784 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
785 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
786 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
787 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
788 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
793 #define YSCALE_YUV_2_MONOX_C \
794 const uint8_t * const d128=dither_8x8_220[y&7];\
795 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
797 for (i=0; i<dstW-1; i+=2){\
802 for (j=0; j<lumFilterSize; j++)\
804 Y1 += lumSrc[j][i] * lumFilter[j];\
805 Y2 += lumSrc[j][i+1] * lumFilter[j];\
816 acc+= acc + g[Y1+d128[(i+0)&7]];\
817 acc+= acc + g[Y2+d128[(i+1)&7]];\
819 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
825 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
826 switch(c->dstFormat)\
828 case PIX_FMT_RGB48BE:\
829 case PIX_FMT_RGB48LE:\
831 ((uint8_t*)dest)[ 0]= r[Y1];\
832 ((uint8_t*)dest)[ 1]= r[Y1];\
833 ((uint8_t*)dest)[ 2]= g[Y1];\
834 ((uint8_t*)dest)[ 3]= g[Y1];\
835 ((uint8_t*)dest)[ 4]= b[Y1];\
836 ((uint8_t*)dest)[ 5]= b[Y1];\
837 ((uint8_t*)dest)[ 6]= r[Y2];\
838 ((uint8_t*)dest)[ 7]= r[Y2];\
839 ((uint8_t*)dest)[ 8]= g[Y2];\
840 ((uint8_t*)dest)[ 9]= g[Y2];\
841 ((uint8_t*)dest)[10]= b[Y2];\
842 ((uint8_t*)dest)[11]= b[Y2];\
849 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
850 func(uint32_t,needAlpha)\
851 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
852 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
855 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
857 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
858 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
862 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
863 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
871 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
872 func(uint32_t,needAlpha)\
873 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
874 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
877 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
879 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
880 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
884 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
885 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
892 ((uint8_t*)dest)[0]= r[Y1];\
893 ((uint8_t*)dest)[1]= g[Y1];\
894 ((uint8_t*)dest)[2]= b[Y1];\
895 ((uint8_t*)dest)[3]= r[Y2];\
896 ((uint8_t*)dest)[4]= g[Y2];\
897 ((uint8_t*)dest)[5]= b[Y2];\
903 ((uint8_t*)dest)[0]= b[Y1];\
904 ((uint8_t*)dest)[1]= g[Y1];\
905 ((uint8_t*)dest)[2]= r[Y1];\
906 ((uint8_t*)dest)[3]= b[Y2];\
907 ((uint8_t*)dest)[4]= g[Y2];\
908 ((uint8_t*)dest)[5]= r[Y2];\
912 case PIX_FMT_RGB565:\
913 case PIX_FMT_BGR565:\
915 const int dr1= dither_2x2_8[y&1 ][0];\
916 const int dg1= dither_2x2_4[y&1 ][0];\
917 const int db1= dither_2x2_8[(y&1)^1][0];\
918 const int dr2= dither_2x2_8[y&1 ][1];\
919 const int dg2= dither_2x2_4[y&1 ][1];\
920 const int db2= dither_2x2_8[(y&1)^1][1];\
922 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
923 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
927 case PIX_FMT_RGB555:\
928 case PIX_FMT_BGR555:\
930 const int dr1= dither_2x2_8[y&1 ][0];\
931 const int dg1= dither_2x2_8[y&1 ][1];\
932 const int db1= dither_2x2_8[(y&1)^1][0];\
933 const int dr2= dither_2x2_8[y&1 ][1];\
934 const int dg2= dither_2x2_8[y&1 ][0];\
935 const int db2= dither_2x2_8[(y&1)^1][1];\
937 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
938 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
945 const uint8_t * const d64= dither_8x8_73[y&7];\
946 const uint8_t * const d32= dither_8x8_32[y&7];\
948 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
949 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
956 const uint8_t * const d64= dither_8x8_73 [y&7];\
957 const uint8_t * const d128=dither_8x8_220[y&7];\
959 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
960 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
964 case PIX_FMT_RGB4_BYTE:\
965 case PIX_FMT_BGR4_BYTE:\
967 const uint8_t * const d64= dither_8x8_73 [y&7];\
968 const uint8_t * const d128=dither_8x8_220[y&7];\
970 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
971 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
975 case PIX_FMT_MONOBLACK:\
976 case PIX_FMT_MONOWHITE:\
981 case PIX_FMT_YUYV422:\
983 ((uint8_t*)dest)[2*i2+0]= Y1;\
984 ((uint8_t*)dest)[2*i2+1]= U;\
985 ((uint8_t*)dest)[2*i2+2]= Y2;\
986 ((uint8_t*)dest)[2*i2+3]= V;\
989 case PIX_FMT_UYVY422:\
991 ((uint8_t*)dest)[2*i2+0]= U;\
992 ((uint8_t*)dest)[2*i2+1]= Y1;\
993 ((uint8_t*)dest)[2*i2+2]= V;\
994 ((uint8_t*)dest)[2*i2+3]= Y2;\
997 case PIX_FMT_GRAY16BE:\
999 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1000 ((uint8_t*)dest)[2*i2+1]= Y1;\
1001 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1002 ((uint8_t*)dest)[2*i2+3]= Y2;\
1005 case PIX_FMT_GRAY16LE:\
1007 ((uint8_t*)dest)[2*i2+0]= Y1;\
1008 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1009 ((uint8_t*)dest)[2*i2+2]= Y2;\
1010 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1016 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1017 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1018 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1021 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)
1024 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1025 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1026 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1029 int step= fmt_depth(c->dstFormat)/8;
1032 switch(c->dstFormat){
1040 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1041 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1042 dest[aidx]= needAlpha ? A : 255;
1049 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1050 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1058 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1075 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1076 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1077 dest[aidx]= needAlpha ? A : 255;
1084 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1085 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1093 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1108 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1110 uint8_t *ptr = plane + stride*y;
1111 for (i=0; i<height; i++){
1112 memset(ptr, val, width);
1117 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1120 for (i = 0; i < width; i++) {
1125 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1129 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1130 uint8_t *src1, uint8_t *src2, int width)
1134 for (i = 0; i < width; i++) {
1135 int r = src1[6*i + 0];
1136 int g = src1[6*i + 2];
1137 int b = src1[6*i + 4];
1139 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1140 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1144 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1145 uint8_t *src1, uint8_t *src2, int width)
1149 for (i = 0; i < width; i++) {
1150 int r= src1[12*i + 0] + src1[12*i + 6];
1151 int g= src1[12*i + 2] + src1[12*i + 8];
1152 int b= src1[12*i + 4] + src1[12*i + 10];
1154 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1155 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1159 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1160 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1163 for (i=0; i<width; i++)\
1165 int b= (((const type*)src)[i]>>shb)&maskb;\
1166 int g= (((const type*)src)[i]>>shg)&maskg;\
1167 int r= (((const type*)src)[i]>>shr)&maskr;\
1169 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1173 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1174 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1175 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1176 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1177 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1178 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1180 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused){
1182 for (i=0; i<width; i++){
1187 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1188 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1191 for (i=0; i<width; i++)\
1193 int b= (((const type*)src)[i]&maskb)>>shb;\
1194 int g= (((const type*)src)[i]&maskg)>>shg;\
1195 int r= (((const type*)src)[i]&maskr)>>shr;\
1197 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1198 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1201 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1204 for (i=0; i<width; i++)\
1206 int pix0= ((const type*)src)[2*i+0];\
1207 int pix1= ((const type*)src)[2*i+1];\
1208 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1209 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1210 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1211 g&= maskg|(2*maskg);\
1215 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1216 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1220 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1221 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1222 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1223 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1224 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1225 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1227 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1230 for (i=0; i<width; i++)
1234 dst[i]= pal[d] & 0xFF;
1238 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1239 const uint8_t *src1, const uint8_t *src2,
1240 long width, uint32_t *pal)
1243 assert(src1 == src2);
1244 for (i=0; i<width; i++)
1246 int p= pal[src1[i]];
1253 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1256 for (i=0; i<width/8; i++){
1259 dst[8*i+j]= ((d>>(7-j))&1)*255;
1263 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1266 for (i=0; i<width/8; i++){
1269 dst[8*i+j]= ((d>>(7-j))&1)*255;
1274 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1276 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1281 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1282 #define COMPILE_ALTIVEC
1288 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1292 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1293 #define COMPILE_MMX2
1296 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1297 #define COMPILE_3DNOW
1301 #define COMPILE_TEMPLATE_MMX 0
1302 #define COMPILE_TEMPLATE_MMX2 0
1303 #define COMPILE_TEMPLATE_AMD3DNOW 0
1304 #define COMPILE_TEMPLATE_ALTIVEC 0
1307 #define RENAME(a) a ## _C
1308 #include "swscale_template.c"
1311 #ifdef COMPILE_ALTIVEC
1313 #undef COMPILE_TEMPLATE_ALTIVEC
1314 #define COMPILE_TEMPLATE_ALTIVEC 1
1315 #define RENAME(a) a ## _altivec
1316 #include "swscale_template.c"
1324 #undef COMPILE_TEMPLATE_MMX
1325 #undef COMPILE_TEMPLATE_MMX2
1326 #undef COMPILE_TEMPLATE_AMD3DNOW
1327 #define COMPILE_TEMPLATE_MMX 1
1328 #define COMPILE_TEMPLATE_MMX2 0
1329 #define COMPILE_TEMPLATE_AMD3DNOW 0
1330 #define RENAME(a) a ## _MMX
1331 #include "swscale_template.c"
1337 #undef COMPILE_TEMPLATE_MMX
1338 #undef COMPILE_TEMPLATE_MMX2
1339 #undef COMPILE_TEMPLATE_AMD3DNOW
1340 #define COMPILE_TEMPLATE_MMX 1
1341 #define COMPILE_TEMPLATE_MMX2 1
1342 #define COMPILE_TEMPLATE_AMD3DNOW 0
1343 #define RENAME(a) a ## _MMX2
1344 #include "swscale_template.c"
1348 #ifdef COMPILE_3DNOW
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 1
1356 #define RENAME(a) a ## _3DNow
1357 #include "swscale_template.c"
1362 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1364 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1365 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1366 else return getSplineCoeff( 0.0,
1373 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1374 int srcW, int dstW, int filterAlign, int one, int flags,
1375 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1381 int64_t *filter=NULL;
1382 int64_t *filter2=NULL;
1383 const int64_t fone= 1LL<<54;
1386 if (flags & SWS_CPU_CAPS_MMX)
1387 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1390 // NOTE: the +1 is for the MMX scaler which reads over the end
1391 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1393 if (FFABS(xInc - 0x10000) <10) // unscaled
1397 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1399 for (i=0; i<dstW; i++)
1401 filter[i*filterSize]= fone;
1406 else if (flags&SWS_POINT) // lame looking point sampling mode
1411 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1413 xDstInSrc= xInc/2 - 0x8000;
1414 for (i=0; i<dstW; i++)
1416 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1418 (*filterPos)[i]= xx;
1423 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1428 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1430 xDstInSrc= xInc/2 - 0x8000;
1431 for (i=0; i<dstW; i++)
1433 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1436 (*filterPos)[i]= xx;
1437 //bilinear upscale / linear interpolate / area averaging
1438 for (j=0; j<filterSize; j++)
1440 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1441 if (coeff<0) coeff=0;
1442 filter[i*filterSize + j]= coeff;
1453 if (flags&SWS_BICUBIC) sizeFactor= 4;
1454 else if (flags&SWS_X) sizeFactor= 8;
1455 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1456 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1457 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1458 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1459 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1460 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1462 sizeFactor= 0; //GCC warning killer
1466 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1467 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1469 if (filterSize > srcW-2) filterSize=srcW-2;
1471 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1473 xDstInSrc= xInc - 0x10000;
1474 for (i=0; i<dstW; i++)
1476 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1478 (*filterPos)[i]= xx;
1479 for (j=0; j<filterSize; j++)
1481 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1487 floatd= d * (1.0/(1<<30));
1489 if (flags & SWS_BICUBIC)
1491 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1492 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1493 int64_t dd = ( d*d)>>30;
1494 int64_t ddd= (dd*d)>>30;
1497 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1498 else if (d < 1LL<<31)
1499 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1502 coeff *= fone>>(30+24);
1504 /* else if (flags & SWS_X)
1506 double p= param ? param*0.01 : 0.3;
1507 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1508 coeff*= pow(2.0, - p*d*d);
1510 else if (flags & SWS_X)
1512 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1519 if (c<0.0) c= -pow(-c, A);
1521 coeff= (c*0.5 + 0.5)*fone;
1523 else if (flags & SWS_AREA)
1525 int64_t d2= d - (1<<29);
1526 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1527 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1529 coeff *= fone>>(30+16);
1531 else if (flags & SWS_GAUSS)
1533 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1534 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1536 else if (flags & SWS_SINC)
1538 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1540 else if (flags & SWS_LANCZOS)
1542 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1543 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1544 if (floatd>p) coeff=0;
1546 else if (flags & SWS_BILINEAR)
1549 if (coeff<0) coeff=0;
1550 coeff *= fone >> 30;
1552 else if (flags & SWS_SPLINE)
1554 double p=-2.196152422706632;
1555 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1558 coeff= 0.0; //GCC warning killer
1562 filter[i*filterSize + j]= coeff;
1569 /* apply src & dst Filter to filter -> filter2
1572 assert(filterSize>0);
1573 filter2Size= filterSize;
1574 if (srcFilter) filter2Size+= srcFilter->length - 1;
1575 if (dstFilter) filter2Size+= dstFilter->length - 1;
1576 assert(filter2Size>0);
1577 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1579 for (i=0; i<dstW; i++)
1584 for (k=0; k<srcFilter->length; k++){
1585 for (j=0; j<filterSize; j++)
1586 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1589 for (j=0; j<filterSize; j++)
1590 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1594 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1598 /* try to reduce the filter-size (step1 find size and shift left) */
1599 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1601 for (i=dstW-1; i>=0; i--)
1603 int min= filter2Size;
1607 /* get rid off near zero elements on the left by shifting left */
1608 for (j=0; j<filter2Size; j++)
1611 cutOff += FFABS(filter2[i*filter2Size]);
1613 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1615 /* preserve monotonicity because the core can't handle the filter otherwise */
1616 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1618 // move filter coefficients left
1619 for (k=1; k<filter2Size; k++)
1620 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1621 filter2[i*filter2Size + k - 1]= 0;
1626 /* count near zeros on the right */
1627 for (j=filter2Size-1; j>0; j--)
1629 cutOff += FFABS(filter2[i*filter2Size + j]);
1631 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1635 if (min>minFilterSize) minFilterSize= min;
1638 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1639 // we can handle the special case 4,
1640 // so we don't want to go to the full 8
1641 if (minFilterSize < 5)
1644 // We really don't want to waste our time
1645 // doing useless computation, so fall back on
1646 // the scalar C code for very small filters.
1647 // Vectorizing is worth it only if you have a
1648 // decent-sized vector.
1649 if (minFilterSize < 3)
1653 if (flags & SWS_CPU_CAPS_MMX) {
1654 // special case for unscaled vertical filtering
1655 if (minFilterSize == 1 && filterAlign == 2)
1659 assert(minFilterSize > 0);
1660 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1661 assert(filterSize > 0);
1662 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1663 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1665 *outFilterSize= filterSize;
1667 if (flags&SWS_PRINT_INFO)
1668 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1669 /* try to reduce the filter-size (step2 reduce it) */
1670 for (i=0; i<dstW; i++)
1674 for (j=0; j<filterSize; j++)
1676 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1677 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1678 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1679 filter[i*filterSize + j]= 0;
1684 //FIXME try to align filterPos if possible
1687 for (i=0; i<dstW; i++)
1690 if ((*filterPos)[i] < 0)
1692 // move filter coefficients left to compensate for filterPos
1693 for (j=1; j<filterSize; j++)
1695 int left= FFMAX(j + (*filterPos)[i], 0);
1696 filter[i*filterSize + left] += filter[i*filterSize + j];
1697 filter[i*filterSize + j]=0;
1702 if ((*filterPos)[i] + filterSize > srcW)
1704 int shift= (*filterPos)[i] + filterSize - srcW;
1705 // move filter coefficients right to compensate for filterPos
1706 for (j=filterSize-2; j>=0; j--)
1708 int right= FFMIN(j + shift, filterSize-1);
1709 filter[i*filterSize +right] += filter[i*filterSize +j];
1710 filter[i*filterSize +j]=0;
1712 (*filterPos)[i]= srcW - filterSize;
1716 // Note the +1 is for the MMX scaler which reads over the end
1717 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1718 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1720 /* normalize & store in outFilter */
1721 for (i=0; i<dstW; i++)
1727 for (j=0; j<filterSize; j++)
1729 sum+= filter[i*filterSize + j];
1731 sum= (sum + one/2)/ one;
1732 for (j=0; j<*outFilterSize; j++)
1734 int64_t v= filter[i*filterSize + j] + error;
1735 int intV= ROUNDED_DIV(v, sum);
1736 (*outFilter)[i*(*outFilterSize) + j]= intV;
1737 error= v - intV*sum;
1741 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1742 for (i=0; i<*outFilterSize; i++)
1744 int j= dstW*(*outFilterSize);
1745 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1756 static void initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1759 x86_reg imm8OfPShufW1A;
1760 x86_reg imm8OfPShufW2A;
1761 x86_reg fragmentLengthA;
1763 x86_reg imm8OfPShufW1B;
1764 x86_reg imm8OfPShufW2B;
1765 x86_reg fragmentLengthB;
1770 // create an optimized horizontal scaling routine
1778 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1779 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1780 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1781 "punpcklbw %%mm7, %%mm1 \n\t"
1782 "punpcklbw %%mm7, %%mm0 \n\t"
1783 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1785 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1787 "psubw %%mm1, %%mm0 \n\t"
1788 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1789 "pmullw %%mm3, %%mm0 \n\t"
1790 "psllw $7, %%mm1 \n\t"
1791 "paddw %%mm1, %%mm0 \n\t"
1793 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1795 "add $8, %%"REG_a" \n\t"
1799 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1800 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1801 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1806 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1810 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1811 "=r" (fragmentLengthA)
1818 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1819 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1820 "punpcklbw %%mm7, %%mm0 \n\t"
1821 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1823 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1825 "psubw %%mm1, %%mm0 \n\t"
1826 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1827 "pmullw %%mm3, %%mm0 \n\t"
1828 "psllw $7, %%mm1 \n\t"
1829 "paddw %%mm1, %%mm0 \n\t"
1831 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1833 "add $8, %%"REG_a" \n\t"
1837 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1838 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1839 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1844 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1848 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1849 "=r" (fragmentLengthB)
1852 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1855 for (i=0; i<dstW/numSplits; i++)
1862 int b=((xpos+xInc)>>16) - xx;
1863 int c=((xpos+xInc*2)>>16) - xx;
1864 int d=((xpos+xInc*3)>>16) - xx;
1866 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
1867 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
1868 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
1869 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1870 int maxShift= 3-(d+inc);
1873 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1874 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1875 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1876 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1879 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1881 filterCode[fragmentPos + imm8OfPShufW1]=
1882 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1883 filterCode[fragmentPos + imm8OfPShufW2]=
1884 a | (b<<2) | (c<<4) | (d<<6);
1886 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1887 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1889 if (shift && i>=shift)
1891 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1892 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1893 filterPos[i/2]-=shift;
1896 fragmentPos+= fragmentLength;
1898 filterCode[fragmentPos]= RET;
1902 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1904 #endif /* COMPILE_MMX2 */
1906 static void globalInit(void){
1907 // generating tables:
1909 for (i=0; i<768; i++){
1910 int c= av_clip_uint8(i-256);
1915 static SwsFunc getSwsFunc(SwsContext *c)
1917 #if CONFIG_RUNTIME_CPUDETECT
1918 int flags = c->flags;
1920 #if ARCH_X86 && CONFIG_GPL
1921 // ordered per speed fastest first
1922 if (flags & SWS_CPU_CAPS_MMX2) {
1923 sws_init_swScale_MMX2(c);
1924 return swScale_MMX2;
1925 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1926 sws_init_swScale_3DNow(c);
1927 return swScale_3DNow;
1928 } else if (flags & SWS_CPU_CAPS_MMX) {
1929 sws_init_swScale_MMX(c);
1932 sws_init_swScale_C(c);
1938 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1939 sws_init_swScale_altivec(c);
1940 return swScale_altivec;
1942 sws_init_swScale_C(c);
1946 sws_init_swScale_C(c);
1948 #endif /* ARCH_X86 && CONFIG_GPL */
1949 #else //CONFIG_RUNTIME_CPUDETECT
1950 #if COMPILE_TEMPLATE_MMX2
1951 sws_init_swScale_MMX2(c);
1952 return swScale_MMX2;
1953 #elif COMPILE_TEMPLATE_AMD3DNOW
1954 sws_init_swScale_3DNow(c);
1955 return swScale_3DNow;
1956 #elif COMPILE_TEMPLATE_MMX
1957 sws_init_swScale_MMX(c);
1959 #elif COMPILE_TEMPLATE_ALTIVEC
1960 sws_init_swScale_altivec(c);
1961 return swScale_altivec;
1963 sws_init_swScale_C(c);
1966 #endif //!CONFIG_RUNTIME_CPUDETECT
1969 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1970 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1971 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1973 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1974 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1978 const uint8_t *srcPtr= src[0];
1979 uint8_t *dstPtr= dst;
1980 for (i=0; i<srcSliceH; i++)
1982 memcpy(dstPtr, srcPtr, c->srcW);
1983 srcPtr+= srcStride[0];
1984 dstPtr+= dstStride[0];
1987 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1988 if (c->dstFormat == PIX_FMT_NV12)
1989 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1991 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1996 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1997 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1998 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2000 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2005 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2006 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2007 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2009 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2014 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2015 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2016 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2018 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2023 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2024 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2025 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2027 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2032 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2033 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2034 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2035 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2036 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2038 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2041 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2046 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2047 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2048 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2049 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2050 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2052 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2057 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2058 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2059 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2060 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2061 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2063 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2066 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2071 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2072 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
2073 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2074 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2075 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2077 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2082 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2083 int srcSliceH, uint8_t* dst[], int dstStride[]){
2084 const enum PixelFormat srcFormat= c->srcFormat;
2085 const enum PixelFormat dstFormat= c->dstFormat;
2086 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2087 const uint8_t *palette)=NULL;
2089 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2090 uint8_t *srcPtr= src[0];
2092 if (!usePal(srcFormat))
2093 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2094 sws_format_name(srcFormat), sws_format_name(dstFormat));
2097 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2098 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2099 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2100 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2101 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2102 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2103 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2104 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2108 for (i=0; i<srcSliceH; i++) {
2109 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2110 srcPtr+= srcStride[0];
2111 dstPtr+= dstStride[0];
2117 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2118 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2119 int srcSliceH, uint8_t* dst[], int dstStride[]){
2120 const enum PixelFormat srcFormat= c->srcFormat;
2121 const enum PixelFormat dstFormat= c->dstFormat;
2122 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2123 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2124 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2125 const int dstId= fmt_depth(dstFormat) >> 2;
2126 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2129 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2130 || (isRGB(srcFormat) && isRGB(dstFormat))){
2131 switch(srcId | (dstId<<4)){
2132 case 0x34: conv= rgb16to15; break;
2133 case 0x36: conv= rgb24to15; break;
2134 case 0x38: conv= rgb32to15; break;
2135 case 0x43: conv= rgb15to16; break;
2136 case 0x46: conv= rgb24to16; break;
2137 case 0x48: conv= rgb32to16; break;
2138 case 0x63: conv= rgb15to24; break;
2139 case 0x64: conv= rgb16to24; break;
2140 case 0x68: conv= rgb32to24; break;
2141 case 0x83: conv= rgb15to32; break;
2142 case 0x84: conv= rgb16to32; break;
2143 case 0x86: conv= rgb24to32; break;
2144 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2145 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2147 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2148 || (isRGB(srcFormat) && isBGR(dstFormat))){
2149 switch(srcId | (dstId<<4)){
2150 case 0x33: conv= rgb15tobgr15; break;
2151 case 0x34: conv= rgb16tobgr15; break;
2152 case 0x36: conv= rgb24tobgr15; break;
2153 case 0x38: conv= rgb32tobgr15; break;
2154 case 0x43: conv= rgb15tobgr16; break;
2155 case 0x44: conv= rgb16tobgr16; break;
2156 case 0x46: conv= rgb24tobgr16; break;
2157 case 0x48: conv= rgb32tobgr16; break;
2158 case 0x63: conv= rgb15tobgr24; break;
2159 case 0x64: conv= rgb16tobgr24; break;
2160 case 0x66: conv= rgb24tobgr24; break;
2161 case 0x68: conv= rgb32tobgr24; break;
2162 case 0x83: conv= rgb15tobgr32; break;
2163 case 0x84: conv= rgb16tobgr32; break;
2164 case 0x86: conv= rgb24tobgr32; break;
2165 case 0x88: conv= rgb32tobgr32; break;
2166 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2167 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2170 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2171 sws_format_name(srcFormat), sws_format_name(dstFormat));
2176 uint8_t *srcPtr= src[0];
2177 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2178 srcPtr += ALT32_CORR;
2180 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2181 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2185 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2187 for (i=0; i<srcSliceH; i++)
2189 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2190 srcPtr+= srcStride[0];
2191 dstPtr+= dstStride[0];
2198 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2199 int srcSliceH, uint8_t* dst[], int dstStride[]){
2203 dst[0]+ srcSliceY *dstStride[0],
2204 dst[1]+(srcSliceY>>1)*dstStride[1],
2205 dst[2]+(srcSliceY>>1)*dstStride[2],
2207 dstStride[0], dstStride[1], srcStride[0]);
2209 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2213 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2214 int srcSliceH, uint8_t* dst[], int dstStride[]){
2218 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2219 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2221 uint8_t *srcPtr= src[0];
2222 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2224 for (i=0; i<srcSliceH; i++)
2226 memcpy(dstPtr, srcPtr, c->srcW);
2227 srcPtr+= srcStride[0];
2228 dstPtr+= dstStride[0];
2232 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2233 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2234 srcSliceH >> 2, srcStride[1], dstStride[1]);
2235 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2236 srcSliceH >> 2, srcStride[2], dstStride[2]);
2238 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2239 srcSliceH >> 2, srcStride[1], dstStride[2]);
2240 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2241 srcSliceH >> 2, srcStride[2], dstStride[1]);
2244 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2248 /* unscaled copy like stuff (assumes nearly identical formats) */
2249 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2250 int srcSliceH, uint8_t* dst[], int dstStride[])
2252 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2253 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2257 uint8_t *srcPtr= src[0];
2258 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2261 /* universal length finder */
2262 while(length+c->srcW <= FFABS(dstStride[0])
2263 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2266 for (i=0; i<srcSliceH; i++)
2268 memcpy(dstPtr, srcPtr, length);
2269 srcPtr+= srcStride[0];
2270 dstPtr+= dstStride[0];
2276 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2277 int srcSliceH, uint8_t* dst[], int dstStride[])
2280 for (plane=0; plane<4; plane++)
2282 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2283 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2284 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2285 uint8_t *srcPtr= src[plane];
2286 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2288 if (!dst[plane]) continue;
2289 // ignore palette for GRAY8
2290 if (plane == 1 && !dst[2]) continue;
2291 if (!src[plane] || (plane == 1 && !src[2])){
2292 if(is16BPS(c->dstFormat))
2294 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2297 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2298 if (!isBE(c->srcFormat)) srcPtr++;
2299 for (i=0; i<height; i++){
2300 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2301 srcPtr+= srcStride[plane];
2302 dstPtr+= dstStride[plane];
2304 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2305 for (i=0; i<height; i++){
2306 for (j=0; j<length; j++){
2307 dstPtr[ j<<1 ] = srcPtr[j];
2308 dstPtr[(j<<1)+1] = srcPtr[j];
2310 srcPtr+= srcStride[plane];
2311 dstPtr+= dstStride[plane];
2313 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2314 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2316 for (i=0; i<height; i++){
2317 for (j=0; j<length; j++)
2318 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2319 srcPtr+= srcStride[plane];
2320 dstPtr+= dstStride[plane];
2322 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2323 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2326 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2328 for (i=0; i<height; i++)
2330 memcpy(dstPtr, srcPtr, length);
2331 srcPtr+= srcStride[plane];
2332 dstPtr+= dstStride[plane];
2341 static void getSubSampleFactors(int *h, int *v, int format){
2343 case PIX_FMT_UYVY422:
2344 case PIX_FMT_YUYV422:
2348 case PIX_FMT_YUV420P:
2349 case PIX_FMT_YUV420PLE:
2350 case PIX_FMT_YUV420PBE:
2351 case PIX_FMT_YUVA420P:
2352 case PIX_FMT_GRAY16BE:
2353 case PIX_FMT_GRAY16LE:
2354 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2360 case PIX_FMT_YUV440P:
2364 case PIX_FMT_YUV410P:
2368 case PIX_FMT_YUV444P:
2369 case PIX_FMT_YUV444PLE:
2370 case PIX_FMT_YUV444PBE:
2374 case PIX_FMT_YUV422P:
2375 case PIX_FMT_YUV422PLE:
2376 case PIX_FMT_YUV422PBE:
2380 case PIX_FMT_YUV411P:
2391 static uint16_t roundToInt16(int64_t f){
2392 int r= (f + (1<<15))>>16;
2393 if (r<-0x7FFF) return 0x8000;
2394 else if (r> 0x7FFF) return 0x7FFF;
2398 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2399 int64_t crv = inv_table[0];
2400 int64_t cbu = inv_table[1];
2401 int64_t cgu = -inv_table[2];
2402 int64_t cgv = -inv_table[3];
2406 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2407 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2409 c->brightness= brightness;
2410 c->contrast = contrast;
2411 c->saturation= saturation;
2412 c->srcRange = srcRange;
2413 c->dstRange = dstRange;
2414 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2416 c->uOffset= 0x0400040004000400LL;
2417 c->vOffset= 0x0400040004000400LL;
2423 crv= (crv*224) / 255;
2424 cbu= (cbu*224) / 255;
2425 cgu= (cgu*224) / 255;
2426 cgv= (cgv*224) / 255;
2429 cy = (cy *contrast )>>16;
2430 crv= (crv*contrast * saturation)>>32;
2431 cbu= (cbu*contrast * saturation)>>32;
2432 cgu= (cgu*contrast * saturation)>>32;
2433 cgv= (cgv*contrast * saturation)>>32;
2435 oy -= 256*brightness;
2437 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2438 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2439 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2440 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2441 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2442 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2444 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2445 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2446 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2447 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2448 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2449 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2451 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2454 #ifdef COMPILE_ALTIVEC
2455 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2456 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2461 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2462 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2464 *inv_table = c->srcColorspaceTable;
2465 *table = c->dstColorspaceTable;
2466 *srcRange = c->srcRange;
2467 *dstRange = c->dstRange;
2468 *brightness= c->brightness;
2469 *contrast = c->contrast;
2470 *saturation= c->saturation;
2475 static int handle_jpeg(enum PixelFormat *format)
2478 case PIX_FMT_YUVJ420P:
2479 *format = PIX_FMT_YUV420P;
2481 case PIX_FMT_YUVJ422P:
2482 *format = PIX_FMT_YUV422P;
2484 case PIX_FMT_YUVJ444P:
2485 *format = PIX_FMT_YUV444P;
2487 case PIX_FMT_YUVJ440P:
2488 *format = PIX_FMT_YUV440P;
2495 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2496 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2501 int usesVFilter, usesHFilter;
2502 int unscaled, needsDither;
2503 int srcRange, dstRange;
2504 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2506 if (flags & SWS_CPU_CAPS_MMX)
2507 __asm__ volatile("emms\n\t"::: "memory");
2510 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2511 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2512 #if COMPILE_TEMPLATE_MMX2
2513 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2514 #elif COMPILE_TEMPLATE_AMD3DNOW
2515 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2516 #elif COMPILE_TEMPLATE_MMX
2517 flags |= SWS_CPU_CAPS_MMX;
2518 #elif COMPILE_TEMPLATE_ALTIVEC
2519 flags |= SWS_CPU_CAPS_ALTIVEC;
2521 flags |= SWS_CPU_CAPS_BFIN;
2523 #endif /* CONFIG_RUNTIME_CPUDETECT */
2524 if (clip_table[512] != 255) globalInit();
2525 if (!rgb15to16) sws_rgb2rgb_init(flags);
2527 unscaled = (srcW == dstW && srcH == dstH);
2528 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2529 && (fmt_depth(dstFormat))<24
2530 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2532 srcRange = handle_jpeg(&srcFormat);
2533 dstRange = handle_jpeg(&dstFormat);
2535 if (!isSupportedIn(srcFormat))
2537 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2540 if (!isSupportedOut(dstFormat))
2542 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2546 i= flags & ( SWS_POINT
2557 if(!i || (i & (i-1)))
2559 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2564 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
2566 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2567 srcW, srcH, dstW, dstH);
2570 if(srcW > VOFW || dstW > VOFW){
2571 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2575 if (!dstFilter) dstFilter= &dummyFilter;
2576 if (!srcFilter) srcFilter= &dummyFilter;
2578 c= av_mallocz(sizeof(SwsContext));
2580 c->av_class = &sws_context_class;
2585 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2586 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2588 c->dstFormat= dstFormat;
2589 c->srcFormat= srcFormat;
2590 c->vRounder= 4* 0x0001000100010001ULL;
2592 usesHFilter= usesVFilter= 0;
2593 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2594 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2595 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2596 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2597 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2598 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2599 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2600 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2602 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2603 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2605 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2606 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2608 // drop some chroma lines if the user wants it
2609 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2610 c->chrSrcVSubSample+= c->vChrDrop;
2612 // drop every other pixel for chroma calculation unless user wants full chroma
2613 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2614 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2615 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2616 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2617 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2618 c->chrSrcHSubSample=1;
2621 c->param[0] = param[0];
2622 c->param[1] = param[1];
2625 c->param[1] = SWS_PARAM_DEFAULT;
2628 // Note the -((-x)>>y) is so that we always round toward +inf.
2629 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2630 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2631 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2632 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2634 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2636 /* unscaled special cases */
2637 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2640 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2642 c->swScale= PlanarToNV12Wrapper;
2645 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2646 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2648 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2651 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2653 c->swScale= yvu9toyv12Wrapper;
2657 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2658 c->swScale= bgr24toyv12Wrapper;
2660 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2661 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2662 && (isBGR(dstFormat) || isRGB(dstFormat))
2663 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2664 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2665 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2666 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2667 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2668 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2669 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2670 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2671 && dstFormat != PIX_FMT_RGB32_1
2672 && dstFormat != PIX_FMT_BGR32_1
2673 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2674 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2675 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2676 c->swScale= rgb2rgbWrapper;
2678 if ((usePal(srcFormat) && (
2679 dstFormat == PIX_FMT_RGB32 ||
2680 dstFormat == PIX_FMT_RGB32_1 ||
2681 dstFormat == PIX_FMT_RGB24 ||
2682 dstFormat == PIX_FMT_BGR32 ||
2683 dstFormat == PIX_FMT_BGR32_1 ||
2684 dstFormat == PIX_FMT_BGR24)))
2685 c->swScale= pal2rgbWrapper;
2687 if (srcFormat == PIX_FMT_YUV422P)
2689 if (dstFormat == PIX_FMT_YUYV422)
2690 c->swScale= YUV422PToYuy2Wrapper;
2691 else if (dstFormat == PIX_FMT_UYVY422)
2692 c->swScale= YUV422PToUyvyWrapper;
2695 /* LQ converters if -sws 0 or -sws 4*/
2696 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2698 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2700 if (dstFormat == PIX_FMT_YUYV422)
2701 c->swScale= PlanarToYuy2Wrapper;
2702 else if (dstFormat == PIX_FMT_UYVY422)
2703 c->swScale= PlanarToUyvyWrapper;
2706 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2707 c->swScale= YUYV2YUV420Wrapper;
2708 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2709 c->swScale= UYVY2YUV420Wrapper;
2710 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2711 c->swScale= YUYV2YUV422Wrapper;
2712 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2713 c->swScale= UYVY2YUV422Wrapper;
2715 #ifdef COMPILE_ALTIVEC
2716 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2717 !(c->flags & SWS_BITEXACT) &&
2718 srcFormat == PIX_FMT_YUV420P) {
2719 // unscaled YV12 -> packed YUV, we want speed
2720 if (dstFormat == PIX_FMT_YUYV422)
2721 c->swScale= yv12toyuy2_unscaled_altivec;
2722 else if (dstFormat == PIX_FMT_UYVY422)
2723 c->swScale= yv12touyvy_unscaled_altivec;
2728 if ( srcFormat == dstFormat
2729 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2730 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2731 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2732 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2733 || (isGray(dstFormat) && isGray(srcFormat))
2734 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2735 && c->chrDstHSubSample == c->chrSrcHSubSample
2736 && c->chrDstVSubSample == c->chrSrcVSubSample
2737 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2738 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2740 if (isPacked(c->srcFormat))
2741 c->swScale= packedCopy;
2742 else /* Planar YUV or gray */
2743 c->swScale= planarCopy;
2746 if (flags & SWS_CPU_CAPS_BFIN)
2747 ff_bfin_get_unscaled_swscale (c);
2751 if (flags&SWS_PRINT_INFO)
2752 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2753 sws_format_name(srcFormat), sws_format_name(dstFormat));
2758 if (flags & SWS_CPU_CAPS_MMX2)
2760 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2761 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2763 if (flags&SWS_PRINT_INFO)
2764 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2766 if (usesHFilter) c->canMMX2BeUsed=0;
2771 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2772 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2774 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2775 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2776 // n-2 is the last chrominance sample available
2777 // this is not perfect, but no one should notice the difference, the more correct variant
2778 // would be like the vertical one, but that would require some special code for the
2779 // first and last pixel
2780 if (flags&SWS_FAST_BILINEAR)
2782 if (c->canMMX2BeUsed)
2787 //we don't use the x86 asm scaler if MMX is available
2788 else if (flags & SWS_CPU_CAPS_MMX)
2790 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2791 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2795 /* precalculate horizontal scaler filter coefficients */
2797 const int filterAlign=
2798 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2799 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2802 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2803 srcW , dstW, filterAlign, 1<<14,
2804 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2805 srcFilter->lumH, dstFilter->lumH, c->param);
2806 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2807 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2808 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2809 srcFilter->chrH, dstFilter->chrH, c->param);
2811 #define MAX_MMX2_FILTER_CODE_SIZE 10000
2812 #if defined(COMPILE_MMX2)
2813 // can't downscale !!!
2814 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2816 #ifdef MAP_ANONYMOUS
2817 c->lumMmx2FilterCode = mmap(NULL, MAX_MMX2_FILTER_CODE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2818 c->chrMmx2FilterCode = mmap(NULL, MAX_MMX2_FILTER_CODE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2819 #elif HAVE_VIRTUALALLOC
2820 c->lumMmx2FilterCode = VirtualAlloc(NULL, MAX_MMX2_FILTER_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2821 c->chrMmx2FilterCode = VirtualAlloc(NULL, MAX_MMX2_FILTER_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2823 c->lumMmx2FilterCode = av_malloc(MAX_MMX2_FILTER_CODE_SIZE);
2824 c->chrMmx2FilterCode = av_malloc(MAX_MMX2_FILTER_CODE_SIZE);
2827 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2828 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2829 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2830 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2832 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2833 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2835 #ifdef MAP_ANONYMOUS
2836 mprotect(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, PROT_EXEC | PROT_READ);
2837 mprotect(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, PROT_EXEC | PROT_READ);
2840 #endif /* defined(COMPILE_MMX2) */
2841 } // initialize horizontal stuff
2845 /* precalculate vertical scaler filter coefficients */
2847 const int filterAlign=
2848 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2849 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2852 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2853 srcH , dstH, filterAlign, (1<<12),
2854 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2855 srcFilter->lumV, dstFilter->lumV, c->param);
2856 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2857 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2858 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2859 srcFilter->chrV, dstFilter->chrV, c->param);
2861 #ifdef COMPILE_ALTIVEC
2862 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2863 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2865 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2867 short *p = (short *)&c->vYCoeffsBank[i];
2869 p[j] = c->vLumFilter[i];
2872 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2874 short *p = (short *)&c->vCCoeffsBank[i];
2876 p[j] = c->vChrFilter[i];
2881 // calculate buffer sizes so that they won't run out while handling these damn slices
2882 c->vLumBufSize= c->vLumFilterSize;
2883 c->vChrBufSize= c->vChrFilterSize;
2884 for (i=0; i<dstH; i++)
2886 int chrI= i*c->chrDstH / dstH;
2887 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2888 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2890 nextSlice>>= c->chrSrcVSubSample;
2891 nextSlice<<= c->chrSrcVSubSample;
2892 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2893 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2894 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2895 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2898 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2899 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2900 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2901 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2902 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2903 //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)
2904 /* align at 16 bytes for AltiVec */
2905 for (i=0; i<c->vLumBufSize; i++)
2906 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2907 for (i=0; i<c->vChrBufSize; i++)
2908 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2909 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2910 for (i=0; i<c->vLumBufSize; i++)
2911 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2913 //try to avoid drawing green stuff between the right end and the stride end
2914 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2916 assert(2*VOFW == VOF);
2918 assert(c->chrDstH <= dstH);
2920 if (flags&SWS_PRINT_INFO)
2923 const char *dither= " dithered";
2925 const char *dither= "";
2927 if (flags&SWS_FAST_BILINEAR)
2928 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2929 else if (flags&SWS_BILINEAR)
2930 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2931 else if (flags&SWS_BICUBIC)
2932 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2933 else if (flags&SWS_X)
2934 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2935 else if (flags&SWS_POINT)
2936 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2937 else if (flags&SWS_AREA)
2938 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2939 else if (flags&SWS_BICUBLIN)
2940 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2941 else if (flags&SWS_GAUSS)
2942 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2943 else if (flags&SWS_SINC)
2944 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2945 else if (flags&SWS_LANCZOS)
2946 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2947 else if (flags&SWS_SPLINE)
2948 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2950 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2952 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2953 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2954 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2956 av_log(c, AV_LOG_INFO, "from %s to %s ",
2957 sws_format_name(srcFormat), sws_format_name(dstFormat));
2959 if (flags & SWS_CPU_CAPS_MMX2)
2960 av_log(c, AV_LOG_INFO, "using MMX2\n");
2961 else if (flags & SWS_CPU_CAPS_3DNOW)
2962 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2963 else if (flags & SWS_CPU_CAPS_MMX)
2964 av_log(c, AV_LOG_INFO, "using MMX\n");
2965 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2966 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2968 av_log(c, AV_LOG_INFO, "using C\n");
2971 if (flags & SWS_PRINT_INFO)
2973 if (flags & SWS_CPU_CAPS_MMX)
2975 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2976 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2979 if (c->hLumFilterSize==4)
2980 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2981 else if (c->hLumFilterSize==8)
2982 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2984 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2986 if (c->hChrFilterSize==4)
2987 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2988 else if (c->hChrFilterSize==8)
2989 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2991 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2997 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2999 if (flags & SWS_FAST_BILINEAR)
3000 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3002 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3005 if (isPlanarYUV(dstFormat))
3007 if (c->vLumFilterSize==1)
3008 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3010 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3014 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3015 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3016 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3017 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3018 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3020 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3023 if (dstFormat==PIX_FMT_BGR24)
3024 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3025 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3026 else if (dstFormat==PIX_FMT_RGB32)
3027 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3028 else if (dstFormat==PIX_FMT_BGR565)
3029 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3030 else if (dstFormat==PIX_FMT_BGR555)
3031 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3033 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3035 if (flags & SWS_PRINT_INFO)
3037 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3038 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3039 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3040 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3043 c->swScale= getSwsFunc(c);
3047 static void reset_ptr(uint8_t* src[], int format){
3048 if(!isALPHA(format))
3050 if(!isPlanarYUV(format)){
3052 if( format != PIX_FMT_PAL8
3053 && format != PIX_FMT_RGB8
3054 && format != PIX_FMT_BGR8
3055 && format != PIX_FMT_RGB4_BYTE
3056 && format != PIX_FMT_BGR4_BYTE
3063 * swscale wrapper, so we don't need to export the SwsContext.
3064 * Assumes planar YUV to be in YUV order instead of YVU.
3066 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3067 int srcSliceH, uint8_t* dst[], int dstStride[]){
3069 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3070 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3072 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3073 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3076 if (c->sliceDir == 0) {
3077 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3080 if (usePal(c->srcFormat)){
3081 for (i=0; i<256; i++){
3082 int p, r, g, b,y,u,v;
3083 if(c->srcFormat == PIX_FMT_PAL8){
3084 p=((uint32_t*)(src[1]))[i];
3088 }else if(c->srcFormat == PIX_FMT_RGB8){
3092 }else if(c->srcFormat == PIX_FMT_BGR8){
3096 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
3101 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3106 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3107 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3108 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3109 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3112 switch(c->dstFormat) {
3117 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3119 case PIX_FMT_BGR32_1:
3123 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3125 case PIX_FMT_RGB32_1:
3129 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3136 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3141 // copy strides, so they can safely be modified
3142 if (c->sliceDir == 1) {
3143 // slices go from top to bottom
3144 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3145 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3147 reset_ptr(src2, c->srcFormat);
3148 reset_ptr(dst2, c->dstFormat);
3150 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3152 // slices go from bottom to top => we flip the image internally
3153 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3154 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3156 src2[0] += (srcSliceH-1)*srcStride[0];
3157 if (!usePal(c->srcFormat))
3158 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3159 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3160 src2[3] += (srcSliceH-1)*srcStride[3];
3161 dst2[0] += ( c->dstH -1)*dstStride[0];
3162 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3163 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3164 dst2[3] += ( c->dstH -1)*dstStride[3];
3166 reset_ptr(src2, c->srcFormat);
3167 reset_ptr(dst2, c->dstFormat);
3169 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3173 #if LIBSWSCALE_VERSION_MAJOR < 1
3174 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3175 int srcSliceH, uint8_t* dst[], int dstStride[]){
3176 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3180 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3181 float lumaSharpen, float chromaSharpen,
3182 float chromaHShift, float chromaVShift,
3185 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3187 if (lumaGBlur!=0.0){
3188 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3189 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3191 filter->lumH= sws_getIdentityVec();
3192 filter->lumV= sws_getIdentityVec();
3195 if (chromaGBlur!=0.0){
3196 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3197 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3199 filter->chrH= sws_getIdentityVec();
3200 filter->chrV= sws_getIdentityVec();
3203 if (chromaSharpen!=0.0){
3204 SwsVector *id= sws_getIdentityVec();
3205 sws_scaleVec(filter->chrH, -chromaSharpen);
3206 sws_scaleVec(filter->chrV, -chromaSharpen);
3207 sws_addVec(filter->chrH, id);
3208 sws_addVec(filter->chrV, id);
3212 if (lumaSharpen!=0.0){
3213 SwsVector *id= sws_getIdentityVec();
3214 sws_scaleVec(filter->lumH, -lumaSharpen);
3215 sws_scaleVec(filter->lumV, -lumaSharpen);
3216 sws_addVec(filter->lumH, id);
3217 sws_addVec(filter->lumV, id);
3221 if (chromaHShift != 0.0)
3222 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3224 if (chromaVShift != 0.0)
3225 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3227 sws_normalizeVec(filter->chrH, 1.0);
3228 sws_normalizeVec(filter->chrV, 1.0);
3229 sws_normalizeVec(filter->lumH, 1.0);
3230 sws_normalizeVec(filter->lumV, 1.0);
3232 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3233 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3238 SwsVector *sws_getGaussianVec(double variance, double quality){
3239 const int length= (int)(variance*quality + 0.5) | 1;
3241 double *coeff= av_malloc(length*sizeof(double));
3242 double middle= (length-1)*0.5;
3243 SwsVector *vec= av_malloc(sizeof(SwsVector));
3246 vec->length= length;
3248 for (i=0; i<length; i++)
3250 double dist= i-middle;
3251 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3254 sws_normalizeVec(vec, 1.0);
3259 SwsVector *sws_getConstVec(double c, int length){
3261 double *coeff= av_malloc(length*sizeof(double));
3262 SwsVector *vec= av_malloc(sizeof(SwsVector));
3265 vec->length= length;
3267 for (i=0; i<length; i++)
3274 SwsVector *sws_getIdentityVec(void){
3275 return sws_getConstVec(1.0, 1);
3278 double sws_dcVec(SwsVector *a){
3282 for (i=0; i<a->length; i++)
3288 void sws_scaleVec(SwsVector *a, double scalar){
3291 for (i=0; i<a->length; i++)
3292 a->coeff[i]*= scalar;
3295 void sws_normalizeVec(SwsVector *a, double height){
3296 sws_scaleVec(a, height/sws_dcVec(a));
3299 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3300 int length= a->length + b->length - 1;
3301 double *coeff= av_malloc(length*sizeof(double));
3303 SwsVector *vec= av_malloc(sizeof(SwsVector));
3306 vec->length= length;
3308 for (i=0; i<length; i++) coeff[i]= 0.0;
3310 for (i=0; i<a->length; i++)
3312 for (j=0; j<b->length; j++)
3314 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3321 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3322 int length= FFMAX(a->length, b->length);
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++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3333 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3338 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3339 int length= FFMAX(a->length, b->length);
3340 double *coeff= av_malloc(length*sizeof(double));
3342 SwsVector *vec= av_malloc(sizeof(SwsVector));
3345 vec->length= length;
3347 for (i=0; i<length; i++) coeff[i]= 0.0;
3349 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3350 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3355 /* shift left / or right if "shift" is negative */
3356 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3357 int length= a->length + FFABS(shift)*2;
3358 double *coeff= av_malloc(length*sizeof(double));
3360 SwsVector *vec= av_malloc(sizeof(SwsVector));
3363 vec->length= length;
3365 for (i=0; i<length; i++) coeff[i]= 0.0;
3367 for (i=0; i<a->length; i++)
3369 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3375 void sws_shiftVec(SwsVector *a, int shift){
3376 SwsVector *shifted= sws_getShiftedVec(a, shift);
3378 a->coeff= shifted->coeff;
3379 a->length= shifted->length;
3383 void sws_addVec(SwsVector *a, SwsVector *b){
3384 SwsVector *sum= sws_sumVec(a, b);
3386 a->coeff= sum->coeff;
3387 a->length= sum->length;
3391 void sws_subVec(SwsVector *a, SwsVector *b){
3392 SwsVector *diff= sws_diffVec(a, b);
3394 a->coeff= diff->coeff;
3395 a->length= diff->length;
3399 void sws_convVec(SwsVector *a, SwsVector *b){
3400 SwsVector *conv= sws_getConvVec(a, b);
3402 a->coeff= conv->coeff;
3403 a->length= conv->length;
3407 SwsVector *sws_cloneVec(SwsVector *a){
3408 double *coeff= av_malloc(a->length*sizeof(double));
3410 SwsVector *vec= av_malloc(sizeof(SwsVector));
3413 vec->length= a->length;
3415 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3420 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3426 for (i=0; i<a->length; i++)
3427 if (a->coeff[i]>max) max= a->coeff[i];
3429 for (i=0; i<a->length; i++)
3430 if (a->coeff[i]<min) min= a->coeff[i];
3434 for (i=0; i<a->length; i++)
3436 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3437 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3438 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3439 av_log(log_ctx, log_level, "|\n");
3443 #if LIBSWSCALE_VERSION_MAJOR < 1
3444 void sws_printVec(SwsVector *a){
3445 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3449 void sws_freeVec(SwsVector *a){
3451 av_freep(&a->coeff);
3456 void sws_freeFilter(SwsFilter *filter){
3457 if (!filter) return;
3459 if (filter->lumH) sws_freeVec(filter->lumH);
3460 if (filter->lumV) sws_freeVec(filter->lumV);
3461 if (filter->chrH) sws_freeVec(filter->chrH);
3462 if (filter->chrV) sws_freeVec(filter->chrV);
3467 void sws_freeContext(SwsContext *c){
3473 for (i=0; i<c->vLumBufSize; i++)
3474 av_freep(&c->lumPixBuf[i]);
3475 av_freep(&c->lumPixBuf);
3480 for (i=0; i<c->vChrBufSize; i++)
3481 av_freep(&c->chrPixBuf[i]);
3482 av_freep(&c->chrPixBuf);
3485 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3486 for (i=0; i<c->vLumBufSize; i++)
3487 av_freep(&c->alpPixBuf[i]);
3488 av_freep(&c->alpPixBuf);
3491 av_freep(&c->vLumFilter);
3492 av_freep(&c->vChrFilter);
3493 av_freep(&c->hLumFilter);
3494 av_freep(&c->hChrFilter);
3495 #ifdef COMPILE_ALTIVEC
3496 av_freep(&c->vYCoeffsBank);
3497 av_freep(&c->vCCoeffsBank);
3500 av_freep(&c->vLumFilterPos);
3501 av_freep(&c->vChrFilterPos);
3502 av_freep(&c->hLumFilterPos);
3503 av_freep(&c->hChrFilterPos);
3505 #if ARCH_X86 && CONFIG_GPL
3506 #ifdef MAP_ANONYMOUS
3507 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE);
3508 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE);
3509 #elif HAVE_VIRTUALALLOC
3510 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, MEM_RELEASE);
3511 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, MAX_MMX2_FILTER_CODE_SIZE, MEM_RELEASE);
3513 av_free(c->lumMmx2FilterCode);
3514 av_free(c->chrMmx2FilterCode);
3516 c->lumMmx2FilterCode=NULL;
3517 c->chrMmx2FilterCode=NULL;
3518 #endif /* ARCH_X86 && CONFIG_GPL */
3520 av_freep(&c->lumMmx2Filter);
3521 av_freep(&c->chrMmx2Filter);
3522 av_freep(&c->lumMmx2FilterPos);
3523 av_freep(&c->chrMmx2FilterPos);
3524 av_freep(&c->yuvTable);
3529 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3530 int srcW, int srcH, enum PixelFormat srcFormat,
3531 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3532 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3534 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3537 param = default_param;
3540 if (context->srcW != srcW || context->srcH != srcH ||
3541 context->srcFormat != srcFormat ||
3542 context->dstW != dstW || context->dstH != dstH ||
3543 context->dstFormat != dstFormat || context->flags != flags ||
3544 context->param[0] != param[0] || context->param[1] != param[1])
3546 sws_freeContext(context);
3551 return sws_getContext(srcW, srcH, srcFormat,
3552 dstW, dstH, dstFormat, flags,
3553 srcFilter, dstFilter, param);