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
67 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
68 #define MAP_ANONYMOUS MAP_ANON
72 #define WIN32_LEAN_AND_MEAN
76 #include "swscale_internal.h"
78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/bswap.h"
81 unsigned swscale_version(void)
83 return LIBSWSCALE_VERSION_INT;
90 //#define HAVE_AMD3DNOW
93 //#define WORDS_BIGENDIAN
96 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
98 #define RET 0xC3 //near return opcode for x86
103 #define PI 3.14159265358979323846
106 #define isSupportedIn(x) ( \
107 (x)==PIX_FMT_YUV420P \
108 || (x)==PIX_FMT_YUVA420P \
109 || (x)==PIX_FMT_YUYV422 \
110 || (x)==PIX_FMT_UYVY422 \
111 || (x)==PIX_FMT_RGB32 \
112 || (x)==PIX_FMT_RGB32_1 \
113 || (x)==PIX_FMT_BGR24 \
114 || (x)==PIX_FMT_BGR565 \
115 || (x)==PIX_FMT_BGR555 \
116 || (x)==PIX_FMT_BGR32 \
117 || (x)==PIX_FMT_BGR32_1 \
118 || (x)==PIX_FMT_RGB24 \
119 || (x)==PIX_FMT_RGB565 \
120 || (x)==PIX_FMT_RGB555 \
121 || (x)==PIX_FMT_GRAY8 \
122 || (x)==PIX_FMT_YUV410P \
123 || (x)==PIX_FMT_YUV440P \
124 || (x)==PIX_FMT_GRAY16BE \
125 || (x)==PIX_FMT_GRAY16LE \
126 || (x)==PIX_FMT_YUV444P \
127 || (x)==PIX_FMT_YUV422P \
128 || (x)==PIX_FMT_YUV411P \
129 || (x)==PIX_FMT_PAL8 \
130 || (x)==PIX_FMT_BGR8 \
131 || (x)==PIX_FMT_RGB8 \
132 || (x)==PIX_FMT_BGR4_BYTE \
133 || (x)==PIX_FMT_RGB4_BYTE \
134 || (x)==PIX_FMT_YUV440P \
135 || (x)==PIX_FMT_MONOWHITE \
136 || (x)==PIX_FMT_MONOBLACK \
138 #define isSupportedOut(x) ( \
139 (x)==PIX_FMT_YUV420P \
140 || (x)==PIX_FMT_YUVA420P \
141 || (x)==PIX_FMT_YUYV422 \
142 || (x)==PIX_FMT_UYVY422 \
143 || (x)==PIX_FMT_YUV444P \
144 || (x)==PIX_FMT_YUV422P \
145 || (x)==PIX_FMT_YUV411P \
148 || (x)==PIX_FMT_NV12 \
149 || (x)==PIX_FMT_NV21 \
150 || (x)==PIX_FMT_GRAY16BE \
151 || (x)==PIX_FMT_GRAY16LE \
152 || (x)==PIX_FMT_GRAY8 \
153 || (x)==PIX_FMT_YUV410P \
154 || (x)==PIX_FMT_YUV440P \
156 #define isPacked(x) ( \
158 || (x)==PIX_FMT_YUYV422 \
159 || (x)==PIX_FMT_UYVY422 \
163 #define usePal(x) ( \
165 || (x)==PIX_FMT_BGR4_BYTE \
166 || (x)==PIX_FMT_RGB4_BYTE \
167 || (x)==PIX_FMT_BGR8 \
168 || (x)==PIX_FMT_RGB8 \
171 #define RGB2YUV_SHIFT 15
172 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
173 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
174 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
175 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
176 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
177 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
178 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
179 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
180 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
182 extern const int32_t ff_yuv2rgb_coeffs[8][4];
184 static const double rgb2yuv_table[8][9]={
185 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
186 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
187 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
188 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
189 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
190 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
191 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
192 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
197 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
200 more intelligent misalignment avoidance for the horizontal scaler
201 write special vertical cubic upscale version
202 optimize C code (YV12 / minmax)
203 add support for packed pixel YUV input & output
204 add support for Y8 output
205 optimize BGR24 & BGR32
206 add BGR4 output support
207 write special BGR->BGR scaler
210 #if ARCH_X86 && CONFIG_GPL
211 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
212 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
213 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
214 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
215 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
216 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
217 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
218 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
220 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
221 0x0103010301030103LL,
222 0x0200020002000200LL,};
224 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
225 0x0602060206020602LL,
226 0x0004000400040004LL,};
228 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
229 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
230 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
231 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
232 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
233 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
235 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
236 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
237 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
240 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
241 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
242 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
244 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
245 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
246 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
247 #endif /* FAST_BGR2YV12 */
248 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
250 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
252 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
253 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
254 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
255 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
256 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
258 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
259 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
260 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
263 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
265 #endif /* ARCH_X86 && CONFIG_GPL */
267 // clipping helper table for C implementations:
268 static unsigned char clip_table[768];
270 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
272 static const uint8_t __attribute__((aligned(8))) dither_2x2_4[2][8]={
273 { 1, 3, 1, 3, 1, 3, 1, 3, },
274 { 2, 0, 2, 0, 2, 0, 2, 0, },
277 static const uint8_t __attribute__((aligned(8))) dither_2x2_8[2][8]={
278 { 6, 2, 6, 2, 6, 2, 6, 2, },
279 { 0, 4, 0, 4, 0, 4, 0, 4, },
282 const uint8_t __attribute__((aligned(8))) dither_8x8_32[8][8]={
283 { 17, 9, 23, 15, 16, 8, 22, 14, },
284 { 5, 29, 3, 27, 4, 28, 2, 26, },
285 { 21, 13, 19, 11, 20, 12, 18, 10, },
286 { 0, 24, 6, 30, 1, 25, 7, 31, },
287 { 16, 8, 22, 14, 17, 9, 23, 15, },
288 { 4, 28, 2, 26, 5, 29, 3, 27, },
289 { 20, 12, 18, 10, 21, 13, 19, 11, },
290 { 1, 25, 7, 31, 0, 24, 6, 30, },
294 const uint8_t __attribute__((aligned(8))) dither_8x8_64[8][8]={
295 { 0, 48, 12, 60, 3, 51, 15, 63, },
296 { 32, 16, 44, 28, 35, 19, 47, 31, },
297 { 8, 56, 4, 52, 11, 59, 7, 55, },
298 { 40, 24, 36, 20, 43, 27, 39, 23, },
299 { 2, 50, 14, 62, 1, 49, 13, 61, },
300 { 34, 18, 46, 30, 33, 17, 45, 29, },
301 { 10, 58, 6, 54, 9, 57, 5, 53, },
302 { 42, 26, 38, 22, 41, 25, 37, 21, },
306 const uint8_t __attribute__((aligned(8))) dither_8x8_73[8][8]={
307 { 0, 55, 14, 68, 3, 58, 17, 72, },
308 { 37, 18, 50, 32, 40, 22, 54, 35, },
309 { 9, 64, 5, 59, 13, 67, 8, 63, },
310 { 46, 27, 41, 23, 49, 31, 44, 26, },
311 { 2, 57, 16, 71, 1, 56, 15, 70, },
312 { 39, 21, 52, 34, 38, 19, 51, 33, },
313 { 11, 66, 7, 62, 10, 65, 6, 60, },
314 { 48, 30, 43, 25, 47, 29, 42, 24, },
318 const uint8_t __attribute__((aligned(8))) dither_8x8_128[8][8]={
319 { 68, 36, 92, 60, 66, 34, 90, 58, },
320 { 20, 116, 12, 108, 18, 114, 10, 106, },
321 { 84, 52, 76, 44, 82, 50, 74, 42, },
322 { 0, 96, 24, 120, 6, 102, 30, 126, },
323 { 64, 32, 88, 56, 70, 38, 94, 62, },
324 { 16, 112, 8, 104, 22, 118, 14, 110, },
325 { 80, 48, 72, 40, 86, 54, 78, 46, },
326 { 4, 100, 28, 124, 2, 98, 26, 122, },
331 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
332 {117, 62, 158, 103, 113, 58, 155, 100, },
333 { 34, 199, 21, 186, 31, 196, 17, 182, },
334 {144, 89, 131, 76, 141, 86, 127, 72, },
335 { 0, 165, 41, 206, 10, 175, 52, 217, },
336 {110, 55, 151, 96, 120, 65, 162, 107, },
337 { 28, 193, 14, 179, 38, 203, 24, 189, },
338 {138, 83, 124, 69, 148, 93, 134, 79, },
339 { 7, 172, 48, 213, 3, 168, 45, 210, },
342 // tries to correct a gamma of 1.5
343 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
344 { 0, 143, 18, 200, 2, 156, 25, 215, },
345 { 78, 28, 125, 64, 89, 36, 138, 74, },
346 { 10, 180, 3, 161, 16, 195, 8, 175, },
347 {109, 51, 93, 38, 121, 60, 105, 47, },
348 { 1, 152, 23, 210, 0, 147, 20, 205, },
349 { 85, 33, 134, 71, 81, 30, 130, 67, },
350 { 14, 190, 6, 171, 12, 185, 5, 166, },
351 {117, 57, 101, 44, 113, 54, 97, 41, },
354 // tries to correct a gamma of 2.0
355 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
356 { 0, 124, 8, 193, 0, 140, 12, 213, },
357 { 55, 14, 104, 42, 66, 19, 119, 52, },
358 { 3, 168, 1, 145, 6, 187, 3, 162, },
359 { 86, 31, 70, 21, 99, 39, 82, 28, },
360 { 0, 134, 11, 206, 0, 129, 9, 200, },
361 { 62, 17, 114, 48, 58, 16, 109, 45, },
362 { 5, 181, 2, 157, 4, 175, 1, 151, },
363 { 95, 36, 78, 26, 90, 34, 74, 24, },
366 // tries to correct a gamma of 2.5
367 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
368 { 0, 107, 3, 187, 0, 125, 6, 212, },
369 { 39, 7, 86, 28, 49, 11, 102, 36, },
370 { 1, 158, 0, 131, 3, 180, 1, 151, },
371 { 68, 19, 52, 12, 81, 25, 64, 17, },
372 { 0, 119, 5, 203, 0, 113, 4, 195, },
373 { 45, 9, 96, 33, 42, 8, 91, 30, },
374 { 2, 172, 1, 144, 2, 165, 0, 137, },
375 { 77, 23, 60, 15, 72, 21, 56, 14, },
379 const char *sws_format_name(enum PixelFormat format)
382 case PIX_FMT_YUV420P:
384 case PIX_FMT_YUVA420P:
386 case PIX_FMT_YUYV422:
392 case PIX_FMT_YUV422P:
394 case PIX_FMT_YUV444P:
398 case PIX_FMT_YUV410P:
400 case PIX_FMT_YUV411P:
406 case PIX_FMT_GRAY16BE:
408 case PIX_FMT_GRAY16LE:
412 case PIX_FMT_MONOWHITE:
414 case PIX_FMT_MONOBLACK:
418 case PIX_FMT_YUVJ420P:
420 case PIX_FMT_YUVJ422P:
422 case PIX_FMT_YUVJ444P:
424 case PIX_FMT_XVMC_MPEG2_MC:
425 return "xvmc_mpeg2_mc";
426 case PIX_FMT_XVMC_MPEG2_IDCT:
427 return "xvmc_mpeg2_idct";
428 case PIX_FMT_UYVY422:
430 case PIX_FMT_UYYVYY411:
432 case PIX_FMT_RGB32_1:
434 case PIX_FMT_BGR32_1:
446 case PIX_FMT_BGR4_BYTE:
452 case PIX_FMT_RGB4_BYTE:
458 case PIX_FMT_YUV440P:
460 case PIX_FMT_VDPAU_H264:
462 case PIX_FMT_VDPAU_MPEG1:
463 return "vdpau_mpeg1";
464 case PIX_FMT_VDPAU_MPEG2:
465 return "vdpau_mpeg2";
466 case PIX_FMT_VDPAU_WMV3:
468 case PIX_FMT_VDPAU_VC1:
471 return "Unknown format";
475 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
476 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
477 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++)
485 for (j=0; j<lumFilterSize; j++)
486 val += lumSrc[j][i] * lumFilter[j];
488 dest[i]= av_clip_uint8(val>>19);
492 for (i=0; i<chrDstW; i++)
497 for (j=0; j<chrFilterSize; j++)
499 u += chrSrc[j][i] * chrFilter[j];
500 v += chrSrc[j][i + VOFW] * chrFilter[j];
503 uDest[i]= av_clip_uint8(u>>19);
504 vDest[i]= av_clip_uint8(v>>19);
507 if (CONFIG_SWSCALE_ALPHA && aDest)
508 for (i=0; i<dstW; i++){
511 for (j=0; j<lumFilterSize; j++)
512 val += alpSrc[j][i] * lumFilter[j];
514 aDest[i]= av_clip_uint8(val>>19);
519 static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
520 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
521 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
523 //FIXME Optimize (just quickly written not optimized..)
525 for (i=0; i<dstW; i++)
529 for (j=0; j<lumFilterSize; j++)
530 val += lumSrc[j][i] * lumFilter[j];
532 dest[i]= av_clip_uint8(val>>19);
538 if (dstFormat == PIX_FMT_NV12)
539 for (i=0; i<chrDstW; i++)
544 for (j=0; j<chrFilterSize; j++)
546 u += chrSrc[j][i] * chrFilter[j];
547 v += chrSrc[j][i + VOFW] * chrFilter[j];
550 uDest[2*i]= av_clip_uint8(u>>19);
551 uDest[2*i+1]= av_clip_uint8(v>>19);
554 for (i=0; i<chrDstW; i++)
559 for (j=0; j<chrFilterSize; j++)
561 u += chrSrc[j][i] * chrFilter[j];
562 v += chrSrc[j][i + VOFW] * chrFilter[j];
565 uDest[2*i]= av_clip_uint8(v>>19);
566 uDest[2*i+1]= av_clip_uint8(u>>19);
570 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
571 for (i=0; i<(dstW>>1); i++){\
577 int av_unused A1, A2;\
578 type av_unused *r, *b, *g;\
581 for (j=0; j<lumFilterSize; j++)\
583 Y1 += lumSrc[j][i2] * lumFilter[j];\
584 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
586 for (j=0; j<chrFilterSize; j++)\
588 U += chrSrc[j][i] * chrFilter[j];\
589 V += chrSrc[j][i+VOFW] * chrFilter[j];\
598 for (j=0; j<lumFilterSize; j++){\
599 A1 += alpSrc[j][i2 ] * lumFilter[j];\
600 A2 += alpSrc[j][i2+1] * lumFilter[j];\
606 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
607 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
608 if ((Y1|Y2|U|V)&256)\
610 if (Y1>255) Y1=255; \
611 else if (Y1<0)Y1=0; \
612 if (Y2>255) Y2=255; \
613 else if (Y2<0)Y2=0; \
619 if (alpha && ((A1|A2)&256)){\
620 A1=av_clip_uint8(A1);\
621 A2=av_clip_uint8(A2);\
624 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
625 for (i=0; i<dstW; i++){\
633 for (j=0; j<lumFilterSize; j++){\
634 Y += lumSrc[j][i ] * lumFilter[j];\
636 for (j=0; j<chrFilterSize; j++){\
637 U += chrSrc[j][i ] * chrFilter[j];\
638 V += chrSrc[j][i+VOFW] * chrFilter[j];\
645 for (j=0; j<lumFilterSize; j++)\
646 A += alpSrc[j][i ] * lumFilter[j];\
649 A = av_clip_uint8(A);\
652 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
653 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
654 Y-= c->yuv2rgb_y_offset;\
655 Y*= c->yuv2rgb_y_coeff;\
657 R= Y + V*c->yuv2rgb_v2r_coeff;\
658 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
659 B= Y + U*c->yuv2rgb_u2b_coeff;\
660 if ((R|G|B)&(0xC0000000)){\
661 if (R>=(256<<22)) R=(256<<22)-1; \
663 if (G>=(256<<22)) G=(256<<22)-1; \
665 if (B>=(256<<22)) B=(256<<22)-1; \
670 #define YSCALE_YUV_2_GRAY16_C \
671 for (i=0; i<(dstW>>1); i++){\
680 for (j=0; j<lumFilterSize; j++)\
682 Y1 += lumSrc[j][i2] * lumFilter[j];\
683 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
687 if ((Y1|Y2|U|V)&65536)\
689 if (Y1>65535) Y1=65535; \
690 else if (Y1<0)Y1=0; \
691 if (Y2>65535) Y2=65535; \
692 else if (Y2<0)Y2=0; \
695 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
696 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
697 r = (type *)c->table_rV[V]; \
698 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
699 b = (type *)c->table_bU[U]; \
701 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
702 for (i=0; i<(dstW>>1); i++){ \
704 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
705 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
706 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
707 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
708 type av_unused *r, *b, *g; \
709 int av_unused A1, A2; \
711 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
712 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
715 #define YSCALE_YUV_2_GRAY16_2_C \
716 for (i=0; i<(dstW>>1); i++){ \
718 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
719 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
721 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
722 YSCALE_YUV_2_PACKED2_C(type,alpha)\
723 r = (type *)c->table_rV[V];\
724 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
725 b = (type *)c->table_bU[U];\
727 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
728 for (i=0; i<(dstW>>1); i++){\
730 int Y1= buf0[i2 ]>>7;\
731 int Y2= buf0[i2+1]>>7;\
732 int U= (uvbuf1[i ])>>7;\
733 int V= (uvbuf1[i+VOFW])>>7;\
734 type av_unused *r, *b, *g;\
735 int av_unused A1, A2;\
741 #define YSCALE_YUV_2_GRAY16_1_C \
742 for (i=0; i<(dstW>>1); i++){\
744 int Y1= buf0[i2 ]<<1;\
745 int Y2= buf0[i2+1]<<1;\
747 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
748 YSCALE_YUV_2_PACKED1_C(type,alpha)\
749 r = (type *)c->table_rV[V];\
750 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
751 b = (type *)c->table_bU[U];\
753 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
754 for (i=0; i<(dstW>>1); i++){\
756 int Y1= buf0[i2 ]>>7;\
757 int Y2= buf0[i2+1]>>7;\
758 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
759 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
760 type av_unused *r, *b, *g;\
761 int av_unused A1, A2;\
767 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
768 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
769 r = (type *)c->table_rV[V];\
770 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
771 b = (type *)c->table_bU[U];\
773 #define YSCALE_YUV_2_MONO2_C \
774 const uint8_t * const d128=dither_8x8_220[y&7];\
775 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
776 for (i=0; i<dstW-7; i+=8){\
778 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
779 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
780 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
781 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
782 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
783 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
784 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
785 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
786 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
791 #define YSCALE_YUV_2_MONOX_C \
792 const uint8_t * const d128=dither_8x8_220[y&7];\
793 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
795 for (i=0; i<dstW-1; i+=2){\
800 for (j=0; j<lumFilterSize; j++)\
802 Y1 += lumSrc[j][i] * lumFilter[j];\
803 Y2 += lumSrc[j][i+1] * lumFilter[j];\
814 acc+= acc + g[Y1+d128[(i+0)&7]];\
815 acc+= acc + g[Y2+d128[(i+1)&7]];\
817 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
823 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
824 switch(c->dstFormat)\
829 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
830 func(uint32_t,needAlpha)\
831 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
832 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
835 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
837 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
838 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
842 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
843 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
851 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
852 func(uint32_t,needAlpha)\
853 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
854 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
857 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
859 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
860 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
864 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
865 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
872 ((uint8_t*)dest)[0]= r[Y1];\
873 ((uint8_t*)dest)[1]= g[Y1];\
874 ((uint8_t*)dest)[2]= b[Y1];\
875 ((uint8_t*)dest)[3]= r[Y2];\
876 ((uint8_t*)dest)[4]= g[Y2];\
877 ((uint8_t*)dest)[5]= b[Y2];\
883 ((uint8_t*)dest)[0]= b[Y1];\
884 ((uint8_t*)dest)[1]= g[Y1];\
885 ((uint8_t*)dest)[2]= r[Y1];\
886 ((uint8_t*)dest)[3]= b[Y2];\
887 ((uint8_t*)dest)[4]= g[Y2];\
888 ((uint8_t*)dest)[5]= r[Y2];\
892 case PIX_FMT_RGB565:\
893 case PIX_FMT_BGR565:\
895 const int dr1= dither_2x2_8[y&1 ][0];\
896 const int dg1= dither_2x2_4[y&1 ][0];\
897 const int db1= dither_2x2_8[(y&1)^1][0];\
898 const int dr2= dither_2x2_8[y&1 ][1];\
899 const int dg2= dither_2x2_4[y&1 ][1];\
900 const int db2= dither_2x2_8[(y&1)^1][1];\
902 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
903 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
907 case PIX_FMT_RGB555:\
908 case PIX_FMT_BGR555:\
910 const int dr1= dither_2x2_8[y&1 ][0];\
911 const int dg1= dither_2x2_8[y&1 ][1];\
912 const int db1= dither_2x2_8[(y&1)^1][0];\
913 const int dr2= dither_2x2_8[y&1 ][1];\
914 const int dg2= dither_2x2_8[y&1 ][0];\
915 const int db2= dither_2x2_8[(y&1)^1][1];\
917 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
918 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
925 const uint8_t * const d64= dither_8x8_73[y&7];\
926 const uint8_t * const d32= dither_8x8_32[y&7];\
928 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
929 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
936 const uint8_t * const d64= dither_8x8_73 [y&7];\
937 const uint8_t * const d128=dither_8x8_220[y&7];\
939 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
940 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
944 case PIX_FMT_RGB4_BYTE:\
945 case PIX_FMT_BGR4_BYTE:\
947 const uint8_t * const d64= dither_8x8_73 [y&7];\
948 const uint8_t * const d128=dither_8x8_220[y&7];\
950 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
951 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
955 case PIX_FMT_MONOBLACK:\
956 case PIX_FMT_MONOWHITE:\
961 case PIX_FMT_YUYV422:\
963 ((uint8_t*)dest)[2*i2+0]= Y1;\
964 ((uint8_t*)dest)[2*i2+1]= U;\
965 ((uint8_t*)dest)[2*i2+2]= Y2;\
966 ((uint8_t*)dest)[2*i2+3]= V;\
969 case PIX_FMT_UYVY422:\
971 ((uint8_t*)dest)[2*i2+0]= U;\
972 ((uint8_t*)dest)[2*i2+1]= Y1;\
973 ((uint8_t*)dest)[2*i2+2]= V;\
974 ((uint8_t*)dest)[2*i2+3]= Y2;\
977 case PIX_FMT_GRAY16BE:\
979 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
980 ((uint8_t*)dest)[2*i2+1]= Y1;\
981 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
982 ((uint8_t*)dest)[2*i2+3]= Y2;\
985 case PIX_FMT_GRAY16LE:\
987 ((uint8_t*)dest)[2*i2+0]= Y1;\
988 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
989 ((uint8_t*)dest)[2*i2+2]= Y2;\
990 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
996 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
997 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
998 int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1001 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)
1004 static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
1005 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
1006 int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1009 int step= fmt_depth(c->dstFormat)/8;
1012 switch(c->dstFormat){
1020 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1021 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1022 dest[aidx]= needAlpha ? A : 255;
1029 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1030 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1038 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1055 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1056 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1057 dest[aidx]= needAlpha ? A : 255;
1064 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1065 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1073 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1088 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1090 uint8_t *ptr = plane + stride*y;
1091 for (i=0; i<height; i++){
1092 memset(ptr, val, width);
1097 //Note: we have C, X86, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1099 #if !HAVE_MMX || defined (RUNTIME_CPUDETECT) || !CONFIG_GPL
1104 #if (HAVE_ALTIVEC || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1106 #define COMPILE_ALTIVEC
1112 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1116 #if (HAVE_MMX2 || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1117 #define COMPILE_MMX2
1120 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
1121 #define COMPILE_3DNOW
1127 #undef HAVE_AMD3DNOW
1131 #define HAVE_AMD3DNOW 0
1132 #define HAVE_ALTIVEC 0
1135 #define RENAME(a) a ## _C
1136 #include "swscale_template.c"
1139 #ifdef COMPILE_ALTIVEC
1142 #define HAVE_ALTIVEC 1
1143 #define RENAME(a) a ## _altivec
1144 #include "swscale_template.c"
1154 #undef HAVE_AMD3DNOW
1156 #define RENAME(a) a ## _X86
1157 #include "swscale_template.c"
1164 #undef HAVE_AMD3DNOW
1167 #define HAVE_AMD3DNOW 0
1168 #define RENAME(a) a ## _MMX
1169 #include "swscale_template.c"
1177 #undef HAVE_AMD3DNOW
1180 #define HAVE_AMD3DNOW 0
1181 #define RENAME(a) a ## _MMX2
1182 #include "swscale_template.c"
1186 #ifdef COMPILE_3DNOW
1190 #undef HAVE_AMD3DNOW
1193 #define HAVE_AMD3DNOW 1
1194 #define RENAME(a) a ## _3DNow
1195 #include "swscale_template.c"
1200 // minor note: the HAVE_xyz are messed up after this line so don't use them
1202 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1204 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1205 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1206 else return getSplineCoeff( 0.0,
1213 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1214 int srcW, int dstW, int filterAlign, int one, int flags,
1215 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1221 int64_t *filter=NULL;
1222 int64_t *filter2=NULL;
1223 const int64_t fone= 1LL<<54;
1226 if (flags & SWS_CPU_CAPS_MMX)
1227 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1230 // NOTE: the +1 is for the MMX scaler which reads over the end
1231 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1233 if (FFABS(xInc - 0x10000) <10) // unscaled
1237 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1239 for (i=0; i<dstW; i++)
1241 filter[i*filterSize]= fone;
1246 else if (flags&SWS_POINT) // lame looking point sampling mode
1251 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1253 xDstInSrc= xInc/2 - 0x8000;
1254 for (i=0; i<dstW; i++)
1256 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1258 (*filterPos)[i]= xx;
1263 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1267 if (flags&SWS_BICUBIC) filterSize= 4;
1268 else if (flags&SWS_X ) filterSize= 4;
1269 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1270 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1272 xDstInSrc= xInc/2 - 0x8000;
1273 for (i=0; i<dstW; i++)
1275 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1278 (*filterPos)[i]= xx;
1279 //bilinear upscale / linear interpolate / area averaging
1280 for (j=0; j<filterSize; j++)
1282 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1283 if (coeff<0) coeff=0;
1284 filter[i*filterSize + j]= coeff;
1295 if (flags&SWS_BICUBIC) sizeFactor= 4;
1296 else if (flags&SWS_X) sizeFactor= 8;
1297 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1298 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1299 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1300 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1301 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1302 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1304 sizeFactor= 0; //GCC warning killer
1308 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1309 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1311 if (filterSize > srcW-2) filterSize=srcW-2;
1313 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1315 xDstInSrc= xInc - 0x10000;
1316 for (i=0; i<dstW; i++)
1318 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1320 (*filterPos)[i]= xx;
1321 for (j=0; j<filterSize; j++)
1323 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1329 floatd= d * (1.0/(1<<30));
1331 if (flags & SWS_BICUBIC)
1333 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1334 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1335 int64_t dd = ( d*d)>>30;
1336 int64_t ddd= (dd*d)>>30;
1339 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1340 else if (d < 1LL<<31)
1341 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1344 coeff *= fone>>(30+24);
1346 /* else if (flags & SWS_X)
1348 double p= param ? param*0.01 : 0.3;
1349 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1350 coeff*= pow(2.0, - p*d*d);
1352 else if (flags & SWS_X)
1354 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1361 if (c<0.0) c= -pow(-c, A);
1363 coeff= (c*0.5 + 0.5)*fone;
1365 else if (flags & SWS_AREA)
1367 int64_t d2= d - (1<<29);
1368 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1369 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1371 coeff *= fone>>(30+16);
1373 else if (flags & SWS_GAUSS)
1375 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1376 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1378 else if (flags & SWS_SINC)
1380 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1382 else if (flags & SWS_LANCZOS)
1384 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1385 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1386 if (floatd>p) coeff=0;
1388 else if (flags & SWS_BILINEAR)
1391 if (coeff<0) coeff=0;
1392 coeff *= fone >> 30;
1394 else if (flags & SWS_SPLINE)
1396 double p=-2.196152422706632;
1397 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1400 coeff= 0.0; //GCC warning killer
1404 filter[i*filterSize + j]= coeff;
1411 /* apply src & dst Filter to filter -> filter2
1414 assert(filterSize>0);
1415 filter2Size= filterSize;
1416 if (srcFilter) filter2Size+= srcFilter->length - 1;
1417 if (dstFilter) filter2Size+= dstFilter->length - 1;
1418 assert(filter2Size>0);
1419 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1421 for (i=0; i<dstW; i++)
1426 for (k=0; k<srcFilter->length; k++){
1427 for (j=0; j<filterSize; j++)
1428 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1431 for (j=0; j<filterSize; j++)
1432 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1436 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1440 /* try to reduce the filter-size (step1 find size and shift left) */
1441 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1443 for (i=dstW-1; i>=0; i--)
1445 int min= filter2Size;
1449 /* get rid off near zero elements on the left by shifting left */
1450 for (j=0; j<filter2Size; j++)
1453 cutOff += FFABS(filter2[i*filter2Size]);
1455 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1457 /* preserve monotonicity because the core can't handle the filter otherwise */
1458 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1460 // move filter coefficients left
1461 for (k=1; k<filter2Size; k++)
1462 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1463 filter2[i*filter2Size + k - 1]= 0;
1468 /* count near zeros on the right */
1469 for (j=filter2Size-1; j>0; j--)
1471 cutOff += FFABS(filter2[i*filter2Size + j]);
1473 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1477 if (min>minFilterSize) minFilterSize= min;
1480 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1481 // we can handle the special case 4,
1482 // so we don't want to go to the full 8
1483 if (minFilterSize < 5)
1486 // We really don't want to waste our time
1487 // doing useless computation, so fall back on
1488 // the scalar C code for very small filters.
1489 // Vectorizing is worth it only if you have a
1490 // decent-sized vector.
1491 if (minFilterSize < 3)
1495 if (flags & SWS_CPU_CAPS_MMX) {
1496 // special case for unscaled vertical filtering
1497 if (minFilterSize == 1 && filterAlign == 2)
1501 assert(minFilterSize > 0);
1502 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1503 assert(filterSize > 0);
1504 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1505 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1507 *outFilterSize= filterSize;
1509 if (flags&SWS_PRINT_INFO)
1510 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1511 /* try to reduce the filter-size (step2 reduce it) */
1512 for (i=0; i<dstW; i++)
1516 for (j=0; j<filterSize; j++)
1518 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1519 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1520 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1521 filter[i*filterSize + j]= 0;
1526 //FIXME try to align filterPos if possible
1529 for (i=0; i<dstW; i++)
1532 if ((*filterPos)[i] < 0)
1534 // move filter coefficients left to compensate for filterPos
1535 for (j=1; j<filterSize; j++)
1537 int left= FFMAX(j + (*filterPos)[i], 0);
1538 filter[i*filterSize + left] += filter[i*filterSize + j];
1539 filter[i*filterSize + j]=0;
1544 if ((*filterPos)[i] + filterSize > srcW)
1546 int shift= (*filterPos)[i] + filterSize - srcW;
1547 // move filter coefficients right to compensate for filterPos
1548 for (j=filterSize-2; j>=0; j--)
1550 int right= FFMIN(j + shift, filterSize-1);
1551 filter[i*filterSize +right] += filter[i*filterSize +j];
1552 filter[i*filterSize +j]=0;
1554 (*filterPos)[i]= srcW - filterSize;
1558 // Note the +1 is for the MMX scaler which reads over the end
1559 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1560 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1562 /* normalize & store in outFilter */
1563 for (i=0; i<dstW; i++)
1569 for (j=0; j<filterSize; j++)
1571 sum+= filter[i*filterSize + j];
1573 sum= (sum + one/2)/ one;
1574 for (j=0; j<*outFilterSize; j++)
1576 int64_t v= filter[i*filterSize + j] + error;
1577 int intV= ROUNDED_DIV(v, sum);
1578 (*outFilter)[i*(*outFilterSize) + j]= intV;
1579 error= v - intV*sum;
1583 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1584 for (i=0; i<*outFilterSize; i++)
1586 int j= dstW*(*outFilterSize);
1587 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1598 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1601 x86_reg imm8OfPShufW1A;
1602 x86_reg imm8OfPShufW2A;
1603 x86_reg fragmentLengthA;
1605 x86_reg imm8OfPShufW1B;
1606 x86_reg imm8OfPShufW2B;
1607 x86_reg fragmentLengthB;
1612 // create an optimized horizontal scaling routine
1620 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1621 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1622 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1623 "punpcklbw %%mm7, %%mm1 \n\t"
1624 "punpcklbw %%mm7, %%mm0 \n\t"
1625 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1627 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1629 "psubw %%mm1, %%mm0 \n\t"
1630 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1631 "pmullw %%mm3, %%mm0 \n\t"
1632 "psllw $7, %%mm1 \n\t"
1633 "paddw %%mm1, %%mm0 \n\t"
1635 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1637 "add $8, %%"REG_a" \n\t"
1641 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1642 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1643 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1648 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1652 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1653 "=r" (fragmentLengthA)
1660 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1661 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1662 "punpcklbw %%mm7, %%mm0 \n\t"
1663 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1665 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1667 "psubw %%mm1, %%mm0 \n\t"
1668 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1669 "pmullw %%mm3, %%mm0 \n\t"
1670 "psllw $7, %%mm1 \n\t"
1671 "paddw %%mm1, %%mm0 \n\t"
1673 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1675 "add $8, %%"REG_a" \n\t"
1679 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1680 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1681 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1686 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1690 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1691 "=r" (fragmentLengthB)
1694 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1697 for (i=0; i<dstW/numSplits; i++)
1704 int b=((xpos+xInc)>>16) - xx;
1705 int c=((xpos+xInc*2)>>16) - xx;
1706 int d=((xpos+xInc*3)>>16) - xx;
1708 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1709 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1710 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1711 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1716 int maxShift= 3-(d+1);
1719 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1721 funnyCode[fragmentPos + imm8OfPShufW1B]=
1722 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1723 funnyCode[fragmentPos + imm8OfPShufW2B]=
1724 a | (b<<2) | (c<<4) | (d<<6);
1726 if (i+3>=dstW) shift=maxShift; //avoid overread
1727 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1729 if (shift && i>=shift)
1731 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1732 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1733 filterPos[i/2]-=shift;
1736 fragmentPos+= fragmentLengthB;
1743 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1745 funnyCode[fragmentPos + imm8OfPShufW1A]=
1746 funnyCode[fragmentPos + imm8OfPShufW2A]=
1747 a | (b<<2) | (c<<4) | (d<<6);
1749 if (i+4>=dstW) shift=maxShift; //avoid overread
1750 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1752 if (shift && i>=shift)
1754 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1755 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1756 filterPos[i/2]-=shift;
1759 fragmentPos+= fragmentLengthA;
1762 funnyCode[fragmentPos]= RET;
1766 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1768 #endif /* COMPILE_MMX2 */
1770 static void globalInit(void){
1771 // generating tables:
1773 for (i=0; i<768; i++){
1774 int c= av_clip_uint8(i-256);
1779 static SwsFunc getSwsFunc(int flags){
1781 #if defined(RUNTIME_CPUDETECT) && CONFIG_GPL
1783 // ordered per speed fastest first
1784 if (flags & SWS_CPU_CAPS_MMX2)
1785 return swScale_MMX2;
1786 else if (flags & SWS_CPU_CAPS_3DNOW)
1787 return swScale_3DNow;
1788 else if (flags & SWS_CPU_CAPS_MMX)
1795 if (flags & SWS_CPU_CAPS_ALTIVEC)
1796 return swScale_altivec;
1801 #endif /* ARCH_X86 */
1802 #else //RUNTIME_CPUDETECT
1804 return swScale_MMX2;
1806 return swScale_3DNow;
1810 return swScale_altivec;
1814 #endif //!RUNTIME_CPUDETECT
1817 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1818 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1819 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1821 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1822 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1826 uint8_t *srcPtr= src[0];
1827 uint8_t *dstPtr= dst;
1828 for (i=0; i<srcSliceH; i++)
1830 memcpy(dstPtr, srcPtr, c->srcW);
1831 srcPtr+= srcStride[0];
1832 dstPtr+= dstStride[0];
1835 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1836 if (c->dstFormat == PIX_FMT_NV12)
1837 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1839 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1844 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1845 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1846 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1848 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1853 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1854 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1855 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1857 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1862 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1863 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1864 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1866 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1871 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1872 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1873 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1875 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1880 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1881 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1882 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1883 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1884 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1886 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1891 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1892 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1893 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1894 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1895 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1897 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1902 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1903 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1904 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1905 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1906 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1908 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1913 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1914 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1915 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1916 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1917 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1919 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1924 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1925 int srcSliceH, uint8_t* dst[], int dstStride[]){
1926 const enum PixelFormat srcFormat= c->srcFormat;
1927 const enum PixelFormat dstFormat= c->dstFormat;
1928 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1929 const uint8_t *palette)=NULL;
1931 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1932 uint8_t *srcPtr= src[0];
1934 if (!usePal(srcFormat))
1935 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1936 sws_format_name(srcFormat), sws_format_name(dstFormat));
1939 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1940 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1941 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1942 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1943 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1944 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
1945 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1946 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1950 for (i=0; i<srcSliceH; i++) {
1951 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1952 srcPtr+= srcStride[0];
1953 dstPtr+= dstStride[0];
1959 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1960 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1961 int srcSliceH, uint8_t* dst[], int dstStride[]){
1962 const enum PixelFormat srcFormat= c->srcFormat;
1963 const enum PixelFormat dstFormat= c->dstFormat;
1964 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1965 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1966 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1967 const int dstId= fmt_depth(dstFormat) >> 2;
1968 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1971 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1972 || (isRGB(srcFormat) && isRGB(dstFormat))){
1973 switch(srcId | (dstId<<4)){
1974 case 0x34: conv= rgb16to15; break;
1975 case 0x36: conv= rgb24to15; break;
1976 case 0x38: conv= rgb32to15; break;
1977 case 0x43: conv= rgb15to16; break;
1978 case 0x46: conv= rgb24to16; break;
1979 case 0x48: conv= rgb32to16; break;
1980 case 0x63: conv= rgb15to24; break;
1981 case 0x64: conv= rgb16to24; break;
1982 case 0x68: conv= rgb32to24; break;
1983 case 0x83: conv= rgb15to32; break;
1984 case 0x84: conv= rgb16to32; break;
1985 case 0x86: conv= rgb24to32; break;
1986 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1987 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1989 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1990 || (isRGB(srcFormat) && isBGR(dstFormat))){
1991 switch(srcId | (dstId<<4)){
1992 case 0x33: conv= rgb15tobgr15; break;
1993 case 0x34: conv= rgb16tobgr15; break;
1994 case 0x36: conv= rgb24tobgr15; break;
1995 case 0x38: conv= rgb32tobgr15; break;
1996 case 0x43: conv= rgb15tobgr16; break;
1997 case 0x44: conv= rgb16tobgr16; break;
1998 case 0x46: conv= rgb24tobgr16; break;
1999 case 0x48: conv= rgb32tobgr16; break;
2000 case 0x63: conv= rgb15tobgr24; break;
2001 case 0x64: conv= rgb16tobgr24; break;
2002 case 0x66: conv= rgb24tobgr24; break;
2003 case 0x68: conv= rgb32tobgr24; break;
2004 case 0x83: conv= rgb15tobgr32; break;
2005 case 0x84: conv= rgb16tobgr32; break;
2006 case 0x86: conv= rgb24tobgr32; break;
2007 case 0x88: conv= rgb32tobgr32; break;
2008 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2009 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2012 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2013 sws_format_name(srcFormat), sws_format_name(dstFormat));
2018 uint8_t *srcPtr= src[0];
2019 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2020 srcPtr += ALT32_CORR;
2022 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2023 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2027 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2029 for (i=0; i<srcSliceH; i++)
2031 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2032 srcPtr+= srcStride[0];
2033 dstPtr+= dstStride[0];
2040 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2041 int srcSliceH, uint8_t* dst[], int dstStride[]){
2045 dst[0]+ srcSliceY *dstStride[0],
2046 dst[1]+(srcSliceY>>1)*dstStride[1],
2047 dst[2]+(srcSliceY>>1)*dstStride[2],
2049 dstStride[0], dstStride[1], srcStride[0]);
2053 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2054 int srcSliceH, uint8_t* dst[], int dstStride[]){
2058 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2059 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2061 uint8_t *srcPtr= src[0];
2062 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2064 for (i=0; i<srcSliceH; i++)
2066 memcpy(dstPtr, srcPtr, c->srcW);
2067 srcPtr+= srcStride[0];
2068 dstPtr+= dstStride[0];
2072 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2073 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
2074 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
2076 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
2077 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
2082 /* unscaled copy like stuff (assumes nearly identical formats) */
2083 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2084 int srcSliceH, uint8_t* dst[], int dstStride[])
2086 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2087 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2091 uint8_t *srcPtr= src[0];
2092 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2095 /* universal length finder */
2096 while(length+c->srcW <= FFABS(dstStride[0])
2097 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2100 for (i=0; i<srcSliceH; i++)
2102 memcpy(dstPtr, srcPtr, length);
2103 srcPtr+= srcStride[0];
2104 dstPtr+= dstStride[0];
2110 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2111 int srcSliceH, uint8_t* dst[], int dstStride[])
2114 for (plane=0; plane<4; plane++)
2116 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2117 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2118 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2120 if (!dst[plane]) continue;
2121 // ignore palette for GRAY8
2122 if (plane == 1 && !dst[2]) continue;
2123 if (!src[plane] || (plane == 1 && !src[2]))
2124 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2127 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2128 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2132 uint8_t *srcPtr= src[plane];
2133 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2134 for (i=0; i<height; i++)
2136 memcpy(dstPtr, srcPtr, length);
2137 srcPtr+= srcStride[plane];
2138 dstPtr+= dstStride[plane];
2146 static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2147 int srcSliceH, uint8_t* dst[], int dstStride[]){
2149 int length= c->srcW;
2151 int height= srcSliceH;
2153 uint8_t *srcPtr= src[0];
2154 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2156 if (!isGray(c->dstFormat)){
2157 int height= -((-srcSliceH)>>c->chrDstVSubSample);
2158 memset(dst[1], 128, dstStride[1]*height);
2159 memset(dst[2], 128, dstStride[2]*height);
2161 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
2162 for (i=0; i<height; i++)
2164 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2165 srcPtr+= srcStride[0];
2166 dstPtr+= dstStride[0];
2171 static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2172 int srcSliceH, uint8_t* dst[], int dstStride[]){
2174 int length= c->srcW;
2176 int height= srcSliceH;
2178 uint8_t *srcPtr= src[0];
2179 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2180 for (i=0; i<height; i++)
2182 for (j=0; j<length; j++)
2184 dstPtr[j<<1] = srcPtr[j];
2185 dstPtr[(j<<1)+1] = srcPtr[j];
2187 srcPtr+= srcStride[0];
2188 dstPtr+= dstStride[0];
2193 static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2194 int srcSliceH, uint8_t* dst[], int dstStride[]){
2196 int length= c->srcW;
2198 int height= srcSliceH;
2200 uint16_t *srcPtr= (uint16_t*)src[0];
2201 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
2202 for (i=0; i<height; i++)
2204 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
2205 srcPtr+= srcStride[0]/2;
2206 dstPtr+= dstStride[0]/2;
2212 static void getSubSampleFactors(int *h, int *v, int format){
2214 case PIX_FMT_UYVY422:
2215 case PIX_FMT_YUYV422:
2219 case PIX_FMT_YUV420P:
2220 case PIX_FMT_YUVA420P:
2221 case PIX_FMT_GRAY16BE:
2222 case PIX_FMT_GRAY16LE:
2223 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2229 case PIX_FMT_YUV440P:
2233 case PIX_FMT_YUV410P:
2237 case PIX_FMT_YUV444P:
2241 case PIX_FMT_YUV422P:
2245 case PIX_FMT_YUV411P:
2256 static uint16_t roundToInt16(int64_t f){
2257 int r= (f + (1<<15))>>16;
2258 if (r<-0x7FFF) return 0x8000;
2259 else if (r> 0x7FFF) return 0x7FFF;
2264 * @param inv_table the yuv2rgb coefficients, normally ff_yuv2rgb_coeffs[x]
2265 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
2266 * @return -1 if not supported
2268 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2269 int64_t crv = inv_table[0];
2270 int64_t cbu = inv_table[1];
2271 int64_t cgu = -inv_table[2];
2272 int64_t cgv = -inv_table[3];
2276 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2277 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2279 c->brightness= brightness;
2280 c->contrast = contrast;
2281 c->saturation= saturation;
2282 c->srcRange = srcRange;
2283 c->dstRange = dstRange;
2284 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
2286 c->uOffset= 0x0400040004000400LL;
2287 c->vOffset= 0x0400040004000400LL;
2293 crv= (crv*224) / 255;
2294 cbu= (cbu*224) / 255;
2295 cgu= (cgu*224) / 255;
2296 cgv= (cgv*224) / 255;
2299 cy = (cy *contrast )>>16;
2300 crv= (crv*contrast * saturation)>>32;
2301 cbu= (cbu*contrast * saturation)>>32;
2302 cgu= (cgu*contrast * saturation)>>32;
2303 cgv= (cgv*contrast * saturation)>>32;
2305 oy -= 256*brightness;
2307 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2308 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2309 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2310 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2311 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2312 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2314 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2315 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2316 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2317 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2318 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2319 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2321 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2324 #ifdef COMPILE_ALTIVEC
2325 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2326 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2332 * @return -1 if not supported
2334 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2335 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2337 *inv_table = c->srcColorspaceTable;
2338 *table = c->dstColorspaceTable;
2339 *srcRange = c->srcRange;
2340 *dstRange = c->dstRange;
2341 *brightness= c->brightness;
2342 *contrast = c->contrast;
2343 *saturation= c->saturation;
2348 static int handle_jpeg(enum PixelFormat *format)
2351 case PIX_FMT_YUVJ420P:
2352 *format = PIX_FMT_YUV420P;
2354 case PIX_FMT_YUVJ422P:
2355 *format = PIX_FMT_YUV422P;
2357 case PIX_FMT_YUVJ444P:
2358 *format = PIX_FMT_YUV444P;
2360 case PIX_FMT_YUVJ440P:
2361 *format = PIX_FMT_YUV440P;
2368 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2369 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2373 int usesVFilter, usesHFilter;
2374 int unscaled, needsDither;
2375 int srcRange, dstRange;
2376 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2378 if (flags & SWS_CPU_CAPS_MMX)
2379 __asm__ volatile("emms\n\t"::: "memory");
2382 #if !defined(RUNTIME_CPUDETECT) || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
2383 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2385 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2387 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2389 flags |= SWS_CPU_CAPS_MMX;
2391 flags |= SWS_CPU_CAPS_ALTIVEC;
2393 flags |= SWS_CPU_CAPS_BFIN;
2395 #endif /* RUNTIME_CPUDETECT */
2396 if (clip_table[512] != 255) globalInit();
2397 if (!rgb15to16) sws_rgb2rgb_init(flags);
2399 unscaled = (srcW == dstW && srcH == dstH);
2400 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2401 && (fmt_depth(dstFormat))<24
2402 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2404 srcRange = handle_jpeg(&srcFormat);
2405 dstRange = handle_jpeg(&dstFormat);
2407 if (!isSupportedIn(srcFormat))
2409 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2412 if (!isSupportedOut(dstFormat))
2414 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2418 i= flags & ( SWS_POINT
2429 if(!i || (i & (i-1)))
2431 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2436 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
2438 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2439 srcW, srcH, dstW, dstH);
2442 if(srcW > VOFW || dstW > VOFW){
2443 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2447 if (!dstFilter) dstFilter= &dummyFilter;
2448 if (!srcFilter) srcFilter= &dummyFilter;
2450 c= av_mallocz(sizeof(SwsContext));
2452 c->av_class = &sws_context_class;
2457 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2458 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2460 c->dstFormat= dstFormat;
2461 c->srcFormat= srcFormat;
2462 c->vRounder= 4* 0x0001000100010001ULL;
2464 usesHFilter= usesVFilter= 0;
2465 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2466 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2467 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2468 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2469 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2470 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2471 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2472 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2474 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2475 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2477 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2478 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2480 // drop some chroma lines if the user wants it
2481 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2482 c->chrSrcVSubSample+= c->vChrDrop;
2484 // drop every other pixel for chroma calculation unless user wants full chroma
2485 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2486 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2487 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2488 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2489 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2490 c->chrSrcHSubSample=1;
2493 c->param[0] = param[0];
2494 c->param[1] = param[1];
2497 c->param[1] = SWS_PARAM_DEFAULT;
2500 c->chrIntHSubSample= c->chrDstHSubSample;
2501 c->chrIntVSubSample= c->chrSrcVSubSample;
2503 // Note the -((-x)>>y) is so that we always round toward +inf.
2504 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2505 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2506 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2507 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2509 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2511 /* unscaled special cases */
2512 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2515 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2517 c->swScale= PlanarToNV12Wrapper;
2520 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2521 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2523 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2526 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2528 c->swScale= yvu9toyv12Wrapper;
2532 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2533 c->swScale= bgr24toyv12Wrapper;
2535 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2536 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2537 && (isBGR(dstFormat) || isRGB(dstFormat))
2538 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2539 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2540 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2541 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2542 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2543 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2544 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2545 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2546 && dstFormat != PIX_FMT_RGB32_1
2547 && dstFormat != PIX_FMT_BGR32_1
2548 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2549 c->swScale= rgb2rgbWrapper;
2551 if ((usePal(srcFormat) && (
2552 dstFormat == PIX_FMT_RGB32 ||
2553 dstFormat == PIX_FMT_RGB32_1 ||
2554 dstFormat == PIX_FMT_RGB24 ||
2555 dstFormat == PIX_FMT_BGR32 ||
2556 dstFormat == PIX_FMT_BGR32_1 ||
2557 dstFormat == PIX_FMT_BGR24)))
2558 c->swScale= pal2rgbWrapper;
2560 if (srcFormat == PIX_FMT_YUV422P)
2562 if (dstFormat == PIX_FMT_YUYV422)
2563 c->swScale= YUV422PToYuy2Wrapper;
2564 else if (dstFormat == PIX_FMT_UYVY422)
2565 c->swScale= YUV422PToUyvyWrapper;
2568 /* LQ converters if -sws 0 or -sws 4*/
2569 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2571 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2573 if (dstFormat == PIX_FMT_YUYV422)
2574 c->swScale= PlanarToYuy2Wrapper;
2575 else if (dstFormat == PIX_FMT_UYVY422)
2576 c->swScale= PlanarToUyvyWrapper;
2579 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV420P)
2580 c->swScale= YUYV2YUV420Wrapper;
2581 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV420P)
2582 c->swScale= UYVY2YUV420Wrapper;
2584 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2585 c->swScale= YUYV2YUV422Wrapper;
2586 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2587 c->swScale= UYVY2YUV422Wrapper;
2589 #ifdef COMPILE_ALTIVEC
2590 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2591 !(c->flags & SWS_BITEXACT) &&
2592 srcFormat == PIX_FMT_YUV420P) {
2593 // unscaled YV12 -> packed YUV, we want speed
2594 if (dstFormat == PIX_FMT_YUYV422)
2595 c->swScale= yv12toyuy2_unscaled_altivec;
2596 else if (dstFormat == PIX_FMT_UYVY422)
2597 c->swScale= yv12touyvy_unscaled_altivec;
2602 if ( srcFormat == dstFormat
2603 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2604 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2605 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2606 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2608 if (isPacked(c->srcFormat))
2609 c->swScale= packedCopy;
2610 else /* Planar YUV or gray */
2611 c->swScale= planarCopy;
2614 /* gray16{le,be} conversions */
2615 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2617 c->swScale= gray16togray;
2619 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2621 c->swScale= graytogray16;
2623 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2625 c->swScale= gray16swap;
2629 if (flags & SWS_CPU_CAPS_BFIN)
2630 ff_bfin_get_unscaled_swscale (c);
2634 if (flags&SWS_PRINT_INFO)
2635 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2636 sws_format_name(srcFormat), sws_format_name(dstFormat));
2641 if (flags & SWS_CPU_CAPS_MMX2)
2643 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2644 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2646 if (flags&SWS_PRINT_INFO)
2647 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2649 if (usesHFilter) c->canMMX2BeUsed=0;
2654 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2655 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2657 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2658 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2659 // n-2 is the last chrominance sample available
2660 // this is not perfect, but no one should notice the difference, the more correct variant
2661 // would be like the vertical one, but that would require some special code for the
2662 // first and last pixel
2663 if (flags&SWS_FAST_BILINEAR)
2665 if (c->canMMX2BeUsed)
2670 //we don't use the x86 asm scaler if MMX is available
2671 else if (flags & SWS_CPU_CAPS_MMX)
2673 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2674 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2678 /* precalculate horizontal scaler filter coefficients */
2680 const int filterAlign=
2681 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2682 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2685 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2686 srcW , dstW, filterAlign, 1<<14,
2687 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2688 srcFilter->lumH, dstFilter->lumH, c->param);
2689 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2690 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2691 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2692 srcFilter->chrH, dstFilter->chrH, c->param);
2694 #define MAX_FUNNY_CODE_SIZE 10000
2695 #if defined(COMPILE_MMX2)
2696 // can't downscale !!!
2697 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2699 #ifdef MAP_ANONYMOUS
2700 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2701 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2702 #elif HAVE_VIRTUALALLOC
2703 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2704 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2706 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2707 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2710 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2711 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2712 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2713 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2715 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2716 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2718 #endif /* defined(COMPILE_MMX2) */
2719 } // initialize horizontal stuff
2723 /* precalculate vertical scaler filter coefficients */
2725 const int filterAlign=
2726 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2727 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2730 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2731 srcH , dstH, filterAlign, (1<<12),
2732 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2733 srcFilter->lumV, dstFilter->lumV, c->param);
2734 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2735 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2736 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2737 srcFilter->chrV, dstFilter->chrV, c->param);
2740 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2741 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2743 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2745 short *p = (short *)&c->vYCoeffsBank[i];
2747 p[j] = c->vLumFilter[i];
2750 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2752 short *p = (short *)&c->vCCoeffsBank[i];
2754 p[j] = c->vChrFilter[i];
2759 // calculate buffer sizes so that they won't run out while handling these damn slices
2760 c->vLumBufSize= c->vLumFilterSize;
2761 c->vChrBufSize= c->vChrFilterSize;
2762 for (i=0; i<dstH; i++)
2764 int chrI= i*c->chrDstH / dstH;
2765 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2766 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2768 nextSlice>>= c->chrSrcVSubSample;
2769 nextSlice<<= c->chrSrcVSubSample;
2770 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2771 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2772 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2773 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2776 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2777 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2778 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2779 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2780 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2781 //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)
2782 /* align at 16 bytes for AltiVec */
2783 for (i=0; i<c->vLumBufSize; i++)
2784 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2785 for (i=0; i<c->vChrBufSize; i++)
2786 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2787 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2788 for (i=0; i<c->vLumBufSize; i++)
2789 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2791 //try to avoid drawing green stuff between the right end and the stride end
2792 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2794 assert(2*VOFW == VOF);
2796 assert(c->chrDstH <= dstH);
2798 if (flags&SWS_PRINT_INFO)
2801 const char *dither= " dithered";
2803 const char *dither= "";
2805 if (flags&SWS_FAST_BILINEAR)
2806 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2807 else if (flags&SWS_BILINEAR)
2808 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2809 else if (flags&SWS_BICUBIC)
2810 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2811 else if (flags&SWS_X)
2812 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2813 else if (flags&SWS_POINT)
2814 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2815 else if (flags&SWS_AREA)
2816 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2817 else if (flags&SWS_BICUBLIN)
2818 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2819 else if (flags&SWS_GAUSS)
2820 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2821 else if (flags&SWS_SINC)
2822 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2823 else if (flags&SWS_LANCZOS)
2824 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2825 else if (flags&SWS_SPLINE)
2826 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2828 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2830 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2831 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2832 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2834 av_log(c, AV_LOG_INFO, "from %s to %s ",
2835 sws_format_name(srcFormat), sws_format_name(dstFormat));
2837 if (flags & SWS_CPU_CAPS_MMX2)
2838 av_log(c, AV_LOG_INFO, "using MMX2\n");
2839 else if (flags & SWS_CPU_CAPS_3DNOW)
2840 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2841 else if (flags & SWS_CPU_CAPS_MMX)
2842 av_log(c, AV_LOG_INFO, "using MMX\n");
2843 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2844 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2846 av_log(c, AV_LOG_INFO, "using C\n");
2849 if (flags & SWS_PRINT_INFO)
2851 if (flags & SWS_CPU_CAPS_MMX)
2853 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2854 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2857 if (c->hLumFilterSize==4)
2858 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2859 else if (c->hLumFilterSize==8)
2860 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2862 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2864 if (c->hChrFilterSize==4)
2865 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2866 else if (c->hChrFilterSize==8)
2867 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2869 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2875 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2877 if (flags & SWS_FAST_BILINEAR)
2878 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2880 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2883 if (isPlanarYUV(dstFormat))
2885 if (c->vLumFilterSize==1)
2886 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2888 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2892 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2893 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2894 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2895 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2896 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2898 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2901 if (dstFormat==PIX_FMT_BGR24)
2902 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2903 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2904 else if (dstFormat==PIX_FMT_RGB32)
2905 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2906 else if (dstFormat==PIX_FMT_BGR565)
2907 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2908 else if (dstFormat==PIX_FMT_BGR555)
2909 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2911 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2913 if (flags & SWS_PRINT_INFO)
2915 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2916 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2917 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2918 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2921 c->swScale= getSwsFunc(flags);
2926 * swscale wrapper, so we don't need to export the SwsContext.
2927 * Assumes planar YUV to be in YUV order instead of YVU.
2929 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2930 int srcSliceH, uint8_t* dst[], int dstStride[]){
2932 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2934 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2935 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2938 if (c->sliceDir == 0) {
2939 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2942 if (usePal(c->srcFormat)){
2943 for (i=0; i<256; i++){
2944 int p, r, g, b,y,u,v;
2945 if(c->srcFormat == PIX_FMT_PAL8){
2946 p=((uint32_t*)(src[1]))[i];
2950 }else if(c->srcFormat == PIX_FMT_RGB8){
2954 }else if(c->srcFormat == PIX_FMT_BGR8){
2958 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2963 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2968 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2969 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2970 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2971 c->pal_yuv[i]= y + (u<<8) + (v<<16);
2974 switch(c->dstFormat) {
2976 #ifndef WORDS_BIGENDIAN
2979 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2981 case PIX_FMT_BGR32_1:
2982 #ifdef WORDS_BIGENDIAN
2985 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2987 case PIX_FMT_RGB32_1:
2988 #ifdef WORDS_BIGENDIAN
2991 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2994 #ifndef WORDS_BIGENDIAN
2998 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3003 // copy strides, so they can safely be modified
3004 if (c->sliceDir == 1) {
3005 // slices go from top to bottom
3006 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3007 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3008 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
3010 // slices go from bottom to top => we flip the image internally
3011 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
3012 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
3013 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2],
3014 dst[3] + (c->dstH-1)*dstStride[3]};
3015 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3016 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3018 src2[0] += (srcSliceH-1)*srcStride[0];
3019 if (!usePal(c->srcFormat))
3020 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3021 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3022 src2[3] += (srcSliceH-1)*srcStride[3];
3024 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3028 #if LIBSWSCALE_VERSION_MAJOR < 1
3029 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3030 int srcSliceH, uint8_t* dst[], int dstStride[]){
3031 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3035 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3036 float lumaSharpen, float chromaSharpen,
3037 float chromaHShift, float chromaVShift,
3040 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3042 if (lumaGBlur!=0.0){
3043 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3044 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3046 filter->lumH= sws_getIdentityVec();
3047 filter->lumV= sws_getIdentityVec();
3050 if (chromaGBlur!=0.0){
3051 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3052 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3054 filter->chrH= sws_getIdentityVec();
3055 filter->chrV= sws_getIdentityVec();
3058 if (chromaSharpen!=0.0){
3059 SwsVector *id= sws_getIdentityVec();
3060 sws_scaleVec(filter->chrH, -chromaSharpen);
3061 sws_scaleVec(filter->chrV, -chromaSharpen);
3062 sws_addVec(filter->chrH, id);
3063 sws_addVec(filter->chrV, id);
3067 if (lumaSharpen!=0.0){
3068 SwsVector *id= sws_getIdentityVec();
3069 sws_scaleVec(filter->lumH, -lumaSharpen);
3070 sws_scaleVec(filter->lumV, -lumaSharpen);
3071 sws_addVec(filter->lumH, id);
3072 sws_addVec(filter->lumV, id);
3076 if (chromaHShift != 0.0)
3077 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3079 if (chromaVShift != 0.0)
3080 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3082 sws_normalizeVec(filter->chrH, 1.0);
3083 sws_normalizeVec(filter->chrV, 1.0);
3084 sws_normalizeVec(filter->lumH, 1.0);
3085 sws_normalizeVec(filter->lumV, 1.0);
3087 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3088 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3093 SwsVector *sws_getGaussianVec(double variance, double quality){
3094 const int length= (int)(variance*quality + 0.5) | 1;
3096 double *coeff= av_malloc(length*sizeof(double));
3097 double middle= (length-1)*0.5;
3098 SwsVector *vec= av_malloc(sizeof(SwsVector));
3101 vec->length= length;
3103 for (i=0; i<length; i++)
3105 double dist= i-middle;
3106 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3109 sws_normalizeVec(vec, 1.0);
3114 SwsVector *sws_getConstVec(double c, int length){
3116 double *coeff= av_malloc(length*sizeof(double));
3117 SwsVector *vec= av_malloc(sizeof(SwsVector));
3120 vec->length= length;
3122 for (i=0; i<length; i++)
3129 SwsVector *sws_getIdentityVec(void){
3130 return sws_getConstVec(1.0, 1);
3133 double sws_dcVec(SwsVector *a){
3137 for (i=0; i<a->length; i++)
3143 void sws_scaleVec(SwsVector *a, double scalar){
3146 for (i=0; i<a->length; i++)
3147 a->coeff[i]*= scalar;
3150 void sws_normalizeVec(SwsVector *a, double height){
3151 sws_scaleVec(a, height/sws_dcVec(a));
3154 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3155 int length= a->length + b->length - 1;
3156 double *coeff= av_malloc(length*sizeof(double));
3158 SwsVector *vec= av_malloc(sizeof(SwsVector));
3161 vec->length= length;
3163 for (i=0; i<length; i++) coeff[i]= 0.0;
3165 for (i=0; i<a->length; i++)
3167 for (j=0; j<b->length; j++)
3169 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3176 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3177 int length= FFMAX(a->length, b->length);
3178 double *coeff= av_malloc(length*sizeof(double));
3180 SwsVector *vec= av_malloc(sizeof(SwsVector));
3183 vec->length= length;
3185 for (i=0; i<length; i++) coeff[i]= 0.0;
3187 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3188 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3193 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3194 int length= FFMAX(a->length, b->length);
3195 double *coeff= av_malloc(length*sizeof(double));
3197 SwsVector *vec= av_malloc(sizeof(SwsVector));
3200 vec->length= length;
3202 for (i=0; i<length; i++) coeff[i]= 0.0;
3204 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3205 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3210 /* shift left / or right if "shift" is negative */
3211 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3212 int length= a->length + FFABS(shift)*2;
3213 double *coeff= av_malloc(length*sizeof(double));
3215 SwsVector *vec= av_malloc(sizeof(SwsVector));
3218 vec->length= length;
3220 for (i=0; i<length; i++) coeff[i]= 0.0;
3222 for (i=0; i<a->length; i++)
3224 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3230 void sws_shiftVec(SwsVector *a, int shift){
3231 SwsVector *shifted= sws_getShiftedVec(a, shift);
3233 a->coeff= shifted->coeff;
3234 a->length= shifted->length;
3238 void sws_addVec(SwsVector *a, SwsVector *b){
3239 SwsVector *sum= sws_sumVec(a, b);
3241 a->coeff= sum->coeff;
3242 a->length= sum->length;
3246 void sws_subVec(SwsVector *a, SwsVector *b){
3247 SwsVector *diff= sws_diffVec(a, b);
3249 a->coeff= diff->coeff;
3250 a->length= diff->length;
3254 void sws_convVec(SwsVector *a, SwsVector *b){
3255 SwsVector *conv= sws_getConvVec(a, b);
3257 a->coeff= conv->coeff;
3258 a->length= conv->length;
3262 SwsVector *sws_cloneVec(SwsVector *a){
3263 double *coeff= av_malloc(a->length*sizeof(double));
3265 SwsVector *vec= av_malloc(sizeof(SwsVector));
3268 vec->length= a->length;
3270 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3275 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3281 for (i=0; i<a->length; i++)
3282 if (a->coeff[i]>max) max= a->coeff[i];
3284 for (i=0; i<a->length; i++)
3285 if (a->coeff[i]<min) min= a->coeff[i];
3289 for (i=0; i<a->length; i++)
3291 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3292 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3293 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3294 av_log(log_ctx, log_level, "|\n");
3298 #if LIBSWSCALE_VERSION_MAJOR < 1
3299 void sws_printVec(SwsVector *a){
3300 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3304 void sws_freeVec(SwsVector *a){
3306 av_freep(&a->coeff);
3311 void sws_freeFilter(SwsFilter *filter){
3312 if (!filter) return;
3314 if (filter->lumH) sws_freeVec(filter->lumH);
3315 if (filter->lumV) sws_freeVec(filter->lumV);
3316 if (filter->chrH) sws_freeVec(filter->chrH);
3317 if (filter->chrV) sws_freeVec(filter->chrV);
3322 void sws_freeContext(SwsContext *c){
3328 for (i=0; i<c->vLumBufSize; i++)
3329 av_freep(&c->lumPixBuf[i]);
3330 av_freep(&c->lumPixBuf);
3335 for (i=0; i<c->vChrBufSize; i++)
3336 av_freep(&c->chrPixBuf[i]);
3337 av_freep(&c->chrPixBuf);
3340 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3341 for (i=0; i<c->vLumBufSize; i++)
3342 av_freep(&c->alpPixBuf[i]);
3343 av_freep(&c->alpPixBuf);
3346 av_freep(&c->vLumFilter);
3347 av_freep(&c->vChrFilter);
3348 av_freep(&c->hLumFilter);
3349 av_freep(&c->hChrFilter);
3351 av_freep(&c->vYCoeffsBank);
3352 av_freep(&c->vCCoeffsBank);
3355 av_freep(&c->vLumFilterPos);
3356 av_freep(&c->vChrFilterPos);
3357 av_freep(&c->hLumFilterPos);
3358 av_freep(&c->hChrFilterPos);
3360 #if ARCH_X86 && CONFIG_GPL
3361 #ifdef MAP_ANONYMOUS
3362 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3363 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3364 #elif HAVE_VIRTUALALLOC
3365 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3366 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3368 av_free(c->funnyYCode );
3369 av_free(c->funnyUVCode);
3372 c->funnyUVCode=NULL;
3373 #endif /* ARCH_X86 && CONFIG_GPL */
3375 av_freep(&c->lumMmx2Filter);
3376 av_freep(&c->chrMmx2Filter);
3377 av_freep(&c->lumMmx2FilterPos);
3378 av_freep(&c->chrMmx2FilterPos);
3379 av_freep(&c->yuvTable);
3384 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3385 int srcW, int srcH, enum PixelFormat srcFormat,
3386 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3387 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
3389 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3392 param = default_param;
3395 if (context->srcW != srcW || context->srcH != srcH ||
3396 context->srcFormat != srcFormat ||
3397 context->dstW != dstW || context->dstH != dstH ||
3398 context->dstFormat != dstFormat || context->flags != flags ||
3399 context->param[0] != param[0] || context->param[1] != param[1])
3401 sws_freeContext(context);
3406 return sws_getContext(srcW, srcH, srcFormat,
3407 dstW, dstH, dstFormat, flags,
3408 srcFilter, dstFilter, param);