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 #include "swscale_internal.h"
74 #include "libavutil/x86_cpu.h"
75 #include "libavutil/bswap.h"
77 unsigned swscale_version(void)
79 return LIBSWSCALE_VERSION_INT;
86 //#define HAVE_AMD3DNOW
89 //#define WORDS_BIGENDIAN
92 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
94 #define RET 0xC3 //near return opcode for x86
99 #define PI 3.14159265358979323846
102 #define isSupportedIn(x) ( \
103 (x)==PIX_FMT_YUV420P \
104 || (x)==PIX_FMT_YUVA420P \
105 || (x)==PIX_FMT_YUYV422 \
106 || (x)==PIX_FMT_UYVY422 \
107 || (x)==PIX_FMT_RGB32 \
108 || (x)==PIX_FMT_RGB32_1 \
109 || (x)==PIX_FMT_BGR24 \
110 || (x)==PIX_FMT_BGR565 \
111 || (x)==PIX_FMT_BGR555 \
112 || (x)==PIX_FMT_BGR32 \
113 || (x)==PIX_FMT_BGR32_1 \
114 || (x)==PIX_FMT_RGB24 \
115 || (x)==PIX_FMT_RGB565 \
116 || (x)==PIX_FMT_RGB555 \
117 || (x)==PIX_FMT_GRAY8 \
118 || (x)==PIX_FMT_YUV410P \
119 || (x)==PIX_FMT_YUV440P \
120 || (x)==PIX_FMT_GRAY16BE \
121 || (x)==PIX_FMT_GRAY16LE \
122 || (x)==PIX_FMT_YUV444P \
123 || (x)==PIX_FMT_YUV422P \
124 || (x)==PIX_FMT_YUV411P \
125 || (x)==PIX_FMT_PAL8 \
126 || (x)==PIX_FMT_BGR8 \
127 || (x)==PIX_FMT_RGB8 \
128 || (x)==PIX_FMT_BGR4_BYTE \
129 || (x)==PIX_FMT_RGB4_BYTE \
130 || (x)==PIX_FMT_YUV440P \
131 || (x)==PIX_FMT_MONOWHITE \
132 || (x)==PIX_FMT_MONOBLACK \
134 #define isSupportedOut(x) ( \
135 (x)==PIX_FMT_YUV420P \
136 || (x)==PIX_FMT_YUYV422 \
137 || (x)==PIX_FMT_UYVY422 \
138 || (x)==PIX_FMT_YUV444P \
139 || (x)==PIX_FMT_YUV422P \
140 || (x)==PIX_FMT_YUV411P \
143 || (x)==PIX_FMT_NV12 \
144 || (x)==PIX_FMT_NV21 \
145 || (x)==PIX_FMT_GRAY16BE \
146 || (x)==PIX_FMT_GRAY16LE \
147 || (x)==PIX_FMT_GRAY8 \
148 || (x)==PIX_FMT_YUV410P \
149 || (x)==PIX_FMT_YUV440P \
151 #define isPacked(x) ( \
153 || (x)==PIX_FMT_YUYV422 \
154 || (x)==PIX_FMT_UYVY422 \
158 #define usePal(x) ( \
160 || (x)==PIX_FMT_BGR4_BYTE \
161 || (x)==PIX_FMT_RGB4_BYTE \
162 || (x)==PIX_FMT_BGR8 \
163 || (x)==PIX_FMT_RGB8 \
166 #define RGB2YUV_SHIFT 15
167 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
168 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
169 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
170 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
171 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
172 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
173 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
174 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
175 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
177 extern const int32_t ff_yuv2rgb_coeffs[8][4];
179 static const double rgb2yuv_table[8][9]={
180 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
181 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
182 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
183 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
184 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
185 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
186 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
187 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
192 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
195 more intelligent misalignment avoidance for the horizontal scaler
196 write special vertical cubic upscale version
197 optimize C code (YV12 / minmax)
198 add support for packed pixel YUV input & output
199 add support for Y8 output
200 optimize BGR24 & BGR32
201 add BGR4 output support
202 write special BGR->BGR scaler
205 #if ARCH_X86 && CONFIG_GPL
206 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
207 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
208 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
209 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
210 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
211 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
212 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
213 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
215 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
216 0x0103010301030103LL,
217 0x0200020002000200LL,};
219 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
220 0x0602060206020602LL,
221 0x0004000400040004LL,};
223 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
224 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
225 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
226 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
227 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
228 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
230 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
231 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
232 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
235 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
236 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
237 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
239 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
240 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
241 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
242 #endif /* FAST_BGR2YV12 */
243 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
244 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
245 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
247 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
248 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
249 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
250 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
251 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
253 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
254 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
255 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
258 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
260 #endif /* ARCH_X86 && CONFIG_GPL */
262 // clipping helper table for C implementations:
263 static unsigned char clip_table[768];
265 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
267 static const uint8_t __attribute__((aligned(8))) dither_2x2_4[2][8]={
268 { 1, 3, 1, 3, 1, 3, 1, 3, },
269 { 2, 0, 2, 0, 2, 0, 2, 0, },
272 static const uint8_t __attribute__((aligned(8))) dither_2x2_8[2][8]={
273 { 6, 2, 6, 2, 6, 2, 6, 2, },
274 { 0, 4, 0, 4, 0, 4, 0, 4, },
277 const uint8_t __attribute__((aligned(8))) dither_8x8_32[8][8]={
278 { 17, 9, 23, 15, 16, 8, 22, 14, },
279 { 5, 29, 3, 27, 4, 28, 2, 26, },
280 { 21, 13, 19, 11, 20, 12, 18, 10, },
281 { 0, 24, 6, 30, 1, 25, 7, 31, },
282 { 16, 8, 22, 14, 17, 9, 23, 15, },
283 { 4, 28, 2, 26, 5, 29, 3, 27, },
284 { 20, 12, 18, 10, 21, 13, 19, 11, },
285 { 1, 25, 7, 31, 0, 24, 6, 30, },
289 const uint8_t __attribute__((aligned(8))) dither_8x8_64[8][8]={
290 { 0, 48, 12, 60, 3, 51, 15, 63, },
291 { 32, 16, 44, 28, 35, 19, 47, 31, },
292 { 8, 56, 4, 52, 11, 59, 7, 55, },
293 { 40, 24, 36, 20, 43, 27, 39, 23, },
294 { 2, 50, 14, 62, 1, 49, 13, 61, },
295 { 34, 18, 46, 30, 33, 17, 45, 29, },
296 { 10, 58, 6, 54, 9, 57, 5, 53, },
297 { 42, 26, 38, 22, 41, 25, 37, 21, },
301 const uint8_t __attribute__((aligned(8))) dither_8x8_73[8][8]={
302 { 0, 55, 14, 68, 3, 58, 17, 72, },
303 { 37, 18, 50, 32, 40, 22, 54, 35, },
304 { 9, 64, 5, 59, 13, 67, 8, 63, },
305 { 46, 27, 41, 23, 49, 31, 44, 26, },
306 { 2, 57, 16, 71, 1, 56, 15, 70, },
307 { 39, 21, 52, 34, 38, 19, 51, 33, },
308 { 11, 66, 7, 62, 10, 65, 6, 60, },
309 { 48, 30, 43, 25, 47, 29, 42, 24, },
313 const uint8_t __attribute__((aligned(8))) dither_8x8_128[8][8]={
314 { 68, 36, 92, 60, 66, 34, 90, 58, },
315 { 20, 116, 12, 108, 18, 114, 10, 106, },
316 { 84, 52, 76, 44, 82, 50, 74, 42, },
317 { 0, 96, 24, 120, 6, 102, 30, 126, },
318 { 64, 32, 88, 56, 70, 38, 94, 62, },
319 { 16, 112, 8, 104, 22, 118, 14, 110, },
320 { 80, 48, 72, 40, 86, 54, 78, 46, },
321 { 4, 100, 28, 124, 2, 98, 26, 122, },
326 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
327 {117, 62, 158, 103, 113, 58, 155, 100, },
328 { 34, 199, 21, 186, 31, 196, 17, 182, },
329 {144, 89, 131, 76, 141, 86, 127, 72, },
330 { 0, 165, 41, 206, 10, 175, 52, 217, },
331 {110, 55, 151, 96, 120, 65, 162, 107, },
332 { 28, 193, 14, 179, 38, 203, 24, 189, },
333 {138, 83, 124, 69, 148, 93, 134, 79, },
334 { 7, 172, 48, 213, 3, 168, 45, 210, },
337 // tries to correct a gamma of 1.5
338 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
339 { 0, 143, 18, 200, 2, 156, 25, 215, },
340 { 78, 28, 125, 64, 89, 36, 138, 74, },
341 { 10, 180, 3, 161, 16, 195, 8, 175, },
342 {109, 51, 93, 38, 121, 60, 105, 47, },
343 { 1, 152, 23, 210, 0, 147, 20, 205, },
344 { 85, 33, 134, 71, 81, 30, 130, 67, },
345 { 14, 190, 6, 171, 12, 185, 5, 166, },
346 {117, 57, 101, 44, 113, 54, 97, 41, },
349 // tries to correct a gamma of 2.0
350 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
351 { 0, 124, 8, 193, 0, 140, 12, 213, },
352 { 55, 14, 104, 42, 66, 19, 119, 52, },
353 { 3, 168, 1, 145, 6, 187, 3, 162, },
354 { 86, 31, 70, 21, 99, 39, 82, 28, },
355 { 0, 134, 11, 206, 0, 129, 9, 200, },
356 { 62, 17, 114, 48, 58, 16, 109, 45, },
357 { 5, 181, 2, 157, 4, 175, 1, 151, },
358 { 95, 36, 78, 26, 90, 34, 74, 24, },
361 // tries to correct a gamma of 2.5
362 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
363 { 0, 107, 3, 187, 0, 125, 6, 212, },
364 { 39, 7, 86, 28, 49, 11, 102, 36, },
365 { 1, 158, 0, 131, 3, 180, 1, 151, },
366 { 68, 19, 52, 12, 81, 25, 64, 17, },
367 { 0, 119, 5, 203, 0, 113, 4, 195, },
368 { 45, 9, 96, 33, 42, 8, 91, 30, },
369 { 2, 172, 1, 144, 2, 165, 0, 137, },
370 { 77, 23, 60, 15, 72, 21, 56, 14, },
374 const char *sws_format_name(enum PixelFormat format)
377 case PIX_FMT_YUV420P:
379 case PIX_FMT_YUVA420P:
381 case PIX_FMT_YUYV422:
387 case PIX_FMT_YUV422P:
389 case PIX_FMT_YUV444P:
393 case PIX_FMT_YUV410P:
395 case PIX_FMT_YUV411P:
401 case PIX_FMT_GRAY16BE:
403 case PIX_FMT_GRAY16LE:
407 case PIX_FMT_MONOWHITE:
409 case PIX_FMT_MONOBLACK:
413 case PIX_FMT_YUVJ420P:
415 case PIX_FMT_YUVJ422P:
417 case PIX_FMT_YUVJ444P:
419 case PIX_FMT_XVMC_MPEG2_MC:
420 return "xvmc_mpeg2_mc";
421 case PIX_FMT_XVMC_MPEG2_IDCT:
422 return "xvmc_mpeg2_idct";
423 case PIX_FMT_UYVY422:
425 case PIX_FMT_UYYVYY411:
427 case PIX_FMT_RGB32_1:
429 case PIX_FMT_BGR32_1:
441 case PIX_FMT_BGR4_BYTE:
447 case PIX_FMT_RGB4_BYTE:
453 case PIX_FMT_YUV440P:
455 case PIX_FMT_VDPAU_H264:
457 case PIX_FMT_VDPAU_MPEG1:
458 return "vdpau_mpeg1";
459 case PIX_FMT_VDPAU_MPEG2:
460 return "vdpau_mpeg2";
461 case PIX_FMT_VDPAU_WMV3:
463 case PIX_FMT_VDPAU_VC1:
466 return "Unknown format";
470 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
471 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
472 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
474 //FIXME Optimize (just quickly written not optimized..)
476 for (i=0; i<dstW; i++)
480 for (j=0; j<lumFilterSize; j++)
481 val += lumSrc[j][i] * lumFilter[j];
483 dest[i]= av_clip_uint8(val>>19);
487 for (i=0; i<chrDstW; i++)
492 for (j=0; j<chrFilterSize; j++)
494 u += chrSrc[j][i] * chrFilter[j];
495 v += chrSrc[j][i + VOFW] * chrFilter[j];
498 uDest[i]= av_clip_uint8(u>>19);
499 vDest[i]= av_clip_uint8(v>>19);
503 static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
504 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
505 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
507 //FIXME Optimize (just quickly written not optimized..)
509 for (i=0; i<dstW; i++)
513 for (j=0; j<lumFilterSize; j++)
514 val += lumSrc[j][i] * lumFilter[j];
516 dest[i]= av_clip_uint8(val>>19);
522 if (dstFormat == PIX_FMT_NV12)
523 for (i=0; i<chrDstW; i++)
528 for (j=0; j<chrFilterSize; j++)
530 u += chrSrc[j][i] * chrFilter[j];
531 v += chrSrc[j][i + VOFW] * chrFilter[j];
534 uDest[2*i]= av_clip_uint8(u>>19);
535 uDest[2*i+1]= av_clip_uint8(v>>19);
538 for (i=0; i<chrDstW; i++)
543 for (j=0; j<chrFilterSize; j++)
545 u += chrSrc[j][i] * chrFilter[j];
546 v += chrSrc[j][i + VOFW] * chrFilter[j];
549 uDest[2*i]= av_clip_uint8(v>>19);
550 uDest[2*i+1]= av_clip_uint8(u>>19);
554 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type) \
555 for (i=0; i<(dstW>>1); i++){\
561 type av_unused *r, *b, *g;\
564 for (j=0; j<lumFilterSize; j++)\
566 Y1 += lumSrc[j][i2] * lumFilter[j];\
567 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
569 for (j=0; j<chrFilterSize; j++)\
571 U += chrSrc[j][i] * chrFilter[j];\
572 V += chrSrc[j][i+VOFW] * chrFilter[j];\
579 #define YSCALE_YUV_2_PACKEDX_C(type) \
580 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type)\
581 if ((Y1|Y2|U|V)&256)\
583 if (Y1>255) Y1=255; \
584 else if (Y1<0)Y1=0; \
585 if (Y2>255) Y2=255; \
586 else if (Y2<0)Y2=0; \
593 #define YSCALE_YUV_2_PACKEDX_FULL_C \
594 for (i=0; i<dstW; i++){\
601 for (j=0; j<lumFilterSize; j++){\
602 Y += lumSrc[j][i ] * lumFilter[j];\
604 for (j=0; j<chrFilterSize; j++){\
605 U += chrSrc[j][i ] * chrFilter[j];\
606 V += chrSrc[j][i+VOFW] * chrFilter[j];\
612 #define YSCALE_YUV_2_RGBX_FULL_C(rnd) \
613 YSCALE_YUV_2_PACKEDX_FULL_C\
614 Y-= c->yuv2rgb_y_offset;\
615 Y*= c->yuv2rgb_y_coeff;\
617 R= Y + V*c->yuv2rgb_v2r_coeff;\
618 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
619 B= Y + U*c->yuv2rgb_u2b_coeff;\
620 if ((R|G|B)&(0xC0000000)){\
621 if (R>=(256<<22)) R=(256<<22)-1; \
623 if (G>=(256<<22)) G=(256<<22)-1; \
625 if (B>=(256<<22)) B=(256<<22)-1; \
630 #define YSCALE_YUV_2_GRAY16_C \
631 for (i=0; i<(dstW>>1); i++){\
640 for (j=0; j<lumFilterSize; j++)\
642 Y1 += lumSrc[j][i2] * lumFilter[j];\
643 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
647 if ((Y1|Y2|U|V)&65536)\
649 if (Y1>65535) Y1=65535; \
650 else if (Y1<0)Y1=0; \
651 if (Y2>65535) Y2=65535; \
652 else if (Y2<0)Y2=0; \
655 #define YSCALE_YUV_2_RGBX_C(type) \
656 YSCALE_YUV_2_PACKEDX_C(type) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
657 r = (type *)c->table_rV[V]; \
658 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
659 b = (type *)c->table_bU[U]; \
661 #define YSCALE_YUV_2_PACKED2_C \
662 for (i=0; i<(dstW>>1); i++){ \
664 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
665 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
666 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
667 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
669 #define YSCALE_YUV_2_GRAY16_2_C \
670 for (i=0; i<(dstW>>1); i++){ \
672 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
673 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
675 #define YSCALE_YUV_2_RGB2_C(type) \
676 YSCALE_YUV_2_PACKED2_C\
678 r = (type *)c->table_rV[V];\
679 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
680 b = (type *)c->table_bU[U];\
682 #define YSCALE_YUV_2_PACKED1_C \
683 for (i=0; i<(dstW>>1); i++){\
685 int Y1= buf0[i2 ]>>7;\
686 int Y2= buf0[i2+1]>>7;\
687 int U= (uvbuf1[i ])>>7;\
688 int V= (uvbuf1[i+VOFW])>>7;\
690 #define YSCALE_YUV_2_GRAY16_1_C \
691 for (i=0; i<(dstW>>1); i++){\
693 int Y1= buf0[i2 ]<<1;\
694 int Y2= buf0[i2+1]<<1;\
696 #define YSCALE_YUV_2_RGB1_C(type) \
697 YSCALE_YUV_2_PACKED1_C\
699 r = (type *)c->table_rV[V];\
700 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
701 b = (type *)c->table_bU[U];\
703 #define YSCALE_YUV_2_PACKED1B_C \
704 for (i=0; i<(dstW>>1); i++){\
706 int Y1= buf0[i2 ]>>7;\
707 int Y2= buf0[i2+1]>>7;\
708 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
709 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
711 #define YSCALE_YUV_2_RGB1B_C(type) \
712 YSCALE_YUV_2_PACKED1B_C\
714 r = (type *)c->table_rV[V];\
715 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
716 b = (type *)c->table_bU[U];\
718 #define YSCALE_YUV_2_MONO2_C \
719 const uint8_t * const d128=dither_8x8_220[y&7];\
720 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
721 for (i=0; i<dstW-7; i+=8){\
723 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
724 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
725 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
726 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
727 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
728 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
729 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
730 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
731 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
736 #define YSCALE_YUV_2_MONOX_C \
737 const uint8_t * const d128=dither_8x8_220[y&7];\
738 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
740 for (i=0; i<dstW-1; i+=2){\
745 for (j=0; j<lumFilterSize; j++)\
747 Y1 += lumSrc[j][i] * lumFilter[j];\
748 Y2 += lumSrc[j][i+1] * lumFilter[j];\
759 acc+= acc + g[Y1+d128[(i+0)&7]];\
760 acc+= acc + g[Y2+d128[(i+1)&7]];\
762 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
768 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
769 switch(c->dstFormat)\
773 case PIX_FMT_RGB32_1:\
774 case PIX_FMT_BGR32_1:\
776 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
777 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
782 ((uint8_t*)dest)[0]= r[Y1];\
783 ((uint8_t*)dest)[1]= g[Y1];\
784 ((uint8_t*)dest)[2]= b[Y1];\
785 ((uint8_t*)dest)[3]= r[Y2];\
786 ((uint8_t*)dest)[4]= g[Y2];\
787 ((uint8_t*)dest)[5]= b[Y2];\
793 ((uint8_t*)dest)[0]= b[Y1];\
794 ((uint8_t*)dest)[1]= g[Y1];\
795 ((uint8_t*)dest)[2]= r[Y1];\
796 ((uint8_t*)dest)[3]= b[Y2];\
797 ((uint8_t*)dest)[4]= g[Y2];\
798 ((uint8_t*)dest)[5]= r[Y2];\
802 case PIX_FMT_RGB565:\
803 case PIX_FMT_BGR565:\
805 const int dr1= dither_2x2_8[y&1 ][0];\
806 const int dg1= dither_2x2_4[y&1 ][0];\
807 const int db1= dither_2x2_8[(y&1)^1][0];\
808 const int dr2= dither_2x2_8[y&1 ][1];\
809 const int dg2= dither_2x2_4[y&1 ][1];\
810 const int db2= dither_2x2_8[(y&1)^1][1];\
812 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
813 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
817 case PIX_FMT_RGB555:\
818 case PIX_FMT_BGR555:\
820 const int dr1= dither_2x2_8[y&1 ][0];\
821 const int dg1= dither_2x2_8[y&1 ][1];\
822 const int db1= dither_2x2_8[(y&1)^1][0];\
823 const int dr2= dither_2x2_8[y&1 ][1];\
824 const int dg2= dither_2x2_8[y&1 ][0];\
825 const int db2= dither_2x2_8[(y&1)^1][1];\
827 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
828 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
835 const uint8_t * const d64= dither_8x8_73[y&7];\
836 const uint8_t * const d32= dither_8x8_32[y&7];\
838 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
839 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
846 const uint8_t * const d64= dither_8x8_73 [y&7];\
847 const uint8_t * const d128=dither_8x8_220[y&7];\
849 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
850 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
854 case PIX_FMT_RGB4_BYTE:\
855 case PIX_FMT_BGR4_BYTE:\
857 const uint8_t * const d64= dither_8x8_73 [y&7];\
858 const uint8_t * const d128=dither_8x8_220[y&7];\
860 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
861 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
865 case PIX_FMT_MONOBLACK:\
866 case PIX_FMT_MONOWHITE:\
871 case PIX_FMT_YUYV422:\
873 ((uint8_t*)dest)[2*i2+0]= Y1;\
874 ((uint8_t*)dest)[2*i2+1]= U;\
875 ((uint8_t*)dest)[2*i2+2]= Y2;\
876 ((uint8_t*)dest)[2*i2+3]= V;\
879 case PIX_FMT_UYVY422:\
881 ((uint8_t*)dest)[2*i2+0]= U;\
882 ((uint8_t*)dest)[2*i2+1]= Y1;\
883 ((uint8_t*)dest)[2*i2+2]= V;\
884 ((uint8_t*)dest)[2*i2+3]= Y2;\
887 case PIX_FMT_GRAY16BE:\
889 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
890 ((uint8_t*)dest)[2*i2+1]= Y1;\
891 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
892 ((uint8_t*)dest)[2*i2+3]= Y2;\
895 case PIX_FMT_GRAY16LE:\
897 ((uint8_t*)dest)[2*i2+0]= Y1;\
898 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
899 ((uint8_t*)dest)[2*i2+2]= Y2;\
900 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
906 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
907 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
908 uint8_t *dest, int dstW, int y)
911 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
914 static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
915 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
916 uint8_t *dest, int dstW, int y)
919 int step= fmt_depth(c->dstFormat)/8;
922 switch(c->dstFormat){
929 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
943 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
956 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
958 uint8_t *ptr = plane + stride*y;
959 for (i=0; i<height; i++){
960 memset(ptr, val, width);
965 //Note: we have C, X86, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
967 #if !HAVE_MMX || defined (RUNTIME_CPUDETECT) || !CONFIG_GPL
972 #if (HAVE_ALTIVEC || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
974 #define COMPILE_ALTIVEC
980 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
984 #if (HAVE_MMX2 || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
988 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && CONFIG_GPL
989 #define COMPILE_3DNOW
999 #define HAVE_AMD3DNOW 0
1000 #define HAVE_ALTIVEC 0
1003 #define RENAME(a) a ## _C
1004 #include "swscale_template.c"
1007 #ifdef COMPILE_ALTIVEC
1010 #define HAVE_ALTIVEC 1
1011 #define RENAME(a) a ## _altivec
1012 #include "swscale_template.c"
1022 #undef HAVE_AMD3DNOW
1024 #define RENAME(a) a ## _X86
1025 #include "swscale_template.c"
1032 #undef HAVE_AMD3DNOW
1035 #define HAVE_AMD3DNOW 0
1036 #define RENAME(a) a ## _MMX
1037 #include "swscale_template.c"
1045 #undef HAVE_AMD3DNOW
1048 #define HAVE_AMD3DNOW 0
1049 #define RENAME(a) a ## _MMX2
1050 #include "swscale_template.c"
1054 #ifdef COMPILE_3DNOW
1058 #undef HAVE_AMD3DNOW
1061 #define HAVE_AMD3DNOW 1
1062 #define RENAME(a) a ## _3DNow
1063 #include "swscale_template.c"
1068 // minor note: the HAVE_xyz are messed up after this line so don't use them
1070 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1072 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1073 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1074 else return getSplineCoeff( 0.0,
1081 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1082 int srcW, int dstW, int filterAlign, int one, int flags,
1083 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1089 int64_t *filter=NULL;
1090 int64_t *filter2=NULL;
1091 const int64_t fone= 1LL<<54;
1094 if (flags & SWS_CPU_CAPS_MMX)
1095 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1098 // NOTE: the +1 is for the MMX scaler which reads over the end
1099 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1101 if (FFABS(xInc - 0x10000) <10) // unscaled
1105 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1107 for (i=0; i<dstW; i++)
1109 filter[i*filterSize]= fone;
1114 else if (flags&SWS_POINT) // lame looking point sampling mode
1119 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1121 xDstInSrc= xInc/2 - 0x8000;
1122 for (i=0; i<dstW; i++)
1124 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1126 (*filterPos)[i]= xx;
1131 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1135 if (flags&SWS_BICUBIC) filterSize= 4;
1136 else if (flags&SWS_X ) filterSize= 4;
1137 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1138 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1140 xDstInSrc= xInc/2 - 0x8000;
1141 for (i=0; i<dstW; i++)
1143 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1146 (*filterPos)[i]= xx;
1147 //bilinear upscale / linear interpolate / area averaging
1148 for (j=0; j<filterSize; j++)
1150 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1151 if (coeff<0) coeff=0;
1152 filter[i*filterSize + j]= coeff;
1163 if (flags&SWS_BICUBIC) sizeFactor= 4;
1164 else if (flags&SWS_X) sizeFactor= 8;
1165 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1166 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1167 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1168 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1169 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1170 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1172 sizeFactor= 0; //GCC warning killer
1176 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1177 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1179 if (filterSize > srcW-2) filterSize=srcW-2;
1181 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1183 xDstInSrc= xInc - 0x10000;
1184 for (i=0; i<dstW; i++)
1186 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1188 (*filterPos)[i]= xx;
1189 for (j=0; j<filterSize; j++)
1191 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1197 floatd= d * (1.0/(1<<30));
1199 if (flags & SWS_BICUBIC)
1201 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1202 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1203 int64_t dd = ( d*d)>>30;
1204 int64_t ddd= (dd*d)>>30;
1207 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1208 else if (d < 1LL<<31)
1209 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1212 coeff *= fone>>(30+24);
1214 /* else if (flags & SWS_X)
1216 double p= param ? param*0.01 : 0.3;
1217 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1218 coeff*= pow(2.0, - p*d*d);
1220 else if (flags & SWS_X)
1222 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1229 if (c<0.0) c= -pow(-c, A);
1231 coeff= (c*0.5 + 0.5)*fone;
1233 else if (flags & SWS_AREA)
1235 int64_t d2= d - (1<<29);
1236 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1237 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1239 coeff *= fone>>(30+16);
1241 else if (flags & SWS_GAUSS)
1243 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1244 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1246 else if (flags & SWS_SINC)
1248 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1250 else if (flags & SWS_LANCZOS)
1252 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1253 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1254 if (floatd>p) coeff=0;
1256 else if (flags & SWS_BILINEAR)
1259 if (coeff<0) coeff=0;
1260 coeff *= fone >> 30;
1262 else if (flags & SWS_SPLINE)
1264 double p=-2.196152422706632;
1265 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1268 coeff= 0.0; //GCC warning killer
1272 filter[i*filterSize + j]= coeff;
1279 /* apply src & dst Filter to filter -> filter2
1282 assert(filterSize>0);
1283 filter2Size= filterSize;
1284 if (srcFilter) filter2Size+= srcFilter->length - 1;
1285 if (dstFilter) filter2Size+= dstFilter->length - 1;
1286 assert(filter2Size>0);
1287 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1289 for (i=0; i<dstW; i++)
1294 for (k=0; k<srcFilter->length; k++){
1295 for (j=0; j<filterSize; j++)
1296 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1299 for (j=0; j<filterSize; j++)
1300 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1304 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1308 /* try to reduce the filter-size (step1 find size and shift left) */
1309 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1311 for (i=dstW-1; i>=0; i--)
1313 int min= filter2Size;
1317 /* get rid off near zero elements on the left by shifting left */
1318 for (j=0; j<filter2Size; j++)
1321 cutOff += FFABS(filter2[i*filter2Size]);
1323 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1325 /* preserve monotonicity because the core can't handle the filter otherwise */
1326 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1328 // move filter coefficients left
1329 for (k=1; k<filter2Size; k++)
1330 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1331 filter2[i*filter2Size + k - 1]= 0;
1336 /* count near zeros on the right */
1337 for (j=filter2Size-1; j>0; j--)
1339 cutOff += FFABS(filter2[i*filter2Size + j]);
1341 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1345 if (min>minFilterSize) minFilterSize= min;
1348 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1349 // we can handle the special case 4,
1350 // so we don't want to go to the full 8
1351 if (minFilterSize < 5)
1354 // We really don't want to waste our time
1355 // doing useless computation, so fall back on
1356 // the scalar C code for very small filters.
1357 // Vectorizing is worth it only if you have a
1358 // decent-sized vector.
1359 if (minFilterSize < 3)
1363 if (flags & SWS_CPU_CAPS_MMX) {
1364 // special case for unscaled vertical filtering
1365 if (minFilterSize == 1 && filterAlign == 2)
1369 assert(minFilterSize > 0);
1370 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1371 assert(filterSize > 0);
1372 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1373 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1375 *outFilterSize= filterSize;
1377 if (flags&SWS_PRINT_INFO)
1378 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1379 /* try to reduce the filter-size (step2 reduce it) */
1380 for (i=0; i<dstW; i++)
1384 for (j=0; j<filterSize; j++)
1386 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1387 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1388 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1389 filter[i*filterSize + j]= 0;
1394 //FIXME try to align filterPos if possible
1397 for (i=0; i<dstW; i++)
1400 if ((*filterPos)[i] < 0)
1402 // move filter coefficients left to compensate for filterPos
1403 for (j=1; j<filterSize; j++)
1405 int left= FFMAX(j + (*filterPos)[i], 0);
1406 filter[i*filterSize + left] += filter[i*filterSize + j];
1407 filter[i*filterSize + j]=0;
1412 if ((*filterPos)[i] + filterSize > srcW)
1414 int shift= (*filterPos)[i] + filterSize - srcW;
1415 // move filter coefficients right to compensate for filterPos
1416 for (j=filterSize-2; j>=0; j--)
1418 int right= FFMIN(j + shift, filterSize-1);
1419 filter[i*filterSize +right] += filter[i*filterSize +j];
1420 filter[i*filterSize +j]=0;
1422 (*filterPos)[i]= srcW - filterSize;
1426 // Note the +1 is for the MMX scaler which reads over the end
1427 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1428 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1430 /* normalize & store in outFilter */
1431 for (i=0; i<dstW; i++)
1437 for (j=0; j<filterSize; j++)
1439 sum+= filter[i*filterSize + j];
1441 sum= (sum + one/2)/ one;
1442 for (j=0; j<*outFilterSize; j++)
1444 int64_t v= filter[i*filterSize + j] + error;
1445 int intV= ROUNDED_DIV(v, sum);
1446 (*outFilter)[i*(*outFilterSize) + j]= intV;
1447 error= v - intV*sum;
1451 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1452 for (i=0; i<*outFilterSize; i++)
1454 int j= dstW*(*outFilterSize);
1455 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1466 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1469 x86_reg imm8OfPShufW1A;
1470 x86_reg imm8OfPShufW2A;
1471 x86_reg fragmentLengthA;
1473 x86_reg imm8OfPShufW1B;
1474 x86_reg imm8OfPShufW2B;
1475 x86_reg fragmentLengthB;
1480 // create an optimized horizontal scaling routine
1488 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1489 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1490 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1491 "punpcklbw %%mm7, %%mm1 \n\t"
1492 "punpcklbw %%mm7, %%mm0 \n\t"
1493 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1495 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1497 "psubw %%mm1, %%mm0 \n\t"
1498 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1499 "pmullw %%mm3, %%mm0 \n\t"
1500 "psllw $7, %%mm1 \n\t"
1501 "paddw %%mm1, %%mm0 \n\t"
1503 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1505 "add $8, %%"REG_a" \n\t"
1509 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1510 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1511 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1516 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1520 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1521 "=r" (fragmentLengthA)
1528 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1529 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1530 "punpcklbw %%mm7, %%mm0 \n\t"
1531 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1533 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1535 "psubw %%mm1, %%mm0 \n\t"
1536 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1537 "pmullw %%mm3, %%mm0 \n\t"
1538 "psllw $7, %%mm1 \n\t"
1539 "paddw %%mm1, %%mm0 \n\t"
1541 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1543 "add $8, %%"REG_a" \n\t"
1547 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1548 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1549 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1554 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1558 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1559 "=r" (fragmentLengthB)
1562 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1565 for (i=0; i<dstW/numSplits; i++)
1572 int b=((xpos+xInc)>>16) - xx;
1573 int c=((xpos+xInc*2)>>16) - xx;
1574 int d=((xpos+xInc*3)>>16) - xx;
1576 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1577 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1578 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1579 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1584 int maxShift= 3-(d+1);
1587 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1589 funnyCode[fragmentPos + imm8OfPShufW1B]=
1590 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1591 funnyCode[fragmentPos + imm8OfPShufW2B]=
1592 a | (b<<2) | (c<<4) | (d<<6);
1594 if (i+3>=dstW) shift=maxShift; //avoid overread
1595 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1597 if (shift && i>=shift)
1599 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1600 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1601 filterPos[i/2]-=shift;
1604 fragmentPos+= fragmentLengthB;
1611 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1613 funnyCode[fragmentPos + imm8OfPShufW1A]=
1614 funnyCode[fragmentPos + imm8OfPShufW2A]=
1615 a | (b<<2) | (c<<4) | (d<<6);
1617 if (i+4>=dstW) shift=maxShift; //avoid overread
1618 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1620 if (shift && i>=shift)
1622 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1623 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1624 filterPos[i/2]-=shift;
1627 fragmentPos+= fragmentLengthA;
1630 funnyCode[fragmentPos]= RET;
1634 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1636 #endif /* COMPILE_MMX2 */
1638 static void globalInit(void){
1639 // generating tables:
1641 for (i=0; i<768; i++){
1642 int c= av_clip_uint8(i-256);
1647 static SwsFunc getSwsFunc(int flags){
1649 #if defined(RUNTIME_CPUDETECT) && CONFIG_GPL
1651 // ordered per speed fastest first
1652 if (flags & SWS_CPU_CAPS_MMX2)
1653 return swScale_MMX2;
1654 else if (flags & SWS_CPU_CAPS_3DNOW)
1655 return swScale_3DNow;
1656 else if (flags & SWS_CPU_CAPS_MMX)
1663 if (flags & SWS_CPU_CAPS_ALTIVEC)
1664 return swScale_altivec;
1669 #endif /* ARCH_X86 */
1670 #else //RUNTIME_CPUDETECT
1672 return swScale_MMX2;
1674 return swScale_3DNow;
1678 return swScale_altivec;
1682 #endif //!RUNTIME_CPUDETECT
1685 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1686 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1687 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1689 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1690 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1694 uint8_t *srcPtr= src[0];
1695 uint8_t *dstPtr= dst;
1696 for (i=0; i<srcSliceH; i++)
1698 memcpy(dstPtr, srcPtr, c->srcW);
1699 srcPtr+= srcStride[0];
1700 dstPtr+= dstStride[0];
1703 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1704 if (c->dstFormat == PIX_FMT_NV12)
1705 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1707 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1712 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1713 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1714 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1716 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1721 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1722 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1723 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1725 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1730 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1731 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1732 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1734 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1739 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1740 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1741 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1743 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1748 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1749 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1750 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1751 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1752 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1754 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1759 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1760 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1761 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1762 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1763 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1765 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1770 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1771 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1772 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1773 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1774 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1776 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1781 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1782 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1783 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1784 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1785 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1787 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1792 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1793 int srcSliceH, uint8_t* dst[], int dstStride[]){
1794 const enum PixelFormat srcFormat= c->srcFormat;
1795 const enum PixelFormat dstFormat= c->dstFormat;
1796 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1797 const uint8_t *palette)=NULL;
1799 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1800 uint8_t *srcPtr= src[0];
1802 if (!usePal(srcFormat))
1803 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1804 sws_format_name(srcFormat), sws_format_name(dstFormat));
1807 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1808 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1809 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1810 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1811 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1812 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
1813 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1814 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1818 for (i=0; i<srcSliceH; i++) {
1819 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1820 srcPtr+= srcStride[0];
1821 dstPtr+= dstStride[0];
1827 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1828 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1829 int srcSliceH, uint8_t* dst[], int dstStride[]){
1830 const enum PixelFormat srcFormat= c->srcFormat;
1831 const enum PixelFormat dstFormat= c->dstFormat;
1832 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1833 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1834 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1835 const int dstId= fmt_depth(dstFormat) >> 2;
1836 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1839 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1840 || (isRGB(srcFormat) && isRGB(dstFormat))){
1841 switch(srcId | (dstId<<4)){
1842 case 0x34: conv= rgb16to15; break;
1843 case 0x36: conv= rgb24to15; break;
1844 case 0x38: conv= rgb32to15; break;
1845 case 0x43: conv= rgb15to16; break;
1846 case 0x46: conv= rgb24to16; break;
1847 case 0x48: conv= rgb32to16; break;
1848 case 0x63: conv= rgb15to24; break;
1849 case 0x64: conv= rgb16to24; break;
1850 case 0x68: conv= rgb32to24; break;
1851 case 0x83: conv= rgb15to32; break;
1852 case 0x84: conv= rgb16to32; break;
1853 case 0x86: conv= rgb24to32; break;
1854 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1855 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1857 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1858 || (isRGB(srcFormat) && isBGR(dstFormat))){
1859 switch(srcId | (dstId<<4)){
1860 case 0x33: conv= rgb15tobgr15; break;
1861 case 0x34: conv= rgb16tobgr15; break;
1862 case 0x36: conv= rgb24tobgr15; break;
1863 case 0x38: conv= rgb32tobgr15; break;
1864 case 0x43: conv= rgb15tobgr16; break;
1865 case 0x44: conv= rgb16tobgr16; break;
1866 case 0x46: conv= rgb24tobgr16; break;
1867 case 0x48: conv= rgb32tobgr16; break;
1868 case 0x63: conv= rgb15tobgr24; break;
1869 case 0x64: conv= rgb16tobgr24; break;
1870 case 0x66: conv= rgb24tobgr24; break;
1871 case 0x68: conv= rgb32tobgr24; break;
1872 case 0x83: conv= rgb15tobgr32; break;
1873 case 0x84: conv= rgb16tobgr32; break;
1874 case 0x86: conv= rgb24tobgr32; break;
1875 case 0x88: conv= rgb32tobgr32; break;
1876 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1877 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1880 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1881 sws_format_name(srcFormat), sws_format_name(dstFormat));
1886 uint8_t *srcPtr= src[0];
1887 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1888 srcPtr += ALT32_CORR;
1890 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1891 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1895 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1897 for (i=0; i<srcSliceH; i++)
1899 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1900 srcPtr+= srcStride[0];
1901 dstPtr+= dstStride[0];
1908 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1909 int srcSliceH, uint8_t* dst[], int dstStride[]){
1913 dst[0]+ srcSliceY *dstStride[0],
1914 dst[1]+(srcSliceY>>1)*dstStride[1],
1915 dst[2]+(srcSliceY>>1)*dstStride[2],
1917 dstStride[0], dstStride[1], srcStride[0]);
1921 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1922 int srcSliceH, uint8_t* dst[], int dstStride[]){
1926 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1927 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1929 uint8_t *srcPtr= src[0];
1930 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1932 for (i=0; i<srcSliceH; i++)
1934 memcpy(dstPtr, srcPtr, c->srcW);
1935 srcPtr+= srcStride[0];
1936 dstPtr+= dstStride[0];
1940 if (c->dstFormat==PIX_FMT_YUV420P){
1941 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1942 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1944 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1945 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1950 /* unscaled copy like stuff (assumes nearly identical formats) */
1951 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1952 int srcSliceH, uint8_t* dst[], int dstStride[])
1954 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1955 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1959 uint8_t *srcPtr= src[0];
1960 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1963 /* universal length finder */
1964 while(length+c->srcW <= FFABS(dstStride[0])
1965 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1968 for (i=0; i<srcSliceH; i++)
1970 memcpy(dstPtr, srcPtr, length);
1971 srcPtr+= srcStride[0];
1972 dstPtr+= dstStride[0];
1978 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1979 int srcSliceH, uint8_t* dst[], int dstStride[])
1982 for (plane=0; plane<3; plane++)
1984 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1985 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1986 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1988 if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1990 if (!isGray(c->dstFormat))
1991 fillPlane(dst[plane], dstStride[plane], length, height, y, 128);
1995 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1996 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2000 uint8_t *srcPtr= src[plane];
2001 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2002 for (i=0; i<height; i++)
2004 memcpy(dstPtr, srcPtr, length);
2005 srcPtr+= srcStride[plane];
2006 dstPtr+= dstStride[plane];
2014 static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2015 int srcSliceH, uint8_t* dst[], int dstStride[]){
2017 int length= c->srcW;
2019 int height= srcSliceH;
2021 uint8_t *srcPtr= src[0];
2022 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2024 if (!isGray(c->dstFormat)){
2025 int height= -((-srcSliceH)>>c->chrDstVSubSample);
2026 memset(dst[1], 128, dstStride[1]*height);
2027 memset(dst[2], 128, dstStride[2]*height);
2029 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
2030 for (i=0; i<height; i++)
2032 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2033 srcPtr+= srcStride[0];
2034 dstPtr+= dstStride[0];
2039 static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2040 int srcSliceH, uint8_t* dst[], int dstStride[]){
2042 int length= c->srcW;
2044 int height= srcSliceH;
2046 uint8_t *srcPtr= src[0];
2047 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
2048 for (i=0; i<height; i++)
2050 for (j=0; j<length; j++)
2052 dstPtr[j<<1] = srcPtr[j];
2053 dstPtr[(j<<1)+1] = srcPtr[j];
2055 srcPtr+= srcStride[0];
2056 dstPtr+= dstStride[0];
2061 static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2062 int srcSliceH, uint8_t* dst[], int dstStride[]){
2064 int length= c->srcW;
2066 int height= srcSliceH;
2068 uint16_t *srcPtr= (uint16_t*)src[0];
2069 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
2070 for (i=0; i<height; i++)
2072 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
2073 srcPtr+= srcStride[0]/2;
2074 dstPtr+= dstStride[0]/2;
2080 static void getSubSampleFactors(int *h, int *v, int format){
2082 case PIX_FMT_UYVY422:
2083 case PIX_FMT_YUYV422:
2087 case PIX_FMT_YUV420P:
2088 case PIX_FMT_YUVA420P:
2089 case PIX_FMT_GRAY16BE:
2090 case PIX_FMT_GRAY16LE:
2091 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2097 case PIX_FMT_YUV440P:
2101 case PIX_FMT_YUV410P:
2105 case PIX_FMT_YUV444P:
2109 case PIX_FMT_YUV422P:
2113 case PIX_FMT_YUV411P:
2124 static uint16_t roundToInt16(int64_t f){
2125 int r= (f + (1<<15))>>16;
2126 if (r<-0x7FFF) return 0x8000;
2127 else if (r> 0x7FFF) return 0x7FFF;
2132 * @param inv_table the yuv2rgb coefficients, normally ff_yuv2rgb_coeffs[x]
2133 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
2134 * @return -1 if not supported
2136 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2137 int64_t crv = inv_table[0];
2138 int64_t cbu = inv_table[1];
2139 int64_t cgu = -inv_table[2];
2140 int64_t cgv = -inv_table[3];
2144 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2145 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2147 c->brightness= brightness;
2148 c->contrast = contrast;
2149 c->saturation= saturation;
2150 c->srcRange = srcRange;
2151 c->dstRange = dstRange;
2152 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
2154 c->uOffset= 0x0400040004000400LL;
2155 c->vOffset= 0x0400040004000400LL;
2161 crv= (crv*224) / 255;
2162 cbu= (cbu*224) / 255;
2163 cgu= (cgu*224) / 255;
2164 cgv= (cgv*224) / 255;
2167 cy = (cy *contrast )>>16;
2168 crv= (crv*contrast * saturation)>>32;
2169 cbu= (cbu*contrast * saturation)>>32;
2170 cgu= (cgu*contrast * saturation)>>32;
2171 cgv= (cgv*contrast * saturation)>>32;
2173 oy -= 256*brightness;
2175 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2176 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2177 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2178 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2179 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2180 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2182 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2183 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2184 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2185 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2186 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2187 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2189 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2192 #ifdef COMPILE_ALTIVEC
2193 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2194 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2200 * @return -1 if not supported
2202 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2203 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2205 *inv_table = c->srcColorspaceTable;
2206 *table = c->dstColorspaceTable;
2207 *srcRange = c->srcRange;
2208 *dstRange = c->dstRange;
2209 *brightness= c->brightness;
2210 *contrast = c->contrast;
2211 *saturation= c->saturation;
2216 static int handle_jpeg(enum PixelFormat *format)
2219 case PIX_FMT_YUVJ420P:
2220 *format = PIX_FMT_YUV420P;
2222 case PIX_FMT_YUVJ422P:
2223 *format = PIX_FMT_YUV422P;
2225 case PIX_FMT_YUVJ444P:
2226 *format = PIX_FMT_YUV444P;
2228 case PIX_FMT_YUVJ440P:
2229 *format = PIX_FMT_YUV440P;
2236 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2237 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2241 int usesVFilter, usesHFilter;
2242 int unscaled, needsDither;
2243 int srcRange, dstRange;
2244 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2246 if (flags & SWS_CPU_CAPS_MMX)
2247 __asm__ volatile("emms\n\t"::: "memory");
2250 #if !defined(RUNTIME_CPUDETECT) || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
2251 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2253 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2255 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2257 flags |= SWS_CPU_CAPS_MMX;
2259 flags |= SWS_CPU_CAPS_ALTIVEC;
2261 flags |= SWS_CPU_CAPS_BFIN;
2263 #endif /* RUNTIME_CPUDETECT */
2264 if (clip_table[512] != 255) globalInit();
2265 if (!rgb15to16) sws_rgb2rgb_init(flags);
2267 unscaled = (srcW == dstW && srcH == dstH);
2268 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2269 && (fmt_depth(dstFormat))<24
2270 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2272 srcRange = handle_jpeg(&srcFormat);
2273 dstRange = handle_jpeg(&dstFormat);
2275 if (!isSupportedIn(srcFormat))
2277 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2280 if (!isSupportedOut(dstFormat))
2282 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2286 i= flags & ( SWS_POINT
2297 if(!i || (i & (i-1)))
2299 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2304 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
2306 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2307 srcW, srcH, dstW, dstH);
2310 if(srcW > VOFW || dstW > VOFW){
2311 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2315 if (!dstFilter) dstFilter= &dummyFilter;
2316 if (!srcFilter) srcFilter= &dummyFilter;
2318 c= av_mallocz(sizeof(SwsContext));
2320 c->av_class = &sws_context_class;
2325 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2326 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2328 c->dstFormat= dstFormat;
2329 c->srcFormat= srcFormat;
2330 c->vRounder= 4* 0x0001000100010001ULL;
2332 usesHFilter= usesVFilter= 0;
2333 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2334 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2335 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2336 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2337 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2338 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2339 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2340 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2342 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2343 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2345 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2346 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2348 // drop some chroma lines if the user wants it
2349 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2350 c->chrSrcVSubSample+= c->vChrDrop;
2352 // drop every other pixel for chroma calculation unless user wants full chroma
2353 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2354 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2355 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2356 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2357 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2358 c->chrSrcHSubSample=1;
2361 c->param[0] = param[0];
2362 c->param[1] = param[1];
2365 c->param[1] = SWS_PARAM_DEFAULT;
2368 c->chrIntHSubSample= c->chrDstHSubSample;
2369 c->chrIntVSubSample= c->chrSrcVSubSample;
2371 // Note the -((-x)>>y) is so that we always round toward +inf.
2372 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2373 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2374 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2375 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2377 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2379 /* unscaled special cases */
2380 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2383 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2385 c->swScale= PlanarToNV12Wrapper;
2388 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2389 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2391 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2394 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
2396 c->swScale= yvu9toyv12Wrapper;
2400 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
2401 c->swScale= bgr24toyv12Wrapper;
2403 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2404 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2405 && (isBGR(dstFormat) || isRGB(dstFormat))
2406 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2407 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2408 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2409 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2410 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2411 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2412 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2413 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2414 && dstFormat != PIX_FMT_RGB32_1
2415 && dstFormat != PIX_FMT_BGR32_1
2416 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2417 c->swScale= rgb2rgbWrapper;
2419 if ((usePal(srcFormat) && (
2420 dstFormat == PIX_FMT_RGB32 ||
2421 dstFormat == PIX_FMT_RGB32_1 ||
2422 dstFormat == PIX_FMT_RGB24 ||
2423 dstFormat == PIX_FMT_BGR32 ||
2424 dstFormat == PIX_FMT_BGR32_1 ||
2425 dstFormat == PIX_FMT_BGR24)))
2426 c->swScale= pal2rgbWrapper;
2428 if (srcFormat == PIX_FMT_YUV422P)
2430 if (dstFormat == PIX_FMT_YUYV422)
2431 c->swScale= YUV422PToYuy2Wrapper;
2432 else if (dstFormat == PIX_FMT_UYVY422)
2433 c->swScale= YUV422PToUyvyWrapper;
2436 /* LQ converters if -sws 0 or -sws 4*/
2437 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2439 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2441 if (dstFormat == PIX_FMT_YUYV422)
2442 c->swScale= PlanarToYuy2Wrapper;
2443 else if (dstFormat == PIX_FMT_UYVY422)
2444 c->swScale= PlanarToUyvyWrapper;
2447 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV420P)
2448 c->swScale= YUYV2YUV420Wrapper;
2449 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV420P)
2450 c->swScale= UYVY2YUV420Wrapper;
2452 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2453 c->swScale= YUYV2YUV422Wrapper;
2454 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2455 c->swScale= UYVY2YUV422Wrapper;
2457 #ifdef COMPILE_ALTIVEC
2458 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2459 !(c->flags & SWS_BITEXACT) &&
2460 srcFormat == PIX_FMT_YUV420P) {
2461 // unscaled YV12 -> packed YUV, we want speed
2462 if (dstFormat == PIX_FMT_YUYV422)
2463 c->swScale= yv12toyuy2_unscaled_altivec;
2464 else if (dstFormat == PIX_FMT_UYVY422)
2465 c->swScale= yv12touyvy_unscaled_altivec;
2470 if ( srcFormat == dstFormat
2471 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2472 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2473 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2475 if (isPacked(c->srcFormat))
2476 c->swScale= packedCopy;
2477 else /* Planar YUV or gray */
2478 c->swScale= planarCopy;
2481 /* gray16{le,be} conversions */
2482 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2484 c->swScale= gray16togray;
2486 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2488 c->swScale= graytogray16;
2490 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2492 c->swScale= gray16swap;
2496 if (flags & SWS_CPU_CAPS_BFIN)
2497 ff_bfin_get_unscaled_swscale (c);
2501 if (flags&SWS_PRINT_INFO)
2502 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2503 sws_format_name(srcFormat), sws_format_name(dstFormat));
2508 if (flags & SWS_CPU_CAPS_MMX2)
2510 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2511 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2513 if (flags&SWS_PRINT_INFO)
2514 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2516 if (usesHFilter) c->canMMX2BeUsed=0;
2521 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2522 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2524 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2525 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2526 // n-2 is the last chrominance sample available
2527 // this is not perfect, but no one should notice the difference, the more correct variant
2528 // would be like the vertical one, but that would require some special code for the
2529 // first and last pixel
2530 if (flags&SWS_FAST_BILINEAR)
2532 if (c->canMMX2BeUsed)
2537 //we don't use the x86 asm scaler if MMX is available
2538 else if (flags & SWS_CPU_CAPS_MMX)
2540 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2541 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2545 /* precalculate horizontal scaler filter coefficients */
2547 const int filterAlign=
2548 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2549 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2552 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2553 srcW , dstW, filterAlign, 1<<14,
2554 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2555 srcFilter->lumH, dstFilter->lumH, c->param);
2556 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2557 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2558 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2559 srcFilter->chrH, dstFilter->chrH, c->param);
2561 #define MAX_FUNNY_CODE_SIZE 10000
2562 #if defined(COMPILE_MMX2)
2563 // can't downscale !!!
2564 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2566 #ifdef MAP_ANONYMOUS
2567 c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2568 c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2570 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2571 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2574 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2575 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2576 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2577 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2579 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2580 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2582 #endif /* defined(COMPILE_MMX2) */
2583 } // initialize horizontal stuff
2587 /* precalculate vertical scaler filter coefficients */
2589 const int filterAlign=
2590 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2591 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2594 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2595 srcH , dstH, filterAlign, (1<<12),
2596 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2597 srcFilter->lumV, dstFilter->lumV, c->param);
2598 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2599 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2600 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2601 srcFilter->chrV, dstFilter->chrV, c->param);
2604 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2605 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2607 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2609 short *p = (short *)&c->vYCoeffsBank[i];
2611 p[j] = c->vLumFilter[i];
2614 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2616 short *p = (short *)&c->vCCoeffsBank[i];
2618 p[j] = c->vChrFilter[i];
2623 // calculate buffer sizes so that they won't run out while handling these damn slices
2624 c->vLumBufSize= c->vLumFilterSize;
2625 c->vChrBufSize= c->vChrFilterSize;
2626 for (i=0; i<dstH; i++)
2628 int chrI= i*c->chrDstH / dstH;
2629 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2630 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2632 nextSlice>>= c->chrSrcVSubSample;
2633 nextSlice<<= c->chrSrcVSubSample;
2634 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2635 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2636 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2637 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2640 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2641 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2642 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2643 //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)
2644 /* align at 16 bytes for AltiVec */
2645 for (i=0; i<c->vLumBufSize; i++)
2646 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2647 for (i=0; i<c->vChrBufSize; i++)
2648 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2650 //try to avoid drawing green stuff between the right end and the stride end
2651 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2653 assert(2*VOFW == VOF);
2655 assert(c->chrDstH <= dstH);
2657 if (flags&SWS_PRINT_INFO)
2660 const char *dither= " dithered";
2662 const char *dither= "";
2664 if (flags&SWS_FAST_BILINEAR)
2665 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2666 else if (flags&SWS_BILINEAR)
2667 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2668 else if (flags&SWS_BICUBIC)
2669 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2670 else if (flags&SWS_X)
2671 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2672 else if (flags&SWS_POINT)
2673 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2674 else if (flags&SWS_AREA)
2675 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2676 else if (flags&SWS_BICUBLIN)
2677 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2678 else if (flags&SWS_GAUSS)
2679 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2680 else if (flags&SWS_SINC)
2681 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2682 else if (flags&SWS_LANCZOS)
2683 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2684 else if (flags&SWS_SPLINE)
2685 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2687 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2689 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2690 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2691 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2693 av_log(c, AV_LOG_INFO, "from %s to %s ",
2694 sws_format_name(srcFormat), sws_format_name(dstFormat));
2696 if (flags & SWS_CPU_CAPS_MMX2)
2697 av_log(c, AV_LOG_INFO, "using MMX2\n");
2698 else if (flags & SWS_CPU_CAPS_3DNOW)
2699 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2700 else if (flags & SWS_CPU_CAPS_MMX)
2701 av_log(c, AV_LOG_INFO, "using MMX\n");
2702 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2703 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2705 av_log(c, AV_LOG_INFO, "using C\n");
2708 if (flags & SWS_PRINT_INFO)
2710 if (flags & SWS_CPU_CAPS_MMX)
2712 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2713 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2716 if (c->hLumFilterSize==4)
2717 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2718 else if (c->hLumFilterSize==8)
2719 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2721 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2723 if (c->hChrFilterSize==4)
2724 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2725 else if (c->hChrFilterSize==8)
2726 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2728 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2734 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2736 if (flags & SWS_FAST_BILINEAR)
2737 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2739 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2742 if (isPlanarYUV(dstFormat))
2744 if (c->vLumFilterSize==1)
2745 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2747 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2751 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2752 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2753 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2754 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2755 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2757 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2760 if (dstFormat==PIX_FMT_BGR24)
2761 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2762 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2763 else if (dstFormat==PIX_FMT_RGB32)
2764 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2765 else if (dstFormat==PIX_FMT_BGR565)
2766 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2767 else if (dstFormat==PIX_FMT_BGR555)
2768 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2770 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2772 if (flags & SWS_PRINT_INFO)
2774 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2775 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2776 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2777 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2780 c->swScale= getSwsFunc(flags);
2785 * swscale wrapper, so we don't need to export the SwsContext.
2786 * Assumes planar YUV to be in YUV order instead of YVU.
2788 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2789 int srcSliceH, uint8_t* dst[], int dstStride[]){
2791 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2793 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2794 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2797 if (c->sliceDir == 0) {
2798 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2801 if (usePal(c->srcFormat)){
2802 for (i=0; i<256; i++){
2803 int p, r, g, b,y,u,v;
2804 if(c->srcFormat == PIX_FMT_PAL8){
2805 p=((uint32_t*)(src[1]))[i];
2809 }else if(c->srcFormat == PIX_FMT_RGB8){
2813 }else if(c->srcFormat == PIX_FMT_BGR8){
2817 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2822 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2827 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2828 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2829 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2830 c->pal_yuv[i]= y + (u<<8) + (v<<16);
2833 switch(c->dstFormat) {
2835 #ifndef WORDS_BIGENDIAN
2838 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2840 case PIX_FMT_BGR32_1:
2841 #ifdef WORDS_BIGENDIAN
2844 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2846 case PIX_FMT_RGB32_1:
2847 #ifdef WORDS_BIGENDIAN
2850 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2853 #ifndef WORDS_BIGENDIAN
2857 c->pal_rgb[i]= b + (g<<8) + (r<<16);
2862 // copy strides, so they can safely be modified
2863 if (c->sliceDir == 1) {
2864 // slices go from top to bottom
2865 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2866 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2867 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2869 // slices go from bottom to top => we flip the image internally
2870 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2871 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2872 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2],
2873 dst[3] + (c->dstH-1)*dstStride[3]};
2874 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2875 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2877 src2[0] += (srcSliceH-1)*srcStride[0];
2878 if (!usePal(c->srcFormat))
2879 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2880 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2881 src2[3] += (srcSliceH-1)*srcStride[3];
2883 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2887 #if LIBSWSCALE_VERSION_MAJOR < 1
2888 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2889 int srcSliceH, uint8_t* dst[], int dstStride[]){
2890 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2894 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2895 float lumaSharpen, float chromaSharpen,
2896 float chromaHShift, float chromaVShift,
2899 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2901 if (lumaGBlur!=0.0){
2902 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2903 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2905 filter->lumH= sws_getIdentityVec();
2906 filter->lumV= sws_getIdentityVec();
2909 if (chromaGBlur!=0.0){
2910 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2911 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2913 filter->chrH= sws_getIdentityVec();
2914 filter->chrV= sws_getIdentityVec();
2917 if (chromaSharpen!=0.0){
2918 SwsVector *id= sws_getIdentityVec();
2919 sws_scaleVec(filter->chrH, -chromaSharpen);
2920 sws_scaleVec(filter->chrV, -chromaSharpen);
2921 sws_addVec(filter->chrH, id);
2922 sws_addVec(filter->chrV, id);
2926 if (lumaSharpen!=0.0){
2927 SwsVector *id= sws_getIdentityVec();
2928 sws_scaleVec(filter->lumH, -lumaSharpen);
2929 sws_scaleVec(filter->lumV, -lumaSharpen);
2930 sws_addVec(filter->lumH, id);
2931 sws_addVec(filter->lumV, id);
2935 if (chromaHShift != 0.0)
2936 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2938 if (chromaVShift != 0.0)
2939 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2941 sws_normalizeVec(filter->chrH, 1.0);
2942 sws_normalizeVec(filter->chrV, 1.0);
2943 sws_normalizeVec(filter->lumH, 1.0);
2944 sws_normalizeVec(filter->lumV, 1.0);
2946 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2947 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
2952 SwsVector *sws_getGaussianVec(double variance, double quality){
2953 const int length= (int)(variance*quality + 0.5) | 1;
2955 double *coeff= av_malloc(length*sizeof(double));
2956 double middle= (length-1)*0.5;
2957 SwsVector *vec= av_malloc(sizeof(SwsVector));
2960 vec->length= length;
2962 for (i=0; i<length; i++)
2964 double dist= i-middle;
2965 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
2968 sws_normalizeVec(vec, 1.0);
2973 SwsVector *sws_getConstVec(double c, int length){
2975 double *coeff= av_malloc(length*sizeof(double));
2976 SwsVector *vec= av_malloc(sizeof(SwsVector));
2979 vec->length= length;
2981 for (i=0; i<length; i++)
2988 SwsVector *sws_getIdentityVec(void){
2989 return sws_getConstVec(1.0, 1);
2992 double sws_dcVec(SwsVector *a){
2996 for (i=0; i<a->length; i++)
3002 void sws_scaleVec(SwsVector *a, double scalar){
3005 for (i=0; i<a->length; i++)
3006 a->coeff[i]*= scalar;
3009 void sws_normalizeVec(SwsVector *a, double height){
3010 sws_scaleVec(a, height/sws_dcVec(a));
3013 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3014 int length= a->length + b->length - 1;
3015 double *coeff= av_malloc(length*sizeof(double));
3017 SwsVector *vec= av_malloc(sizeof(SwsVector));
3020 vec->length= length;
3022 for (i=0; i<length; i++) coeff[i]= 0.0;
3024 for (i=0; i<a->length; i++)
3026 for (j=0; j<b->length; j++)
3028 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3035 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3036 int length= FFMAX(a->length, b->length);
3037 double *coeff= av_malloc(length*sizeof(double));
3039 SwsVector *vec= av_malloc(sizeof(SwsVector));
3042 vec->length= length;
3044 for (i=0; i<length; i++) coeff[i]= 0.0;
3046 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3047 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3052 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3053 int length= FFMAX(a->length, b->length);
3054 double *coeff= av_malloc(length*sizeof(double));
3056 SwsVector *vec= av_malloc(sizeof(SwsVector));
3059 vec->length= length;
3061 for (i=0; i<length; i++) coeff[i]= 0.0;
3063 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3064 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3069 /* shift left / or right if "shift" is negative */
3070 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3071 int length= a->length + FFABS(shift)*2;
3072 double *coeff= av_malloc(length*sizeof(double));
3074 SwsVector *vec= av_malloc(sizeof(SwsVector));
3077 vec->length= length;
3079 for (i=0; i<length; i++) coeff[i]= 0.0;
3081 for (i=0; i<a->length; i++)
3083 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3089 void sws_shiftVec(SwsVector *a, int shift){
3090 SwsVector *shifted= sws_getShiftedVec(a, shift);
3092 a->coeff= shifted->coeff;
3093 a->length= shifted->length;
3097 void sws_addVec(SwsVector *a, SwsVector *b){
3098 SwsVector *sum= sws_sumVec(a, b);
3100 a->coeff= sum->coeff;
3101 a->length= sum->length;
3105 void sws_subVec(SwsVector *a, SwsVector *b){
3106 SwsVector *diff= sws_diffVec(a, b);
3108 a->coeff= diff->coeff;
3109 a->length= diff->length;
3113 void sws_convVec(SwsVector *a, SwsVector *b){
3114 SwsVector *conv= sws_getConvVec(a, b);
3116 a->coeff= conv->coeff;
3117 a->length= conv->length;
3121 SwsVector *sws_cloneVec(SwsVector *a){
3122 double *coeff= av_malloc(a->length*sizeof(double));
3124 SwsVector *vec= av_malloc(sizeof(SwsVector));
3127 vec->length= a->length;
3129 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3134 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3140 for (i=0; i<a->length; i++)
3141 if (a->coeff[i]>max) max= a->coeff[i];
3143 for (i=0; i<a->length; i++)
3144 if (a->coeff[i]<min) min= a->coeff[i];
3148 for (i=0; i<a->length; i++)
3150 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3151 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3152 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3153 av_log(log_ctx, log_level, "|\n");
3157 #if LIBSWSCALE_VERSION_MAJOR < 1
3158 void sws_printVec(SwsVector *a){
3159 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3163 void sws_freeVec(SwsVector *a){
3165 av_freep(&a->coeff);
3170 void sws_freeFilter(SwsFilter *filter){
3171 if (!filter) return;
3173 if (filter->lumH) sws_freeVec(filter->lumH);
3174 if (filter->lumV) sws_freeVec(filter->lumV);
3175 if (filter->chrH) sws_freeVec(filter->chrH);
3176 if (filter->chrV) sws_freeVec(filter->chrV);
3181 void sws_freeContext(SwsContext *c){
3187 for (i=0; i<c->vLumBufSize; i++)
3188 av_freep(&c->lumPixBuf[i]);
3189 av_freep(&c->lumPixBuf);
3194 for (i=0; i<c->vChrBufSize; i++)
3195 av_freep(&c->chrPixBuf[i]);
3196 av_freep(&c->chrPixBuf);
3199 av_freep(&c->vLumFilter);
3200 av_freep(&c->vChrFilter);
3201 av_freep(&c->hLumFilter);
3202 av_freep(&c->hChrFilter);
3204 av_freep(&c->vYCoeffsBank);
3205 av_freep(&c->vCCoeffsBank);
3208 av_freep(&c->vLumFilterPos);
3209 av_freep(&c->vChrFilterPos);
3210 av_freep(&c->hLumFilterPos);
3211 av_freep(&c->hChrFilterPos);
3213 #if ARCH_X86 && CONFIG_GPL
3214 #ifdef MAP_ANONYMOUS
3215 if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
3216 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3218 av_free(c->funnyYCode);
3219 av_free(c->funnyUVCode);
3222 c->funnyUVCode=NULL;
3223 #endif /* ARCH_X86 && CONFIG_GPL */
3225 av_freep(&c->lumMmx2Filter);
3226 av_freep(&c->chrMmx2Filter);
3227 av_freep(&c->lumMmx2FilterPos);
3228 av_freep(&c->chrMmx2FilterPos);
3229 av_freep(&c->yuvTable);
3234 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3235 int srcW, int srcH, enum PixelFormat srcFormat,
3236 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3237 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
3239 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3242 param = default_param;
3245 if (context->srcW != srcW || context->srcH != srcH ||
3246 context->srcFormat != srcFormat ||
3247 context->dstW != dstW || context->dstH != dstH ||
3248 context->dstFormat != dstFormat || context->flags != flags ||
3249 context->param[0] != param[0] || context->param[1] != param[1])
3251 sws_freeContext(context);
3256 return sws_getContext(srcW, srcH, srcFormat,
3257 dstW, dstH, dstFormat, flags,
3258 srcFilter, dstFilter, param);