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/intreadwrite.h"
78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/avutil.h"
80 #include "libavutil/bswap.h"
81 #include "libavutil/pixdesc.h"
83 unsigned swscale_version(void)
85 return LIBSWSCALE_VERSION_INT;
88 const char *swscale_configuration(void)
90 return FFMPEG_CONFIGURATION;
93 const char *swscale_license(void)
95 #define LICENSE_PREFIX "libswscale license: "
96 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
103 //#define HAVE_AMD3DNOW
108 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
110 #define RET 0xC3 //near return opcode for x86
115 #define PI 3.14159265358979323846
118 #define isSupportedIn(x) ( \
119 (x)==PIX_FMT_YUV420P \
120 || (x)==PIX_FMT_YUVA420P \
121 || (x)==PIX_FMT_YUYV422 \
122 || (x)==PIX_FMT_UYVY422 \
123 || (x)==PIX_FMT_RGB48BE \
124 || (x)==PIX_FMT_RGB48LE \
125 || (x)==PIX_FMT_RGB32 \
126 || (x)==PIX_FMT_RGB32_1 \
127 || (x)==PIX_FMT_BGR24 \
128 || (x)==PIX_FMT_BGR565 \
129 || (x)==PIX_FMT_BGR555 \
130 || (x)==PIX_FMT_BGR32 \
131 || (x)==PIX_FMT_BGR32_1 \
132 || (x)==PIX_FMT_RGB24 \
133 || (x)==PIX_FMT_RGB565 \
134 || (x)==PIX_FMT_RGB555 \
135 || (x)==PIX_FMT_GRAY8 \
136 || (x)==PIX_FMT_YUV410P \
137 || (x)==PIX_FMT_YUV440P \
138 || (x)==PIX_FMT_NV12 \
139 || (x)==PIX_FMT_NV21 \
140 || (x)==PIX_FMT_GRAY16BE \
141 || (x)==PIX_FMT_GRAY16LE \
142 || (x)==PIX_FMT_YUV444P \
143 || (x)==PIX_FMT_YUV422P \
144 || (x)==PIX_FMT_YUV411P \
145 || (x)==PIX_FMT_PAL8 \
146 || (x)==PIX_FMT_BGR8 \
147 || (x)==PIX_FMT_RGB8 \
148 || (x)==PIX_FMT_BGR4_BYTE \
149 || (x)==PIX_FMT_RGB4_BYTE \
150 || (x)==PIX_FMT_YUV440P \
151 || (x)==PIX_FMT_MONOWHITE \
152 || (x)==PIX_FMT_MONOBLACK \
153 || (x)==PIX_FMT_YUV420P16LE \
154 || (x)==PIX_FMT_YUV422P16LE \
155 || (x)==PIX_FMT_YUV444P16LE \
156 || (x)==PIX_FMT_YUV420P16BE \
157 || (x)==PIX_FMT_YUV422P16BE \
158 || (x)==PIX_FMT_YUV444P16BE \
161 int sws_isSupportedInput(enum PixelFormat pix_fmt)
163 return isSupportedIn(pix_fmt);
166 #define isSupportedOut(x) ( \
167 (x)==PIX_FMT_YUV420P \
168 || (x)==PIX_FMT_YUVA420P \
169 || (x)==PIX_FMT_YUYV422 \
170 || (x)==PIX_FMT_UYVY422 \
171 || (x)==PIX_FMT_YUV444P \
172 || (x)==PIX_FMT_YUV422P \
173 || (x)==PIX_FMT_YUV411P \
176 || (x)==PIX_FMT_NV12 \
177 || (x)==PIX_FMT_NV21 \
178 || (x)==PIX_FMT_GRAY16BE \
179 || (x)==PIX_FMT_GRAY16LE \
180 || (x)==PIX_FMT_GRAY8 \
181 || (x)==PIX_FMT_YUV410P \
182 || (x)==PIX_FMT_YUV440P \
183 || (x)==PIX_FMT_YUV420P16LE \
184 || (x)==PIX_FMT_YUV422P16LE \
185 || (x)==PIX_FMT_YUV444P16LE \
186 || (x)==PIX_FMT_YUV420P16BE \
187 || (x)==PIX_FMT_YUV422P16BE \
188 || (x)==PIX_FMT_YUV444P16BE \
191 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
193 return isSupportedOut(pix_fmt);
196 #define isPacked(x) ( \
198 || (x)==PIX_FMT_YUYV422 \
199 || (x)==PIX_FMT_UYVY422 \
203 #define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
205 #define RGB2YUV_SHIFT 15
206 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
207 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
208 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
209 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
210 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
211 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
212 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
213 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
214 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
216 extern const int32_t ff_yuv2rgb_coeffs[8][4];
218 static const double rgb2yuv_table[8][9]={
219 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
220 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
221 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
222 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
223 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
224 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
225 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
226 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
231 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
234 more intelligent misalignment avoidance for the horizontal scaler
235 write special vertical cubic upscale version
236 optimize C code (YV12 / minmax)
237 add support for packed pixel YUV input & output
238 add support for Y8 output
239 optimize BGR24 & BGR32
240 add BGR4 output support
241 write special BGR->BGR scaler
244 #if ARCH_X86 && CONFIG_GPL
245 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
246 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
247 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
248 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
249 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
250 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
251 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
252 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
254 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
255 0x0103010301030103LL,
256 0x0200020002000200LL,};
258 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
259 0x0602060206020602LL,
260 0x0004000400040004LL,};
262 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
263 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
264 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
265 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
266 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
267 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
269 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
270 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
271 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
274 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
275 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
276 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
278 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
279 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
280 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
281 #endif /* FAST_BGR2YV12 */
282 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
283 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
284 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
286 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
287 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
288 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
289 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
290 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
292 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
293 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
294 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
297 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
299 #endif /* ARCH_X86 && CONFIG_GPL */
301 // clipping helper table for C implementations:
302 static unsigned char clip_table[768];
304 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
306 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
307 { 1, 3, 1, 3, 1, 3, 1, 3, },
308 { 2, 0, 2, 0, 2, 0, 2, 0, },
311 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
312 { 6, 2, 6, 2, 6, 2, 6, 2, },
313 { 0, 4, 0, 4, 0, 4, 0, 4, },
316 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
317 { 17, 9, 23, 15, 16, 8, 22, 14, },
318 { 5, 29, 3, 27, 4, 28, 2, 26, },
319 { 21, 13, 19, 11, 20, 12, 18, 10, },
320 { 0, 24, 6, 30, 1, 25, 7, 31, },
321 { 16, 8, 22, 14, 17, 9, 23, 15, },
322 { 4, 28, 2, 26, 5, 29, 3, 27, },
323 { 20, 12, 18, 10, 21, 13, 19, 11, },
324 { 1, 25, 7, 31, 0, 24, 6, 30, },
327 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
328 { 0, 55, 14, 68, 3, 58, 17, 72, },
329 { 37, 18, 50, 32, 40, 22, 54, 35, },
330 { 9, 64, 5, 59, 13, 67, 8, 63, },
331 { 46, 27, 41, 23, 49, 31, 44, 26, },
332 { 2, 57, 16, 71, 1, 56, 15, 70, },
333 { 39, 21, 52, 34, 38, 19, 51, 33, },
334 { 11, 66, 7, 62, 10, 65, 6, 60, },
335 { 48, 30, 43, 25, 47, 29, 42, 24, },
339 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
340 {117, 62, 158, 103, 113, 58, 155, 100, },
341 { 34, 199, 21, 186, 31, 196, 17, 182, },
342 {144, 89, 131, 76, 141, 86, 127, 72, },
343 { 0, 165, 41, 206, 10, 175, 52, 217, },
344 {110, 55, 151, 96, 120, 65, 162, 107, },
345 { 28, 193, 14, 179, 38, 203, 24, 189, },
346 {138, 83, 124, 69, 148, 93, 134, 79, },
347 { 7, 172, 48, 213, 3, 168, 45, 210, },
350 // tries to correct a gamma of 1.5
351 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
352 { 0, 143, 18, 200, 2, 156, 25, 215, },
353 { 78, 28, 125, 64, 89, 36, 138, 74, },
354 { 10, 180, 3, 161, 16, 195, 8, 175, },
355 {109, 51, 93, 38, 121, 60, 105, 47, },
356 { 1, 152, 23, 210, 0, 147, 20, 205, },
357 { 85, 33, 134, 71, 81, 30, 130, 67, },
358 { 14, 190, 6, 171, 12, 185, 5, 166, },
359 {117, 57, 101, 44, 113, 54, 97, 41, },
362 // tries to correct a gamma of 2.0
363 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
364 { 0, 124, 8, 193, 0, 140, 12, 213, },
365 { 55, 14, 104, 42, 66, 19, 119, 52, },
366 { 3, 168, 1, 145, 6, 187, 3, 162, },
367 { 86, 31, 70, 21, 99, 39, 82, 28, },
368 { 0, 134, 11, 206, 0, 129, 9, 200, },
369 { 62, 17, 114, 48, 58, 16, 109, 45, },
370 { 5, 181, 2, 157, 4, 175, 1, 151, },
371 { 95, 36, 78, 26, 90, 34, 74, 24, },
374 // tries to correct a gamma of 2.5
375 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
376 { 0, 107, 3, 187, 0, 125, 6, 212, },
377 { 39, 7, 86, 28, 49, 11, 102, 36, },
378 { 1, 158, 0, 131, 3, 180, 1, 151, },
379 { 68, 19, 52, 12, 81, 25, 64, 17, },
380 { 0, 119, 5, 203, 0, 113, 4, 195, },
381 { 45, 9, 96, 33, 42, 8, 91, 30, },
382 { 2, 172, 1, 144, 2, 165, 0, 137, },
383 { 77, 23, 60, 15, 72, 21, 56, 14, },
387 const char *sws_format_name(enum PixelFormat format)
389 if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
390 return av_pix_fmt_descriptors[format].name;
392 return "Unknown format";
395 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
396 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
397 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
398 int dstW, int chrDstW, int big_endian)
400 //FIXME Optimize (just quickly written not optimized..)
403 for (i = 0; i < dstW; i++) {
407 for (j = 0; j < lumFilterSize; j++)
408 val += lumSrc[j][i] * lumFilter[j];
411 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
413 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
418 for (i = 0; i < chrDstW; i++) {
423 for (j = 0; j < chrFilterSize; j++) {
424 u += chrSrc[j][i ] * chrFilter[j];
425 v += chrSrc[j][i + VOFW] * chrFilter[j];
429 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
430 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
432 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
433 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
438 if (CONFIG_SWSCALE_ALPHA && aDest) {
439 for (i = 0; i < dstW; i++) {
443 for (j = 0; j < lumFilterSize; j++)
444 val += alpSrc[j][i] * lumFilter[j];
447 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
449 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
455 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
456 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
457 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
458 enum PixelFormat dstFormat)
460 if (isBE(dstFormat)) {
461 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
462 chrFilter, chrSrc, chrFilterSize,
464 dest, uDest, vDest, aDest,
467 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
468 chrFilter, chrSrc, chrFilterSize,
470 dest, uDest, vDest, aDest,
475 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
476 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
477 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
479 //FIXME Optimize (just quickly written not optimized..)
481 for (i=0; i<dstW; i++) {
484 for (j=0; j<lumFilterSize; j++)
485 val += lumSrc[j][i] * lumFilter[j];
487 dest[i]= av_clip_uint8(val>>19);
491 for (i=0; i<chrDstW; i++) {
495 for (j=0; j<chrFilterSize; j++) {
496 u += chrSrc[j][i] * chrFilter[j];
497 v += chrSrc[j][i + VOFW] * chrFilter[j];
500 uDest[i]= av_clip_uint8(u>>19);
501 vDest[i]= av_clip_uint8(v>>19);
504 if (CONFIG_SWSCALE_ALPHA && aDest)
505 for (i=0; i<dstW; i++) {
508 for (j=0; j<lumFilterSize; j++)
509 val += alpSrc[j][i] * lumFilter[j];
511 aDest[i]= av_clip_uint8(val>>19);
516 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
517 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
518 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
520 //FIXME Optimize (just quickly written not optimized..)
522 for (i=0; i<dstW; i++) {
525 for (j=0; j<lumFilterSize; j++)
526 val += lumSrc[j][i] * lumFilter[j];
528 dest[i]= av_clip_uint8(val>>19);
534 if (dstFormat == PIX_FMT_NV12)
535 for (i=0; i<chrDstW; i++) {
539 for (j=0; j<chrFilterSize; j++) {
540 u += chrSrc[j][i] * chrFilter[j];
541 v += chrSrc[j][i + VOFW] * chrFilter[j];
544 uDest[2*i]= av_clip_uint8(u>>19);
545 uDest[2*i+1]= av_clip_uint8(v>>19);
548 for (i=0; i<chrDstW; i++) {
552 for (j=0; j<chrFilterSize; j++) {
553 u += chrSrc[j][i] * chrFilter[j];
554 v += chrSrc[j][i + VOFW] * chrFilter[j];
557 uDest[2*i]= av_clip_uint8(v>>19);
558 uDest[2*i+1]= av_clip_uint8(u>>19);
562 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
563 for (i=0; i<(dstW>>1); i++) {\
569 int av_unused A1, A2;\
570 type av_unused *r, *b, *g;\
573 for (j=0; j<lumFilterSize; j++) {\
574 Y1 += lumSrc[j][i2] * lumFilter[j];\
575 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
577 for (j=0; j<chrFilterSize; j++) {\
578 U += chrSrc[j][i] * chrFilter[j];\
579 V += chrSrc[j][i+VOFW] * chrFilter[j];\
588 for (j=0; j<lumFilterSize; j++) {\
589 A1 += alpSrc[j][i2 ] * lumFilter[j];\
590 A2 += alpSrc[j][i2+1] * lumFilter[j];\
596 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
597 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
598 if ((Y1|Y2|U|V)&256) {\
599 if (Y1>255) Y1=255; \
600 else if (Y1<0)Y1=0; \
601 if (Y2>255) Y2=255; \
602 else if (Y2<0)Y2=0; \
608 if (alpha && ((A1|A2)&256)) {\
609 A1=av_clip_uint8(A1);\
610 A2=av_clip_uint8(A2);\
613 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
614 for (i=0; i<dstW; i++) {\
622 for (j=0; j<lumFilterSize; j++) {\
623 Y += lumSrc[j][i ] * lumFilter[j];\
625 for (j=0; j<chrFilterSize; j++) {\
626 U += chrSrc[j][i ] * chrFilter[j];\
627 V += chrSrc[j][i+VOFW] * chrFilter[j];\
634 for (j=0; j<lumFilterSize; j++)\
635 A += alpSrc[j][i ] * lumFilter[j];\
638 A = av_clip_uint8(A);\
641 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
642 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
643 Y-= c->yuv2rgb_y_offset;\
644 Y*= c->yuv2rgb_y_coeff;\
646 R= Y + V*c->yuv2rgb_v2r_coeff;\
647 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
648 B= Y + U*c->yuv2rgb_u2b_coeff;\
649 if ((R|G|B)&(0xC0000000)) {\
650 if (R>=(256<<22)) R=(256<<22)-1; \
652 if (G>=(256<<22)) G=(256<<22)-1; \
654 if (B>=(256<<22)) B=(256<<22)-1; \
659 #define YSCALE_YUV_2_GRAY16_C \
660 for (i=0; i<(dstW>>1); i++) {\
669 for (j=0; j<lumFilterSize; j++) {\
670 Y1 += lumSrc[j][i2] * lumFilter[j];\
671 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
675 if ((Y1|Y2|U|V)&65536) {\
676 if (Y1>65535) Y1=65535; \
677 else if (Y1<0)Y1=0; \
678 if (Y2>65535) Y2=65535; \
679 else if (Y2<0)Y2=0; \
682 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
683 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
684 r = (type *)c->table_rV[V]; \
685 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
686 b = (type *)c->table_bU[U]; \
688 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
689 for (i=0; i<(dstW>>1); i++) { \
691 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
692 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
693 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
694 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
695 type av_unused *r, *b, *g; \
696 int av_unused A1, A2; \
698 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
699 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
702 #define YSCALE_YUV_2_GRAY16_2_C \
703 for (i=0; i<(dstW>>1); i++) { \
705 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
706 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
708 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
709 YSCALE_YUV_2_PACKED2_C(type,alpha)\
710 r = (type *)c->table_rV[V];\
711 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
712 b = (type *)c->table_bU[U];\
714 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
715 for (i=0; i<(dstW>>1); i++) {\
717 int Y1= buf0[i2 ]>>7;\
718 int Y2= buf0[i2+1]>>7;\
719 int U= (uvbuf1[i ])>>7;\
720 int V= (uvbuf1[i+VOFW])>>7;\
721 type av_unused *r, *b, *g;\
722 int av_unused A1, A2;\
728 #define YSCALE_YUV_2_GRAY16_1_C \
729 for (i=0; i<(dstW>>1); i++) {\
731 int Y1= buf0[i2 ]<<1;\
732 int Y2= buf0[i2+1]<<1;\
734 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
735 YSCALE_YUV_2_PACKED1_C(type,alpha)\
736 r = (type *)c->table_rV[V];\
737 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
738 b = (type *)c->table_bU[U];\
740 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
741 for (i=0; i<(dstW>>1); i++) {\
743 int Y1= buf0[i2 ]>>7;\
744 int Y2= buf0[i2+1]>>7;\
745 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
746 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
747 type av_unused *r, *b, *g;\
748 int av_unused A1, A2;\
754 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
755 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
756 r = (type *)c->table_rV[V];\
757 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
758 b = (type *)c->table_bU[U];\
760 #define YSCALE_YUV_2_MONO2_C \
761 const uint8_t * const d128=dither_8x8_220[y&7];\
762 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
763 for (i=0; i<dstW-7; i+=8) {\
765 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
766 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
767 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
768 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
769 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
770 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
771 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
772 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
773 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
778 #define YSCALE_YUV_2_MONOX_C \
779 const uint8_t * const d128=dither_8x8_220[y&7];\
780 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
782 for (i=0; i<dstW-1; i+=2) {\
787 for (j=0; j<lumFilterSize; j++) {\
788 Y1 += lumSrc[j][i] * lumFilter[j];\
789 Y2 += lumSrc[j][i+1] * lumFilter[j];\
799 acc+= acc + g[Y1+d128[(i+0)&7]];\
800 acc+= acc + g[Y2+d128[(i+1)&7]];\
802 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
808 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
809 switch(c->dstFormat) {\
810 case PIX_FMT_RGB48BE:\
811 case PIX_FMT_RGB48LE:\
813 ((uint8_t*)dest)[ 0]= r[Y1];\
814 ((uint8_t*)dest)[ 1]= r[Y1];\
815 ((uint8_t*)dest)[ 2]= g[Y1];\
816 ((uint8_t*)dest)[ 3]= g[Y1];\
817 ((uint8_t*)dest)[ 4]= b[Y1];\
818 ((uint8_t*)dest)[ 5]= b[Y1];\
819 ((uint8_t*)dest)[ 6]= r[Y2];\
820 ((uint8_t*)dest)[ 7]= r[Y2];\
821 ((uint8_t*)dest)[ 8]= g[Y2];\
822 ((uint8_t*)dest)[ 9]= g[Y2];\
823 ((uint8_t*)dest)[10]= b[Y2];\
824 ((uint8_t*)dest)[11]= b[Y2];\
831 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
832 func(uint32_t,needAlpha)\
833 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
834 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
837 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
839 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
840 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
844 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
845 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
853 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
854 func(uint32_t,needAlpha)\
855 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
856 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
859 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
861 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
862 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
866 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
867 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
874 ((uint8_t*)dest)[0]= r[Y1];\
875 ((uint8_t*)dest)[1]= g[Y1];\
876 ((uint8_t*)dest)[2]= b[Y1];\
877 ((uint8_t*)dest)[3]= r[Y2];\
878 ((uint8_t*)dest)[4]= g[Y2];\
879 ((uint8_t*)dest)[5]= b[Y2];\
885 ((uint8_t*)dest)[0]= b[Y1];\
886 ((uint8_t*)dest)[1]= g[Y1];\
887 ((uint8_t*)dest)[2]= r[Y1];\
888 ((uint8_t*)dest)[3]= b[Y2];\
889 ((uint8_t*)dest)[4]= g[Y2];\
890 ((uint8_t*)dest)[5]= r[Y2];\
894 case PIX_FMT_RGB565:\
895 case PIX_FMT_BGR565:\
897 const int dr1= dither_2x2_8[y&1 ][0];\
898 const int dg1= dither_2x2_4[y&1 ][0];\
899 const int db1= dither_2x2_8[(y&1)^1][0];\
900 const int dr2= dither_2x2_8[y&1 ][1];\
901 const int dg2= dither_2x2_4[y&1 ][1];\
902 const int db2= dither_2x2_8[(y&1)^1][1];\
904 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
905 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
909 case PIX_FMT_RGB555:\
910 case PIX_FMT_BGR555:\
912 const int dr1= dither_2x2_8[y&1 ][0];\
913 const int dg1= dither_2x2_8[y&1 ][1];\
914 const int db1= dither_2x2_8[(y&1)^1][0];\
915 const int dr2= dither_2x2_8[y&1 ][1];\
916 const int dg2= dither_2x2_8[y&1 ][0];\
917 const int db2= dither_2x2_8[(y&1)^1][1];\
919 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
920 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
927 const uint8_t * const d64= dither_8x8_73[y&7];\
928 const uint8_t * const d32= dither_8x8_32[y&7];\
930 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
931 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
938 const uint8_t * const d64= dither_8x8_73 [y&7];\
939 const uint8_t * const d128=dither_8x8_220[y&7];\
941 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
942 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
946 case PIX_FMT_RGB4_BYTE:\
947 case PIX_FMT_BGR4_BYTE:\
949 const uint8_t * const d64= dither_8x8_73 [y&7];\
950 const uint8_t * const d128=dither_8x8_220[y&7];\
952 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
953 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
957 case PIX_FMT_MONOBLACK:\
958 case PIX_FMT_MONOWHITE:\
963 case PIX_FMT_YUYV422:\
965 ((uint8_t*)dest)[2*i2+0]= Y1;\
966 ((uint8_t*)dest)[2*i2+1]= U;\
967 ((uint8_t*)dest)[2*i2+2]= Y2;\
968 ((uint8_t*)dest)[2*i2+3]= V;\
971 case PIX_FMT_UYVY422:\
973 ((uint8_t*)dest)[2*i2+0]= U;\
974 ((uint8_t*)dest)[2*i2+1]= Y1;\
975 ((uint8_t*)dest)[2*i2+2]= V;\
976 ((uint8_t*)dest)[2*i2+3]= Y2;\
979 case PIX_FMT_GRAY16BE:\
981 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
982 ((uint8_t*)dest)[2*i2+1]= Y1;\
983 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
984 ((uint8_t*)dest)[2*i2+3]= Y2;\
987 case PIX_FMT_GRAY16LE:\
989 ((uint8_t*)dest)[2*i2+0]= Y1;\
990 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
991 ((uint8_t*)dest)[2*i2+2]= Y2;\
992 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
998 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
999 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1000 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1003 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)
1006 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1007 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1008 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1011 int step= fmt_depth(c->dstFormat)/8;
1014 switch(c->dstFormat) {
1022 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1023 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1024 dest[aidx]= needAlpha ? A : 255;
1031 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1032 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1040 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1057 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1058 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1059 dest[aidx]= needAlpha ? A : 255;
1066 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1067 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1075 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1090 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1093 uint8_t *ptr = plane + stride*y;
1094 for (i=0; i<height; i++) {
1095 memset(ptr, val, width);
1100 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1103 for (i = 0; i < width; i++) {
1108 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1112 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1113 uint8_t *src1, uint8_t *src2, int width)
1117 for (i = 0; i < width; i++) {
1118 int r = src1[6*i + 0];
1119 int g = src1[6*i + 2];
1120 int b = src1[6*i + 4];
1122 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1123 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1127 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1128 uint8_t *src1, uint8_t *src2, int width)
1132 for (i = 0; i < width; i++) {
1133 int r= src1[12*i + 0] + src1[12*i + 6];
1134 int g= src1[12*i + 2] + src1[12*i + 8];
1135 int b= src1[12*i + 4] + src1[12*i + 10];
1137 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1138 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1142 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1143 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1146 for (i=0; i<width; i++) {\
1147 int b= (((const type*)src)[i]>>shb)&maskb;\
1148 int g= (((const type*)src)[i]>>shg)&maskg;\
1149 int r= (((const type*)src)[i]>>shr)&maskr;\
1151 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1155 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1156 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1157 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1158 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1159 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1160 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1162 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1165 for (i=0; i<width; i++) {
1170 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1171 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1174 for (i=0; i<width; i++) {\
1175 int b= (((const type*)src)[i]&maskb)>>shb;\
1176 int g= (((const type*)src)[i]&maskg)>>shg;\
1177 int r= (((const type*)src)[i]&maskr)>>shr;\
1179 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1180 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1183 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1186 for (i=0; i<width; i++) {\
1187 int pix0= ((const type*)src)[2*i+0];\
1188 int pix1= ((const type*)src)[2*i+1];\
1189 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1190 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1191 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1192 g&= maskg|(2*maskg);\
1196 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1197 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1201 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1202 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1203 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1204 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1205 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1206 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1208 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1211 for (i=0; i<width; i++) {
1214 dst[i]= pal[d] & 0xFF;
1218 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1219 const uint8_t *src1, const uint8_t *src2,
1220 long width, uint32_t *pal)
1223 assert(src1 == src2);
1224 for (i=0; i<width; i++) {
1225 int p= pal[src1[i]];
1232 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1235 for (i=0; i<width/8; i++) {
1238 dst[8*i+j]= ((d>>(7-j))&1)*255;
1242 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1245 for (i=0; i<width/8; i++) {
1248 dst[8*i+j]= ((d>>(7-j))&1)*255;
1253 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1255 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1260 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1261 #define COMPILE_ALTIVEC
1267 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1271 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1272 #define COMPILE_MMX2
1275 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1276 #define COMPILE_3DNOW
1280 #define COMPILE_TEMPLATE_MMX 0
1281 #define COMPILE_TEMPLATE_MMX2 0
1282 #define COMPILE_TEMPLATE_AMD3DNOW 0
1283 #define COMPILE_TEMPLATE_ALTIVEC 0
1286 #define RENAME(a) a ## _C
1287 #include "swscale_template.c"
1290 #ifdef COMPILE_ALTIVEC
1292 #undef COMPILE_TEMPLATE_ALTIVEC
1293 #define COMPILE_TEMPLATE_ALTIVEC 1
1294 #define RENAME(a) a ## _altivec
1295 #include "swscale_template.c"
1303 #undef COMPILE_TEMPLATE_MMX
1304 #undef COMPILE_TEMPLATE_MMX2
1305 #undef COMPILE_TEMPLATE_AMD3DNOW
1306 #define COMPILE_TEMPLATE_MMX 1
1307 #define COMPILE_TEMPLATE_MMX2 0
1308 #define COMPILE_TEMPLATE_AMD3DNOW 0
1309 #define RENAME(a) a ## _MMX
1310 #include "swscale_template.c"
1316 #undef COMPILE_TEMPLATE_MMX
1317 #undef COMPILE_TEMPLATE_MMX2
1318 #undef COMPILE_TEMPLATE_AMD3DNOW
1319 #define COMPILE_TEMPLATE_MMX 1
1320 #define COMPILE_TEMPLATE_MMX2 1
1321 #define COMPILE_TEMPLATE_AMD3DNOW 0
1322 #define RENAME(a) a ## _MMX2
1323 #include "swscale_template.c"
1327 #ifdef COMPILE_3DNOW
1329 #undef COMPILE_TEMPLATE_MMX
1330 #undef COMPILE_TEMPLATE_MMX2
1331 #undef COMPILE_TEMPLATE_AMD3DNOW
1332 #define COMPILE_TEMPLATE_MMX 1
1333 #define COMPILE_TEMPLATE_MMX2 0
1334 #define COMPILE_TEMPLATE_AMD3DNOW 1
1335 #define RENAME(a) a ## _3DNow
1336 #include "swscale_template.c"
1341 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1343 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1344 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1345 else return getSplineCoeff( 0.0,
1352 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1353 int srcW, int dstW, int filterAlign, int one, int flags,
1354 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1360 int64_t *filter=NULL;
1361 int64_t *filter2=NULL;
1362 const int64_t fone= 1LL<<54;
1365 if (flags & SWS_CPU_CAPS_MMX)
1366 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1369 // NOTE: the +1 is for the MMX scaler which reads over the end
1370 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
1372 if (FFABS(xInc - 0x10000) <10) { // unscaled
1375 FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1377 for (i=0; i<dstW; i++) {
1378 filter[i*filterSize]= fone;
1382 } else if (flags&SWS_POINT) { // lame looking point sampling mode
1386 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1388 xDstInSrc= xInc/2 - 0x8000;
1389 for (i=0; i<dstW; i++) {
1390 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1392 (*filterPos)[i]= xx;
1396 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1400 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1402 xDstInSrc= xInc/2 - 0x8000;
1403 for (i=0; i<dstW; i++) {
1404 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1407 (*filterPos)[i]= xx;
1408 //bilinear upscale / linear interpolate / area averaging
1409 for (j=0; j<filterSize; j++) {
1410 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1411 if (coeff<0) coeff=0;
1412 filter[i*filterSize + j]= coeff;
1421 if (flags&SWS_BICUBIC) sizeFactor= 4;
1422 else if (flags&SWS_X) sizeFactor= 8;
1423 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1424 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1425 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1426 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1427 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1428 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1430 sizeFactor= 0; //GCC warning killer
1434 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1435 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1437 if (filterSize > srcW-2) filterSize=srcW-2;
1439 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1441 xDstInSrc= xInc - 0x10000;
1442 for (i=0; i<dstW; i++) {
1443 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1445 (*filterPos)[i]= xx;
1446 for (j=0; j<filterSize; j++) {
1447 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1453 floatd= d * (1.0/(1<<30));
1455 if (flags & SWS_BICUBIC) {
1456 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1457 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1458 int64_t dd = ( d*d)>>30;
1459 int64_t ddd= (dd*d)>>30;
1462 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1463 else if (d < 1LL<<31)
1464 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1467 coeff *= fone>>(30+24);
1469 /* else if (flags & SWS_X) {
1470 double p= param ? param*0.01 : 0.3;
1471 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1472 coeff*= pow(2.0, - p*d*d);
1474 else if (flags & SWS_X) {
1475 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1482 if (c<0.0) c= -pow(-c, A);
1484 coeff= (c*0.5 + 0.5)*fone;
1485 } else if (flags & SWS_AREA) {
1486 int64_t d2= d - (1<<29);
1487 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1488 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1490 coeff *= fone>>(30+16);
1491 } else if (flags & SWS_GAUSS) {
1492 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1493 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1494 } else if (flags & SWS_SINC) {
1495 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1496 } else if (flags & SWS_LANCZOS) {
1497 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1498 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1499 if (floatd>p) coeff=0;
1500 } else if (flags & SWS_BILINEAR) {
1502 if (coeff<0) coeff=0;
1503 coeff *= fone >> 30;
1504 } else if (flags & SWS_SPLINE) {
1505 double p=-2.196152422706632;
1506 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1508 coeff= 0.0; //GCC warning killer
1512 filter[i*filterSize + j]= coeff;
1519 /* apply src & dst Filter to filter -> filter2
1522 assert(filterSize>0);
1523 filter2Size= filterSize;
1524 if (srcFilter) filter2Size+= srcFilter->length - 1;
1525 if (dstFilter) filter2Size+= dstFilter->length - 1;
1526 assert(filter2Size>0);
1527 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
1529 for (i=0; i<dstW; i++) {
1533 for (k=0; k<srcFilter->length; k++) {
1534 for (j=0; j<filterSize; j++)
1535 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1538 for (j=0; j<filterSize; j++)
1539 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1543 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1547 /* try to reduce the filter-size (step1 find size and shift left) */
1548 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1550 for (i=dstW-1; i>=0; i--) {
1551 int min= filter2Size;
1555 /* get rid off near zero elements on the left by shifting left */
1556 for (j=0; j<filter2Size; j++) {
1558 cutOff += FFABS(filter2[i*filter2Size]);
1560 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1562 /* preserve monotonicity because the core can't handle the filter otherwise */
1563 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1565 // move filter coefficients left
1566 for (k=1; k<filter2Size; k++)
1567 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1568 filter2[i*filter2Size + k - 1]= 0;
1573 /* count near zeros on the right */
1574 for (j=filter2Size-1; j>0; j--) {
1575 cutOff += FFABS(filter2[i*filter2Size + j]);
1577 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1581 if (min>minFilterSize) minFilterSize= min;
1584 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1585 // we can handle the special case 4,
1586 // so we don't want to go to the full 8
1587 if (minFilterSize < 5)
1590 // We really don't want to waste our time
1591 // doing useless computation, so fall back on
1592 // the scalar C code for very small filters.
1593 // Vectorizing is worth it only if you have a
1594 // decent-sized vector.
1595 if (minFilterSize < 3)
1599 if (flags & SWS_CPU_CAPS_MMX) {
1600 // special case for unscaled vertical filtering
1601 if (minFilterSize == 1 && filterAlign == 2)
1605 assert(minFilterSize > 0);
1606 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1607 assert(filterSize > 0);
1608 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1609 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1611 *outFilterSize= filterSize;
1613 if (flags&SWS_PRINT_INFO)
1614 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1615 /* try to reduce the filter-size (step2 reduce it) */
1616 for (i=0; i<dstW; i++) {
1619 for (j=0; j<filterSize; j++) {
1620 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1621 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1622 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1623 filter[i*filterSize + j]= 0;
1628 //FIXME try to align filterPos if possible
1631 for (i=0; i<dstW; i++) {
1633 if ((*filterPos)[i] < 0) {
1634 // move filter coefficients left to compensate for filterPos
1635 for (j=1; j<filterSize; j++) {
1636 int left= FFMAX(j + (*filterPos)[i], 0);
1637 filter[i*filterSize + left] += filter[i*filterSize + j];
1638 filter[i*filterSize + j]=0;
1643 if ((*filterPos)[i] + filterSize > srcW) {
1644 int shift= (*filterPos)[i] + filterSize - srcW;
1645 // move filter coefficients right to compensate for filterPos
1646 for (j=filterSize-2; j>=0; j--) {
1647 int right= FFMIN(j + shift, filterSize-1);
1648 filter[i*filterSize +right] += filter[i*filterSize +j];
1649 filter[i*filterSize +j]=0;
1651 (*filterPos)[i]= srcW - filterSize;
1655 // Note the +1 is for the MMX scaler which reads over the end
1656 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1657 FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
1659 /* normalize & store in outFilter */
1660 for (i=0; i<dstW; i++) {
1665 for (j=0; j<filterSize; j++) {
1666 sum+= filter[i*filterSize + j];
1668 sum= (sum + one/2)/ one;
1669 for (j=0; j<*outFilterSize; j++) {
1670 int64_t v= filter[i*filterSize + j] + error;
1671 int intV= ROUNDED_DIV(v, sum);
1672 (*outFilter)[i*(*outFilterSize) + j]= intV;
1673 error= v - intV*sum;
1677 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1678 for (i=0; i<*outFilterSize; i++) {
1679 int j= dstW*(*outFilterSize);
1680 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1691 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1694 x86_reg imm8OfPShufW1A;
1695 x86_reg imm8OfPShufW2A;
1696 x86_reg fragmentLengthA;
1698 x86_reg imm8OfPShufW1B;
1699 x86_reg imm8OfPShufW2B;
1700 x86_reg fragmentLengthB;
1705 // create an optimized horizontal scaling routine
1706 /* This scaler is made of runtime-generated MMX2 code using specially
1707 * tuned pshufw instructions. For every four output pixels, if four
1708 * input pixels are enough for the fast bilinear scaling, then a chunk
1709 * of fragmentB is used. If five input pixels are needed, then a chunk
1710 * of fragmentA is used.
1719 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1720 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1721 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1722 "punpcklbw %%mm7, %%mm1 \n\t"
1723 "punpcklbw %%mm7, %%mm0 \n\t"
1724 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1726 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1728 "psubw %%mm1, %%mm0 \n\t"
1729 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1730 "pmullw %%mm3, %%mm0 \n\t"
1731 "psllw $7, %%mm1 \n\t"
1732 "paddw %%mm1, %%mm0 \n\t"
1734 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1736 "add $8, %%"REG_a" \n\t"
1740 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1741 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1742 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1747 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1751 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1752 "=r" (fragmentLengthA)
1759 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1760 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1761 "punpcklbw %%mm7, %%mm0 \n\t"
1762 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1764 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1766 "psubw %%mm1, %%mm0 \n\t"
1767 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1768 "pmullw %%mm3, %%mm0 \n\t"
1769 "psllw $7, %%mm1 \n\t"
1770 "paddw %%mm1, %%mm0 \n\t"
1772 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1774 "add $8, %%"REG_a" \n\t"
1778 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1779 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1780 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1785 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1789 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1790 "=r" (fragmentLengthB)
1793 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1796 for (i=0; i<dstW/numSplits; i++) {
1801 int b=((xpos+xInc)>>16) - xx;
1802 int c=((xpos+xInc*2)>>16) - xx;
1803 int d=((xpos+xInc*3)>>16) - xx;
1805 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
1806 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
1807 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
1808 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1809 int maxShift= 3-(d+inc);
1813 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1814 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1815 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1816 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1819 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1821 filterCode[fragmentPos + imm8OfPShufW1]=
1822 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1823 filterCode[fragmentPos + imm8OfPShufW2]=
1824 a | (b<<2) | (c<<4) | (d<<6);
1826 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1827 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1829 if (shift && i>=shift) {
1830 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1831 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1832 filterPos[i/2]-=shift;
1836 fragmentPos+= fragmentLength;
1839 filterCode[fragmentPos]= RET;
1844 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1846 return fragmentPos + 1;
1848 #endif /* COMPILE_MMX2 */
1850 static void globalInit(void)
1852 // generating tables:
1854 for (i=0; i<768; i++) {
1855 int c= av_clip_uint8(i-256);
1860 static SwsFunc getSwsFunc(SwsContext *c)
1862 #if CONFIG_RUNTIME_CPUDETECT
1863 int flags = c->flags;
1865 #if ARCH_X86 && CONFIG_GPL
1866 // ordered per speed fastest first
1867 if (flags & SWS_CPU_CAPS_MMX2) {
1868 sws_init_swScale_MMX2(c);
1869 return swScale_MMX2;
1870 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1871 sws_init_swScale_3DNow(c);
1872 return swScale_3DNow;
1873 } else if (flags & SWS_CPU_CAPS_MMX) {
1874 sws_init_swScale_MMX(c);
1877 sws_init_swScale_C(c);
1883 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1884 sws_init_swScale_altivec(c);
1885 return swScale_altivec;
1887 sws_init_swScale_C(c);
1891 sws_init_swScale_C(c);
1893 #endif /* ARCH_X86 && CONFIG_GPL */
1894 #else //CONFIG_RUNTIME_CPUDETECT
1895 #if COMPILE_TEMPLATE_MMX2
1896 sws_init_swScale_MMX2(c);
1897 return swScale_MMX2;
1898 #elif COMPILE_TEMPLATE_AMD3DNOW
1899 sws_init_swScale_3DNow(c);
1900 return swScale_3DNow;
1901 #elif COMPILE_TEMPLATE_MMX
1902 sws_init_swScale_MMX(c);
1904 #elif COMPILE_TEMPLATE_ALTIVEC
1905 sws_init_swScale_altivec(c);
1906 return swScale_altivec;
1908 sws_init_swScale_C(c);
1911 #endif //!CONFIG_RUNTIME_CPUDETECT
1914 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1915 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1917 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1919 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1920 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1923 const uint8_t *srcPtr= src[0];
1924 uint8_t *dstPtr= dst;
1925 for (i=0; i<srcSliceH; i++) {
1926 memcpy(dstPtr, srcPtr, c->srcW);
1927 srcPtr+= srcStride[0];
1928 dstPtr+= dstStride[0];
1931 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1932 if (c->dstFormat == PIX_FMT_NV12)
1933 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1935 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1940 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1941 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1943 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1945 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1950 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1951 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1953 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1955 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1960 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1961 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1963 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1965 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1970 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1971 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1973 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1975 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1980 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1981 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1983 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1984 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1985 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1987 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1990 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1995 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1996 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1998 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1999 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2000 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2002 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2007 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2008 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2010 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2011 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2012 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2014 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2017 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2022 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2023 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2025 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2026 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2027 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2029 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2034 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2035 int srcSliceH, uint8_t* dst[], int dstStride[])
2037 const enum PixelFormat srcFormat= c->srcFormat;
2038 const enum PixelFormat dstFormat= c->dstFormat;
2039 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2040 const uint8_t *palette)=NULL;
2042 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2043 uint8_t *srcPtr= src[0];
2045 if (!usePal(srcFormat))
2046 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2047 sws_format_name(srcFormat), sws_format_name(dstFormat));
2050 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2051 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2052 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2053 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2054 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2055 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2056 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2057 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2061 for (i=0; i<srcSliceH; i++) {
2062 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2063 srcPtr+= srcStride[0];
2064 dstPtr+= dstStride[0];
2070 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2071 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2072 int srcSliceH, uint8_t* dst[], int dstStride[])
2074 const enum PixelFormat srcFormat= c->srcFormat;
2075 const enum PixelFormat dstFormat= c->dstFormat;
2076 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2077 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2078 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2079 const int dstId= fmt_depth(dstFormat) >> 2;
2080 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2083 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2084 || (isRGB(srcFormat) && isRGB(dstFormat))) {
2085 switch(srcId | (dstId<<4)) {
2086 case 0x34: conv= rgb16to15; break;
2087 case 0x36: conv= rgb24to15; break;
2088 case 0x38: conv= rgb32to15; break;
2089 case 0x43: conv= rgb15to16; break;
2090 case 0x46: conv= rgb24to16; break;
2091 case 0x48: conv= rgb32to16; break;
2092 case 0x63: conv= rgb15to24; break;
2093 case 0x64: conv= rgb16to24; break;
2094 case 0x68: conv= rgb32to24; break;
2095 case 0x83: conv= rgb15to32; break;
2096 case 0x84: conv= rgb16to32; break;
2097 case 0x86: conv= rgb24to32; break;
2098 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2099 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2101 } else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2102 || (isRGB(srcFormat) && isBGR(dstFormat))) {
2103 switch(srcId | (dstId<<4)) {
2104 case 0x33: conv= rgb15tobgr15; break;
2105 case 0x34: conv= rgb16tobgr15; break;
2106 case 0x36: conv= rgb24tobgr15; break;
2107 case 0x38: conv= rgb32tobgr15; break;
2108 case 0x43: conv= rgb15tobgr16; break;
2109 case 0x44: conv= rgb16tobgr16; break;
2110 case 0x46: conv= rgb24tobgr16; break;
2111 case 0x48: conv= rgb32tobgr16; break;
2112 case 0x63: conv= rgb15tobgr24; break;
2113 case 0x64: conv= rgb16tobgr24; break;
2114 case 0x66: conv= rgb24tobgr24; break;
2115 case 0x68: conv= rgb32tobgr24; break;
2116 case 0x83: conv= rgb15tobgr32; break;
2117 case 0x84: conv= rgb16tobgr32; break;
2118 case 0x86: conv= rgb24tobgr32; break;
2119 case 0x88: conv= rgb32tobgr32; break;
2120 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2121 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2124 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2125 sws_format_name(srcFormat), sws_format_name(dstFormat));
2129 uint8_t *srcPtr= src[0];
2130 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2131 srcPtr += ALT32_CORR;
2133 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2134 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2137 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2139 for (i=0; i<srcSliceH; i++) {
2140 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2141 srcPtr+= srcStride[0];
2142 dstPtr+= dstStride[0];
2149 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2150 int srcSliceH, uint8_t* dst[], int dstStride[])
2155 dst[0]+ srcSliceY *dstStride[0],
2156 dst[1]+(srcSliceY>>1)*dstStride[1],
2157 dst[2]+(srcSliceY>>1)*dstStride[2],
2159 dstStride[0], dstStride[1], srcStride[0]);
2161 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2165 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2166 int srcSliceH, uint8_t* dst[], int dstStride[])
2171 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2172 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2174 uint8_t *srcPtr= src[0];
2175 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2177 for (i=0; i<srcSliceH; i++) {
2178 memcpy(dstPtr, srcPtr, c->srcW);
2179 srcPtr+= srcStride[0];
2180 dstPtr+= dstStride[0];
2184 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
2185 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2186 srcSliceH >> 2, srcStride[1], dstStride[1]);
2187 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2188 srcSliceH >> 2, srcStride[2], dstStride[2]);
2190 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2191 srcSliceH >> 2, srcStride[1], dstStride[2]);
2192 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2193 srcSliceH >> 2, srcStride[2], dstStride[1]);
2196 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2200 /* unscaled copy like stuff (assumes nearly identical formats) */
2201 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2202 int srcSliceH, uint8_t* dst[], int dstStride[])
2204 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2205 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2208 uint8_t *srcPtr= src[0];
2209 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2212 /* universal length finder */
2213 while(length+c->srcW <= FFABS(dstStride[0])
2214 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2217 for (i=0; i<srcSliceH; i++) {
2218 memcpy(dstPtr, srcPtr, length);
2219 srcPtr+= srcStride[0];
2220 dstPtr+= dstStride[0];
2226 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2227 int srcSliceH, uint8_t* dst[], int dstStride[])
2230 for (plane=0; plane<4; plane++) {
2231 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2232 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2233 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2234 uint8_t *srcPtr= src[plane];
2235 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2237 if (!dst[plane]) continue;
2238 // ignore palette for GRAY8
2239 if (plane == 1 && !dst[2]) continue;
2240 if (!src[plane] || (plane == 1 && !src[2])) {
2241 if(is16BPS(c->dstFormat))
2243 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2245 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
2246 if (!isBE(c->srcFormat)) srcPtr++;
2247 for (i=0; i<height; i++) {
2248 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2249 srcPtr+= srcStride[plane];
2250 dstPtr+= dstStride[plane];
2252 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
2253 for (i=0; i<height; i++) {
2254 for (j=0; j<length; j++) {
2255 dstPtr[ j<<1 ] = srcPtr[j];
2256 dstPtr[(j<<1)+1] = srcPtr[j];
2258 srcPtr+= srcStride[plane];
2259 dstPtr+= dstStride[plane];
2261 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2262 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
2264 for (i=0; i<height; i++) {
2265 for (j=0; j<length; j++)
2266 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2267 srcPtr+= srcStride[plane];
2268 dstPtr+= dstStride[plane];
2270 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2271 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2273 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2275 for (i=0; i<height; i++) {
2276 memcpy(dstPtr, srcPtr, length);
2277 srcPtr+= srcStride[plane];
2278 dstPtr+= dstStride[plane];
2287 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
2289 *h = av_pix_fmt_descriptors[format].log2_chroma_w;
2290 *v = av_pix_fmt_descriptors[format].log2_chroma_h;
2293 static uint16_t roundToInt16(int64_t f)
2295 int r= (f + (1<<15))>>16;
2296 if (r<-0x7FFF) return 0x8000;
2297 else if (r> 0x7FFF) return 0x7FFF;
2301 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
2303 int64_t crv = inv_table[0];
2304 int64_t cbu = inv_table[1];
2305 int64_t cgu = -inv_table[2];
2306 int64_t cgv = -inv_table[3];
2310 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2311 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2313 c->brightness= brightness;
2314 c->contrast = contrast;
2315 c->saturation= saturation;
2316 c->srcRange = srcRange;
2317 c->dstRange = dstRange;
2318 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2320 c->uOffset= 0x0400040004000400LL;
2321 c->vOffset= 0x0400040004000400LL;
2327 crv= (crv*224) / 255;
2328 cbu= (cbu*224) / 255;
2329 cgu= (cgu*224) / 255;
2330 cgv= (cgv*224) / 255;
2333 cy = (cy *contrast )>>16;
2334 crv= (crv*contrast * saturation)>>32;
2335 cbu= (cbu*contrast * saturation)>>32;
2336 cgu= (cgu*contrast * saturation)>>32;
2337 cgv= (cgv*contrast * saturation)>>32;
2339 oy -= 256*brightness;
2341 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2342 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2343 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2344 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2345 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2346 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2348 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2349 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2350 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2351 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2352 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2353 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2355 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2358 #ifdef COMPILE_ALTIVEC
2359 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2360 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2365 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
2367 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2369 *inv_table = c->srcColorspaceTable;
2370 *table = c->dstColorspaceTable;
2371 *srcRange = c->srcRange;
2372 *dstRange = c->dstRange;
2373 *brightness= c->brightness;
2374 *contrast = c->contrast;
2375 *saturation= c->saturation;
2380 static int handle_jpeg(enum PixelFormat *format)
2383 case PIX_FMT_YUVJ420P:
2384 *format = PIX_FMT_YUV420P;
2386 case PIX_FMT_YUVJ422P:
2387 *format = PIX_FMT_YUV422P;
2389 case PIX_FMT_YUVJ444P:
2390 *format = PIX_FMT_YUV444P;
2392 case PIX_FMT_YUVJ440P:
2393 *format = PIX_FMT_YUV440P;
2400 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2401 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2406 int usesVFilter, usesHFilter;
2407 int unscaled, needsDither;
2408 int srcRange, dstRange;
2409 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2411 if (flags & SWS_CPU_CAPS_MMX)
2412 __asm__ volatile("emms\n\t"::: "memory");
2415 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2416 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2417 #if COMPILE_TEMPLATE_MMX2
2418 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2419 #elif COMPILE_TEMPLATE_AMD3DNOW
2420 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2421 #elif COMPILE_TEMPLATE_MMX
2422 flags |= SWS_CPU_CAPS_MMX;
2423 #elif COMPILE_TEMPLATE_ALTIVEC
2424 flags |= SWS_CPU_CAPS_ALTIVEC;
2426 flags |= SWS_CPU_CAPS_BFIN;
2428 #endif /* CONFIG_RUNTIME_CPUDETECT */
2429 if (clip_table[512] != 255) globalInit();
2430 if (!rgb15to16) sws_rgb2rgb_init(flags);
2432 unscaled = (srcW == dstW && srcH == dstH);
2433 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2434 && (fmt_depth(dstFormat))<24
2435 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2437 srcRange = handle_jpeg(&srcFormat);
2438 dstRange = handle_jpeg(&dstFormat);
2440 if (!isSupportedIn(srcFormat)) {
2441 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2444 if (!isSupportedOut(dstFormat)) {
2445 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2449 i= flags & ( SWS_POINT
2460 if(!i || (i & (i-1))) {
2461 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2466 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
2467 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2468 srcW, srcH, dstW, dstH);
2471 if(srcW > VOFW || dstW > VOFW) {
2472 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2476 if (!dstFilter) dstFilter= &dummyFilter;
2477 if (!srcFilter) srcFilter= &dummyFilter;
2479 FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
2481 c->av_class = &sws_context_class;
2486 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2487 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2489 c->dstFormat= dstFormat;
2490 c->srcFormat= srcFormat;
2491 c->vRounder= 4* 0x0001000100010001ULL;
2493 usesHFilter= usesVFilter= 0;
2494 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2495 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2496 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2497 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2498 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2499 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2500 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2501 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2503 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2504 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2506 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2507 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2509 // drop some chroma lines if the user wants it
2510 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2511 c->chrSrcVSubSample+= c->vChrDrop;
2513 // drop every other pixel for chroma calculation unless user wants full chroma
2514 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2515 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2516 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2517 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2518 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2519 c->chrSrcHSubSample=1;
2522 c->param[0] = param[0];
2523 c->param[1] = param[1];
2526 c->param[1] = SWS_PARAM_DEFAULT;
2529 // Note the -((-x)>>y) is so that we always round toward +inf.
2530 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2531 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2532 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2533 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2535 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2537 /* unscaled special cases */
2538 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat))) {
2540 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
2541 c->swScale= PlanarToNV12Wrapper;
2544 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2545 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
2546 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2549 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2550 c->swScale= yvu9toyv12Wrapper;
2554 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2555 c->swScale= bgr24toyv12Wrapper;
2557 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2558 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2559 && (isBGR(dstFormat) || isRGB(dstFormat))
2560 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2561 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2562 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2563 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2564 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2565 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2566 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2567 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2568 && dstFormat != PIX_FMT_RGB32_1
2569 && dstFormat != PIX_FMT_BGR32_1
2570 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2571 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2572 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2573 c->swScale= rgb2rgbWrapper;
2575 if ((usePal(srcFormat) && (
2576 dstFormat == PIX_FMT_RGB32 ||
2577 dstFormat == PIX_FMT_RGB32_1 ||
2578 dstFormat == PIX_FMT_RGB24 ||
2579 dstFormat == PIX_FMT_BGR32 ||
2580 dstFormat == PIX_FMT_BGR32_1 ||
2581 dstFormat == PIX_FMT_BGR24)))
2582 c->swScale= pal2rgbWrapper;
2584 if (srcFormat == PIX_FMT_YUV422P) {
2585 if (dstFormat == PIX_FMT_YUYV422)
2586 c->swScale= YUV422PToYuy2Wrapper;
2587 else if (dstFormat == PIX_FMT_UYVY422)
2588 c->swScale= YUV422PToUyvyWrapper;
2591 /* LQ converters if -sws 0 or -sws 4*/
2592 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2594 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2595 if (dstFormat == PIX_FMT_YUYV422)
2596 c->swScale= PlanarToYuy2Wrapper;
2597 else if (dstFormat == PIX_FMT_UYVY422)
2598 c->swScale= PlanarToUyvyWrapper;
2601 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2602 c->swScale= YUYV2YUV420Wrapper;
2603 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2604 c->swScale= UYVY2YUV420Wrapper;
2605 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2606 c->swScale= YUYV2YUV422Wrapper;
2607 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2608 c->swScale= UYVY2YUV422Wrapper;
2610 #ifdef COMPILE_ALTIVEC
2611 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2612 !(c->flags & SWS_BITEXACT) &&
2613 srcFormat == PIX_FMT_YUV420P) {
2614 // unscaled YV12 -> packed YUV, we want speed
2615 if (dstFormat == PIX_FMT_YUYV422)
2616 c->swScale= yv12toyuy2_unscaled_altivec;
2617 else if (dstFormat == PIX_FMT_UYVY422)
2618 c->swScale= yv12touyvy_unscaled_altivec;
2623 if ( srcFormat == dstFormat
2624 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2625 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2626 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2627 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2628 || (isGray(dstFormat) && isGray(srcFormat))
2629 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2630 && c->chrDstHSubSample == c->chrSrcHSubSample
2631 && c->chrDstVSubSample == c->chrSrcVSubSample
2632 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2633 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2635 if (isPacked(c->srcFormat))
2636 c->swScale= packedCopy;
2637 else /* Planar YUV or gray */
2638 c->swScale= planarCopy;
2641 if (flags & SWS_CPU_CAPS_BFIN)
2642 ff_bfin_get_unscaled_swscale (c);
2646 if (flags&SWS_PRINT_INFO)
2647 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2648 sws_format_name(srcFormat), sws_format_name(dstFormat));
2653 if (flags & SWS_CPU_CAPS_MMX2) {
2654 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2655 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
2656 if (flags&SWS_PRINT_INFO)
2657 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2659 if (usesHFilter) c->canMMX2BeUsed=0;
2664 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2665 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2667 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2668 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2669 // n-2 is the last chrominance sample available
2670 // this is not perfect, but no one should notice the difference, the more correct variant
2671 // would be like the vertical one, but that would require some special code for the
2672 // first and last pixel
2673 if (flags&SWS_FAST_BILINEAR) {
2674 if (c->canMMX2BeUsed) {
2678 //we don't use the x86 asm scaler if MMX is available
2679 else if (flags & SWS_CPU_CAPS_MMX) {
2680 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2681 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2685 /* precalculate horizontal scaler filter coefficients */
2687 const int filterAlign=
2688 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2689 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2692 if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2693 srcW , dstW, filterAlign, 1<<14,
2694 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2695 srcFilter->lumH, dstFilter->lumH, c->param) < 0)
2697 if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2698 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2699 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2700 srcFilter->chrH, dstFilter->chrH, c->param) < 0)
2703 #if defined(COMPILE_MMX2)
2704 // can't downscale !!!
2705 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
2706 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
2707 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2709 #ifdef MAP_ANONYMOUS
2710 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2711 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2712 #elif HAVE_VIRTUALALLOC
2713 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2714 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2716 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2717 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2720 FF_ALLOCZ_OR_GOTO(c, c->lumMmx2Filter , (dstW /8+8)*sizeof(int16_t), fail);
2721 FF_ALLOCZ_OR_GOTO(c, c->chrMmx2Filter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
2722 FF_ALLOCZ_OR_GOTO(c, c->lumMmx2FilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
2723 FF_ALLOCZ_OR_GOTO(c, c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
2725 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2726 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2728 #ifdef MAP_ANONYMOUS
2729 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2730 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2733 #endif /* defined(COMPILE_MMX2) */
2734 } // initialize horizontal stuff
2738 /* precalculate vertical scaler filter coefficients */
2740 const int filterAlign=
2741 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2742 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2745 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2746 srcH , dstH, filterAlign, (1<<12),
2747 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2748 srcFilter->lumV, dstFilter->lumV, c->param) < 0)
2750 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2751 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2752 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2753 srcFilter->chrV, dstFilter->chrV, c->param) < 0)
2756 #ifdef COMPILE_ALTIVEC
2757 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
2758 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
2760 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2762 short *p = (short *)&c->vYCoeffsBank[i];
2764 p[j] = c->vLumFilter[i];
2767 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2769 short *p = (short *)&c->vCCoeffsBank[i];
2771 p[j] = c->vChrFilter[i];
2776 // calculate buffer sizes so that they won't run out while handling these damn slices
2777 c->vLumBufSize= c->vLumFilterSize;
2778 c->vChrBufSize= c->vChrFilterSize;
2779 for (i=0; i<dstH; i++) {
2780 int chrI= i*c->chrDstH / dstH;
2781 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2782 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2784 nextSlice>>= c->chrSrcVSubSample;
2785 nextSlice<<= c->chrSrcVSubSample;
2786 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2787 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2788 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2789 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2792 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2793 // allocate several megabytes to handle all possible cases)
2794 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2795 FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
2796 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2797 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2798 //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)
2799 /* align at 16 bytes for AltiVec */
2800 for (i=0; i<c->vLumBufSize; i++) {
2801 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
2802 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2804 for (i=0; i<c->vChrBufSize; i++) {
2805 FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
2806 c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2808 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2809 for (i=0; i<c->vLumBufSize; i++) {
2810 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
2811 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2814 //try to avoid drawing green stuff between the right end and the stride end
2815 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2817 assert(2*VOFW == VOF);
2819 assert(c->chrDstH <= dstH);
2821 if (flags&SWS_PRINT_INFO) {
2823 const char *dither= " dithered";
2825 const char *dither= "";
2827 if (flags&SWS_FAST_BILINEAR)
2828 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2829 else if (flags&SWS_BILINEAR)
2830 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2831 else if (flags&SWS_BICUBIC)
2832 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2833 else if (flags&SWS_X)
2834 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2835 else if (flags&SWS_POINT)
2836 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2837 else if (flags&SWS_AREA)
2838 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2839 else if (flags&SWS_BICUBLIN)
2840 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2841 else if (flags&SWS_GAUSS)
2842 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2843 else if (flags&SWS_SINC)
2844 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2845 else if (flags&SWS_LANCZOS)
2846 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2847 else if (flags&SWS_SPLINE)
2848 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2850 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2852 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2853 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2854 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2856 av_log(c, AV_LOG_INFO, "from %s to %s ",
2857 sws_format_name(srcFormat), sws_format_name(dstFormat));
2859 if (flags & SWS_CPU_CAPS_MMX2)
2860 av_log(c, AV_LOG_INFO, "using MMX2\n");
2861 else if (flags & SWS_CPU_CAPS_3DNOW)
2862 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2863 else if (flags & SWS_CPU_CAPS_MMX)
2864 av_log(c, AV_LOG_INFO, "using MMX\n");
2865 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2866 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2868 av_log(c, AV_LOG_INFO, "using C\n");
2871 if (flags & SWS_PRINT_INFO) {
2872 if (flags & SWS_CPU_CAPS_MMX) {
2873 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2874 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2876 if (c->hLumFilterSize==4)
2877 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2878 else if (c->hLumFilterSize==8)
2879 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2881 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2883 if (c->hChrFilterSize==4)
2884 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2885 else if (c->hChrFilterSize==8)
2886 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2888 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2892 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2894 if (flags & SWS_FAST_BILINEAR)
2895 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2897 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2900 if (isPlanarYUV(dstFormat)) {
2901 if (c->vLumFilterSize==1)
2902 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2904 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2906 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2907 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2908 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2909 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2910 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2912 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2915 if (dstFormat==PIX_FMT_BGR24)
2916 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2917 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2918 else if (dstFormat==PIX_FMT_RGB32)
2919 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2920 else if (dstFormat==PIX_FMT_BGR565)
2921 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2922 else if (dstFormat==PIX_FMT_BGR555)
2923 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2925 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2927 if (flags & SWS_PRINT_INFO) {
2928 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2929 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2930 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2931 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2934 c->swScale= getSwsFunc(c);
2942 static void reset_ptr(uint8_t* src[], int format)
2944 if(!isALPHA(format))
2946 if(!isPlanarYUV(format)) {
2948 if( format != PIX_FMT_PAL8
2949 && format != PIX_FMT_RGB8
2950 && format != PIX_FMT_BGR8
2951 && format != PIX_FMT_RGB4_BYTE
2952 && format != PIX_FMT_BGR4_BYTE
2959 * swscale wrapper, so we don't need to export the SwsContext.
2960 * Assumes planar YUV to be in YUV order instead of YVU.
2962 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2963 int srcSliceH, uint8_t* dst[], int dstStride[])
2966 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2967 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2969 // do not mess up sliceDir if we have a "trailing" 0-size slice
2973 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2974 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2977 if (c->sliceDir == 0) {
2978 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2981 if (usePal(c->srcFormat)) {
2982 for (i=0; i<256; i++) {
2983 int p, r, g, b,y,u,v;
2984 if(c->srcFormat == PIX_FMT_PAL8) {
2985 p=((uint32_t*)(src[1]))[i];
2989 } else if(c->srcFormat == PIX_FMT_RGB8) {
2993 } else if(c->srcFormat == PIX_FMT_BGR8) {
2997 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
3002 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3007 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3008 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3009 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3010 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3013 switch(c->dstFormat) {
3018 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3020 case PIX_FMT_BGR32_1:
3024 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3026 case PIX_FMT_RGB32_1:
3030 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3037 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3042 // copy strides, so they can safely be modified
3043 if (c->sliceDir == 1) {
3044 // slices go from top to bottom
3045 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3046 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3048 reset_ptr(src2, c->srcFormat);
3049 reset_ptr(dst2, c->dstFormat);
3051 /* reset slice direction at end of frame */
3052 if (srcSliceY + srcSliceH == c->srcH)
3055 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3057 // slices go from bottom to top => we flip the image internally
3058 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3059 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3061 src2[0] += (srcSliceH-1)*srcStride[0];
3062 if (!usePal(c->srcFormat))
3063 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3064 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3065 src2[3] += (srcSliceH-1)*srcStride[3];
3066 dst2[0] += ( c->dstH -1)*dstStride[0];
3067 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3068 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3069 dst2[3] += ( c->dstH -1)*dstStride[3];
3071 reset_ptr(src2, c->srcFormat);
3072 reset_ptr(dst2, c->dstFormat);
3074 /* reset slice direction at end of frame */
3078 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3082 #if LIBSWSCALE_VERSION_MAJOR < 1
3083 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3084 int srcSliceH, uint8_t* dst[], int dstStride[])
3086 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3090 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3091 float lumaSharpen, float chromaSharpen,
3092 float chromaHShift, float chromaVShift,
3095 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3099 if (lumaGBlur!=0.0) {
3100 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3101 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3103 filter->lumH= sws_getIdentityVec();
3104 filter->lumV= sws_getIdentityVec();
3107 if (chromaGBlur!=0.0) {
3108 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3109 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3111 filter->chrH= sws_getIdentityVec();
3112 filter->chrV= sws_getIdentityVec();
3115 if (chromaSharpen!=0.0) {
3116 SwsVector *id= sws_getIdentityVec();
3117 sws_scaleVec(filter->chrH, -chromaSharpen);
3118 sws_scaleVec(filter->chrV, -chromaSharpen);
3119 sws_addVec(filter->chrH, id);
3120 sws_addVec(filter->chrV, id);
3124 if (lumaSharpen!=0.0) {
3125 SwsVector *id= sws_getIdentityVec();
3126 sws_scaleVec(filter->lumH, -lumaSharpen);
3127 sws_scaleVec(filter->lumV, -lumaSharpen);
3128 sws_addVec(filter->lumH, id);
3129 sws_addVec(filter->lumV, id);
3133 if (chromaHShift != 0.0)
3134 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3136 if (chromaVShift != 0.0)
3137 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3139 sws_normalizeVec(filter->chrH, 1.0);
3140 sws_normalizeVec(filter->chrV, 1.0);
3141 sws_normalizeVec(filter->lumH, 1.0);
3142 sws_normalizeVec(filter->lumV, 1.0);
3144 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3145 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3150 SwsVector *sws_allocVec(int length)
3152 SwsVector *vec = av_malloc(sizeof(SwsVector));
3155 vec->length = length;
3156 vec->coeff = av_malloc(sizeof(double) * length);
3162 SwsVector *sws_getGaussianVec(double variance, double quality)
3164 const int length= (int)(variance*quality + 0.5) | 1;
3166 double middle= (length-1)*0.5;
3167 SwsVector *vec= sws_allocVec(length);
3172 for (i=0; i<length; i++) {
3173 double dist= i-middle;
3174 vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3177 sws_normalizeVec(vec, 1.0);
3182 SwsVector *sws_getConstVec(double c, int length)
3185 SwsVector *vec= sws_allocVec(length);
3190 for (i=0; i<length; i++)
3197 SwsVector *sws_getIdentityVec(void)
3199 return sws_getConstVec(1.0, 1);
3202 double sws_dcVec(SwsVector *a)
3207 for (i=0; i<a->length; i++)
3213 void sws_scaleVec(SwsVector *a, double scalar)
3217 for (i=0; i<a->length; i++)
3218 a->coeff[i]*= scalar;
3221 void sws_normalizeVec(SwsVector *a, double height)
3223 sws_scaleVec(a, height/sws_dcVec(a));
3226 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
3228 int length= a->length + b->length - 1;
3230 SwsVector *vec= sws_getConstVec(0.0, length);
3235 for (i=0; i<a->length; i++) {
3236 for (j=0; j<b->length; j++) {
3237 vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
3244 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
3246 int length= FFMAX(a->length, b->length);
3248 SwsVector *vec= sws_getConstVec(0.0, length);
3253 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3254 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3259 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
3261 int length= FFMAX(a->length, b->length);
3263 SwsVector *vec= sws_getConstVec(0.0, length);
3268 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3269 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3274 /* shift left / or right if "shift" is negative */
3275 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
3277 int length= a->length + FFABS(shift)*2;
3279 SwsVector *vec= sws_getConstVec(0.0, length);
3284 for (i=0; i<a->length; i++) {
3285 vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3291 void sws_shiftVec(SwsVector *a, int shift)
3293 SwsVector *shifted= sws_getShiftedVec(a, shift);
3295 a->coeff= shifted->coeff;
3296 a->length= shifted->length;
3300 void sws_addVec(SwsVector *a, SwsVector *b)
3302 SwsVector *sum= sws_sumVec(a, b);
3304 a->coeff= sum->coeff;
3305 a->length= sum->length;
3309 void sws_subVec(SwsVector *a, SwsVector *b)
3311 SwsVector *diff= sws_diffVec(a, b);
3313 a->coeff= diff->coeff;
3314 a->length= diff->length;
3318 void sws_convVec(SwsVector *a, SwsVector *b)
3320 SwsVector *conv= sws_getConvVec(a, b);
3322 a->coeff= conv->coeff;
3323 a->length= conv->length;
3327 SwsVector *sws_cloneVec(SwsVector *a)
3330 SwsVector *vec= sws_allocVec(a->length);
3335 for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
3340 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
3347 for (i=0; i<a->length; i++)
3348 if (a->coeff[i]>max) max= a->coeff[i];
3350 for (i=0; i<a->length; i++)
3351 if (a->coeff[i]<min) min= a->coeff[i];
3355 for (i=0; i<a->length; i++) {
3356 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3357 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3358 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3359 av_log(log_ctx, log_level, "|\n");
3363 #if LIBSWSCALE_VERSION_MAJOR < 1
3364 void sws_printVec(SwsVector *a)
3366 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3370 void sws_freeVec(SwsVector *a)
3373 av_freep(&a->coeff);
3378 void sws_freeFilter(SwsFilter *filter)
3380 if (!filter) return;
3382 if (filter->lumH) sws_freeVec(filter->lumH);
3383 if (filter->lumV) sws_freeVec(filter->lumV);
3384 if (filter->chrH) sws_freeVec(filter->chrH);
3385 if (filter->chrV) sws_freeVec(filter->chrV);
3390 void sws_freeContext(SwsContext *c)
3396 for (i=0; i<c->vLumBufSize; i++)
3397 av_freep(&c->lumPixBuf[i]);
3398 av_freep(&c->lumPixBuf);
3402 for (i=0; i<c->vChrBufSize; i++)
3403 av_freep(&c->chrPixBuf[i]);
3404 av_freep(&c->chrPixBuf);
3407 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
3408 for (i=0; i<c->vLumBufSize; i++)
3409 av_freep(&c->alpPixBuf[i]);
3410 av_freep(&c->alpPixBuf);
3413 av_freep(&c->vLumFilter);
3414 av_freep(&c->vChrFilter);
3415 av_freep(&c->hLumFilter);
3416 av_freep(&c->hChrFilter);
3417 #ifdef COMPILE_ALTIVEC
3418 av_freep(&c->vYCoeffsBank);
3419 av_freep(&c->vCCoeffsBank);
3422 av_freep(&c->vLumFilterPos);
3423 av_freep(&c->vChrFilterPos);
3424 av_freep(&c->hLumFilterPos);
3425 av_freep(&c->hChrFilterPos);
3427 #if ARCH_X86 && CONFIG_GPL
3428 #ifdef MAP_ANONYMOUS
3429 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3430 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3431 #elif HAVE_VIRTUALALLOC
3432 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3433 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3435 av_free(c->lumMmx2FilterCode);
3436 av_free(c->chrMmx2FilterCode);
3438 c->lumMmx2FilterCode=NULL;
3439 c->chrMmx2FilterCode=NULL;
3440 #endif /* ARCH_X86 && CONFIG_GPL */
3442 av_freep(&c->lumMmx2Filter);
3443 av_freep(&c->chrMmx2Filter);
3444 av_freep(&c->lumMmx2FilterPos);
3445 av_freep(&c->chrMmx2FilterPos);
3446 av_freep(&c->yuvTable);
3451 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3452 int srcW, int srcH, enum PixelFormat srcFormat,
3453 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3454 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3456 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3459 param = default_param;
3462 if (context->srcW != srcW || context->srcH != srcH ||
3463 context->srcFormat != srcFormat ||
3464 context->dstW != dstW || context->dstH != dstH ||
3465 context->dstFormat != dstFormat || context->flags != flags ||
3466 context->param[0] != param[0] || context->param[1] != param[1])
3468 sws_freeContext(context);
3473 return sws_getContext(srcW, srcH, srcFormat,
3474 dstW, dstH, dstFormat, flags,
3475 srcFilter, dstFilter, param);