2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27 {BGR,RGB}{1,4,8,15,16} support dithering
29 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30 YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
41 tested special converters (most are tested actually, but I did not write it down ...)
48 untested special converters
49 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
50 YV12/I420 -> YV12/I420
51 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
52 BGR24 -> BGR32 & RGB24 -> RGB32
53 BGR32 -> BGR24 & RGB32 -> RGB24
57 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
66 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67 #define MAP_ANONYMOUS MAP_ANON
71 #define WIN32_LEAN_AND_MEAN
75 #include "swscale_internal.h"
77 #include "libavutil/intreadwrite.h"
78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/avutil.h"
80 #include "libavutil/bswap.h"
82 unsigned swscale_version(void)
84 return LIBSWSCALE_VERSION_INT;
91 //#define HAVE_AMD3DNOW
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_RGB48BE \
112 || (x)==PIX_FMT_RGB48LE \
113 || (x)==PIX_FMT_RGB32 \
114 || (x)==PIX_FMT_RGB32_1 \
115 || (x)==PIX_FMT_BGR24 \
116 || (x)==PIX_FMT_BGR565 \
117 || (x)==PIX_FMT_BGR555 \
118 || (x)==PIX_FMT_BGR32 \
119 || (x)==PIX_FMT_BGR32_1 \
120 || (x)==PIX_FMT_RGB24 \
121 || (x)==PIX_FMT_RGB565 \
122 || (x)==PIX_FMT_RGB555 \
123 || (x)==PIX_FMT_GRAY8 \
124 || (x)==PIX_FMT_YUV410P \
125 || (x)==PIX_FMT_YUV440P \
126 || (x)==PIX_FMT_GRAY16BE \
127 || (x)==PIX_FMT_GRAY16LE \
128 || (x)==PIX_FMT_YUV444P \
129 || (x)==PIX_FMT_YUV422P \
130 || (x)==PIX_FMT_YUV411P \
131 || (x)==PIX_FMT_PAL8 \
132 || (x)==PIX_FMT_BGR8 \
133 || (x)==PIX_FMT_RGB8 \
134 || (x)==PIX_FMT_BGR4_BYTE \
135 || (x)==PIX_FMT_RGB4_BYTE \
136 || (x)==PIX_FMT_YUV440P \
137 || (x)==PIX_FMT_MONOWHITE \
138 || (x)==PIX_FMT_MONOBLACK \
139 || (x)==PIX_FMT_YUV420P16LE \
140 || (x)==PIX_FMT_YUV422P16LE \
141 || (x)==PIX_FMT_YUV444P16LE \
142 || (x)==PIX_FMT_YUV420P16BE \
143 || (x)==PIX_FMT_YUV422P16BE \
144 || (x)==PIX_FMT_YUV444P16BE \
146 #define isSupportedOut(x) ( \
147 (x)==PIX_FMT_YUV420P \
148 || (x)==PIX_FMT_YUVA420P \
149 || (x)==PIX_FMT_YUYV422 \
150 || (x)==PIX_FMT_UYVY422 \
151 || (x)==PIX_FMT_YUV444P \
152 || (x)==PIX_FMT_YUV422P \
153 || (x)==PIX_FMT_YUV411P \
156 || (x)==PIX_FMT_NV12 \
157 || (x)==PIX_FMT_NV21 \
158 || (x)==PIX_FMT_GRAY16BE \
159 || (x)==PIX_FMT_GRAY16LE \
160 || (x)==PIX_FMT_GRAY8 \
161 || (x)==PIX_FMT_YUV410P \
162 || (x)==PIX_FMT_YUV440P \
163 || (x)==PIX_FMT_YUV420P16LE \
164 || (x)==PIX_FMT_YUV422P16LE \
165 || (x)==PIX_FMT_YUV444P16LE \
166 || (x)==PIX_FMT_YUV420P16BE \
167 || (x)==PIX_FMT_YUV422P16BE \
168 || (x)==PIX_FMT_YUV444P16BE \
170 #define isPacked(x) ( \
172 || (x)==PIX_FMT_YUYV422 \
173 || (x)==PIX_FMT_UYVY422 \
177 #define usePal(x) ( \
179 || (x)==PIX_FMT_BGR4_BYTE \
180 || (x)==PIX_FMT_RGB4_BYTE \
181 || (x)==PIX_FMT_BGR8 \
182 || (x)==PIX_FMT_RGB8 \
185 #define RGB2YUV_SHIFT 15
186 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
187 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
188 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
189 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
190 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
191 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
192 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
193 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
194 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
196 extern const int32_t ff_yuv2rgb_coeffs[8][4];
198 static const double rgb2yuv_table[8][9]={
199 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
200 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
201 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
202 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
203 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
204 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
205 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
206 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
211 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
214 more intelligent misalignment avoidance for the horizontal scaler
215 write special vertical cubic upscale version
216 optimize C code (YV12 / minmax)
217 add support for packed pixel YUV input & output
218 add support for Y8 output
219 optimize BGR24 & BGR32
220 add BGR4 output support
221 write special BGR->BGR scaler
224 #if ARCH_X86 && CONFIG_GPL
225 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
226 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
227 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
228 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
229 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
230 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
231 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
232 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
234 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
235 0x0103010301030103LL,
236 0x0200020002000200LL,};
238 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
239 0x0602060206020602LL,
240 0x0004000400040004LL,};
242 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
243 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
244 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
245 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
246 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
247 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
250 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
251 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
254 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
255 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
256 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
258 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
259 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
260 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
261 #endif /* FAST_BGR2YV12 */
262 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
263 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
264 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
266 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
267 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
268 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
269 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
270 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
272 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
273 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
274 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
277 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
279 #endif /* ARCH_X86 && CONFIG_GPL */
281 // clipping helper table for C implementations:
282 static unsigned char clip_table[768];
284 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
286 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4[2][8])={
287 { 1, 3, 1, 3, 1, 3, 1, 3, },
288 { 2, 0, 2, 0, 2, 0, 2, 0, },
291 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8[2][8])={
292 { 6, 2, 6, 2, 6, 2, 6, 2, },
293 { 0, 4, 0, 4, 0, 4, 0, 4, },
296 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32[8][8])={
297 { 17, 9, 23, 15, 16, 8, 22, 14, },
298 { 5, 29, 3, 27, 4, 28, 2, 26, },
299 { 21, 13, 19, 11, 20, 12, 18, 10, },
300 { 0, 24, 6, 30, 1, 25, 7, 31, },
301 { 16, 8, 22, 14, 17, 9, 23, 15, },
302 { 4, 28, 2, 26, 5, 29, 3, 27, },
303 { 20, 12, 18, 10, 21, 13, 19, 11, },
304 { 1, 25, 7, 31, 0, 24, 6, 30, },
307 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73[8][8])={
308 { 0, 55, 14, 68, 3, 58, 17, 72, },
309 { 37, 18, 50, 32, 40, 22, 54, 35, },
310 { 9, 64, 5, 59, 13, 67, 8, 63, },
311 { 46, 27, 41, 23, 49, 31, 44, 26, },
312 { 2, 57, 16, 71, 1, 56, 15, 70, },
313 { 39, 21, 52, 34, 38, 19, 51, 33, },
314 { 11, 66, 7, 62, 10, 65, 6, 60, },
315 { 48, 30, 43, 25, 47, 29, 42, 24, },
319 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
320 {117, 62, 158, 103, 113, 58, 155, 100, },
321 { 34, 199, 21, 186, 31, 196, 17, 182, },
322 {144, 89, 131, 76, 141, 86, 127, 72, },
323 { 0, 165, 41, 206, 10, 175, 52, 217, },
324 {110, 55, 151, 96, 120, 65, 162, 107, },
325 { 28, 193, 14, 179, 38, 203, 24, 189, },
326 {138, 83, 124, 69, 148, 93, 134, 79, },
327 { 7, 172, 48, 213, 3, 168, 45, 210, },
330 // tries to correct a gamma of 1.5
331 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
332 { 0, 143, 18, 200, 2, 156, 25, 215, },
333 { 78, 28, 125, 64, 89, 36, 138, 74, },
334 { 10, 180, 3, 161, 16, 195, 8, 175, },
335 {109, 51, 93, 38, 121, 60, 105, 47, },
336 { 1, 152, 23, 210, 0, 147, 20, 205, },
337 { 85, 33, 134, 71, 81, 30, 130, 67, },
338 { 14, 190, 6, 171, 12, 185, 5, 166, },
339 {117, 57, 101, 44, 113, 54, 97, 41, },
342 // tries to correct a gamma of 2.0
343 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
344 { 0, 124, 8, 193, 0, 140, 12, 213, },
345 { 55, 14, 104, 42, 66, 19, 119, 52, },
346 { 3, 168, 1, 145, 6, 187, 3, 162, },
347 { 86, 31, 70, 21, 99, 39, 82, 28, },
348 { 0, 134, 11, 206, 0, 129, 9, 200, },
349 { 62, 17, 114, 48, 58, 16, 109, 45, },
350 { 5, 181, 2, 157, 4, 175, 1, 151, },
351 { 95, 36, 78, 26, 90, 34, 74, 24, },
354 // tries to correct a gamma of 2.5
355 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220[8][8])={
356 { 0, 107, 3, 187, 0, 125, 6, 212, },
357 { 39, 7, 86, 28, 49, 11, 102, 36, },
358 { 1, 158, 0, 131, 3, 180, 1, 151, },
359 { 68, 19, 52, 12, 81, 25, 64, 17, },
360 { 0, 119, 5, 203, 0, 113, 4, 195, },
361 { 45, 9, 96, 33, 42, 8, 91, 30, },
362 { 2, 172, 1, 144, 2, 165, 0, 137, },
363 { 77, 23, 60, 15, 72, 21, 56, 14, },
367 const char *sws_format_name(enum PixelFormat format)
370 case PIX_FMT_YUV420P:
372 case PIX_FMT_YUVA420P:
374 case PIX_FMT_YUYV422:
380 case PIX_FMT_YUV422P:
382 case PIX_FMT_YUV444P:
386 case PIX_FMT_YUV410P:
388 case PIX_FMT_YUV411P:
394 case PIX_FMT_GRAY16BE:
396 case PIX_FMT_GRAY16LE:
400 case PIX_FMT_MONOWHITE:
402 case PIX_FMT_MONOBLACK:
406 case PIX_FMT_YUVJ420P:
408 case PIX_FMT_YUVJ422P:
410 case PIX_FMT_YUVJ444P:
412 case PIX_FMT_XVMC_MPEG2_MC:
413 return "xvmc_mpeg2_mc";
414 case PIX_FMT_XVMC_MPEG2_IDCT:
415 return "xvmc_mpeg2_idct";
416 case PIX_FMT_UYVY422:
418 case PIX_FMT_UYYVYY411:
420 case PIX_FMT_RGB32_1:
422 case PIX_FMT_BGR32_1:
434 case PIX_FMT_BGR4_BYTE:
440 case PIX_FMT_RGB4_BYTE:
442 case PIX_FMT_RGB48BE:
444 case PIX_FMT_RGB48LE:
450 case PIX_FMT_YUV440P:
452 case PIX_FMT_VDPAU_H264:
454 case PIX_FMT_VDPAU_MPEG1:
455 return "vdpau_mpeg1";
456 case PIX_FMT_VDPAU_MPEG2:
457 return "vdpau_mpeg2";
458 case PIX_FMT_VDPAU_WMV3:
460 case PIX_FMT_VDPAU_VC1:
462 case PIX_FMT_VDPAU_MPEG4:
463 return "vdpau_mpeg4";
464 case PIX_FMT_YUV420P16LE:
465 return "yuv420p16le";
466 case PIX_FMT_YUV422P16LE:
467 return "yuv422p16le";
468 case PIX_FMT_YUV444P16LE:
469 return "yuv444p16le";
470 case PIX_FMT_YUV420P16BE:
471 return "yuv420p16be";
472 case PIX_FMT_YUV422P16BE:
473 return "yuv422p16be";
474 case PIX_FMT_YUV444P16BE:
475 return "yuv444p16be";
477 return "Unknown format";
481 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
482 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
483 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
484 int dstW, int chrDstW, int big_endian)
486 //FIXME Optimize (just quickly written not optimized..)
489 for (i = 0; i < dstW; i++) {
493 for (j = 0; j < lumFilterSize; j++)
494 val += lumSrc[j][i] * lumFilter[j];
497 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
499 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
504 for (i = 0; i < chrDstW; i++) {
509 for (j = 0; j < chrFilterSize; j++) {
510 u += chrSrc[j][i ] * chrFilter[j];
511 v += chrSrc[j][i + VOFW] * chrFilter[j];
515 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
516 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
518 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
519 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
524 if (CONFIG_SWSCALE_ALPHA && aDest) {
525 for (i = 0; i < dstW; i++) {
529 for (j = 0; j < lumFilterSize; j++)
530 val += alpSrc[j][i] * lumFilter[j];
533 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
535 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
541 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
542 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
543 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
544 enum PixelFormat dstFormat)
546 if (isBE(dstFormat)) {
547 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
548 chrFilter, chrSrc, chrFilterSize,
550 dest, uDest, vDest, aDest,
553 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
554 chrFilter, chrSrc, chrFilterSize,
556 dest, uDest, vDest, aDest,
561 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
562 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
563 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
565 //FIXME Optimize (just quickly written not optimized..)
567 for (i=0; i<dstW; i++) {
570 for (j=0; j<lumFilterSize; j++)
571 val += lumSrc[j][i] * lumFilter[j];
573 dest[i]= av_clip_uint8(val>>19);
577 for (i=0; i<chrDstW; i++) {
581 for (j=0; j<chrFilterSize; j++) {
582 u += chrSrc[j][i] * chrFilter[j];
583 v += chrSrc[j][i + VOFW] * chrFilter[j];
586 uDest[i]= av_clip_uint8(u>>19);
587 vDest[i]= av_clip_uint8(v>>19);
590 if (CONFIG_SWSCALE_ALPHA && aDest)
591 for (i=0; i<dstW; i++) {
594 for (j=0; j<lumFilterSize; j++)
595 val += alpSrc[j][i] * lumFilter[j];
597 aDest[i]= av_clip_uint8(val>>19);
602 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
603 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
604 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
606 //FIXME Optimize (just quickly written not optimized..)
608 for (i=0; i<dstW; i++) {
611 for (j=0; j<lumFilterSize; j++)
612 val += lumSrc[j][i] * lumFilter[j];
614 dest[i]= av_clip_uint8(val>>19);
620 if (dstFormat == PIX_FMT_NV12)
621 for (i=0; i<chrDstW; i++) {
625 for (j=0; j<chrFilterSize; j++) {
626 u += chrSrc[j][i] * chrFilter[j];
627 v += chrSrc[j][i + VOFW] * chrFilter[j];
630 uDest[2*i]= av_clip_uint8(u>>19);
631 uDest[2*i+1]= av_clip_uint8(v>>19);
634 for (i=0; i<chrDstW; i++) {
638 for (j=0; j<chrFilterSize; j++) {
639 u += chrSrc[j][i] * chrFilter[j];
640 v += chrSrc[j][i + VOFW] * chrFilter[j];
643 uDest[2*i]= av_clip_uint8(v>>19);
644 uDest[2*i+1]= av_clip_uint8(u>>19);
648 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
649 for (i=0; i<(dstW>>1); i++) {\
655 int av_unused A1, A2;\
656 type av_unused *r, *b, *g;\
659 for (j=0; j<lumFilterSize; j++) {\
660 Y1 += lumSrc[j][i2] * lumFilter[j];\
661 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
663 for (j=0; j<chrFilterSize; j++) {\
664 U += chrSrc[j][i] * chrFilter[j];\
665 V += chrSrc[j][i+VOFW] * chrFilter[j];\
674 for (j=0; j<lumFilterSize; j++) {\
675 A1 += alpSrc[j][i2 ] * lumFilter[j];\
676 A2 += alpSrc[j][i2+1] * lumFilter[j];\
682 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
683 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
684 if ((Y1|Y2|U|V)&256) {\
685 if (Y1>255) Y1=255; \
686 else if (Y1<0)Y1=0; \
687 if (Y2>255) Y2=255; \
688 else if (Y2<0)Y2=0; \
694 if (alpha && ((A1|A2)&256)) {\
695 A1=av_clip_uint8(A1);\
696 A2=av_clip_uint8(A2);\
699 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
700 for (i=0; i<dstW; i++) {\
708 for (j=0; j<lumFilterSize; j++) {\
709 Y += lumSrc[j][i ] * lumFilter[j];\
711 for (j=0; j<chrFilterSize; j++) {\
712 U += chrSrc[j][i ] * chrFilter[j];\
713 V += chrSrc[j][i+VOFW] * chrFilter[j];\
720 for (j=0; j<lumFilterSize; j++)\
721 A += alpSrc[j][i ] * lumFilter[j];\
724 A = av_clip_uint8(A);\
727 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
728 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
729 Y-= c->yuv2rgb_y_offset;\
730 Y*= c->yuv2rgb_y_coeff;\
732 R= Y + V*c->yuv2rgb_v2r_coeff;\
733 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
734 B= Y + U*c->yuv2rgb_u2b_coeff;\
735 if ((R|G|B)&(0xC0000000)) {\
736 if (R>=(256<<22)) R=(256<<22)-1; \
738 if (G>=(256<<22)) G=(256<<22)-1; \
740 if (B>=(256<<22)) B=(256<<22)-1; \
745 #define YSCALE_YUV_2_GRAY16_C \
746 for (i=0; i<(dstW>>1); i++) {\
755 for (j=0; j<lumFilterSize; j++) {\
756 Y1 += lumSrc[j][i2] * lumFilter[j];\
757 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
761 if ((Y1|Y2|U|V)&65536) {\
762 if (Y1>65535) Y1=65535; \
763 else if (Y1<0)Y1=0; \
764 if (Y2>65535) Y2=65535; \
765 else if (Y2<0)Y2=0; \
768 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
769 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
770 r = (type *)c->table_rV[V]; \
771 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
772 b = (type *)c->table_bU[U]; \
774 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
775 for (i=0; i<(dstW>>1); i++) { \
777 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
778 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
779 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
780 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
781 type av_unused *r, *b, *g; \
782 int av_unused A1, A2; \
784 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
785 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
788 #define YSCALE_YUV_2_GRAY16_2_C \
789 for (i=0; i<(dstW>>1); i++) { \
791 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
792 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
794 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
795 YSCALE_YUV_2_PACKED2_C(type,alpha)\
796 r = (type *)c->table_rV[V];\
797 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
798 b = (type *)c->table_bU[U];\
800 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
801 for (i=0; i<(dstW>>1); i++) {\
803 int Y1= buf0[i2 ]>>7;\
804 int Y2= buf0[i2+1]>>7;\
805 int U= (uvbuf1[i ])>>7;\
806 int V= (uvbuf1[i+VOFW])>>7;\
807 type av_unused *r, *b, *g;\
808 int av_unused A1, A2;\
814 #define YSCALE_YUV_2_GRAY16_1_C \
815 for (i=0; i<(dstW>>1); i++) {\
817 int Y1= buf0[i2 ]<<1;\
818 int Y2= buf0[i2+1]<<1;\
820 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
821 YSCALE_YUV_2_PACKED1_C(type,alpha)\
822 r = (type *)c->table_rV[V];\
823 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
824 b = (type *)c->table_bU[U];\
826 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
827 for (i=0; i<(dstW>>1); i++) {\
829 int Y1= buf0[i2 ]>>7;\
830 int Y2= buf0[i2+1]>>7;\
831 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
832 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
833 type av_unused *r, *b, *g;\
834 int av_unused A1, A2;\
840 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
841 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
842 r = (type *)c->table_rV[V];\
843 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
844 b = (type *)c->table_bU[U];\
846 #define YSCALE_YUV_2_MONO2_C \
847 const uint8_t * const d128=dither_8x8_220[y&7];\
848 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
849 for (i=0; i<dstW-7; i+=8) {\
851 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
852 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
853 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
854 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
855 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
856 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
857 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
858 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
859 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
864 #define YSCALE_YUV_2_MONOX_C \
865 const uint8_t * const d128=dither_8x8_220[y&7];\
866 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
868 for (i=0; i<dstW-1; i+=2) {\
873 for (j=0; j<lumFilterSize; j++) {\
874 Y1 += lumSrc[j][i] * lumFilter[j];\
875 Y2 += lumSrc[j][i+1] * lumFilter[j];\
885 acc+= acc + g[Y1+d128[(i+0)&7]];\
886 acc+= acc + g[Y2+d128[(i+1)&7]];\
888 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
894 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
895 switch(c->dstFormat) {\
896 case PIX_FMT_RGB48BE:\
897 case PIX_FMT_RGB48LE:\
899 ((uint8_t*)dest)[ 0]= r[Y1];\
900 ((uint8_t*)dest)[ 1]= r[Y1];\
901 ((uint8_t*)dest)[ 2]= g[Y1];\
902 ((uint8_t*)dest)[ 3]= g[Y1];\
903 ((uint8_t*)dest)[ 4]= b[Y1];\
904 ((uint8_t*)dest)[ 5]= b[Y1];\
905 ((uint8_t*)dest)[ 6]= r[Y2];\
906 ((uint8_t*)dest)[ 7]= r[Y2];\
907 ((uint8_t*)dest)[ 8]= g[Y2];\
908 ((uint8_t*)dest)[ 9]= g[Y2];\
909 ((uint8_t*)dest)[10]= b[Y2];\
910 ((uint8_t*)dest)[11]= b[Y2];\
917 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
918 func(uint32_t,needAlpha)\
919 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
920 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
923 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
925 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
926 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
930 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
931 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
939 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
940 func(uint32_t,needAlpha)\
941 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
942 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
945 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
947 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
948 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
952 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
953 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
960 ((uint8_t*)dest)[0]= r[Y1];\
961 ((uint8_t*)dest)[1]= g[Y1];\
962 ((uint8_t*)dest)[2]= b[Y1];\
963 ((uint8_t*)dest)[3]= r[Y2];\
964 ((uint8_t*)dest)[4]= g[Y2];\
965 ((uint8_t*)dest)[5]= b[Y2];\
971 ((uint8_t*)dest)[0]= b[Y1];\
972 ((uint8_t*)dest)[1]= g[Y1];\
973 ((uint8_t*)dest)[2]= r[Y1];\
974 ((uint8_t*)dest)[3]= b[Y2];\
975 ((uint8_t*)dest)[4]= g[Y2];\
976 ((uint8_t*)dest)[5]= r[Y2];\
980 case PIX_FMT_RGB565:\
981 case PIX_FMT_BGR565:\
983 const int dr1= dither_2x2_8[y&1 ][0];\
984 const int dg1= dither_2x2_4[y&1 ][0];\
985 const int db1= dither_2x2_8[(y&1)^1][0];\
986 const int dr2= dither_2x2_8[y&1 ][1];\
987 const int dg2= dither_2x2_4[y&1 ][1];\
988 const int db2= dither_2x2_8[(y&1)^1][1];\
990 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
991 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
995 case PIX_FMT_RGB555:\
996 case PIX_FMT_BGR555:\
998 const int dr1= dither_2x2_8[y&1 ][0];\
999 const int dg1= dither_2x2_8[y&1 ][1];\
1000 const int db1= dither_2x2_8[(y&1)^1][0];\
1001 const int dr2= dither_2x2_8[y&1 ][1];\
1002 const int dg2= dither_2x2_8[y&1 ][0];\
1003 const int db2= dither_2x2_8[(y&1)^1][1];\
1005 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
1006 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
1013 const uint8_t * const d64= dither_8x8_73[y&7];\
1014 const uint8_t * const d32= dither_8x8_32[y&7];\
1016 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
1017 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
1024 const uint8_t * const d64= dither_8x8_73 [y&7];\
1025 const uint8_t * const d128=dither_8x8_220[y&7];\
1027 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
1028 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
1032 case PIX_FMT_RGB4_BYTE:\
1033 case PIX_FMT_BGR4_BYTE:\
1035 const uint8_t * const d64= dither_8x8_73 [y&7];\
1036 const uint8_t * const d128=dither_8x8_220[y&7];\
1038 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
1039 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
1043 case PIX_FMT_MONOBLACK:\
1044 case PIX_FMT_MONOWHITE:\
1049 case PIX_FMT_YUYV422:\
1051 ((uint8_t*)dest)[2*i2+0]= Y1;\
1052 ((uint8_t*)dest)[2*i2+1]= U;\
1053 ((uint8_t*)dest)[2*i2+2]= Y2;\
1054 ((uint8_t*)dest)[2*i2+3]= V;\
1057 case PIX_FMT_UYVY422:\
1059 ((uint8_t*)dest)[2*i2+0]= U;\
1060 ((uint8_t*)dest)[2*i2+1]= Y1;\
1061 ((uint8_t*)dest)[2*i2+2]= V;\
1062 ((uint8_t*)dest)[2*i2+3]= Y2;\
1065 case PIX_FMT_GRAY16BE:\
1067 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1068 ((uint8_t*)dest)[2*i2+1]= Y1;\
1069 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1070 ((uint8_t*)dest)[2*i2+3]= Y2;\
1073 case PIX_FMT_GRAY16LE:\
1075 ((uint8_t*)dest)[2*i2+0]= Y1;\
1076 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1077 ((uint8_t*)dest)[2*i2+2]= Y2;\
1078 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1084 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1085 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1086 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1089 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)
1092 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1093 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1094 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1097 int step= fmt_depth(c->dstFormat)/8;
1100 switch(c->dstFormat) {
1108 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1109 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1110 dest[aidx]= needAlpha ? A : 255;
1117 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1118 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1126 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1143 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1144 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1145 dest[aidx]= needAlpha ? A : 255;
1152 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1153 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1161 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1176 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1179 uint8_t *ptr = plane + stride*y;
1180 for (i=0; i<height; i++) {
1181 memset(ptr, val, width);
1186 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width)
1189 for (i = 0; i < width; i++) {
1194 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1198 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1199 uint8_t *src1, uint8_t *src2, int width)
1203 for (i = 0; i < width; i++) {
1204 int r = src1[6*i + 0];
1205 int g = src1[6*i + 2];
1206 int b = src1[6*i + 4];
1208 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1209 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1213 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1214 uint8_t *src1, uint8_t *src2, int width)
1218 for (i = 0; i < width; i++) {
1219 int r= src1[12*i + 0] + src1[12*i + 6];
1220 int g= src1[12*i + 2] + src1[12*i + 8];
1221 int b= src1[12*i + 4] + src1[12*i + 10];
1223 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1224 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1228 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1229 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1232 for (i=0; i<width; i++) {\
1233 int b= (((const type*)src)[i]>>shb)&maskb;\
1234 int g= (((const type*)src)[i]>>shg)&maskg;\
1235 int r= (((const type*)src)[i]>>shr)&maskr;\
1237 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1241 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1242 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1243 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1244 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1245 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1246 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1248 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1251 for (i=0; i<width; i++) {
1256 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1257 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1260 for (i=0; i<width; i++) {\
1261 int b= (((const type*)src)[i]&maskb)>>shb;\
1262 int g= (((const type*)src)[i]&maskg)>>shg;\
1263 int r= (((const type*)src)[i]&maskr)>>shr;\
1265 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1266 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1269 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1272 for (i=0; i<width; i++) {\
1273 int pix0= ((const type*)src)[2*i+0];\
1274 int pix1= ((const type*)src)[2*i+1];\
1275 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1276 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1277 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1278 g&= maskg|(2*maskg);\
1282 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1283 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1287 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1288 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1289 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1290 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1291 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1292 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1294 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1297 for (i=0; i<width; i++) {
1300 dst[i]= pal[d] & 0xFF;
1304 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1305 const uint8_t *src1, const uint8_t *src2,
1306 long width, uint32_t *pal)
1309 assert(src1 == src2);
1310 for (i=0; i<width; i++) {
1311 int p= pal[src1[i]];
1318 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1321 for (i=0; i<width/8; i++) {
1324 dst[8*i+j]= ((d>>(7-j))&1)*255;
1328 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1331 for (i=0; i<width/8; i++) {
1334 dst[8*i+j]= ((d>>(7-j))&1)*255;
1339 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1341 #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1346 #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
1347 #define COMPILE_ALTIVEC
1353 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1357 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1358 #define COMPILE_MMX2
1361 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1362 #define COMPILE_3DNOW
1366 #define COMPILE_TEMPLATE_MMX 0
1367 #define COMPILE_TEMPLATE_MMX2 0
1368 #define COMPILE_TEMPLATE_AMD3DNOW 0
1369 #define COMPILE_TEMPLATE_ALTIVEC 0
1372 #define RENAME(a) a ## _C
1373 #include "swscale_template.c"
1376 #ifdef COMPILE_ALTIVEC
1378 #undef COMPILE_TEMPLATE_ALTIVEC
1379 #define COMPILE_TEMPLATE_ALTIVEC 1
1380 #define RENAME(a) a ## _altivec
1381 #include "swscale_template.c"
1389 #undef COMPILE_TEMPLATE_MMX
1390 #undef COMPILE_TEMPLATE_MMX2
1391 #undef COMPILE_TEMPLATE_AMD3DNOW
1392 #define COMPILE_TEMPLATE_MMX 1
1393 #define COMPILE_TEMPLATE_MMX2 0
1394 #define COMPILE_TEMPLATE_AMD3DNOW 0
1395 #define RENAME(a) a ## _MMX
1396 #include "swscale_template.c"
1402 #undef COMPILE_TEMPLATE_MMX
1403 #undef COMPILE_TEMPLATE_MMX2
1404 #undef COMPILE_TEMPLATE_AMD3DNOW
1405 #define COMPILE_TEMPLATE_MMX 1
1406 #define COMPILE_TEMPLATE_MMX2 1
1407 #define COMPILE_TEMPLATE_AMD3DNOW 0
1408 #define RENAME(a) a ## _MMX2
1409 #include "swscale_template.c"
1413 #ifdef COMPILE_3DNOW
1415 #undef COMPILE_TEMPLATE_MMX
1416 #undef COMPILE_TEMPLATE_MMX2
1417 #undef COMPILE_TEMPLATE_AMD3DNOW
1418 #define COMPILE_TEMPLATE_MMX 1
1419 #define COMPILE_TEMPLATE_MMX2 0
1420 #define COMPILE_TEMPLATE_AMD3DNOW 1
1421 #define RENAME(a) a ## _3DNow
1422 #include "swscale_template.c"
1427 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1429 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1430 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1431 else return getSplineCoeff( 0.0,
1438 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1439 int srcW, int dstW, int filterAlign, int one, int flags,
1440 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1446 int64_t *filter=NULL;
1447 int64_t *filter2=NULL;
1448 const int64_t fone= 1LL<<54;
1451 if (flags & SWS_CPU_CAPS_MMX)
1452 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1455 // NOTE: the +1 is for the MMX scaler which reads over the end
1456 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
1458 if (FFABS(xInc - 0x10000) <10) { // unscaled
1461 FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1463 for (i=0; i<dstW; i++) {
1464 filter[i*filterSize]= fone;
1468 } else if (flags&SWS_POINT) { // lame looking point sampling mode
1472 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1474 xDstInSrc= xInc/2 - 0x8000;
1475 for (i=0; i<dstW; i++) {
1476 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1478 (*filterPos)[i]= xx;
1482 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
1486 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1488 xDstInSrc= xInc/2 - 0x8000;
1489 for (i=0; i<dstW; i++) {
1490 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1493 (*filterPos)[i]= xx;
1494 //bilinear upscale / linear interpolate / area averaging
1495 for (j=0; j<filterSize; j++) {
1496 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1497 if (coeff<0) coeff=0;
1498 filter[i*filterSize + j]= coeff;
1507 if (flags&SWS_BICUBIC) sizeFactor= 4;
1508 else if (flags&SWS_X) sizeFactor= 8;
1509 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1510 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1511 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1512 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1513 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1514 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1516 sizeFactor= 0; //GCC warning killer
1520 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1521 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1523 if (filterSize > srcW-2) filterSize=srcW-2;
1525 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
1527 xDstInSrc= xInc - 0x10000;
1528 for (i=0; i<dstW; i++) {
1529 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1531 (*filterPos)[i]= xx;
1532 for (j=0; j<filterSize; j++) {
1533 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1539 floatd= d * (1.0/(1<<30));
1541 if (flags & SWS_BICUBIC) {
1542 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1543 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1544 int64_t dd = ( d*d)>>30;
1545 int64_t ddd= (dd*d)>>30;
1548 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1549 else if (d < 1LL<<31)
1550 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1553 coeff *= fone>>(30+24);
1555 /* else if (flags & SWS_X) {
1556 double p= param ? param*0.01 : 0.3;
1557 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1558 coeff*= pow(2.0, - p*d*d);
1560 else if (flags & SWS_X) {
1561 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1568 if (c<0.0) c= -pow(-c, A);
1570 coeff= (c*0.5 + 0.5)*fone;
1571 } else if (flags & SWS_AREA) {
1572 int64_t d2= d - (1<<29);
1573 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1574 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1576 coeff *= fone>>(30+16);
1577 } else if (flags & SWS_GAUSS) {
1578 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1579 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1580 } else if (flags & SWS_SINC) {
1581 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1582 } else if (flags & SWS_LANCZOS) {
1583 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1584 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1585 if (floatd>p) coeff=0;
1586 } else if (flags & SWS_BILINEAR) {
1588 if (coeff<0) coeff=0;
1589 coeff *= fone >> 30;
1590 } else if (flags & SWS_SPLINE) {
1591 double p=-2.196152422706632;
1592 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1594 coeff= 0.0; //GCC warning killer
1598 filter[i*filterSize + j]= coeff;
1605 /* apply src & dst Filter to filter -> filter2
1608 assert(filterSize>0);
1609 filter2Size= filterSize;
1610 if (srcFilter) filter2Size+= srcFilter->length - 1;
1611 if (dstFilter) filter2Size+= dstFilter->length - 1;
1612 assert(filter2Size>0);
1613 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
1615 for (i=0; i<dstW; i++) {
1619 for (k=0; k<srcFilter->length; k++) {
1620 for (j=0; j<filterSize; j++)
1621 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1624 for (j=0; j<filterSize; j++)
1625 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1629 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1633 /* try to reduce the filter-size (step1 find size and shift left) */
1634 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1636 for (i=dstW-1; i>=0; i--) {
1637 int min= filter2Size;
1641 /* get rid off near zero elements on the left by shifting left */
1642 for (j=0; j<filter2Size; j++) {
1644 cutOff += FFABS(filter2[i*filter2Size]);
1646 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1648 /* preserve monotonicity because the core can't handle the filter otherwise */
1649 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1651 // move filter coefficients left
1652 for (k=1; k<filter2Size; k++)
1653 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1654 filter2[i*filter2Size + k - 1]= 0;
1659 /* count near zeros on the right */
1660 for (j=filter2Size-1; j>0; j--) {
1661 cutOff += FFABS(filter2[i*filter2Size + j]);
1663 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1667 if (min>minFilterSize) minFilterSize= min;
1670 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1671 // we can handle the special case 4,
1672 // so we don't want to go to the full 8
1673 if (minFilterSize < 5)
1676 // We really don't want to waste our time
1677 // doing useless computation, so fall back on
1678 // the scalar C code for very small filters.
1679 // Vectorizing is worth it only if you have a
1680 // decent-sized vector.
1681 if (minFilterSize < 3)
1685 if (flags & SWS_CPU_CAPS_MMX) {
1686 // special case for unscaled vertical filtering
1687 if (minFilterSize == 1 && filterAlign == 2)
1691 assert(minFilterSize > 0);
1692 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1693 assert(filterSize > 0);
1694 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1695 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1697 *outFilterSize= filterSize;
1699 if (flags&SWS_PRINT_INFO)
1700 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1701 /* try to reduce the filter-size (step2 reduce it) */
1702 for (i=0; i<dstW; i++) {
1705 for (j=0; j<filterSize; j++) {
1706 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1707 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1708 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1709 filter[i*filterSize + j]= 0;
1714 //FIXME try to align filterPos if possible
1717 for (i=0; i<dstW; i++) {
1719 if ((*filterPos)[i] < 0) {
1720 // move filter coefficients left to compensate for filterPos
1721 for (j=1; j<filterSize; j++) {
1722 int left= FFMAX(j + (*filterPos)[i], 0);
1723 filter[i*filterSize + left] += filter[i*filterSize + j];
1724 filter[i*filterSize + j]=0;
1729 if ((*filterPos)[i] + filterSize > srcW) {
1730 int shift= (*filterPos)[i] + filterSize - srcW;
1731 // move filter coefficients right to compensate for filterPos
1732 for (j=filterSize-2; j>=0; j--) {
1733 int right= FFMIN(j + shift, filterSize-1);
1734 filter[i*filterSize +right] += filter[i*filterSize +j];
1735 filter[i*filterSize +j]=0;
1737 (*filterPos)[i]= srcW - filterSize;
1741 // Note the +1 is for the MMX scaler which reads over the end
1742 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1743 FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
1745 /* normalize & store in outFilter */
1746 for (i=0; i<dstW; i++) {
1751 for (j=0; j<filterSize; j++) {
1752 sum+= filter[i*filterSize + j];
1754 sum= (sum + one/2)/ one;
1755 for (j=0; j<*outFilterSize; j++) {
1756 int64_t v= filter[i*filterSize + j] + error;
1757 int intV= ROUNDED_DIV(v, sum);
1758 (*outFilter)[i*(*outFilterSize) + j]= intV;
1759 error= v - intV*sum;
1763 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1764 for (i=0; i<*outFilterSize; i++) {
1765 int j= dstW*(*outFilterSize);
1766 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1777 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
1780 x86_reg imm8OfPShufW1A;
1781 x86_reg imm8OfPShufW2A;
1782 x86_reg fragmentLengthA;
1784 x86_reg imm8OfPShufW1B;
1785 x86_reg imm8OfPShufW2B;
1786 x86_reg fragmentLengthB;
1791 // create an optimized horizontal scaling routine
1799 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1800 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1801 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1802 "punpcklbw %%mm7, %%mm1 \n\t"
1803 "punpcklbw %%mm7, %%mm0 \n\t"
1804 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1806 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1808 "psubw %%mm1, %%mm0 \n\t"
1809 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1810 "pmullw %%mm3, %%mm0 \n\t"
1811 "psllw $7, %%mm1 \n\t"
1812 "paddw %%mm1, %%mm0 \n\t"
1814 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1816 "add $8, %%"REG_a" \n\t"
1820 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1821 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1822 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1827 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1831 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1832 "=r" (fragmentLengthA)
1839 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1840 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1841 "punpcklbw %%mm7, %%mm0 \n\t"
1842 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1844 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1846 "psubw %%mm1, %%mm0 \n\t"
1847 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1848 "pmullw %%mm3, %%mm0 \n\t"
1849 "psllw $7, %%mm1 \n\t"
1850 "paddw %%mm1, %%mm0 \n\t"
1852 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1854 "add $8, %%"REG_a" \n\t"
1858 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1859 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1860 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1865 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1869 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1870 "=r" (fragmentLengthB)
1873 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1876 for (i=0; i<dstW/numSplits; i++) {
1881 int b=((xpos+xInc)>>16) - xx;
1882 int c=((xpos+xInc*2)>>16) - xx;
1883 int d=((xpos+xInc*3)>>16) - xx;
1885 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
1886 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
1887 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
1888 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
1889 int maxShift= 3-(d+inc);
1893 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1894 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1895 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1896 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1899 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
1901 filterCode[fragmentPos + imm8OfPShufW1]=
1902 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
1903 filterCode[fragmentPos + imm8OfPShufW2]=
1904 a | (b<<2) | (c<<4) | (d<<6);
1906 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
1907 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1909 if (shift && i>=shift) {
1910 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
1911 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
1912 filterPos[i/2]-=shift;
1916 fragmentPos+= fragmentLength;
1919 filterCode[fragmentPos]= RET;
1924 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1926 return fragmentPos + 1;
1928 #endif /* COMPILE_MMX2 */
1930 static void globalInit(void)
1932 // generating tables:
1934 for (i=0; i<768; i++) {
1935 int c= av_clip_uint8(i-256);
1940 static SwsFunc getSwsFunc(SwsContext *c)
1942 #if CONFIG_RUNTIME_CPUDETECT
1943 int flags = c->flags;
1945 #if ARCH_X86 && CONFIG_GPL
1946 // ordered per speed fastest first
1947 if (flags & SWS_CPU_CAPS_MMX2) {
1948 sws_init_swScale_MMX2(c);
1949 return swScale_MMX2;
1950 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1951 sws_init_swScale_3DNow(c);
1952 return swScale_3DNow;
1953 } else if (flags & SWS_CPU_CAPS_MMX) {
1954 sws_init_swScale_MMX(c);
1957 sws_init_swScale_C(c);
1963 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1964 sws_init_swScale_altivec(c);
1965 return swScale_altivec;
1967 sws_init_swScale_C(c);
1971 sws_init_swScale_C(c);
1973 #endif /* ARCH_X86 && CONFIG_GPL */
1974 #else //CONFIG_RUNTIME_CPUDETECT
1975 #if COMPILE_TEMPLATE_MMX2
1976 sws_init_swScale_MMX2(c);
1977 return swScale_MMX2;
1978 #elif COMPILE_TEMPLATE_AMD3DNOW
1979 sws_init_swScale_3DNow(c);
1980 return swScale_3DNow;
1981 #elif COMPILE_TEMPLATE_MMX
1982 sws_init_swScale_MMX(c);
1984 #elif COMPILE_TEMPLATE_ALTIVEC
1985 sws_init_swScale_altivec(c);
1986 return swScale_altivec;
1988 sws_init_swScale_C(c);
1991 #endif //!CONFIG_RUNTIME_CPUDETECT
1994 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1995 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1997 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1999 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2000 memcpy(dst, src[0], srcSliceH*dstStride[0]);
2003 const uint8_t *srcPtr= src[0];
2004 uint8_t *dstPtr= dst;
2005 for (i=0; i<srcSliceH; i++) {
2006 memcpy(dstPtr, srcPtr, c->srcW);
2007 srcPtr+= srcStride[0];
2008 dstPtr+= dstStride[0];
2011 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
2012 if (c->dstFormat == PIX_FMT_NV12)
2013 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
2015 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
2020 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2021 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2023 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2025 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2030 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2031 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2033 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2035 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
2040 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2041 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2043 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2045 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2050 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2051 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2053 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
2055 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
2060 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2061 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2063 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2064 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2065 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2067 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2070 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2075 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2076 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2078 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2079 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2080 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2082 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2087 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2088 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2090 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2091 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
2092 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
2094 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2097 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2102 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2103 int srcSliceH, uint8_t* dstParam[], int dstStride[])
2105 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
2106 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
2107 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
2109 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
2114 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2115 int srcSliceH, uint8_t* dst[], int dstStride[])
2117 const enum PixelFormat srcFormat= c->srcFormat;
2118 const enum PixelFormat dstFormat= c->dstFormat;
2119 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
2120 const uint8_t *palette)=NULL;
2122 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2123 uint8_t *srcPtr= src[0];
2125 if (!usePal(srcFormat))
2126 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2127 sws_format_name(srcFormat), sws_format_name(dstFormat));
2130 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
2131 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
2132 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
2133 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
2134 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
2135 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
2136 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2137 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2141 for (i=0; i<srcSliceH; i++) {
2142 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
2143 srcPtr+= srcStride[0];
2144 dstPtr+= dstStride[0];
2150 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
2151 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2152 int srcSliceH, uint8_t* dst[], int dstStride[])
2154 const enum PixelFormat srcFormat= c->srcFormat;
2155 const enum PixelFormat dstFormat= c->dstFormat;
2156 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2157 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2158 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2159 const int dstId= fmt_depth(dstFormat) >> 2;
2160 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2163 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2164 || (isRGB(srcFormat) && isRGB(dstFormat))) {
2165 switch(srcId | (dstId<<4)) {
2166 case 0x34: conv= rgb16to15; break;
2167 case 0x36: conv= rgb24to15; break;
2168 case 0x38: conv= rgb32to15; break;
2169 case 0x43: conv= rgb15to16; break;
2170 case 0x46: conv= rgb24to16; break;
2171 case 0x48: conv= rgb32to16; break;
2172 case 0x63: conv= rgb15to24; break;
2173 case 0x64: conv= rgb16to24; break;
2174 case 0x68: conv= rgb32to24; break;
2175 case 0x83: conv= rgb15to32; break;
2176 case 0x84: conv= rgb16to32; break;
2177 case 0x86: conv= rgb24to32; break;
2178 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2179 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2181 } else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2182 || (isRGB(srcFormat) && isBGR(dstFormat))) {
2183 switch(srcId | (dstId<<4)) {
2184 case 0x33: conv= rgb15tobgr15; break;
2185 case 0x34: conv= rgb16tobgr15; break;
2186 case 0x36: conv= rgb24tobgr15; break;
2187 case 0x38: conv= rgb32tobgr15; break;
2188 case 0x43: conv= rgb15tobgr16; break;
2189 case 0x44: conv= rgb16tobgr16; break;
2190 case 0x46: conv= rgb24tobgr16; break;
2191 case 0x48: conv= rgb32tobgr16; break;
2192 case 0x63: conv= rgb15tobgr24; break;
2193 case 0x64: conv= rgb16tobgr24; break;
2194 case 0x66: conv= rgb24tobgr24; break;
2195 case 0x68: conv= rgb32tobgr24; break;
2196 case 0x83: conv= rgb15tobgr32; break;
2197 case 0x84: conv= rgb16tobgr32; break;
2198 case 0x86: conv= rgb24tobgr32; break;
2199 case 0x88: conv= rgb32tobgr32; break;
2200 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2201 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2204 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2205 sws_format_name(srcFormat), sws_format_name(dstFormat));
2209 uint8_t *srcPtr= src[0];
2210 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2211 srcPtr += ALT32_CORR;
2213 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2214 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2217 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2219 for (i=0; i<srcSliceH; i++) {
2220 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2221 srcPtr+= srcStride[0];
2222 dstPtr+= dstStride[0];
2229 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2230 int srcSliceH, uint8_t* dst[], int dstStride[])
2235 dst[0]+ srcSliceY *dstStride[0],
2236 dst[1]+(srcSliceY>>1)*dstStride[1],
2237 dst[2]+(srcSliceY>>1)*dstStride[2],
2239 dstStride[0], dstStride[1], srcStride[0]);
2241 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2245 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2246 int srcSliceH, uint8_t* dst[], int dstStride[])
2251 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2252 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2254 uint8_t *srcPtr= src[0];
2255 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2257 for (i=0; i<srcSliceH; i++) {
2258 memcpy(dstPtr, srcPtr, c->srcW);
2259 srcPtr+= srcStride[0];
2260 dstPtr+= dstStride[0];
2264 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
2265 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2266 srcSliceH >> 2, srcStride[1], dstStride[1]);
2267 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2268 srcSliceH >> 2, srcStride[2], dstStride[2]);
2270 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
2271 srcSliceH >> 2, srcStride[1], dstStride[2]);
2272 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
2273 srcSliceH >> 2, srcStride[2], dstStride[1]);
2276 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2280 /* unscaled copy like stuff (assumes nearly identical formats) */
2281 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2282 int srcSliceH, uint8_t* dst[], int dstStride[])
2284 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2285 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2288 uint8_t *srcPtr= src[0];
2289 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2292 /* universal length finder */
2293 while(length+c->srcW <= FFABS(dstStride[0])
2294 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2297 for (i=0; i<srcSliceH; i++) {
2298 memcpy(dstPtr, srcPtr, length);
2299 srcPtr+= srcStride[0];
2300 dstPtr+= dstStride[0];
2306 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2307 int srcSliceH, uint8_t* dst[], int dstStride[])
2310 for (plane=0; plane<4; plane++) {
2311 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2312 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2313 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2314 uint8_t *srcPtr= src[plane];
2315 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2317 if (!dst[plane]) continue;
2318 // ignore palette for GRAY8
2319 if (plane == 1 && !dst[2]) continue;
2320 if (!src[plane] || (plane == 1 && !src[2])) {
2321 if(is16BPS(c->dstFormat))
2323 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2325 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
2326 if (!isBE(c->srcFormat)) srcPtr++;
2327 for (i=0; i<height; i++) {
2328 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2329 srcPtr+= srcStride[plane];
2330 dstPtr+= dstStride[plane];
2332 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
2333 for (i=0; i<height; i++) {
2334 for (j=0; j<length; j++) {
2335 dstPtr[ j<<1 ] = srcPtr[j];
2336 dstPtr[(j<<1)+1] = srcPtr[j];
2338 srcPtr+= srcStride[plane];
2339 dstPtr+= dstStride[plane];
2341 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2342 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
2344 for (i=0; i<height; i++) {
2345 for (j=0; j<length; j++)
2346 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2347 srcPtr+= srcStride[plane];
2348 dstPtr+= dstStride[plane];
2350 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2351 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2353 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2355 for (i=0; i<height; i++) {
2356 memcpy(dstPtr, srcPtr, length);
2357 srcPtr+= srcStride[plane];
2358 dstPtr+= dstStride[plane];
2367 static void getSubSampleFactors(int *h, int *v, int format)
2370 case PIX_FMT_UYVY422:
2371 case PIX_FMT_YUYV422:
2375 case PIX_FMT_YUV420P:
2376 case PIX_FMT_YUV420P16LE:
2377 case PIX_FMT_YUV420P16BE:
2378 case PIX_FMT_YUVA420P:
2379 case PIX_FMT_GRAY16BE:
2380 case PIX_FMT_GRAY16LE:
2381 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2387 case PIX_FMT_YUV440P:
2391 case PIX_FMT_YUV410P:
2395 case PIX_FMT_YUV444P:
2396 case PIX_FMT_YUV444P16LE:
2397 case PIX_FMT_YUV444P16BE:
2401 case PIX_FMT_YUV422P:
2402 case PIX_FMT_YUV422P16LE:
2403 case PIX_FMT_YUV422P16BE:
2407 case PIX_FMT_YUV411P:
2418 static uint16_t roundToInt16(int64_t f)
2420 int r= (f + (1<<15))>>16;
2421 if (r<-0x7FFF) return 0x8000;
2422 else if (r> 0x7FFF) return 0x7FFF;
2426 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
2428 int64_t crv = inv_table[0];
2429 int64_t cbu = inv_table[1];
2430 int64_t cgu = -inv_table[2];
2431 int64_t cgv = -inv_table[3];
2435 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2436 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2438 c->brightness= brightness;
2439 c->contrast = contrast;
2440 c->saturation= saturation;
2441 c->srcRange = srcRange;
2442 c->dstRange = dstRange;
2443 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2445 c->uOffset= 0x0400040004000400LL;
2446 c->vOffset= 0x0400040004000400LL;
2452 crv= (crv*224) / 255;
2453 cbu= (cbu*224) / 255;
2454 cgu= (cgu*224) / 255;
2455 cgv= (cgv*224) / 255;
2458 cy = (cy *contrast )>>16;
2459 crv= (crv*contrast * saturation)>>32;
2460 cbu= (cbu*contrast * saturation)>>32;
2461 cgu= (cgu*contrast * saturation)>>32;
2462 cgv= (cgv*contrast * saturation)>>32;
2464 oy -= 256*brightness;
2466 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2467 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2468 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2469 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2470 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2471 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2473 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2474 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2475 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2476 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2477 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2478 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2480 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2483 #ifdef COMPILE_ALTIVEC
2484 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2485 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2490 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
2492 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2494 *inv_table = c->srcColorspaceTable;
2495 *table = c->dstColorspaceTable;
2496 *srcRange = c->srcRange;
2497 *dstRange = c->dstRange;
2498 *brightness= c->brightness;
2499 *contrast = c->contrast;
2500 *saturation= c->saturation;
2505 static int handle_jpeg(enum PixelFormat *format)
2508 case PIX_FMT_YUVJ420P:
2509 *format = PIX_FMT_YUV420P;
2511 case PIX_FMT_YUVJ422P:
2512 *format = PIX_FMT_YUV422P;
2514 case PIX_FMT_YUVJ444P:
2515 *format = PIX_FMT_YUV444P;
2517 case PIX_FMT_YUVJ440P:
2518 *format = PIX_FMT_YUV440P;
2525 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2526 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
2531 int usesVFilter, usesHFilter;
2532 int unscaled, needsDither;
2533 int srcRange, dstRange;
2534 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2536 if (flags & SWS_CPU_CAPS_MMX)
2537 __asm__ volatile("emms\n\t"::: "memory");
2540 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
2541 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2542 #if COMPILE_TEMPLATE_MMX2
2543 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2544 #elif COMPILE_TEMPLATE_AMD3DNOW
2545 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2546 #elif COMPILE_TEMPLATE_MMX
2547 flags |= SWS_CPU_CAPS_MMX;
2548 #elif COMPILE_TEMPLATE_ALTIVEC
2549 flags |= SWS_CPU_CAPS_ALTIVEC;
2551 flags |= SWS_CPU_CAPS_BFIN;
2553 #endif /* CONFIG_RUNTIME_CPUDETECT */
2554 if (clip_table[512] != 255) globalInit();
2555 if (!rgb15to16) sws_rgb2rgb_init(flags);
2557 unscaled = (srcW == dstW && srcH == dstH);
2558 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2559 && (fmt_depth(dstFormat))<24
2560 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2562 srcRange = handle_jpeg(&srcFormat);
2563 dstRange = handle_jpeg(&dstFormat);
2565 if (!isSupportedIn(srcFormat)) {
2566 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2569 if (!isSupportedOut(dstFormat)) {
2570 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2574 i= flags & ( SWS_POINT
2585 if(!i || (i & (i-1))) {
2586 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2591 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
2592 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2593 srcW, srcH, dstW, dstH);
2596 if(srcW > VOFW || dstW > VOFW) {
2597 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2601 if (!dstFilter) dstFilter= &dummyFilter;
2602 if (!srcFilter) srcFilter= &dummyFilter;
2604 FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
2606 c->av_class = &sws_context_class;
2611 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2612 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2614 c->dstFormat= dstFormat;
2615 c->srcFormat= srcFormat;
2616 c->vRounder= 4* 0x0001000100010001ULL;
2618 usesHFilter= usesVFilter= 0;
2619 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2620 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2621 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2622 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2623 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2624 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2625 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2626 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2628 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2629 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2631 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2632 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2634 // drop some chroma lines if the user wants it
2635 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2636 c->chrSrcVSubSample+= c->vChrDrop;
2638 // drop every other pixel for chroma calculation unless user wants full chroma
2639 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2640 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2641 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2642 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2643 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2644 c->chrSrcHSubSample=1;
2647 c->param[0] = param[0];
2648 c->param[1] = param[1];
2651 c->param[1] = SWS_PARAM_DEFAULT;
2654 // Note the -((-x)>>y) is so that we always round toward +inf.
2655 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2656 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2657 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2658 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2660 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2662 /* unscaled special cases */
2663 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat))) {
2665 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
2666 c->swScale= PlanarToNV12Wrapper;
2669 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2670 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
2671 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2674 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
2675 c->swScale= yvu9toyv12Wrapper;
2679 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2680 c->swScale= bgr24toyv12Wrapper;
2682 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2683 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2684 && (isBGR(dstFormat) || isRGB(dstFormat))
2685 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2686 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2687 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2688 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2689 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2690 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2691 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2692 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2693 && dstFormat != PIX_FMT_RGB32_1
2694 && dstFormat != PIX_FMT_BGR32_1
2695 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
2696 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
2697 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2698 c->swScale= rgb2rgbWrapper;
2700 if ((usePal(srcFormat) && (
2701 dstFormat == PIX_FMT_RGB32 ||
2702 dstFormat == PIX_FMT_RGB32_1 ||
2703 dstFormat == PIX_FMT_RGB24 ||
2704 dstFormat == PIX_FMT_BGR32 ||
2705 dstFormat == PIX_FMT_BGR32_1 ||
2706 dstFormat == PIX_FMT_BGR24)))
2707 c->swScale= pal2rgbWrapper;
2709 if (srcFormat == PIX_FMT_YUV422P) {
2710 if (dstFormat == PIX_FMT_YUYV422)
2711 c->swScale= YUV422PToYuy2Wrapper;
2712 else if (dstFormat == PIX_FMT_UYVY422)
2713 c->swScale= YUV422PToUyvyWrapper;
2716 /* LQ converters if -sws 0 or -sws 4*/
2717 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
2719 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
2720 if (dstFormat == PIX_FMT_YUYV422)
2721 c->swScale= PlanarToYuy2Wrapper;
2722 else if (dstFormat == PIX_FMT_UYVY422)
2723 c->swScale= PlanarToUyvyWrapper;
2726 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2727 c->swScale= YUYV2YUV420Wrapper;
2728 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2729 c->swScale= UYVY2YUV420Wrapper;
2730 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2731 c->swScale= YUYV2YUV422Wrapper;
2732 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2733 c->swScale= UYVY2YUV422Wrapper;
2735 #ifdef COMPILE_ALTIVEC
2736 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2737 !(c->flags & SWS_BITEXACT) &&
2738 srcFormat == PIX_FMT_YUV420P) {
2739 // unscaled YV12 -> packed YUV, we want speed
2740 if (dstFormat == PIX_FMT_YUYV422)
2741 c->swScale= yv12toyuy2_unscaled_altivec;
2742 else if (dstFormat == PIX_FMT_UYVY422)
2743 c->swScale= yv12touyvy_unscaled_altivec;
2748 if ( srcFormat == dstFormat
2749 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2750 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2751 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2752 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2753 || (isGray(dstFormat) && isGray(srcFormat))
2754 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2755 && c->chrDstHSubSample == c->chrSrcHSubSample
2756 && c->chrDstVSubSample == c->chrSrcVSubSample
2757 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
2758 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
2760 if (isPacked(c->srcFormat))
2761 c->swScale= packedCopy;
2762 else /* Planar YUV or gray */
2763 c->swScale= planarCopy;
2766 if (flags & SWS_CPU_CAPS_BFIN)
2767 ff_bfin_get_unscaled_swscale (c);
2771 if (flags&SWS_PRINT_INFO)
2772 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2773 sws_format_name(srcFormat), sws_format_name(dstFormat));
2778 if (flags & SWS_CPU_CAPS_MMX2) {
2779 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2780 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
2781 if (flags&SWS_PRINT_INFO)
2782 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2784 if (usesHFilter) c->canMMX2BeUsed=0;
2789 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2790 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2792 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2793 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2794 // n-2 is the last chrominance sample available
2795 // this is not perfect, but no one should notice the difference, the more correct variant
2796 // would be like the vertical one, but that would require some special code for the
2797 // first and last pixel
2798 if (flags&SWS_FAST_BILINEAR) {
2799 if (c->canMMX2BeUsed) {
2803 //we don't use the x86 asm scaler if MMX is available
2804 else if (flags & SWS_CPU_CAPS_MMX) {
2805 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2806 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2810 /* precalculate horizontal scaler filter coefficients */
2812 const int filterAlign=
2813 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2814 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2817 if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2818 srcW , dstW, filterAlign, 1<<14,
2819 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2820 srcFilter->lumH, dstFilter->lumH, c->param) < 0)
2822 if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2823 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2824 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2825 srcFilter->chrH, dstFilter->chrH, c->param) < 0)
2828 #if defined(COMPILE_MMX2)
2829 // can't downscale !!!
2830 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
2831 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
2832 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
2834 #ifdef MAP_ANONYMOUS
2835 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2836 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2837 #elif HAVE_VIRTUALALLOC
2838 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2839 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2841 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
2842 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
2845 FF_ALLOCZ_OR_GOTO(c, c->lumMmx2Filter , (dstW /8+8)*sizeof(int16_t), fail);
2846 FF_ALLOCZ_OR_GOTO(c, c->chrMmx2Filter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
2847 FF_ALLOCZ_OR_GOTO(c, c->lumMmx2FilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
2848 FF_ALLOCZ_OR_GOTO(c, c->chrMmx2FilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
2850 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2851 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2853 #ifdef MAP_ANONYMOUS
2854 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2855 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
2858 #endif /* defined(COMPILE_MMX2) */
2859 } // initialize horizontal stuff
2863 /* precalculate vertical scaler filter coefficients */
2865 const int filterAlign=
2866 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2867 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2870 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2871 srcH , dstH, filterAlign, (1<<12),
2872 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2873 srcFilter->lumV, dstFilter->lumV, c->param) < 0)
2875 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2876 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2877 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2878 srcFilter->chrV, dstFilter->chrV, c->param) < 0)
2881 #ifdef COMPILE_ALTIVEC
2882 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
2883 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
2885 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2887 short *p = (short *)&c->vYCoeffsBank[i];
2889 p[j] = c->vLumFilter[i];
2892 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2894 short *p = (short *)&c->vCCoeffsBank[i];
2896 p[j] = c->vChrFilter[i];
2901 // calculate buffer sizes so that they won't run out while handling these damn slices
2902 c->vLumBufSize= c->vLumFilterSize;
2903 c->vChrBufSize= c->vChrFilterSize;
2904 for (i=0; i<dstH; i++) {
2905 int chrI= i*c->chrDstH / dstH;
2906 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2907 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2909 nextSlice>>= c->chrSrcVSubSample;
2910 nextSlice<<= c->chrSrcVSubSample;
2911 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2912 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2913 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2914 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2917 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2918 // allocate several megabytes to handle all possible cases)
2919 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2920 FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
2921 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2922 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
2923 //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)
2924 /* align at 16 bytes for AltiVec */
2925 for (i=0; i<c->vLumBufSize; i++) {
2926 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
2927 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
2929 for (i=0; i<c->vChrBufSize; i++) {
2930 FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
2931 c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
2933 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2934 for (i=0; i<c->vLumBufSize; i++) {
2935 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
2936 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
2939 //try to avoid drawing green stuff between the right end and the stride end
2940 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2942 assert(2*VOFW == VOF);
2944 assert(c->chrDstH <= dstH);
2946 if (flags&SWS_PRINT_INFO) {
2948 const char *dither= " dithered";
2950 const char *dither= "";
2952 if (flags&SWS_FAST_BILINEAR)
2953 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2954 else if (flags&SWS_BILINEAR)
2955 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2956 else if (flags&SWS_BICUBIC)
2957 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2958 else if (flags&SWS_X)
2959 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2960 else if (flags&SWS_POINT)
2961 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2962 else if (flags&SWS_AREA)
2963 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2964 else if (flags&SWS_BICUBLIN)
2965 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2966 else if (flags&SWS_GAUSS)
2967 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2968 else if (flags&SWS_SINC)
2969 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2970 else if (flags&SWS_LANCZOS)
2971 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2972 else if (flags&SWS_SPLINE)
2973 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2975 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2977 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2978 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2979 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2981 av_log(c, AV_LOG_INFO, "from %s to %s ",
2982 sws_format_name(srcFormat), sws_format_name(dstFormat));
2984 if (flags & SWS_CPU_CAPS_MMX2)
2985 av_log(c, AV_LOG_INFO, "using MMX2\n");
2986 else if (flags & SWS_CPU_CAPS_3DNOW)
2987 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2988 else if (flags & SWS_CPU_CAPS_MMX)
2989 av_log(c, AV_LOG_INFO, "using MMX\n");
2990 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2991 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2993 av_log(c, AV_LOG_INFO, "using C\n");
2996 if (flags & SWS_PRINT_INFO) {
2997 if (flags & SWS_CPU_CAPS_MMX) {
2998 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2999 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
3001 if (c->hLumFilterSize==4)
3002 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
3003 else if (c->hLumFilterSize==8)
3004 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
3006 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
3008 if (c->hChrFilterSize==4)
3009 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
3010 else if (c->hChrFilterSize==8)
3011 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
3013 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
3017 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
3019 if (flags & SWS_FAST_BILINEAR)
3020 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
3022 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
3025 if (isPlanarYUV(dstFormat)) {
3026 if (c->vLumFilterSize==1)
3027 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3029 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3031 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
3032 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
3033 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3034 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
3035 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3037 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3040 if (dstFormat==PIX_FMT_BGR24)
3041 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
3042 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
3043 else if (dstFormat==PIX_FMT_RGB32)
3044 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3045 else if (dstFormat==PIX_FMT_BGR565)
3046 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3047 else if (dstFormat==PIX_FMT_BGR555)
3048 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
3050 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
3052 if (flags & SWS_PRINT_INFO) {
3053 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3054 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
3055 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
3056 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
3059 c->swScale= getSwsFunc(c);
3067 static void reset_ptr(uint8_t* src[], int format)
3069 if(!isALPHA(format))
3071 if(!isPlanarYUV(format)) {
3073 if( format != PIX_FMT_PAL8
3074 && format != PIX_FMT_RGB8
3075 && format != PIX_FMT_BGR8
3076 && format != PIX_FMT_RGB4_BYTE
3077 && format != PIX_FMT_BGR4_BYTE
3084 * swscale wrapper, so we don't need to export the SwsContext.
3085 * Assumes planar YUV to be in YUV order instead of YVU.
3087 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3088 int srcSliceH, uint8_t* dst[], int dstStride[])
3091 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
3092 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
3094 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3095 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
3098 if (c->sliceDir == 0) {
3099 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
3102 if (usePal(c->srcFormat)) {
3103 for (i=0; i<256; i++) {
3104 int p, r, g, b,y,u,v;
3105 if(c->srcFormat == PIX_FMT_PAL8) {
3106 p=((uint32_t*)(src[1]))[i];
3110 } else if(c->srcFormat == PIX_FMT_RGB8) {
3114 } else if(c->srcFormat == PIX_FMT_BGR8) {
3118 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
3123 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
3128 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3129 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3130 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
3131 c->pal_yuv[i]= y + (u<<8) + (v<<16);
3134 switch(c->dstFormat) {
3139 c->pal_rgb[i]= r + (g<<8) + (b<<16);
3141 case PIX_FMT_BGR32_1:
3145 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
3147 case PIX_FMT_RGB32_1:
3151 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
3158 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3163 // copy strides, so they can safely be modified
3164 if (c->sliceDir == 1) {
3165 // slices go from top to bottom
3166 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3167 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3169 reset_ptr(src2, c->srcFormat);
3170 reset_ptr(dst2, c->dstFormat);
3172 /* reset slice direction at end of frame */
3173 if (srcSliceY + srcSliceH == c->srcH)
3176 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3178 // slices go from bottom to top => we flip the image internally
3179 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3180 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3182 src2[0] += (srcSliceH-1)*srcStride[0];
3183 if (!usePal(c->srcFormat))
3184 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3185 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3186 src2[3] += (srcSliceH-1)*srcStride[3];
3187 dst2[0] += ( c->dstH -1)*dstStride[0];
3188 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3189 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3190 dst2[3] += ( c->dstH -1)*dstStride[3];
3192 reset_ptr(src2, c->srcFormat);
3193 reset_ptr(dst2, c->dstFormat);
3195 /* reset slice direction at end of frame */
3199 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3203 #if LIBSWSCALE_VERSION_MAJOR < 1
3204 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3205 int srcSliceH, uint8_t* dst[], int dstStride[])
3207 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3211 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3212 float lumaSharpen, float chromaSharpen,
3213 float chromaHShift, float chromaVShift,
3216 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3220 if (lumaGBlur!=0.0) {
3221 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3222 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3224 filter->lumH= sws_getIdentityVec();
3225 filter->lumV= sws_getIdentityVec();
3228 if (chromaGBlur!=0.0) {
3229 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3230 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3232 filter->chrH= sws_getIdentityVec();
3233 filter->chrV= sws_getIdentityVec();
3236 if (chromaSharpen!=0.0) {
3237 SwsVector *id= sws_getIdentityVec();
3238 sws_scaleVec(filter->chrH, -chromaSharpen);
3239 sws_scaleVec(filter->chrV, -chromaSharpen);
3240 sws_addVec(filter->chrH, id);
3241 sws_addVec(filter->chrV, id);
3245 if (lumaSharpen!=0.0) {
3246 SwsVector *id= sws_getIdentityVec();
3247 sws_scaleVec(filter->lumH, -lumaSharpen);
3248 sws_scaleVec(filter->lumV, -lumaSharpen);
3249 sws_addVec(filter->lumH, id);
3250 sws_addVec(filter->lumV, id);
3254 if (chromaHShift != 0.0)
3255 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3257 if (chromaVShift != 0.0)
3258 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3260 sws_normalizeVec(filter->chrH, 1.0);
3261 sws_normalizeVec(filter->chrV, 1.0);
3262 sws_normalizeVec(filter->lumH, 1.0);
3263 sws_normalizeVec(filter->lumV, 1.0);
3265 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3266 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3271 SwsVector *sws_allocVec(int length)
3273 SwsVector *vec = av_malloc(sizeof(SwsVector));
3276 vec->length = length;
3277 vec->coeff = av_malloc(sizeof(double) * length);
3283 SwsVector *sws_getGaussianVec(double variance, double quality)
3285 const int length= (int)(variance*quality + 0.5) | 1;
3287 double middle= (length-1)*0.5;
3288 SwsVector *vec= sws_allocVec(length);
3293 for (i=0; i<length; i++) {
3294 double dist= i-middle;
3295 vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3298 sws_normalizeVec(vec, 1.0);
3303 SwsVector *sws_getConstVec(double c, int length)
3306 SwsVector *vec= sws_allocVec(length);
3311 for (i=0; i<length; i++)
3318 SwsVector *sws_getIdentityVec(void)
3320 return sws_getConstVec(1.0, 1);
3323 double sws_dcVec(SwsVector *a)
3328 for (i=0; i<a->length; i++)
3334 void sws_scaleVec(SwsVector *a, double scalar)
3338 for (i=0; i<a->length; i++)
3339 a->coeff[i]*= scalar;
3342 void sws_normalizeVec(SwsVector *a, double height)
3344 sws_scaleVec(a, height/sws_dcVec(a));
3347 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
3349 int length= a->length + b->length - 1;
3351 SwsVector *vec= sws_getConstVec(0.0, length);
3356 for (i=0; i<a->length; i++) {
3357 for (j=0; j<b->length; j++) {
3358 vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
3365 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
3367 int length= FFMAX(a->length, b->length);
3369 SwsVector *vec= sws_getConstVec(0.0, length);
3374 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3375 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3380 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
3382 int length= FFMAX(a->length, b->length);
3384 SwsVector *vec= sws_getConstVec(0.0, length);
3389 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3390 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3395 /* shift left / or right if "shift" is negative */
3396 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
3398 int length= a->length + FFABS(shift)*2;
3400 SwsVector *vec= sws_getConstVec(0.0, length);
3405 for (i=0; i<a->length; i++) {
3406 vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3412 void sws_shiftVec(SwsVector *a, int shift)
3414 SwsVector *shifted= sws_getShiftedVec(a, shift);
3416 a->coeff= shifted->coeff;
3417 a->length= shifted->length;
3421 void sws_addVec(SwsVector *a, SwsVector *b)
3423 SwsVector *sum= sws_sumVec(a, b);
3425 a->coeff= sum->coeff;
3426 a->length= sum->length;
3430 void sws_subVec(SwsVector *a, SwsVector *b)
3432 SwsVector *diff= sws_diffVec(a, b);
3434 a->coeff= diff->coeff;
3435 a->length= diff->length;
3439 void sws_convVec(SwsVector *a, SwsVector *b)
3441 SwsVector *conv= sws_getConvVec(a, b);
3443 a->coeff= conv->coeff;
3444 a->length= conv->length;
3448 SwsVector *sws_cloneVec(SwsVector *a)
3451 SwsVector *vec= sws_allocVec(a->length);
3456 for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
3461 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
3468 for (i=0; i<a->length; i++)
3469 if (a->coeff[i]>max) max= a->coeff[i];
3471 for (i=0; i<a->length; i++)
3472 if (a->coeff[i]<min) min= a->coeff[i];
3476 for (i=0; i<a->length; i++) {
3477 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3478 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3479 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3480 av_log(log_ctx, log_level, "|\n");
3484 #if LIBSWSCALE_VERSION_MAJOR < 1
3485 void sws_printVec(SwsVector *a)
3487 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3491 void sws_freeVec(SwsVector *a)
3494 av_freep(&a->coeff);
3499 void sws_freeFilter(SwsFilter *filter)
3501 if (!filter) return;
3503 if (filter->lumH) sws_freeVec(filter->lumH);
3504 if (filter->lumV) sws_freeVec(filter->lumV);
3505 if (filter->chrH) sws_freeVec(filter->chrH);
3506 if (filter->chrV) sws_freeVec(filter->chrV);
3511 void sws_freeContext(SwsContext *c)
3517 for (i=0; i<c->vLumBufSize; i++)
3518 av_freep(&c->lumPixBuf[i]);
3519 av_freep(&c->lumPixBuf);
3523 for (i=0; i<c->vChrBufSize; i++)
3524 av_freep(&c->chrPixBuf[i]);
3525 av_freep(&c->chrPixBuf);
3528 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
3529 for (i=0; i<c->vLumBufSize; i++)
3530 av_freep(&c->alpPixBuf[i]);
3531 av_freep(&c->alpPixBuf);
3534 av_freep(&c->vLumFilter);
3535 av_freep(&c->vChrFilter);
3536 av_freep(&c->hLumFilter);
3537 av_freep(&c->hChrFilter);
3538 #ifdef COMPILE_ALTIVEC
3539 av_freep(&c->vYCoeffsBank);
3540 av_freep(&c->vCCoeffsBank);
3543 av_freep(&c->vLumFilterPos);
3544 av_freep(&c->vChrFilterPos);
3545 av_freep(&c->hLumFilterPos);
3546 av_freep(&c->hChrFilterPos);
3548 #if ARCH_X86 && CONFIG_GPL
3549 #ifdef MAP_ANONYMOUS
3550 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
3551 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
3552 #elif HAVE_VIRTUALALLOC
3553 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
3554 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
3556 av_free(c->lumMmx2FilterCode);
3557 av_free(c->chrMmx2FilterCode);
3559 c->lumMmx2FilterCode=NULL;
3560 c->chrMmx2FilterCode=NULL;
3561 #endif /* ARCH_X86 && CONFIG_GPL */
3563 av_freep(&c->lumMmx2Filter);
3564 av_freep(&c->chrMmx2Filter);
3565 av_freep(&c->lumMmx2FilterPos);
3566 av_freep(&c->chrMmx2FilterPos);
3567 av_freep(&c->yuvTable);
3572 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3573 int srcW, int srcH, enum PixelFormat srcFormat,
3574 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3575 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
3577 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
3580 param = default_param;
3583 if (context->srcW != srcW || context->srcH != srcH ||
3584 context->srcFormat != srcFormat ||
3585 context->dstW != dstW || context->dstH != dstH ||
3586 context->dstFormat != dstFormat || context->flags != flags ||
3587 context->param[0] != param[0] || context->param[1] != param[1])
3589 sws_freeContext(context);
3594 return sws_getContext(srcW, srcH, srcFormat,
3595 dstW, dstH, dstFormat, flags,
3596 srcFilter, dstFilter, param);